[gedit] Do not use gloabal vars for the command line parsing



commit 0e380005b0b98b9d34d6ff820f3f6e450cc9921a
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Aug 12 11:07:59 2014 +0200

    Do not use gloabal vars for the command line parsing
    
    Also factor out an open_files helper, this will be used to also
    implement application::open in the next commit

 gedit/gedit-app.c |  122 ++++++++++++++++++++++++++++++++++-------------------
 gedit/gedit.c     |    2 +-
 2 files changed, 79 insertions(+), 45 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 81b2520..8ce0879 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -83,17 +83,18 @@ struct _GeditAppPrivate
 
        PeasExtensionSet  *extensions;
        GNetworkMonitor   *monitor;
-};
 
-static gboolean new_window = FALSE;
-static gboolean new_document = FALSE;
-static gchar *geometry = NULL;
-static const GtkSourceEncoding *encoding = NULL;
-static GInputStream *stdin_stream = NULL;
-static GSList *file_list = NULL;
-static gint line_position = 0;
-static gint column_position = 0;
-static GApplicationCommandLine *command_line = NULL;
+       /* command line parsing */
+       gboolean new_window;
+       gboolean new_document;
+       gchar *geometry;
+       const GtkSourceEncoding *encoding;
+       GInputStream *stdin_stream;
+       GSList *file_list;
+       gint line_position;
+       gint column_position;
+       GApplicationCommandLine *command_line;
+};
 
 static const GOptionEntry options[] =
 {
@@ -710,16 +711,26 @@ get_active_window (GtkApplication *app)
 }
 
 static void
-set_command_line_wait (GeditTab *tab)
+set_command_line_wait (GeditApp *app,
+                      GeditTab *tab)
 {
        g_object_set_data_full (G_OBJECT (tab),
                                "GeditTabCommandLineWait",
-                               g_object_ref (command_line),
+                               g_object_ref (app->priv->command_line),
                                (GDestroyNotify)g_object_unref);
 }
 
 static void
-gedit_app_activate (GApplication *application)
+open_files (GApplication            *application,
+           gboolean                 new_window,
+           gboolean                 new_document,
+           gchar                   *geometry,
+           gint                     line_position,
+           gint                     column_position,
+           const GtkSourceEncoding *encoding,
+           GInputStream            *stdin_stream,
+           GSList                  *file_list,
+           GApplicationCommandLine *command_line)
 {
        GeditWindow *window = NULL;
        GeditTab *tab;
@@ -741,8 +752,7 @@ gedit_app_activate (GApplication *application)
 
        if (geometry)
        {
-               gtk_window_parse_geometry (GTK_WINDOW (window),
-                                          geometry);
+               gtk_window_parse_geometry (GTK_WINDOW (window), geometry);
        }
 
        if (stdin_stream)
@@ -759,7 +769,8 @@ gedit_app_activate (GApplication *application)
 
                if (doc_created && command_line)
                {
-                       set_command_line_wait (tab);
+                       set_command_line_wait (GEDIT_APP (application),
+                                              tab);
                }
                g_input_stream_close (stdin_stream, NULL, NULL);
        }
@@ -791,7 +802,8 @@ gedit_app_activate (GApplication *application)
 
                if (command_line)
                {
-                       set_command_line_wait (tab);
+                       set_command_line_wait (GEDIT_APP (application),
+                                              tab);
                }
        }
 
@@ -799,20 +811,39 @@ gedit_app_activate (GApplication *application)
 }
 
 static void
-clear_options (void)
+gedit_app_activate (GApplication *application)
 {
-       g_free (geometry);
-       g_clear_object (&stdin_stream);
-       g_slist_free_full (file_list, g_object_unref);
+       GeditAppPrivate *priv = GEDIT_APP (application)->priv;
+
+       open_files (application,
+                   priv->new_window,
+                   priv->new_document,
+                   priv->geometry,
+                   priv->line_position,
+                   priv->column_position,
+                   priv->encoding,
+                   priv->stdin_stream,
+                   priv->file_list,
+                   priv->command_line);
+}
 
