2 * Copyright © 2010 Codethink Limited
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.
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.
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.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gapplicationimpl.h"
24 #include "gapplication.h"
26 #include "gdbusconnection.h"
27 #include "gdbusintrospection.h"
28 #include "gdbuserror.h"
33 #include "gapplicationimpl-dbus-interface.c"
35 struct _GApplicationImpl
37 GDBusConnection *session_bus;
38 const gchar *bus_name;
43 GMainLoop *cmdline_mainloop;
47 g_application_impl_method_call (GDBusConnection *connection,
49 const gchar *object_path,
50 const gchar *interface_name,
51 const gchar *method_name,
53 GDBusMethodInvocation *invocation,
56 GApplicationImpl *impl = user_data;
57 GApplicationClass *class;
59 class = G_APPLICATION_GET_CLASS (impl->app);
61 if (strcmp (method_name, "Activate") == 0)
63 GVariant *platform_data;
65 g_variant_get (parameters, "(@a{sv})", &platform_data);
66 class->before_emit (impl->app, platform_data);
67 g_signal_emit_by_name (impl->app, "activate");
68 class->after_emit (impl->app, platform_data);
69 g_variant_unref (platform_data);
72 else if (strcmp (method_name, "Open") == 0)
74 GVariant *platform_data;
80 g_variant_get (parameters, "(@ass@a{sv})",
81 &array, &hint, &platform_data);
83 n = g_variant_n_children (array);
84 files = g_new (GFile *, n + 1);
86 for (i = 0; i < n; i++)
90 g_variant_get_child (array, i, "&s", &uri);
91 files[i] = g_file_new_for_uri (uri);
93 g_variant_unref (array);
96 class->before_emit (impl->app, platform_data);
97 g_signal_emit_by_name (impl->app, "open", files, n, hint);
98 class->after_emit (impl->app, platform_data);
100 g_variant_unref (platform_data);
102 for (i = 0; i < n; i++)
103 g_object_unref (files[i]);
108 g_assert_not_reached ();
112 application_path_from_appid (const gchar *appid)
114 gchar *appid_path, *iter;
116 appid_path = g_strconcat ("/", appid, NULL);
117 for (iter = appid_path; *iter; iter++)
127 g_application_impl_destroy (GApplicationImpl *impl)
129 if (impl->session_bus)
132 g_dbus_connection_unregister_object (impl->session_bus,
135 g_object_unref (impl->session_bus);
136 g_free (impl->object_path);
140 g_assert (impl->object_path == NULL);
141 g_assert (impl->object_id == 0);
144 g_slice_free (GApplicationImpl, impl);
148 g_application_impl_register (GApplication *application,
150 GApplicationFlags flags,
152 GCancellable *cancellable,
155 const static GDBusInterfaceVTable vtable = {
156 g_application_impl_method_call
158 GApplicationImpl *impl;
162 impl = g_slice_new (GApplicationImpl);
164 impl->app = application;
165 impl->bus_name = appid;
167 impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
170 if (impl->session_bus == NULL)
172 g_slice_free (GApplicationImpl, impl);
176 impl->object_path = application_path_from_appid (appid);
178 if (flags & G_APPLICATION_FLAGS_IS_LAUNCHER)
186 impl->object_id = g_dbus_connection_register_object (impl->session_bus,
188 (GDBusInterfaceInfo *)
189 &org_gtk_Application,
194 if (impl->object_id == 0)
196 g_object_unref (impl->session_bus);
197 g_free (impl->object_path);
198 impl->session_bus = NULL;
199 impl->object_path = NULL;
201 g_slice_free (GApplicationImpl, impl);
205 reply = g_dbus_connection_call_sync (impl->session_bus,
206 "org.freedesktop.DBus",
207 "/org/freedesktop/DBus",
208 "org.freedesktop.DBus",
210 g_variant_new ("(su)",
211 /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
212 impl->bus_name, 0x4),
213 G_VARIANT_TYPE ("(u)"),
214 0, -1, cancellable, error);
218 g_dbus_connection_unregister_object (impl->session_bus,
222 g_object_unref (impl->session_bus);
223 g_free (impl->object_path);
224 impl->session_bus = NULL;
225 impl->object_path = NULL;
227 g_slice_free (GApplicationImpl, impl);
231 g_variant_get (reply, "(u)", &rval);
232 g_variant_unref (reply);
234 /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
235 if ((*is_remote = (rval == 3)))
237 g_dbus_connection_unregister_object (impl->session_bus,
241 if (flags & G_APPLICATION_FLAGS_IS_SERVICE)
243 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
244 "Unable to acquire bus name `%s'", appid);
245 g_object_unref (impl->session_bus);
246 g_free (impl->object_path);
248 g_slice_free (GApplicationImpl, impl);
257 g_application_impl_activate (GApplicationImpl *impl,
258 GVariant *platform_data)
260 g_dbus_connection_call (impl->session_bus,
263 "org.gtk.Application",
265 g_variant_new ("(@a{sv})", platform_data),
266 NULL, 0, -1, NULL, NULL, NULL);
270 g_application_impl_open (GApplicationImpl *impl,
274 GVariant *platform_data)
276 GVariantBuilder builder;
279 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
280 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
281 for (i = 0; i < n_files; i++)
283 gchar *uri = g_file_get_uri (files[i]);
284 g_variant_builder_add (&builder, "s", uri);
287 g_variant_builder_close (&builder);
288 g_variant_builder_add (&builder, "s", hint);
289 g_variant_builder_add_value (&builder, platform_data);
291 g_dbus_connection_call (impl->session_bus,
294 "org.gtk.Application",
296 g_variant_builder_end (&builder),
297 NULL, 0, -1, NULL, NULL, NULL);
301 g_application_impl_flush (GApplicationImpl *impl)
303 g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);