Maintainer's effort v3:
[mmediamanager.git] / src / mm-dbus-manager.c
blobdad2d2b316e191b6c1007d78c7d90633082975f9
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <dbus/dbus-glib.h>
25 #include <dbus/dbus-glib-bindings.h>
26 #include "mm-error.h"
27 #include "mm-dbus-manager.h"
28 #include "mm-type-builtins.h"
30 gboolean mm_dbus_manager_register_app (MMDBusManager *manager, char *path,
31 char *name, GError **error);
32 gboolean mm_dbus_manager_get_registered_apps (MMDBusManager *manager, char ***app_paths,
33 char ***app_names, GError **error);
35 #include "mm-dbus-manager-server-bindings.h"
37 #define MM_DBUS_MANAGER_GET_PRIVATE(o) \
38 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_MANAGER, MMDBusManagerDetails))
40 struct _MMDBusManagerDetails {
41 GPtrArray *application_names;
42 GPtrArray *application_paths;
43 DBusGProxy *bus_proxy;
44 DBusGConnection *connection;
47 G_DEFINE_TYPE (MMDBusManager, mm_dbus_manager, G_TYPE_OBJECT);
49 static void
50 mm_dbus_manager_finalize (GObject *o)
52 MMDBusManager *m = MM_DBUS_MANAGER (o);
54 g_ptr_array_foreach (m->details->application_paths,
55 (GFunc) g_free,
56 NULL);
57 g_ptr_array_free (m->details->application_paths, TRUE);
59 if (m->details->bus_proxy) {
60 g_object_unref (m->details->bus_proxy);
63 G_OBJECT_CLASS (mm_dbus_manager_parent_class)->finalize (o);
66 static void
67 mm_dbus_manager_class_init (MMDBusManagerClass *klass)
69 GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 object_class->finalize = mm_dbus_manager_finalize;
73 dbus_g_object_type_install_info (MM_TYPE_DBUS_MANAGER,
74 &dbus_glib_mm_dbus_manager_object_info);
75 dbus_g_error_domain_register (MM_DBUS_ERROR_QUARK, NULL, MM_TYPE_MD_BUS_ERROR_ENUM);
77 g_type_class_add_private (klass, sizeof (MMDBusManagerDetails));
80 static void
81 activate_applications (MMDBusManager *manager)
83 GDir *dir;
84 const char *filename;
85 const char *dirname;
86 GError *error = NULL;
88 dirname = MMEDIAMANAGER_EXTENSIONDIR"/dbus";
89 dir = g_dir_open (dirname, 0, &error);
90 if (!dir) {
91 g_warning ("Unable to open the DBus extensions directory %s: %s",
92 dirname, error->message);
93 g_error_free (error);
94 return;
97 while ((filename = g_dir_read_name (dir)) != NULL) {
98 char *path;
99 GKeyFile *keyfile;
100 char *executable;
102 path = g_build_filename (dirname, filename, NULL);
103 keyfile = g_key_file_new ();
104 if (!g_key_file_load_from_file (keyfile, path, 0, &error)) {
105 g_warning ("Unable to load extension descriptor %s: %s", path,
106 error->message);
107 g_error_free (error);
108 error = NULL;
109 g_key_file_free (keyfile);
110 g_free (path);
112 /* move on to the next file */
113 continue;
116 /* we are looking for the HelperPath line under the MediaManager group */
117 executable = g_key_file_get_string (keyfile,
118 "MediaManager",
119 "HelperPath",
120 &error);
121 if (!executable) {
122 g_warning ("Descriptor %s is malformed: %s", path, error->message);
123 g_error_free (error);
124 error = NULL;
125 g_key_file_free (keyfile);
126 g_free (path);
128 continue;
131 if (!g_spawn_command_line_async (executable, &error)) {
132 g_warning ("Unable to spawn the extension %s: %s", executable,
133 error->message);
134 g_error_free (error);
135 error = NULL;
138 g_key_file_free (keyfile);
139 g_free (path);
140 g_free (executable);
144 static void
145 register_on_the_bus (MMDBusManager *manager)
147 GError *error = NULL;
148 MMDBusManagerDetails *details = manager->details;
150 details->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
151 if (!details->connection) {
152 g_critical ("Error getting a connection to the session bus: %s",
153 error->message);
154 g_error_free (error);
155 exit (1);
158 details->bus_proxy = dbus_g_proxy_new_for_name (details->connection,
159 DBUS_SERVICE_DBUS,
160 DBUS_PATH_DBUS,
161 DBUS_INTERFACE_DBUS);
163 /* register the object */
164 dbus_g_connection_register_g_object (details->connection,
165 MM_DBUS_MANAGER_PATH,
166 G_OBJECT (manager));
169 static void
170 mm_dbus_manager_init (MMDBusManager *manager)
172 MMDBusManagerDetails *details = manager->details = MM_DBUS_MANAGER_GET_PRIVATE (manager);
174 details->application_names = g_ptr_array_new ();
175 details->application_paths = g_ptr_array_new ();
176 register_on_the_bus (manager);
177 activate_applications (manager);
180 /* implementation of dbus methods */
182 gboolean
183 mm_dbus_manager_register_app (MMDBusManager *manager, char *name,
184 char *path, GError **error)
186 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
188 if (path == NULL) {
189 /* set the error, the path can't be NULL */
190 g_set_error (error, MM_DBUS_ERROR_QUARK,
191 MM_DBUS_ERROR_NULL_ATTRIBUTE,
192 "Error: can't register an app with NULL path");
193 return FALSE;
195 if (name == NULL) {
196 /* set the error, the path can't be NULL */
197 g_set_error (error, MM_DBUS_ERROR_QUARK,
198 MM_DBUS_ERROR_NULL_ATTRIBUTE,
199 "Error: can't register an app with NULL name");
200 return FALSE;
203 g_ptr_array_add (manager->details->application_names,
204 g_strdup (name));
205 g_ptr_array_add (manager->details->application_paths,
206 g_strdup (path));
207 return TRUE;
210 gboolean
211 mm_dbus_manager_get_registered_apps (MMDBusManager *manager,
212 char ***app_names,
213 char ***app_paths,
214 GError **error)
216 int index;
217 int size = manager->details->application_paths->len;
218 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
220 if (app_paths == NULL || app_names == NULL) {
221 /* set the error, the client is doing something wrong */
222 g_set_error (error, MM_DBUS_ERROR_QUARK,
223 MM_DBUS_ERROR_NULL_ATTRIBUTE,
224 "Error: pointers passed to GetRegisteredApps can't be NULL");
225 return FALSE;
228 *app_paths = g_new (gchar *, size + 1);
229 (*app_paths) [size] = NULL;
230 *app_names = g_new (gchar *, size + 1);
231 (*app_names) [size] = NULL;
233 for (index = 0; index < size; index++) {
234 (*app_paths) [index] =
235 g_strdup (g_ptr_array_index (manager->details->application_paths, index));
236 (*app_names) [index] =
237 g_strdup (g_ptr_array_index (manager->details->application_names, index));
240 return TRUE;