[gtranslator] Port to GtkApplication.



commit e877ae03e879744da37481f063b993e6021ad072
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Wed Nov 3 13:26:43 2010 +0100

    Port to GtkApplication.

 configure.ac                           |    1 -
 src/gtr-application.c                  |  221 +++++++++++++++++++++++---------
 src/gtr-application.h                  |    5 +-
 src/gtr-settings.c                     |    6 +-
 src/gtr-window.c                       |    2 +
 src/main.c                             |  214 +------------------------------
 src/plugin-system/gtr-plugins-engine.c |    4 +-
 7 files changed, 169 insertions(+), 284 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 4e42fba..e045c2f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -105,7 +105,6 @@ PKG_CHECK_MODULES(GTRANSLATOR, [
 	gtksourceview-3.0 >= $SOURCEVIEW_REQUIRED
 	gdl-3.0 >= $GDL_REQUIRED
 	gsettings-desktop-schemas
-	unique-3.0 >= 2.90.1
 	libgda-4.0 >= 4.2.0
 ])
 
diff --git a/src/gtr-application.c b/src/gtr-application.c
index dd1c783..44b9db4 100644
--- a/src/gtr-application.c
+++ b/src/gtr-application.c
@@ -42,12 +42,16 @@
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
+#ifdef GDK_WINDOWING_X11
+#include <gdk/gdkx.h>
+#endif
+
 #define GTR_APPLICATION_GET_PRIVATE(object)	(G_TYPE_INSTANCE_GET_PRIVATE ( \
 					 (object),	\
 					 GTR_TYPE_APPLICATION,     \
 					 GtrApplicationPrivate))
 
-G_DEFINE_TYPE (GtrApplication, gtr_application, UNIQUE_TYPE_APP)
+G_DEFINE_TYPE (GtrApplication, gtr_application, GTK_TYPE_APPLICATION)
 
 struct _GtrApplicationPrivate
 {
@@ -55,7 +59,6 @@ struct _GtrApplicationPrivate
   GSettings *tm_settings;
   GSettings *window_settings;
 
-  GList *windows;
   GtrWindow *active_window;
 
   gchar *toolbars_file;
@@ -71,6 +74,18 @@ struct _GtrApplicationPrivate
 };
 
 static GtrApplication *instance = NULL;
+static gchar **file_arguments = NULL;
+static gboolean option_new_window = FALSE;
+
+static const GOptionEntry options[] = {
+  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments,
+    NULL, N_("[FILE...]")},      /* collects file arguments */
+
+  { "new-window", 'n',  0, G_OPTION_ARG_NONE, &option_new_window,
+    NULL, N_("Create a new toplevel window in an existing instance of Gtranslator")},
+
+  {NULL}
+};
 
 static gboolean
 ensure_user_config_dir (void)
