[libgda] GdaBrowser: application lifecycle corrections



commit d31ead7a9db698bcf551d8c759118eb0796fe65a
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Sep 23 20:48:29 2014 +0200

    GdaBrowser: application lifecycle corrections

 tools/browser/browser-window.c |    1 +
 tools/common/t-app.c           |   10 +-
 tools/tools-main.c             |  218 ++++++++++++++++++++++------------------
 3 files changed, 127 insertions(+), 102 deletions(-)
---
diff --git a/tools/browser/browser-window.c b/tools/browser/browser-window.c
index 1724b3f..79685ac 100644
--- a/tools/browser/browser-window.c
+++ b/tools/browser/browser-window.c
@@ -364,6 +364,7 @@ browser_window_new (TConnection *tcnc, BrowserPerspectiveFactory *factory)
        g_return_val_if_fail (T_IS_CONNECTION (tcnc), NULL);
 
        bwin = BROWSER_WINDOW (g_object_new (BROWSER_TYPE_WINDOW, "application", t_app_get(), NULL));
+       gtk_application_add_window (GTK_APPLICATION (t_app_get ()), GTK_WINDOW (bwin));
        bwin->priv->tcnc = g_object_ref (tcnc);
        bwin->priv->trans_status_sigid = gda_signal_connect (tcnc, "transaction-status-changed",
                                                             G_CALLBACK (transaction_status_changed_cb), bwin,
diff --git a/tools/common/t-app.c b/tools/common/t-app.c
index 9284e8b..9f52a59 100644
--- a/tools/common/t-app.c
+++ b/tools/common/t-app.c
@@ -313,13 +313,11 @@ idle_create_window (GApplication *app)
        gtk_window_set_default_size ((GtkWindow*) bwin, 640, 480);
 #define PRGNAME "Gda Browser"
        gtk_window_set_title (GTK_WINDOW (bwin), PRGNAME);
-
        gtk_widget_show (bwin);
+
        g_application_release (app); /* release now that the 1st window has been created, cf. 
t_app_add_feature() where
                                      * g_application_hold() is called */
 
-       global_t_app->priv->features |= T_APP_BROWSER;
-
        g_object_unref (G_OBJECT (global_t_app));
 
        return FALSE;
@@ -350,10 +348,12 @@ t_app_add_feature (TAppFeatures feature)
        }
 
 #ifdef HAVE_GTK_CLASSES
