GApplication: Add a menu example to the docs
[platform/upstream/glib.git] / gio / gapplication.c
index ebcd3c2..fe94183 100644 (file)
@@ -27,6 +27,7 @@
 #include "gapplicationcommandline.h"
 #include "gapplicationimpl.h"
 #include "gactiongroup.h"
+#include "gmenumodel.h"
 #include "gsettings.h"
 
 #include "gioenumtypes.h"
  * GApplication also implements the #GActionGroup interface and lets you
  * easily export actions by adding them with g_application_set_action_group().
  * When invoking an action by calling g_action_group_activate_action() on
- * the application, it is always invoked in the primary instance.
+ * the application, it is always invoked in the primary instance. The actions
+ * are also exported on the session bus, and GIO provides the #GDBusActionGroup
+ * wrapper to conveniently access them remotely. Additionally,
+ * g_application_set_menu() can be used to export representation data
+ * for the actions, in the form of a  #GMenuModel.
  *
  * There is a number of different entry points into a #GApplication:
  * <itemizedlist>
  * </xi:include>
  * </programlisting>
  * </example>
+ *
+ * <example id="gapplication-example-menu"><title>A GApplication with menus</title>
+ * <programlisting>
+ * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-menu.c">
+ *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
+ * </xi:include>
+ * </programlisting>
+ * </example>
  */
 
 struct _GApplicationPrivate
@@ -149,7 +162,7 @@ struct _GApplicationPrivate
   gchar             *id;
 
   GActionGroup      *actions;
-  GMainLoop         *mainloop;
+  GMenuModel        *menu;
 
   guint              inactivity_timeout_id;
   guint              inactivity_timeout;
@@ -160,7 +173,7 @@ struct _GApplicationPrivate
   guint              did_startup : 1;
   guint              did_shutdown : 1;
 
-  GHashTable        *remote_actions;  /* string -> RemoteActionInfo */
+  GActionGroup      *remote_actions;
   GApplicationImpl  *impl;
 };
 
@@ -172,7 +185,8 @@ enum
   PROP_IS_REGISTERED,
   PROP_IS_REMOTE,
   PROP_INACTIVITY_TIMEOUT,
-  PROP_ACTION_GROUP
+  PROP_ACTION_GROUP,
+  PROP_MENU
 };
 
 enum