@@ -155,37 +170,40 @@ window_focus_in_event (GtrWindow      *window,
 static void
 on_window_destroy_cb (GtrWindow *window, GtrApplication *app)
 {
-  app->priv->windows = g_list_remove (app->priv->windows,
-                                      window);
+  GList *windows;
 
-  if (window == app->priv->active_window)
-    set_active_window (app, app->priv->windows != NULL ? app->priv->windows->data : NULL);
+  windows = gtk_application_get_windows (GTK_APPLICATION (app));
 
-  if(app->priv->active_window == NULL)
-    {
-      ensure_user_config_dir ();
-      save_accels ();
-      gtk_main_quit ();
-    }
+  if (window == app->priv->active_window)
+    set_active_window (app, windows != NULL ? windows->data : NULL);
 }
 
 static void
 gtr_application_init (GtrApplication *application)
 {
+  GtrApplicationPrivate *priv;
   gchar *gtr_folder;
   gchar *path_default_gtr_toolbar;
   gchar *profiles_file;
   gchar *dir;
-  GtrApplicationPrivate *priv;
+  gchar *pixmaps_dir;
 
   application->priv = GTR_APPLICATION_GET_PRIVATE (application);
   priv = application->priv;
 
   priv->active_window = NULL;
-  priv->windows = NULL;
   priv->last_dir = NULL;
   priv->first_run = FALSE;
 
+  g_set_application_name (_("Gtranslator"));
+  gtk_window_set_default_icon_name ("gtranslator");
+
+  /* We set the default icon dir */
+  pixmaps_dir = gtr_dirs_get_pixmaps_dir ();
+  gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
+                                     pixmaps_dir);
+  g_free (pixmaps_dir);
+
   /* Creating config folder */
   ensure_user_config_dir (); /* FIXME: is this really needed ? */
 
@@ -247,44 +265,44 @@ gtr_application_init (GtrApplication *application)
 static void
 gtr_application_dispose (GObject * object)
 {
-  GtrApplication *app = GTR_APPLICATION (object);
+  GtrApplicationPrivate *priv = GTR_APPLICATION (object)->priv;
 
   DEBUG_PRINT ("Disposing app");
 
-  if (app->priv->settings != NULL)
+  if (priv->settings != NULL)
     {
-      g_object_unref (app->priv->settings);
-      app->priv->settings = NULL;
+      g_object_unref (priv->settings);
+      priv->settings = NULL;
     }
 
-  if (app->priv->tm_settings != NULL)
+  if (priv->tm_settings != NULL)
     {
-      g_object_unref (app->priv->tm_settings);
-      app->priv->tm_settings = NULL;
+      g_object_unref (priv->tm_settings);
+      priv->tm_settings = NULL;
     }
 
-  if (app->priv->window_settings != NULL)
+  if (priv->window_settings != NULL)
     {
-      g_object_unref (app->priv->window_settings);
-      app->priv->window_settings = NULL;
+      g_object_unref (priv->window_settings);
+      priv->window_settings = NULL;
     }
 
-  if (app->priv->icon_factory != NULL)
+  if (priv->icon_factory != NULL)
     {
-      g_object_unref (app->priv->icon_factory);
-      app->priv->icon_factory = NULL;
+      g_object_unref (priv->icon_factory);
+      priv->icon_factory = NULL;
     }
 
-  if (app->priv->tm)
+  if (priv->tm)
     {
-      g_object_unref (app->priv->tm);
-      app->priv->tm = NULL;
+      g_object_unref (priv->tm);
+      priv->tm = NULL;
     }
 
-  if (app->priv->toolbars_model)
+  if (priv->toolbars_model)
     {
-      g_object_unref (app->priv->toolbars_model);
-      app->priv->toolbars_model = NULL;
+      g_object_unref (priv->toolbars_model);
+      priv->toolbars_model = NULL;
     }
 
   G_OBJECT_CLASS (gtr_application_parent_class)->dispose (object);
@@ -301,23 +319,128 @@ gtr_application_finalize (GObject *object)
   G_OBJECT_CLASS (gtr_application_parent_class)->finalize (object);
 }
 
+static GSList *
+get_command_line_files ()
+{
+  GSList *files;
+
+  if (file_arguments)
+    {
+      gint i;
+
+      for (i = 0; file_arguments[i]; i++)
+        {
+          GFile *file;
+
+          file = g_file_new_for_commandline_arg (file_arguments[i]);
+
+          if (file != NULL)
+            files = g_slist_prepend (files, file);
+          else
+            g_print (_("%s: malformed file name or URI.\n"),
+                     file_arguments[i]);
+        }
+    }
+
+  return g_slist_reverse (files);
+}
+
+static gint
+gtr_application_command_line (GApplication            *application,
+                              GApplicationCommandLine *command_line)
+{
+  GtrApplicationPrivate *priv = GTR_APPLICATION (application)->priv;
+  GtrWindow *window;
+  GList *windows;
+  GOptionContext *context;
+  GError *error = NULL;
+  gint argc;
+  gchar **argv;
+
+  windows = gtk_application_get_windows (GTK_APPLICATION (application));
+
+  argv = g_application_command_line_get_arguments (command_line, &argc);
+
+  /* Setup command line options */
+  context = g_option_context_new (_("- Edit PO files"));
+  g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
+  g_option_context_add_group (context, gtk_get_option_group (TRUE));
+
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+    {
+       g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
+               error->message, argv[0]);
+       g_error_free (error);
+       g_option_context_free (context);
+       return 1;
+    }
+
+  g_option_context_free (context);
+
+  if (option_new_window || windows == NULL)
+    {
+      window = gtr_application_create_window (GTR_APPLICATION (application));
+      gtk_application_add_window (GTK_APPLICATION (application),
+                                  GTK_WINDOW (window));
+
+      /* If it is the first run, the default directory was created in this
+       * run, then we show the First run Assistant
+       */
+      if (priv->first_run)
+        gtr_show_assistant (window);
+    }
+  else
+    window = gtr_application_get_active_window (GTR_APPLICATION (application));
+
+  if (file_arguments != NULL)
+    {
+      GSList *files;
+
+      files = get_command_line_files ();
+      if (files != NULL)
+        {
+          gtr_actions_load_locations (window, files);
+          g_slist_free_full (files, g_object_unref);
+        }
+    }
+
+  g_strfreev (argv);
+
+  return 0;
+}
+
+static void
+gtr_application_quit_mainloop (GApplication *application)
+{
+  ensure_user_config_dir ();
+  save_accels ();
+
+  G_APPLICATION_CLASS (gtr_application_parent_class)->quit_mainloop (application);
+}
+
 static void
 gtr_application_class_init (GtrApplicationClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
 
   g_type_class_add_private (klass, sizeof (GtrApplicationPrivate));
 
   object_class->dispose = gtr_application_dispose;
   object_class->finalize = gtr_application_finalize;
+
+  application_class->command_line = gtr_application_command_line;
+  application_class->quit_mainloop = gtr_application_quit_mainloop;
 }
 
 GtrApplication *
 _gtr_application_new ()
 {
   instance = GTR_APPLICATION (g_object_new (GTR_TYPE_APPLICATION,
-                                            "name", "org.gnome.Gtranslator",
-                                            "startup-id", NULL, NULL));
+                                            "application-id", "org.gnome.Gtranslator",
+                                            "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
+                                            "inactivity-timeout", 10000,
+                                            NULL));
 
   return instance;
 }
