[gnome-control-center] shell: Make the application a GtkApplication subclass



commit 64467d7f0a4078ae918cbac04e45e6a93d9c517f
Author: William Jon McCann <jmccann redhat com>
Date:   Sat Feb 16 21:56:16 2013 -0500

    shell: Make the application a GtkApplication subclass
    
    This promotes better encapsulation and allows us to move
    application logic out of main() and rename the confusingly
    named control-center.c to main.c
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692174

 shell/Makefile.am                            |    4 +-
 shell/{control-center.c => cc-application.c} |  245 +++++++++++++++-----------
 shell/cc-application.h                       |   70 ++++++++
 shell/gnome-control-center.c                 |    4 -
 shell/main.c                                 |   57 ++++++
 5 files changed, 276 insertions(+), 104 deletions(-)
---
diff --git a/shell/Makefile.am b/shell/Makefile.am
index cff6112..b90a5f6 100644
--- a/shell/Makefile.am
+++ b/shell/Makefile.am
@@ -22,7 +22,9 @@ BUILT_SOURCES =                       \
 
 gnome_control_center_SOURCES =                 \
        $(BUILT_SOURCES)                        \
-       control-center.c                        \
+       main.c                                  \
+       cc-application.c                        \
+       cc-application.h                        \
        cc-shell-log.c                          \
        cc-shell-log.h                          \
        gnome-control-center.c                  \
diff --git a/shell/control-center.c b/shell/cc-application.c
similarity index 53%
rename from shell/control-center.c
rename to shell/cc-application.c
index c082aa9..cbd05f6 100644
--- a/shell/control-center.c
+++ b/shell/cc-application.c
@@ -1,46 +1,47 @@
 /*
- * Copyright (c) 2009, 2010 Intel, Inc.
- * Copyright (c) 2010 Red Hat, Inc.
+ * Copyright  2013 Red Hat, Inc.
  *
- * The Control Center is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
- * The Control Center is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License along
- * with the Control Center; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * Author: Thomas Wood <thos gnome org>
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
 #include "config.h"
 
-#include <glib/gi18n.h>
 #include <stdlib.h>
 
-#include "gnome-control-center.h"
-#include "cc-panel-loader.h"
-
-#include <gtk/gtk.h>
-#include <string.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
 #include <libnotify/notify.h>
-#include <egg-list-box.h>
+#include <clutter-gtk/clutter-gtk.h>
 
-#ifdef GDK_WINDOWING_X11
-#include <X11/Xlib.h>
-#endif
+#include "cc-application.h"
+#include "cc-panel-loader.h"
+#include "cc-shell-log.h"
+#include "gnome-control-center.h"
 
 #ifdef HAVE_CHEESE
 #include <cheese-gtk.h>
 #endif /* HAVE_CHEESE */
 
