[frogr] Move the creation of GtkApplication to the controller



commit 2e87399514b7b62c60d5ba34d1899a7b0745a504
Author: Mario Sanchez Prada <msanchez gnome org>
Date:   Sat Nov 17 01:56:23 2012 +0100

    Move the creation of GtkApplication to the controller

 src/frogr-controller.c |  197 ++++++++++++++++++++++++++++++++++-------------
 src/frogr-controller.h |    6 +-
 src/frogr-main-view.c  |  158 ++++++++++++++++++++------------------
 src/main.c             |  102 +++----------------------
 4 files changed, 238 insertions(+), 225 deletions(-)
---
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index d0e5ca5..835f5f8 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -141,6 +141,16 @@ typedef enum {
 
 /* Prototypes */
 
+static gboolean _load_pictures_on_idle (gpointer data);
+
+static void _g_application_startup_cb (GApplication *app, gpointer data);
+
+static void _g_application_activate_cb (GApplication *app, gpointer data);
+
+static void _g_application_open_files_cb (GApplication *app, GFile **files, gint n_files, gchar *hint, gpointer data);
+
+static void _g_application_shutdown_cb (GApplication *app, gpointer data);
+
 static void _set_state (FrogrController *self, FrogrControllerState state);
 
 static GCancellable *_register_new_cancellable (FrogrController *self);
@@ -249,6 +259,104 @@ static gboolean _show_add_to_group_dialog_on_idle (GSList *pictures);
 
 /* Private functions */
 
+static gboolean
+_load_pictures_on_idle (gpointer data)
+{
+  FrogrController *fcontroller = NULL;
+  GSList *fileuris = NULL;
+
+  g_return_val_if_fail (data, FALSE);
+
+  fcontroller = frogr_controller_get_instance ();
+  fileuris = (GSList *)data;
+
+  frogr_controller_load_pictures (fcontroller, fileuris);
+  return FALSE;
+}
+
+static void
+_g_application_startup_cb (GApplication *app, gpointer data)
+{
+  FrogrController *self = FROGR_CONTROLLER (data);
+  FrogrControllerPrivate *priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  FrogrAccount *account = NULL;
+  gboolean use_dark_theme;
+
+  DEBUG ("%s", "Application started!\n");
+
+  /* Create UI window */
+  priv->mainview = frogr_main_view_new (GTK_APPLICATION (app));
+  g_object_add_weak_pointer (G_OBJECT (priv->mainview),
+                             (gpointer) & priv->mainview);
+
+  /* Start on idle state */
+  _set_state (self, FROGR_STATE_IDLE);
+
+  /* Select the dark theme if needed */
+  use_dark_theme = frogr_config_get_use_dark_theme (priv->config);
+  frogr_controller_set_use_dark_theme (self, use_dark_theme);
+
+  /* Select the right account */
+  account = frogr_config_get_active_account (priv->config);
+  if (account)
+    frogr_controller_set_active_account (self, account);
+}
+
+static void
+_g_application_activate_cb (GApplication *app, gpointer data)
+{
+  FrogrController *self = FROGR_CONTROLLER (data);
+  FrogrControllerPrivate *priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  GtkWindow *mainview_window = NULL;
+
+  DEBUG ("%s", "Application activated!\n");
+
+  mainview_window = frogr_main_view_get_window (priv->mainview);
+  gtk_window_present (mainview_window);
+}
+
+static void
+_g_application_open_files_cb (GApplication *app, GFile **files, gint n_files, gchar *hint, gpointer data)
+{
+  GSList *fileuris = NULL;
+  int i = 0;
+
+  DEBUG ("Trying to open %d files\n", n_files);
+
+  for (i = 0; i < n_files; i++)
+    {
+      gchar *fileuri = NULL;
+
+      fileuri = g_strdup (g_file_get_uri (files[i]));
+      if (fileuri)
+        fileuris = g_slist_append (fileuris, fileuri);
+    }
+
+  if (fileuris)
+    gdk_threads_add_idle (_load_pictures_on_idle, fileuris);
+}
+
+static void
+_g_application_shutdown_cb (GApplication *app, gpointer data)
+{
+  FrogrController *self = FROGR_CONTROLLER (data);
+  FrogrControllerPrivate *priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+
+  DEBUG ("%s", "Shutting down application...");
+
+  if (priv->app_running)
+    {
+      while (gtk_events_pending ())
+        gtk_main_iteration ();
+
+      g_object_unref (priv->mainview);
+
+      priv->app_running = FALSE;
+
+      frogr_config_save_all (priv->config);
+    }
+}
+
 void
 _set_state (FrogrController *self, FrogrControllerState state)
 {
@@ -2135,9 +2243,6 @@ frogr_controller_init (FrogrController *self)
       frogr_controller_set_proxy (self, use_gnome_proxy,
                                   host, port, username, password);
     }
-
-  /* Select the dark theme if needed */
-  frogr_controller_set_use_dark_theme (self, frogr_config_get_use_dark_theme (priv->config));
 }
 
 
