Coverage Report

Created: 2025-07-01 07:09

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