[gnome-system-log] app: port to GtkApplication and GtkApplicationWindow



commit 0c4d021ee3d9df404a92a1eefbfc2fe47af020b7
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Thu Jun 28 19:58:47 2012 -0400

    app: port to GtkApplication and GtkApplicationWindow
    
    So that we can use GMenuModel and greatly simplify code.

 src/logview-app.c     |  163 ++++++++++++++++++++-----------------------------
 src/logview-app.h     |   11 ++--
 src/logview-main.c    |   80 ++----------------------
 src/logview-manager.c |    5 +-
 src/logview-window.c  |   27 ++++----
 src/logview-window.h  |    6 +-
 6 files changed, 98 insertions(+), 194 deletions(-)
---
diff --git a/src/logview-app.c b/src/logview-app.c
index 386ea7a..465d5d7 100644
--- a/src/logview-app.c
+++ b/src/logview-app.c
@@ -35,55 +35,15 @@
 struct _LogviewAppPrivate {
   LogviewPrefs *prefs;
   LogviewManager *manager;
-  LogviewWindow *window;
-};
 
-enum {
-  APP_QUIT,
-  LAST_SIGNAL
+  GtkWidget *window;
 };
 
-static guint signals[LAST_SIGNAL] = { 0 };
-
-static LogviewApp *app_singleton = NULL;
-
-G_DEFINE_TYPE (LogviewApp, logview_app, G_TYPE_OBJECT);
+G_DEFINE_TYPE (LogviewApp, logview_app, GTK_TYPE_APPLICATION);
 
 #define GET_PRIVATE(o) \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGVIEW_TYPE_APP, LogviewAppPrivate))
 
-static gboolean
-main_window_delete_cb (GtkWidget *widget,
-                       GdkEvent *event,
-                       gpointer user_data)
-{
-  LogviewApp *app = user_data;
-
-  g_signal_emit (app, signals[APP_QUIT], 0, NULL);
-
-  return FALSE;
-}
-
-static gboolean
-logview_app_set_window (LogviewApp *app)
-{
-  LogviewWindow *window;
-  gboolean retval = FALSE;
-
-  window = LOGVIEW_WINDOW (logview_window_new ());
-
-  if (window) {
-    app->priv->window = window;
-    g_signal_connect (window, "delete-event",
-                      G_CALLBACK (main_window_delete_cb), app);
-    retval = TRUE;
-  }
-
-  gtk_window_set_default_icon_name ("logview");
-
-  return retval;
-}
-
 typedef struct {
   LogviewApp *app;
   GSList *logs;
@@ -287,80 +247,82 @@ do_finalize (GObject *obj)
 }
 
 static void
-logview_app_class_init (LogviewAppClass *klass)
+logview_app_activate (GApplication *application)
 {
-  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  LogviewApp *app = LOGVIEW_APP (application);
+  char *active_log;
+  gchar **logs;
 
-  oclass->finalize = do_finalize;
+  G_APPLICATION_CLASS (logview_app_parent_class)->activate (application);
 
-  signals[APP_QUIT] =
-    g_signal_new ("app-quit",
-                  G_OBJECT_CLASS_TYPE (oclass),
-                  G_SIGNAL_RUN_LAST,
-                  G_STRUCT_OFFSET (LogviewAppClass, app_quit),
-                  NULL, NULL,
-                  g_cclosure_marshal_VOID__VOID,
-                  G_TYPE_NONE, 0);
+  logs = logview_prefs_get_stored_logfiles (app->priv->prefs);
 
-  g_type_class_add_private (klass, sizeof (LogviewAppPrivate));
-}
+  if (!logs || !logs[0]) {
+    logview_app_first_time_initialize (app);
+  } else {
+    active_log = logview_prefs_get_active_logfile (app->priv->prefs);
+    logview_manager_add_logs_from_names (app->priv->manager, logs, active_log);
+    g_free (active_log);
+  }
 
-static void
-logview_app_init (LogviewApp *self)
-{
-  LogviewAppPrivate *priv = self->priv = GET_PRIVATE (self);
+  g_strfreev (logs);
 
-  priv->prefs = logview_prefs_get ();
-  priv->manager = logview_manager_get ();
+  gtk_widget_show (app->priv->window);
 }
 
