Move a confusing comment to the right place
[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   /* don't try to be the primary instance if
275    * G_APPLICATION_IS_LAUNCHER was specified.
276    */
277   if (flags & G_APPLICATION_IS_LAUNCHER)
278     {
279       impl->object_id = 0;
280       *is_remote = TRUE;
281
282       return impl;
283     }
284
285   impl->object_id = g_dbus_connection_register_object (impl->session_bus,
286                                                        impl->object_path,
287                                                        (GDBusInterfaceInfo *)
288                                                          &org_gtk_Application,
289                                                        &vtable,
290                                                        impl, NULL,
291                                                        error);
292
293   if (impl->object_id == 0)
294     {
295       g_object_unref (impl->session_bus);
296       g_free (impl->object_path);
297       impl->session_bus = NULL;
298       impl->object_path = NULL;
299
300       g_slice_free (GApplicationImpl, impl);
301       return NULL;
302     }
303
304   reply = g_dbus_connection_call_sync (impl->session_bus,
305                                        "org.freedesktop.DBus",
306                                        "/org/freedesktop/DBus",
307                                        "org.freedesktop.DBus",
308                                        "RequestName",
309                                        g_variant_new ("(su)",
310                                        /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
311                                                       impl->bus_name, 0x4),
312                                        G_VARIANT_TYPE ("(u)"),
313                                        0, -1, cancellable, error);
314
315   if (reply == NULL)
316     {
317       g_dbus_connection_unregister_object (impl->session_bus,
318                                            impl->object_id);
319       impl->object_id = 0;
320
321       g_object_unref (impl->session_bus);
322       g_free (impl->object_path);
323       impl->session_bus = NULL;
324       impl->object_path = NULL;
325
326       g_slice_free (GApplicationImpl, impl);
327       return NULL;
328     }
329
330   g_variant_get (reply, "(u)", &rval);
331   g_variant_unref (reply);
332
333   /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
334   if ((*is_remote = (rval == 3)))
335     {
336       g_dbus_connection_unregister_object (impl->session_bus,
337                                            impl->object_id);
338       impl->object_id = 0;
339
340       if (flags & G_APPLICATION_IS_SERVICE)
341         {
342           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
343                        "Unable to acquire bus name `%s'", appid);
344           g_object_unref (impl->session_bus);
345           g_free (impl->object_path);
346
347           g_slice_free (GApplicationImpl, impl);
348           impl = NULL;
349         }
350     }
351
352   return impl;
353 }
354
355 void
356 g_application_impl_activate (GApplicationImpl *impl,
357                              GVariant         *platform_data)
358 {
359   g_dbus_connection_call (impl->session_bus,
360                           impl->bus_name,
361                           impl->object_path,
362                           "org.gtk.Application",
363                           "Activate",
364                           g_variant_new ("(@a{sv})", platform_data),
365                           NULL, 0, -1, NULL, NULL, NULL);
366 }
367
368 void
369 g_application_impl_open (GApplicationImpl  *impl,
370                          GFile            **files,
371                          gint               n_files,
372                          const gchar       *hint,
373                          GVariant          *platform_data)
374 {
375   GVariantBuilder builder;
376   gint i;
377
378   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
379   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
380   for (i = 0; i < n_files; i++)
381     {
382       gchar *uri = g_file_get_uri (files[i]);
383       g_variant_builder_add (&builder, "s", uri);
384       g_free (uri);
385     }
386   g_variant_builder_close (&builder);
387   g_variant_builder_add (&builder, "s", hint);
388   g_variant_builder_add_value (&builder, platform_data);
389
390   g_dbus_connection_call (impl->session_bus,
391                           impl->bus_name,
392                           impl->object_path,
393                           "org.gtk.Application",
394                           "Open",
395                           g_variant_builder_end (&builder),
396                           NULL, 0, -1, NULL, NULL, NULL);
397 }
398
399 static void
400 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
401                                         const gchar           *sender,
402                                         const gchar           *object_path,
403                                         const gchar           *interface_name,
404                                         const gchar           *method_name,
405                                         GVariant              *parameters,
406                                         GDBusMethodInvocation *invocation,
407                                         gpointer               user_data)
408 {
409   const gchar *message;
410
411   g_variant_get_child (parameters, 0, "&s", &message);
412
413   if (strcmp (method_name, "Print") == 0)
414     g_print ("%s", message);
415   else if (strcmp (method_name, "PrintError") == 0)
416     g_printerr ("%s", message);
417   else
418     g_assert_not_reached ();
419
420   g_dbus_method_invocation_return_value (invocation, NULL);
421 }
422
423 typedef struct
424 {
425   GMainLoop *loop;
426   int status;
427 } CommandLineData;
428
429 static void
430 g_application_impl_cmdline_done (GObject      *source,
431                                  GAsyncResult *result,
432                                  gpointer      user_data)
433 {
434   CommandLineData *data = user_data;
435   GError *error = NULL;
436   GVariant *reply;
437
438   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
439                                          result, &error);
440
441   if (reply != NULL)
442     {
443       g_variant_get (reply, "(i)", &data->status);
444       g_variant_unref (reply);
445     }
446
447   else
448     {
449       g_printerr ("%s\n", error->message);
450       g_error_free (error);
451       data->status = 1;
452     }
453
454   g_main_loop_quit (data->loop);
455 }
456
457 int
458 g_application_impl_command_line (GApplicationImpl  *impl,
459                                  gchar            **arguments,
460                                  GVariant          *platform_data)
461 {
462   const static GDBusInterfaceVTable vtable = {
463     g_application_impl_cmdline_method_call
464   };
465   const gchar *object_path = "/org/gtk/Application/CommandLine";
466   GMainContext *context;
467   CommandLineData data;
468   guint object_id;
469
470   context = g_main_context_new ();
471   data.loop = g_main_loop_new (context, FALSE);
472   g_main_context_push_thread_default (context);
473
474   object_id = g_dbus_connection_register_object (impl->session_bus,
475                                                  object_path,
476                                                  (GDBusInterfaceInfo *)
477                                                    &org_gtk_private_Cmdline,
478                                                  &vtable, &data, NULL, NULL);
479   /* In theory we should try other paths... */
480   g_assert (object_id != 0);
481
482   g_dbus_connection_call (impl->session_bus,
483                           impl->bus_name,
484                           impl->object_path,
485                           "org.gtk.Application",
486                           "CommandLine",
487                           g_variant_new ("(o^aay@a{sv})", object_path,
488                                          arguments, platform_data),
489                           G_VARIANT_TYPE ("(i)"), 0, -1, NULL,
490                           g_application_impl_cmdline_done, &data);
491
492   g_main_loop_run (data.loop);
493
494   g_main_context_pop_thread_default (context);
495   g_main_context_unref (context);
496   g_main_loop_unref (data.loop);
497
498   return data.status;
499 }
500
501 void
502 g_application_impl_flush (GApplicationImpl *impl)
503 {
504   g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
505 }
506
507
508 /* GDBusCommandLine implementation {{{1 */
509
510 typedef GApplicationCommandLineClass GDBusCommandLineClass;
511 static GType g_dbus_command_line_get_type (void);
512 typedef struct
513 {
514   GApplicationCommandLine  parent_instance;
515   GDBusMethodInvocation   *invocation;
516
517   GDBusConnection *connection;
518   const gchar     *bus_name;
519   const gchar     *object_path;
520 } GDBusCommandLine;
521
522
523 G_DEFINE_TYPE (GDBusCommandLine,
524                g_dbus_command_line,
525                G_TYPE_APPLICATION_COMMAND_LINE)
526
527 static void
528 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
529                                    const gchar             *message)
530 {
531   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
532
533   g_dbus_connection_call (gdbcl->connection,
534                           gdbcl->bus_name,
535                           gdbcl->object_path,
536                           "org.gtk.private.CommandLine", "Print",
537                           g_variant_new ("(s)", message),
538                           NULL, 0, -1, NULL, NULL, NULL);
539 }
540
541 static void
542 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
543                                       const gchar             *message)
544 {
545   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
546
547   g_dbus_connection_call (gdbcl->connection,
548                           gdbcl->bus_name,
549                           gdbcl->object_path,
550                           "org.gtk.private.CommandLine", "PrintError",
551                           g_variant_new ("(s)", message),
552                           NULL, 0, -1, NULL, NULL, NULL);
553 }
554
555 static void
556 g_dbus_command_line_finalize (GObject *object)
557 {
558   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
559   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
560   gint status;
561
562   status = g_application_command_line_get_exit_status (cmdline);
563
564   g_dbus_method_invocation_return_value (gdbcl->invocation,
565                                          g_variant_new ("(i)", status));
566   g_object_unref (gdbcl->invocation);
567
568   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
569     ->finalize (object);
570 }
571
572 static void
573 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
574 {
575 }
576
577 static void
578 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
579 {
580   GObjectClass *object_class = G_OBJECT_CLASS (class);
581
582   object_class->finalize = g_dbus_command_line_finalize;
583   class->printerr_literal = g_dbus_command_line_printerr_literal;
584   class->print_literal = g_dbus_command_line_print_literal;
585 }
586
587 static GApplicationCommandLine *
588 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
589 {
590   GDBusCommandLine *gdbcl;
591   GVariant *args;
592
593   args = g_dbus_method_invocation_get_parameters (invocation);
594
595   gdbcl = g_object_new (g_dbus_command_line_get_type (),
596                         "arguments", g_variant_get_child_value (args, 1),
597                         "platform-data", g_variant_get_child_value (args, 2),
598                         NULL);
599   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
600   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
601   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
602   gdbcl->invocation = g_object_ref (invocation);
603
604   return G_APPLICATION_COMMAND_LINE (gdbcl);
605 }
606
607 /* Epilogue {{{1 */
608
609 /* vim:set foldmethod=marker: */