-#include "cc-shell-log.h"
+struct _CcApplicationPrivate
+{
+  GnomeControlCenter *window;
+};
+
+G_DEFINE_TYPE (CcApplication, cc_application, GTK_TYPE_APPLICATION)
 
 G_GNUC_NORETURN static gboolean
 option_version_cb (const gchar *option_name,
@@ -74,11 +75,31 @@ const GOptionEntry all_options[] = {
   { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
 };
 
+static void
+help_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       user_data)
+{
+  CcApplication *self = CC_APPLICATION (user_data);
+  CcPanel *panel;
+  GtkWidget *window;
+  const char *uri = NULL;
+
+  panel = cc_shell_get_active_panel (CC_SHELL (self->priv->window));
+  if (panel)
+    uri = cc_panel_get_help_uri (panel);
+
+  window = cc_shell_get_toplevel (CC_SHELL (self->priv->window));
+  gtk_show_uri (gtk_widget_get_screen (window),
+                uri ? uri : "help:gnome-help/prefs",
+                GDK_CURRENT_TIME, NULL);
+}
+
 static int
-application_command_line_cb (GApplication  *application,
-                             GApplicationCommandLine  *command_line,
-                             GnomeControlCenter      *shell)
+cc_application_command_line (GApplication *application,
+                             GApplicationCommandLine *command_line)
 {
+  CcApplication *self = CC_APPLICATION (application);
   int argc;
   char **argv;
   int retval = 0;
@@ -139,23 +160,21 @@ application_command_line_cb (GApplication  *application,
       return 0;
     }
 
-  g_option_context_free (context);
-
 #ifdef HAVE_CHEESE
   cheese_gtk_init (&argc, &argv);
 #endif /* HAVE_CHEESE */
 
   cc_shell_log_set_debug (verbose);
 
-  gnome_control_center_show (shell, GTK_APPLICATION (application));
+  gnome_control_center_show (self->priv->window, GTK_APPLICATION (application));
 
   if (search_str)
     {
-      gnome_control_center_set_search_item (shell, search_str);
+      gnome_control_center_set_search_item (self->priv->window, search_str);
     }
   else if (show_overview)
     {
-      gnome_control_center_set_overview_page (shell);
+      gnome_control_center_set_overview_page (self->priv->window);
     }
   else if (start_panels != NULL && start_panels[0] != NULL)
     {
@@ -165,11 +184,11 @@ application_command_line_cb (GApplication  *application,
       start_id = start_panels[0];
 
       if (start_panels[1])
-       g_debug ("Extra argument: %s", start_panels[1]);
+        g_debug ("Extra argument: %s", start_panels[1]);
       else
-       g_debug ("No extra argument");
+        g_debug ("No extra argument");
 
-      if (!cc_shell_set_active_panel_from_id (CC_SHELL (shell), start_id, (const gchar**)start_panels+1, 
&err))
+      if (!cc_shell_set_active_panel_from_id (CC_SHELL (self->priv->window), start_id, (const 
gchar**)start_panels+1, &err))
         {
           g_warning ("Could not load setting panel \"%s\": %s", start_id,
                      (err) ? err->message : "Unknown error");
@@ -182,61 +201,71 @@ application_command_line_cb (GApplication  *application,
         }
     }
 
-  gnome_control_center_present (shell);
-  gdk_notify_startup_complete ();
-
-  g_strfreev (argv);
   if (start_panels != NULL)
     {
       g_strfreev (start_panels);
       start_panels = NULL;
     }
+
   show_overview = FALSE;
 
+  g_option_context_free (context);
+  g_strfreev (argv);
+
   return retval;
 }
 
 static void
-help_activated (GSimpleAction *action,
-                GVariant      *parameter,
-                gpointer       user_data)
+cc_application_quit (GSimpleAction *simple,
+                     GVariant *parameter,
+                     gpointer user_data)
 {
-  GnomeControlCenter *shell = user_data;
-  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
-  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
-  const char *uri = NULL;
+  CcApplication *self = CC_APPLICATION (user_data);
 
-  if (panel)
-    uri = cc_panel_get_help_uri (panel);
-
-  gtk_show_uri (gtk_widget_get_screen (window),
-                uri ? uri : "help:gnome-help/prefs",
-                GDK_CURRENT_TIME, NULL);
+  g_clear_object (&self->priv->window);
 }
 
+
 static void
-quit_activated (GSimpleAction *action,
-                GVariant      *parameter,
-                gpointer       user_data)
+cc_application_activate (GApplication *application)
 {
-  GnomeControlCenter *shell = user_data;
-  g_object_unref (shell);
+  CcApplication *self = CC_APPLICATION (application);
+
+  gnome_control_center_present (self->priv->window);
 }
 
 static void
-application_startup_cb (GApplication       *application,
-                        GnomeControlCenter *shell)
+cc_application_startup (GApplication *application)
 {
-  GMenu *menu, *section;
-  GAction *action;
+  CcApplication *self = CC_APPLICATION (application);
+  GMenu *menu;
+  GMenu *section;
+  GSimpleAction *action;
+
+  G_APPLICATION_CLASS (cc_application_parent_class)->startup (application);
+
+#ifdef HAVE_CHEESE
+  if (gtk_clutter_init (NULL, NULL) != CLUTTER_INIT_SUCCESS)
+    {
+      g_critical ("Unable to initialize Clutter");
+      return;
+    }
+#endif /* HAVE_CHEESE */
 
-  action = G_ACTION (g_simple_action_new ("help", NULL));
-  g_action_map_add_action (G_ACTION_MAP (application), action);
-  g_signal_connect (action, "activate", G_CALLBACK (help_activated), shell);
+  /* register a symbolic icon size for use in sidebar lists */
+  gtk_icon_size_register ("cc-sidebar-list", 24, 24);
 
-  action = G_ACTION (g_simple_action_new ("quit", NULL));
-  g_action_map_add_action (G_ACTION_MAP (application), action);
-  g_signal_connect (action, "activate", G_CALLBACK (quit_activated), shell);
+  notify_init ("gnome-control-center");
+
+  action = g_simple_action_new ("help", NULL);
+  g_action_map_add_action (G_ACTION_MAP (application), G_ACTION (action));
+  g_signal_connect (action, "activate", G_CALLBACK (help_activated), self);
+  g_object_unref (action);
+
+  action = g_simple_action_new ("quit", NULL);
+  g_action_map_add_action (G_ACTION_MAP (application), G_ACTION (action));
+  g_signal_connect (action, "activate", G_CALLBACK (cc_application_quit), self);
+  g_object_unref (action);
 
   menu = g_menu_new ();
 
@@ -252,48 +281,66 @@ application_startup_cb (GApplication       *application,
   gtk_application_add_accelerator (GTK_APPLICATION (application),
                                    "F1", "app.help", NULL);
 
-  /* nothing else to do here, we don't want to show a window before
-   * we've looked at the commandline
-   */
+  self->priv->window = gnome_control_center_new ();
 }
 
-int
-main (int argc, char **argv)
+static GObject *
+cc_application_constructor (GType type,
+                            guint n_construct_params,
+                            GObjectConstructParam *construct_params)
 {
-  GnomeControlCenter *shell;
-  GtkApplication *application;
-  int status;
+  static GObject *self = NULL;
 
-  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-  textdomain (GETTEXT_PACKAGE);
+  if (self == NULL)
+    {
+      self = G_OBJECT_CLASS (cc_application_parent_class)->constructor (type,
+                                                                        n_construct_params,
+                                                                        construct_params);
+      g_object_add_weak_pointer (self, (gpointer) &self);
+      return self;
+    }
 
-#ifdef GDK_WINDOWING_X11
-  XInitThreads ();
-#endif
+  return g_object_ref (self);
+}
 
-  gtk_init (&argc, &argv);
-  cc_shell_log_init ();
 
-  g_type_ensure (egg_list_box_get_type ());
+static void
+cc_application_dispose (GObject *object)
+{
+  G_OBJECT_CLASS (cc_application_parent_class)->dispose (object);
+}
 
-  /* register a symbolic icon size for use in sidebar lists */
-  gtk_icon_size_register ("cc-sidebar-list", 24, 24);
 
-  notify_init ("gnome-control-center");
+static void
+cc_application_init (CcApplication *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+                                            CC_TYPE_APPLICATION,
+                                            CcApplicationPrivate);
+}
 
-  shell = gnome_control_center_new ();
 
-  /* enforce single instance of this application */
-  application = gtk_application_new ("org.gnome.ControlCenter", G_APPLICATION_HANDLES_COMMAND_LINE);
-  g_signal_connect (application, "startup",
-                    G_CALLBACK (application_startup_cb), shell);
-  g_signal_connect (application, "command-line",
-                    G_CALLBACK (application_command_line_cb), shell);
+static void
+cc_application_class_init (CcApplicationClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GApplicationClass *application_class = G_APPLICATION_CLASS (class);
+
+  object_class->constructor = cc_application_constructor;
+  object_class->dispose = cc_application_dispose;
+  application_class->activate = cc_application_activate;
+  application_class->startup = cc_application_startup;
+  application_class->command_line = cc_application_command_line;
 
-  status = g_application_run (G_APPLICATION (application), argc, argv);
+  g_type_class_add_private (class, sizeof (CcApplicationPrivate));
+}
 
-  g_object_unref (application);
 
-  return status;
+GtkApplication *
+cc_application_new (void)
+{
+  return g_object_new (CC_TYPE_APPLICATION,
+                       "application-id", "org.gnome.ControlCenter",
+                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
+                       NULL);
 }
diff --git a/shell/cc-application.h b/shell/cc-application.h
new file mode 100644
index 0000000..b74b44d
--- /dev/null
+++ b/shell/cc-application.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright  2013 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef CC_APPLICATION_H
+#define CC_APPLICATION_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define CC_TYPE_APPLICATION (cc_application_get_type ())
+
+#define CC_APPLICATION(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   CC_TYPE_APPLICATION, CcApplication))
+
+#define CC_APPLICATION_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   CC_TYPE_APPLICATION, CcApplicationClass))
+
+#define CC_IS_APPLICATION(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   CC_TYPE_APPLICATION))
+
+#define CC_IS_APPLICATION_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   CC_TYPE_APPLICATION))
+
+#define CC_APPLICATION_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   CC_TYPE_APPLICATION, CcApplicationClass))
+
+typedef struct _CcApplication        CcApplication;
+typedef struct _CcApplicationClass   CcApplicationClass;
+typedef struct _CcApplicationPrivate CcApplicationPrivate;
+
+struct _CcApplication
+{
+  GtkApplication parent_instance;
+  CcApplicationPrivate *priv;
+};
+
+struct _CcApplicationClass
+{
+  GtkApplicationClass parent_class;
+};
+
+GType                  cc_application_get_type               (void) G_GNUC_CONST;
+
+GtkApplication        *cc_application_new                    (void);
+
+G_END_DECLS
+
+#endif /* CC_APPLICATION_H */
diff --git a/shell/gnome-control-center.c b/shell/gnome-control-center.c
index 064776c..a37959a 100644
--- a/shell/gnome-control-center.c
+++ b/shell/gnome-control-center.c
@@ -32,10 +32,6 @@
 #include <string.h>
 #include <libgd/gd-styled-text-renderer.h>
 
-#ifdef HAVE_CHEESE
-#include <clutter-gtk/clutter-gtk.h>
-#endif /* HAVE_CHEESE */
-
 #include "cc-panel.h"
 #include "cc-shell.h"
 #include "cc-shell-category-view.h"
diff --git a/shell/main.c b/shell/main.c
new file mode 100644
index 0000000..bffac0b
--- /dev/null
+++ b/shell/main.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2009, 2010 Intel, Inc.
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * The Control Center is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * The Control Center is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with the Control Center; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Thomas Wood <thos gnome org>
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <egg-list-box.h>
+
+#ifdef GDK_WINDOWING_X11
+#include <X11/Xlib.h>
+#endif
+
+#include "cc-application.h"
+
+int
+main (int argc, char **argv)
+{
+  GtkApplication *application;
+  int status;
+
+  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+  textdomain (GETTEXT_PACKAGE);
+
+#ifdef GDK_WINDOWING_X11
+  XInitThreads ();
+#endif
+
+  g_type_ensure (egg_list_box_get_type ());
+
+  application = cc_application_new ();
+  status = g_application_run (G_APPLICATION (application), argc, argv);
+
+  g_object_unref (application);
+
+  return status;
+}


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