-LogviewApp*
-logview_app_get (void)
+static void
+logview_app_startup (GApplication *application)
 {
-  if (!app_singleton) {
-    app_singleton = g_object_new (LOGVIEW_TYPE_APP, NULL);
+  LogviewApp *app = LOGVIEW_APP (application);
 
-    if (!logview_app_set_window (app_singleton)) {
-      g_object_unref (app_singleton);
-      app_singleton = NULL;
-    }
-  }
+  G_APPLICATION_CLASS (logview_app_parent_class)->startup (application);
 
-  return app_singleton;
+  app->priv->window = logview_window_new (GTK_APPLICATION (app));
 }
 
-void
-logview_app_initialize (LogviewApp *app, char **log_files)
+static void
+logview_app_open (GApplication *application,
+                  GFile       **files,
+                  gint          n_files,
+                  const gchar  *hint)
 {
-  LogviewAppPrivate *priv;
+  LogviewApp *app = LOGVIEW_APP (application);
+  gchar **logs;
+  gint idx;
 
-  g_assert (LOGVIEW_IS_APP (app));
+  logs = g_malloc0 ((n_files + 1) * sizeof (gchar *));
+  for (idx = 0; idx < n_files; idx++)
+    logs[idx] = g_file_get_path (files[idx]);
 
-  priv = app->priv;
+  logs[n_files] = NULL;
+  logview_manager_add_logs_from_names (app->priv->manager, logs, logs[0]);
+  g_strfreev (logs);
 
-  /* open regular logs and add each log passed as a parameter */
+  gtk_widget_show (app->priv->window);
+}
 
-  if (log_files == NULL) {
-    char *active_log;
-    gchar **logs;
+static void
+logview_app_class_init (LogviewAppClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  GApplicationClass *aclass = G_APPLICATION_CLASS (klass);
 
-    active_log = logview_prefs_get_active_logfile (priv->prefs);
-    logs = logview_prefs_get_stored_logfiles (priv->prefs);
+  oclass->finalize = do_finalize;
 
-    if (!logs || !logs[0]) {
-      logview_app_first_time_initialize (app);
-    } else {
-      logview_manager_add_logs_from_names (priv->manager,
-                                           logs, active_log);
+  aclass->activate = logview_app_activate;
+  aclass->open = logview_app_open;
+  aclass->startup = logview_app_startup;
 
-      g_free (active_log);
-      g_strfreev (logs);
-    }
-  } else {
-    logview_manager_add_logs_from_names (priv->manager, log_files, NULL);
-  }
+  g_type_class_add_private (klass, sizeof (LogviewAppPrivate));
+}
+
+static void
+logview_app_init (LogviewApp *self)
+{
+  LogviewAppPrivate *priv = self->priv = GET_PRIVATE (self);
 
-  gtk_widget_show (GTK_WIDGET (priv->window));
+  priv->prefs = logview_prefs_get ();
+  priv->manager = logview_manager_get ();
 }
 
 void
