[glib/wip/gapplication] Add GApplication



commit dfd5dd47d4008fd0cffce35957db0ce0116da4cf
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Tue May 18 09:13:08 2010 +0100

    Add GApplication
    
    GApplication is a base class that encapsulates an application.
    
    Every GApplication is, on systems with D-Bus available, exposed as an
    object with a well-known name on the session bus; a GApplication also
    exposes "actions" as D-Bus methods through introspection.
    
    By default, a GApplication is a single instance: no more than one
    GApplication with the same name can exist at the same time.

 gio/Makefile.am         |    5 +
 gio/gapplication.c      |  498 +++++++++++++++++++++++++++++++++++++++++++++++
 gio/gapplication.h      |  131 +++++++++++++
 gio/gio-marshal.list    |    1 +
 gio/gio.h               |    1 +
 gio/gunixapplication.c  |  312 +++++++++++++++++++++++++++++
 gio/tests/Makefile.am   |    4 +
 gio/tests/application.c |  124 ++++++++++++
 8 files changed, 1076 insertions(+), 0 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index d0bd37e..366574d 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -268,6 +268,8 @@ SUBDIRS += tests
 
 libgio_2_0_la_SOURCES =		\
 	gappinfo.c 		\
+	gapplication.c		\
+	gapplication.h		\
 	gasynchelper.c 		\
 	gasynchelper.h 		\
 	gasyncinitable.c	\
@@ -372,6 +374,8 @@ libgio_2_0_la_SOURCES =		\
 	$(marshal_sources) 	\
 	$(NULL)
 
+EXTRA_DIST += gunixapplication.c
+
 $(libgio_2_0_la_OBJECTS): $(marshal_sources)
 
 libgio_2_0_la_LIBADD = \
@@ -425,6 +429,7 @@ gio-win32-res.o: gio.rc
 
 gio_headers =			\
 	gappinfo.h 		\
+	gapplication.h		\
 	gasyncinitable.h	\
 	gasyncresult.h 		\
 	gbufferedinputstream.h 	\
