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 "gapplicationcommandline.h"
34 #include "gdbusmethodinvocation.h"
36 /* DBus Interface definition {{{1 */
37 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
39 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
40 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
42 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
43 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
45 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
46 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
47 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
49 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
50 static const GDBusArgInfo *activate_out[] = { NULL };
52 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
53 static const GDBusArgInfo *open_out[] = { NULL };
55 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
56 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
58 static const GDBusMethodInfo activate_method = {
59 -1, (gchar *) "Activate",
60 (GDBusArgInfo **) activate_in,
61 (GDBusArgInfo **) activate_out
64 static const GDBusMethodInfo open_method = {
66 (GDBusArgInfo **) open_in,
67 (GDBusArgInfo **) open_out
70 static const GDBusMethodInfo command_line_method = {
71 -1, (gchar *) "CommandLine",
72 (GDBusArgInfo **) cmdline_in,
73 (GDBusArgInfo **) cmdline_out
76 static const GDBusMethodInfo *application_methods[] = {
77 &activate_method, &open_method, &command_line_method, NULL
80 const GDBusInterfaceInfo org_gtk_Application = {
81 -1, (gchar *) "org.gtk.Application",
82 (GDBusMethodInfo **) application_methods
85 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
86 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
87 static const GDBusArgInfo *print_out[] = { NULL };
89 static const GDBusMethodInfo stdout_method = {
90 -1, (gchar *) "Print",
91 (GDBusArgInfo **) print_in,
92 (GDBusArgInfo **) print_out
95 static const GDBusMethodInfo stderr_method = {
96 -1, (gchar *) "PrintError",
97 (GDBusArgInfo **) print_in,
98 (GDBusArgInfo **) print_out
101 static const GDBusMethodInfo *cmdline_methods[] = {
102 &stdout_method, &stderr_method, NULL
105 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
106 -1, (gchar *) "org.gtk.private.CommandLine",
107 (GDBusMethodInfo **) cmdline_methods
111 /* GApplication implementation {{{1 */
112 struct _GApplicationImpl
114 GDBusConnection *session_bus;
115 const gchar *bus_name;
122 static GApplicationCommandLine *
123 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
127 g_application_impl_method_call (GDBusConnection *connection,
129 const gchar *object_path,
130 const gchar *interface_name,
131 const gchar *method_name,
132 GVariant *parameters,
133 GDBusMethodInvocation *invocation,
136 GApplicationImpl *impl = user_data;
137 GApplicationClass *class;
139 class = G_APPLICATION_GET_CLASS (impl->app);
141 if (strcmp (method_name, "Activate") == 0)
143 GVariant *platform_data;
145 g_variant_get (parameters, "(@a{sv})", &platform_data);
146 class->before_emit (impl->app, platform_data);
147 g_signal_emit_by_name (impl->app, "activate");
148 class->after_emit (impl->app, platform_data);
149 g_variant_unref (platform_data);
152 else if (strcmp (method_name, "Open") == 0)
154 GVariant *platform_data;
160 g_variant_get (parameters, "(@ass@a{sv})",
161 &array, &hint, &platform_data);
163 n = g_variant_n_children (array);
164 files = g_new (GFile *, n + 1);
166 for (i = 0; i < n; i++)
170 g_variant_get_child (array, i, "&s", &uri);
171 files[i] = g_file_new_for_uri (uri);
173 g_variant_unref (array);
176 class->before_emit (impl->app, platform_data);
177 g_signal_emit_by_name (impl->app, "open", files, n, hint);
178 class->after_emit (impl->app, platform_data);
180 g_variant_unref (platform_data);
182 for (i = 0; i < n; i++)
183 g_object_unref (files[i]);
187 else if (strcmp (method_name, "CommandLine") == 0)
189 GApplicationCommandLine *cmdline;
190 GVariant *platform_data;
193 cmdline = g_dbus_command_line_new (invocation);
194 platform_data = g_variant_get_child_value (parameters, 2);
195 class->before_emit (impl->app, platform_data);
196 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
197 g_application_command_line_set_exit_status (cmdline, status);
198 class->after_emit (impl->app, platform_data);
199 g_variant_unref (platform_data);
200 g_object_unref (cmdline);
204 g_assert_not_reached ();
208 application_path_from_appid (const gchar *appid)
210 gchar *appid_path, *iter;
212 appid_path = g_strconcat ("/", appid, NULL);
213 for (iter = appid_path; *iter; iter++)
223 g_application_impl_destroy (GApplicationImpl *impl)
225 if (impl->session_bus)
228 g_dbus_connection_unregister_object (impl->session_bus,
231 g_object_unref (impl->session_bus);
232 g_free (impl->object_path);
236 g_assert (impl->object_path == NULL);
237 g_assert (impl->object_id == 0);
240 g_slice_free (GApplicationImpl, impl);
244 g_application_impl_register (GApplication *application,
246 GApplicationFlags flags,
248 GCancellable *cancellable,
251 const static GDBusInterfaceVTable vtable = {
252 g_application_impl_method_call
254 GApplicationImpl *impl;
258 impl = g_slice_new (GApplicationImpl);
260 impl->app = application;
261 impl->bus_name = appid;
263 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
266 if (impl->session_bus == NULL)
268 g_slice_free (GApplicationImpl, impl);
272 impl->object_path = application_path_from_appid (appid);
274 if (flags & G_APPLICATION_IS_LAUNCHER)
282 impl->object_id = g_dbus_connection_register_object (impl->session_bus,
284 (GDBusInterfaceInfo *)
285 &org_gtk_Application,
290 if (impl->object_id == 0)
292 g_object_unref (impl->session_bus);
293 g_free (impl->object_path);
294 impl->session_bus = NULL;
295 impl->object_path = NULL;
297 g_slice_free (GApplicationImpl, impl);
301 reply = g_dbus_connection_call_sync (impl->session_bus,
302 "org.freedesktop.DBus",
303 "/org/freedesktop/DBus",
304 "org.freedesktop.DBus",
306 g_variant_new ("(su)",
307 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
308 impl->bus_name, 0x4),
309 G_VARIANT_TYPE ("(u)"),
310 0, -1, cancellable, error);
314 g_dbus_connection_unregister_object (impl->session_bus,
318 g_object_unref (impl->session_bus);
319 g_free (impl->object_path);
320 impl->session_bus = NULL;
321 impl->object_path = NULL;
323 g_slice_free (GApplicationImpl, impl);
327 g_variant_get (reply, "(u)", &rval);
328 g_variant_unref (reply);
330 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
331 if ((*is_remote = (rval == 3)))
333 g_dbus_connection_unregister_object (impl->session_bus,
337 if (flags & G_APPLICATION_IS_SERVICE)
339 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
340 "Unable to acquire bus name `%s'", appid);
341 g_object_unref (impl->session_bus);
342 g_free (impl->object_path);
344 g_slice_free (GApplicationImpl, impl);
353 g_application_impl_activate (GApplicationImpl *impl,
354 GVariant *platform_data)
356 g_dbus_connection_call (impl->session_bus,
359 "org.gtk.Application",
361 g_variant_new ("(@a{sv})", platform_data),
362 NULL, 0, -1, NULL, NULL, NULL);
366 g_application_impl_open (GApplicationImpl *impl,
370 GVariant *platform_data)
372 GVariantBuilder builder;
375 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
376 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
377 for (i = 0; i < n_files; i++)
379 gchar *uri = g_file_get_uri (files[i]);
380 g_variant_builder_add (&builder, "s", uri);
383 g_variant_builder_close (&builder);
384 g_variant_builder_add (&builder, "s", hint);
385 g_variant_builder_add_value (&builder, platform_data);
387 g_dbus_connection_call (impl->session_bus,
390 "org.gtk.Application",
392 g_variant_builder_end (&builder),
393 NULL, 0, -1, NULL, NULL, NULL);
397 g_application_impl_cmdline_method_call (GDBusConnection *connection,
399 const gchar *object_path,
400 const gchar *interface_name,
401 const gchar *method_name,
402 GVariant *parameters,
403 GDBusMethodInvocation *invocation,
406 const gchar *message;
408 g_variant_get_child (parameters, 0, "&s", &message);
410 if (strcmp (method_name, "Print") == 0)
411 g_print ("%s", message);
412 else if (strcmp (method_name, "PrintError") == 0)
413 g_printerr ("%s", message);
415 g_assert_not_reached ();
417 g_dbus_method_invocation_return_value (invocation, NULL);
427 g_application_impl_cmdline_done (GObject *source,
428 GAsyncResult *result,
431 CommandLineData *data = user_data;
432 GError *error = NULL;
435 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
440 g_variant_get (reply, "(i)", &data->status);
441 g_variant_unref (reply);
446 g_printerr ("%s\n", error->message);
447 g_error_free (error);
451 g_main_loop_quit (data->loop);
455 g_application_impl_command_line (GApplicationImpl *impl,
457 GVariant *platform_data)
459 const static GDBusInterfaceVTable vtable = {
460 g_application_impl_cmdline_method_call
462 const gchar *object_path = "/org/gtk/Application/CommandLine";
463 GMainContext *context;
464 CommandLineData data;
467 context = g_main_context_new ();
468 data.loop = g_main_loop_new (context, FALSE);
469 g_main_context_push_thread_default (context);
471 object_id = g_dbus_connection_register_object (impl->session_bus,
473 (GDBusInterfaceInfo *)
474 &org_gtk_private_Cmdline,
475 &vtable, &data, NULL, NULL);
476 /* In theory we should try other paths... */
477 g_assert (object_id != 0);
479 g_dbus_connection_call (impl->session_bus,
482 "org.gtk.Application",
484 g_variant_new ("(o^aay@a{sv})", object_path,
485 arguments, platform_data),
486 G_VARIANT_TYPE ("(i)"), 0, -1, NULL,
487 g_application_impl_cmdline_done, &data);
489 g_main_loop_run (data.loop);
491 g_main_context_pop_thread_default (context);
492 g_main_context_unref (context);
493 g_main_loop_unref (data.loop);
499 g_application_impl_flush (GApplicationImpl *impl)
501 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
505 /* GDBusCommandLine implementation {{{1 */
507 typedef GApplicationCommandLineClass GDBusCommandLineClass;
508 static GType g_dbus_command_line_get_type (void);
511 GApplicationCommandLine parent_instance;
512 GDBusMethodInvocation *invocation;
514 GDBusConnection *connection;
515 const gchar *bus_name;
516 const gchar *object_path;
520 G_DEFINE_TYPE (GDBusCommandLine,
522 G_TYPE_APPLICATION_COMMAND_LINE)
525 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
526 const gchar *message)
528 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
530 g_dbus_connection_call (gdbcl->connection,
533 "org.gtk.private.CommandLine", "Print",
534 g_variant_new ("(s)", message),
535 NULL, 0, -1, NULL, NULL, NULL);
539 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
540 const gchar *message)
542 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
544 g_dbus_connection_call (gdbcl->connection,
547 "org.gtk.private.CommandLine", "PrintError",
548 g_variant_new ("(s)", message),
549 NULL, 0, -1, NULL, NULL, NULL);
553 g_dbus_command_line_finalize (GObject *object)
555 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
556 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
559 status = g_application_command_line_get_exit_status (cmdline);
561 g_dbus_method_invocation_return_value (gdbcl->invocation,
562 g_variant_new ("(i)", status));
563 g_object_unref (gdbcl->invocation);
565 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
570 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
575 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
577 GObjectClass *object_class = G_OBJECT_CLASS (class);
579 object_class->finalize = g_dbus_command_line_finalize;
580 class->printerr_literal = g_dbus_command_line_printerr_literal;
581 class->print_literal = g_dbus_command_line_print_literal;
584 static GApplicationCommandLine *
585 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
587 GDBusCommandLine *gdbcl;
590 args = g_dbus_method_invocation_get_parameters (invocation);
592 gdbcl = g_object_new (g_dbus_command_line_get_type (),
593 "arguments", g_variant_get_child_value (args, 1),
594 "platform-data", g_variant_get_child_value (args, 2),
596 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
597 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
598 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
599 gdbcl->invocation = g_object_ref (invocation);
601 return G_APPLICATION_COMMAND_LINE (gdbcl);
606 /* vim:set foldmethod=marker: */