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 "gdbusactiongroup.h"
27 #include "gapplication.h"
29 #include "gdbusconnection.h"
30 #include "gdbusintrospection.h"
31 #include "gdbuserror.h"
32 #include "gmenuexporter.h"
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
40 /* DBus Interface definition {{{1 */
41 static const gchar org_gtk_Application_xml[] =
43 " <interface name='org.gtk.Application'>"
44 " <method name='Activate'>"
45 " <arg type='a{sv}' name='platform-data' direction='in'/>"
47 " <method name='Open'>"
48 " <arg type='as' name='uris' direction='in'/>"
49 " <arg type='s' name='hint' direction='in'/>"
50 " <arg type='a{sv}' name='platform-data' direction='in'/>"
52 " <method name='CommandLine'>"
53 " <arg type='o' name='path' direction='in'/>"
54 " <arg type='aay' name='arguments' direction='in'/>"
55 " <arg type='a{sv}' name='platform-data' direction='in'/>"
56 " <arg type='i' name='exit-status' direction='out'/>"
61 static GDBusInterfaceInfo *org_gtk_Application;
63 static const gchar org_gtk_private_CommandLine_xml[] =
65 " <interface name='org.gtk.private.CommandLine'>"
66 " <method name='Print'>"
67 " <arg type='s' name='message' direction='in'/>"
69 " <method name='PrintError'>"
70 " <arg type='s' name='message' direction='in'/>"
75 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
77 /* GApplication implementation {{{1 */
78 struct _GApplicationImpl
80 GDBusConnection *session_bus;
81 const gchar *bus_name;
84 gboolean actions_exported;
85 gboolean menu_exported;
90 static GApplicationCommandLine *
91 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
95 g_application_impl_method_call (GDBusConnection *connection,
97 const gchar *object_path,
98 const gchar *interface_name,
99 const gchar *method_name,
100 GVariant *parameters,
101 GDBusMethodInvocation *invocation,
104 GApplicationImpl *impl = user_data;
105 GApplicationClass *class;
107 class = G_APPLICATION_GET_CLASS (impl->app);
109 if (strcmp (method_name, "Activate") == 0)
111 GVariant *platform_data;
113 g_variant_get (parameters, "(@a{sv})", &platform_data);
114 class->before_emit (impl->app, platform_data);
115 g_signal_emit_by_name (impl->app, "activate");
116 class->after_emit (impl->app, platform_data);
117 g_variant_unref (platform_data);
119 g_dbus_method_invocation_return_value (invocation, NULL);
122 else if (strcmp (method_name, "Open") == 0)
124 GVariant *platform_data;
130 g_variant_get (parameters, "(@ass@a{sv})",
131 &array, &hint, &platform_data);
133 n = g_variant_n_children (array);
134 files = g_new (GFile *, n + 1);
136 for (i = 0; i < n; i++)
140 g_variant_get_child (array, i, "&s", &uri);
141 files[i] = g_file_new_for_uri (uri);
143 g_variant_unref (array);
146 class->before_emit (impl->app, platform_data);
147 g_signal_emit_by_name (impl->app, "open", files, n, hint);
148 class->after_emit (impl->app, platform_data);
150 g_variant_unref (platform_data);
152 for (i = 0; i < n; i++)
153 g_object_unref (files[i]);
156 g_dbus_method_invocation_return_value (invocation, NULL);
159 else if (strcmp (method_name, "CommandLine") == 0)
161 GApplicationCommandLine *cmdline;
162 GVariant *platform_data;
165 cmdline = g_dbus_command_line_new (invocation);
166 platform_data = g_variant_get_child_value (parameters, 2);
167 class->before_emit (impl->app, platform_data);
168 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
169 g_application_command_line_set_exit_status (cmdline, status);
170 class->after_emit (impl->app, platform_data);
171 g_variant_unref (platform_data);
172 g_object_unref (cmdline);
175 g_assert_not_reached ();
179 application_path_from_appid (const gchar *appid)
181 gchar *appid_path, *iter;
183 appid_path = g_strconcat ("/", appid, NULL);
184 for (iter = appid_path; *iter; iter++)
197 g_application_impl_destroy (GApplicationImpl *impl)
199 if (impl->session_bus)
202 g_dbus_connection_unregister_object (impl->session_bus,
204 if (impl->actions_exported)
205 g_action_group_dbus_export_stop (impl->app);
206 if (impl->menu_exported)
207 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
209 g_dbus_connection_call (impl->session_bus,
210 "org.freedesktop.DBus",
211 "/org/freedesktop/DBus",
212 "org.freedesktop.DBus",
214 g_variant_new ("(s)",
217 G_DBUS_CALL_FLAGS_NONE,
218 -1, NULL, NULL, NULL);
220 g_object_unref (impl->session_bus);
221 g_free (impl->object_path);
225 g_assert (impl->object_path == NULL);
226 g_assert (impl->object_id == 0);
229 g_slice_free (GApplicationImpl, impl);
233 g_application_impl_register (GApplication *application,
235 GApplicationFlags flags,
236 GActionGroup **remote_actions,
237 GCancellable *cancellable,
240 const static GDBusInterfaceVTable vtable = {
241 g_application_impl_method_call
243 GDBusActionGroup *actions;
244 GApplicationImpl *impl;
248 impl = g_slice_new0 (GApplicationImpl);
250 impl->app = application;
251 impl->bus_name = appid;
253 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
255 if (impl->session_bus == NULL)
257 /* If we can't connect to the session bus, proceed as a normal
258 * non-unique application.
260 *remote_actions = NULL;
264 impl->object_path = application_path_from_appid (appid);
266 /* Only try to be the primary instance if
267 * G_APPLICATION_IS_LAUNCHER was not specified.
269 if (~flags & G_APPLICATION_IS_LAUNCHER)
271 if (org_gtk_Application == NULL)
273 GError *error = NULL;
276 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
277 if G_UNLIKELY (info == NULL)
278 g_error ("%s", error->message);
279 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
280 g_assert (org_gtk_Application != NULL);
281 g_dbus_interface_info_ref (org_gtk_Application);
282 g_dbus_node_info_unref (info);
285 /* Attempt to become primary instance. */
287 g_dbus_connection_register_object (impl->session_bus,
290 &vtable, impl, NULL, error);
292 if (impl->object_id == 0)
294 g_object_unref (impl->session_bus);
295 g_free (impl->object_path);
296 impl->session_bus = NULL;
297 impl->object_path = NULL;
299 g_slice_free (GApplicationImpl, impl);
303 if (!g_action_group_dbus_export_start (impl->session_bus,
307 g_dbus_connection_unregister_object (impl->session_bus,
310 g_object_unref (impl->session_bus);
311 g_free (impl->object_path);
312 impl->session_bus = NULL;
313 impl->object_path = NULL;
315 g_slice_free (GApplicationImpl, impl);
318 impl->actions_exported = TRUE;
320 if (g_application_get_app_menu (impl->app))
322 if (!g_menu_model_dbus_export_start (impl->session_bus,
324 g_application_get_app_menu (impl->app),
327 g_action_group_dbus_export_stop (impl->app);
328 impl->actions_exported = FALSE;
330 g_dbus_connection_unregister_object (impl->session_bus,
333 g_object_unref (impl->session_bus);
334 g_free (impl->object_path);
335 impl->session_bus = NULL;
336 impl->object_path = NULL;
338 g_slice_free (GApplicationImpl, impl);
341 impl->menu_exported = TRUE;
344 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
345 reply = g_dbus_connection_call_sync (impl->session_bus,
346 "org.freedesktop.DBus",
347 "/org/freedesktop/DBus",
348 "org.freedesktop.DBus",
350 g_variant_new ("(su)",
353 G_VARIANT_TYPE ("(u)"),
354 0, -1, cancellable, error);
358 g_dbus_connection_unregister_object (impl->session_bus,
362 g_action_group_dbus_export_stop (impl->app);
363 impl->actions_exported = FALSE;
365 if (impl->menu_exported)
367 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
368 impl->menu_exported = FALSE;
371 g_object_unref (impl->session_bus);
372 g_free (impl->object_path);
373 impl->session_bus = NULL;
374 impl->object_path = NULL;
376 g_slice_free (GApplicationImpl, impl);
380 g_variant_get (reply, "(u)", &rval);
381 g_variant_unref (reply);
383 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
386 /* We are the primary instance. */
387 g_dbus_connection_emit_signal (impl->session_bus,
390 "org.gtk.Application",
392 g_variant_new ("(s)",
395 *remote_actions = NULL;
399 /* We didn't make it. Drop our service-side stuff. */
400 g_dbus_connection_unregister_object (impl->session_bus,
403 g_action_group_dbus_export_stop (impl->app);
404 impl->actions_exported = FALSE;
405 if (impl->menu_exported)
407 g_menu_model_dbus_export_stop (g_application_get_app_menu (impl->app));
408 impl->menu_exported = FALSE;
411 if (flags & G_APPLICATION_IS_SERVICE)
413 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
414 "Unable to acquire bus name `%s'", appid);
415 g_object_unref (impl->session_bus);
416 g_free (impl->object_path);
418 g_slice_free (GApplicationImpl, impl);
423 /* We are non-primary. Try to get the primary's list of actions.
424 * This also serves as a mechanism to ensure that the primary exists
425 * (ie: DBus service files installed correctly, etc).
427 actions = g_dbus_action_group_new_sync (impl->session_bus, impl->bus_name, impl->object_path,
428 G_DBUS_ACTION_GROUP_FLAGS_NONE, NULL, error);
432 /* The primary appears not to exist. Fail the registration. */
433 g_object_unref (impl->session_bus);
434 g_free (impl->object_path);
435 impl->session_bus = NULL;
436 impl->object_path = NULL;
438 g_slice_free (GApplicationImpl, impl);
442 *remote_actions = G_ACTION_GROUP (actions);
448 g_application_impl_activate (GApplicationImpl *impl,
449 GVariant *platform_data)
451 g_dbus_connection_call (impl->session_bus,
454 "org.gtk.Application",
456 g_variant_new ("(@a{sv})", platform_data),
457 NULL, 0, -1, NULL, NULL, NULL);
461 g_application_impl_open (GApplicationImpl *impl,
465 GVariant *platform_data)
467 GVariantBuilder builder;
470 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
471 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
472 for (i = 0; i < n_files; i++)
474 gchar *uri = g_file_get_uri (files[i]);
475 g_variant_builder_add (&builder, "s", uri);
478 g_variant_builder_close (&builder);
479 g_variant_builder_add (&builder, "s", hint);
480 g_variant_builder_add_value (&builder, platform_data);
482 g_dbus_connection_call (impl->session_bus,
485 "org.gtk.Application",
487 g_variant_builder_end (&builder),
488 NULL, 0, -1, NULL, NULL, NULL);
492 g_application_impl_cmdline_method_call (GDBusConnection *connection,
494 const gchar *object_path,
495 const gchar *interface_name,
496 const gchar *method_name,
497 GVariant *parameters,
498 GDBusMethodInvocation *invocation,
501 const gchar *message;
503 g_variant_get_child (parameters, 0, "&s", &message);
505 if (strcmp (method_name, "Print") == 0)
506 g_print ("%s", message);
507 else if (strcmp (method_name, "PrintError") == 0)
508 g_printerr ("%s", message);
510 g_assert_not_reached ();
512 g_dbus_method_invocation_return_value (invocation, NULL);
522 g_application_impl_cmdline_done (GObject *source,
523 GAsyncResult *result,
526 CommandLineData *data = user_data;
527 GError *error = NULL;
530 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
535 g_variant_get (reply, "(i)", &data->status);
536 g_variant_unref (reply);
541 g_printerr ("%s\n", error->message);
542 g_error_free (error);
546 g_main_loop_quit (data->loop);
550 g_application_impl_command_line (GApplicationImpl *impl,
552 GVariant *platform_data)
554 const static GDBusInterfaceVTable vtable = {
555 g_application_impl_cmdline_method_call
557 const gchar *object_path = "/org/gtk/Application/CommandLine";
558 GMainContext *context;
559 CommandLineData data;
562 context = g_main_context_new ();
563 data.loop = g_main_loop_new (context, FALSE);
564 g_main_context_push_thread_default (context);
566 if (org_gtk_private_CommandLine == NULL)
568 GError *error = NULL;
571 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
572 if G_UNLIKELY (info == NULL)
573 g_error ("%s", error->message);
574 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
575 g_assert (org_gtk_private_CommandLine != NULL);
576 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
577 g_dbus_node_info_unref (info);
580 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
581 org_gtk_private_CommandLine,
582 &vtable, &data, NULL, NULL);
583 /* In theory we should try other paths... */
584 g_assert (object_id != 0);
586 g_dbus_connection_call (impl->session_bus,
589 "org.gtk.Application",
591 g_variant_new ("(o^aay@a{sv})", object_path,
592 arguments, platform_data),
593 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
594 g_application_impl_cmdline_done, &data);
596 g_main_loop_run (data.loop);
598 g_main_context_pop_thread_default (context);
599 g_main_context_unref (context);
600 g_main_loop_unref (data.loop);
606 g_application_impl_flush (GApplicationImpl *impl)
608 if (impl->session_bus)
609 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
613 /* GDBusCommandLine implementation {{{1 */
615 typedef GApplicationCommandLineClass GDBusCommandLineClass;
616 static GType g_dbus_command_line_get_type (void);
619 GApplicationCommandLine parent_instance;
620 GDBusMethodInvocation *invocation;
622 GDBusConnection *connection;
623 const gchar *bus_name;
624 const gchar *object_path;
628 G_DEFINE_TYPE (GDBusCommandLine,
630 G_TYPE_APPLICATION_COMMAND_LINE)
633 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
634 const gchar *message)
636 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
638 g_dbus_connection_call (gdbcl->connection,
641 "org.gtk.private.CommandLine", "Print",
642 g_variant_new ("(s)", message),
643 NULL, 0, -1, NULL, NULL, NULL);
647 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
648 const gchar *message)
650 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
652 g_dbus_connection_call (gdbcl->connection,
655 "org.gtk.private.CommandLine", "PrintError",
656 g_variant_new ("(s)", message),
657 NULL, 0, -1, NULL, NULL, NULL);
661 g_dbus_command_line_finalize (GObject *object)
663 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
664 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
667 status = g_application_command_line_get_exit_status (cmdline);
669 g_dbus_method_invocation_return_value (gdbcl->invocation,
670 g_variant_new ("(i)", status));
671 g_object_unref (gdbcl->invocation);
673 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
678 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
683 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
685 GObjectClass *object_class = G_OBJECT_CLASS (class);
687 object_class->finalize = g_dbus_command_line_finalize;
688 class->printerr_literal = g_dbus_command_line_printerr_literal;
689 class->print_literal = g_dbus_command_line_print_literal;
692 static GApplicationCommandLine *
693 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
695 GDBusCommandLine *gdbcl;
698 args = g_dbus_method_invocation_get_parameters (invocation);
700 gdbcl = g_object_new (g_dbus_command_line_get_type (),
701 "arguments", g_variant_get_child_value (args, 1),
702 "platform-data", g_variant_get_child_value (args, 2),
704 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
705 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
706 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
707 gdbcl->invocation = g_object_ref (invocation);
709 return G_APPLICATION_COMMAND_LINE (gdbcl);
714 /* vim:set foldmethod=marker: */