Update the DBus manager and the tests to send/return names and paths
[mmediamanager.git] / src / mm-dbus-manager.c
blob59ae06ed9743cc462c009533e5f155f7be065580
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-dbus-manager.h"
27 #include "mm-dbus-manager-type-builtins.h"
29 gboolean mm_dbus_manager_register_app (MMDBusManager *manager, char *path,
30 char *name, GError **error);
31 gboolean mm_dbus_manager_get_registered_apps (MMDBusManager *manager, char ***app_paths,
32 char ***app_names, GError **error);
34 #include "mm-dbus-manager-server-bindings.h"
36 #define MM_DBUS_MANAGER_GET_PRIVATE(o) \
37 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_MANAGER, MMDBusManagerDetails))
39 struct _MMDBusManagerDetails {
40 GPtrArray *application_names;
41 GPtrArray *application_paths;
42 DBusGProxy *bus_proxy;
43 DBusGConnection *connection;
46 G_DEFINE_TYPE (MMDBusManager, mm_dbus_manager, G_TYPE_OBJECT);
48 static void
49 mm_dbus_manager_finalize (GObject *o)
51 MMDBusManager *m = MM_DBUS_MANAGER (o);
53 g_ptr_array_foreach (m->details->application_paths,
54 (GFunc) g_free,
55 NULL);
56 g_ptr_array_free (m->details->application_paths, TRUE);
58 if (m->details->bus_proxy) {
59 g_object_unref (m->details->bus_proxy);
62 G_OBJECT_CLASS (mm_dbus_manager_parent_class)->finalize (o);
65 static void
66 mm_dbus_manager_class_init (MMDBusManagerClass *klass)
68 GObjectClass *object_class = G_OBJECT_CLASS (klass);
70 object_class->finalize = mm_dbus_manager_finalize;
72 dbus_g_object_type_install_info (MM_TYPE_DBUS_MANAGER,
73 &dbus_glib_mm_dbus_manager_object_info);
74 dbus_g_error_domain_register (MM_DBUS_ERROR_QUARK, NULL, MM_TYPE_MD_BUS_ERROR_ENUM);
76 g_type_class_add_private (klass, sizeof (MMDBusManagerDetails));
79 static void
80 activate_applications (MMDBusManager *manager)
82 GDir *dir;
83 const char *filename;
84 const char *dirname;
85 GError *error = NULL;
87 dirname = "/home/anarki/mmediamanager-extensions/dbus";
88 dir = g_dir_open (dirname, 0, &error);
89 if (!dir) {
90 g_warning ("Unable to open the DBus extensions directory %s: %s",
91 dirname, error->message);
92 g_error_free (error);
93 return;
96 while ((filename = g_dir_read_name (dir)) != NULL) {
97 char *path;
98 GKeyFile *keyfile;
99 char *executable;
101 path = g_build_filename (dirname, filename, NULL);
102 keyfile = g_key_file_new ();
103 if (!g_key_file_load_from_file (keyfile, path, 0, &error)) {
104 g_warning ("Unable to load extension descriptor %s: %s", path,
105 error->message);
106 g_error_free (error);
107 error = NULL;
108 g_key_file_free (keyfile);
109 g_free (path);
111 /* move on to the next file */
112 continue;
115 /* we are looking for the HelperPath line under the MediaManager group */
116 executable = g_key_file_get_string (keyfile,
117 "MediaManager",
118 "HelperPath",
119 &error);
120 if (!executable) {
121 g_warning ("Descriptor %s is malformed: %s", path, error->message);
122 g_error_free (error);
123 error = NULL;
124 g_key_file_free (keyfile);
125 g_free (path);
127 continue;
130 if (!g_spawn_command_line_async (executable, &error)) {
131 g_warning ("Unable to spawn the extension %s: %s", executable,
132 error->message);
133 g_error_free (error);
134 error = NULL;
137 g_key_file_free (keyfile);
138 g_free (path);
139 g_free (executable);
143 static void
144 register_on_the_bus (MMDBusManager *manager)
146 GError *error = NULL;
147 MMDBusManagerDetails *details = manager->details;
149 details->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
150 if (!details->connection) {
151 g_critical ("Error getting a connection to the session bus: %s",
152 error->message);
153 g_error_free (error);
154 exit (1);
157 details->bus_proxy = dbus_g_proxy_new_for_name (details->connection,
158 DBUS_SERVICE_DBUS,
159 DBUS_PATH_DBUS,
160 DBUS_INTERFACE_DBUS);
162 /* register the object */
163 dbus_g_connection_register_g_object (details->connection,
164 MM_DBUS_MANAGER_PATH,
165 G_OBJECT (manager));
168 static void
169 mm_dbus_manager_init (MMDBusManager *manager)
171 MMDBusManagerDetails *details = manager->details = MM_DBUS_MANAGER_GET_PRIVATE (manager);
173 details->application_names = g_ptr_array_new ();
174 details->application_paths = g_ptr_array_new ();
175 register_on_the_bus (manager);
176 activate_applications (manager);
179 /* implementation of dbus methods */
181 gboolean
182 mm_dbus_manager_register_app (MMDBusManager *manager, char *name,
183 char *path, GError **error)
185 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
187 if (path == NULL) {
188 /* set the error, the path can't be NULL */
189 g_set_error (error, MM_DBUS_ERROR_QUARK,
190 MM_DBUS_ERROR_NULL_ATTRIBUTE,
191 "Error: can't register an app with NULL path");
192 return FALSE;
194 if (name == NULL) {
195 /* set the error, the path can't be NULL */
196 g_set_error (error, MM_DBUS_ERROR_QUARK,
197 MM_DBUS_ERROR_NULL_ATTRIBUTE,
198 "Error: can't register an app with NULL name");
199 return FALSE;
202 g_ptr_array_add (manager->details->application_names,
203 g_strdup (name));
204 g_ptr_array_add (manager->details->application_paths,
205 g_strdup (path));
206 return TRUE;
209 gboolean
210 mm_dbus_manager_get_registered_apps (MMDBusManager *manager,
211 char ***app_names,
212 char ***app_paths,
213 GError **error)
215 int index;
216 int size = manager->details->application_paths->len;
217 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
219 if (app_paths == NULL || app_names == NULL) {
220 /* set the error, the client is doing something wrong */
221 g_set_error (error, MM_DBUS_ERROR_QUARK,
222 MM_DBUS_ERROR_NULL_ATTRIBUTE,
223 "Error: pointers passed to GetRegisteredApps can't be NULL");
224 return FALSE;
227 *app_paths = g_new (gchar *, size + 1);
228 (*app_paths) [size] = NULL;
229 *app_names = g_new (gchar *, size + 1);
230 (*app_names) [size] = NULL;
232 for (index = 0; index < size; index++) {
233 (*app_paths) [index] =
234 g_strdup (g_ptr_array_index (manager->details->application_paths, index));
235 (*app_names) [index] =
236 g_strdup (g_ptr_array_index (manager->details->application_names, index));
239 return TRUE;