wip junk
[platform/upstream/glib.git] / gio / gapplicationimpl-dbus.c
index 68b0d28..4b1b5c9 100644 (file)
@@ -12,9 +12,7 @@
  * 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.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Ryan Lortie <desrt@desrt.ca>
  */
@@ -48,7 +46,7 @@
 /* DBus Interface definition {{{1 */
 
 /* For documentation of these interfaces, see
- * http://live.gnome.org/GTK+/GApplication-dbus-apis
+ * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
  */
 static const gchar org_gtk_Application_xml[] =
   "<node>"
@@ -73,6 +71,26 @@ static const gchar org_gtk_Application_xml[] =
 
 static GDBusInterfaceInfo *org_gtk_Application;
 
+static const gchar org_freedesktop_Application_xml[] =
+  "<node>"
+    "<interface name='org.freedesktop.Application'>"
+      "<method name='Activate'>"
+        "<arg type='a{sv}' name='platform-data' direction='in'/>"
+      "</method>"
+      "<method name='Open'>"
+        "<arg type='as' name='uris' direction='in'/>"
+        "<arg type='a{sv}' name='platform-data' direction='in'/>"
+      "</method>"
+      "<method name='ActivateAction'>"
+        "<arg type='s' name='action-name' direction='in'/>"
+        "<arg type='av' name='parameter' direction='in'/>"
+        "<arg type='a{sv}' name='platform-data' direction='in'/>"
+      "</method>"
+    "</interface>"
+  "</node>";
+
+static GDBusInterfaceInfo *org_freedesktop_Application;
+
 static const gchar org_gtk_private_CommandLine_xml[] =
   "<node>"
     "<interface name='org.gtk.private.CommandLine'>"
@@ -96,6 +114,7 @@ struct _GApplicationImpl
 
   gchar           *object_path;
   guint            object_id;
+  guint            fdo_object_id;
   guint            actions_id;
 
   gboolean         properties_live;
