Keep on documenting objects.
[mmediamanager.git] / src / mm-dbus-manager.c
blob8a777df8cfcc627c2df4bec07f5f50dbfe16c2c2
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>
27 #include "libmmanager/mm-error.h"
28 #include "libmmanager/mm-type-builtins.h"
29 #include "mm-dbus-manager.h"
31 gboolean mm_dbus_manager_register_app (MMDBusManager *manager, char *path,
32 char *name, GError **error);
33 gboolean mm_dbus_manager_get_registered_apps (MMDBusManager *manager, char ***app_paths,
34 char ***app_names, GError **error);
36 #include "mm-dbus-manager-server-bindings.h"
38 #define MM_DBUS_MANAGER_GET_PRIVATE(o) \
39 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_MANAGER, MMDBusManagerDetails))
41 struct _MMDBusManagerDetails {
42 GPtrArray *application_names;
43 GPtrArray *application_paths;
44 DBusGProxy *bus_proxy;
45 DBusGConnection *connection;
48 G_DEFINE_TYPE (MMDBusManager, mm_dbus_manager, G_TYPE_OBJECT);
50 static void
51 mm_dbus_manager_finalize (GObject *o)
53 MMDBusManager *m = MM_DBUS_MANAGER (o);
55 g_ptr_array_foreach (m->details->application_paths,
56 (GFunc) g_free,
57 NULL);
58 g_ptr_array_free (m->details->application_paths, TRUE);
60 if (m->details->bus_proxy) {
61 g_object_unref (m->details->bus_proxy);
64 G_OBJECT_CLASS (mm_dbus_manager_parent_class)->finalize (o);
67 static void
68 mm_dbus_manager_class_init (MMDBusManagerClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 object_class->finalize = mm_dbus_manager_finalize;
74 dbus_g_object_type_install_info (MM_TYPE_DBUS_MANAGER,
75 &dbus_glib_mm_dbus_manager_object_info);
76 dbus_g_error_domain_register (MM_DBUS_ERROR_QUARK, NULL, MM_TYPE_MD_BUS_ERROR_ENUM);
78 g_type_class_add_private (klass, sizeof (MMDBusManagerDetails));
81 static void
82 activate_applications (MMDBusManager *manager)
84 GDir *dir;
85 const char *filename;
86 const char *dirname;
87 GError *error = NULL;
89 dirname = MMEDIAMANAGER_EXTENSIONDIR"/dbus";
90 dir = g_dir_open (dirname, 0, &error);
91 if (!dir) {
92 g_warning ("Unable to open the DBus extensions directory %s: %s",
93 dirname, error->message);
94 g_error_free (error);
95 return;
98 while ((filename = g_dir_read_name (dir)) != NULL) {
99 char *path;
100 GKeyFile *keyfile;
101 char *executable;
103 path = g_build_filename (dirname, filename, NULL);
104 keyfile = g_key_file_new ();
105 if (!g_key_file_load_from_file (keyfile, path, 0, &error)) {
106 g_warning ("Unable to load extension descriptor %s: %s", path,
107 error->message);
108 g_error_free (error);
109 error = NULL;
110 g_key_file_free (keyfile);
111 g_free (path);
113 /* move on to the next file */
114 continue;
117 /* we are looking for the HelperPath line under the MediaManager group */
118 executable = g_key_file_get_string (keyfile,
119 "MediaManager",
120 "HelperPath",
121 &error);
122 if (!executable) {
123 g_warning ("Descriptor %s is malformed: %s", path, error->message);
124 g_error_free (error);
125 error = NULL;
126 g_key_file_free (keyfile);
127 g_free (path);
129 continue;
132 if (!g_spawn_command_line_async (executable, &error)) {
133 g_warning ("Unable to spawn the extension %s: %s", executable,
134 error->message);
135 g_error_free (error);
136 error = NULL;
139 g_key_file_free (keyfile);
140 g_free (path);
141 g_free (executable);
145 static void
146 register_on_the_bus (MMDBusManager *manager)
148 GError *error = NULL;
149 MMDBusManagerDetails *details = manager->details;
151 details->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
152 if (!details->connection) {
153 g_critical ("Error getting a connection to the session bus: %s",
154 error->message);
155 g_error_free (error);
156 exit (1);
159 details->bus_proxy = dbus_g_proxy_new_for_name (details->connection,
160 DBUS_SERVICE_DBUS,
161 DBUS_PATH_DBUS,
162 DBUS_INTERFACE_DBUS);
164 /* register the object */
165 dbus_g_connection_register_g_object (details->connection,
166 MM_DBUS_MANAGER_PATH,
167 G_OBJECT (manager));
170 static void
171 mm_dbus_manager_init (MMDBusManager *manager)
173 MMDBusManagerDetails *details = manager->details = MM_DBUS_MANAGER_GET_PRIVATE (manager);
175 details->application_names = g_ptr_array_new ();
176 details->application_paths = g_ptr_array_new ();
177 register_on_the_bus (manager);
178 activate_applications (manager);
181 /* implementation of dbus methods */
183 gboolean
184 mm_dbus_manager_register_app (MMDBusManager *manager, char *name,
185 char *path, GError **error)
187 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
189 if (path == NULL) {
190 /* set the error, the path can't be NULL */
191 g_set_error (error, MM_DBUS_ERROR_QUARK,
192 MM_DBUS_ERROR_NULL_ATTRIBUTE,
193 "Error: can't register an app with NULL path");
194 return FALSE;
196 if (name == NULL) {
197 /* set the error, the path can't be NULL */
198 g_set_error (error, MM_DBUS_ERROR_QUARK,
199 MM_DBUS_ERROR_NULL_ATTRIBUTE,
200 "Error: can't register an app with NULL name");
201 return FALSE;
204 g_ptr_array_add (manager->details->application_names,
205 g_strdup (name));
206 g_ptr_array_add (manager->details->application_paths,
207 g_strdup (path));
208 return TRUE;
211 gboolean
212 mm_dbus_manager_get_registered_apps (MMDBusManager *manager,
213 char ***app_names,
214 char ***app_paths,
215 GError **error)
217 int index;
218 int size = manager->details->application_paths->len;
219 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
221 if (app_paths == NULL || app_names == NULL) {
222 /* set the error, the client is doing something wrong */
223 g_set_error (error, MM_DBUS_ERROR_QUARK,
224 MM_DBUS_ERROR_NULL_ATTRIBUTE,
225 "Error: pointers passed to GetRegisteredApps can't be NULL");
226 return FALSE;
229 *app_paths = g_new (gchar *, size + 1);
230 (*app_paths) [size] = NULL;
231 *app_names = g_new (gchar *, size + 1);
232 (*app_names) [size] = NULL;
234 for (index = 0; index < size; index++) {
235 (*app_paths) [index] =
236 g_strdup (g_ptr_array_index (manager->details->application_paths, index));
237 (*app_names) [index] =
238 g_strdup (g_ptr_array_index (manager->details->application_names, index));
241 return TRUE;