Rename exporter APIs
[platform/upstream/glib.git] / gio / gapplicationimpl-dbus.c
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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "gapplicationimpl.h"
23
24 #include "gactiongroup.h"
25 #include "gactiongroupexporter.h"
26 #include "gdbusactiongroup.h"
27 #include "gapplication.h"
28 #include "gfile.h"
29 #include "gdbusconnection.h"
30 #include "gdbusintrospection.h"
31 #include "gdbuserror.h"
32 #include "gmenuexporter.h"
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
39
40 /* DBus Interface definition {{{1 */
41 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
42
43 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
44 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
45
46 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
47 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
48
49 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
50 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
51 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
52
53 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
54 static const GDBusArgInfo *activate_out[] = { NULL };
55
56 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
57 static const GDBusArgInfo *open_out[] = { NULL };
58
59 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
60 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
61
62 static const GDBusMethodInfo activate_method = {
63   -1, (gchar *) "Activate",
64   (GDBusArgInfo **) activate_in,
65   (GDBusArgInfo **) activate_out
66 };
67
68 static const GDBusMethodInfo open_method = {
69   -1, (gchar *) "Open",
70   (GDBusArgInfo **) open_in,
71   (GDBusArgInfo **) open_out
72 };
73
74 static const GDBusMethodInfo command_line_method = {
75   -1, (gchar *) "CommandLine",
76   (GDBusArgInfo **) cmdline_in,
77   (GDBusArgInfo **) cmdline_out
78 };
79
80 static const GDBusMethodInfo *application_methods[] = {
81   &activate_method, &open_method, &command_line_method, NULL
82 };
83
84 const GDBusInterfaceInfo org_gtk_Application = {
85   -1, (gchar *) "org.gtk.Application",
86   (GDBusMethodInfo **) application_methods
87 };
88
89 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
90 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
91 static const GDBusArgInfo *print_out[] = { NULL };
92
93 static const GDBusMethodInfo stdout_method = {
94   -1, (gchar *) "Print",
95   (GDBusArgInfo **) print_in,
96   (GDBusArgInfo **) print_out
97 };
98
99 static const GDBusMethodInfo stderr_method = {
100   -1, (gchar *) "PrintError",
101   (GDBusArgInfo **) print_in,
102   (GDBusArgInfo **) print_out
103 };
104
105 static const GDBusMethodInfo *cmdline_methods[] = {
106   &stdout_method, &stderr_method, NULL
107 };
108
109 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
110   -1, (gchar *) "org.gtk.private.CommandLine",
111   (GDBusMethodInfo **) cmdline_methods
112 };
113
114 /* GApplication implementation {{{1 */
115 struct _GApplicationImpl
116 {
117   GDBusConnection *session_bus;
118   const gchar     *bus_name;
119   gchar           *object_path;
120   guint            object_id;
121   gboolean         actions_exported;
122   gboolean         menu_exported;
123   gpointer         app;
124 };
125
126
127 static GApplicationCommandLine *
128 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
129
130
131 static void
132 g_application_impl_method_call (GDBusConnection       *connection,
133                                 const gchar           *sender,
134                                 const gchar           *object_path,
135                                 const gchar           *interface_name,
136                                 const gchar           *method_name,
137                                 GVariant              *parameters,
138                                 GDBusMethodInvocation *invocation,
139                                 gpointer               user_data)
140 {
141   GApplicationImpl *impl = user_data;
142   GApplicationClass *class;
143
144   class = G_APPLICATION_GET_CLASS (impl->app);
145
146   if (strcmp (method_name, "Activate") == 0)
147     {
148       GVariant *platform_data;
149
150       g_variant_get (parameters, "(@a{sv})", &platform_data);
151       class->before_emit (impl->app, platform_data);
152       g_signal_emit_by_name (impl->app, "activate");
153       class->after_emit (impl->app, platform_data);
154       g_variant_unref (platform_data);
155
156       g_dbus_method_invocation_return_value (invocation, NULL);
157     }
158
159   else if (strcmp (method_name, "Open") == 0)
160     {
161       GVariant *platform_data;
162       const gchar *hint;
163       GVariant *array;
164       GFile **files;
165       gint n, i;
166
167       g_variant_get (parameters, "(@ass@a{sv})",
168                      &array, &hint, &platform_data);
169
170       n = g_variant_n_children (array);
171       files = g_new (GFile *, n + 1);
172
173       for (i = 0; i < n; i++)
174         {
175           const gchar *uri;
176
177           g_variant_get_child (array, i, "&s", &uri);
178           files[i] = g_file_new_for_uri (uri);
179         }
180       g_variant_unref (array);
181       files[n] = NULL;
182
183       class->before_emit (impl->app, platform_data);
184       g_signal_emit_by_name (impl->app, "open", files, n, hint);
185       class->after_emit (impl->app, platform_data);
186
187       g_variant_unref (platform_data);
188
189       for (i = 0; i < n; i++)
190         g_object_unref (files[i]);
191       g_free (files);
192
193       g_dbus_method_invocation_return_value (invocation, NULL);
194     }
195
196   else if (strcmp (method_name, "CommandLine") == 0)
197     {
198       GApplicationCommandLine *cmdline;
199       GVariant *platform_data;
200       int status;
201
202       cmdline = g_dbus_command_line_new (invocation);
203       platform_data = g_variant_get_child_value (parameters, 2);
204       class->before_emit (impl->app, platform_data);
205       g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
206       g_application_command_line_set_exit_status (cmdline, status);
207       class->after_emit (impl->app, platform_data);
208       g_variant_unref (platform_data);
209       g_object_unref (cmdline);
210     }
211   else
212     g_assert_not_reached ();
213 }
214
215 static gchar *
216 application_path_from_appid (const gchar *appid)
217 {
218   gchar *appid_path, *iter;
219
220   appid_path = g_strconcat ("/", appid, NULL);
221   for (iter = appid_path; *iter; iter++)
222     {
223       if (*iter == '.')
224         *iter = '/';
225
226       if (*iter == '-')
227         *iter = '_';
228     }
229
230   return appid_path;
231 }
232
233 void
234 g_application_impl_destroy (GApplicationImpl *impl)
235 {
236   if (impl->session_bus)
237     {
238       if (impl->object_id)
239         g_dbus_connection_unregister_object (impl->session_bus,
240                                              impl->object_id);
241       if (impl->actions_exported)
242         g_action_group_dbus_export_stop (impl->app);
243       if (impl->menu_exported)
244         g_menu_model_dbus_export_stop (g_application_get_menu (impl->app));
245
246       g_dbus_connection_call (impl->session_bus,
247                               "org.freedesktop.DBus",
248                               "/org/freedesktop/DBus",
249                               "org.freedesktop.DBus",
250                               "ReleaseName",
251                               g_variant_new ("(s)",
252                                              impl->bus_name),
253                               NULL,
254                               G_DBUS_CALL_FLAGS_NONE,
255                               -1, NULL, NULL, NULL);
256
257       g_object_unref (impl->session_bus);
258       g_free (impl->object_path);
259     }
260   else
261     {
262       g_assert (impl->object_path == NULL);
263       g_assert (impl->object_id == 0);
264     }
265
266   g_slice_free (GApplicationImpl, impl);
267 }
268
269 GApplicationImpl *
270 g_application_impl_register (GApplication       *application,
271                              const gchar        *appid,
272                              GApplicationFlags   flags,
273                              GActionGroup      **remote_actions,
274                              GCancellable       *cancellable,
275                              GError            **error)
276 {
277   const static GDBusInterfaceVTable vtable = {
278     g_application_impl_method_call
279   };
280   GDBusActionGroup *actions;
281   GApplicationImpl *impl;
282   GVariant *reply;
283   guint32 rval;
284
285   impl = g_slice_new0 (GApplicationImpl);
286
287   impl->app = application;
288   impl->bus_name = appid;
289
290   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
291
292   if (impl->session_bus == NULL)
293     {
294       /* If we can't connect to the session bus, proceed as a normal
295        * non-unique application.
296        */
297       *remote_actions = NULL;
298       return impl;
299     }
300
301   impl->object_path = application_path_from_appid (appid);
302
303   /* Only try to be the primary instance if
304    * G_APPLICATION_IS_LAUNCHER was not specified.
305    */
306   if (~flags & G_APPLICATION_IS_LAUNCHER)
307     {
308       /* Attempt to become primary instance. */
309       impl->object_id =
310         g_dbus_connection_register_object (impl->session_bus,
311                                            impl->object_path,
312                                            (GDBusInterfaceInfo *)
313                                              &org_gtk_Application,
314                                            &vtable, impl, NULL, error);
315
316       if (impl->object_id == 0)
317         {
318           g_object_unref (impl->session_bus);
319           g_free (impl->object_path);
320           impl->session_bus = NULL;
321           impl->object_path = NULL;
322
323           g_slice_free (GApplicationImpl, impl);
324           return NULL;
325         }
326
327       if (!g_action_group_dbus_export_start (impl->session_bus,
328                                              impl->object_path,
329                                              impl->app, error))
330         {
331           g_dbus_connection_unregister_object (impl->session_bus,
332                                                impl->object_id);
333
334           g_object_unref (impl->session_bus);
335           g_free (impl->object_path);
336           impl->session_bus = NULL;
337           impl->object_path = NULL;
338
339           g_slice_free (GApplicationImpl, impl);
340           return NULL;
341         }
342       impl->actions_exported = TRUE;
343
344       if (g_application_get_menu (impl->app))
345         {
346           if (!g_menu_model_dbus_export_start (impl->session_bus,
347                                                impl->object_path,
348                                                g_application_get_menu (impl->app),
349                                                error))
350             {
351               g_action_group_dbus_export_stop (impl->app);
352               impl->actions_exported = FALSE;
353
354               g_dbus_connection_unregister_object (impl->session_bus,
355                                                    impl->object_id);
356
357               g_object_unref (impl->session_bus);
358               g_free (impl->object_path);
359               impl->session_bus = NULL;
360               impl->object_path = NULL;
361
362               g_slice_free (GApplicationImpl, impl);
363               return NULL;
364             }
365           impl->menu_exported = TRUE;
366         }
367
368       /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
369       reply = g_dbus_connection_call_sync (impl->session_bus,
370                                            "org.freedesktop.DBus",
371                                            "/org/freedesktop/DBus",
372                                            "org.freedesktop.DBus",
373                                            "RequestName",
374                                            g_variant_new ("(su)",
375                                                           impl->bus_name,
376                                                           0x4),
377                                            G_VARIANT_TYPE ("(u)"),
378                                            0, -1, cancellable, error);
379
380       if (reply == NULL)
381         {
382           g_dbus_connection_unregister_object (impl->session_bus,
383                                                impl->object_id);
384           impl->object_id = 0;
385
386           g_action_group_dbus_export_stop (impl->app);
387           impl->actions_exported = FALSE;
388
389           if (impl->menu_exported)
390             {
391               g_menu_model_dbus_export_stop (g_application_get_menu (impl->app));
392               impl->menu_exported = FALSE;
393             }
394
395           g_object_unref (impl->session_bus);
396           g_free (impl->object_path);
397           impl->session_bus = NULL;
398           impl->object_path = NULL;
399
400           g_slice_free (GApplicationImpl, impl);
401           return NULL;
402         }
403
404       g_variant_get (reply, "(u)", &rval);
405       g_variant_unref (reply);
406
407       /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
408       if (rval != 3)
409         {
410           /* We are the primary instance. */
411           g_dbus_connection_emit_signal (impl->session_bus,
412                                          NULL,
413                                          impl->object_path,
414                                          "org.gtk.Application",
415                                          "Hello",
416                                          g_variant_new ("(s)",
417                                                         impl->bus_name),
418                                          NULL);
419           *remote_actions = NULL;
420           return impl;
421         }
422
423       /* We didn't make it.  Drop our service-side stuff. */
424       g_dbus_connection_unregister_object (impl->session_bus,
425                                            impl->object_id);
426       impl->object_id = 0;
427       g_action_group_dbus_export_stop (impl->app);
428       impl->actions_exported = FALSE;
429       if (impl->menu_exported)
430         {
431           g_menu_model_dbus_export_stop (g_application_get_menu (impl->app));
432           impl->menu_exported = FALSE;
433         }
434
435       if (flags & G_APPLICATION_IS_SERVICE)
436         {
437           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
438                        "Unable to acquire bus name `%s'", appid);
439           g_object_unref (impl->session_bus);
440           g_free (impl->object_path);
441
442           g_slice_free (GApplicationImpl, impl);
443           return NULL;
444         }
445     }
446
447   /* We are non-primary.  Try to get the primary's list of actions.
448    * This also serves as a mechanism to ensure that the primary exists
449    * (ie: DBus service files installed correctly, etc).
450    */
451   actions = g_dbus_action_group_new_sync (impl->session_bus, impl->bus_name, impl->object_path,
452                                           G_DBUS_ACTION_GROUP_FLAGS_NONE, NULL, error);
453
454   if (actions == NULL)
455     {
456       /* The primary appears not to exist.  Fail the registration. */
457       g_object_unref (impl->session_bus);
458       g_free (impl->object_path);
459       impl->session_bus = NULL;
460       impl->object_path = NULL;
461
462       g_slice_free (GApplicationImpl, impl);
463       return NULL;
464     }
465
466   *remote_actions = G_ACTION_GROUP (actions);
467
468   return impl;
469 }
470
471 void
472 g_application_impl_activate (GApplicationImpl *impl,
473                              GVariant         *platform_data)
474 {
475   g_dbus_connection_call (impl->session_bus,
476                           impl->bus_name,
477                           impl->object_path,
478                           "org.gtk.Application",
479                           "Activate",
480                           g_variant_new ("(@a{sv})", platform_data),
481                           NULL, 0, -1, NULL, NULL, NULL);
482 }
483
484 void
485 g_application_impl_open (GApplicationImpl  *impl,
486                          GFile            **files,
487                          gint               n_files,
488                          const gchar       *hint,
489                          GVariant          *platform_data)
490 {
491   GVariantBuilder builder;
492   gint i;
493
494   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
495   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
496   for (i = 0; i < n_files; i++)
497     {
498       gchar *uri = g_file_get_uri (files[i]);
499       g_variant_builder_add (&builder, "s", uri);
500       g_free (uri);
501     }
502   g_variant_builder_close (&builder);
503   g_variant_builder_add (&builder, "s", hint);
504   g_variant_builder_add_value (&builder, platform_data);
505
506   g_dbus_connection_call (impl->session_bus,
507                           impl->bus_name,
508                           impl->object_path,
509                           "org.gtk.Application",
510                           "Open",
511                           g_variant_builder_end (&builder),
512                           NULL, 0, -1, NULL, NULL, NULL);
513 }
514
515 static void
516 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
517                                         const gchar           *sender,
518                                         const gchar           *object_path,
519                                         const gchar           *interface_name,
520                                         const gchar           *method_name,
521                                         GVariant              *parameters,
522                                         GDBusMethodInvocation *invocation,
523                                         gpointer               user_data)
524 {
525   const gchar *message;
526
527   g_variant_get_child (parameters, 0, "&s", &message);
528
529   if (strcmp (method_name, "Print") == 0)
530     g_print ("%s", message);
531   else if (strcmp (method_name, "PrintError") == 0)
532     g_printerr ("%s", message);
533   else
534     g_assert_not_reached ();
535
536   g_dbus_method_invocation_return_value (invocation, NULL);
537 }
538
539 typedef struct
540 {
541   GMainLoop *loop;
542   int status;
543 } CommandLineData;
544
545 static void
546 g_application_impl_cmdline_done (GObject      *source,
547                                  GAsyncResult *result,
548                                  gpointer      user_data)
549 {
550   CommandLineData *data = user_data;
551   GError *error = NULL;
552   GVariant *reply;
553
554   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
555                                          result, &error);
556
557   if (reply != NULL)
558     {
559       g_variant_get (reply, "(i)", &data->status);
560       g_variant_unref (reply);
561     }
562
563   else
564     {
565       g_printerr ("%s\n", error->message);
566       g_error_free (error);
567       data->status = 1;
568     }
569
570   g_main_loop_quit (data->loop);
571 }
572
573 int
574 g_application_impl_command_line (GApplicationImpl  *impl,
575                                  gchar            **arguments,
576                                  GVariant          *platform_data)
577 {
578   const static GDBusInterfaceVTable vtable = {
579     g_application_impl_cmdline_method_call
580   };
581   const gchar *object_path = "/org/gtk/Application/CommandLine";
582   GMainContext *context;
583   CommandLineData data;
584   guint object_id;
585
586   context = g_main_context_new ();
587   data.loop = g_main_loop_new (context, FALSE);
588   g_main_context_push_thread_default (context);
589
590   object_id = g_dbus_connection_register_object (impl->session_bus,
591                                                  object_path,
592                                                  (GDBusInterfaceInfo *)
593                                                    &org_gtk_private_Cmdline,
594                                                  &vtable, &data, NULL, NULL);
595   /* In theory we should try other paths... */
596   g_assert (object_id != 0);
597
598   g_dbus_connection_call (impl->session_bus,
599                           impl->bus_name,
600                           impl->object_path,
601                           "org.gtk.Application",
602                           "CommandLine",
603                           g_variant_new ("(o^aay@a{sv})", object_path,
604                                          arguments, platform_data),
605                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
606                           g_application_impl_cmdline_done, &data);
607
608   g_main_loop_run (data.loop);
609
610   g_main_context_pop_thread_default (context);
611   g_main_context_unref (context);
612   g_main_loop_unref (data.loop);
613
614   return data.status;
615 }
616
617 void
618 g_application_impl_flush (GApplicationImpl *impl)
619 {
620   if (impl->session_bus)
621     g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
622 }
623
624
625 /* GDBusCommandLine implementation {{{1 */
626
627 typedef GApplicationCommandLineClass GDBusCommandLineClass;
628 static GType g_dbus_command_line_get_type (void);
629 typedef struct
630 {
631   GApplicationCommandLine  parent_instance;
632   GDBusMethodInvocation   *invocation;
633
634   GDBusConnection *connection;
635   const gchar     *bus_name;
636   const gchar     *object_path;
637 } GDBusCommandLine;
638
639
640 G_DEFINE_TYPE (GDBusCommandLine,
641                g_dbus_command_line,
642                G_TYPE_APPLICATION_COMMAND_LINE)
643
644 static void
645 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
646                                    const gchar             *message)
647 {
648   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
649
650   g_dbus_connection_call (gdbcl->connection,
651                           gdbcl->bus_name,
652                           gdbcl->object_path,
653                           "org.gtk.private.CommandLine", "Print",
654                           g_variant_new ("(s)", message),
655                           NULL, 0, -1, NULL, NULL, NULL);
656 }
657
658 static void
659 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
660                                       const gchar             *message)
661 {
662   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
663
664   g_dbus_connection_call (gdbcl->connection,
665                           gdbcl->bus_name,
666                           gdbcl->object_path,
667                           "org.gtk.private.CommandLine", "PrintError",
668                           g_variant_new ("(s)", message),
669                           NULL, 0, -1, NULL, NULL, NULL);
670 }
671
672 static void
673 g_dbus_command_line_finalize (GObject *object)
674 {
675   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
676   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
677   gint status;
678
679   status = g_application_command_line_get_exit_status (cmdline);
680
681   g_dbus_method_invocation_return_value (gdbcl->invocation,
682                                          g_variant_new ("(i)", status));
683   g_object_unref (gdbcl->invocation);
684
685   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
686     ->finalize (object);
687 }
688
689 static void
690 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
691 {
692 }
693
694 static void
695 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
696 {
697   GObjectClass *object_class = G_OBJECT_CLASS (class);
698
699   object_class->finalize = g_dbus_command_line_finalize;
700   class->printerr_literal = g_dbus_command_line_printerr_literal;
701   class->print_literal = g_dbus_command_line_print_literal;
702 }
703
704 static GApplicationCommandLine *
705 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
706 {
707   GDBusCommandLine *gdbcl;
708   GVariant *args;
709
710   args = g_dbus_method_invocation_get_parameters (invocation);
711
712   gdbcl = g_object_new (g_dbus_command_line_get_type (),
713                         "arguments", g_variant_get_child_value (args, 1),
714                         "platform-data", g_variant_get_child_value (args, 2),
715                         NULL);
716   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
717   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
718   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
719   gdbcl->invocation = g_object_ref (invocation);
720
721   return G_APPLICATION_COMMAND_LINE (gdbcl);
722 }
723
724 /* Epilogue {{{1 */
725
726 /* vim:set foldmethod=marker: */