2 * Copyright © 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gapplicationimpl.h"
26 #include "gactiongroup.h"
27 #include "gactiongroupexporter.h"
28 #include "gremoteactiongroup.h"
29 #include "gdbusactiongroup-private.h"
30 #include "gapplication.h"
32 #include "gdbusconnection.h"
33 #include "gdbusintrospection.h"
34 #include "gdbuserror.h"
35 #include "glib/gstdio.h"
40 #include "gapplicationcommandline.h"
41 #include "gdbusmethodinvocation.h"
44 #include "gunixinputstream.h"
45 #include "gunixfdlist.h"
48 /* D-Bus Interface definition {{{1 */
50 /* For documentation of these interfaces, see
51 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
53 static const gchar org_gtk_Application_xml[] =
55 "<interface name='org.gtk.Application'>"
56 "<method name='Activate'>"
57 "<arg type='a{sv}' name='platform-data' direction='in'/>"
59 "<method name='Open'>"
60 "<arg type='as' name='uris' direction='in'/>"
61 "<arg type='s' name='hint' direction='in'/>"
62 "<arg type='a{sv}' name='platform-data' direction='in'/>"
64 "<method name='CommandLine'>"
65 "<arg type='o' name='path' direction='in'/>"
66 "<arg type='aay' name='arguments' direction='in'/>"
67 "<arg type='a{sv}' name='platform-data' direction='in'/>"
68 "<arg type='i' name='exit-status' direction='out'/>"
70 "<property name='Busy' type='b' access='read'/>"
74 static GDBusInterfaceInfo *org_gtk_Application;
76 static const gchar org_freedesktop_Application_xml[] =
78 "<interface name='org.freedesktop.Application'>"
79 "<method name='Activate'>"
80 "<arg type='a{sv}' name='platform-data' direction='in'/>"
82 "<method name='Open'>"
83 "<arg type='as' name='uris' direction='in'/>"
84 "<arg type='a{sv}' name='platform-data' direction='in'/>"
86 "<method name='ActivateAction'>"
87 "<arg type='s' name='action-name' direction='in'/>"
88 "<arg type='av' name='parameter' direction='in'/>"
89 "<arg type='a{sv}' name='platform-data' direction='in'/>"
94 static GDBusInterfaceInfo *org_freedesktop_Application;
96 static const gchar org_gtk_private_CommandLine_xml[] =
98 "<interface name='org.gtk.private.CommandLine'>"
99 "<method name='Print'>"
100 "<arg type='s' name='message' direction='in'/>"
102 "<method name='PrintError'>"
103 "<arg type='s' name='message' direction='in'/>"
108 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
110 /* GApplication implementation {{{1 */
111 struct _GApplicationImpl
113 GDBusConnection *session_bus;
114 GActionGroup *exported_actions;
115 const gchar *bus_name;
116 guint name_lost_signal;
123 gboolean properties_live;
131 static GApplicationCommandLine *
132 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
135 g_application_impl_get_property (GDBusConnection *connection,
137 const gchar *object_path,
138 const gchar *interface_name,
139 const gchar *property_name,
143 GApplicationImpl *impl = user_data;
145 if (strcmp (property_name, "Busy") == 0)
146 return g_variant_new_boolean (impl->busy);
148 g_assert_not_reached ();
154 send_property_change (GApplicationImpl *impl)
156 GVariantBuilder builder;
158 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
159 g_variant_builder_add (&builder,
161 "Busy", g_variant_new_boolean (impl->busy));
163 g_dbus_connection_emit_signal (impl->session_bus,
166 "org.freedesktop.DBus.Properties",
168 g_variant_new ("(sa{sv}as)",
169 "org.gtk.Application",
176 g_application_impl_method_call (GDBusConnection *connection,
178 const gchar *object_path,
179 const gchar *interface_name,
180 const gchar *method_name,
181 GVariant *parameters,
182 GDBusMethodInvocation *invocation,
185 GApplicationImpl *impl = user_data;
186 GApplicationClass *class;
188 class = G_APPLICATION_GET_CLASS (impl->app);
190 if (strcmp (method_name, "Activate") == 0)
192 GVariant *platform_data;
194 /* Completely the same for both freedesktop and gtk interfaces */
196 g_variant_get (parameters, "(@a{sv})", &platform_data);
198 class->before_emit (impl->app, platform_data);
199 g_signal_emit_by_name (impl->app, "activate");
200 class->after_emit (impl->app, platform_data);
201 g_variant_unref (platform_data);
203 g_dbus_method_invocation_return_value (invocation, NULL);
206 else if (strcmp (method_name, "Open") == 0)
208 GApplicationFlags flags;
209 GVariant *platform_data;
215 flags = g_application_get_flags (impl->app);
216 if ((flags & G_APPLICATION_HANDLES_OPEN) == 0)
218 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files");
222 /* freedesktop interface has no hint parameter */
223 if (g_str_equal (interface_name, "org.freedesktop.Application"))
225 g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
229 g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
231 n = g_variant_n_children (array);
232 files = g_new (GFile *, n + 1);
234 for (i = 0; i < n; i++)
238 g_variant_get_child (array, i, "&s", &uri);
239 files[i] = g_file_new_for_uri (uri);
241 g_variant_unref (array);
244 class->before_emit (impl->app, platform_data);
245 g_signal_emit_by_name (impl->app, "open", files, n, hint);
246 class->after_emit (impl->app, platform_data);
248 g_variant_unref (platform_data);
250 for (i = 0; i < n; i++)
251 g_object_unref (files[i]);
254 g_dbus_method_invocation_return_value (invocation, NULL);
257 else if (strcmp (method_name, "CommandLine") == 0)
259 GApplicationFlags flags;
260 GApplicationCommandLine *cmdline;
261 GVariant *platform_data;
264 flags = g_application_get_flags (impl->app);
265 if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0)
267 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED,
268 "Application does not handle command line arguments");
272 /* Only on the GtkApplication interface */
274 cmdline = g_dbus_command_line_new (invocation);
275 platform_data = g_variant_get_child_value (parameters, 2);
276 class->before_emit (impl->app, platform_data);
277 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
278 g_application_command_line_set_exit_status (cmdline, status);
279 class->after_emit (impl->app, platform_data);
280 g_variant_unref (platform_data);
281 g_object_unref (cmdline);
283 else if (g_str_equal (method_name, "ActivateAction"))
285 GVariant *parameter = NULL;
286 GVariant *platform_data;
289 const GVariantType *parameter_type = NULL;
291 /* Only on the freedesktop interface */
293 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
294 g_variant_iter_next (iter, "v", ¶meter);
295 g_variant_iter_free (iter);
297 /* Check the action exists and the parameter type matches. */
298 if (!g_action_group_query_action (impl->exported_actions,
299 name, NULL, ¶meter_type,
302 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
303 "Unknown action ‘%s’", name);
304 g_clear_pointer (¶meter, g_variant_unref);
305 g_variant_unref (platform_data);
309 if (!((parameter_type == NULL && parameter == NULL) ||
310 (parameter_type != NULL && parameter != NULL && g_variant_is_of_type (parameter, parameter_type))))
312 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
313 "Invalid parameter for action ‘%s’: expected type %s but got type %s",
315 (parameter_type != NULL) ? (const gchar *) parameter_type : "()",
316 (parameter != NULL) ? g_variant_get_type_string (parameter) : "()");
317 g_clear_pointer (¶meter, g_variant_unref);
318 g_variant_unref (platform_data);
322 class->before_emit (impl->app, platform_data);
323 g_action_group_activate_action (impl->exported_actions, name, parameter);
324 class->after_emit (impl->app, platform_data);
327 g_variant_unref (parameter);
329 g_variant_unref (platform_data);
331 g_dbus_method_invocation_return_value (invocation, NULL);
334 g_assert_not_reached ();
338 application_path_from_appid (const gchar *appid)
340 gchar *appid_path, *iter;
343 /* this is a private implementation detail */
344 return g_strdup ("/org/gtk/Application/anonymous");
346 appid_path = g_strconcat ("/", appid, NULL);
347 for (iter = appid_path; *iter; iter++)
359 static void g_application_impl_stop_primary (GApplicationImpl *impl);
362 name_lost (GDBusConnection *bus,
363 const char *sender_name,
364 const char *object_path,
365 const char *interface_name,
366 const char *signal_name,
367 GVariant *parameters,
370 GApplicationImpl *impl = user_data;
373 impl->primary = FALSE;
374 g_application_impl_stop_primary (impl);
375 g_signal_emit_by_name (impl->app, "name-lost", &handled);
378 /* Attempt to become the primary instance.
380 * Returns %TRUE if everything went OK, regardless of if we became the
381 * primary instance or not. %FALSE is reserved for when something went
382 * seriously wrong (and @error will be set too, in that case).
384 * After a %TRUE return, impl->primary will be TRUE if we were
388 g_application_impl_attempt_primary (GApplicationImpl *impl,
389 GCancellable *cancellable,
392 static const GDBusInterfaceVTable vtable = {
393 g_application_impl_method_call,
394 g_application_impl_get_property,
395 NULL, /* set_property */
398 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
399 GBusNameOwnerFlags name_owner_flags;
400 GApplicationFlags app_flags;
403 GError *local_error = NULL;
405 if (org_gtk_Application == NULL)
407 GError *my_error = NULL;
410 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &my_error);
411 if G_UNLIKELY (info == NULL)
412 g_error ("%s", my_error->message);
413 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
414 g_assert (org_gtk_Application != NULL);
415 g_dbus_interface_info_ref (org_gtk_Application);
416 g_dbus_node_info_unref (info);
418 info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &my_error);
419 if G_UNLIKELY (info == NULL)
420 g_error ("%s", my_error->message);
421 org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
422 g_assert (org_freedesktop_Application != NULL);
423 g_dbus_interface_info_ref (org_freedesktop_Application);
424 g_dbus_node_info_unref (info);
427 /* We could possibly have been D-Bus activated as a result of incoming
428 * requests on either the application or actiongroup interfaces.
429 * Because of how GDBus dispatches messages, we need to ensure that
430 * both of those things are registered before we attempt to request
433 * The action group need not be populated yet, as long as it happens
434 * before we return to the mainloop. The reason for that is because
435 * GDBus does the check to make sure the object exists from the worker
436 * thread but doesn't actually dispatch the action invocation until we
437 * hit the mainloop in this thread. There is also no danger of
438 * receiving 'activate' or 'open' signals until after 'startup' runs,
439 * for the same reason.
441 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
442 org_gtk_Application, &vtable, impl, NULL, error);
444 if (impl->object_id == 0)
447 impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
448 org_freedesktop_Application, &vtable, impl, NULL, error);
450 if (impl->fdo_object_id == 0)
453 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
454 impl->exported_actions, error);
456 if (impl->actions_id == 0)
459 impl->registered = TRUE;
460 if (!app_class->dbus_register (impl->app,
465 g_return_val_if_fail (local_error != NULL, FALSE);
466 g_propagate_error (error, g_steal_pointer (&local_error));
470 g_return_val_if_fail (local_error == NULL, FALSE);
472 if (impl->bus_name == NULL)
474 /* If this is a non-unique application then it is sufficient to
475 * have our object paths registered. We can return now.
477 * Note: non-unique applications always act as primary-instance.
479 impl->primary = TRUE;
483 /* If this is a unique application then we need to attempt to own
484 * the well-known name and fall back to remote mode (!is_primary)
485 * in the case that we can't do that.
487 name_owner_flags = G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE;
488 app_flags = g_application_get_flags (impl->app);
490 if (app_flags & G_APPLICATION_ALLOW_REPLACEMENT)
492 impl->name_lost_signal = g_dbus_connection_signal_subscribe (impl->session_bus,
493 "org.freedesktop.DBus",
494 "org.freedesktop.DBus",
496 "/org/freedesktop/DBus",
498 G_DBUS_SIGNAL_FLAGS_NONE,
503 name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
505 if (app_flags & G_APPLICATION_REPLACE)
506 name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
508 reply = g_dbus_connection_call_sync (impl->session_bus,
509 "org.freedesktop.DBus",
510 "/org/freedesktop/DBus",
511 "org.freedesktop.DBus",
513 g_variant_new ("(su)", impl->bus_name, name_owner_flags),
514 G_VARIANT_TYPE ("(u)"),
515 0, -1, cancellable, error);
520 g_variant_get (reply, "(u)", &rval);
521 g_variant_unref (reply);
523 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
524 impl->primary = (rval != 3);
526 if (!impl->primary && impl->name_lost_signal)
528 g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
529 impl->name_lost_signal = 0;
535 /* Stop doing the things that the primary instance does.
537 * This should be called if attempting to become the primary instance
538 * failed (in order to clean up any partial success) and should also
539 * be called when freeing the GApplication.
541 * It is safe to call this multiple times.
544 g_application_impl_stop_primary (GApplicationImpl *impl)
546 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
548 if (impl->registered)
550 app_class->dbus_unregister (impl->app,
553 impl->registered = FALSE;
558 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
562 if (impl->fdo_object_id)
564 g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
565 impl->fdo_object_id = 0;
568 if (impl->actions_id)
570 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
571 impl->actions_id = 0;
574 if (impl->name_lost_signal)
576 g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
577 impl->name_lost_signal = 0;
580 if (impl->primary && impl->bus_name)
582 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
583 "/org/freedesktop/DBus", "org.freedesktop.DBus",
584 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
585 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
586 impl->primary = FALSE;
591 g_application_impl_set_busy_state (GApplicationImpl *impl,
594 if (impl->busy != busy)
597 send_property_change (impl);
602 g_application_impl_destroy (GApplicationImpl *impl)
604 g_application_impl_stop_primary (impl);
606 if (impl->session_bus)
607 g_object_unref (impl->session_bus);
609 g_free (impl->object_path);
611 g_slice_free (GApplicationImpl, impl);
615 g_application_impl_register (GApplication *application,
617 GApplicationFlags flags,
618 GActionGroup *exported_actions,
619 GRemoteActionGroup **remote_actions,
620 GCancellable *cancellable,
623 GDBusActionGroup *actions;
624 GApplicationImpl *impl;
626 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
628 impl = g_slice_new0 (GApplicationImpl);
630 impl->app = application;
631 impl->exported_actions = exported_actions;
633 /* non-unique applications do not attempt to acquire a bus name */
634 if (~flags & G_APPLICATION_NON_UNIQUE)
635 impl->bus_name = appid;
637 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
639 if (impl->session_bus == NULL)
641 /* If we can't connect to the session bus, proceed as a normal
642 * non-unique application.
644 *remote_actions = NULL;
648 impl->object_path = application_path_from_appid (appid);
650 /* Only try to be the primary instance if
651 * G_APPLICATION_IS_LAUNCHER was not specified.
653 if (~flags & G_APPLICATION_IS_LAUNCHER)
655 if (!g_application_impl_attempt_primary (impl, cancellable, error))
657 g_application_impl_destroy (impl);
664 /* We didn't make it. Drop our service-side stuff. */
665 g_application_impl_stop_primary (impl);
667 if (flags & G_APPLICATION_IS_SERVICE)
669 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
670 "Unable to acquire bus name '%s'", appid);
671 g_application_impl_destroy (impl);
677 /* We are non-primary. Try to get the primary's list of actions.
678 * This also serves as a mechanism to ensure that the primary exists
679 * (ie: D-Bus service files installed correctly, etc).
681 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
682 if (!g_dbus_action_group_sync (actions, cancellable, error))
684 /* The primary appears not to exist. Fail the registration. */
685 g_application_impl_destroy (impl);
686 g_object_unref (actions);
691 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
697 g_application_impl_activate (GApplicationImpl *impl,
698 GVariant *platform_data)
700 g_dbus_connection_call (impl->session_bus,
703 "org.gtk.Application",
705 g_variant_new ("(@a{sv})", platform_data),
706 NULL, 0, -1, NULL, NULL, NULL);
710 g_application_impl_open (GApplicationImpl *impl,
714 GVariant *platform_data)
716 GVariantBuilder builder;
719 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
720 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
721 for (i = 0; i < n_files; i++)
723 gchar *uri = g_file_get_uri (files[i]);
724 g_variant_builder_add (&builder, "s", uri);
727 g_variant_builder_close (&builder);
728 g_variant_builder_add (&builder, "s", hint);
729 g_variant_builder_add_value (&builder, platform_data);
731 g_dbus_connection_call (impl->session_bus,
734 "org.gtk.Application",
736 g_variant_builder_end (&builder),
737 NULL, 0, -1, NULL, NULL, NULL);
741 g_application_impl_cmdline_method_call (GDBusConnection *connection,
743 const gchar *object_path,
744 const gchar *interface_name,
745 const gchar *method_name,
746 GVariant *parameters,
747 GDBusMethodInvocation *invocation,
750 const gchar *message;
752 g_variant_get_child (parameters, 0, "&s", &message);
754 if (strcmp (method_name, "Print") == 0)
755 g_print ("%s", message);
756 else if (strcmp (method_name, "PrintError") == 0)
757 g_printerr ("%s", message);
759 g_assert_not_reached ();
761 g_dbus_method_invocation_return_value (invocation, NULL);
771 g_application_impl_cmdline_done (GObject *source,
772 GAsyncResult *result,
775 CommandLineData *data = user_data;
776 GError *error = NULL;
780 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
782 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
788 g_variant_get (reply, "(i)", &data->status);
789 g_variant_unref (reply);
794 g_printerr ("%s\n", error->message);
795 g_error_free (error);
799 g_main_loop_quit (data->loop);
803 g_application_impl_command_line (GApplicationImpl *impl,
804 const gchar * const *arguments,
805 GVariant *platform_data)
807 static const GDBusInterfaceVTable vtable = {
808 g_application_impl_cmdline_method_call, NULL, NULL, { 0 }
810 const gchar *object_path = "/org/gtk/Application/CommandLine";
811 GMainContext *context;
812 CommandLineData data;
813 guint object_id G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
815 context = g_main_context_new ();
816 data.loop = g_main_loop_new (context, FALSE);
817 g_main_context_push_thread_default (context);
819 if (org_gtk_private_CommandLine == NULL)
821 GError *error = NULL;
824 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
825 if G_UNLIKELY (info == NULL)
826 g_error ("%s", error->message);
827 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
828 g_assert (org_gtk_private_CommandLine != NULL);
829 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
830 g_dbus_node_info_unref (info);
833 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
834 org_gtk_private_CommandLine,
835 &vtable, &data, NULL, NULL);
836 /* In theory we should try other paths... */
837 g_assert (object_id != 0);
841 GError *error = NULL;
842 GUnixFDList *fd_list;
844 /* send along the stdin in case
845 * g_application_command_line_get_stdin_data() is called
847 fd_list = g_unix_fd_list_new ();
848 g_unix_fd_list_append (fd_list, 0, &error);
849 g_assert_no_error (error);
851 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
852 "org.gtk.Application", "CommandLine",
853 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
854 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
855 g_application_impl_cmdline_done, &data);
856 g_object_unref (fd_list);
859 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
860 "org.gtk.Application", "CommandLine",
861 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
862 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
863 g_application_impl_cmdline_done, &data);
866 g_main_loop_run (data.loop);
868 g_main_context_pop_thread_default (context);
869 g_main_context_unref (context);
870 g_main_loop_unref (data.loop);
876 g_application_impl_flush (GApplicationImpl *impl)
878 if (impl->session_bus)
879 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
883 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
885 return impl->session_bus;
889 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
891 return impl->object_path;
894 /* GDBusCommandLine implementation {{{1 */
896 typedef GApplicationCommandLineClass GDBusCommandLineClass;
897 static GType g_dbus_command_line_get_type (void);
900 GApplicationCommandLine parent_instance;
901 GDBusMethodInvocation *invocation;
903 GDBusConnection *connection;
904 const gchar *bus_name;
905 const gchar *object_path;
909 G_DEFINE_TYPE (GDBusCommandLine,
911 G_TYPE_APPLICATION_COMMAND_LINE)
914 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
915 const gchar *message)
917 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
919 g_dbus_connection_call (gdbcl->connection,
922 "org.gtk.private.CommandLine", "Print",
923 g_variant_new ("(s)", message),
924 NULL, 0, -1, NULL, NULL, NULL);
928 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
929 const gchar *message)
931 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
933 g_dbus_connection_call (gdbcl->connection,
936 "org.gtk.private.CommandLine", "PrintError",
937 g_variant_new ("(s)", message),
938 NULL, 0, -1, NULL, NULL, NULL);
941 static GInputStream *
942 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
945 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
946 GInputStream *result = NULL;
947 GDBusMessage *message;
948 GUnixFDList *fd_list;
950 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
951 fd_list = g_dbus_message_get_unix_fd_list (message);
953 if (fd_list && g_unix_fd_list_get_length (fd_list))
957 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
958 result = g_unix_input_stream_new (fds[0], TRUE);
959 for (i = 1; i < n_fds; i++)
960 (void) g_close (fds[i], NULL);
971 g_dbus_command_line_finalize (GObject *object)
973 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
975 g_object_unref (gdbcl->invocation);
977 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
982 g_dbus_command_line_done (GApplicationCommandLine *cmdline)
984 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
987 status = g_application_command_line_get_exit_status (cmdline);
989 g_dbus_method_invocation_return_value (gdbcl->invocation,
990 g_variant_new ("(i)", status));
992 G_APPLICATION_COMMAND_LINE_CLASS (g_dbus_command_line_parent_class)->done (cmdline);
996 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
1001 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
1003 GObjectClass *object_class = G_OBJECT_CLASS (class);
1005 object_class->finalize = g_dbus_command_line_finalize;
1006 class->printerr_literal = g_dbus_command_line_printerr_literal;
1007 class->print_literal = g_dbus_command_line_print_literal;
1008 class->get_stdin = g_dbus_command_line_get_stdin;
1009 class->done = g_dbus_command_line_done;
1012 static GApplicationCommandLine *
1013 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
1015 GDBusCommandLine *gdbcl;
1017 GVariant *arguments, *platform_data;
1019 args = g_dbus_method_invocation_get_parameters (invocation);
1021 arguments = g_variant_get_child_value (args, 1);
1022 platform_data = g_variant_get_child_value (args, 2);
1023 gdbcl = g_object_new (g_dbus_command_line_get_type (),
1024 "arguments", arguments,
1025 "platform-data", platform_data,
1027 g_variant_unref (arguments);
1028 g_variant_unref (platform_data);
1030 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
1031 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
1032 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
1033 gdbcl->invocation = g_object_ref (invocation);
1035 return G_APPLICATION_COMMAND_LINE (gdbcl);
1040 /* vim:set foldmethod=marker: */