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>
22 #include "gapplicationimpl.h"
24 #include "gactiongroup.h"
25 #include "gactiongroupexporter.h"
26 #include "gremoteactiongroup.h"
27 #include "gdbusactiongroup.h"
28 #include "gapplication.h"
30 #include "gdbusconnection.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
33 #include "gmenuexporter.h"
38 #include "gapplicationcommandline.h"
39 #include "gdbusmethodinvocation.h"
41 G_GNUC_INTERNAL gboolean
42 g_dbus_action_group_sync (GDBusActionGroup *group,
43 GCancellable *cancellable,
47 /* DBus Interface definition {{{1 */
48 static const gchar org_gtk_Application_xml[] =
50 " <interface name='org.gtk.Application'>"
51 " <method name='Activate'>"
52 " <arg type='a{sv}' name='platform-data' direction='in'/>"
54 " <method name='Open'>"
55 " <arg type='as' name='uris' direction='in'/>"
56 " <arg type='s' name='hint' direction='in'/>"
57 " <arg type='a{sv}' name='platform-data' direction='in'/>"
59 " <method name='CommandLine'>"
60 " <arg type='o' name='path' direction='in'/>"
61 " <arg type='aay' name='arguments' direction='in'/>"
62 " <arg type='a{sv}' name='platform-data' direction='in'/>"
63 " <arg type='i' name='exit-status' direction='out'/>"
65 " <property name='AppMenu' type='ao' access='read'/>"
66 " <property name='MenuBar' type='ao' access='read'/>"
70 static GDBusInterfaceInfo *org_gtk_Application;
72 static const gchar org_gtk_private_CommandLine_xml[] =
74 " <interface name='org.gtk.private.CommandLine'>"
75 " <method name='Print'>"
76 " <arg type='s' name='message' direction='in'/>"
78 " <method name='PrintError'>"
79 " <arg type='s' name='message' direction='in'/>"
84 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
86 /* GApplication implementation {{{1 */
87 struct _GApplicationImpl
89 GDBusConnection *session_bus;
90 GActionGroup *exported_actions;
91 const gchar *bus_name;
103 gboolean properties_live;
109 static GApplicationCommandLine *
110 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
114 g_application_impl_method_call (GDBusConnection *connection,
116 const gchar *object_path,
117 const gchar *interface_name,
118 const gchar *method_name,
119 GVariant *parameters,
120 GDBusMethodInvocation *invocation,
123 GApplicationImpl *impl = user_data;
124 GApplicationClass *class;
126 class = G_APPLICATION_GET_CLASS (impl->app);
128 if (strcmp (method_name, "Activate") == 0)
130 GVariant *platform_data;
132 g_variant_get (parameters, "(@a{sv})", &platform_data);
133 class->before_emit (impl->app, platform_data);
134 g_signal_emit_by_name (impl->app, "activate");
135 class->after_emit (impl->app, platform_data);
136 g_variant_unref (platform_data);
138 g_dbus_method_invocation_return_value (invocation, NULL);
141 else if (strcmp (method_name, "Open") == 0)
143 GVariant *platform_data;
149 g_variant_get (parameters, "(@ass@a{sv})",
150 &array, &hint, &platform_data);
152 n = g_variant_n_children (array);
153 files = g_new (GFile *, n + 1);
155 for (i = 0; i < n; i++)
159 g_variant_get_child (array, i, "&s", &uri);
160 files[i] = g_file_new_for_uri (uri);
162 g_variant_unref (array);
165 class->before_emit (impl->app, platform_data);
166 g_signal_emit_by_name (impl->app, "open", files, n, hint);
167 class->after_emit (impl->app, platform_data);
169 g_variant_unref (platform_data);
171 for (i = 0; i < n; i++)
172 g_object_unref (files[i]);
175 g_dbus_method_invocation_return_value (invocation, NULL);
178 else if (strcmp (method_name, "CommandLine") == 0)
180 GApplicationCommandLine *cmdline;
181 GVariant *platform_data;
184 cmdline = g_dbus_command_line_new (invocation);
185 platform_data = g_variant_get_child_value (parameters, 2);
186 class->before_emit (impl->app, platform_data);
187 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
188 g_application_command_line_set_exit_status (cmdline, status);
189 class->after_emit (impl->app, platform_data);
190 g_variant_unref (platform_data);
191 g_object_unref (cmdline);
194 g_assert_not_reached ();
198 g_application_impl_get_property (GDBusConnection *connection,
200 const gchar *object_path,
201 const gchar *interface_name,
202 const gchar *property_name,
206 GApplicationImpl *impl = user_data;
207 GVariantBuilder builder;
209 /* We use this boolean to detect if the properties have ever been
210 * queried before. If they have not been queried, then there is no
211 * point emitting change signals since nobody is watching anyway.
213 impl->properties_live = TRUE;
215 g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
217 if (g_str_equal (property_name, "AppMenu"))
219 if (impl->app_menu_path != NULL)
220 g_variant_builder_add (&builder, "o", impl->app_menu_path);
223 else if (g_str_equal (property_name, "MenuBar"))
225 if (impl->menubar_path != NULL)
226 g_variant_builder_add (&builder, "o", impl->menubar_path);
230 g_assert_not_reached ();
232 return g_variant_builder_end (&builder);
236 application_path_from_appid (const gchar *appid)
238 gchar *appid_path, *iter;
240 appid_path = g_strconcat ("/", appid, NULL);
241 for (iter = appid_path; *iter; iter++)
254 g_application_impl_publish_menu (GApplicationImpl *impl,
262 /* unexport any existing menu */
265 g_dbus_connection_unexport_menu_model (impl->session_bus, *id);
271 /* export the new menu, if there is one */
274 /* try getting the preferred name */
275 *path = g_strconcat (impl->object_path, "/menus/", type, NULL);
276 *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL);
278 /* keep trying until we get a working name... */
279 for (i = 0; *id == 0; i++)
282 *path = g_strdup_printf ("%s/menus/%s%d", impl->object_path, type, i);
283 *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL);
287 /* notify for changes, if needed */
288 if (impl->properties_live)
290 GVariantBuilder builder;
292 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
293 g_variant_builder_add (&builder, "{sv}", type, g_variant_new_objv ((const gchar **) path, *path != NULL));
294 g_dbus_connection_emit_signal (impl->session_bus, NULL, impl->object_path,
295 "org.freedesktop.DBus.Properties", "PropertiesChanged",
296 g_variant_new ("(sa{sv}as)", "org.gtk.Actions", &builder, NULL), NULL);
301 g_application_impl_app_menu_changed (GObject *source,
305 GApplicationImpl *impl = user_data;
307 g_assert (source == impl->app);
309 g_application_impl_publish_menu (impl, "AppMenu", g_application_get_app_menu (impl->app),
310 &impl->app_menu_id, &impl->app_menu_path);
314 g_application_impl_menubar_changed (GObject *source,
318 GApplicationImpl *impl = user_data;
320 g_assert (source == impl->app);
322 g_application_impl_publish_menu (impl, "MenuBar", g_application_get_menubar (impl->app),
323 &impl->menubar_id, &impl->menubar_path);
326 /* Attempt to become the primary instance.
328 * Returns %TRUE if everything went OK, regardless of if we became the
329 * primary instance or not. %FALSE is reserved for when something went
330 * seriously wrong (and @error will be set too, in that case).
332 * After a %TRUE return, impl->primary will be TRUE if we were
336 g_application_impl_attempt_primary (GApplicationImpl *impl,
337 GCancellable *cancellable,
340 const static GDBusInterfaceVTable vtable = {
341 g_application_impl_method_call,
342 g_application_impl_get_property
347 if (org_gtk_Application == NULL)
349 GError *error = NULL;
352 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
353 if G_UNLIKELY (info == NULL)
354 g_error ("%s", error->message);
355 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
356 g_assert (org_gtk_Application != NULL);
357 g_dbus_interface_info_ref (org_gtk_Application);
358 g_dbus_node_info_unref (info);
361 /* We could possibly have been D-Bus activated as a result of incoming
362 * requests on either the application or actiongroup interfaces.
363 * Because of how GDBus dispatches messages, we need to ensure that
364 * both of those things are registered before we attempt to request
367 * The action group need not be populated yet, as long as it happens
368 * before we return to the mainloop. The reason for that is because
369 * GDBus does the check to make sure the object exists from the worker
370 * thread but doesn't actually dispatch the action invocation until we
371 * hit the mainloop in this thread. There is also no danger of
372 * receiving 'activate' or 'open' signals until after 'startup' runs,
373 * for the same reason.
375 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
376 org_gtk_Application, &vtable, impl, NULL, error);
378 if (impl->object_id == 0)
381 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
382 impl->exported_actions, error);
384 if (impl->actions_id == 0)
387 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
388 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
389 "org.freedesktop.DBus", "RequestName",
390 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
391 0, -1, cancellable, error);
396 g_variant_get (reply, "(u)", &rval);
397 g_variant_unref (reply);
399 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
400 impl->primary = (rval != 3);
404 g_signal_connect (impl->app, "notify::app-menu", G_CALLBACK (g_application_impl_app_menu_changed), impl);
405 g_signal_connect (impl->app, "notify::menubar", G_CALLBACK (g_application_impl_menubar_changed), impl);
406 g_application_impl_app_menu_changed (impl->app, NULL, impl);
407 g_application_impl_menubar_changed (impl->app, NULL, impl);
413 /* Stop doing the things that the primary instance does.
415 * This should be called if attempting to become the primary instance
416 * failed (in order to clean up any partial success) and should also
417 * be called when freeing the GApplication.
419 * It is safe to call this multiple times.
422 g_application_impl_stop_primary (GApplicationImpl *impl)
426 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
430 if (impl->actions_id)
432 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
433 impl->actions_id = 0;
438 g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_app_menu_changed, impl);
439 g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_menubar_changed, impl);
441 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
442 "/org/freedesktop/DBus", "org.freedesktop.DBus",
443 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
444 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
445 impl->primary = FALSE;
448 if (impl->app_menu_id)
450 g_dbus_connection_unexport_menu_model (impl->session_bus, impl->app_menu_id);
451 g_free (impl->app_menu_path);
452 impl->app_menu_path = NULL;
453 impl->app_menu_id = 0;
456 if (impl->menubar_id)
458 g_dbus_connection_unexport_menu_model (impl->session_bus, impl->menubar_id);
459 g_free (impl->menubar_path);
460 impl->menubar_path = NULL;
461 impl->menubar_id = 0;
466 g_application_impl_destroy (GApplicationImpl *impl)
468 g_application_impl_stop_primary (impl);
470 if (impl->session_bus)
471 g_object_unref (impl->session_bus);
473 g_free (impl->object_path);
475 g_slice_free (GApplicationImpl, impl);
479 g_application_impl_register (GApplication *application,
481 GApplicationFlags flags,
482 GActionGroup *exported_actions,
483 GRemoteActionGroup **remote_actions,
484 GCancellable *cancellable,
487 GDBusActionGroup *actions;
488 GApplicationImpl *impl;
490 impl = g_slice_new0 (GApplicationImpl);
492 impl->app = application;
493 impl->exported_actions = exported_actions;
494 impl->bus_name = appid;
496 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
498 if (impl->session_bus == NULL)
500 /* If we can't connect to the session bus, proceed as a normal
501 * non-unique application.
503 *remote_actions = NULL;
507 impl->object_path = application_path_from_appid (appid);
509 /* Only try to be the primary instance if
510 * G_APPLICATION_IS_LAUNCHER was not specified.
512 if (~flags & G_APPLICATION_IS_LAUNCHER)
514 if (!g_application_impl_attempt_primary (impl, cancellable, error))
516 g_application_impl_destroy (impl);
523 /* We didn't make it. Drop our service-side stuff. */
524 g_application_impl_stop_primary (impl);
526 if (flags & G_APPLICATION_IS_SERVICE)
528 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
529 "Unable to acquire bus name `%s'", appid);
530 g_application_impl_destroy (impl);
536 /* We are non-primary. Try to get the primary's list of actions.
537 * This also serves as a mechanism to ensure that the primary exists
538 * (ie: DBus service files installed correctly, etc).
540 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
541 if (!g_dbus_action_group_sync (actions, cancellable, error))
543 /* The primary appears not to exist. Fail the registration. */
544 g_application_impl_destroy (impl);
545 g_object_unref (actions);
550 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
556 g_application_impl_activate (GApplicationImpl *impl,
557 GVariant *platform_data)
559 g_dbus_connection_call (impl->session_bus,
562 "org.gtk.Application",
564 g_variant_new ("(@a{sv})", platform_data),
565 NULL, 0, -1, NULL, NULL, NULL);
569 g_application_impl_open (GApplicationImpl *impl,
573 GVariant *platform_data)
575 GVariantBuilder builder;
578 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
579 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
580 for (i = 0; i < n_files; i++)
582 gchar *uri = g_file_get_uri (files[i]);
583 g_variant_builder_add (&builder, "s", uri);
586 g_variant_builder_close (&builder);
587 g_variant_builder_add (&builder, "s", hint);
588 g_variant_builder_add_value (&builder, platform_data);
590 g_dbus_connection_call (impl->session_bus,
593 "org.gtk.Application",
595 g_variant_builder_end (&builder),
596 NULL, 0, -1, NULL, NULL, NULL);
600 g_application_impl_cmdline_method_call (GDBusConnection *connection,
602 const gchar *object_path,
603 const gchar *interface_name,
604 const gchar *method_name,
605 GVariant *parameters,
606 GDBusMethodInvocation *invocation,
609 const gchar *message;
611 g_variant_get_child (parameters, 0, "&s", &message);
613 if (strcmp (method_name, "Print") == 0)
614 g_print ("%s", message);
615 else if (strcmp (method_name, "PrintError") == 0)
616 g_printerr ("%s", message);
618 g_assert_not_reached ();
620 g_dbus_method_invocation_return_value (invocation, NULL);
630 g_application_impl_cmdline_done (GObject *source,
631 GAsyncResult *result,
634 CommandLineData *data = user_data;
635 GError *error = NULL;
638 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
643 g_variant_get (reply, "(i)", &data->status);
644 g_variant_unref (reply);
649 g_printerr ("%s\n", error->message);
650 g_error_free (error);
654 g_main_loop_quit (data->loop);
658 g_application_impl_command_line (GApplicationImpl *impl,
660 GVariant *platform_data)
662 const static GDBusInterfaceVTable vtable = {
663 g_application_impl_cmdline_method_call
665 const gchar *object_path = "/org/gtk/Application/CommandLine";
666 GMainContext *context;
667 CommandLineData data;
670 context = g_main_context_new ();
671 data.loop = g_main_loop_new (context, FALSE);
672 g_main_context_push_thread_default (context);
674 if (org_gtk_private_CommandLine == NULL)
676 GError *error = NULL;
679 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
680 if G_UNLIKELY (info == NULL)
681 g_error ("%s", error->message);
682 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
683 g_assert (org_gtk_private_CommandLine != NULL);
684 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
685 g_dbus_node_info_unref (info);
688 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
689 org_gtk_private_CommandLine,
690 &vtable, &data, NULL, NULL);
691 /* In theory we should try other paths... */
692 g_assert (object_id != 0);
694 g_dbus_connection_call (impl->session_bus,
697 "org.gtk.Application",
699 g_variant_new ("(o^aay@a{sv})", object_path,
700 arguments, platform_data),
701 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
702 g_application_impl_cmdline_done, &data);
704 g_main_loop_run (data.loop);
706 g_main_context_pop_thread_default (context);
707 g_main_context_unref (context);
708 g_main_loop_unref (data.loop);
714 g_application_impl_flush (GApplicationImpl *impl)
716 if (impl->session_bus)
717 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
721 /* GDBusCommandLine implementation {{{1 */
723 typedef GApplicationCommandLineClass GDBusCommandLineClass;
724 static GType g_dbus_command_line_get_type (void);
727 GApplicationCommandLine parent_instance;
728 GDBusMethodInvocation *invocation;
730 GDBusConnection *connection;
731 const gchar *bus_name;
732 const gchar *object_path;
736 G_DEFINE_TYPE (GDBusCommandLine,
738 G_TYPE_APPLICATION_COMMAND_LINE)
741 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
742 const gchar *message)
744 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
746 g_dbus_connection_call (gdbcl->connection,
749 "org.gtk.private.CommandLine", "Print",
750 g_variant_new ("(s)", message),
751 NULL, 0, -1, NULL, NULL, NULL);
755 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
756 const gchar *message)
758 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
760 g_dbus_connection_call (gdbcl->connection,
763 "org.gtk.private.CommandLine", "PrintError",
764 g_variant_new ("(s)", message),
765 NULL, 0, -1, NULL, NULL, NULL);
769 g_dbus_command_line_finalize (GObject *object)
771 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
772 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
775 status = g_application_command_line_get_exit_status (cmdline);
777 g_dbus_method_invocation_return_value (gdbcl->invocation,
778 g_variant_new ("(i)", status));
779 g_object_unref (gdbcl->invocation);
781 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
786 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
791 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
793 GObjectClass *object_class = G_OBJECT_CLASS (class);
795 object_class->finalize = g_dbus_command_line_finalize;
796 class->printerr_literal = g_dbus_command_line_printerr_literal;
797 class->print_literal = g_dbus_command_line_print_literal;
800 static GApplicationCommandLine *
801 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
803 GDBusCommandLine *gdbcl;
806 args = g_dbus_method_invocation_get_parameters (invocation);
808 gdbcl = g_object_new (g_dbus_command_line_get_type (),
809 "arguments", g_variant_get_child_value (args, 1),
810 "platform-data", g_variant_get_child_value (args, 2),
812 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
813 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
814 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
815 gdbcl->invocation = g_object_ref (invocation);
817 return G_APPLICATION_COMMAND_LINE (gdbcl);
822 /* vim:set foldmethod=marker: */