-       if (feature & T_APP_BROWSER) {
+       if ((feature & T_APP_BROWSER) &&
+           !(global_t_app->priv->features & T_APP_BROWSER)) {
                g_application_hold (G_APPLICATION (global_t_app)); /* up until the 1st window is created */
                g_object_ref (G_OBJECT (global_t_app));
                g_idle_add ((GSourceFunc) idle_create_window, global_t_app);
+               global_t_app->priv->features |= T_APP_BROWSER;
        }
 #endif
 
@@ -578,7 +578,7 @@ t_app_open_connections (gint argc, const gchar *argv[], GError **error)
                if (info)
                        str = g_strdup (info->name);
                else
-                       str = g_strdup_printf ("c%d", i-1);
+                       str = g_strdup_printf ("c%d", i);
                if (!ostream) {
                        gchar *params, *prov, *user;
                        gda_connection_string_split (argv[i], &params, &prov, &user, NULL);
diff --git a/tools/tools-main.c b/tools/tools-main.c
index 5ac11a0..dd7e8ea 100644
--- a/tools/tools-main.c
+++ b/tools/tools-main.c
@@ -116,7 +116,7 @@ static GOptionEntry entries[] = {
 };
 
 
-static void output_data_model (TContext *console, GdaDataModel *model);
+static void output_data_model (GdaDataModel *model);
 
 static gboolean
 ticker (G_GNUC_UNUSED gpointer data)
@@ -193,11 +193,12 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
        GError *error = NULL;
        int exit_status = EXIT_SUCCESS;
        gboolean is_interactive = TRUE; /* final interactivity status */
+       gboolean nocnc = FALSE;
 #ifdef IS_BROWSER
+       gboolean ui = TRUE;
        is_interactive = FALSE;
-#else
-       t_app_add_feature (T_APP_TERM_CONSOLE);
 #endif
+       t_app_add_feature (T_APP_TERM_CONSOLE);
 
        /* options parsing */
        context = g_option_context_new (_("[DSN|connection string]..."));        
@@ -206,12 +207,14 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
         if (!g_option_context_parse (context, &argc, &argv, &error)) {
                 g_fprintf  (stderr, "Can't parse arguments: %s\n", error->message);
                exit_status = EXIT_FAILURE;
+               t_app_request_quit ();
                goto out;
         }
         g_option_context_free (context);
 
        if (show_version) {
                g_application_command_line_print (cmdline, _("GDA SQL console version " PACKAGE_VERSION 
"\n"));
+               t_app_request_quit ();
                goto out;
        }
 
@@ -221,29 +224,31 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
        if (list_providers ||
            list_configs ||
            list_data_files ||
-           purge_data_files)
+           purge_data_files) {
                is_interactive = FALSE;
-       if (is_interactive)
-               t_app_add_feature (T_APP_TERM_CONSOLE);
+#ifdef IS_BROWSER
+               ui = FALSE;
+#endif
+               nocnc = TRUE;
+       }
 
        TContext *term_console;
        FILE *ostream = NULL;
        term_console = t_app_get_term_console ();
-       if (term_console) {
-               t_term_context_set_interactive (T_TERM_CONTEXT (term_console), is_interactive);
-
-               /* output file */
-               if (outfile) {
-                       if (! t_context_set_output_file (term_console, outfile, &error)) {
-                               g_print ("Can't set output file as '%s': %s\n", outfile,
-                                        error->message);
-                               exit_status = EXIT_FAILURE;
-                               goto out;
-                       }
+       g_assert (term_console);
+       t_term_context_set_interactive (T_TERM_CONTEXT (term_console), is_interactive);
+
+       /* output file */
+       if (outfile) {
+               if (! t_context_set_output_file (term_console, outfile, &error)) {
+                       g_print ("Can't set output file as '%s': %s\n", outfile,
+                                error->message);
+                       exit_status = EXIT_FAILURE;
+                       t_app_request_quit ();
+                       goto out;
                }
-
-               ostream = t_context_get_output_stream (term_console, NULL);
        }
+       ostream = t_context_get_output_stream (term_console, NULL);
 
        /* welcome message */
        if (is_interactive && !ostream) {
@@ -258,8 +263,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
 #endif
                gchar *c1, *c2, *c3, *c4;
                ToolOutputFormat oformat = BASE_TOOL_OUTPUT_FORMAT_DEFAULT;
-               if (term_console)
-                       oformat = t_context_get_output_format (term_console);
+               oformat = t_context_get_output_format (term_console);
 
 #ifdef IS_BROWSER
   #define PRGNAME "Gda Browser"
@@ -291,7 +295,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                }
                else {
                        GdaDataModel *model = t_config_info_list_all_providers ();
-                       output_data_model (term_console, model);
+                       output_data_model (model);
                        g_object_unref (model);
                }
        }
@@ -302,7 +306,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                }
                else {
                        GdaDataModel *model = t_config_info_list_all_dsn ();
-                       output_data_model (term_console, model);
+                       output_data_model (model);
                        g_object_unref (model);
                }
        }
@@ -315,7 +319,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                g_free (confdir);
                model = t_config_info_list_data_files (&error);
                if (model) {
-                       output_data_model (term_console, model);
+                       output_data_model (model);
                        g_object_unref (model);
                }
                else
@@ -335,22 +339,21 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                                 error->message);       
        }
 
