1 /*
2 * Copyright © 2010 Codethink Limited
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * Authors: Ryan Lortie <desrt@desrt.ca>
18 */
19
20 #include "config.h"
21
22 #include "gapplicationimpl.h"
23
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"
34
35 #include <string.h>
36 #include <stdio.h>
37
38 #include "gapplicationcommandline.h"
39 #include "gdbusmethodinvocation.h"
40
41 #ifdef G_OS_UNIX
42 #include "gunixinputstream.h"
43 #include "gunixfdlist.h"
44 #endif
45
46 /* DBus Interface definition {{{1 */
47
48 /* For documentation of these interfaces, see
49 * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI
50 */
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>";
71
72 static GDBusInterfaceInfo *org_gtk_Application;
73
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>";
91
92 static GDBusInterfaceInfo *org_freedesktop_Application;
93
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>";
105
106 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
107
108 /* GApplication implementation {{{1 */
109 struct _GApplicationImpl
110 {
111 GDBusConnection *session_bus;
112 GActionGroup *exported_actions;
113 const gchar *bus_name;
114 guint name_lost_signal;
115
116 gchar *object_path;
117 guint object_id;
118 guint fdo_object_id;
119 guint actions_id;
120
121 gboolean properties_live;
122 gboolean primary;
123 gboolean busy;
124 gboolean registered;
125 GApplication *app;
126 };
127
128
129 static GApplicationCommandLine *
130 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
131
132 static GVariant *
g_application_impl_get_property(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * property_name,GError ** error,gpointer user_data)133 g_application_impl_get_property (GDBusConnection *connection,
134 const gchar *sender,
135 const gchar *object_path,
136 const gchar *interface_name,
137 const gchar *property_name,
138 GError **error,
139 gpointer user_data)
140 {
141 GApplicationImpl *impl = user_data;
142
143 if (strcmp (property_name, "Busy") == 0)
144 return g_variant_new_boolean (impl->busy);
145
146 g_assert_not_reached ();
147
148 return NULL;
149 }
150
151 static void
send_property_change(GApplicationImpl * impl)152 send_property_change (GApplicationImpl *impl)
153 {
154 GVariantBuilder builder;
155
156 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
157 g_variant_builder_add (&builder,
158 "{sv}",
159 "Busy", g_variant_new_boolean (impl->busy));
160
161 g_dbus_connection_emit_signal (impl->session_bus,
162 NULL,
163 impl->object_path,
164 "org.freedesktop.DBus.Properties",
165 "PropertiesChanged",
166 g_variant_new ("(sa{sv}as)",
167 "org.gtk.Application",
168 &builder,
169 NULL),
170 NULL);
171 }
172
173 static void
g_application_impl_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)174 g_application_impl_method_call (GDBusConnection *connection,
175 const gchar *sender,
176 const gchar *object_path,
177 const gchar *interface_name,
178 const gchar *method_name,
179 GVariant *parameters,
180 GDBusMethodInvocation *invocation,
181 gpointer user_data)
182 {
183 GApplicationImpl *impl = user_data;
184 GApplicationClass *class;
185
186 class = G_APPLICATION_GET_CLASS (impl->app);
187
188 if (strcmp (method_name, "Activate") == 0)
189 {
190 GVariant *platform_data;
191
192 /* Completely the same for both freedesktop and gtk interfaces */
193
194 g_variant_get (parameters, "(@a{sv})", &platform_data);
195
196 class->before_emit (impl->app, platform_data);
197 g_signal_emit_by_name (impl->app, "activate");
198 class->after_emit (impl->app, platform_data);
199 g_variant_unref (platform_data);
200
201 g_dbus_method_invocation_return_value (invocation, NULL);
202 }
203
204 else if (strcmp (method_name, "Open") == 0)
205 {
206 GApplicationFlags flags;
207 GVariant *platform_data;
208 const gchar *hint;
209 GVariant *array;
210 GFile **files;
211 gint n, i;
212
213 flags = g_application_get_flags (impl->app);
214 if ((flags & G_APPLICATION_HANDLES_OPEN) == 0)
215 {
216 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files");
217 return;
218 }
219
220 /* freedesktop interface has no hint parameter */
221 if (g_str_equal (interface_name, "org.freedesktop.Application"))
222 {
223 g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data);
224 hint = "";
225 }
226 else
227 g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data);
228
229 n = g_variant_n_children (array);
230 files = g_new (GFile *, n + 1);
231
232 for (i = 0; i < n; i++)
233 {
234 const gchar *uri;
235
236 g_variant_get_child (array, i, "&s", &uri);
237 files[i] = g_file_new_for_uri (uri);
238 }
239 g_variant_unref (array);
240 files[n] = NULL;
241
242 class->before_emit (impl->app, platform_data);
243 g_signal_emit_by_name (impl->app, "open", files, n, hint);
244 class->after_emit (impl->app, platform_data);
245
246 g_variant_unref (platform_data);
247
248 for (i = 0; i < n; i++)
249 g_object_unref (files[i]);
250 g_free (files);
251
252 g_dbus_method_invocation_return_value (invocation, NULL);
253 }
254
255 else if (strcmp (method_name, "CommandLine") == 0)
256 {
257 GApplicationFlags flags;
258 GApplicationCommandLine *cmdline;
259 GVariant *platform_data;
260 int status;
261
262 flags = g_application_get_flags (impl->app);
263 if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0)
264 {
265 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED,
266 "Application does not handle command line arguments");
267 return;
268 }
269
270 /* Only on the GtkApplication interface */
271
272 cmdline = g_dbus_command_line_new (invocation);
273 platform_data = g_variant_get_child_value (parameters, 2);
274 class->before_emit (impl->app, platform_data);
275 g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
276 g_application_command_line_set_exit_status (cmdline, status);
277 class->after_emit (impl->app, platform_data);
278 g_variant_unref (platform_data);
279 g_object_unref (cmdline);
280 }
281 else if (g_str_equal (method_name, "ActivateAction"))
282 {
283 GVariant *parameter = NULL;
284 GVariant *platform_data;
285 GVariantIter *iter;
286 const gchar *name;
287
288 /* Only on the freedesktop interface */
289
290 g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data);
291 g_variant_iter_next (iter, "v", ¶meter);
292 g_variant_iter_free (iter);
293
294 class->before_emit (impl->app, platform_data);
295 g_action_group_activate_action (impl->exported_actions, name, parameter);
296 class->after_emit (impl->app, platform_data);
297
298 if (parameter)
299 g_variant_unref (parameter);
300
301 g_variant_unref (platform_data);
302
303 g_dbus_method_invocation_return_value (invocation, NULL);
304 }
305 else
306 g_assert_not_reached ();
307 }
308
309 static gchar *
application_path_from_appid(const gchar * appid)310 application_path_from_appid (const gchar *appid)
311 {
312 gchar *appid_path, *iter;
313
314 if (appid == NULL)
315 /* this is a private implementation detail */
316 return g_strdup ("/org/gtk/Application/anonymous");
317
318 appid_path = g_strconcat ("/", appid, NULL);
319 for (iter = appid_path; *iter; iter++)
320 {
321 if (*iter == '.')
322 *iter = '/';
323
324 if (*iter == '-')
325 *iter = '_';
326 }
327
328 return appid_path;
329 }
330
331 static void g_application_impl_stop_primary (GApplicationImpl *impl);
332
333 static void
name_lost(GDBusConnection * bus,const char * sender_name,const char * object_path,const char * interface_name,const char * signal_name,GVariant * parameters,gpointer user_data)334 name_lost (GDBusConnection *bus,
335 const char *sender_name,
336 const char *object_path,
337 const char *interface_name,
338 const char *signal_name,
339 GVariant *parameters,
340 gpointer user_data)
341 {
342 GApplicationImpl *impl = user_data;
343 gboolean handled;
344
345 impl->primary = FALSE;
346 g_application_impl_stop_primary (impl);
347 g_signal_emit_by_name (impl->app, "name-lost", &handled);
348 }
349
350 /* Attempt to become the primary instance.
351 *
352 * Returns %TRUE if everything went OK, regardless of if we became the
353 * primary instance or not. %FALSE is reserved for when something went
354 * seriously wrong (and @error will be set too, in that case).
355 *
356 * After a %TRUE return, impl->primary will be TRUE if we were
357 * successful.
358 */
359 static gboolean
g_application_impl_attempt_primary(GApplicationImpl * impl,GCancellable * cancellable,GError ** error)360 g_application_impl_attempt_primary (GApplicationImpl *impl,
361 GCancellable *cancellable,
362 GError **error)
363 {
364 const static GDBusInterfaceVTable vtable = {
365 g_application_impl_method_call,
366 g_application_impl_get_property,
367 NULL /* set_property */
368 };
369 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
370 GBusNameOwnerFlags name_owner_flags;
371 GApplicationFlags app_flags;
372 GVariant *reply;
373 guint32 rval;
374
375 if (org_gtk_Application == NULL)
376 {
377 GError *error = NULL;
378 GDBusNodeInfo *info;
379
380 info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
381 if G_UNLIKELY (info == NULL)
382 g_error ("%s", error->message);
383 org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
384 g_assert (org_gtk_Application != NULL);
385 g_dbus_interface_info_ref (org_gtk_Application);
386 g_dbus_node_info_unref (info);
387
388 info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error);
389 if G_UNLIKELY (info == NULL)
390 g_error ("%s", error->message);
391 org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application");
392 g_assert (org_freedesktop_Application != NULL);
393 g_dbus_interface_info_ref (org_freedesktop_Application);
394 g_dbus_node_info_unref (info);
395 }
396
397 /* We could possibly have been D-Bus activated as a result of incoming
398 * requests on either the application or actiongroup interfaces.
399 * Because of how GDBus dispatches messages, we need to ensure that
400 * both of those things are registered before we attempt to request
401 * our name.
402 *
403 * The action group need not be populated yet, as long as it happens
404 * before we return to the mainloop. The reason for that is because
405 * GDBus does the check to make sure the object exists from the worker
406 * thread but doesn't actually dispatch the action invocation until we
407 * hit the mainloop in this thread. There is also no danger of
408 * receiving 'activate' or 'open' signals until after 'startup' runs,
409 * for the same reason.
410 */
411 impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
412 org_gtk_Application, &vtable, impl, NULL, error);
413
414 if (impl->object_id == 0)
415 return FALSE;
416
417 impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
418 org_freedesktop_Application, &vtable, impl, NULL, error);
419
420 if (impl->fdo_object_id == 0)
421 return FALSE;
422
423 impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
424 impl->exported_actions, error);
425
426 if (impl->actions_id == 0)
427 return FALSE;
428
429 impl->registered = TRUE;
430 if (!app_class->dbus_register (impl->app,
431 impl->session_bus,
432 impl->object_path,
433 error))
434 return FALSE;
435
436 if (impl->bus_name == NULL)
437 {
438 /* If this is a non-unique application then it is sufficient to
439 * have our object paths registered. We can return now.
440 *
441 * Note: non-unique applications always act as primary-instance.
442 */
443 impl->primary = TRUE;
444 return TRUE;
445 }
446
447 /* If this is a unique application then we need to attempt to own
448 * the well-known name and fall back to remote mode (!is_primary)
449 * in the case that we can't do that.
450 */
451 name_owner_flags = G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE;
452 app_flags = g_application_get_flags (impl->app);
453
454 if (app_flags & G_APPLICATION_ALLOW_REPLACEMENT)
455 {
456 impl->name_lost_signal = g_dbus_connection_signal_subscribe (impl->session_bus,
457 "org.freedesktop.DBus",
458 "org.freedesktop.DBus",
459 "NameLost",
460 "/org/freedesktop/DBus",
461 impl->bus_name,
462 G_DBUS_SIGNAL_FLAGS_NONE,
463 name_lost,
464 impl,
465 NULL);
466
467 name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
468 }
469 if (app_flags & G_APPLICATION_REPLACE)
470 name_owner_flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
471
472 reply = g_dbus_connection_call_sync (impl->session_bus,
473 "org.freedesktop.DBus",
474 "/org/freedesktop/DBus",
475 "org.freedesktop.DBus",
476 "RequestName",
477 g_variant_new ("(su)", impl->bus_name, name_owner_flags),
478 G_VARIANT_TYPE ("(u)"),
479 0, -1, cancellable, error);
480
481 if (reply == NULL)
482 return FALSE;
483
484 g_variant_get (reply, "(u)", &rval);
485 g_variant_unref (reply);
486
487 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
488 impl->primary = (rval != 3);
489
490 if (!impl->primary && impl->name_lost_signal)
491 {
492 g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
493 impl->name_lost_signal = 0;
494 }
495
496 return TRUE;
497 }
498
499 /* Stop doing the things that the primary instance does.
500 *
501 * This should be called if attempting to become the primary instance
502 * failed (in order to clean up any partial success) and should also
503 * be called when freeing the GApplication.
504 *
505 * It is safe to call this multiple times.
506 */
507 static void
g_application_impl_stop_primary(GApplicationImpl * impl)508 g_application_impl_stop_primary (GApplicationImpl *impl)
509 {
510 GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
511
512 if (impl->registered)
513 {
514 app_class->dbus_unregister (impl->app,
515 impl->session_bus,
516 impl->object_path);
517 impl->registered = FALSE;
518 }
519
520 if (impl->object_id)
521 {
522 g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
523 impl->object_id = 0;
524 }
525
526 if (impl->fdo_object_id)
527 {
528 g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id);
529 impl->fdo_object_id = 0;
530 }
531
532 if (impl->actions_id)
533 {
534 g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
535 impl->actions_id = 0;
536 }
537
538 if (impl->name_lost_signal)
539 {
540 g_dbus_connection_signal_unsubscribe (impl->session_bus, impl->name_lost_signal);
541 impl->name_lost_signal = 0;
542 }
543
544 if (impl->primary && impl->bus_name)
545 {
546 g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
547 "/org/freedesktop/DBus", "org.freedesktop.DBus",
548 "ReleaseName", g_variant_new ("(s)", impl->bus_name),
549 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
550 impl->primary = FALSE;
551 }
552 }
553
554 void
g_application_impl_set_busy_state(GApplicationImpl * impl,gboolean busy)555 g_application_impl_set_busy_state (GApplicationImpl *impl,
556 gboolean busy)
557 {
558 if (impl->busy != busy)
559 {
560 impl->busy = busy;
561 send_property_change (impl);
562 }
563 }
564
565 void
g_application_impl_destroy(GApplicationImpl * impl)566 g_application_impl_destroy (GApplicationImpl *impl)
567 {
568 g_application_impl_stop_primary (impl);
569
570 if (impl->session_bus)
571 g_object_unref (impl->session_bus);
572
573 g_free (impl->object_path);
574
575 g_slice_free (GApplicationImpl, impl);
576 }
577
578 GApplicationImpl *
g_application_impl_register(GApplication * application,const gchar * appid,GApplicationFlags flags,GActionGroup * exported_actions,GRemoteActionGroup ** remote_actions,GCancellable * cancellable,GError ** error)579 g_application_impl_register (GApplication *application,
580 const gchar *appid,
581 GApplicationFlags flags,
582 GActionGroup *exported_actions,
583 GRemoteActionGroup **remote_actions,
584 GCancellable *cancellable,
585 GError **error)
586 {
587 GDBusActionGroup *actions;
588 GApplicationImpl *impl;
589
590 g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
591
592 impl = g_slice_new0 (GApplicationImpl);
593
594 impl->app = application;
595 impl->exported_actions = exported_actions;
596
597 /* non-unique applications do not attempt to acquire a bus name */
598 if (~flags & G_APPLICATION_NON_UNIQUE)
599 impl->bus_name = appid;
600
601 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
602
603 if (impl->session_bus == NULL)
604 {
605 /* If we can't connect to the session bus, proceed as a normal
606 * non-unique application.
607 */
608 *remote_actions = NULL;
609 return impl;
610 }
611
612 impl->object_path = application_path_from_appid (appid);
613
614 /* Only try to be the primary instance if
615 * G_APPLICATION_IS_LAUNCHER was not specified.
616 */
617 if (~flags & G_APPLICATION_IS_LAUNCHER)
618 {
619 if (!g_application_impl_attempt_primary (impl, cancellable, error))
620 {
621 g_application_impl_destroy (impl);
622 return NULL;
623 }
624
625 if (impl->primary)
626 return impl;
627
628 /* We didn't make it. Drop our service-side stuff. */
629 g_application_impl_stop_primary (impl);
630
631 if (flags & G_APPLICATION_IS_SERVICE)
632 {
633 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
634 "Unable to acquire bus name '%s'", appid);
635 g_application_impl_destroy (impl);
636
637 return NULL;
638 }
639 }
640
641 /* We are non-primary. Try to get the primary's list of actions.
642 * This also serves as a mechanism to ensure that the primary exists
643 * (ie: DBus service files installed correctly, etc).
644 */
645 actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
646 if (!g_dbus_action_group_sync (actions, cancellable, error))
647 {
648 /* The primary appears not to exist. Fail the registration. */
649 g_application_impl_destroy (impl);
650 g_object_unref (actions);
651
652 return NULL;
653 }
654
655 *remote_actions = G_REMOTE_ACTION_GROUP (actions);
656
657 return impl;
658 }
659
660 void
g_application_impl_activate(GApplicationImpl * impl,GVariant * platform_data)661 g_application_impl_activate (GApplicationImpl *impl,
662 GVariant *platform_data)
663 {
664 g_dbus_connection_call (impl->session_bus,
665 impl->bus_name,
666 impl->object_path,
667 "org.gtk.Application",
668 "Activate",
669 g_variant_new ("(@a{sv})", platform_data),
670 NULL, 0, -1, NULL, NULL, NULL);
671 }
672
673 void
g_application_impl_open(GApplicationImpl * impl,GFile ** files,gint n_files,const gchar * hint,GVariant * platform_data)674 g_application_impl_open (GApplicationImpl *impl,
675 GFile **files,
676 gint n_files,
677 const gchar *hint,
678 GVariant *platform_data)
679 {
680 GVariantBuilder builder;
681 gint i;
682
683 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
684 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
685 for (i = 0; i < n_files; i++)
686 {
687 gchar *uri = g_file_get_uri (files[i]);
688 g_variant_builder_add (&builder, "s", uri);
689 g_free (uri);
690 }
691 g_variant_builder_close (&builder);
692 g_variant_builder_add (&builder, "s", hint);
693 g_variant_builder_add_value (&builder, platform_data);
694
695 g_dbus_connection_call (impl->session_bus,
696 impl->bus_name,
697 impl->object_path,
698 "org.gtk.Application",
699 "Open",
700 g_variant_builder_end (&builder),
701 NULL, 0, -1, NULL, NULL, NULL);
702 }
703
704 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)705 g_application_impl_cmdline_method_call (GDBusConnection *connection,
706 const gchar *sender,
707 const gchar *object_path,
708 const gchar *interface_name,
709 const gchar *method_name,
710 GVariant *parameters,
711 GDBusMethodInvocation *invocation,
712 gpointer user_data)
713 {
714 const gchar *message;
715
716 g_variant_get_child (parameters, 0, "&s", &message);
717
718 if (strcmp (method_name, "Print") == 0)
719 g_print ("%s", message);
720 else if (strcmp (method_name, "PrintError") == 0)
721 g_printerr ("%s", message);
722 else
723 g_assert_not_reached ();
724
725 g_dbus_method_invocation_return_value (invocation, NULL);
726 }
727
728 typedef struct
729 {
730 GMainLoop *loop;
731 int status;
732 } CommandLineData;
733
734 static void
g_application_impl_cmdline_done(GObject * source,GAsyncResult * result,gpointer user_data)735 g_application_impl_cmdline_done (GObject *source,
736 GAsyncResult *result,
737 gpointer user_data)
738 {
739 CommandLineData *data = user_data;
740 GError *error = NULL;
741 GVariant *reply;
742
743 #ifdef G_OS_UNIX
744 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error);
745 #else
746 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
747 #endif
748
749
750 if (reply != NULL)
751 {
752 g_variant_get (reply, "(i)", &data->status);
753 g_variant_unref (reply);
754 }
755
756 else
757 {
758 g_printerr ("%s\n", error->message);
759 g_error_free (error);
760 data->status = 1;
761 }
762
763 g_main_loop_quit (data->loop);
764 }
765
766 int
g_application_impl_command_line(GApplicationImpl * impl,const gchar * const * arguments,GVariant * platform_data)767 g_application_impl_command_line (GApplicationImpl *impl,
768 const gchar * const *arguments,
769 GVariant *platform_data)
770 {
771 const static GDBusInterfaceVTable vtable = {
772 g_application_impl_cmdline_method_call
773 };
774 const gchar *object_path = "/org/gtk/Application/CommandLine";
775 GMainContext *context;
776 CommandLineData data;
777 guint object_id G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
778
779 context = g_main_context_new ();
780 data.loop = g_main_loop_new (context, FALSE);
781 g_main_context_push_thread_default (context);
782
783 if (org_gtk_private_CommandLine == NULL)
784 {
785 GError *error = NULL;
786 GDBusNodeInfo *info;
787
788 info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
789 if G_UNLIKELY (info == NULL)
790 g_error ("%s", error->message);
791 org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
792 g_assert (org_gtk_private_CommandLine != NULL);
793 g_dbus_interface_info_ref (org_gtk_private_CommandLine);
794 g_dbus_node_info_unref (info);
795 }
796
797 object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
798 org_gtk_private_CommandLine,
799 &vtable, &data, NULL, NULL);
800 /* In theory we should try other paths... */
801 g_assert (object_id != 0);
802
803 #ifdef G_OS_UNIX
804 {
805 GError *error = NULL;
806 GUnixFDList *fd_list;
807
808 /* send along the stdin in case
809 * g_application_command_line_get_stdin_data() is called
810 */
811 fd_list = g_unix_fd_list_new ();
812 g_unix_fd_list_append (fd_list, 0, &error);
813 g_assert_no_error (error);
814
815 g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path,
816 "org.gtk.Application", "CommandLine",
817 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
818 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL,
819 g_application_impl_cmdline_done, &data);
820 g_object_unref (fd_list);
821 }
822 #else
823 g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path,
824 "org.gtk.Application", "CommandLine",
825 g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data),
826 G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
827 g_application_impl_cmdline_done, &data);
828 #endif
829
830 g_main_loop_run (data.loop);
831
832 g_main_context_pop_thread_default (context);
833 g_main_context_unref (context);
834 g_main_loop_unref (data.loop);
835
836 return data.status;
837 }
838
839 void
g_application_impl_flush(GApplicationImpl * impl)840 g_application_impl_flush (GApplicationImpl *impl)
841 {
842 if (impl->session_bus)
843 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
844 }
845
846 GDBusConnection *
g_application_impl_get_dbus_connection(GApplicationImpl * impl)847 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
848 {
849 return impl->session_bus;
850 }
851
852 const gchar *
g_application_impl_get_dbus_object_path(GApplicationImpl * impl)853 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
854 {
855 return impl->object_path;
856 }
857
858 /* GDBusCommandLine implementation {{{1 */
859
860 typedef GApplicationCommandLineClass GDBusCommandLineClass;
861 static GType g_dbus_command_line_get_type (void);
862 typedef struct
863 {
864 GApplicationCommandLine parent_instance;
865 GDBusMethodInvocation *invocation;
866
867 GDBusConnection *connection;
868 const gchar *bus_name;
869 const gchar *object_path;
870 } GDBusCommandLine;
871
872
G_DEFINE_TYPE(GDBusCommandLine,g_dbus_command_line,G_TYPE_APPLICATION_COMMAND_LINE)873 G_DEFINE_TYPE (GDBusCommandLine,
874 g_dbus_command_line,
875 G_TYPE_APPLICATION_COMMAND_LINE)
876
877 static void
878 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
879 const gchar *message)
880 {
881 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
882
883 g_dbus_connection_call (gdbcl->connection,
884 gdbcl->bus_name,
885 gdbcl->object_path,
886 "org.gtk.private.CommandLine", "Print",
887 g_variant_new ("(s)", message),
888 NULL, 0, -1, NULL, NULL, NULL);
889 }
890
891 static void
g_dbus_command_line_printerr_literal(GApplicationCommandLine * cmdline,const gchar * message)892 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
893 const gchar *message)
894 {
895 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
896
897 g_dbus_connection_call (gdbcl->connection,
898 gdbcl->bus_name,
899 gdbcl->object_path,
900 "org.gtk.private.CommandLine", "PrintError",
901 g_variant_new ("(s)", message),
902 NULL, 0, -1, NULL, NULL, NULL);
903 }
904
905 static GInputStream *
g_dbus_command_line_get_stdin(GApplicationCommandLine * cmdline)906 g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline)
907 {
908 #ifdef G_OS_UNIX
909 GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
910 GInputStream *result = NULL;
911 GDBusMessage *message;
912 GUnixFDList *fd_list;
913
914 message = g_dbus_method_invocation_get_message (gdbcl->invocation);
915 fd_list = g_dbus_message_get_unix_fd_list (message);
916
917 if (fd_list && g_unix_fd_list_get_length (fd_list))
918 {
919 gint *fds, n_fds, i;
920
921 fds = g_unix_fd_list_steal_fds (fd_list, &n_fds);
922 result = g_unix_input_stream_new (fds[0], TRUE);
923 for (i = 1; i < n_fds; i++)
924 (void) g_close (fds[i], NULL);
925 g_free (fds);
926 }
927
928 return result;
929 #else
930 return NULL;
931 #endif
932 }
933
934 static void
g_dbus_command_line_finalize(GObject * object)935 g_dbus_command_line_finalize (GObject *object)
936 {
937 GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
938 GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
939 gint status;
940
941 status = g_application_command_line_get_exit_status (cmdline);
942
943 g_dbus_method_invocation_return_value (gdbcl->invocation,
944 g_variant_new ("(i)", status));
945 g_object_unref (gdbcl->invocation);
946
947 G_OBJECT_CLASS (g_dbus_command_line_parent_class)
948 ->finalize (object);
949 }
950
951 static void
g_dbus_command_line_init(GDBusCommandLine * gdbcl)952 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
953 {
954 }
955
956 static void
g_dbus_command_line_class_init(GApplicationCommandLineClass * class)957 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
958 {
959 GObjectClass *object_class = G_OBJECT_CLASS (class);
960
961 object_class->finalize = g_dbus_command_line_finalize;
962 class->printerr_literal = g_dbus_command_line_printerr_literal;
963 class->print_literal = g_dbus_command_line_print_literal;
964 class->get_stdin = g_dbus_command_line_get_stdin;
965 }
966
967 static GApplicationCommandLine *
g_dbus_command_line_new(GDBusMethodInvocation * invocation)968 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
969 {
970 GDBusCommandLine *gdbcl;
971 GVariant *args;
972 GVariant *arguments, *platform_data;
973
974 args = g_dbus_method_invocation_get_parameters (invocation);
975
976 arguments = g_variant_get_child_value (args, 1);
977 platform_data = g_variant_get_child_value (args, 2);
978 gdbcl = g_object_new (g_dbus_command_line_get_type (),
979 "arguments", arguments,
980 "platform-data", platform_data,
981 NULL);
982 g_variant_unref (arguments);
983 g_variant_unref (platform_data);
984
985 gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
986 gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
987 g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
988 gdbcl->invocation = g_object_ref (invocation);
989
990 return G_APPLICATION_COMMAND_LINE (gdbcl);
991 }
992
993 /* Epilogue {{{1 */
994
995 /* vim:set foldmethod=marker: */
996