@@ -355,9 +478,6 @@ gtr_application_create_window (GtrApplication *app)
   window = g_object_new (GTR_TYPE_WINDOW, NULL);
   set_active_window (app, window);
 
-  app->priv->windows = g_list_prepend (app->priv->windows,
-                                       window);
-
   state = g_settings_get_int (app->priv->window_settings,
                               GTR_SETTINGS_WINDOW_STATE);
 
@@ -388,13 +508,6 @@ gtr_application_create_window (GtrApplication *app)
 
   gtk_widget_show (GTK_WIDGET (window));
 
-  /*
-   * If it is the first run, the default directory was created in this
-   * run, then we show the First run Assistant
-   */
-  if (app->priv->first_run && app->priv->windows->next == NULL)
-    gtr_show_assistant (window);
-
   return window;
 }
 
@@ -466,24 +579,6 @@ gtr_application_get_active_window (GtrApplication * app)
 }
 
 /**
- * gtr_application_get_windows:
- * @app: a #GtrApplication
- * 
- * Return value: a list of all opened windows.
- **/
-const GList *
-gtr_application_get_windows (GtrApplication * app)
-{
-  g_return_val_if_fail (GTR_IS_APPLICATION (app), NULL);
-
-  if (!app->priv->windows)
-    app->priv->windows =
-      g_list_prepend (app->priv->windows, app->priv->active_window);
-
-  return app->priv->windows;
-}
-
-/**
  * gtr_application_register_icon:
  * @app: a #GtrApplication
  * @icon: the name of the icon
diff --git a/src/gtr-application.h b/src/gtr-application.h
index 97372a5..b16997c 100644
--- a/src/gtr-application.h
+++ b/src/gtr-application.h
@@ -26,7 +26,6 @@
 #include <glib.h>
 #include <glib-object.h>
 #include <gtk/gtk.h>
-#include <unique/uniqueapp.h>
 
 #include "gtr-window.h"
 
@@ -51,7 +50,7 @@ typedef struct _GtrApplication GtrApplication;
 
 struct _GtrApplication
 {
-  UniqueApp base_instance;
+  GtkApplication base_instance;
 
   /*< private > */
   GtrApplicationPrivate *priv;
