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'/>"
70 "<property name='Busy' type='b' access='read'/>"
74 static GDBusInterfaceInfo *org_gtk_Application;
76 static const gchar org_gtk_private_CommandLine_xml[] =
78 "<interface name='org.gtk.private.CommandLine'>"
79 "<method name='Print'>"
80 "<arg type='s' name='message' direction='in'/>"
82 "<method name='PrintError'>"
83 "<arg type='s' name='message' direction='in'/>"
88 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
90 /* GApplication implementation {{{1 */
91 struct _GApplicationImpl
93 GDBusConnection *session_bus;
94 GActionGroup *exported_actions;
95 const gchar *bus_name;
101 gboolean properties_live;
108 static GApplicationCommandLine *
109 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
112 g_application_impl_get_property (GDBusConnection *connection,
114 const gchar *object_path,
115 const gchar *interface_name,
116 const gchar *property_name,
120 GApplicationImpl *impl = user_data;
122 if (strcmp (property_name, "Busy") == 0)
123 return g_variant_new_boolean (impl->busy);
125 g_assert_not_reached ();
131 send_property_change (GApplicationImpl *impl)
133 GVariantBuilder builder;
135 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
136 g_variant_builder_add (&builder,
138 "Busy", g_variant_new_boolean (impl->busy));
140 g_dbus_connection_emit_signal (impl->session_bus,
143 "org.freedesktop.DBus.Properties",
145 g_variant_new ("(sa{sv}as)",
146 "org.gtk.Application",
153 g_application_impl_method_call (GDBusConnection *connection,
155 const gchar *object_path,
156 const gchar *interface_name,
157 const gchar *method_name,
158 GVariant *parameters,
159 GDBusMethodInvocation *invocation,
162 GApplicationImpl *impl = user_data;
163 GApplicationClass *class;
165 class = G_APPLICATION_GET_CLASS (impl->app);
167 if (strcmp (method_name, "Activate") == 0)
169 GVariant *platform_data;
171 g_variant_get (parameters, "(@a{sv})", &platform_data);
172 class->before_emit (impl->app, platform_data);
173 g_signal_emit_by_name (impl->app, "activate");
174 class->after_emit (impl->app, platform_data);
175 g_variant_unref (platform_data);
177 g_dbus_method_invocation_return_value (invocation, NULL);
180 else if (strcmp (method_name, "Open") == 0)
182 GVariant *platform_data;
188 g_variant_get (parameters, "(@as&s@a{sv})",
189 &array, &hint, &platform_data);
191 n = g_variant_n_children (array);
192 files = g_new (GFile *, n + 1);
194 for (i = 0; i < n; i++)
198 g_variant_get_child (array, i, "&s", &uri);
199 files[i] = g_file_new_for_uri (uri);
201 g_variant_unref (array);
204 class->before_emit (impl->app, platform_data);
205 g_signal_emit_by_name (impl->app, "open", files, n, hint);
206 class->after_emit (impl->app, platform_data);
208 g_variant_unref (platform_data);
210 for (i = 0; i < n; i++)
211 g_object_unref (files[i]);
214 g_dbus_method_invocation_return_value (invocation, NULL);
217 else if (strcmp (method_name, "CommandLine") == 0)
219 GApplicationCommandLine *cmdline;
220 GVariant *platform_data;
223 cmdline = g_dbus_command_line_new (invocation);
224 platform_data = g_variant_get_child_value (parameters, 2);
225 class->before_emit (impl->app, platform_data);
226 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
227 g_application_command_line_set_exit_status (cmdline, status);
228 class->after_emit (impl->app, platform_data);
229 g_variant_unref (platform_data);
230 g_object_unref (cmdline);
233 g_assert_not_reached ();
237 application_path_from_appid (const gchar *appid)
239 gchar *appid_path, *iter;
242 /* this is a private implementation detail */
243 return g_strdup ("/org/gtk/Application/anonymous");
245 appid_path = g_strconcat ("/", appid, NULL);
246 for (iter = appid_path; *iter; iter++)
258 /* Attempt to become the primary instance.
260 * Returns %TRUE if everything went OK, regardless of if we became the
261 * primary instance or not. %FALSE is reserved for when something went
262 * seriously wrong (and @error will be set too, in that case).
264 * After a %TRUE return, impl->primary will be TRUE if we were
268 g_application_impl_attempt_primary (GApplicationImpl *impl,
269 GCancellable *cancellable,
272 const static GDBusInterfaceVTable vtable = {
273 g_application_impl_method_call,
274 g_application_impl_get_property,
275 NULL /* set_property */
277 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
281 if (org_gtk_Application == NULL)
283 GError *error = NULL;
286 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
287 if G_UNLIKELY (info == NULL)
288 g_error ("%s", error->message);
289 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
290 g_assert (org_gtk_Application != NULL);
291 g_dbus_interface_info_ref (org_gtk_Application);
292 g_dbus_node_info_unref (info);
295 /* We could possibly have been D-Bus activated as a result of incoming
296 * requests on either the application or actiongroup interfaces.
297 * Because of how GDBus dispatches messages, we need to ensure that
298 * both of those things are registered before we attempt to request
301 * The action group need not be populated yet, as long as it happens
302 * before we return to the mainloop. The reason for that is because
303 * GDBus does the check to make sure the object exists from the worker
304 * thread but doesn't actually dispatch the action invocation until we
305 * hit the mainloop in this thread. There is also no danger of
306 * receiving 'activate' or 'open' signals until after 'startup' runs,
307 * for the same reason.
309 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
310 org_gtk_Application, &vtable, impl, NULL, error);
312 if (impl->object_id == 0)
315 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
316 impl->exported_actions, error);
318 if (impl->actions_id == 0)
321 if (!app_class->dbus_register (impl->app,
327 if (impl->bus_name == NULL)
329 /* If this is a non-unique application then it is sufficient to
330 * have our object paths registered. We can return now.
332 * Note: non-unique applications always act as primary-instance.
334 impl->primary = TRUE;
338 /* If this is a unique application then we need to attempt to own
339 * the well-known name and fall back to remote mode (!is_primary)
340 * in the case that we can't do that.
342 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
343 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
344 "org.freedesktop.DBus", "RequestName",
345 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
346 0, -1, cancellable, error);
351 g_variant_get (reply, "(u)", &rval);
352 g_variant_unref (reply);
354 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
355 impl->primary = (rval != 3);
360 /* Stop doing the things that the primary instance does.
362 * This should be called if attempting to become the primary instance
363 * failed (in order to clean up any partial success) and should also
364 * be called when freeing the GApplication.
366 * It is safe to call this multiple times.
369 g_application_impl_stop_primary (GApplicationImpl *impl)
371 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
373 app_class->dbus_unregister (impl->app,
379 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
383 if (impl->actions_id)
385 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
386 impl->actions_id = 0;
389 if (impl->primary && impl->bus_name)
391 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
392 "/org/freedesktop/DBus", "org.freedesktop.DBus",
393 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
394 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
395 impl->primary = FALSE;
400 g_application_impl_set_busy_state (GApplicationImpl *impl,
403 if (impl->busy != busy)
406 send_property_change (impl);
411 g_application_impl_destroy (GApplicationImpl *impl)
413 g_application_impl_stop_primary (impl);
415 if (impl->session_bus)
416 g_object_unref (impl->session_bus);
418 g_free (impl->object_path);
420 g_slice_free (GApplicationImpl, impl);
424 g_application_impl_register (GApplication *application,
426 GApplicationFlags flags,
427 GActionGroup *exported_actions,
428 GRemoteActionGroup **remote_actions,
429 GCancellable *cancellable,
432 GDBusActionGroup *actions;
433 GApplicationImpl *impl;
435 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
437 impl = g_slice_new0 (GApplicationImpl);
439 impl->app = application;
440 impl->exported_actions = exported_actions;
442 /* non-unique applications do not attempt to acquire a bus name */
443 if (~flags & G_APPLICATION_NON_UNIQUE)
444 impl->bus_name = appid;
446 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
448 if (impl->session_bus == NULL)
450 /* If we can't connect to the session bus, proceed as a normal
451 * non-unique application.
453 *remote_actions = NULL;
457 impl->object_path = application_path_from_appid (appid);
459 /* Only try to be the primary instance if
460 * G_APPLICATION_IS_LAUNCHER was not specified.
462 if (~flags & G_APPLICATION_IS_LAUNCHER)
464 if (!g_application_impl_attempt_primary (impl, cancellable, error))
466 g_application_impl_destroy (impl);
473 /* We didn't make it. Drop our service-side stuff. */
474 g_application_impl_stop_primary (impl);
476 if (flags & G_APPLICATION_IS_SERVICE)
478 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
479 "Unable to acquire bus name '%s'", appid);
480 g_application_impl_destroy (impl);
486 /* We are non-primary. Try to get the primary's list of actions.
487 * This also serves as a mechanism to ensure that the primary exists
488 * (ie: DBus service files installed correctly, etc).
490 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
491 if (!g_dbus_action_group_sync (actions, cancellable, error))
493 /* The primary appears not to exist. Fail the registration. */
494 g_application_impl_destroy (impl);
495 g_object_unref (actions);
500 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
506 g_application_impl_activate (GApplicationImpl *impl,
507 GVariant *platform_data)
509 g_dbus_connection_call (impl->session_bus,
512 "org.gtk.Application",
514 g_variant_new ("(@a{sv})", platform_data),
515 NULL, 0, -1, NULL, NULL, NULL);
519 g_application_impl_open (GApplicationImpl *impl,
523 GVariant *platform_data)
525 GVariantBuilder builder;
528 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
529 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
530 for (i = 0; i < n_files; i++)
532 gchar *uri = g_file_get_uri (files[i]);
533 g_variant_builder_add (&builder, "s", uri);
536 g_variant_builder_close (&builder);
537 g_variant_builder_add (&builder, "s", hint);
538 g_variant_builder_add_value (&builder, platform_data);
540 g_dbus_connection_call (impl->session_bus,
543 "org.gtk.Application",
545 g_variant_builder_end (&builder),
546 NULL, 0, -1, NULL, NULL, NULL);
550 g_application_impl_cmdline_method_call (GDBusConnection *connection,
552 const gchar *object_path,
553 const gchar *interface_name,
554 const gchar *method_name,
555 GVariant *parameters,
556 GDBusMethodInvocation *invocation,
559 const gchar *message;
561 g_variant_get_child (parameters, 0, "&s", &message);
563 if (strcmp (method_name, "Print") == 0)
564 g_print ("%s", message);
565 else if (strcmp (method_name, "PrintError") == 0)
566 g_printerr ("%s", message);
568 g_assert_not_reached ();
570 g_dbus_method_invocation_return_value (invocation, NULL);
580 g_application_impl_cmdline_done (GObject *source,
581 GAsyncResult *result,
584 CommandLineData *data = user_data;
585 GError *error = NULL;
589 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
591 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
597 g_variant_get (reply, "(i)", &data->status);
598 g_variant_unref (reply);
603 g_printerr ("%s\n", error->message);
604 g_error_free (error);
608 g_main_loop_quit (data->loop);
612 g_application_impl_command_line (GApplicationImpl *impl,
614 GVariant *platform_data)
616 const static GDBusInterfaceVTable vtable = {
617 g_application_impl_cmdline_method_call
619 const gchar *object_path = "/org/gtk/Application/CommandLine";
620 GMainContext *context;
621 CommandLineData data;
624 context = g_main_context_new ();
625 data.loop = g_main_loop_new (context, FALSE);
626 g_main_context_push_thread_default (context);
628 if (org_gtk_private_CommandLine == NULL)
630 GError *error = NULL;
633 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
634 if G_UNLIKELY (info == NULL)
635 g_error ("%s", error->message);
636 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
637 g_assert (org_gtk_private_CommandLine != NULL);
638 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
639 g_dbus_node_info_unref (info);
642 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
643 org_gtk_private_CommandLine,
644 &vtable, &data, NULL, NULL);
645 /* In theory we should try other paths... */
646 g_assert (object_id != 0);
650 GError *error = NULL;
651 GUnixFDList *fd_list;
653 /* send along the stdin in case
654 * g_application_command_line_get_stdin_data() is called
656 fd_list = g_unix_fd_list_new ();
657 g_unix_fd_list_append (fd_list, 0, &error);
658 g_assert_no_error (error);
660 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
661 "org.gtk.Application", "CommandLine",
662 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
663 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
664 g_application_impl_cmdline_done, &data);
667 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
668 "org.gtk.Application", "CommandLine",
669 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
670 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
671 g_application_impl_cmdline_done, &data);
674 g_main_loop_run (data.loop);
676 g_main_context_pop_thread_default (context);
677 g_main_context_unref (context);
678 g_main_loop_unref (data.loop);
684 g_application_impl_flush (GApplicationImpl *impl)
686 if (impl->session_bus)
687 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
691 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
693 return impl->session_bus;
697 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
699 return impl->object_path;
702 /* GDBusCommandLine implementation {{{1 */
704 typedef GApplicationCommandLineClass GDBusCommandLineClass;
705 static GType g_dbus_command_line_get_type (void);
708 GApplicationCommandLine parent_instance;
709 GDBusMethodInvocation *invocation;
711 GDBusConnection *connection;
712 const gchar *bus_name;
713 const gchar *object_path;
717 G_DEFINE_TYPE (GDBusCommandLine,
719 G_TYPE_APPLICATION_COMMAND_LINE)
722 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
723 const gchar *message)
725 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
727 g_dbus_connection_call (gdbcl->connection,
730 "org.gtk.private.CommandLine", "Print",
731 g_variant_new ("(s)", message),
732 NULL, 0, -1, NULL, NULL, NULL);
736 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
737 const gchar *message)
739 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
741 g_dbus_connection_call (gdbcl->connection,
744 "org.gtk.private.CommandLine", "PrintError",
745 g_variant_new ("(s)", message),
746 NULL, 0, -1, NULL, NULL, NULL);
749 static GInputStream *
750 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
753 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
754 GInputStream *result = NULL;
755 GDBusMessage *message;
756 GUnixFDList *fd_list;
758 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
759 fd_list = g_dbus_message_get_unix_fd_list (message);
761 if (fd_list && g_unix_fd_list_get_length (fd_list))
765 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
766 result = g_unix_input_stream_new (fds[0], TRUE);
767 for (i = 1; i < n_fds; i++)
768 (void) g_close (fds[i], NULL);
779 g_dbus_command_line_finalize (GObject *object)
781 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
782 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
785 status = g_application_command_line_get_exit_status (cmdline);
787 g_dbus_method_invocation_return_value (gdbcl->invocation,
788 g_variant_new ("(i)", status));
789 g_object_unref (gdbcl->invocation);
791 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
796 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
801 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
803 GObjectClass *object_class = G_OBJECT_CLASS (class);
805 object_class->finalize = g_dbus_command_line_finalize;
806 class->printerr_literal = g_dbus_command_line_printerr_literal;
807 class->print_literal = g_dbus_command_line_print_literal;
808 class->get_stdin = g_dbus_command_line_get_stdin;
811 static GApplicationCommandLine *
812 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
814 GDBusCommandLine *gdbcl;
817 args = g_dbus_method_invocation_get_parameters (invocation);
819 gdbcl = g_object_new (g_dbus_command_line_get_type (),
820 "arguments", g_variant_get_child_value (args, 1),
821 "platform-data", g_variant_get_child_value (args, 2),
823 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
824 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
825 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
826 gdbcl->invocation = g_object_ref (invocation);
828 return G_APPLICATION_COMMAND_LINE (gdbcl);
833 /* vim:set foldmethod=marker: */