@@ -373,7 +335,7 @@ logview_app_add_error (LogviewApp *app,
 
   g_assert (LOGVIEW_IS_APP (app));
 
-  window = app->priv->window;
+  window = LOGVIEW_WINDOW (app->priv->window);
   primary = g_strdup_printf (_("Impossible to open the file %s"), file_path);
 
   logview_window_add_error (window, primary, secondary);
@@ -389,7 +351,7 @@ logview_app_add_errors (LogviewApp *app,
 
   g_assert (LOGVIEW_IS_APP (app));
 
-  window = app->priv->window;
+  window = LOGVIEW_WINDOW (app->priv->window);
 
   if (errors->len == 0) {
     return;
@@ -402,3 +364,12 @@ logview_app_add_errors (LogviewApp *app,
     logview_window_add_errors (window, errors);
   }
 }
+
+LogviewApp *
+logview_app_new (void)
+{
+  return g_object_new (LOGVIEW_TYPE_APP,
+                       "application-id", "org.gnome.Logview",
+                       "flags", G_APPLICATION_HANDLES_OPEN,
+                       NULL);
+}
diff --git a/src/logview-app.h b/src/logview-app.h
index d5c1597..b1acb60 100644
--- a/src/logview-app.h
+++ b/src/logview-app.h
@@ -21,7 +21,7 @@
 #ifndef __LOGVIEW_APP_H__
 #define __LOGVIEW_APP_H__
 
-#include <glib-object.h>
+#include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
@@ -42,22 +42,21 @@ typedef struct _LogviewAppClass LogviewAppClass;
 typedef struct _LogviewAppPrivate LogviewAppPrivate;
 
 struct _LogviewApp {
-  GObject parent;
+  GtkApplication parent;
 
   LogviewAppPrivate *priv;
 };
 
 struct _LogviewAppClass {
-  GObjectClass parent_class;
-
-  void (* app_quit) (LogviewApp *app);
+  GtkApplicationClass parent_class;
 };
 
 
 GType logview_app_get_type (void);
 
 /* public methods */
-LogviewApp * logview_app_get (void);
+LogviewApp * logview_app_new (void);
+
 void         logview_app_initialize (LogviewApp *app,
                                      char **log_files);
 void         logview_app_add_error  (LogviewApp *app,
diff --git a/src/logview-main.c b/src/logview-main.c
index b856c67..1a08372 100644
--- a/src/logview-main.c
+++ b/src/logview-main.c
@@ -30,91 +30,23 @@
 
 #include "logview-app.h"
 
-/* log files specified on the command line */
-static char **log_files = NULL;
-
-static void
-app_quit_cb (LogviewApp *app,
-             gpointer user_data)
-{
-  gtk_main_quit ();
-}
-
-static void
-logview_show_version_and_quit (void)
-{
-  g_print ("%s - Version %s\n"
-           "Copyright (C) 2004-2008 Vincent Noel, Cosimo Cecchi and others.\n",
-           g_get_application_name (),
-           VERSION);
-
-  exit (0);
-}
-
-static GOptionContext *
-create_option_context (void)
-{
-  GOptionContext *context;
-
-  const GOptionEntry entries[] = {
-    { "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
-      logview_show_version_and_quit, N_("Show the application's version"), NULL },
-    { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &log_files,
-      NULL, N_("[LOGFILE...]") },
-    { NULL },
-  };
-
-  context = g_option_context_new (_(" - Browse and monitor logs"));
-  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
-  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
-  g_option_context_set_ignore_unknown_options (context, TRUE);
-  g_option_context_add_group (context, gtk_get_option_group (TRUE));
-
-  return context;
-}
-
 int
 main (int argc, char *argv[])
 {
-  GError *error = NULL;
-  GOptionContext *context;
   LogviewApp *app;
+  gint result;
+
+  g_type_init ();
 
   bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
 
-  context = create_option_context ();
-
-  g_option_context_parse (context, &argc, &argv, &error);
-
-  if (error) {
-    g_critical ("Unable to parse arguments: %s", error->message);
-    g_error_free (error);
-    g_option_context_free (context);
-    
-    exit (1);
-  }
-
-  g_option_context_free (context);
   g_set_application_name (_("Log Viewer"));
 
-  app = logview_app_get ();
-
-  if (!app) {
-    g_critical ("Unable to create the user interface.");
-  
-    exit (1);
-  } else {
-    g_signal_connect (app, "app-quit",
-                      G_CALLBACK (app_quit_cb), NULL);
-  }
-
-  logview_app_initialize (app, log_files);
-
-  gtk_main ();
-
+  app = logview_app_new ();
+  result = g_application_run (G_APPLICATION (app), argc, argv);
   g_object_unref (app);
 
-  return EXIT_SUCCESS;
+  return result;
 }
diff --git a/src/logview-manager.c b/src/logview-manager.c
index ba9e49e..dd48f14 100644
--- a/src/logview-manager.c
+++ b/src/logview-manager.c
@@ -187,7 +187,7 @@ create_log_cb (LogviewLog *log,
     path = g_file_get_path (data->file);
 
     if (!data->is_multiple) {
-      logview_app_add_error (logview_app_get (),
+      logview_app_add_error (LOGVIEW_APP (g_application_get_default ()),
                              path, error->message);
     } else {
       char **error_arr = g_new0 (char *, 3);
@@ -206,7 +206,8 @@ create_log_cb (LogviewLog *log,
     op->current++;
 
     if (op->total == op->current) {
-      logview_app_add_errors (logview_app_get (), op->errors);
+      logview_app_add_errors (LOGVIEW_APP (g_application_get_default ()),
+                              op->errors);
       multiple_creation_op_free (op);
       op = NULL;
     }
diff --git a/src/logview-window.c b/src/logview-window.c
index 8f5a227..09452b6 100644
--- a/src/logview-window.c
+++ b/src/logview-window.c
@@ -76,7 +76,7 @@ struct _LogviewWindowPrivate {
 #define GET_PRIVATE(o) \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGVIEW_TYPE_WINDOW, LogviewWindowPrivate))
 
-G_DEFINE_TYPE (LogviewWindow, logview_window, GTK_TYPE_WINDOW);
+G_DEFINE_TYPE (LogviewWindow, logview_window, GTK_TYPE_APPLICATION_WINDOW);
 
 static void findbar_close_cb  (LogviewFindbar *findbar,
                                gpointer user_data);
@@ -774,6 +774,12 @@ logview_about (GtkWidget *widget, GtkWidget *window)
 }
 
 static void
+logview_quit (GtkAction *action, LogviewWindow *logview)
+{
+  gtk_widget_destroy (GTK_WIDGET (logview));
+}
+
+static void
 logview_toggle_statusbar (GtkAction *action, LogviewWindow *logview)
 {
   if (gtk_widget_get_visible (logview->priv->statusbar))
@@ -820,7 +826,7 @@ static GtkActionEntry entries[] = {
     { "CloseLog", GTK_STOCK_CLOSE, N_("_Close"), "<control>W", N_("Close this log"), 
       G_CALLBACK (logview_close_log) },
     { "Quit", GTK_STOCK_QUIT, N_("_Quit"), "<control>Q", N_("Quit the log viewer"), 
-      G_CALLBACK (gtk_main_quit) },
+      G_CALLBACK (logview_quit) },
 
     { "Copy", GTK_STOCK_COPY, N_("_Copy"), "<control>C", N_("Copy the selection"),
       G_CALLBACK (logview_copy) },
@@ -1259,6 +1265,8 @@ logview_window_init (LogviewWindow *logview)
   priv->manager = logview_manager_get ();
   priv->monitor_id = 0;
 
+  gtk_window_set_icon_name (GTK_WINDOW (logview), "logview");
+
   logview_prefs_get_stored_window_size (priv->prefs, &width, &height);
   gtk_window_set_default_size (GTK_WINDOW (logview), width, height);
 
@@ -1284,7 +1292,6 @@ logview_window_init (LogviewWindow *logview)
                                              &error);
 
   if (res == FALSE) {
-    priv->ui_manager = NULL;
     g_critical ("Can't load the UI description: %s", error->message);
     g_error_free (error);
     return;
@@ -1437,17 +1444,11 @@ logview_window_class_init (LogviewWindowClass *klass)
 /* public methods */
 
 GtkWidget *
-logview_window_new ()
+logview_window_new (GtkApplication *application)
 {
-  LogviewWindow *logview;
-
-  logview = g_object_new (LOGVIEW_TYPE_WINDOW, NULL);
-
-  if (logview->priv->ui_manager == NULL) {
-    return NULL;
-  }
-
-  return GTK_WIDGET (logview);
+  return g_object_new (LOGVIEW_TYPE_WINDOW, 
+                       "application", application,
+                       NULL);
 }
 
 void
diff --git a/src/logview-window.h b/src/logview-window.h
index f47088b..73150fa 100644
--- a/src/logview-window.h
+++ b/src/logview-window.h
@@ -35,18 +35,18 @@ typedef struct _LogviewWindowClass LogviewWindowClass;
 typedef struct _LogviewWindowPrivate LogviewWindowPrivate;
 
 struct _LogviewWindow {
-  GtkWindow parent_instance;
+  GtkApplicationWindow parent_instance;
   LogviewWindowPrivate *priv;
 };
 
 struct _LogviewWindowClass {
-	GtkWindowClass parent_class;
+  GtkApplicationWindowClass parent_class;
 };
 
 GType logview_window_get_type (void);
 
 /* public methods */
-GtkWidget * logview_window_new        (void);
+GtkWidget * logview_window_new        (GtkApplication *application);
 void        logview_window_add_error  (LogviewWindow *window,
                                        const char *primary,
                                        const char *secondary);



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