@@ -364,22 +378,6 @@ g_application_real_add_platform_data (GApplication    *application,
 {
 }
 
-static void
-g_application_real_quit_mainloop (GApplication *application)
-{
-  if (application->priv->mainloop != NULL)
-    g_main_loop_quit (application->priv->mainloop);
-}
-
-static void
-g_application_real_run_mainloop (GApplication *application)
-{
-  if (application->priv->mainloop == NULL)
-    application->priv->mainloop = g_main_loop_new (NULL, FALSE);
-
-  g_main_loop_run (application->priv->mainloop);
-}
-
 /* GObject implementation stuff {{{1 */
 static void
 g_application_set_property (GObject      *object,
@@ -410,6 +408,11 @@ g_application_set_property (GObject      *object,
                                       g_value_get_object (value));
       break;
 
+    case PROP_MENU:
+      g_application_set_menu (application,
+                              g_value_get_object (value));
+      break;
+
     default:
       g_assert_not_reached ();
     }
@@ -422,7 +425,7 @@ g_application_set_property (GObject      *object,
  *
  * Sets or unsets the group of actions associated with the application.
  *
- * These actions are the actions that can be remotely invoked.
+ * These actions can be invoked remotely.
  *
  * It is an error to call this function after the application has been
  * registered.
@@ -445,6 +448,54 @@ g_application_set_action_group (GApplication *application,
     g_object_ref (application->priv->actions);
 }
 
+/**
+ * g_application_set_menu:
+ * @application: a #GApplication
+ * @menu: (allow-none): a #GMenuModel, or %NULL
+ *
+ * Sets or unsets the menu associated with the application. The menu
+ * provides representation data for the exported actions of @application.
+ *
+ * It is an error to call this function after the application has been
+ * registered.
+ *
+ * Since: 2.32
+ */
+void
+g_application_set_menu (GApplication *application,
+                        GMenuModel   *menu)
+{
+  g_return_if_fail (G_IS_APPLICATION (application));
+  g_return_if_fail (!application->priv->is_registered);
+
+  if (application->priv->menu != NULL)
+    g_object_unref (application->priv->menu);
+
+  application->priv->menu = menu;
+
+  if (application->priv->menu != NULL)
+    g_object_ref (application->priv->menu);
+}
+
+/**
+ * g_application_get_menu:
+ * @application: a #GApplication
+ *
+ * Returns the menu model that has been set
+ * with g_application_set_menu().
+ *
+ * Returns: the #GMenuModel associated with @application
+ *
+ * Since: 2.32
+ */
+GMenuModel *
+g_application_get_menu (GApplication *application)
+{
+  g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
+
+  return application->priv->menu;
+}
+
 static void
 g_application_get_property (GObject    *object,
                             guint       prop_id,
@@ -491,6 +542,9 @@ g_application_constructed (GObject *object)
   GApplication *application = G_APPLICATION (object);
 
   g_assert (application->priv->id != NULL);
+
+  if (g_application_get_default () == NULL)
+    g_application_set_default (application);
 }
 
 static void
@@ -502,8 +556,8 @@ g_application_finalize (GObject *object)
     g_application_impl_destroy (application->priv->impl);
   g_free (application->priv->id);
 
-  if (application->priv->mainloop)
-    g_main_loop_unref (application->priv->mainloop);
+  if (g_application_get_default () == application)
+    g_application_set_default (NULL);
 
   G_OBJECT_CLASS (g_application_parent_class)
     ->finalize (object);
@@ -536,8 +590,6 @@ g_application_class_init (GApplicationClass *class)
   class->command_line = g_application_real_command_line;
   class->local_command_line = g_application_real_local_command_line;
   class->add_platform_data = g_application_real_add_platform_data;
-  class->quit_mainloop = g_application_real_quit_mainloop;
-  class->run_mainloop = g_application_real_run_mainloop;
 
   g_object_class_install_property (object_class, PROP_APPLICATION_ID,
     g_param_spec_string ("application-id",
@@ -579,6 +631,13 @@ g_application_class_init (GApplicationClass *class)
                          G_TYPE_ACTION_GROUP,
                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (object_class, PROP_MENU,
+    g_param_spec_object ("menu",
+                         P_("Menu model"),
+                         P_("The menu that the application exports"),
+                         G_TYPE_MENU_MODEL,
+                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
+
   /**
    * GApplication::startup:
    * @application: the application
@@ -696,7 +755,6 @@ get_platform_data (GApplication *application)
 /**
  * g_application_id_is_valid:
  * @application_id: a potential application identifier
- * @returns: %TRUE if @application_id is valid
  *
  * Checks if @application_id is a valid application identifier.
  *
@@ -712,6 +770,8 @@ get_platform_data (GApplication *application)
  *   <listitem>Application identifiers must not contain consecutive '.' (period) characters.</listitem>
  *   <listitem>Application identifiers must not exceed 255 characters.</listitem>
  * </itemizedlist>
+ *
+ * Returns: %TRUE if @application_id is valid
  **/
 gboolean
 g_application_id_is_valid (const gchar *application_id)
@@ -762,13 +822,14 @@ g_application_id_is_valid (const gchar *application_id)
  * g_application_new:
  * @application_id: the application id
  * @flags: the application flags
- * @returns: a new #GApplication instance
  *
  * Creates a new #GApplication instance.
  *
  * This function calls g_type_init() for you.
  *
  * The application id must be valid.  See g_application_id_is_valid().
+ *
+ * Returns: a new #GApplication instance
  **/
 GApplication *
 g_application_new (const gchar       *application_id,
@@ -788,10 +849,11 @@ g_application_new (const gchar       *application_id,
 /**
  * g_application_get_application_id:
  * @application: a #GApplication
- * @returns: the identifier for @application, owned by @application
  *
  * Gets the unique identifier for @application.
  *
+ * Returns: the identifier for @application, owned by @application
+ *
  * Since: 2.28
  **/
 const gchar *
@@ -837,12 +899,13 @@ g_application_set_application_id (GApplication *application,
 /**
  * g_application_get_flags:
  * @application: a #GApplication
- * @returns: the flags for @application
  *
  * Gets the flags for @application.
  *
  * See #GApplicationFlags.
  *
+ * Returns: the flags for @application
+ *
  * Since: 2.28
  **/
 GApplicationFlags
@@ -937,13 +1000,14 @@ g_application_set_inactivity_timeout (GApplication *application,
 /**
  * g_application_get_is_registered:
  * @application: a #GApplication
- * @returns: %TRUE if @application is registered
  *
  * Checks if @application is registered.
  *
  * An application is registered if g_application_register() has been
  * successfully called.
  *
+ * Returns: %TRUE if @application is registered
+ *
  * Since: 2.28
  **/
 gboolean
@@ -957,7 +1021,6 @@ g_application_get_is_registered (GApplication *application)
 /**
  * g_application_get_is_remote:
  * @application: a #GApplication
- * @returns: %TRUE if @application is remote
  *
  * Checks if @application is remote.
  *
@@ -970,6 +1033,8 @@ g_application_get_is_registered (GApplication *application)
  * g_application_register() has been called.  See
  * g_application_get_is_registered().
  *
+ * Returns: %TRUE if @application is remote
+ *
  * Since: 2.28
  **/
 gboolean
@@ -987,7 +1052,6 @@ g_application_get_is_remote (GApplication *application)
  * @application: a #GApplication
  * @cancellable: a #GCancellable, or %NULL
  * @error: a pointer to a NULL #GError, or %NULL
- * @returns: %TRUE if registration succeeded
  *
  * Attempts registration of the application.
  *
@@ -1016,6 +1080,8 @@ g_application_get_is_remote (GApplication *application)
  * instance is or is not the primary instance of the application.  See
  * g_application_get_is_remote() for that.
  *
+ * Returns: %TRUE if registration succeeded
+ *
  * Since: 2.28
  **/
 gboolean
@@ -1088,8 +1154,7 @@ inactivity_timeout_expired (gpointer data)
 {
   GApplication *application = G_APPLICATION (data);
 
-  G_APPLICATION_GET_CLASS (application)
-    ->quit_mainloop (application);
+  application->priv->inactivity_timeout_id = 0;
 
   return FALSE;
 }
@@ -1111,17 +1176,9 @@ g_application_release (GApplication *application)
 {
   application->priv->use_count--;
 
-  if (application->priv->use_count == 0)
-    {
-      if (application->priv->inactivity_timeout)
-        application->priv->inactivity_timeout_id =
-          g_timeout_add (application->priv->inactivity_timeout,
-                         inactivity_timeout_expired, application);
-
-      else
-        G_APPLICATION_GET_CLASS (application)
-          ->quit_mainloop (application);
-    }
+  if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
+    application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
+                                                              inactivity_timeout_expired, application);
 }
 
 /* Activate, Open {{{1 */
@@ -1203,7 +1260,6 @@ g_application_open (GApplication  *application,
  * @application: a #GApplication
  * @argc: the argc from main() (or 0 if @argv is %NULL)
  * @argv: (array length=argc) (allow-none): the argv from main(), or %NULL
- * @returns: the exit status
  *
  * Runs the application.
  *
@@ -1265,13 +1321,15 @@ g_application_open (GApplication  *application,
  *
  * If, after the above is done, the use count of the application is zero
  * then the exit status is returned immediately.  If the use count is
- * non-zero then the mainloop is run until the use count falls to zero,
- * at which point 0 is returned.
+ * non-zero then the default main context is iterated until the use count
+ * falls to zero, at which point 0 is returned.
  *
  * If the %G_APPLICATION_IS_SERVICE flag is set, then the exiting at
  * use count of zero is delayed for a while (ie: the instance stays
  * around to provide its <emphasis>service</emphasis> to others).
  *
+ * Returns: the exit status
+ *
  * Since: 2.28
  **/
 int
@@ -1346,11 +1404,9 @@ g_application_run (GApplication  *application,
         g_timeout_add (10000, inactivity_timeout_expired, application);
     }
 
-  if (application->priv->use_count ||
-      application->priv->inactivity_timeout_id)
+  while (application->priv->use_count || application->priv->inactivity_timeout_id)
     {
-      G_APPLICATION_GET_CLASS (application)
-        ->run_mainloop (application);
+      g_main_context_iteration (NULL, TRUE);
       status = 0;
     }
 
@@ -1372,22 +1428,6 @@ g_application_run (GApplication  *application,
   return status;
 }
 
-static gboolean
-g_application_has_action (GActionGroup *action_group,
-                          const gchar  *action_name)
-{
-  GApplication *application = G_APPLICATION (action_group);
-
-  g_return_val_if_fail (application->priv->is_registered, FALSE);
-
-  if (application->priv->remote_actions != NULL)
-    return g_hash_table_lookup (application->priv->remote_actions,
-                                action_name) != NULL;
-
-  return application->priv->actions &&
-         g_action_group_has_action (application->priv->actions, action_name);
-}
-
 static gchar **
 g_application_list_actions (GActionGroup *action_group)
 {
@@ -1396,23 +1436,7 @@ g_application_list_actions (GActionGroup *action_group)
   g_return_val_if_fail (application->priv->is_registered, NULL);
 
   if (application->priv->remote_actions != NULL)
-    {
-      GHashTableIter iter;
-      gint n, i = 0;
-      gchar **keys;
-      gpointer key;
-
-      n = g_hash_table_size (application->priv->remote_actions);
-      keys = g_new (gchar *, n + 1);
-
-      g_hash_table_iter_init (&iter, application->priv->remote_actions);
-      while (g_hash_table_iter_next (&iter, &key, NULL))
-        keys[i++] = g_strdup (key);
-      g_assert_cmpint (i, ==, n);
-      keys[n] = NULL;
-
-      return keys;
-    }
+    return g_action_group_list_actions (application->priv->remote_actions);
 
   else if (application->priv->actions != NULL)
     return g_action_group_list_actions (application->priv->actions);
@@ -1423,108 +1447,37 @@ g_application_list_actions (GActionGroup *action_group)
 }
 
 static gboolean
-g_application_get_action_enabled (GActionGroup *action_group,
-                                  const gchar  *action_name)
+g_application_query_action (GActionGroup        *group,
+                            const gchar         *action_name,
+                            gboolean            *enabled,
+                            const GVariantType **parameter_type,
+                            const GVariantType **state_type,
+                            GVariant           **state_hint,
+                            GVariant           **state)
 {
-  GApplication *application = G_APPLICATION (action_group);
+  GApplication *application = G_APPLICATION (group);
 
-  g_return_val_if_fail (application->priv->remote_actions != NULL ||
-                        application->priv->actions != NULL, FALSE);
   g_return_val_if_fail (application->priv->is_registered, FALSE);
 
-  if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      return info && info->enabled;
-    }
-
-  return g_action_group_get_action_enabled (application->priv->actions,
-                                            action_name);
-}
-
-static const GVariantType *
-g_application_get_action_parameter_type (GActionGroup *action_group,
-                                         const gchar  *action_name)
-{
-  GApplication *application = G_APPLICATION (action_group);
-
-  g_return_val_if_fail (application->priv->remote_actions != NULL ||
-                        application->priv->actions != NULL, NULL);
-  g_return_val_if_fail (application->priv->is_registered, NULL);
-
-  if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      if (info)
-        return info->parameter_type;
-      else
-        return NULL;
-    }
-
-  return g_action_group_get_action_parameter_type (application->priv->actions,
-                                                   action_name);
-}
-
-static const GVariantType *
-g_application_get_action_state_type (GActionGroup *action_group,
-                                     const gchar  *action_name)
-{
-  GApplication *application = G_APPLICATION (action_group);
-
-  g_return_val_if_fail (application->priv->remote_actions != NULL ||
-                        application->priv->actions != NULL, NULL);
-  g_return_val_if_fail (application->priv->is_registered, NULL);
-
-  if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
-
-      if (info && info->state)
-        return g_variant_get_type (info->state);
-      else
-        return NULL;
-    }
-
-  return g_action_group_get_action_state_type (application->priv->actions,
-                                               action_name);
-}
-
-static GVariant *
-g_application_get_action_state (GActionGroup *action_group,
-                                const gchar  *action_name)
-{
-  GApplication *application = G_APPLICATION (action_group);
-
-  g_return_val_if_fail (application->priv->remote_actions != NULL ||
-                        application->priv->actions != NULL, NULL);
-  g_return_val_if_fail (application->priv->is_registered, NULL);
-
-  if (application->priv->remote_actions)
-    {
-      RemoteActionInfo *info;
-
-      info = g_hash_table_lookup (application->priv->remote_actions,
-                                  action_name);
+  if (application->priv->remote_actions != NULL)
+    return g_action_group_query_action (application->priv->remote_actions,
+                                        action_name,
+                                        enabled,
+                                        parameter_type,
+                                        state_type,
+                                        state_hint,
+                                        state);
 
-      if (info && info->state)
-        return g_variant_ref (info->state);
-      else
-        return NULL;
-    }
+  if (application->priv->actions != NULL)
+    return g_action_group_query_action (application->priv->actions,
+                                        action_name,
+                                        enabled,
+                                        parameter_type,
+                                        state_type,
+                                        state_hint,
+                                        state);
 
-  return g_action_group_get_action_state (application->priv->actions,
-                                          action_name);
+  return FALSE;
 }
 
 static void
@@ -1538,10 +1491,8 @@ g_application_change_action_state (GActionGroup *action_group,
                     application->priv->actions != NULL);
   g_return_if_fail (application->priv->is_registered);
 
-  if (application->priv->is_remote)
-    g_application_impl_change_action_state (application->priv->impl,
-                                            action_name, value,
-                                            get_platform_data (application));
+  if (application->priv->remote_actions)
+    return g_action_group_change_action_state (application->priv->remote_actions, action_name, value);
 
   else
     g_action_group_change_action_state (application->priv->actions,
@@ -1559,10 +1510,8 @@ g_application_activate_action (GActionGroup *action_group,
                     application->priv->actions != NULL);
   g_return_if_fail (application->priv->is_registered);
 
-  if (application->priv->is_remote)
-    g_application_impl_activate_action (application->priv->impl,
-                                        action_name, parameter,
-                                        get_platform_data (application));
+  if (application->priv->remote_actions)
+    return g_action_group_change_action_state (application->priv->remote_actions, action_name, parameter);
 
   else
     g_action_group_activate_action (application->priv->actions,
@@ -1572,16 +1521,54 @@ g_application_activate_action (GActionGroup *action_group,
 static void
 g_application_action_group_iface_init (GActionGroupInterface *iface)
 {
-  iface->has_action = g_application_has_action;
   iface->list_actions = g_application_list_actions;
-
-  iface->get_action_enabled = g_application_get_action_enabled;
-  iface->get_action_parameter_type = g_application_get_action_parameter_type;
-  iface->get_action_state_type = g_application_get_action_state_type;
-  iface->get_action_state = g_application_get_action_state;
+  iface->query_action = g_application_query_action;
   iface->change_action_state = g_application_change_action_state;
   iface->activate_action = g_application_activate_action;
 }
 
+/* Default Application {{{1 */
+
+static GApplication *default_app;
+
+/**
+ * g_application_get_default:
+ * @returns: (transfer none): the default application for this process, or %NULL
+ *
+ * Returns the default #GApplication instance for this process.
+ *
+ * Normally there is only one #GApplication per process and it becomes
+ * the default when it is created.  You can exercise more control over
+ * this by using g_application_set_default().
+ *
+ * If there is no default application then %NULL is returned.
+ *
+ * Since: 2.32
+ **/
+GApplication *
+g_application_get_default (void)
+{
+  return default_app;
+}
+
+/**
+ * g_application_set_default:
+ * @application: the application to set as default, or %NULL
+ *
+ * Sets or unsets the default application for the process, as returned
+ * by g_application_get_default().
+ *
+ * This function does not take its own reference on @application.  If
+ * @application is destroyed then the default application will revert
+ * back to %NULL.
+ *
+ * Since: 2.32
+ **/
+void
+g_application_set_default (GApplication *application)
+{
+  default_app = application;
+}
+
 /* Epilogue {{{1 */
 /* vim:set foldmethod=marker: */