@@ -2152,82 +2257,64 @@ frogr_controller_get_instance (void)
   return FROGR_CONTROLLER (g_object_new (FROGR_TYPE_CONTROLLER, NULL));
 }
 
-FrogrMainView *
-frogr_controller_get_main_view (FrogrController *self)
-{
-  FrogrControllerPrivate *priv = NULL;
-
-  g_return_val_if_fail(FROGR_IS_CONTROLLER (self), FALSE);
-
-  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
-  return priv->mainview;
-}
-
-FrogrModel *
-frogr_controller_get_model (FrogrController *self)
-{
-  FrogrControllerPrivate *priv = NULL;
-
-  g_return_val_if_fail(FROGR_IS_CONTROLLER (self), FALSE);
-
-  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
-  if (!priv->mainview)
-    return NULL;
-
-  return frogr_main_view_get_model (priv->mainview);;
-}
-
-void
-frogr_controller_run_app (FrogrController *self, GtkApplication *app)
+gint
+frogr_controller_run_app (FrogrController *self, int argc, char **argv)
 {
   FrogrControllerPrivate *priv = NULL;
-  FrogrAccount *account = NULL;
+  GtkApplication *app = NULL;
+  gint status;
 
-  g_return_if_fail(FROGR_IS_CONTROLLER (self));
+  g_return_val_if_fail(FROGR_IS_CONTROLLER (self), -1);
 
   priv = FROGR_CONTROLLER_GET_PRIVATE (self);
 
   if (priv->app_running)
     {
       DEBUG ("%s", "Application already running");
-      return;
+      return -1;
     }
-
-  /* Create UI window */
-  priv->mainview = frogr_main_view_new (app);
-  g_object_add_weak_pointer (G_OBJECT (priv->mainview),
-                             (gpointer) & priv->mainview);
-  /* Update flag */
   priv->app_running = TRUE;
 
-  /* Start on idle state */
-  _set_state (self, FROGR_STATE_IDLE);
+  /* Initialize and run the Gtk application */
+  g_set_application_name(APP_SHORTNAME);
+  app = gtk_application_new (APP_ID,
+                             G_APPLICATION_NON_UNIQUE
+                             | G_APPLICATION_HANDLES_OPEN);
 
-  account = frogr_config_get_active_account (priv->config);
-  if (account)
-    frogr_controller_set_active_account (self, account);
+  g_signal_connect (app, "startup", G_CALLBACK (_g_application_startup_cb), self);
+  g_signal_connect (app, "activate", G_CALLBACK (_g_application_activate_cb), self);
+  g_signal_connect (app, "shutdown", G_CALLBACK (_g_application_shutdown_cb), self);
+  g_signal_connect (app, "open", G_CALLBACK (_g_application_open_files_cb), self);
+
+  status = g_application_run (G_APPLICATION (app), argc, argv);
+  g_object_unref (app);
+
+  return status;
 }
 