-       if (term_console) {
-               /* commands file */
-               if (commandsfile) {
-                       if (! t_term_context_set_input_file (T_TERM_CONTEXT (term_console), commandsfile, 
&error)) {
-                               g_print ("Can't read file '%s': %s\n", commandsfile,
-                                        error->message);
-                               exit_status = EXIT_FAILURE;
-                               goto out;
-                       }
-               }
-               else {
-                       /* check if stdin is a term */
-                       if (!isatty (fileno (stdin))) 
-                               t_term_context_set_input_stream (T_TERM_CONTEXT (term_console), stdin);
+       /* commands file */
+       if (commandsfile) {
+               if (! t_term_context_set_input_file (T_TERM_CONTEXT (term_console), commandsfile, &error)) {
+                       g_print ("Can't read file '%s': %s\n", commandsfile,
+                                error->message);
+                       exit_status = EXIT_FAILURE;
+                       t_app_request_quit ();
+                       goto out;
                }
        }
+       else {
+               /* check if stdin is a term */
+               if (!isatty (fileno (stdin)))
+                       t_term_context_set_input_stream (T_TERM_CONTEXT (term_console), stdin);
+       }
 
        /* recreating an argv[] array, one entry per connection to open, and we don't want to include there
         * any SHEBANG command file */
@@ -377,6 +380,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                                                exit_status = EXIT_FAILURE;
                                                fclose (file);
                                                free_strings_array (array);
+                                               t_app_request_quit ();
                                                goto out;
                                        }
 
@@ -387,6 +391,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                                                exit_status = EXIT_FAILURE;
                                                fclose (file);
                                                free_strings_array (array);
+                                               t_app_request_quit ();
                                                goto out;
                                        }
                                        fclose (file);
@@ -416,21 +421,22 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                            NULL, 0, NULL);
 
        /* open connections if specified */
-       if (! t_app_open_connections (array->len, (const gchar **) array->data, &error)) {
-               g_print (_("Error opening connection: %s\n"), error && error->message ? error->message : 
_("No detail"));
-               g_clear_error (&error);
-               exit_status = EXIT_FAILURE;
-               free_strings_array (array);
-               goto out;
+       if (! nocnc) {
+               if (! t_app_open_connections (array->len, (const gchar **) array->data, &error)) {
+                       g_print (_("Error opening connection: %s\n"), error && error->message ? 
error->message : _("No detail"));
+                       g_clear_error (&error);
+                       exit_status = EXIT_FAILURE;
+                       free_strings_array (array);
+                       t_app_request_quit ();
+                       goto out;
+               }
        }
 
-       if (term_console) {
-               GSList *cnclist;
-               cnclist = (GSList*) t_app_get_all_connections ();
-               if (cnclist) {
-                       cnclist = g_slist_last (cnclist);
-                       t_context_set_connection (term_console, T_CONNECTION (cnclist->data));
-               }
+       GSList *cnclist;
+       cnclist = (GSList*) t_app_get_all_connections ();
+       if (cnclist) {
+               cnclist = g_slist_last (cnclist);
+               t_context_set_connection (term_console, T_CONNECTION (cnclist->data));
        }
 
        free_strings_array (array);
@@ -445,6 +451,7 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                        g_print (_("Can't run HTTP server on port %d: %s\n"),
                                 http_port, lerror && lerror->message ? lerror->message : _("No detail"));
                        exit_status = EXIT_FAILURE;
+                       t_app_request_quit ();
                        goto out;
                }
        }
@@ -458,50 +465,62 @@ command_line (GApplication *application, GApplicationCommandLine *cmdline)
                        g_print ("\n");
        }
 
-       if (term_console)
+       if (is_interactive)
                term_console_thread = t_context_run (term_console);
+       else {
+#ifdef IS_BROWSER
+               if (!ui)
+                       t_app_request_quit ();
+#else
+               t_app_request_quit ();
+#endif
+       }
 
 #ifdef IS_BROWSER
