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