@@ -168,7 +187,10 @@ g_application_impl_method_call (GDBusConnection       *connection,
     {
       GVariant *platform_data;
 
+      /* Completely the same for both freedesktop and gtk interfaces */
+
       g_variant_get (parameters, "(@a{sv})", &platform_data);
+
       class->before_emit (impl->app, platform_data);
       g_signal_emit_by_name (impl->app, "activate");
       class->after_emit (impl->app, platform_data);
@@ -179,14 +201,28 @@ g_application_impl_method_call (GDBusConnection       *connection,
 
   else if (strcmp (method_name, "Open") == 0)
     {
+      GApplicationFlags flags;
       GVariant *platform_data;
       const gchar *hint;
       GVariant *array;
       GFile **files;
       gint n, i;
 
-      g_variant_get (parameters, "(@ass@a{sv})",
-                     &array, &hint, &platform_data);
+      flags = g_application_get_flags (impl->app);
+      if ((flags & G_APPLICATION_HANDLES_OPEN) == 0)
+        {
+          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files");
+          return;
+        }
+
+      /* freedesktop interface has no hint parameter */
+      if (g_str_equal (interface_name, "org.freedesktop.Application"))
+        {
+          g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
+          hint = "";
+        }
+      else
+        g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
 
       n = g_variant_n_children (array);
       files = g_new (GFile *, n + 1);
@@ -216,10 +252,21 @@ g_application_impl_method_call (GDBusConnection       *connection,
 
   else if (strcmp (method_name, "CommandLine") == 0)
     {
+      GApplicationFlags flags;
       GApplicationCommandLine *cmdline;
       GVariant *platform_data;
       int status;
 
+      flags = g_application_get_flags (impl->app);
+      if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0)
+        {
+          g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED,
+                                                 "Application does not handle command line arguments");
+          return;
+        }
+
+      /* Only on the GtkApplication interface */
+
       cmdline = g_dbus_command_line_new (invocation);
       platform_data = g_variant_get_child_value (parameters, 2);
       class->before_emit (impl->app, platform_data);
@@ -229,6 +276,30 @@ g_application_impl_method_call (GDBusConnection       *connection,
       g_variant_unref (platform_data);
       g_object_unref (cmdline);
     }
+  else if (g_str_equal (method_name, "ActivateAction"))
+    {
+      GVariant *parameter = NULL;
+      GVariant *platform_data;
+      GVariantIter *iter;
+      const gchar *name;
+
+      /* Only on the freedesktop interface */
+
+      g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
+      g_variant_iter_next (iter, "v", &parameter);
+      g_variant_iter_free (iter);
+
+      class->before_emit (impl->app, platform_data);
+      g_action_group_activate_action (impl->exported_actions, name, parameter);
+      class->after_emit (impl->app, platform_data);
+
+      if (parameter)
+        g_variant_unref (parameter);
+
+      g_variant_unref (platform_data);
+
+      g_dbus_method_invocation_return_value (invocation, NULL);
+    }
   else
     g_assert_not_reached ();
 }
@@ -290,6 +361,14 @@ g_application_impl_attempt_primary (GApplicationImpl  *impl,
       g_assert (org_gtk_Application != NULL);
       g_dbus_interface_info_ref (org_gtk_Application);
       g_dbus_node_info_unref (info);
+
+      info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error);
+      if G_UNLIKELY (info == NULL)
+        g_error ("%s", error->message);
+      org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
+      g_assert (org_freedesktop_Application != NULL);
+      g_dbus_interface_info_ref (org_freedesktop_Application);
+      g_dbus_node_info_unref (info);
     }
 
   /* We could possibly have been D-Bus activated as a result of incoming
@@ -312,6 +391,12 @@ g_application_impl_attempt_primary (GApplicationImpl  *impl,
   if (impl->object_id == 0)
     return FALSE;
 
+  impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
+                                                           org_freedesktop_Application, &vtable, impl, NULL, error);
+
+  if (impl->fdo_object_id == 0)
+    return FALSE;
+
   impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
                                                             impl->exported_actions, error);
 
@@ -380,6 +465,12 @@ g_application_impl_stop_primary (GApplicationImpl *impl)
       impl->object_id = 0;
     }
 
+  if (impl->fdo_object_id)
+    {
+      g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
+      impl->fdo_object_id = 0;
+    }
+
   if (impl->actions_id)
     {
       g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
@@ -476,7 +567,7 @@ g_application_impl_register (GApplication        *application,
       if (flags & G_APPLICATION_IS_SERVICE)
         {
           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
-                       "Unable to acquire bus name `%s'", appid);
+                       "Unable to acquire bus name '%s'", appid);
           g_application_impl_destroy (impl);
 
           return NULL;
@@ -609,9 +700,9 @@ g_application_impl_cmdline_done (GObject      *source,
 }
 
 int
-g_application_impl_command_line (GApplicationImpl  *impl,
-                                 gchar            **arguments,
-                                 GVariant          *platform_data)
+g_application_impl_command_line (GApplicationImpl    *impl,
+                                 const gchar * const *arguments,
+                                 GVariant            *platform_data)
 {
   const static GDBusInterfaceVTable vtable = {
     g_application_impl_cmdline_method_call
@@ -662,6 +753,7 @@ g_application_impl_command_line (GApplicationImpl  *impl,
                                               g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
                                               G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
                                               g_application_impl_cmdline_done, &data);
+    g_object_unref (fd_list);
   }
 #else
   g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
@@ -760,13 +852,13 @@ g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
 
   if (fd_list && g_unix_fd_list_get_length (fd_list))
     {
-      gint *fds, n_fds, i;
+      gint *fds, i;
 
-      fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
-      result = g_unix_input_stream_new (fds[0], TRUE);
-      for (i = 1; i < n_fds; i++)
-        (void) g_close (fds[i], NULL);
-      g_free (fds);
+      fds = g_unix_fd_list_peek_fds (fd_list, NULL);
+      result = g_unix_input_stream_new (fds[0], FALSE);
+      g_object_weak_ref (G_OBJECT (result),
+                         (GWeakNotify) g_object_unref,
+                         g_object_ref (fd_list));
     }
 
   return result;
@@ -813,13 +905,19 @@ g_dbus_command_line_new (GDBusMethodInvocation *invocation)
 {
   GDBusCommandLine *gdbcl;
   GVariant *args;
+  GVariant *arguments, *platform_data;
 
   args = g_dbus_method_invocation_get_parameters (invocation);
 
+  arguments = g_variant_get_child_value (args, 1);
+  platform_data = g_variant_get_child_value (args, 2);
   gdbcl = g_object_new (g_dbus_command_line_get_type (),
-                        "arguments", g_variant_get_child_value (args, 1),
-                        "platform-data", g_variant_get_child_value (args, 2),
+                        "arguments", arguments,
+                        "platform-data", platform_data,
                         NULL);
+  g_variant_unref (arguments);
+  g_variant_unref (platform_data);
+
   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);