@@ -64,7 +63,7 @@ typedef struct _GtrApplicationClass GtrApplicationClass;
 
 struct _GtrApplicationClass
 {
-  UniqueAppClass parent_class;
+  GtkApplicationClass parent_class;
 };
 
 /*
diff --git a/src/gtr-settings.c b/src/gtr-settings.c
index c732a31..6a253cf 100644
--- a/src/gtr-settings.c
+++ b/src/gtr-settings.c
@@ -173,7 +173,7 @@ on_auto_save_changed (GSettings * settings,
 
   auto_save = g_settings_get_boolean (settings, key);
 
-  windows = gtr_application_get_windows (GTR_APP);
+  windows = gtk_application_get_windows (GTK_APPLICATION (GTR_APP));
 
   for (l = windows; l != NULL; l = g_list_next (l))
     {
@@ -199,7 +199,7 @@ on_auto_save_interval_changed (GSettings * settings,
 
   g_settings_get (settings, key, "u", &auto_save_interval);
 
-  windows = gtr_application_get_windows (GTR_APP);
+  windows = gtk_application_get_windows (GTK_APPLICATION (GTR_APP));
 
   for (l = windows; l != NULL; l = g_list_next (l))
     {
@@ -285,7 +285,7 @@ on_pane_switcher_style_changed (GSettings * settings, const gchar * key,
 
   style = g_settings_get_enum (settings, key);
 
-  windows = gtr_application_get_windows (GTR_APP);
+  windows = gtk_application_get_windows (GTK_APPLICATION (GTR_APP));
 
   for (l = windows; l != NULL; l = g_list_next (l))
     {
diff --git a/src/gtr-window.c b/src/gtr-window.c
index 5117781..7a1f7b3 100644
--- a/src/gtr-window.c
+++ b/src/gtr-window.c
@@ -1932,6 +1932,8 @@ gtr_window_dispose (GObject * object)
 static void
 gtr_window_finalize (GObject * object)
 {
+  DEBUG_PRINT ("Finalize window");
+
   G_OBJECT_CLASS (gtr_window_parent_class)->finalize (object);
 }
 
diff --git a/src/main.c b/src/main.c
index f977306..6c67141 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,10 +25,7 @@
 #include <config.h>
 #endif
 
-#include "gtr-actions.h"
 #include "gtr-application.h"
-#include "gtr-plugins-engine.h"
-#include "gtr-utils.h"
 #include "gtr-dirs.h"
 
 #include <errno.h>
@@ -37,10 +34,6 @@
 #include <glib/gi18n.h>
 #include <gio/gio.h>
 
-#ifdef GDK_WINDOWING_X11
-#include <gdk/gdkx.h>
-#endif
-
 #ifdef G_OS_WIN32
 #define SAVE_DATADIR DATADIR
 #undef DATADIR
@@ -50,142 +43,6 @@
 #undef SAVE_DATADIR
 #endif
 
-static gchar **file_arguments = NULL;
-static gboolean option_new_window = FALSE;
-
-static const GOptionEntry options[] = {
-  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments,
-    NULL, N_("[FILE...]")},      /* collects file arguments */
-
-  { "new-window", 'n',  0, G_OPTION_ARG_NONE, &option_new_window,
-    NULL, N_("Create a new toplevel window in an existing instance of Gtranslator")},
-
-  {NULL}
-};
-
-static gchar **
-get_command_line_data ()
-{
-  GPtrArray *array;
-
-  array = g_ptr_array_new ();
-
-  if (file_arguments)
-    {
-      gint i;
-
-      for (i = 0; file_arguments[i]; i++)
-        {
-          GFile *file;
-
-          file = g_file_new_for_commandline_arg (file_arguments[i]);
-
-          if (file != NULL)
-            {
-              g_ptr_array_add (array, g_file_get_uri (file));
-              g_object_unref (file);
-            }
-          else
-            g_print (_("%s: malformed file name or URI.\n"),
-                     file_arguments[i]);
-        }
-    }
-
-  g_ptr_array_add (array, NULL);
-
-  return (gchar **)g_ptr_array_free (array, FALSE);
-}
-
-static GSList *
-get_files_from_command_line_data (const gchar **data)
-{
-  const gchar **ptr;
-  GSList *l = NULL;
-
-  for (ptr = data; ptr != NULL && *ptr != NULL; ptr++)
-    {
-      l = g_slist_prepend (l, g_file_new_for_uri (*ptr));
-    }
-
-  l = g_slist_reverse (l);
-
-  return l;
-}
-
-static UniqueResponse
-unique_app_message_cb (UniqueApp *unique_app,
-                       gint command,
-                       UniqueMessageData *data,
-                       guint timestamp,
-                       gpointer user_data)
-{
-  GtrWindow *window;
-
-  if (command == UNIQUE_NEW)
-    window = gtr_application_create_window (GTR_APPLICATION (unique_app));
-  else
-    window = gtr_application_get_active_window (GTR_APPLICATION (unique_app));
-
-  if (command == UNIQUE_OPEN)
-    {
-      gchar **uris;
-      GSList *files;
-
-      uris = unique_message_data_get_uris (data);
-      files = get_files_from_command_line_data ((const gchar **)uris);
-      g_strfreev (uris);
-
-      if (files != NULL)
-        {
-          gtr_actions_load_locations (window, files);
-          g_slist_foreach (files, (GFunc) g_object_unref, NULL);
-          g_slist_free (files);
-        }
-    }
-
-  /* set the proper interaction time on the window.
-   * Fall back to roundtripping to the X server when we
-   * don't have the timestamp, e.g. when launched from
-   * terminal. We also need to make sure that the window
-   * has been realized otherwise it will not work. lame.
-   */
-  if (!gtk_widget_get_realized (GTK_WIDGET (window)))
-    gtk_widget_realize (GTK_WIDGET (window));
-
-#ifdef GDK_WINDOWING_X11
-  timestamp = gdk_x11_get_server_time (gtk_widget_get_window (GTK_WIDGET (window)));
-#else
-  timestamp = GDK_CURRENT_TIME;
-#endif
-
-  gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
-
-  return UNIQUE_RESPONSE_OK;
-}
-
-static void
-send_unique_data (GtrApplication *app)
-{
-  UniqueMessageData *message_data = NULL;
-
-  if (option_new_window)
-    unique_app_send_message (UNIQUE_APP (app), UNIQUE_NEW, NULL);
-
-  if (file_arguments != NULL)
-    {
-      gchar **uris;
-
-      uris = get_command_line_data ();
-      message_data = unique_message_data_new ();
-      unique_message_data_set_uris (message_data, uris);
-      g_strfreev (uris);
-      unique_app_send_message (UNIQUE_APP (app), UNIQUE_OPEN, message_data);
-      unique_message_data_free (message_data);
-    }
-  else
-    unique_app_send_message (UNIQUE_APP (app), UNIQUE_ACTIVATE, NULL);
-}
-
 #ifdef G_OS_WIN32
 static void
 setup_path (void)
