Add GNotification
[platform/upstream/glib.git] / gio / gapplication.c
index 9593d35..d3132e1 100644 (file)
@@ -32,6 +32,9 @@
 #include "gactionmap.h"
 #include "gmenumodel.h"
 #include "gsettings.h"
+#include "gnotification-private.h"
+#include "gnotificationbackend.h"
+#include "gdbusutils.h"
 
 #include "gioenumtypes.h"
 #include "gioenums.h"
  * applications this is the always the current instance.
  * On Linux, the D-Bus session bus is used for communication.
  *
+ * The use of #GApplication differs from some other commonly-used
+ * uniqueness libraries (such as libunique) in important ways.  The
+ * application is not expected to manually register itself and check if
+ * it is the primary instance.  Instead, the <code>main()</code>
+ * function of a #GApplication should do very little more than
+ * instantiating the application instance, possibly connecting signal
+ * handlers, then calling g_application_run().  All checks for
+ * uniqueness are done internally.  If the application is the primary
+ * instance then the startup signal is emitted and the mainloop runs.
+ * If the application is not the primary instance then a signal is sent
+ * to the primary instance and g_application_run() promptly returns.
+ * See the code examples below.
+ *
  * If used, the expected form of an application identifier is very close
  * to that of of a
  * <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface">DBus bus name</ulink>.
  * </xi:include>
  * </programlisting>
  * </example>
+ *
+ * <example id="gapplication-example-dbushooks"><title>Using extra D-Bus hooks with a GApplication</title>
+ * <programlisting>
+ * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-dbushooks.c">
+ *   <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
+ * </xi:include>
+ * </programlisting>
+ * </example>
  */
 
 /**
  *     g_application_run() if the use-count is non-zero. Since 2.32,
  *     GApplication is iterating the main context directly and is not
  *     using @run_mainloop anymore
+ * @dbus_register: invoked locally during registration, if the application is
+ *     using its D-Bus backend. You can use this to export extra objects on the
+ *     bus, that need to exist before the application tries to own the bus name.
+ *     The function is passed the #GDBusConnection to to session bus, and the
+ *     object path that #GApplication will use to export is D-Bus API.
+ *     If this function returns %TRUE, registration will proceed; otherwise
+ *     registration will abort. Since: 2.34
+ * @dbus_unregister: invoked locally during unregistration, if the application
+ *     is using its D-Bus backend. Use this to undo anything done by the
+ *     @dbus_register vfunc. Since: 2.34
  *
  * Virtual function table for #GApplication.
  *
@@ -208,6 +242,7 @@ struct _GApplicationPrivate
   guint              inactivity_timeout_id;
   guint              inactivity_timeout;
   guint              use_count;
+  guint              busy_count;
 
   guint              is_registered : 1;
   guint              is_remote : 1;
@@ -217,6 +252,8 @@ struct _GApplicationPrivate
 
   GRemoteActionGroup *remote_actions;
   GApplicationImpl   *impl;
+
+  GNotificationBackend *notifications;
 };
 
 enum
@@ -246,6 +283,7 @@ static guint g_application_signals[NR_SIGNALS];
 static void g_application_action_group_iface_init (GActionGroupInterface *);
 static void g_application_action_map_iface_init (GActionMapInterface *);
 G_DEFINE_TYPE_WITH_CODE (GApplication, g_application, G_TYPE_OBJECT,
+ G_ADD_PRIVATE (GApplication)
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_application_action_group_iface_init)
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_application_action_map_iface_init))
 
@@ -442,7 +480,7 @@ g_application_real_local_command_line (GApplication   *application,
 
       if (!g_application_register (application, NULL, &error))
         {
-          g_printerr ("%s", error->message);
+          g_critical ("%s", error->message);
           g_error_free (error);
           *exit_status = 1;
           return TRUE;
@@ -506,6 +544,22 @@ g_application_real_add_platform_data (GApplication    *application,
 {
 }
 
+static gboolean
+g_application_real_dbus_register (GApplication    *application,
+                                  GDBusConnection *connection,
+                                  const gchar     *object_path,
+                                  GError         **error)
+{
+  return TRUE;
+}
+
+static void
+g_application_real_dbus_unregister (GApplication    *application,
+                                    GDBusConnection *connection,
+                                    const gchar     *object_path)
+{
+}
+
 /* GObject implementation stuff {{{1 */
 static void
 g_application_set_property (GObject      *object,
@@ -637,6 +691,9 @@ g_application_finalize (GObject *object)
   if (application->priv->actions)
     g_object_unref (application->priv->actions);
 
+  if (application->priv->notifications)
+    g_object_unref (application->priv->notifications);
+
   G_OBJECT_CLASS (g_application_parent_class)
     ->finalize (object);
 }
@@ -644,9 +701,7 @@ g_application_finalize (GObject *object)
 static void
 g_application_init (GApplication *application)
 {
-  application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
-                                                   G_TYPE_APPLICATION,
-                                                   GApplicationPrivate);
+  application->priv = g_application_get_instance_private (application);
 
   application->priv->actions = g_application_exported_actions_new (application);
 
