[gnome-terminal/simpler-app] app: Simplify GApplication setup



commit 3246dd20abd30434a22829b83d9c87a2ab419bd5
Author: Christian Persch <chpe gnome org>
Date:   Sat May 5 17:24:16 2012 +0200

    app: Simplify GApplication setup

 src/server.c       |  104 ++++++---------------------------------------------
 src/terminal-app.c |   60 ++++++++++++++++++++++-------
 src/terminal-app.h |    2 +-
 3 files changed, 59 insertions(+), 107 deletions(-)
---
diff --git a/src/server.c b/src/server.c
index 23078c8..4ac2011 100644
--- a/src/server.c
+++ b/src/server.c
@@ -62,79 +62,10 @@ static const GOptionEntry options[] = {
   { NULL }
 };
 
-
-typedef struct {
-  GApplication *app;
-  GMainLoop *loop;
-  gboolean owns_name;
-} MainData;
-
-static void
-bus_acquired_cb (GDBusConnection *connection,
-                 const char *name,
-                 gpointer user_data)
-{
-  MainData *data = (MainData *) user_data;
-  GDBusObjectManagerServer *object_manager;
-  TerminalObjectSkeleton *object;
-  TerminalFactory *factory;
-
-  _terminal_debug_print (TERMINAL_DEBUG_FACTORY,
-                         "Bus %s acquired\n", name);
-
-  object = terminal_object_skeleton_new (TERMINAL_FACTORY_OBJECT_PATH);
-  factory = terminal_factory_impl_new ();
-  terminal_object_skeleton_set_factory (object, factory);
-  g_object_unref (factory);
-
-  object_manager = terminal_app_get_object_manager (TERMINAL_APP (data->app));
-  g_dbus_object_manager_server_export (object_manager, G_DBUS_OBJECT_SKELETON (object));
-  g_object_unref (object);
-
-  /* And export the object */
-  g_dbus_object_manager_server_set_connection (object_manager, connection);
-}
-
-static void
-name_acquired_cb (GDBusConnection *connection,
-                  const char *name,
-                  gpointer user_data)
-{
-  MainData *data = (MainData *) user_data;
-
-  _terminal_debug_print (TERMINAL_DEBUG_FACTORY,
-                         "Acquired the name %s on the starter bus\n", name);
-  data->owns_name = TRUE;
-
-  if (g_main_loop_is_running (data->loop))
-    g_main_loop_quit (data->loop);
-}
-
-static void
-name_lost_cb (GDBusConnection *connection,
-              const char *name,
-              gpointer user_data)
-{
-  MainData *data = (MainData *) user_data;
-
-  if (connection) {
-    _terminal_debug_print (TERMINAL_DEBUG_FACTORY,
-                          "Lost the name %s on the starter bus\n", name);
-  } else {
-    g_printerr ("Failed to connect to starter bus\n");
-  }
-
-  data->owns_name = FALSE;
-
-  if (g_main_loop_is_running (data->loop))
-    g_main_loop_quit (data->loop);
-}
-
 int
 main (int argc, char **argv)
 {
-  MainData data;
-  guint owner_id;
+  GApplication *app;
   int exit_code = EXIT_FAILURE;
   const char *home_dir;
   GError *error = NULL;
@@ -166,33 +97,24 @@ main (int argc, char **argv)
     exit (EXIT_FAILURE);
   }
 
-  data.app = terminal_app_new ();
-  data.loop = g_main_loop_new (NULL, FALSE);
-  data.owns_name = FALSE;
-
-  owner_id = g_bus_own_name (G_BUS_TYPE_STARTER,
-                             bus_name ? bus_name : TERMINAL_UNIQUE_NAME,
-                             G_BUS_NAME_OWNER_FLAGS_NONE,
-                             bus_acquired_cb,
-                             name_acquired_cb,
-                             name_lost_cb,
-                             &data, NULL);
-
-  g_main_loop_run (data.loop);
-
-  g_main_loop_unref (data.loop);
-  data.loop = NULL;
+  app = terminal_app_new (bus_name);
+  g_free (bus_name);
 
-  if (!data.owns_name)
+  if (!g_application_register (app, NULL, &error)) {
+    g_printerr ("Failed to register application: %s\n", error->message);
+    g_error_free (error);
     goto out;
+  }
 
-  exit_code = g_application_run (data.app, 0, NULL);
+  if (g_application_get_is_remote (app)) {
+    /* How the fuck did this happen? */
+    g_printerr ("Cannot be remote instance!\n");
+    goto out;
+  }
 
-  g_bus_unown_name (owner_id);
+  exit_code = g_application_run (app, 0, NULL);
 
 out:
-
-  g_free (bus_name);
   terminal_app_shutdown ();
 
   return exit_code;