diff --git a/gio/gapplication.c b/gio/gapplication.c
new file mode 100644
index 0000000..80cfcd2
--- /dev/null
+++ b/gio/gapplication.c
@@ -0,0 +1,498 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright © 2010 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Colin Walters <walters verbum org>
+ */
+
+#include "config.h"
+
+#include "gapplication.h"
+#include "gio-marshal.h"
+
+/**
+ * SECTION: gapplication
+ * @title: GApplication
+ * @short_description: Core application class
+ *
+ * A #GApplication is the foundation of an application. There can be at most
+ * one instance of this class.
+ *
+ * Since: 2.24
+ */
+
+G_DEFINE_TYPE (GApplication, g_application, G_TYPE_OBJECT);
+
+enum
+{
+  PROP_0,
+
+  PROP_APPID
+};
+
+enum
+{
+  QUIT,
+  ACTION,
+
+  LAST_SIGNAL
+};
+
+static guint application_signals[LAST_SIGNAL] = { 0 };
+
+typedef struct {
+  char *name;
+  char *description;
+  guint enabled : 1;
+} GApplicationAction;
+
+struct _GApplicationPrivate
+{
+  char *appid;
+  GHashTable *actions; /* name -> GApplicationAction */
+  GMainLoop *mainloop;
+
+  guint actions_changed_id;
+
+#ifdef G_OS_UNIX
+  GDBusConnection *session_bus;
+  GDBusProxy *session_bus_proxy;
+#endif
+};
+
+static GApplication *application_instance = NULL;
+
+static void     _g_application_platform_init                    (GApplication  *app);
+static gboolean _g_application_platform_acquire_single_instance (const char    *appid,
+                                                                 GError       **error);
+static void     _g_application_platform_default_exit            (void) G_GNUC_NORETURN;
+static void     _g_application_platform_on_actions_changed      (GApplication  *app);
+
+#ifdef G_OS_UNIX
+#include "gunixapplication.c"
+#endif
+
+static gboolean
+g_application_default_quit (GApplication *application,
+                            guint         timestamp)
+{
+  g_return_val_if_fail (application->priv->mainloop != NULL, FALSE);
+  g_main_loop_quit (application->priv->mainloop);
+
+  return TRUE;
+}
+
+static void
+g_application_default_run (GApplication *application)
+{
+  GMainLoop *mainloop = g_application_get_mainloop (application);
+
+  g_main_loop_run (mainloop);
+}
+
+static gboolean
+timeout_handle_actions_changed (gpointer user_data)
+{
+  GApplication *application = user_data;
+
+  application->priv->actions_changed_id = 0;
+
+  _g_application_platform_on_actions_changed (application);
+
+  return FALSE;
+}
+
+static void
+queue_actions_change_notification (GApplication *application)
+{
+  GApplicationPrivate *priv = application->priv;
+
+  if (priv->actions_changed_id == 0)
+    priv->actions_changed_id = g_timeout_add (0, timeout_handle_actions_changed, application);
+}
+
+static void
+g_application_action_free (gpointer data)
+{
+  if (G_LIKELY (data != NULL))
+    {
+      GApplicationAction *action = data;
+
+      g_free (action->name);
+      g_free (action->description);
+
+      g_slice_free (GApplicationAction, action);
+    }
+}
+
+/**
+ * g_application_new:
+ * @appid: (allow-none): System-dependent application identifier
+ * @flags: Initialization flags
+ *
+ * Create a new #GApplication, or if one has already been initialized,
+ * return the existing instance.  If the application is already running
+ * in another process, this function will terminate the current process.
+ *
+ * This function is defined to call g_type_init() as its very first
+ * action.
+ *
+ * If called multiple times, this function will return the extant
+ * application instance.
+ *
+ * Returns: (transfer full): An application instance
+ */
+GApplication*
+g_application_new (const char        *appid,
+                   GApplicationFlags  flags)
+{
+  GApplication *app;
+  GParameter parameters = { NULL, };
+  
+  g_type_init ();
+    
+  g_return_val_if_fail (application_instance == NULL, NULL);
+  
+  parameters.name = "appid";
+
+  g_value_init (&parameters.value, G_TYPE_STRING);
+  g_value_set_string (&parameters.value, appid);
+
+  app = g_application_try_new (flags, G_TYPE_APPLICATION,
+                               NULL, NULL,
+                               1,
+                               &parameters);
+
+  g_value_unset (&parameters.value);
+
+  return app;
+}
+
+/**
+ * g_application_try_new:
+ * @flags: Application flags
+ * @type: Actual GObject class type to instantiate
+ * @on_other_process_exists: (scope call) (allow-none): Called if this application is already running
+ * @user_data: (closure): User data
+ * @n_parameters: Number of object construction parameters
+ * @args: (array length=8): Object construction parameters
+ *
+ * Attempt to create an application instance.  If the application is
+ * already running in another process, then call @on_other_process_exists.
+ * The default if @on_other_process_exists is %NULL will terminate the
+ * current process.
+ *
+ * This function is defined to call g_type_init() as its very first
+ * action.
+ *
+ * If called multiple times, this function will return the extant
+ * application instance.
+ *
+ * Returns: (transfer full): The application instance
+ */
+GApplication *
+g_application_try_new (GApplicationFlags   flags,
+                       GType               class_type,
+                       GApplicationExistsCallback on_other_process_exists,
+                       gpointer            user_data,
+                       guint               n_parameters,
+                       GParameter         *parameters)
+{
+  if (application_instance != NULL)
+    return g_object_ref (application_instance);
+
+  g_type_init ();
+  
+  if (!(flags & G_APPLICATION_FLAG_DISABLE_SINGLE_INSTANCE))
+    {
+      if (!_g_application_platform_acquire_single_instance (g_value_get_string (&parameters[0].value), NULL))
+        {
+          if (on_other_process_exists)
+            on_other_process_exists (user_data);
+          else
+            _g_application_platform_default_exit ();
+        }
+    }
+
+  return G_APPLICATION (g_object_newv (class_type, n_parameters, parameters));
+}
+
+void
+g_application_add_action (GApplication *app,
+                          const gchar  *name,
+                          const gchar  *description)
+{
+  GApplicationAction *action;
+
+  g_return_if_fail (G_IS_APPLICATION (app));
+  g_return_if_fail (g_hash_table_lookup (app->priv->actions, "name") == NULL);
+  
+  action = g_slice_new (GApplicationAction);
+  action->name = g_strdup (name);
+  action->description = g_strdup (description);
+  action->enabled = TRUE;
+  
+  g_hash_table_insert (app->priv->actions, action->name, action);
+  queue_actions_change_notification (app);
+}
+
+void 
+g_application_invoke_action (GApplication *app,
+                             const char   *name,
+                             guint         timestamp)
+{
+  GApplicationAction *action;
+
+  g_return_if_fail (G_IS_APPLICATION (app));
+  g_return_if_fail (name != NULL);
+  
+  action = g_hash_table_lookup (app->priv->actions, name);
+  g_return_if_fail (action != NULL);
+  if (!action->enabled)
+    return;
+  
+  g_signal_emit (app, application_signals[ACTION], g_quark_from_string (name), name, timestamp);
+}
+
+void
+g_application_set_action_enabled (GApplication *app,
+                                  const char   *name,
+                                  gboolean      enabled)
+{
+  GApplicationAction *action;
+
+  g_return_if_fail (G_IS_APPLICATION (app));
+  g_return_if_fail (name != NULL);
+  
+  enabled = !!enabled;
+  
+  action = g_hash_table_lookup (app->priv->actions, name);
+  g_return_if_fail (action != NULL);
+  if (action->enabled == enabled)
+    return;
+
+  action->enabled = enabled;
+
+  queue_actions_change_notification (app);
+}
+
+/**
+ * g_application_run:
+ * @app: a #GApplication
+ *
+ * Start the application.  The default implementation of this
+ * virtual function will simply run a main loop.
+ */
+void
+g_application_run (GApplication  *app)
+{
+  g_return_if_fail (G_IS_APPLICATION (app));
+  
+  G_APPLICATION_GET_CLASS (app)->run (app);
+}
+
+/**
+ * g_application_quit:
+ * @app: a #GApplication
+ * @timestamp: Platform-specific event timestamp, may be 0 for default
+ *
+ * Request the application exit.  By default, this
+ * method will exit the main loop.
+ *
+ * Returns: %TRUE if the application accepted the request, %FALSE otherwise
+ */
+gboolean
+g_application_quit (GApplication *app, guint timestamp)
+{
+  gboolean retval;
+
+  g_signal_emit (app, application_signals[QUIT], 0, timestamp, &retval);
+
+  return retval;
+}
+
+/**
+ * g_application_get_instance:
+ * 
+ * Returns: (transfer none): The singleton instance of #GApplication, or %NULL if none
+ */
+GApplication *
+g_application_get_instance (void)
+{
+  return application_instance;
+}
+
+/**
+ * g_application_get_id:
+ *
+ * Retrieves the platform specific identifier for the #GApplication
+ * 
+ * Return value: The platform-specific identifier. The returned string
+ *   is owned by the #GApplication instance and it should never be
+ *   modified or freed
+ *
+ * Since: 2.26
+ */
+G_CONST_RETURN char *  
+g_application_get_id (GApplication *app)
+{
+  g_return_val_if_fail (G_IS_APPLICATION (app), NULL);
+
+  return app->priv->appid;
+}
+
+/**
+ * g_application_get_mainloop:
+ * @app: a #GApplication
+ *
+ * Retrieves the #GMainLoop associated with the #GApplication
+ *
+ * Return value: (transfer none): The #GMainLoop associated with
+ *   this application
+ */
+GMainLoop *
+g_application_get_mainloop (GApplication *app)
+{
+  GApplicationPrivate *priv;
+
+  g_return_val_if_fail (G_IS_APPLICATION (app), NULL);
+
+  priv = app->priv;
+
+  if (priv->mainloop == NULL)
+    priv->mainloop = g_main_loop_new (NULL, TRUE);
+
+  return priv->mainloop;
+}
+
+static void
+g_application_init (GApplication *app)
+{
+  app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app,
+                                           G_TYPE_APPLICATION,
+                                           GApplicationPrivate);
+  app->priv->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                              g_application_action_free);
+  _g_application_platform_init (app);
+}
+
+static void
+g_application_get_property (GObject    *object,
+                            guint       prop_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+  GApplication *app = G_APPLICATION (object);
+
+  switch (prop_id)
+    {
+      case PROP_APPID:
+        g_value_set_string (value, g_application_get_id (app));
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_application_set_property (GObject      *object,
+                            guint         prop_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+  GApplication *app = G_APPLICATION (object);
+
+  switch (prop_id)
+    {
+      case PROP_APPID:
+        app->priv->appid = g_value_dup_string (value);
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static GObject*
+g_application_constructor  (GType                  type,
+                            guint                  n_construct_properties,
+                            GObjectConstructParam *construct_params)
+{
+  GObject *object;
+  
+  if (application_instance != NULL)
+    return G_OBJECT (g_object_ref (application_instance));
+
+  object = (* G_OBJECT_CLASS (g_application_parent_class)->constructor) (type,
+                                                                         n_construct_properties,
+                                                                         construct_params);
+  application_instance = G_APPLICATION (object);
+  
+  return object; 
+}
+
+static void
+g_application_class_init (GApplicationClass *klass)
+{
+  GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
+
+  g_type_class_add_private (klass, sizeof (GApplicationPrivate));
+
+  gobject_class->constructor = g_application_constructor;
+  gobject_class->set_property = g_application_set_property;
+  gobject_class->get_property = g_application_get_property;
+  
+  klass->run = g_application_default_run;
+  klass->quit = g_application_default_quit;
+  
+  application_signals[QUIT] =
+    g_signal_new (g_intern_static_string ("quit"),
+		  G_OBJECT_CLASS_TYPE (klass),
+		  G_SIGNAL_RUN_LAST,
+		  G_STRUCT_OFFSET (GApplicationClass, quit),
+                  g_signal_accumulator_true_handled, NULL,
+		  _gio_marshal_BOOLEAN__UINT,
+		  G_TYPE_BOOLEAN, 1,
+                  G_TYPE_UINT);
+
+  application_signals[ACTION] =
+    g_signal_new (g_intern_static_string ("action"),
+		  G_OBJECT_CLASS_TYPE (klass),
+		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED,
+		  G_STRUCT_OFFSET (GApplicationClass, action),
+                  NULL, NULL,
+                  _gio_marshal_VOID__INT,
+		  G_TYPE_NONE, 1,
+                  G_TYPE_INT); 
+ 
+   /**
+   * GApplication:appid:
+   *
+   * On Freedesktop platforms, this is a DBus name that will be acquired on the
+   * session bus.
+   *
+   * @TODO on windows, have this be the guid?
+   * @TODO MacOS X
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_APPID,
+                                   g_param_spec_string ("appid",
+                                                        "Application ID",
+                                                        "Platform-specific identifer for this application",
+                                                        NULL,
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+}
diff --git a/gio/gapplication.h b/gio/gapplication.h
new file mode 100644
index 0000000..aadee5a
--- /dev/null
+++ b/gio/gapplication.h
@@ -0,0 +1,131 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright © 2010 Red Hat, Inc
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Colin Walters <walters verbum org>
+ */
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_APPLICATION_H__
+#define __G_APPLICATION_H__
+
+#include <glib-object.h>
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_APPLICATION                              (g_application_get_type ())
+#define G_APPLICATION(inst)                             (G_TYPE_CHECK_INSTANCE_CAST ((inst),                     \
+                                                             G_TYPE_APPLICATION, GApplication))
+#define G_APPLICATION_CLASS(class)                      (G_TYPE_CHECK_CLASS_CAST ((class),                       \
+                                                             G_TYPE_APPLICATION, GApplicationClass))
+#define G_IS_APPLICATION(inst)                          (G_TYPE_CHECK_INSTANCE_TYPE ((inst),                     \
+                                                             G_TYPE_APPLICATION))
+#define G_IS_APPLICATION_CLASS(class)                   (G_TYPE_CHECK_CLASS_TYPE ((class),                       \
+                                                             G_TYPE_APPLICATION))
+#define G_APPLICATION_GET_CLASS(inst)                   (G_TYPE_INSTANCE_GET_CLASS ((inst),                      \
+                                                             G_TYPE_APPLICATION, GApplicationClass))
+
+typedef struct _GApplication                             GApplication;
+typedef struct _GApplicationPrivate                      GApplicationPrivate;
+typedef struct _GApplicationClass                        GApplicationClass;
+
+/**
+ * GApplicationClass:
+ * @quit: virtual method called when a quit is requested
+ **/
+struct _GApplicationClass
+{
+  GObjectClass parent_class;
+
+  /* signals */
+
+  void (* action)(GApplication *app, const char *name, guint timestamp);
+  gboolean (* quit) (GApplication *app, guint timestamp);
+  
+  /* vfuncs */
+  
+  void (*run) (GApplication *app);
+
+  /* Padding for future expansion */
+  void (*_g_reserved1) (void);
+  void (*_g_reserved2) (void);
+  void (*_g_reserved3) (void);
+  void (*_g_reserved4) (void);
+  void (*_g_reserved5) (void);
+  void (*_g_reserved6) (void);
+};
+
+struct _GApplication
+{
+  GObject parent_instance;
+  GApplicationPrivate *priv;
+};
+
+/**
+ * GApplicationFlags:
+ * @G_APPLICATION_FLAG_DISABLE_SINGLE_INSTANCE: If specified, do not check in construction for another running process
+ *
+ * Flags used when creating a #GApplication.
+ */
+typedef enum
+{
+    G_APPLICATION_FLAG_DISABLE_SINGLE_INSTANCE = 1 << 0
+} GApplicationFlags;
+
+typedef void (*GApplicationExistsCallback) (gpointer user_data);
+
+GType                   g_application_get_type                      (void) G_GNUC_CONST;
+
+GApplication *          g_application_new                           (const char         *appid,
+                                                                     GApplicationFlags   flags);
+                 
+GApplication *          g_application_try_new                       (GApplicationFlags   flags,
+                                                                     GType               class_type,
+                                                                     GApplicationExistsCallback on_other_process_exists,
+                                                                     gpointer            user_data,
+                                                                     guint               n_parameters,
+                                                                     GParameter         *parameters);
+
+GApplication *          g_application_get_instance                  (void);
+
+const char *            g_application_get_id                        (GApplication      *app);
+
+void                    g_application_add_action                    (GApplication      *app,
+                                                                     const char        *name,
+                                                                     const char        *description);
+
+void                    g_application_set_action_enabled            (GApplication      *app,
+                                                                     const char        *name,
+                                                                     gboolean           enabled);
+
+void                    g_application_invoke_action                 (GApplication      *app,
+                                                                     const char        *name,
+                                                                     guint              timestamp);
+
+GMainLoop *             g_application_get_mainloop                  (GApplication      *app);
+
+void                    g_application_run                           (GApplication      *app);             
+gboolean                g_application_quit                          (GApplication      *app, guint timestamp);
+
+G_END_DECLS
+
+#endif /* __G_APPLICATION_H__ */
diff --git a/gio/gio-marshal.list b/gio/gio-marshal.list
index 7899b9d..644dfdc 100644
--- a/gio/gio-marshal.list
+++ b/gio/gio-marshal.list
@@ -9,3 +9,4 @@ BOOL:UINT
 VOID:STRING,STRING,BOXED
 VOID:BOOL,BOXED
 VOID:BOXED,BOXED
