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 "gapplication.h"
26 #include "gdbusconnection.h"
27 #include "gdbusintrospection.h"
28 #include "gdbuserror.h"
33 #include "gapplicationimpl-dbus-interface.c"
34 #include "gapplicationcommandline.h"
35 #include "gdbusmethodinvocation.h"
37 struct _GApplicationImpl
39 GDBusConnection *session_bus;
40 const gchar *bus_name;
47 static GApplicationCommandLine *
48 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
52 g_application_impl_method_call (GDBusConnection *connection,
54 const gchar *object_path,
55 const gchar *interface_name,
56 const gchar *method_name,
58 GDBusMethodInvocation *invocation,
61 GApplicationImpl *impl = user_data;
62 GApplicationClass *class;
64 class = G_APPLICATION_GET_CLASS (impl->app);
66 if (strcmp (method_name, "Activate") == 0)
68 GVariant *platform_data;
70 g_variant_get (parameters, "(@a{sv})", &platform_data);
71 class->before_emit (impl->app, platform_data);
72 g_signal_emit_by_name (impl->app, "activate");
73 class->after_emit (impl->app, platform_data);
74 g_variant_unref (platform_data);
77 else if (strcmp (method_name, "Open") == 0)
79 GVariant *platform_data;
85 g_variant_get (parameters, "(@ass@a{sv})",
86 &array, &hint, &platform_data);
88 n = g_variant_n_children (array);
89 files = g_new (GFile *, n + 1);
91 for (i = 0; i < n; i++)
95 g_variant_get_child (array, i, "&s", &uri);
96 files[i] = g_file_new_for_uri (uri);
98 g_variant_unref (array);
101 class->before_emit (impl->app, platform_data);
102 g_signal_emit_by_name (impl->app, "open", files, n, hint);
103 class->after_emit (impl->app, platform_data);
105 g_variant_unref (platform_data);
107 for (i = 0; i < n; i++)
108 g_object_unref (files[i]);
112 else if (strcmp (method_name, "CommandLine") == 0)
114 GApplicationCommandLine *cmdline;
115 GVariant *platform_data;
118 cmdline = g_dbus_command_line_new (invocation);
119 platform_data = g_variant_get_child_value (parameters, 2);
120 class->before_emit (impl->app, platform_data);
121 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
122 g_application_command_line_set_exit_status (cmdline, status);
123 class->after_emit (impl->app, platform_data);
124 g_variant_unref (platform_data);
125 g_object_unref (cmdline);
129 g_assert_not_reached ();
133 application_path_from_appid (const gchar *appid)
135 gchar *appid_path, *iter;
137 appid_path = g_strconcat ("/", appid, NULL);
138 for (iter = appid_path; *iter; iter++)
148 g_application_impl_destroy (GApplicationImpl *impl)
150 if (impl->session_bus)
153 g_dbus_connection_unregister_object (impl->session_bus,
156 g_object_unref (impl->session_bus);
157 g_free (impl->object_path);
161 g_assert (impl->object_path == NULL);
162 g_assert (impl->object_id == 0);
165 g_slice_free (GApplicationImpl, impl);
169 g_application_impl_register (GApplication *application,
171 GApplicationFlags flags,
173 GCancellable *cancellable,
176 const static GDBusInterfaceVTable vtable = {
177 g_application_impl_method_call
179 GApplicationImpl *impl;
183 impl = g_slice_new (GApplicationImpl);
185 impl->app = application;
186 impl->bus_name = appid;
188 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
191 if (impl->session_bus == NULL)
193 g_slice_free (GApplicationImpl, impl);
197 impl->object_path = application_path_from_appid (appid);
199 if (flags & G_APPLICATION_IS_LAUNCHER)
207 impl->object_id = g_dbus_connection_register_object (impl->session_bus,
209 (GDBusInterfaceInfo *)
210 &org_gtk_Application,
215 if (impl->object_id == 0)
217 g_object_unref (impl->session_bus);
218 g_free (impl->object_path);
219 impl->session_bus = NULL;
220 impl->object_path = NULL;
222 g_slice_free (GApplicationImpl, impl);
226 reply = g_dbus_connection_call_sync (impl->session_bus,
227 "org.freedesktop.DBus",
228 "/org/freedesktop/DBus",
229 "org.freedesktop.DBus",
231 g_variant_new ("(su)",
232 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
233 impl->bus_name, 0x4),
234 G_VARIANT_TYPE ("(u)"),
235 0, -1, cancellable, error);
239 g_dbus_connection_unregister_object (impl->session_bus,
243 g_object_unref (impl->session_bus);
244 g_free (impl->object_path);
245 impl->session_bus = NULL;
246 impl->object_path = NULL;
248 g_slice_free (GApplicationImpl, impl);
252 g_variant_get (reply, "(u)", &rval);
253 g_variant_unref (reply);
255 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
256 if ((*is_remote = (rval == 3)))
258 g_dbus_connection_unregister_object (impl->session_bus,
262 if (flags & G_APPLICATION_IS_SERVICE)
264 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
265 "Unable to acquire bus name `%s'", appid);
266 g_object_unref (impl->session_bus);
267 g_free (impl->object_path);
269 g_slice_free (GApplicationImpl, impl);
278 g_application_impl_activate (GApplicationImpl *impl,
279 GVariant *platform_data)
281 g_dbus_connection_call (impl->session_bus,
284 "org.gtk.Application",
286 g_variant_new ("(@a{sv})", platform_data),
287 NULL, 0, -1, NULL, NULL, NULL);
291 g_application_impl_open (GApplicationImpl *impl,
295 GVariant *platform_data)
297 GVariantBuilder builder;
300 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
301 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
302 for (i = 0; i < n_files; i++)
304 gchar *uri = g_file_get_uri (files[i]);
305 g_variant_builder_add (&builder, "s", uri);
308 g_variant_builder_close (&builder);
309 g_variant_builder_add (&builder, "s", hint);
310 g_variant_builder_add_value (&builder, platform_data);
312 g_dbus_connection_call (impl->session_bus,
315 "org.gtk.Application",
317 g_variant_builder_end (&builder),
318 NULL, 0, -1, NULL, NULL, NULL);
322 g_application_impl_cmdline_method_call (GDBusConnection *connection,
324 const gchar *object_path,
325 const gchar *interface_name,
326 const gchar *method_name,
327 GVariant *parameters,
328 GDBusMethodInvocation *invocation,
331 const gchar *message;
333 g_variant_get_child (parameters, 0, "&s", &message);
335 if (strcmp (method_name, "Print") == 0)
336 g_print ("%s", message);
337 else if (strcmp (method_name, "PrintError") == 0)
338 g_printerr ("%s", message);
340 g_assert_not_reached ();
342 g_dbus_method_invocation_return_value (invocation, NULL);
352 g_application_impl_cmdline_done (GObject *source,
353 GAsyncResult *result,
356 CommandLineData *data = user_data;
357 GError *error = NULL;
360 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
365 g_variant_get (reply, "(i)", &data->status);
366 g_variant_unref (reply);
371 g_printerr ("%s\n", error->message);
372 g_error_free (error);
376 g_main_loop_quit (data->loop);
380 g_application_impl_command_line (GApplicationImpl *impl,
382 GVariant *platform_data)
384 const static GDBusInterfaceVTable vtable = {
385 g_application_impl_cmdline_method_call
387 const gchar *object_path = "/org/gtk/Application/CommandLine";
388 GMainContext *context;
389 CommandLineData data;
392 context = g_main_context_new ();
393 data.loop = g_main_loop_new (context, FALSE);
394 g_main_context_push_thread_default (context);
396 object_id = g_dbus_connection_register_object (impl->session_bus,
398 (GDBusInterfaceInfo *)
399 &org_gtk_private_Cmdline,
400 &vtable, &data, NULL, NULL);
401 /* In theory we should try other paths... */
402 g_assert (object_id != 0);
404 g_dbus_connection_call (impl->session_bus,
407 "org.gtk.Application",
409 g_variant_new ("(o@aay@a{sv})", object_path,
410 arguments, platform_data),
411 G_VARIANT_TYPE ("(i)"), 0, -1, NULL,
412 g_application_impl_cmdline_done, &data);
414 g_main_loop_run (data.loop);
416 g_main_context_pop_thread_default (context);
417 g_main_context_unref (context);
418 g_main_loop_unref (data.loop);
424 g_application_impl_flush (GApplicationImpl *impl)
426 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
433 typedef GApplicationCommandLineClass GDBusCommandLineClass;
434 static GType g_dbus_command_line_get_type (void);
437 GApplicationCommandLine parent_instance;
438 GDBusMethodInvocation *invocation;
440 GDBusConnection *connection;
441 const gchar *bus_name;
442 const gchar *object_path;
446 G_DEFINE_TYPE (GDBusCommandLine,
448 G_TYPE_APPLICATION_COMMAND_LINE)
451 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
452 const gchar *message)
454 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
456 g_dbus_connection_call (gdbcl->connection,
459 "org.gtk.private.CommandLine", "Print",
460 g_variant_new ("(s)", message),
461 NULL, 0, -1, NULL, NULL, NULL);
465 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
466 const gchar *message)
468 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
470 g_dbus_connection_call (gdbcl->connection,
473 "org.gtk.private.CommandLine", "PrintError",
474 g_variant_new ("(s)", message),
475 NULL, 0, -1, NULL, NULL, NULL);
479 g_dbus_command_line_finalize (GObject *object)
481 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
482 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
485 status = g_application_command_line_get_exit_status (cmdline);
487 g_dbus_method_invocation_return_value (gdbcl->invocation,
488 g_variant_new ("(i)", status));
489 g_object_unref (gdbcl->invocation);
491 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
496 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
501 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
503 GObjectClass *object_class = G_OBJECT_CLASS (class);
505 object_class->finalize = g_dbus_command_line_finalize;
506 class->printerr_literal = g_dbus_command_line_printerr_literal;
507 class->print_literal = g_dbus_command_line_print_literal;
510 static GApplicationCommandLine *
511 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
513 GDBusCommandLine *gdbcl;
516 args = g_dbus_method_invocation_get_parameters (invocation);
518 gdbcl = g_object_new (g_dbus_command_line_get_type (),
519 "arguments", g_variant_get_child_value (args, 1),
520 "platform-data", g_variant_get_child_value (args, 2),
522 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
523 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
524 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
525 gdbcl->invocation = g_object_ref (invocation);
527 return G_APPLICATION_COMMAND_LINE (gdbcl);