@@ -215,13 +72,7 @@ gint
 main (gint argc, gchar * argv[])
 {
   GtrApplication *app;
-  GError *error = NULL;
-  GtrPluginsEngine *engine;
-  GtrWindow *window;
-  GSList *file_list = NULL;
-  GOptionContext *context;
-  gchar *pixmaps_dir;
-  gchar **uris;
+  gint status;
 
   /* Init type system and threads as soon as possible */
   g_type_init ();
@@ -234,75 +85,14 @@ main (gint argc, gchar * argv[])
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
 
-  g_set_application_name (_("Gtranslator"));
-  gtk_window_set_default_icon_name ("gtranslator");
-
-  /* Setup command line options */
-  context = g_option_context_new (_("- Edit PO files"));
-  g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
-
 #ifdef G_OS_WIN32
   setup_path ();
 #endif
 
-  gtk_init (&argc, &argv);
-
-  if (!g_option_context_parse (context, &argc, &argv, &error))
-    {
-       g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
-               error->message, argv[0]);
-       g_error_free (error);
-       return 1;
-    }
-
-  g_option_context_free (context);
-
   app = _gtr_application_new ();
 
-  if (unique_app_is_running (UNIQUE_APP (app)))
-    {
-      send_unique_data (app);
-
-      /* we never popup a window... tell startup-notification
-       * that we are done. */
-      gdk_notify_startup_complete ();
-
-      g_object_unref (app);
-      exit (0);
-    }
-  else
-    {
-      g_signal_connect (app, "message-received",
-                        G_CALLBACK (unique_app_message_cb), NULL);
-    }
-
-  /* We set the default icon dir */
-  pixmaps_dir = gtr_dirs_get_pixmaps_dir ();
-  gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
-                                     pixmaps_dir);
-  g_free (pixmaps_dir);
-
-  /* Init plugin engine */
-  engine = gtr_plugins_engine_get_default ();
-
-  /* Create the main app-window. */
-  window = gtr_application_create_window (app);
-
-  /* Now we open the files passed as arguments */
-  uris = get_command_line_data ();
-  file_list = get_files_from_command_line_data ((const gchar **)uris);
-  g_strfreev (uris);
-  if (file_list)
-    {
-      gtr_actions_load_locations (window, (const GSList *) file_list);
-      g_slist_foreach (file_list, (GFunc) g_object_unref, NULL);
-      g_slist_free (file_list);
-    }
-
-  /* Enter main GTK loop */
-  gtk_main ();
+  status = g_application_run (G_APPLICATION (app), argc, argv);
 