+VOID:INT
diff --git a/gio/gio.h b/gio/gio.h
index dd5c811..fee37ab 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -28,6 +28,7 @@
 #include <gio/giotypes.h>
 
 #include <gio/gappinfo.h>
+#include <gio/gapplication.h>
 #include <gio/gasyncresult.h>
 #include <gio/gasyncinitable.h>
 #include <gio/gbufferedinputstream.h>
diff --git a/gio/gunixapplication.c b/gio/gunixapplication.c
new file mode 100644
index 0000000..185a115
--- /dev/null
+++ b/gio/gunixapplication.c
@@ -0,0 +1,312 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright © 2010 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Colin Walters <walters verbum org>
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include "gioerror.h"
+
+#include "gdbusconnection.h"
+#include "gdbusintrospection.h"
+#include "gdbusmethodinvocation.h"
+
+static void
+application_dbus_method_call (GDBusConnection       *connection,
+                              const gchar           *sender,
+                              const gchar           *object_path,
+                              const gchar           *interface_name,
+                              const gchar           *method_name,
+                              GVariant              *parameters,
+                              GDBusMethodInvocation *invocation,
+                              gpointer               user_data)
+{
+  if (method_name == NULL && *method_name == '\0')
+    return;
+
+  if (strcmp (method_name, "Quit") == 0)
+    {
+      guint32 timestamp;
+      g_variant_get (parameters, "(u)", &timestamp);
+      
+      g_application_quit (application_instance, timestamp);
+      
+      g_dbus_method_invocation_return_value (invocation, NULL);
+    }
+  else if (strcmp (method_name, "ListActions") == 0)
+    {
+      char **actions;
+      GHashTableIter iter;
+      gpointer key, value;
+      int i;
+      GVariant *return_args;
+      GVariant *result;
+      
+      actions = g_new (char *, g_hash_table_size (application_instance->priv->actions) + 1);
+      g_hash_table_iter_init (&iter, application_instance->priv->actions);
+      i = 0;
+      while (g_hash_table_iter_next (&iter, &key, &value))
+        actions[i++] = key;
+      actions[i] = NULL;
+      
+      result = g_variant_new_strv ((const char *const*)actions, -1);
+      return_args = g_variant_new_tuple (&result, 1);
+      g_dbus_method_invocation_return_value (invocation, return_args);
+      g_variant_unref (return_args);
+      g_variant_unref (result);
+      g_free (actions);
+    }
+  else if (strcmp (method_name, "InvokeAction") == 0)
+    {
+      const char *action_name;
+      guint32 timestamp;
+      GApplicationAction *action;
+      
+      g_variant_get (parameters, "(su)", &action_name, &timestamp);
+      
+      action = g_hash_table_lookup (application_instance->priv->actions, action_name);
+      
+      if (!action)
+        {
+          char *errmsg  = g_strdup_printf ("Invalid action: %s", action_name);
+          g_dbus_method_invocation_return_dbus_error (invocation, "org.freedesktop.DBus.Application.InvalidAction", errmsg);
+          g_free (errmsg);
+          return;
+        }
+      
+      g_signal_emit (application_instance, application_signals[ACTION], g_quark_from_string (action_name), (guint)timestamp);
+      
+      g_dbus_method_invocation_return_value (invocation, NULL);
+    }
+}
+
+static const GDBusArgInfo application_quit_in_args[] =
+{
+  {
+    -1,
+    "timestamp",
+    "u",
+    NULL
+  }
+};
+
+static const GDBusArgInfo * const application_quit_in_args_p[] = {
+  &application_quit_in_args[0],
+  NULL
+};
+
+static const GDBusArgInfo application_list_actions_out_args[] =
+{
+  {
+    -1,
+    "actions",
+    "as",
+    NULL
+  }
+};
+
+static const GDBusArgInfo * const application_list_actions_out_args_p[] = {
+  &application_list_actions_out_args[0],
+  NULL
+};
+
+static const GDBusArgInfo application_invoke_action_in_args[] =
+{
+  {
+    -1,
+    "action",
+    "s",
+    NULL
+  },
+  {
+    -1,
+    "timestamp",
+    "u",
+    NULL
+  }
+};
+
+static const GDBusArgInfo * const application_invoke_action_in_args_p[] = {
+  &application_invoke_action_in_args[0],
+  &application_invoke_action_in_args[1],
+  NULL
+};
+
+static const GDBusMethodInfo application_quit_method_info =
+{
+  -1,
+  "Quit",
+  NULL,
+  NULL,
+  NULL
+};
+
+static const GDBusMethodInfo application_list_actions_method_info =
+{
+  -1,
+  "ListActions",
+  NULL,
+  (GDBusArgInfo **) &application_list_actions_out_args_p,
+  NULL
+};
+
+static const GDBusMethodInfo application_invoke_action_method_info =
+{
+  -1,
+  "InvokeAction",
+  (GDBusArgInfo **) &application_invoke_action_in_args_p,
+  NULL,
+  NULL
+};
+
+static const GDBusMethodInfo * const application_dbus_method_info_p[] =
+{
+  &application_quit_method_info,
+  &application_list_actions_method_info,
+  &application_invoke_action_method_info,
+  NULL
+};
+
+static const GDBusSignalInfo application_dbus_signal_info[] =
+{
+  {
+    -1,
+    "ActionsChanged",
+    NULL,
+    NULL
+  }
+};
+
+static const GDBusSignalInfo * const application_dbus_signal_info_p[] = {
+  &application_dbus_signal_info[0],
+  NULL
+};
+
+static const GDBusInterfaceInfo application_dbus_interface_info =
+{
+  -1,
+  "org.freedesktop.Application",
+  (GDBusMethodInfo **) application_dbus_method_info_p,
+  (GDBusSignalInfo **) application_dbus_signal_info_p,
+  NULL,
+};
+
+static GDBusInterfaceVTable application_dbus_vtable =
+{
+  application_dbus_method_call,
+  NULL,
+  NULL
+};
+
+static void
+_g_application_platform_init (GApplication *app)
+{
+  GApplicationPrivate *priv = app->priv;
+  GError *error = NULL;
+  guint registration_id;
+  
+  priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+  if (priv->session_bus == NULL)
+    {
+      if (error != NULL)
+        {
+          g_warning ("Failed to connect to the session bus: %s", error->message);
+          g_clear_error (&error);
+        }
+      else
+        g_warning ("Failed to connect to the session bus");
+
+      return;
+    }
+    
+  registration_id = g_dbus_connection_register_object (priv->session_bus, "/org/freedesktop/Application",
+                                                       &application_dbus_interface_info,
+                                                       &application_dbus_vtable,
+                                                       NULL, NULL,
+                                                       &error);
+  if (registration_id == 0)
+    g_error ("%s", error->message);
+}
+
+static gboolean
+_g_application_platform_acquire_single_instance (const char  *appid,
+                                                 GError     **error)
+{
+  GDBusConnection *connection;
+  gboolean ret = FALSE;
+  GVariant *request_result;
+  guint32 request_status;
+
+  if (application_instance != NULL)
+    connection = g_object_ref (application_instance->priv->session_bus);    
+  else
+    {
+      connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+      if (connection == NULL)
+        return FALSE;
+    }
+
+  request_result = g_dbus_connection_call_sync (connection,
+                                                "org.freedesktop.DBus",
+                                                "/org/freedesktop/DBus",
+                                                "org.freedesktop.DBus",
+                                                "RequestName",
+                                                g_variant_new ("(su)", appid, 0x4),
+                                                0, -1, NULL,
+                                                error);
+  if (request_result == NULL)
+    goto out;     
+
+  if (strcmp (g_variant_get_type_string (request_result), "(u)") != 0)
+    goto out;
+  g_variant_get (request_result, "(u)", &request_status);
+  if (request_status == 1 || request_status == 4)
+    {
+      ret = TRUE;
+    }
+  else
+    {
+      char *errmsg = g_strdup ("Unknown error");
+      if (request_status == 3)
+        errmsg = g_strdup_printf ("Another process has name \"%s\"", appid);
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", errmsg);
+      g_free (errmsg);
+    } 
+out:
+  g_object_unref (connection);
+  
+  return ret;
+}
+
+static void
+_g_application_platform_on_actions_changed (GApplication *app)
+{
+  g_dbus_connection_emit_signal (app->priv->session_bus, NULL,
+                                 "/org/freedesktop/Application",
+                                 "org.freedesktop.Application",
+                                 "ActionsChanged", NULL, NULL);
+}
+
+static void
+_g_application_platform_default_exit (void)
+{
+  exit (0);
+}
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index f4b3e0d..0b1da7b 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -50,6 +50,7 @@ TEST_PROGS +=	 		\
 	gdbus-error		\
 	gdbus-peer		\
 	gdbus-exit-on-close	\