-void
-frogr_controller_quit_app (FrogrController *self)
+FrogrMainView *
+frogr_controller_get_main_view (FrogrController *self)
 {
   FrogrControllerPrivate *priv = NULL;
 
-  g_return_if_fail(FROGR_IS_CONTROLLER (self));
+  g_return_val_if_fail(FROGR_IS_CONTROLLER (self), FALSE);
 
   priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  return priv->mainview;
+}
 
-  if (priv->app_running)
-    {
-      while (gtk_events_pending ())
-        gtk_main_iteration ();
+FrogrModel *
+frogr_controller_get_model (FrogrController *self)
+{
+  FrogrControllerPrivate *priv = NULL;
 
-      g_object_unref (priv->mainview);
+  g_return_val_if_fail(FROGR_IS_CONTROLLER (self), FALSE);
 
-      priv->app_running = FALSE;
+  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  if (!priv->mainview)
+    return NULL;
 
-      frogr_config_save_all (priv->config);
-    }
+  return frogr_main_view_get_model (priv->mainview);;
 }
 
 void
diff --git a/src/frogr-controller.h b/src/frogr-controller.h
index 05e6204..c804c50 100644
--- a/src/frogr-controller.h
+++ b/src/frogr-controller.h
@@ -65,14 +65,12 @@ GType frogr_controller_get_type (void) G_GNUC_CONST;
 
 FrogrController *frogr_controller_get_instance (void);
 
+gint frogr_controller_run_app (FrogrController *self, int argc, char **argv);
+
 FrogrMainView *frogr_controller_get_main_view (FrogrController *self);
 
 FrogrModel *frogr_controller_get_model (FrogrController *self);
 
-void frogr_controller_run_app (FrogrController *self, GtkApplication *app);
-
-void frogr_controller_quit_app (FrogrController *self);
-
 void frogr_controller_set_active_account (FrogrController *self,
                                           FrogrAccount *account);
 