@@ -682,6 +737,8 @@ 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->dbus_register = g_application_real_dbus_register;
+  class->dbus_unregister = g_application_real_dbus_unregister;
 
   g_object_class_install_property (object_class, PROP_APPLICATION_ID,
     g_param_spec_string ("application-id",
@@ -795,8 +852,6 @@ g_application_class_init (GApplicationClass *class)
                   g_signal_accumulator_first_wins, NULL,
                   NULL,
                   G_TYPE_INT, 1, G_TYPE_APPLICATION_COMMAND_LINE);
-
-  g_type_class_add_private (class, sizeof (GApplicationPrivate));
 }
 
 static GVariant *
@@ -910,8 +965,6 @@ g_application_id_is_valid (const gchar *application_id)
  *
  * Creates a new #GApplication instance.
  *
- * This function calls g_type_init() for you.
- *
  * If non-%NULL, the application id must be valid.  See
  * g_application_id_is_valid().
  *
@@ -926,8 +979,6 @@ g_application_new (const gchar       *application_id,
 {
   g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
 
-  g_type_init ();
-
   return g_object_new (G_TYPE_APPLICATION,
                        "application-id", application_id,
                        "flags", flags,
@@ -1298,6 +1349,8 @@ g_application_register (GApplication  *application,
 void
 g_application_hold (GApplication *application)
 {
+  g_return_if_fail (G_IS_APPLICATION (application));
+
   if (application->priv->inactivity_timeout_id)
     {
       g_source_remove (application->priv->inactivity_timeout_id);
@@ -1332,6 +1385,8 @@ inactivity_timeout_expired (gpointer data)
 void
 g_application_release (GApplication *application)
 {
+  g_return_if_fail (G_IS_APPLICATION (application));
+
   application->priv->use_count--;
 
   if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
@@ -1482,9 +1537,19 @@ g_application_open (GApplication  *application,
  * 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).
+ * If the %G_APPLICATION_IS_SERVICE flag is set, then the service will
+ * run for as much as 10 seconds with a use count of zero while waiting
+ * for the message that caused the activation to arrive.  After that,
+ * if the use count falls to zero the application will exit immediately,
+ * except in the case that g_application_set_inactivity_timeout() is in
+ * use.
+ *
+ * This function sets the prgname (g_set_prgname()), if not already set,
+ * to the basename of argv[0].  Since 2.38, if %G_APPLICATION_IS_SERVICE
+ * is specified, the prgname is set to the application ID.  The main
+ * impact of this is is that the wmclass of windows created by Gtk+ will
+ * be set accordingly, which helps the window manager determine which
+ * application is showing the window.
  *
  * Returns: the exit status
  *
@@ -1508,13 +1573,20 @@ g_application_run (GApplication  *application,
     arguments[i] = g_strdup (argv[i]);
   arguments[i] = NULL;
 
-  if (g_get_prgname () == NULL && argc > 0)
+  if (g_get_prgname () == NULL)
     {
-      gchar *prgname;
+      if (application->priv->flags & G_APPLICATION_IS_SERVICE)
+        {
+          g_set_prgname (application->priv->id);
+        }
+      else if (argc > 0)
+        {
+          gchar *prgname;
 
-      prgname = g_path_get_basename (argv[0]);
-      g_set_prgname (prgname);
-      g_free (prgname);
+          prgname = g_path_get_basename (argv[0]);
+          g_set_prgname (prgname);
+          g_free (prgname);
+        }
     }
 
   if (!G_APPLICATION_GET_CLASS (application)
@@ -1572,7 +1644,7 @@ g_application_run (GApplication  *application,
       status = 0;
     }
 
-  if (!application->priv->is_remote)
+  if (application->priv->is_registered && !application->priv->is_remote)
     {
       g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
 
@@ -1686,9 +1758,9 @@ g_application_lookup_action (GActionMap  *action_map,
 {
   GApplication *application = G_APPLICATION (action_map);
 
-  g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions), NULL);
+  g_return_val_if_fail (G_IS_ACTION_MAP (application->priv->actions), NULL);
 
-  return g_simple_action_group_lookup (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
+  return g_action_map_lookup_action (G_ACTION_MAP (application->priv->actions), action_name);
 }
 
 static void
@@ -1697,9 +1769,9 @@ g_application_add_action (GActionMap *action_map,
 {
   GApplication *application = G_APPLICATION (action_map);
 
-  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
+  g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
 
-  g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (application->priv->actions), action);
+  g_action_map_add_action (G_ACTION_MAP (application->priv->actions), action);
 }
 
 static void
@@ -1708,9 +1780,9 @@ g_application_remove_action (GActionMap  *action_map,
 {
   GApplication *application = G_APPLICATION (action_map);
 
-  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (application->priv->actions));
+  g_return_if_fail (G_IS_ACTION_MAP (application->priv->actions));
 
-  g_simple_action_group_remove (G_SIMPLE_ACTION_GROUP (application->priv->actions), action_name);
+  g_action_map_remove_action (G_ACTION_MAP (application->priv->actions), action_name);
 }
 
 static void
@@ -1798,5 +1870,153 @@ g_application_quit (GApplication *application)
   application->priv->must_quit_now = TRUE;
 }
 
+/**
+ * g_application_mark_busy:
+ * @application: a #GApplication
+ *
+ * Increases the busy count of @application.
+ *
+ * Use this function to indicate that the application is busy, for instance
+ * while a long running operation is pending.
+ *
+ * The busy state will be exposed to other processes, so a session shell will
+ * use that information to indicate the state to the user (e.g. with a
+ * spinner).
+ *
+ * To cancel the busy indication, use g_application_unmark_busy().
+ *
+ * Since: 2.38
+ **/
+void
+g_application_mark_busy (GApplication *application)
+{
+  gboolean was_busy;
+
+  g_return_if_fail (G_IS_APPLICATION (application));
+
+  was_busy = (application->priv->busy_count > 0);
+  application->priv->busy_count++;
+
+  if (!was_busy)
+    g_application_impl_set_busy_state (application->priv->impl, TRUE);
+}
+
+/**
+ * g_application_unmark_busy:
+ * @application: a #GApplication
+ *
+ * Decreases the busy count of @application.
+ *
+ * When the busy count reaches zero, the new state will be propagated
+ * to other processes.
+ *
+ * This function must only be called to cancel the effect of a previous
+ * call to g_application_mark_busy().
+ *
+ * Since: 2.38
+ **/
+void
+g_application_unmark_busy (GApplication *application)
+{
+  g_return_if_fail (G_IS_APPLICATION (application));
+  g_return_if_fail (application->priv->busy_count > 0);
+
+  application->priv->busy_count--;
+
+  if (application->priv->busy_count == 0)
+    g_application_impl_set_busy_state (application->priv->impl, FALSE);
+}
+
+/* Notifications {{{1 */
+
+/**
+ * g_application_send_notification:
+ * @application: a #GApplication
+ * @id: (allow-none): id of the notification, or %NULL
+ * @notification: the #GNotification to send
+ *
+ * Sends a notification on behalf of @application to the desktop shell.
+ * There is no guarantee that the notification is displayed immediately,
+ * or even at all.
+ *
+ * Notifications may persist after the application exits. It will be
+ * D-Bus-activated when the notification or one of its actions is
+ * activated.
+ *
+ * Modifying @notification after this call has no effect. However, the
+ * object can be reused for a later call to this function.
+ *
+ * @id may be any string that uniquely identifies the event for the
+ * application. It does not need to be in any special format. For
+ * example, "new-message" might be appropriate for a notification about
+ * new messages.
+ *
+ * If a previous notification was sent with the same @id, it will be
+ * replaced with @notification and shown again as if it was a new
+ * notification. This works even for notifications sent from a previous
+ * execution of the application, as long as @id is the same string.
+ *
+ * @id may be %NULL, but it is impossible to replace or withdraw
+ * notifications without an id.
+ *
+ * If @notification is no longer relevant, it can be withdrawn with
+ * g_application_withdraw_notification().
+ *
+ * Since: 2.40
+ */
+void
+g_application_send_notification (GApplication  *application,
+                                 const gchar   *id,
+                                 GNotification *notification)
+{
+  gchar *generated_id = NULL;
+
+  g_return_if_fail (G_IS_APPLICATION (application));
+  g_return_if_fail (G_IS_NOTIFICATION (notification));
+  g_return_if_fail (g_application_get_is_registered (application));
+  g_return_if_fail (!g_application_get_is_remote (application));
+
+  if (application->priv->notifications == NULL)
+    application->priv->notifications = g_notification_backend_new_default (application);
+
+  if (id == NULL)
+    {
+      generated_id = g_dbus_generate_guid ();
+      id = generated_id;
+    }
+
+  g_notification_backend_send_notification (application->priv->notifications, id, notification);
+
+  g_free (generated_id);
+}
+
+/**
+ * g_application_withdraw_notification:
+ * @application: a #GApplication
+ * @id: id of a previously sent notification
+ *
+ * Withdraws a notification that was sent with
+ * g_application_send_notification().
+ *
+ * This call does nothing if a notification with @id doesn't exist or
+ * the notification was never sent.
+ *
+ * This function works even for notifications sent in previous
+ * executions of this application, as long @id is the same as it was for
+ * the sent notification.
+ *
+ * Since: 2.40
+ */
+void
+g_application_withdraw_notification (GApplication *application,
+                                     const gchar  *id)
+{
+  g_return_if_fail (G_IS_APPLICATION (application));
+  g_return_if_fail (id != NULL);
+
+  if (application->priv->notifications)
+    g_notification_backend_withdraw_notification (application->priv->notifications, id);
+}
+
 /* Epilogue {{{1 */
 /* vim:set foldmethod=marker: */