+	application		\
 	$(NULL)
 
 SAMPLE_PROGS = 				\
@@ -243,6 +244,9 @@ gdbus_example_proxy_subclass_LDADD   = $(progs_ldadd)
 gdbus_example_export_SOURCES = gdbus-example-export.c
 gdbus_example_export_LDADD   = $(progs_ldadd)
 
+application_SOURCES = application.c
+application_LDADD   = $(progs_ldadd)
+
 EXTRA_DIST += \
 	socket-common.c						\
 	org.gtk.test.gschema					\
diff --git a/gio/tests/application.c b/gio/tests/application.c
new file mode 100644
index 0000000..71526d1
--- /dev/null
+++ b/gio/tests/application.c
@@ -0,0 +1,124 @@
+#include <stdlib.h>
+#include <gio.h>
+#include <gstdio.h>
+
+enum
+{
+  INVOKE_ACTION,
+  CHECK_ACTION,
+  DISABLE_ACTION,
+  INVOKE_DISABLED_ACTION,
+  CHECK_DISABLED_ACTION,
+  END
+};
+
+static guint timestamp = 0;
+static gint state = -1;
+static gboolean action_invoked = FALSE;
+
+static void
+on_app_action (GApplication *application,
+               guint         action_timestamp)
+{
+  action_invoked = TRUE;
+}
+
+static gboolean
+check_invoke_action (gpointer data)
+{
+  GApplication *application = data;
+
+  if (state == INVOKE_ACTION)
+    {
+      timestamp = (guint) time (NULL);
+
+      if (g_test_verbose ())
+        g_print ("Invoking About...\n");
+
+      g_application_invoke_action (application, "About", timestamp);
+      state = CHECK_ACTION;
+      return TRUE;
+    }
+
+  if (state == CHECK_ACTION)
+    {
+      if (g_test_verbose ())
+        g_print ("Verifying About invocation...\n");
+
+      g_assert (action_invoked);
+      state = DISABLE_ACTION;
+      return TRUE;
+    }
+
+  if (state == DISABLE_ACTION)
+    {
+      if (g_test_verbose ())
+        g_print ("Disabling About...\n");
+
+      g_application_set_action_enabled (application, "About", FALSE);
+      action_invoked = FALSE;
+      state = INVOKE_DISABLED_ACTION;
+      return TRUE;
+    }
+
+  if (state == INVOKE_DISABLED_ACTION)
+    {
+      if (g_test_verbose ())
+        g_print ("Invoking disabled About action...\n");
+
+      g_application_invoke_action (application, "About", (guint) time (NULL));
+      state = CHECK_DISABLED_ACTION;
+      return TRUE;
+    }
+
+  if (state == CHECK_DISABLED_ACTION)
+    {
+      if (g_test_verbose ())
+        g_print ("Verifying lack of About invocation...\n");
+
+      g_assert (!action_invoked);
+      state = END;
+      return TRUE;
+    }
+
+  if (state == END)
+    {
+      if (g_test_verbose ())
+        g_print ("Test complete\n");
+
+      g_application_quit (application, (guint) time (NULL));
+      return FALSE;
+    }
+
+  g_assert_not_reached ();
+}
+
+static void
+test_basic (void)
+{
+  GApplication *app;
+
+  app = g_application_new ("org.gtk.TestApplication", 0);
+  g_application_add_action (app, "About", "Print an about message");
+
+  g_signal_connect (app, "action::About", G_CALLBACK (on_app_action), NULL);
+
+  state = INVOKE_ACTION;
+  g_timeout_add (100, check_invoke_action, app);
+
+  g_application_run (app);
+
+  g_assert (state == END);
+  g_object_unref (app);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/application/basic", test_basic);
+
+  return g_test_run ();
+}



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