Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / gapplicationimpl-dbus.c
blobac029a9f361f3e9e170d10fd26aa086415c51ec6
1 /*
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.1 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, see <http://www.gnu.org/licenses/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #include "gapplicationimpl.h"
24 #include "gactiongroup.h"
25 #include "gactiongroupexporter.h"
26 #include "gremoteactiongroup.h"
27 #include "gdbusactiongroup-private.h"
28 #include "gapplication.h"
29 #include "gfile.h"
30 #include "gdbusconnection.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
33 #include "glib/gstdio.h"
35 #include <string.h>
36 #include <stdio.h>
38 #include "gapplicationcommandline.h"
39 #include "gdbusmethodinvocation.h"
41 #ifdef G_OS_UNIX
42 #include "gunixinputstream.h"
43 #include "gunixfdlist.h"
44 #endif
46 /* DBus Interface definition {{{1 */
48 /* For documentation of these interfaces, see
49 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
51 static const gchar org_gtk_Application_xml[] =
52 "<node>"
53 "<interface name='org.gtk.Application'>"
54 "<method name='Activate'>"
55 "<arg type='a{sv}' name='platform-data' direction='in'/>"
56 "</method>"
57 "<method name='Open'>"
58 "<arg type='as' name='uris' direction='in'/>"
59 "<arg type='s' name='hint' direction='in'/>"
60 "<arg type='a{sv}' name='platform-data' direction='in'/>"
61 "</method>"
62 "<method name='CommandLine'>"
63 "<arg type='o' name='path' direction='in'/>"
64 "<arg type='aay' name='arguments' direction='in'/>"
65 "<arg type='a{sv}' name='platform-data' direction='in'/>"
66 "<arg type='i' name='exit-status' direction='out'/>"
67 "</method>"
68 "<property name='Busy' type='b' access='read'/>"
69 "</interface>"
70 "</node>";
72 static GDBusInterfaceInfo *org_gtk_Application;
74 static const gchar org_freedesktop_Application_xml[] =
75 "<node>"
76 "<interface name='org.freedesktop.Application'>"
77 "<method name='Activate'>"
78 "<arg type='a{sv}' name='platform-data' direction='in'/>"
79 "</method>"
80 "<method name='Open'>"
81 "<arg type='as' name='uris' direction='in'/>"
82 "<arg type='a{sv}' name='platform-data' direction='in'/>"
83 "</method>"
84 "<method name='ActivateAction'>"
85 "<arg type='s' name='action-name' direction='in'/>"
86 "<arg type='av' name='parameter' direction='in'/>"
87 "<arg type='a{sv}' name='platform-data' direction='in'/>"
88 "</method>"
89 "</interface>"
90 "</node>";
92 static GDBusInterfaceInfo *org_freedesktop_Application;
94 static const gchar org_gtk_private_CommandLine_xml[] =
95 "<node>"
96 "<interface name='org.gtk.private.CommandLine'>"
97 "<method name='Print'>"
98 "<arg type='s' name='message' direction='in'/>"
99 "</method>"
100 "<method name='PrintError'>"
101 "<arg type='s' name='message' direction='in'/>"
102 "</method>"
103 "</interface>"
104 "</node>";
106 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
108 /* GApplication implementation {{{1 */
109 struct _GApplicationImpl
111 GDBusConnection *session_bus;
112 GActionGroup *exported_actions;
113 const gchar *bus_name;
115 gchar *object_path;
116 guint object_id;
117 guint fdo_object_id;
118 guint actions_id;
120 gboolean properties_live;
121 gboolean primary;
122 gboolean busy;
123 gboolean registered;
124 GApplication *app;
128 static GApplicationCommandLine *
129 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
131 static GVariant *
132 g_application_impl_get_property (GDBusConnection *connection,
133 const gchar *sender,
134 const gchar *object_path,
135 const gchar *interface_name,
136 const gchar *property_name,
137 GError **error,
138 gpointer user_data)
140 GApplicationImpl *impl = user_data;
142 if (strcmp (property_name, "Busy") == 0)
143 return g_variant_new_boolean (impl->busy);
145 g_assert_not_reached ();
147 return NULL;
150 static void
151 send_property_change (GApplicationImpl *impl)
153 GVariantBuilder builder;
155 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
156 g_variant_builder_add (&builder,
157 "{sv}",
158 "Busy", g_variant_new_boolean (impl->busy));
160 g_dbus_connection_emit_signal (impl->session_bus,
161 NULL,
162 impl->object_path,
163 "org.freedesktop.DBus.Properties",
164 "PropertiesChanged",
165 g_variant_new ("(sa{sv}as)",
166 "org.gtk.Application",
167 &builder,
168 NULL),
169 NULL);
172 static void
173 g_application_impl_method_call (GDBusConnection *connection,
174 const gchar *sender,
175 const gchar *object_path,
176 const gchar *interface_name,
177 const gchar *method_name,
178 GVariant *parameters,
179 GDBusMethodInvocation *invocation,
180 gpointer user_data)
182 GApplicationImpl *impl = user_data;
183 GApplicationClass *class;
185 class = G_APPLICATION_GET_CLASS (impl->app);
187 if (strcmp (method_name, "Activate") == 0)
189 GVariant *platform_data;
191 /* Completely the same for both freedesktop and gtk interfaces */
193 g_variant_get (parameters, "(@a{sv})", &platform_data);
195 class->before_emit (impl->app, platform_data);
196 g_signal_emit_by_name (impl->app, "activate");
197 class->after_emit (impl->app, platform_data);
198 g_variant_unref (platform_data);
200 g_dbus_method_invocation_return_value (invocation, NULL);
203 else if (strcmp (method_name, "Open") == 0)
205 GApplicationFlags flags;
206 GVariant *platform_data;
207 const gchar *hint;
208 GVariant *array;
209 GFile **files;
210 gint n, i;
212 flags = g_application_get_flags (impl->app);
213 if ((flags & G_APPLICATION_HANDLES_OPEN) == 0)
215 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files");
216 return;
219 /* freedesktop interface has no hint parameter */
220 if (g_str_equal (interface_name, "org.freedesktop.Application"))
222 g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
223 hint = "";
225 else
226 g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
228 n = g_variant_n_children (array);
229 files = g_new (GFile *, n + 1);
231 for (i = 0; i < n; i++)
233 const gchar *uri;
235 g_variant_get_child (array, i, "&s", &uri);
236 files[i] = g_file_new_for_uri (uri);
238 g_variant_unref (array);
239 files[n] = NULL;
241 class->before_emit (impl->app, platform_data);
242 g_signal_emit_by_name (impl->app, "open", files, n, hint);
243 class->after_emit (impl->app, platform_data);
245 g_variant_unref (platform_data);
247 for (i = 0; i < n; i++)
248 g_object_unref (files[i]);
249 g_free (files);
251 g_dbus_method_invocation_return_value (invocation, NULL);
254 else if (strcmp (method_name, "CommandLine") == 0)
256 GApplicationFlags flags;
257 GApplicationCommandLine *cmdline;
258 GVariant *platform_data;
259 int status;
261 flags = g_application_get_flags (impl->app);
262 if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0)
264 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED,
265 "Application does not handle command line arguments");
266 return;
269 /* Only on the GtkApplication interface */
271 cmdline = g_dbus_command_line_new (invocation);
272 platform_data = g_variant_get_child_value (parameters, 2);
273 class->before_emit (impl->app, platform_data);
274 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
275 g_application_command_line_set_exit_status (cmdline, status);
276 class->after_emit (impl->app, platform_data);
277 g_variant_unref (platform_data);
278 g_object_unref (cmdline);
280 else if (g_str_equal (method_name, "ActivateAction"))
282 GVariant *parameter = NULL;
283 GVariant *platform_data;
284 GVariantIter *iter;
285 const gchar *name;
287 /* Only on the freedesktop interface */
289 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
290 g_variant_iter_next (iter, "v", &parameter);
291 g_variant_iter_free (iter);
293 class->before_emit (impl->app, platform_data);
294 g_action_group_activate_action (impl->exported_actions, name, parameter);
295 class->after_emit (impl->app, platform_data);
297 if (parameter)
298 g_variant_unref (parameter);
300 g_variant_unref (platform_data);
302 g_dbus_method_invocation_return_value (invocation, NULL);
304 else
305 g_assert_not_reached ();
308 static gchar *
309 application_path_from_appid (const gchar *appid)
311 gchar *appid_path, *iter;
313 if (appid == NULL)
314 /* this is a private implementation detail */
315 return g_strdup ("/org/gtk/Application/anonymous");
317 appid_path = g_strconcat ("/", appid, NULL);
318 for (iter = appid_path; *iter; iter++)
320 if (*iter == '.')
321 *iter = '/';
323 if (*iter == '-')
324 *iter = '_';
327 return appid_path;
330 /* Attempt to become the primary instance.
332 * Returns %TRUE if everything went OK, regardless of if we became the
333 * primary instance or not. %FALSE is reserved for when something went
334 * seriously wrong (and @error will be set too, in that case).
336 * After a %TRUE return, impl->primary will be TRUE if we were
337 * successful.
339 static gboolean
340 g_application_impl_attempt_primary (GApplicationImpl *impl,
341 GCancellable *cancellable,
342 GError **error)
344 const static GDBusInterfaceVTable vtable = {
345 g_application_impl_method_call,
346 g_application_impl_get_property,
347 NULL /* set_property */
349 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
350 GVariant *reply;
351 guint32 rval;
353 if (org_gtk_Application == NULL)
355 GError *error = NULL;
356 GDBusNodeInfo *info;
358 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
359 if G_UNLIKELY (info == NULL)
360 g_error ("%s", error->message);
361 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
362 g_assert (org_gtk_Application != NULL);
363 g_dbus_interface_info_ref (org_gtk_Application);
364 g_dbus_node_info_unref (info);
366 info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error);
367 if G_UNLIKELY (info == NULL)
368 g_error ("%s", error->message);
369 org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
370 g_assert (org_freedesktop_Application != NULL);
371 g_dbus_interface_info_ref (org_freedesktop_Application);
372 g_dbus_node_info_unref (info);
375 /* We could possibly have been D-Bus activated as a result of incoming
376 * requests on either the application or actiongroup interfaces.
377 * Because of how GDBus dispatches messages, we need to ensure that
378 * both of those things are registered before we attempt to request
379 * our name.
381 * The action group need not be populated yet, as long as it happens
382 * before we return to the mainloop. The reason for that is because
383 * GDBus does the check to make sure the object exists from the worker
384 * thread but doesn't actually dispatch the action invocation until we
385 * hit the mainloop in this thread. There is also no danger of
386 * receiving 'activate' or 'open' signals until after 'startup' runs,
387 * for the same reason.
389 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
390 org_gtk_Application, &vtable, impl, NULL, error);
392 if (impl->object_id == 0)
393 return FALSE;
395 impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
396 org_freedesktop_Application, &vtable, impl, NULL, error);
398 if (impl->fdo_object_id == 0)
399 return FALSE;
401 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
402 impl->exported_actions, error);
404 if (impl->actions_id == 0)
405 return FALSE;
407 impl->registered = TRUE;
408 if (!app_class->dbus_register (impl->app,
409 impl->session_bus,
410 impl->object_path,
411 error))
412 return FALSE;
414 if (impl->bus_name == NULL)
416 /* If this is a non-unique application then it is sufficient to
417 * have our object paths registered. We can return now.
419 * Note: non-unique applications always act as primary-instance.
421 impl->primary = TRUE;
422 return TRUE;
425 /* If this is a unique application then we need to attempt to own
426 * the well-known name and fall back to remote mode (!is_primary)
427 * in the case that we can't do that.
429 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
430 reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
431 "org.freedesktop.DBus", "RequestName",
432 g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
433 0, -1, cancellable, error);
435 if (reply == NULL)
436 return FALSE;
438 g_variant_get (reply, "(u)", &rval);
439 g_variant_unref (reply);
441 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
442 impl->primary = (rval != 3);
444 return TRUE;
447 /* Stop doing the things that the primary instance does.
449 * This should be called if attempting to become the primary instance
450 * failed (in order to clean up any partial success) and should also
451 * be called when freeing the GApplication.
453 * It is safe to call this multiple times.
455 static void
456 g_application_impl_stop_primary (GApplicationImpl *impl)
458 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
460 if (impl->registered)
462 app_class->dbus_unregister (impl->app,
463 impl->session_bus,
464 impl->object_path);
465 impl->registered = FALSE;
468 if (impl->object_id)
470 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
471 impl->object_id = 0;
474 if (impl->fdo_object_id)
476 g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
477 impl->fdo_object_id = 0;
480 if (impl->actions_id)
482 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
483 impl->actions_id = 0;
486 if (impl->primary && impl->bus_name)
488 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
489 "/org/freedesktop/DBus", "org.freedesktop.DBus",
490 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
491 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
492 impl->primary = FALSE;
496 void
497 g_application_impl_set_busy_state (GApplicationImpl *impl,
498 gboolean busy)
500 if (impl->busy != busy)
502 impl->busy = busy;
503 send_property_change (impl);
507 void
508 g_application_impl_destroy (GApplicationImpl *impl)
510 g_application_impl_stop_primary (impl);
512 if (impl->session_bus)
513 g_object_unref (impl->session_bus);
515 g_free (impl->object_path);
517 g_slice_free (GApplicationImpl, impl);
520 GApplicationImpl *
521 g_application_impl_register (GApplication *application,
522 const gchar *appid,
523 GApplicationFlags flags,
524 GActionGroup *exported_actions,
525 GRemoteActionGroup **remote_actions,
526 GCancellable *cancellable,
527 GError **error)
529 GDBusActionGroup *actions;
530 GApplicationImpl *impl;
532 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
534 impl = g_slice_new0 (GApplicationImpl);
536 impl->app = application;
537 impl->exported_actions = exported_actions;
539 /* non-unique applications do not attempt to acquire a bus name */
540 if (~flags & G_APPLICATION_NON_UNIQUE)
541 impl->bus_name = appid;
543 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
545 if (impl->session_bus == NULL)
547 /* If we can't connect to the session bus, proceed as a normal
548 * non-unique application.
550 *remote_actions = NULL;
551 return impl;
554 impl->object_path = application_path_from_appid (appid);
556 /* Only try to be the primary instance if
557 * G_APPLICATION_IS_LAUNCHER was not specified.
559 if (~flags & G_APPLICATION_IS_LAUNCHER)
561 if (!g_application_impl_attempt_primary (impl, cancellable, error))
563 g_application_impl_destroy (impl);
564 return NULL;
567 if (impl->primary)
568 return impl;
570 /* We didn't make it. Drop our service-side stuff. */
571 g_application_impl_stop_primary (impl);
573 if (flags & G_APPLICATION_IS_SERVICE)
575 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
576 "Unable to acquire bus name '%s'", appid);
577 g_application_impl_destroy (impl);
579 return NULL;
583 /* We are non-primary. Try to get the primary's list of actions.
584 * This also serves as a mechanism to ensure that the primary exists
585 * (ie: DBus service files installed correctly, etc).
587 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
588 if (!g_dbus_action_group_sync (actions, cancellable, error))
590 /* The primary appears not to exist. Fail the registration. */
591 g_application_impl_destroy (impl);
592 g_object_unref (actions);
594 return NULL;
597 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
599 return impl;
602 void
603 g_application_impl_activate (GApplicationImpl *impl,
604 GVariant *platform_data)
606 g_dbus_connection_call (impl->session_bus,
607 impl->bus_name,
608 impl->object_path,
609 "org.gtk.Application",
610 "Activate",
611 g_variant_new ("(@a{sv})", platform_data),
612 NULL, 0, -1, NULL, NULL, NULL);
615 void
616 g_application_impl_open (GApplicationImpl *impl,
617 GFile **files,
618 gint n_files,
619 const gchar *hint,
620 GVariant *platform_data)
622 GVariantBuilder builder;
623 gint i;
625 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
626 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
627 for (i = 0; i < n_files; i++)
629 gchar *uri = g_file_get_uri (files[i]);
630 g_variant_builder_add (&builder, "s", uri);
631 g_free (uri);
633 g_variant_builder_close (&builder);
634 g_variant_builder_add (&builder, "s", hint);
635 g_variant_builder_add_value (&builder, platform_data);
637 g_dbus_connection_call (impl->session_bus,
638 impl->bus_name,
639 impl->object_path,
640 "org.gtk.Application",
641 "Open",
642 g_variant_builder_end (&builder),
643 NULL, 0, -1, NULL, NULL, NULL);
646 static void
647 g_application_impl_cmdline_method_call (GDBusConnection *connection,
648 const gchar *sender,
649 const gchar *object_path,
650 const gchar *interface_name,
651 const gchar *method_name,
652 GVariant *parameters,
653 GDBusMethodInvocation *invocation,
654 gpointer user_data)
656 const gchar *message;
658 g_variant_get_child (parameters, 0, "&s", &message);
660 if (strcmp (method_name, "Print") == 0)
661 g_print ("%s", message);
662 else if (strcmp (method_name, "PrintError") == 0)
663 g_printerr ("%s", message);
664 else
665 g_assert_not_reached ();
667 g_dbus_method_invocation_return_value (invocation, NULL);
670 typedef struct
672 GMainLoop *loop;
673 int status;
674 } CommandLineData;
676 static void
677 g_application_impl_cmdline_done (GObject *source,
678 GAsyncResult *result,
679 gpointer user_data)
681 CommandLineData *data = user_data;
682 GError *error = NULL;
683 GVariant *reply;
685 #ifdef G_OS_UNIX
686 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
687 #else
688 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
689 #endif
692 if (reply != NULL)
694 g_variant_get (reply, "(i)", &data->status);
695 g_variant_unref (reply);
698 else
700 g_printerr ("%s\n", error->message);
701 g_error_free (error);
702 data->status = 1;
705 g_main_loop_quit (data->loop);
709 g_application_impl_command_line (GApplicationImpl *impl,
710 const gchar * const *arguments,
711 GVariant *platform_data)
713 const static GDBusInterfaceVTable vtable = {
714 g_application_impl_cmdline_method_call
716 const gchar *object_path = "/org/gtk/Application/CommandLine";
717 GMainContext *context;
718 CommandLineData data;
719 guint object_id;
721 context = g_main_context_new ();
722 data.loop = g_main_loop_new (context, FALSE);
723 g_main_context_push_thread_default (context);
725 if (org_gtk_private_CommandLine == NULL)
727 GError *error = NULL;
728 GDBusNodeInfo *info;
730 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
731 if G_UNLIKELY (info == NULL)
732 g_error ("%s", error->message);
733 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
734 g_assert (org_gtk_private_CommandLine != NULL);
735 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
736 g_dbus_node_info_unref (info);
739 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
740 org_gtk_private_CommandLine,
741 &vtable, &data, NULL, NULL);
742 /* In theory we should try other paths... */
743 g_assert (object_id != 0);
745 #ifdef G_OS_UNIX
747 GError *error = NULL;
748 GUnixFDList *fd_list;
750 /* send along the stdin in case
751 * g_application_command_line_get_stdin_data() is called
753 fd_list = g_unix_fd_list_new ();
754 g_unix_fd_list_append (fd_list, 0, &error);
755 g_assert_no_error (error);
757 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
758 "org.gtk.Application", "CommandLine",
759 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
760 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
761 g_application_impl_cmdline_done, &data);
762 g_object_unref (fd_list);
764 #else
765 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
766 "org.gtk.Application", "CommandLine",
767 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
768 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
769 g_application_impl_cmdline_done, &data);
770 #endif
772 g_main_loop_run (data.loop);
774 g_main_context_pop_thread_default (context);
775 g_main_context_unref (context);
776 g_main_loop_unref (data.loop);
778 return data.status;
781 void
782 g_application_impl_flush (GApplicationImpl *impl)
784 if (impl->session_bus)
785 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
788 GDBusConnection *
789 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
791 return impl->session_bus;
794 const gchar *
795 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
797 return impl->object_path;
800 /* GDBusCommandLine implementation {{{1 */
802 typedef GApplicationCommandLineClass GDBusCommandLineClass;
803 static GType g_dbus_command_line_get_type (void);
804 typedef struct
806 GApplicationCommandLine parent_instance;
807 GDBusMethodInvocation *invocation;
809 GDBusConnection *connection;
810 const gchar *bus_name;
811 const gchar *object_path;
812 } GDBusCommandLine;
815 G_DEFINE_TYPE (GDBusCommandLine,
816 g_dbus_command_line,
817 G_TYPE_APPLICATION_COMMAND_LINE)
819 static void
820 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
821 const gchar *message)
823 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
825 g_dbus_connection_call (gdbcl->connection,
826 gdbcl->bus_name,
827 gdbcl->object_path,
828 "org.gtk.private.CommandLine", "Print",
829 g_variant_new ("(s)", message),
830 NULL, 0, -1, NULL, NULL, NULL);
833 static void
834 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
835 const gchar *message)
837 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
839 g_dbus_connection_call (gdbcl->connection,
840 gdbcl->bus_name,
841 gdbcl->object_path,
842 "org.gtk.private.CommandLine", "PrintError",
843 g_variant_new ("(s)", message),
844 NULL, 0, -1, NULL, NULL, NULL);
847 static GInputStream *
848 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
850 #ifdef G_OS_UNIX
851 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
852 GInputStream *result = NULL;
853 GDBusMessage *message;
854 GUnixFDList *fd_list;
856 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
857 fd_list = g_dbus_message_get_unix_fd_list (message);
859 if (fd_list && g_unix_fd_list_get_length (fd_list))
861 gint *fds, n_fds, i;
863 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
864 result = g_unix_input_stream_new (fds[0], TRUE);
865 for (i = 1; i < n_fds; i++)
866 (void) g_close (fds[i], NULL);
867 g_free (fds);
870 return result;
871 #else
872 return NULL;
873 #endif
876 static void
877 g_dbus_command_line_finalize (GObject *object)
879 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
880 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
881 gint status;
883 status = g_application_command_line_get_exit_status (cmdline);
885 g_dbus_method_invocation_return_value (gdbcl->invocation,
886 g_variant_new ("(i)", status));
887 g_object_unref (gdbcl->invocation);
889 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
890 ->finalize (object);
893 static void
894 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
898 static void
899 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
901 GObjectClass *object_class = G_OBJECT_CLASS (class);
903 object_class->finalize = g_dbus_command_line_finalize;
904 class->printerr_literal = g_dbus_command_line_printerr_literal;
905 class->print_literal = g_dbus_command_line_print_literal;
906 class->get_stdin = g_dbus_command_line_get_stdin;
909 static GApplicationCommandLine *
910 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
912 GDBusCommandLine *gdbcl;
913 GVariant *args;
914 GVariant *arguments, *platform_data;
916 args = g_dbus_method_invocation_get_parameters (invocation);
918 arguments = g_variant_get_child_value (args, 1);
919 platform_data = g_variant_get_child_value (args, 2);
920 gdbcl = g_object_new (g_dbus_command_line_get_type (),
921 "arguments", arguments,
922 "platform-data", platform_data,
923 NULL);
924 g_variant_unref (arguments);
925 g_variant_unref (platform_data);
927 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
928 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
929 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
930 gdbcl->invocation = g_object_ref (invocation);
932 return G_APPLICATION_COMMAND_LINE (gdbcl);
935 /* Epilogue {{{1 */
937 /* vim:set foldmethod=marker: */