diff --git a/src/terminal-app.c b/src/terminal-app.c
index 32efb7c..73126d5 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -1236,9 +1236,6 @@ terminal_app_init (TerminalApp *app)
   terminal_app_ensure_any_profiles (app);
 
   terminal_accels_init ();
-
-  /* FIXMEchpe: find out why this is necessary... */
-  g_application_hold (G_APPLICATION (app));
 }
 
 static void
@@ -1269,11 +1266,6 @@ terminal_app_finalize (GObject *object)
 
   terminal_accels_shutdown ();
 
-  if (app->object_manager) {
-    g_dbus_object_manager_server_unexport (app->object_manager, TERMINAL_FACTORY_OBJECT_PATH);
-    g_object_unref (app->object_manager);
-  }
-
   G_OBJECT_CLASS (terminal_app_parent_class)->finalize (object);
 
   global_app = NULL;
@@ -1282,8 +1274,45 @@ terminal_app_finalize (GObject *object)
 static void
 terminal_app_real_quit (TerminalApp *app)
 {
-  /* Release the hold added when creating the app  */
-  g_application_release (G_APPLICATION (app));
+}
+
+static gboolean
+terminal_app_dbus_register (GApplication    *application,
+                            GDBusConnection *connection,
+                            const gchar     *object_path,
+                            GCancellable    *cancellable,
+                            GError         **error)
+{
+  TerminalApp *app = TERMINAL_APP (application);
+  TerminalObjectSkeleton *object;
+  TerminalFactory *factory;
+
+  object = terminal_object_skeleton_new (TERMINAL_FACTORY_OBJECT_PATH);
+  factory = terminal_factory_impl_new ();
+  terminal_object_skeleton_set_factory (object, factory);
+  g_object_unref (factory);
+
+  app->object_manager = g_dbus_object_manager_server_new (TERMINAL_OBJECT_PATH_PREFIX);
+  g_dbus_object_manager_server_export (app->object_manager, G_DBUS_OBJECT_SKELETON (object));
+  g_object_unref (object);
+
+  /* And export the object */
+  g_dbus_object_manager_server_set_connection (app->object_manager, connection);
+  return TRUE;
+}
+
+static void
+terminal_app_dbus_unregister (GApplication    *application,
+                              GDBusConnection *connection,
+                              const gchar     *object_path)
+{
+  TerminalApp *app = TERMINAL_APP (application);
+
+  if (app->object_manager) {
+    g_dbus_object_manager_server_unexport (app->object_manager, TERMINAL_FACTORY_OBJECT_PATH);
+    g_object_unref (app->object_manager);
+    app->object_manager = NULL;
+  }
 }
 
 static void
@@ -1296,6 +1325,8 @@ terminal_app_class_init (TerminalAppClass *klass)
 
   g_application_class->activate = terminal_app_activate;
   g_application_class->startup = terminal_app_startup;
+  g_application_class->dbus_register = terminal_app_dbus_register;
+  g_application_class->dbus_unregister = terminal_app_dbus_unregister;
 
   klass->quit = terminal_app_real_quit;
 
@@ -1330,13 +1361,13 @@ terminal_app_class_init (TerminalAppClass *klass)
 /* Public API */
 
 GApplication *
-terminal_app_new (void)
+terminal_app_new (const char *bus_name)
 {
-  const GApplicationFlags flags = G_APPLICATION_NON_UNIQUE |
+  const GApplicationFlags flags = G_APPLICATION_REQUIRE_DBUS |
                                   G_APPLICATION_IS_SERVICE;
 
   return g_object_new (TERMINAL_TYPE_APP,
-                       "application-id", TERMINAL_UNIQUE_NAME,
+                       "application-id", bus_name ? bus_name : TERMINAL_UNIQUE_NAME,
                        "flags", flags,
                        NULL);
 }
@@ -1648,7 +1679,6 @@ terminal_app_get_system_font (TerminalApp *app)
 GDBusObjectManagerServer *
 terminal_app_get_object_manager (TerminalApp *app)
 {
-  if (app->object_manager == NULL)
-    app->object_manager = g_dbus_object_manager_server_new (TERMINAL_OBJECT_PATH_PREFIX);
+  g_warn_if_fail (app->object_manager != NULL);
   return app->object_manager;
 }
diff --git a/src/terminal-app.h b/src/terminal-app.h
index 855a9cd..1697214 100644
--- a/src/terminal-app.h
+++ b/src/terminal-app.h
@@ -48,7 +48,7 @@ typedef struct _TerminalApp TerminalApp;
 
 GType terminal_app_get_type (void);
 
-GApplication *terminal_app_new (void);
+GApplication *terminal_app_new (const char *bus_name);
 
 TerminalApp* terminal_app_get (void);
 



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