+static void
+g_application_impl_cmdline_method_call (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ const gchar *message;
+
+ g_variant_get_child (parameters, 0, "&s", &message);
+
+ if (strcmp (method_name, "Print") == 0)
+ g_print ("%s", message);
+ else if (strcmp (method_name, "PrintError") == 0)
+ g_printerr ("%s", message);
+ else
+ g_assert_not_reached ();
+
+ g_dbus_method_invocation_return_value (invocation, NULL);
+}
+
+typedef struct
+{
+ GMainLoop *loop;
+ int status;
+} CommandLineData;
+
+static void
+g_application_impl_cmdline_done (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ CommandLineData *data = user_data;
+ GError *error = NULL;
+ GVariant *reply;
+
+#ifdef G_OS_UNIX
+ reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
+#else
+ reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
+#endif
+
+
+ if (reply != NULL)
+ {
+ g_variant_get (reply, "(i)", &data->status);
+ g_variant_unref (reply);
+ }
+
+ else
+ {
+ g_printerr ("%s\n", error->message);
+ g_error_free (error);
+ data->status = 1;
+ }
+
+ g_main_loop_quit (data->loop);
+}
+
+int
+g_application_impl_command_line (GApplicationImpl *impl,
+ const gchar * const *arguments,
+ GVariant *platform_data)
+{
+ const static GDBusInterfaceVTable vtable = {
+ g_application_impl_cmdline_method_call
+ };
+ const gchar *object_path = "/org/gtk/Application/CommandLine";
+ GMainContext *context;
+ CommandLineData data;
+ guint object_id;
+
+ context = g_main_context_new ();
+ data.loop = g_main_loop_new (context, FALSE);
+ g_main_context_push_thread_default (context);
+
+ if (org_gtk_private_CommandLine == NULL)
+ {
+ GError *error = NULL;
+ GDBusNodeInfo *info;
+
+ info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
+ if G_UNLIKELY (info == NULL)
+ g_error ("%s", error->message);
+ org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
+ g_assert (org_gtk_private_CommandLine != NULL);
+ g_dbus_interface_info_ref (org_gtk_private_CommandLine);
+ g_dbus_node_info_unref (info);
+ }
+
+ object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
+ org_gtk_private_CommandLine,
+ &vtable, &data, NULL, NULL);
+ /* In theory we should try other paths... */
+ g_assert (object_id != 0);
+
+#ifdef G_OS_UNIX
+ {
+ GError *error = NULL;
+ GUnixFDList *fd_list;
+
+ /* send along the stdin in case
+ * g_application_command_line_get_stdin_data() is called
+ */
+ fd_list = g_unix_fd_list_new ();
+ g_unix_fd_list_append (fd_list, 0, &error);
+ g_assert_no_error (error);
+
+ g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
+ "org.gtk.Application", "CommandLine",
+ g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
+ G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
+ g_application_impl_cmdline_done, &data);
+ g_object_unref (fd_list);
+ }
+#else
+ g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
+ "org.gtk.Application", "CommandLine",
+ g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
+ G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
+ g_application_impl_cmdline_done, &data);
+#endif
+
+ g_main_loop_run (data.loop);
+
+ g_main_context_pop_thread_default (context);
+ g_main_context_unref (context);
+ g_main_loop_unref (data.loop);
+
+ return data.status;
+}
+