-       new_window = FALSE;
-       new_document = FALSE;
-       geometry = NULL;
-       encoding = NULL;
-       file_list = NULL;
-       line_position = 0;
-       column_position = 0;
-       command_line = NULL;
+static void
+clear_options (GeditApp *app)
+{
+       GeditAppPrivate *priv = app->priv;
+
+       g_free (priv->geometry);
+       g_clear_object (&priv->stdin_stream);
+       g_slist_free_full (priv->file_list, g_object_unref);
+
+       priv->new_window = FALSE;
+       priv->new_document = FALSE;
+       priv->geometry = NULL;
+       priv->encoding = NULL;
+       priv->file_list = NULL;
+       priv->line_position = 0;
+       priv->column_position = 0;
+       priv->command_line = NULL;
 }
 
 static void
@@ -844,26 +875,29 @@ static gint
 gedit_app_command_line (GApplication            *application,
                         GApplicationCommandLine *cl)
 {
+       GeditAppPrivate *priv;
        GVariantDict *options;
        const gchar *encoding_charset;
        const gchar **remaining_args;
 
+       priv = GEDIT_APP (application)->priv;
+
        options = g_application_command_line_get_options_dict (cl);
 
-       g_variant_dict_lookup (options, "new-window", "b", &new_window);
-       g_variant_dict_lookup (options, "new-document", "b", &new_document);
-       g_variant_dict_lookup (options, "geometry", "s", &geometry);
+       g_variant_dict_lookup (options, "new-window", "b", &priv->new_window);
+       g_variant_dict_lookup (options, "new-document", "b", &priv->new_document);
+       g_variant_dict_lookup (options, "geometry", "s", &priv->geometry);
 
        if (g_variant_dict_contains (options, "wait"))
        {
-               command_line = cl;
+               priv->command_line = cl;
        }
 
        if (g_variant_dict_lookup (options, "encoding", "&s", &encoding_charset))
        {
-               encoding = gtk_source_encoding_get_from_charset (encoding_charset);
+               priv->encoding = gtk_source_encoding_get_from_charset (encoding_charset);
 
-               if (encoding == NULL)
+               if (priv->encoding == NULL)
                {
                        g_application_command_line_printerr (cl,
                                                             _("%s: invalid encoding."),
@@ -883,35 +917,35 @@ gedit_app_command_line (GApplication            *application,
                                if (*(remaining_args[i] + 1) == '\0')
                                {
                                        /* goto the last line of the document */
-                                       line_position = G_MAXINT;
-                                       column_position = 0;
+                                       priv->line_position = G_MAXINT;
+                                       priv->column_position = 0;
                                }
                                else
                                {
                                        get_line_column_position (remaining_args[i] + 1,
-                                                                 &line_position,
-                                                                 &column_position);
+                                                                 &priv->line_position,
+                                                                 &priv->column_position);
                                }
                        }
                        else if (*remaining_args[i] == '-' && *(remaining_args[i] + 1) == '\0')
                        {
-                               stdin_stream = g_application_command_line_get_stdin (cl);
+                               priv->stdin_stream = g_application_command_line_get_stdin (cl);
                        }
                        else
                        {
                                GFile *file;
 
                                file = g_application_command_line_create_file_for_arg (cl, remaining_args[i]);
-                               file_list = g_slist_prepend (file_list, file);
+                               priv->file_list = g_slist_prepend (priv->file_list, file);
                        }
                }
 
-               file_list = g_slist_reverse (file_list);
+               priv->file_list = g_slist_reverse (priv->file_list);
                g_free (remaining_args);
        }
 
        g_application_activate (application);
-       clear_options ();
+       clear_options (GEDIT_APP (application));
 
        return 0;
 }
diff --git a/gedit/gedit.c b/gedit/gedit.c
index dc065c2..619446f 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -127,7 +127,7 @@ main (int argc, char *argv[])
 
        app = g_object_new (type,
                            "application-id", "org.gnome.gedit",
-                           "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
+                           "flags", G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
                            NULL);
 
        status = g_application_run (G_APPLICATION (app), argc, argv);


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