diff --git a/src/frogr-main-view.c b/src/frogr-main-view.c
index 92bdba9..4c92ef6 100644
--- a/src/frogr-main-view.c
+++ b/src/frogr-main-view.c
@@ -139,6 +139,15 @@ enum {
 
 /* Prototypes */
 
+static void _load_project_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _save_project_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _save_project_as_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _authorize_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _preferences_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _about_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _help_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+static void _quit_action (GSimpleAction *action, GVariant *parameter, gpointer data);
+
 static void _update_project_path (FrogrMainView *self, const gchar *path);
 static void _update_window_title (FrogrMainView *self, gboolean dirty);
 
@@ -170,6 +179,8 @@ gboolean _on_icon_view_button_press_event (GtkWidget *widget,
 
 void _on_account_menu_item_toggled (GtkWidget *widget, gpointer self);
 
+static void _quit_application (FrogrMainView *self);
+
 static gboolean _on_main_view_delete_event (GtkWidget *widget,
                                             GdkEvent *event,
                                             gpointer self);
@@ -268,6 +279,76 @@ static void _update_ui (FrogrMainView *self);
 /* Private API */
 
 static void
+_load_project_action (GSimpleAction *action,
+                      GVariant *parameter,
+                      gpointer data)
+{
+  _load_project_dialog (FROGR_MAIN_VIEW (data));
+}
+
+static void
+_save_project_action (GSimpleAction *action,
+                       GVariant *parameter,
+                       gpointer data)
+{
+  _save_current_project (FROGR_MAIN_VIEW (data));
+}
+
+static void
+_save_project_as_action (GSimpleAction *action,
+                         GVariant *parameter,
+                         gpointer data)
+{
+  _save_project_as_dialog (FROGR_MAIN_VIEW (data));
+}
+
+static void
+_authorize_action (GSimpleAction *action,
+                   GVariant *parameter,
+                   gpointer data)
+{
+  FrogrMainViewPrivate *priv = NULL;
+  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
+  frogr_controller_show_auth_dialog (priv->controller);
+}
+
+static void
+_preferences_action (GSimpleAction *action,
+                     GVariant *parameter,
+                     gpointer data)
+{
+  FrogrMainViewPrivate *priv = NULL;
+  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
+  frogr_controller_show_settings_dialog (priv->controller);
+}
+
+static void
+_about_action (GSimpleAction *action,
+               GVariant *parameter,
+               gpointer data)
+{
+  FrogrMainViewPrivate *priv = NULL;
+  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
+  frogr_controller_show_about_dialog (priv->controller);
+}
+
+static void
+_help_action (GSimpleAction *action,
+              GVariant *parameter,
+              gpointer data)
+{
+  frogr_util_open_uri ("ghelp:frogr");
+}
+
+static void
+_quit_action (GSimpleAction *action,
+              GVariant *parameter,
+              gpointer data)
+{
+  _quit_application (FROGR_MAIN_VIEW (data));
+}
+
+static void
 _update_project_path (FrogrMainView *self, const gchar *path)
 {
   FrogrMainViewPrivate *priv = FROGR_MAIN_VIEW_GET_PRIVATE (self);
@@ -770,12 +851,7 @@ static void
 _quit_application (FrogrMainView *self)
 {
   FrogrMainViewPrivate *priv = FROGR_MAIN_VIEW_GET_PRIVATE (self);
-
-  GApplication *app = G_APPLICATION (g_object_ref (priv->gtk_app));
-  frogr_controller_quit_app (priv->controller);
-
-  g_application_quit (app);
-  g_object_unref (app);
+  g_application_quit (G_APPLICATION (priv->gtk_app));
 }
 
 static gboolean
@@ -1841,76 +1917,6 @@ _frogr_main_view_get_property (GObject *object,
     }
 }
 
-static void
-_load_project_action (GSimpleAction *action,
-                      GVariant *parameter,
-                      gpointer data)
-{
-  _load_project_dialog (FROGR_MAIN_VIEW (data));
-}
-
-static void
-_save_project_action (GSimpleAction *action,
-                       GVariant *parameter,
-                       gpointer data)
-{
-  _save_current_project (FROGR_MAIN_VIEW (data));
-}
-
-static void
-_save_project_as_action (GSimpleAction *action,
-                         GVariant *parameter,
-                         gpointer data)
-{
-  _save_project_as_dialog (FROGR_MAIN_VIEW (data));
-}
-
-static void
-_authorize_action (GSimpleAction *action,
-                   GVariant *parameter,
-                   gpointer data)
-{
-  FrogrMainViewPrivate *priv = NULL;
-  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
-  frogr_controller_show_auth_dialog (priv->controller);
-}
-
-static void
-_preferences_action (GSimpleAction *action,
-                     GVariant *parameter,
-                     gpointer data)
-{
-  FrogrMainViewPrivate *priv = NULL;
-  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
-  frogr_controller_show_settings_dialog (priv->controller);
-}
-
-static void
-_about_action (GSimpleAction *action,
-               GVariant *parameter,
-               gpointer data)
-{
-  FrogrMainViewPrivate *priv = NULL;
-  priv = FROGR_MAIN_VIEW_GET_PRIVATE (data);
-  frogr_controller_show_about_dialog (priv->controller);
-}
-
-static void
-_help_action (GSimpleAction *action,
-              GVariant *parameter,
-              gpointer data)
-{
-  frogr_util_open_uri ("ghelp:frogr");
-}
-
-static void
-_quit_action (GSimpleAction *action,
-              GVariant *parameter,
-              gpointer data)
-{
-  _quit_application (FROGR_MAIN_VIEW (data));
-}
-
 static GActionEntry app_entries[] = {
   { "load_project", _load_project_action, NULL, NULL, NULL },
   { "save_project", _save_project_action, NULL, NULL, NULL },
diff --git a/src/main.c b/src/main.c
index 48c84b6..a0d8d64 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,91 +25,13 @@
 
 #include <config.h>
 #include <glib/gi18n.h>
-#include <gtk/gtk.h>
 #include <gst/gst.h>
 #include <libxml/parser.h>
 
-static GSList *
-_get_files_list_from_array (GFile **files, int n_files)
-{
-  GSList *fileuris = NULL;
-  int i = 0;
-
-  for (i = 0; i < n_files; i++)
-    {
-      gchar *fileuri = NULL;
-
-      fileuri = g_strdup (g_file_get_uri (files[i]));
-      if (fileuri)
-        fileuris = g_slist_append (fileuris, fileuri);
-    }
-
-  return fileuris;
-}
-
-static gboolean
-_load_pictures_on_idle (gpointer data)
-{
-  FrogrController *fcontroller = NULL;
-  GSList *fileuris = NULL;
-
-  g_return_val_if_fail (data, FALSE);
-
-  fcontroller = frogr_controller_get_instance ();
-  fileuris = (GSList *)data;
-
-  frogr_controller_load_pictures (fcontroller, fileuris);
-  return FALSE;
-}
-
-static void
-_app_startup_cb (GApplication *app, gpointer data)
-{
-  FrogrController *fcontroller = NULL;
-
-  DEBUG ("Started!\n");
-
-  /* Initialize libxml2 library */
-  xmlInitParser ();
-
-  /* Initialize internationalization */
-  bindtextdomain (GETTEXT_PACKAGE, frogr_util_get_locale_dir ());
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-  textdomain (GETTEXT_PACKAGE);
-
-  /* Actually start the application */
-  fcontroller = frogr_controller_get_instance ();
-  frogr_controller_run_app (fcontroller, GTK_APPLICATION (app));
-}
-
-static void
-_app_activate_cb (GApplication *app, gpointer data)
-{
-  DEBUG ("Activated!\n");
-}
-
-static void
-_app_shutdown_cb (GApplication *app, gpointer data)
-{
-  DEBUG ("%s", "Shutting down application...");
-
-  /* cleanup libxml2 library */
-  xmlCleanupParser();
-}
-
-static void
-_app_open_files_cb (GApplication *app, GFile **files, gint n_files, gchar *hint, gpointer data)
-{
-  DEBUG ("Trying to open %d files\n", n_files);
-
-  GSList *fileuris = _get_files_list_from_array (files, n_files);
-  gdk_threads_add_idle (_load_pictures_on_idle, fileuris);
-}
-
 int
 main (int argc, char **argv)
 {
-  GtkApplication *app = NULL;
+  FrogrController *controller = NULL;
   GError *error = NULL;
   int status;
 
@@ -122,19 +44,19 @@ main (int argc, char **argv)
       g_error_free (error);
     }
 
-  /* Initialize and run the Gtk application */
-  g_set_application_name(APP_SHORTNAME);
-  app = gtk_application_new (APP_ID,
-                             G_APPLICATION_NON_UNIQUE
-                             | G_APPLICATION_HANDLES_OPEN);
+  /* Initialize libxml2 library */
+  xmlInitParser ();
+
+  /* Initialize internationalization */
+  bindtextdomain (GETTEXT_PACKAGE, frogr_util_get_locale_dir ());
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+  textdomain (GETTEXT_PACKAGE);
 
-  g_signal_connect (app, "activate", G_CALLBACK (_app_activate_cb), NULL);
-  g_signal_connect (app, "startup", G_CALLBACK (_app_startup_cb), NULL);
-  g_signal_connect (app, "shutdown", G_CALLBACK (_app_shutdown_cb), NULL);
-  g_signal_connect (app, "open", G_CALLBACK (_app_open_files_cb), NULL);
+  controller = frogr_controller_get_instance ();
+  status = frogr_controller_run_app (controller, argc, argv);
 
-  status = g_application_run (G_APPLICATION (app), argc, argv);
-  g_object_unref (app);
+  /* cleanup libxml2 library */
+  xmlCleanupParser();
 
   return status;
 }



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