GApplication: reduce GVariant abuse
[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 "gapplication.h"
25 #include "gfile.h"
26 #include "gdbusconnection.h"
27 #include "gdbusintrospection.h"
28 #include "gdbuserror.h"
29
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "gapplicationcommandline.h"
34 #include "gdbusmethodinvocation.h"
35
36 /* DBus Interface definition {{{1 */
37 static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
38
39 static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
40 static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
41
42 static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
43 static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
44
45 static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
46 static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
47 static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
48
49 static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
50 static const GDBusArgInfo *activate_out[] = { NULL };
51
52 static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
53 static const GDBusArgInfo *open_out[] = { NULL };
54
55 static const GDBusArgInfo *cmdline_in[] = { &cmdline_path_arg, &cmdline_arguments_arg, &platform_data_arg, NULL };
56 static const GDBusArgInfo *cmdline_out[] = { &cmdline_exit_status_arg, NULL };
57
58 static const GDBusMethodInfo activate_method = {
59   -1, (gchar *) "Activate",
60   (GDBusArgInfo **) activate_in,
61   (GDBusArgInfo **) activate_out
62 };
63
64 static const GDBusMethodInfo open_method = {
65   -1, (gchar *) "Open",
66   (GDBusArgInfo **) open_in,
67   (GDBusArgInfo **) open_out
68 };
69
70 static const GDBusMethodInfo command_line_method = {
71   -1, (gchar *) "CommandLine",
72   (GDBusArgInfo **) cmdline_in,
73   (GDBusArgInfo **) cmdline_out
74 };
75
76 static const GDBusMethodInfo *application_methods[] = {
77   &activate_method, &open_method, &command_line_method, NULL
78 };
79
80 const GDBusInterfaceInfo org_gtk_Application = {
81   -1, (gchar *) "org.gtk.Application",
82   (GDBusMethodInfo **) application_methods
83 };
84
85 static const GDBusArgInfo message_arg = { -1, (gchar *) "message", (gchar *) "s" };
86 static const GDBusArgInfo *print_in[] = { &message_arg, NULL };
87 static const GDBusArgInfo *print_out[] = { NULL };
88
89 static const GDBusMethodInfo stdout_method = {
90   -1, (gchar *) "Print",
91   (GDBusArgInfo **) print_in,
92   (GDBusArgInfo **) print_out
93 };
94
95 static const GDBusMethodInfo stderr_method = {
96   -1, (gchar *) "PrintError",
97   (GDBusArgInfo **) print_in,
98   (GDBusArgInfo **) print_out
99 };
100
101 static const GDBusMethodInfo *cmdline_methods[] = {
102   &stdout_method, &stderr_method, NULL
103 };
104
105 const GDBusInterfaceInfo org_gtk_private_Cmdline = {
106   -1, (gchar *) "org.gtk.private.CommandLine",
107   (GDBusMethodInfo **) cmdline_methods
108 };
109
110
111 /* GApplication implementation {{{1 */
112 struct _GApplicationImpl
113 {
114   GDBusConnection *session_bus;
115   const gchar     *bus_name;
116   gchar           *object_path;
117   guint            object_id;
118   gpointer         app;
119 };
120
121
122 static GApplicationCommandLine *
123 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
124
125
126 static void
127 g_application_impl_method_call (GDBusConnection       *connection,
128                                 const gchar           *sender,
129                                 const gchar           *object_path,
130                                 const gchar           *interface_name,
131                                 const gchar           *method_name,
132                                 GVariant              *parameters,
133                                 GDBusMethodInvocation *invocation,
134                                 gpointer               user_data)
135 {
136   GApplicationImpl *impl = user_data;
137   GApplicationClass *class;
138
139   class = G_APPLICATION_GET_CLASS (impl->app);
140
141   if (strcmp (method_name, "Activate") == 0)
142     {
143       GVariant *platform_data;
144
145       g_variant_get (parameters, "(@a{sv})", &platform_data);
146       class->before_emit (impl->app, platform_data);
147       g_signal_emit_by_name (impl->app, "activate");
148       class->after_emit (impl->app, platform_data);
149       g_variant_unref (platform_data);
150     }
151
152   else if (strcmp (method_name, "Open") == 0)
153     {
154       GVariant *platform_data;
155       const gchar *hint;
156       GVariant *array;
157       GFile **files;
158       gint n, i;
159
160       g_variant_get (parameters, "(@ass@a{sv})",
161                      &array, &hint, &platform_data);
162
163       n = g_variant_n_children (array);
164       files = g_new (GFile *, n + 1);
165
166       for (i = 0; i < n; i++)
167         {
168           const gchar *uri;
169
170           g_variant_get_child (array, i, "&s", &uri);
171           files[i] = g_file_new_for_uri (uri);
172         }
173       g_variant_unref (array);
174       files[n] = NULL;
175
176       class->before_emit (impl->app, platform_data);
177       g_signal_emit_by_name (impl->app, "open", files, n, hint);
178       class->after_emit (impl->app, platform_data);
179
180       g_variant_unref (platform_data);
181
182       for (i = 0; i < n; i++)
183         g_object_unref (files[i]);
184       g_free (files);
185     }
186
187   else if (strcmp (method_name, "CommandLine") == 0)
188     {
189       GApplicationCommandLine *cmdline;
190       GVariant *platform_data;
191       int status;
192
193       cmdline = g_dbus_command_line_new (invocation);
194       platform_data = g_variant_get_child_value (parameters, 2);
195       class->before_emit (impl->app, platform_data);
196       g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
197       g_application_command_line_set_exit_status (cmdline, status);
198       class->after_emit (impl->app, platform_data);
199       g_variant_unref (platform_data);
200       g_object_unref (cmdline);
201     }
202
203   else
204     g_assert_not_reached ();
205 }
206
207 static gchar *
208 application_path_from_appid (const gchar *appid)
209 {
210   gchar *appid_path, *iter;
211
212   appid_path = g_strconcat ("/", appid, NULL);
213   for (iter = appid_path; *iter; iter++)
214     {
215       if (*iter == '.')
216         *iter = '/';
217     }
218
219   return appid_path;
220 }
221
222 void
223 g_application_impl_destroy (GApplicationImpl *impl)
224 {
225   if (impl->session_bus)
226     {
227       if (impl->object_id)
228         g_dbus_connection_unregister_object (impl->session_bus,
229                                              impl->object_id);
230
231       g_object_unref (impl->session_bus);
232       g_free (impl->object_path);
233     }
234   else
235     {
236       g_assert (impl->object_path == NULL);
237       g_assert (impl->object_id == 0);
238     }
239
240   g_slice_free (GApplicationImpl, impl);
241 }
242
243 GApplicationImpl *
244 g_application_impl_register (GApplication       *application,
245                              const gchar        *appid,
246                              GApplicationFlags   flags,
247                              gboolean           *is_remote,
248                              GCancellable       *cancellable,
249                              GError            **error)
250 {
251   const static GDBusInterfaceVTable vtable = {
252     g_application_impl_method_call
253   };
254   GApplicationImpl *impl;
255   GVariant *reply;
256   guint32 rval;
257
258   impl = g_slice_new (GApplicationImpl);
259
260   impl->app = application;
261   impl->bus_name = appid;
262
263   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
264                                       cancellable, error);
265
266   if (impl->session_bus == NULL)
267     {
268       g_slice_free (GApplicationImpl, impl);
269       return NULL;
270     }
271
272   impl->object_path = application_path_from_appid (appid);
273
274   if (flags & G_APPLICATION_IS_LAUNCHER)
275     {
276       impl->object_id = 0;
277       *is_remote = TRUE;
278
279       return impl;
280     }
281
282   impl->object_id = g_dbus_connection_register_object (impl->session_bus,
283                                                        impl->object_path,
284                                                        (GDBusInterfaceInfo *)
285                                                          &org_gtk_Application,
286                                                        &vtable,
287                                                        impl, NULL,
288                                                        error);
289
290   if (impl->object_id == 0)
291     {
292       g_object_unref (impl->session_bus);
293       g_free (impl->object_path);
294       impl->session_bus = NULL;
295       impl->object_path = NULL;
296
297       g_slice_free (GApplicationImpl, impl);
298       return NULL;
299     }
300
301   reply = g_dbus_connection_call_sync (impl->session_bus,
302                                        "org.freedesktop.DBus",
303                                        "/org/freedesktop/DBus",
304                                        "org.freedesktop.DBus",
305                                        "RequestName",
306                                        g_variant_new ("(su)",
307                                        /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
308                                                       impl->bus_name, 0x4),
309                                        G_VARIANT_TYPE ("(u)"),
310                                        0, -1, cancellable, error);
311
312   if (reply == NULL)
313     {
314       g_dbus_connection_unregister_object (impl->session_bus,
315                                            impl->object_id);
316       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   g_variant_get (reply, "(u)", &rval);
328   g_variant_unref (reply);
329
330   /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
331   if ((*is_remote = (rval == 3)))
332     {
333       g_dbus_connection_unregister_object (impl->session_bus,
334                                            impl->object_id);
335       impl->object_id = 0;
336
337       if (flags & G_APPLICATION_IS_SERVICE)
338         {
339           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
340                        "Unable to acquire bus name `%s'", appid);
341           g_object_unref (impl->session_bus);
342           g_free (impl->object_path);
343
344           g_slice_free (GApplicationImpl, impl);
345           impl = NULL;
346         }
347     }
348
349   return impl;
350 }
351
352 void
353 g_application_impl_activate (GApplicationImpl *impl,
354                              GVariant         *platform_data)
355 {
356   g_dbus_connection_call (impl->session_bus,
357                           impl->bus_name,
358                           impl->object_path,
359                           "org.gtk.Application",
360                           "Activate",
361                           g_variant_new ("(@a{sv})", platform_data),
362                           NULL, 0, -1, NULL, NULL, NULL);
363 }
364
365 void
366 g_application_impl_open (GApplicationImpl  *impl,
367                          GFile            **files,
368                          gint               n_files,
369                          const gchar       *hint,
370                          GVariant          *platform_data)
371 {
372   GVariantBuilder builder;
373   gint i;
374
375   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
376   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
377   for (i = 0; i < n_files; i++)
378     {
379       gchar *uri = g_file_get_uri (files[i]);
380       g_variant_builder_add (&builder, "s", uri);
381       g_free (uri);
382     }
383   g_variant_builder_close (&builder);
384   g_variant_builder_add (&builder, "s", hint);
385   g_variant_builder_add_value (&builder, platform_data);
386
387   g_dbus_connection_call (impl->session_bus,
388                           impl->bus_name,
389                           impl->object_path,
390                           "org.gtk.Application",
391                           "Open",
392                           g_variant_builder_end (&builder),
393                           NULL, 0, -1, NULL, NULL, NULL);
394 }
395
396 static void
397 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
398                                         const gchar           *sender,
399                                         const gchar           *object_path,
400                                         const gchar           *interface_name,
401                                         const gchar           *method_name,
402                                         GVariant              *parameters,
403                                         GDBusMethodInvocation *invocation,
404                                         gpointer               user_data)
405 {
406   const gchar *message;
407
408   g_variant_get_child (parameters, 0, "&s", &message);
409
410   if (strcmp (method_name, "Print") == 0)
411     g_print ("%s", message);
412   else if (strcmp (method_name, "PrintError") == 0)
413     g_printerr ("%s", message);
414   else
415     g_assert_not_reached ();
416
417   g_dbus_method_invocation_return_value (invocation, NULL);
418 }
419
420 typedef struct
421 {
422   GMainLoop *loop;
423   int status;
424 } CommandLineData;
425
426 static void
427 g_application_impl_cmdline_done (GObject      *source,
428                                  GAsyncResult *result,
429                                  gpointer      user_data)
430 {
431   CommandLineData *data = user_data;
432   GError *error = NULL;
433   GVariant *reply;
434
435   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
436                                          result, &error);
437
438   if (reply != NULL)
439     {
440       g_variant_get (reply, "(i)", &data->status);
441       g_variant_unref (reply);
442     }
443
444   else
445     {
446       g_printerr ("%s\n", error->message);
447       g_error_free (error);
448       data->status = 1;
449     }
450
451   g_main_loop_quit (data->loop);
452 }
453
454 int
455 g_application_impl_command_line (GApplicationImpl  *impl,
456                                  gchar            **arguments,
457                                  GVariant          *platform_data)
458 {
459   const static GDBusInterfaceVTable vtable = {
460     g_application_impl_cmdline_method_call
461   };
462   const gchar *object_path = "/org/gtk/Application/CommandLine";
463   GMainContext *context;
464   CommandLineData data;
465   guint object_id;
466
467   context = g_main_context_new ();
468   data.loop = g_main_loop_new (context, FALSE);
469   g_main_context_push_thread_default (context);
470
471   object_id = g_dbus_connection_register_object (impl->session_bus,
472                                                  object_path,
473                                                  (GDBusInterfaceInfo *)
474                                                    &org_gtk_private_Cmdline,
475                                                  &vtable, &data, NULL, NULL);
476   /* In theory we should try other paths... */
477   g_assert (object_id != 0);
478
479   g_dbus_connection_call (impl->session_bus,
480                           impl->bus_name,
481                           impl->object_path,
482                           "org.gtk.Application",
483                           "CommandLine",
484                           g_variant_new ("(o^aay@a{sv})", object_path,
485                                          arguments, platform_data),
486                           G_VARIANT_TYPE ("(i)"), 0, -1, NULL,
487                           g_application_impl_cmdline_done, &data);
488
489   g_main_loop_run (data.loop);
490
491   g_main_context_pop_thread_default (context);
492   g_main_context_unref (context);
493   g_main_loop_unref (data.loop);
494
495   return data.status;
496 }
497
498 void
499 g_application_impl_flush (GApplicationImpl *impl)
500 {
501   g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
502 }
503
504
505 /* GDBusCommandLine implementation {{{1 */
506
507 typedef GApplicationCommandLineClass GDBusCommandLineClass;
508 static GType g_dbus_command_line_get_type (void);
509 typedef struct
510 {
511   GApplicationCommandLine  parent_instance;
512   GDBusMethodInvocation   *invocation;
513
514   GDBusConnection *connection;
515   const gchar     *bus_name;
516   const gchar     *object_path;
517 } GDBusCommandLine;
518
519
520 G_DEFINE_TYPE (GDBusCommandLine,
521                g_dbus_command_line,
522                G_TYPE_APPLICATION_COMMAND_LINE)
523
524 static void
525 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
526                                    const gchar             *message)
527 {
528   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
529
530   g_dbus_connection_call (gdbcl->connection,
531                           gdbcl->bus_name,
532                           gdbcl->object_path,
533                           "org.gtk.private.CommandLine", "Print",
534                           g_variant_new ("(s)", message),
535                           NULL, 0, -1, NULL, NULL, NULL);
536 }
537
538 static void
539 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
540                                       const gchar             *message)
541 {
542   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
543
544   g_dbus_connection_call (gdbcl->connection,
545                           gdbcl->bus_name,
546                           gdbcl->object_path,
547                           "org.gtk.private.CommandLine", "PrintError",
548                           g_variant_new ("(s)", message),
549                           NULL, 0, -1, NULL, NULL, NULL);
550 }
551
552 static void
553 g_dbus_command_line_finalize (GObject *object)
554 {
555   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
556   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
557   gint status;
558
559   status = g_application_command_line_get_exit_status (cmdline);
560
561   g_dbus_method_invocation_return_value (gdbcl->invocation,
562                                          g_variant_new ("(i)", status));
563   g_object_unref (gdbcl->invocation);
564
565   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
566     ->finalize (object);
567 }
568
569 static void
570 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
571 {
572 }
573
574 static void
575 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
576 {
577   GObjectClass *object_class = G_OBJECT_CLASS (class);
578
579   object_class->finalize = g_dbus_command_line_finalize;
580   class->printerr_literal = g_dbus_command_line_printerr_literal;
581   class->print_literal = g_dbus_command_line_print_literal;
582 }
583
584 static GApplicationCommandLine *
585 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
586 {
587   GDBusCommandLine *gdbcl;
588   GVariant *args;
589
590   args = g_dbus_method_invocation_get_parameters (invocation);
591
592   gdbcl = g_object_new (g_dbus_command_line_get_type (),
593                         "arguments", g_variant_get_child_value (args, 1),
594                         "platform-data", g_variant_get_child_value (args, 2),
595                         NULL);
596   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
597   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
598   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
599   gdbcl->invocation = g_object_ref (invocation);
600
601   return G_APPLICATION_COMMAND_LINE (gdbcl);
602 }
603
604 /* Epilogue {{{1 */
605
606 /* vim:set foldmethod=marker: */