-       GtkBuilder *builder;
-       builder = gtk_builder_new ();
-       gtk_builder_add_from_string (builder,
-                                    "<interface>"
-                                    "  <menu id='app-menu'>"
-                                    "    <section>"
-                                    "      <item>"
-                                    "        <attribute name='label' translatable='yes'>_New 
Window</attribute>"
-                                    "        <attribute name='action'>app.new</attribute>"
-                                    "        <attribute name='accel'>&lt;Primary&gt;n</attribute>"
-                                    "      </item>"
-                                    "    </section>"
-                                    "    <section>"
-                                    "      <item>"
-                                    "        <attribute name='label' translatable='yes'>_About 
Bloatpad</attribute>"
-                                    "        <attribute name='action'>app.about</attribute>"
-                                    "      </item>"
-                                    "    </section>"
-                                    "    <section>"
-                                    "      <item>"
-                                    "        <attribute name='label' translatable='yes'>_Quit</attribute>"
-                                    "        <attribute name='action'>app.quit</attribute>"
-                                    "        <attribute name='accel'>&lt;Primary&gt;q</attribute>"
-                                    "      </item>"
-                                    "    </section>"
-                                    "  </menu>"
-                                    "  <menu id='menubar'>"
-                                    "    <submenu label='_Edit'>"
-                                    "      <item label='_Copy' action='win.copy'/>"
-                                    "      <item label='_Paste' action='win.paste'/>"
-                                    "    </submenu>"
-                                    "  </menu>"
-                                    "</interface>", -1, NULL);
-       gtk_application_set_app_menu (GTK_APPLICATION (t_app_get ()),
-                                     G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
-       gtk_application_set_menubar (GTK_APPLICATION (t_app_get ()),
-                                    G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
-       g_object_unref (builder);
-
-       t_app_add_feature (T_APP_BROWSER);
+       if (ui) {
+               GtkBuilder *builder;
+               builder = gtk_builder_new ();
+               gtk_builder_add_from_string (builder,
+                                            "<interface>"
+                                            "  <menu id='app-menu'>"
+                                            "    <section>"
+                                            "      <item>"
+                                            "        <attribute name='label' translatable='yes'>_New 
Window</attribute>"
+                                            "        <attribute name='action'>app.new</attribute>"
+                                            "        <attribute name='accel'>&lt;Primary&gt;n</attribute>"
+                                            "      </item>"
+                                            "    </section>"
+                                            "    <section>"
+                                            "      <item>"
+                                            "        <attribute name='label' translatable='yes'>_About 
Bloatpad</attribute>"
+                                            "        <attribute name='action'>app.about</attribute>"
+                                            "      </item>"
+                                            "    </section>"
+                                            "    <section>"
+                                            "      <item>"
+                                            "        <attribute name='label' 
translatable='yes'>_Quit</attribute>"
+                                            "        <attribute name='action'>app.quit</attribute>"
+                                            "        <attribute name='accel'>&lt;Primary&gt;q</attribute>"
+                                            "      </item>"
+                                            "    </section>"
+                                            "  </menu>"
+                                            "  <menu id='menubar'>"
+                                            "    <submenu label='_Edit'>"
+                                            "      <item label='_Copy' action='win.copy'/>"
+                                            "      <item label='_Paste' action='win.paste'/>"
+                                            "    </submenu>"
+                                            "  </menu>"
+                                            "</interface>", -1, NULL);
+               gtk_application_set_app_menu (GTK_APPLICATION (t_app_get ()),
+                                             G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
+               gtk_application_set_menubar (GTK_APPLICATION (t_app_get ()),
+                                            G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
+               g_object_unref (builder);
+
+               t_app_add_feature (T_APP_BROWSER);
+               if (!is_interactive)
+                       t_app_remove_feature (T_APP_TERM_CONSOLE);
+       }
 #endif
 
  out:
@@ -555,13 +574,18 @@ main (int argc, char *argv[])
  * Dumps the data model contents onto @data->output
  */
 static void
-output_data_model (TContext *console, GdaDataModel *model)
+output_data_model (GdaDataModel *model)
 {
        gchar *str;
-       FILE *ostream;
-       ToolOutputFormat oformat;
-       ostream = t_context_get_output_stream (console, NULL);
-       oformat = t_context_get_output_format (console);
+       FILE *ostream = NULL;
+       ToolOutputFormat oformat = BASE_TOOL_OUTPUT_FORMAT_DEFAULT;
+       TContext *console;
+
+       console = t_app_get_term_console ();
+       if (console) {
+               ostream = t_context_get_output_stream (console, NULL);
+               oformat = t_context_get_output_format (console);
+       }
        str = base_tool_output_data_model_to_string (model, oformat, ostream, t_app_get_options ());
        base_tool_output_output_string (ostream, str);
        g_free (str);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]