-  g_object_unref (engine);
   g_object_unref (app);
 
   return 0;
diff --git a/src/plugin-system/gtr-plugins-engine.c b/src/plugin-system/gtr-plugins-engine.c
index f89a7ad..de7bb89 100644
--- a/src/plugin-system/gtr-plugins-engine.c
+++ b/src/plugin-system/gtr-plugins-engine.c
@@ -461,7 +461,7 @@ gtr_plugins_engine_activate_plugin_real (GtrPluginsEngine *
   if (res)
     {
       const GList *wins =
-        gtr_application_get_windows (gtr_application_get_default ());
+        gtk_application_get_windows (GTK_APPLICATION (GTR_APP));
       for (; wins != NULL; wins = wins->next)
         gtr_plugin_activate (info->plugin, GTR_WINDOW (wins->data));
 
@@ -501,7 +501,7 @@ gtr_plugins_engine_deactivate_plugin_real (GtrPluginsEngine *
   if (!info->active || !info->available)
     return;
 
-  wins = gtr_application_get_windows (gtr_application_get_default ());
+  wins = gtk_application_get_windows (GTK_APPLICATION (GTR_APP));
   for (; wins != NULL; wins = wins->next)
     gtr_plugin_deactivate (info->plugin, GTR_WINDOW (wins->data));
 



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