2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gapplicationimpl.h"
26 #include "gactiongroup.h"
27 #include "gactiongroupexporter.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusactiongroup-private.h"
30 #include "gapplication.h"
32 #include "gdbusconnection.h"
33 #include "gdbusintrospection.h"
34 #include "gdbuserror.h"
35 #include "glib/gstdio.h"
40 #include "gapplicationcommandline.h"
41 #include "gdbusmethodinvocation.h"
44 #include "gunixinputstream.h"
45 #include "gunixfdlist.h"
48 /* DBus Interface definition {{{1 */
50 /* For documentation of these interfaces, see
51 * http://live.gnome.org/GTK+/GApplication-dbus-apis
53 static const gchar org_gtk_Application_xml[] =
55 "<interface name='org.gtk.Application'>"
56 "<method name='Activate'>"
57 "<arg type='a{sv}' name='platform-data' direction='in'/>"
59 "<method name='Open'>"
60 "<arg type='as' name='uris' direction='in'/>"
61 "<arg type='s' name='hint' direction='in'/>"
62 "<arg type='a{sv}' name='platform-data' direction='in'/>"
64 "<method name='CommandLine'>"
65 "<arg type='o' name='path' direction='in'/>"
66 "<arg type='aay' name='arguments' direction='in'/>"
67 "<arg type='a{sv}' name='platform-data' direction='in'/>"
68 "<arg type='i' name='exit-status' direction='out'/>"
73 static GDBusInterfaceInfo *org_gtk_Application;
75 static const gchar org_gtk_private_CommandLine_xml[] =
77 "<interface name='org.gtk.private.CommandLine'>"
78 "<method name='Print'>"
79 "<arg type='s' name='message' direction='in'/>"
81 "<method name='PrintError'>"
82 "<arg type='s' name='message' direction='in'/>"
87 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
89 /* GApplication implementation {{{1 */
90 struct _GApplicationImpl
92 GDBusConnection *session_bus;
93 GActionGroup *exported_actions;
94 const gchar *bus_name;
100 gboolean properties_live;
106 static GApplicationCommandLine *
107 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
111 g_application_impl_method_call (GDBusConnection *connection,
113 const gchar *object_path,
114 const gchar *interface_name,
115 const gchar *method_name,
116 GVariant *parameters,
117 GDBusMethodInvocation *invocation,
120 GApplicationImpl *impl = user_data;
121 GApplicationClass *class;
123 class = G_APPLICATION_GET_CLASS (impl->app);
125 if (strcmp (method_name, "Activate") == 0)
127 GVariant *platform_data;
129 g_variant_get (parameters, "(@a{sv})", &platform_data);
130 class->before_emit (impl->app, platform_data);
131 g_signal_emit_by_name (impl->app, "activate");
132 class->after_emit (impl->app, platform_data);
133 g_variant_unref (platform_data);
135 g_dbus_method_invocation_return_value (invocation, NULL);
138 else if (strcmp (method_name, "Open") == 0)
140 GVariant *platform_data;
146 g_variant_get (parameters, "(@ass@a{sv})",
147 &array, &hint, &platform_data);
149 n = g_variant_n_children (array);
150 files = g_new (GFile *, n + 1);
152 for (i = 0; i < n; i++)
156 g_variant_get_child (array, i, "&s", &uri);
157 files[i] = g_file_new_for_uri (uri);
159 g_variant_unref (array);
162 class->before_emit (impl->app, platform_data);
163 g_signal_emit_by_name (impl->app, "open", files, n, hint);
164 class->after_emit (impl->app, platform_data);
166 g_variant_unref (platform_data);
168 for (i = 0; i < n; i++)
169 g_object_unref (files[i]);
172 g_dbus_method_invocation_return_value (invocation, NULL);
175 else if (strcmp (method_name, "CommandLine") == 0)
177 GApplicationCommandLine *cmdline;
178 GVariant *platform_data;
181 cmdline = g_dbus_command_line_new (invocation);
182 platform_data = g_variant_get_child_value (parameters, 2);
183 class->before_emit (impl->app, platform_data);
184 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
185 g_application_command_line_set_exit_status (cmdline, status);
186 class->after_emit (impl->app, platform_data);
187 g_variant_unref (platform_data);
188 g_object_unref (cmdline);
191 g_assert_not_reached ();
195 application_path_from_appid (const gchar *appid)
197 gchar *appid_path, *iter;
200 /* this is a private implementation detail */
201 return g_strdup ("/org/gtk/Application/anonymous");
203 appid_path = g_strconcat ("/", appid, NULL);
204 for (iter = appid_path; *iter; iter++)
216 /* Attempt to become the primary instance.
218 * Returns %TRUE if everything went OK, regardless of if we became the
219 * primary instance or not. %FALSE is reserved for when something went
220 * seriously wrong (and @error will be set too, in that case).
222 * After a %TRUE return, impl->primary will be TRUE if we were
226 g_application_impl_attempt_primary (GApplicationImpl *impl,
227 GCancellable *cancellable,
230 const static GDBusInterfaceVTable vtable = {
231 g_application_impl_method_call,
233 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
237 if (org_gtk_Application == NULL)
239 GError *error = NULL;
242 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
243 if G_UNLIKELY (info == NULL)
244 g_error ("%s", error->message);
245 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
246 g_assert (org_gtk_Application != NULL);
247 g_dbus_interface_info_ref (org_gtk_Application);
248 g_dbus_node_info_unref (info);
251 /* We could possibly have been D-Bus activated as a result of incoming
252 * requests on either the application or actiongroup interfaces.
253 * Because of how GDBus dispatches messages, we need to ensure that
254 * both of those things are registered before we attempt to request
257 * The action group need not be populated yet, as long as it happens
258 * before we return to the mainloop. The reason for that is because
259 * GDBus does the check to make sure the object exists from the worker
260 * thread but doesn't actually dispatch the action invocation until we
261 * hit the mainloop in this thread. There is also no danger of
262 * receiving 'activate' or 'open' signals until after 'startup' runs,
263 * for the same reason.
265 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
266 org_gtk_Application, &vtable, impl, NULL, error);
268 if (impl->object_id == 0)
271 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
272 impl->exported_actions, error);
274 if (impl->actions_id == 0)
277 if (!app_class->dbus_register (impl->app,
283 if (impl->bus_name == NULL)
285 /* If this is a non-unique application then it is sufficient to
286 * have our object paths registered. We can return now.
288 * Note: non-unique applications always act as primary-instance.
290 impl->primary = TRUE;
294 /* If this is a unique application then we need to attempt to own
295 * the well-known name and fall back to remote mode (!is_primary)
296 * in the case that we can't do that.
298 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
299 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
300 "org.freedesktop.DBus", "RequestName",
301 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
302 0, -1, cancellable, error);
307 g_variant_get (reply, "(u)", &rval);
308 g_variant_unref (reply);
310 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
311 impl->primary = (rval != 3);
316 /* Stop doing the things that the primary instance does.
318 * This should be called if attempting to become the primary instance
319 * failed (in order to clean up any partial success) and should also
320 * be called when freeing the GApplication.
322 * It is safe to call this multiple times.
325 g_application_impl_stop_primary (GApplicationImpl *impl)
327 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
329 app_class->dbus_unregister (impl->app,
335 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
339 if (impl->actions_id)
341 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
342 impl->actions_id = 0;
345 if (impl->primary && impl->bus_name)
347 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
348 "/org/freedesktop/DBus", "org.freedesktop.DBus",
349 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
350 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
351 impl->primary = FALSE;
356 g_application_impl_destroy (GApplicationImpl *impl)
358 g_application_impl_stop_primary (impl);
360 if (impl->session_bus)
361 g_object_unref (impl->session_bus);
363 g_free (impl->object_path);
365 g_slice_free (GApplicationImpl, impl);
369 g_application_impl_register (GApplication *application,
371 GApplicationFlags flags,
372 GActionGroup *exported_actions,
373 GRemoteActionGroup **remote_actions,
374 GCancellable *cancellable,
377 GDBusActionGroup *actions;
378 GApplicationImpl *impl;
380 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
382 impl = g_slice_new0 (GApplicationImpl);
384 impl->app = application;
385 impl->exported_actions = exported_actions;
387 /* non-unique applications do not attempt to acquire a bus name */
388 if (~flags & G_APPLICATION_NON_UNIQUE)
389 impl->bus_name = appid;
391 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
393 if (impl->session_bus == NULL)
395 /* If we can't connect to the session bus, proceed as a normal
396 * non-unique application.
398 *remote_actions = NULL;
402 impl->object_path = application_path_from_appid (appid);
404 /* Only try to be the primary instance if
405 * G_APPLICATION_IS_LAUNCHER was not specified.
407 if (~flags & G_APPLICATION_IS_LAUNCHER)
409 if (!g_application_impl_attempt_primary (impl, cancellable, error))
411 g_application_impl_destroy (impl);
418 /* We didn't make it. Drop our service-side stuff. */
419 g_application_impl_stop_primary (impl);
421 if (flags & G_APPLICATION_IS_SERVICE)
423 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
424 "Unable to acquire bus name `%s'", appid);
425 g_application_impl_destroy (impl);
431 /* We are non-primary. Try to get the primary's list of actions.
432 * This also serves as a mechanism to ensure that the primary exists
433 * (ie: DBus service files installed correctly, etc).
435 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
436 if (!g_dbus_action_group_sync (actions, cancellable, error))
438 /* The primary appears not to exist. Fail the registration. */
439 g_application_impl_destroy (impl);
440 g_object_unref (actions);
445 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
451 g_application_impl_activate (GApplicationImpl *impl,
452 GVariant *platform_data)
454 g_dbus_connection_call (impl->session_bus,
457 "org.gtk.Application",
459 g_variant_new ("(@a{sv})", platform_data),
460 NULL, 0, -1, NULL, NULL, NULL);
464 g_application_impl_open (GApplicationImpl *impl,
468 GVariant *platform_data)
470 GVariantBuilder builder;
473 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
474 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
475 for (i = 0; i < n_files; i++)
477 gchar *uri = g_file_get_uri (files[i]);
478 g_variant_builder_add (&builder, "s", uri);
481 g_variant_builder_close (&builder);
482 g_variant_builder_add (&builder, "s", hint);
483 g_variant_builder_add_value (&builder, platform_data);
485 g_dbus_connection_call (impl->session_bus,
488 "org.gtk.Application",
490 g_variant_builder_end (&builder),
491 NULL, 0, -1, NULL, NULL, NULL);
495 g_application_impl_cmdline_method_call (GDBusConnection *connection,
497 const gchar *object_path,
498 const gchar *interface_name,
499 const gchar *method_name,
500 GVariant *parameters,
501 GDBusMethodInvocation *invocation,
504 const gchar *message;
506 g_variant_get_child (parameters, 0, "&s", &message);
508 if (strcmp (method_name, "Print") == 0)
509 g_print ("%s", message);
510 else if (strcmp (method_name, "PrintError") == 0)
511 g_printerr ("%s", message);
513 g_assert_not_reached ();
515 g_dbus_method_invocation_return_value (invocation, NULL);
525 g_application_impl_cmdline_done (GObject *source,
526 GAsyncResult *result,
529 CommandLineData *data = user_data;
530 GError *error = NULL;
534 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
536 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
542 g_variant_get (reply, "(i)", &data->status);
543 g_variant_unref (reply);
548 g_printerr ("%s\n", error->message);
549 g_error_free (error);
553 g_main_loop_quit (data->loop);
557 g_application_impl_command_line (GApplicationImpl *impl,
559 GVariant *platform_data)
561 const static GDBusInterfaceVTable vtable = {
562 g_application_impl_cmdline_method_call
564 const gchar *object_path = "/org/gtk/Application/CommandLine";
565 GMainContext *context;
566 CommandLineData data;
569 context = g_main_context_new ();
570 data.loop = g_main_loop_new (context, FALSE);
571 g_main_context_push_thread_default (context);
573 if (org_gtk_private_CommandLine == NULL)
575 GError *error = NULL;
578 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
579 if G_UNLIKELY (info == NULL)
580 g_error ("%s", error->message);
581 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
582 g_assert (org_gtk_private_CommandLine != NULL);
583 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
584 g_dbus_node_info_unref (info);
587 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
588 org_gtk_private_CommandLine,
589 &vtable, &data, NULL, NULL);
590 /* In theory we should try other paths... */
591 g_assert (object_id != 0);
595 GError *error = NULL;
596 GUnixFDList *fd_list;
598 /* send along the stdin in case
599 * g_application_command_line_get_stdin_data() is called
601 fd_list = g_unix_fd_list_new ();
602 g_unix_fd_list_append (fd_list, 0, &error);
603 g_assert_no_error (error);
605 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
606 "org.gtk.Application", "CommandLine",
607 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
608 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
609 g_application_impl_cmdline_done, &data);
612 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
613 "org.gtk.Application", "CommandLine",
614 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
615 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
616 g_application_impl_cmdline_done, &data);
619 g_main_loop_run (data.loop);
621 g_main_context_pop_thread_default (context);
622 g_main_context_unref (context);
623 g_main_loop_unref (data.loop);
629 g_application_impl_flush (GApplicationImpl *impl)
631 if (impl->session_bus)
632 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
636 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
638 return impl->session_bus;
642 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
644 return impl->object_path;
647 /* GDBusCommandLine implementation {{{1 */
649 typedef GApplicationCommandLineClass GDBusCommandLineClass;
650 static GType g_dbus_command_line_get_type (void);
653 GApplicationCommandLine parent_instance;
654 GDBusMethodInvocation *invocation;
656 GDBusConnection *connection;
657 const gchar *bus_name;
658 const gchar *object_path;
662 G_DEFINE_TYPE (GDBusCommandLine,
664 G_TYPE_APPLICATION_COMMAND_LINE)
667 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
668 const gchar *message)
670 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
672 g_dbus_connection_call (gdbcl->connection,
675 "org.gtk.private.CommandLine", "Print",
676 g_variant_new ("(s)", message),
677 NULL, 0, -1, NULL, NULL, NULL);
681 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
682 const gchar *message)
684 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
686 g_dbus_connection_call (gdbcl->connection,
689 "org.gtk.private.CommandLine", "PrintError",
690 g_variant_new ("(s)", message),
691 NULL, 0, -1, NULL, NULL, NULL);
694 static GInputStream *
695 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
698 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
699 GInputStream *result = NULL;
700 GDBusMessage *message;
701 GUnixFDList *fd_list;
703 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
704 fd_list = g_dbus_message_get_unix_fd_list (message);
706 if (fd_list && g_unix_fd_list_get_length (fd_list))
710 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
711 result = g_unix_input_stream_new (fds[0], TRUE);
712 for (i = 1; i < n_fds; i++)
713 (void) g_close (fds[i], NULL);
724 g_dbus_command_line_finalize (GObject *object)
726 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
727 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
730 status = g_application_command_line_get_exit_status (cmdline);
732 g_dbus_method_invocation_return_value (gdbcl->invocation,
733 g_variant_new ("(i)", status));
734 g_object_unref (gdbcl->invocation);
736 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
741 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
746 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
748 GObjectClass *object_class = G_OBJECT_CLASS (class);
750 object_class->finalize = g_dbus_command_line_finalize;
751 class->printerr_literal = g_dbus_command_line_printerr_literal;
752 class->print_literal = g_dbus_command_line_print_literal;
753 class->get_stdin = g_dbus_command_line_get_stdin;
756 static GApplicationCommandLine *
757 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
759 GDBusCommandLine *gdbcl;
762 args = g_dbus_method_invocation_get_parameters (invocation);
764 gdbcl = g_object_new (g_dbus_command_line_get_type (),
765 "arguments", g_variant_get_child_value (args, 1),
766 "platform-data", g_variant_get_child_value (args, 2),
768 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
769 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
770 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
771 gdbcl->invocation = g_object_ref (invocation);
773 return G_APPLICATION_COMMAND_LINE (gdbcl);
778 /* vim:set foldmethod=marker: */