Validate args before creating the DBus proxy, just for complete safety.
[mmediamanager.git] / src / mm-manager.c
blob4bde96637b6bc3c79725f71d5670fbc14da0261d
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 <glib-object.h>
22 #include <glib.h>
23 #include <dbus/dbus-glib.h>
24 #include "mm-application.h"
25 #include "mm-dbus-application.h"
27 #include "mm-manager.h"
29 G_DEFINE_TYPE (MMManager, mm_manager, G_TYPE_OBJECT);
31 #define MM_MANAGER_GET_PRIVATE(o) \
32 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_MANAGER, MMManagerDetails))
34 MMManager* mm_manager_singleton = NULL;
36 struct _MMManagerDetails {
37 MMModuleManager *module_manager;
38 GHashTable *applications;
41 static void
42 mm_manager_finalize (GObject *o)
44 MMManager *manager = MM_MANAGER (o);
46 g_hash_table_destroy (manager->details->applications);
48 G_OBJECT_CLASS (mm_manager_parent_class)->finalize (o);
51 static void
52 mm_manager_class_init (MMManagerClass *klass)
54 G_OBJECT_CLASS (klass)->finalize = mm_manager_finalize;
56 g_type_class_add_private (klass, sizeof (MMManagerDetails));
59 static void
60 insert_dbus_application (GHashTable *applications,
61 DBusGConnection *connection,
62 const char *name,
63 const char *path)
65 DBusGProxy *app_proxy;
66 guint supported_type;
67 char *desktop_id = NULL;
68 gboolean res;
69 MMApplication *app;
70 GError *error = NULL;
72 /* validate our args */
73 g_return_if_fail (name != NULL);
74 g_return_if_fail (path != NULL);
76 app_proxy = dbus_g_proxy_new_for_name (connection,
77 name, path,
78 "org.gnome.MediaManager.Application");
79 res = dbus_g_proxy_call (app_proxy,
80 "GetApplicationInfo",
81 &error,
82 G_TYPE_INVALID,
83 G_TYPE_UINT,
84 &supported_type,
85 G_TYPE_STRING,
86 &desktop_id,
87 G_TYPE_INVALID);
88 if (!res) {
89 g_warning ("Unable to get application info for the app at path %s: %s", path,
90 error->message);
91 g_object_unref (app_proxy);
92 g_error_free (error);
93 return;
96 app = mm_dbus_application_new (desktop_id, supported_type,
97 name, path);
98 g_hash_table_insert (applications,
99 g_strdup (name), /* key */
100 app); /* value */
101 g_free (desktop_id);
102 g_object_unref (app_proxy);
105 static void
106 insert_dbus_applications (GHashTable *applications)
108 DBusGConnection *connection;
109 DBusGProxy *dbus_manager_proxy;
110 GError *error = NULL;
111 gboolean res;
112 char **registered_names = NULL;
113 char **registered_paths = NULL;
114 int idx, total_apps;
116 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
117 if (error) {
118 g_warning ("Error while connecting to the session bus: %s", error->message);
119 g_error_free (error);
120 return;
123 dbus_manager_proxy = dbus_g_proxy_new_for_name (connection,
124 "org.gnome.MediaManager",
125 "/org/gnome/MediaManager/Manager",
126 "org.gnome.MediaManager.Manager");
127 res = dbus_g_proxy_call (dbus_manager_proxy,
128 "GetRegisteredApps",
129 &error,
130 G_TYPE_INVALID,
131 G_TYPE_STRV,
132 &registered_names,
133 G_TYPE_STRV,
134 &registered_paths,
135 G_TYPE_INVALID);
136 if (!res) {
137 g_warning ("Error while calling GetRegisteredApps on the dbus manager: %s",
138 error->message);
139 g_object_unref (dbus_manager_proxy);
140 g_error_free (error);
141 return;
144 if ((total_apps = G_N_ELEMENTS (registered_names)) != G_N_ELEMENTS (registered_paths)) {
145 /* wtf, something is broken here, we'd better return */
146 goto out;
149 if (total_apps == 1) {
150 /* array is NULL-terminated, so in this case there are no
151 * applications.
153 goto out;
156 for (idx = 0; idx < total_apps - 1; idx++) {
157 insert_dbus_application (applications, connection,
158 registered_names[idx], registered_paths[idx]);
161 out:
162 g_strfreev (registered_names);
163 g_strfreev (registered_paths);
164 g_object_unref (dbus_manager_proxy);
167 static void
168 populate_applications_table (MMManager *manager)
170 GHashTable *applications = manager->details->applications;
171 GList *app_providers, *l;
172 MMApplication *application;
173 MMApplicationProvider *provider;
174 const char *id;
176 app_providers = mm_module_manager_get_all_application_providers (manager->details->module_manager);
177 for (l = app_providers; l; l = l->next) {
178 provider = MM_APPLICATION_PROVIDER (l->data);
179 application = mm_application_provider_get_application (provider);
180 id = mm_application_get_id (application);
181 g_hash_table_insert (applications,
182 g_strdup (id), /* key */
183 application); /* value */
186 g_list_free (app_providers);
188 /* now insert all the application registered on the DBus manager */
189 insert_dbus_applications (applications);
192 static void
193 mm_manager_init (MMManager *manager)
195 MMManagerDetails *details = manager->details = MM_MANAGER_GET_PRIVATE (manager);
197 details->module_manager = MM_MODULE_MANAGER (g_object_new (MM_TYPE_MODULE_MANAGER, NULL));
198 details->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
199 (GDestroyNotify) g_free,
200 NULL);
202 populate_applications_table (manager);
205 /* public methods */
207 MMManager *
208 mm_manager_get (void)
210 if (!mm_manager_singleton) {
211 mm_manager_singleton = MM_MANAGER (g_object_new (MM_TYPE_MANAGER, NULL));
214 return mm_manager_singleton;
217 GList *
218 mm_manager_get_application_list_for_type (MMManager *m,
219 MMApplicationType type)
221 GList *all_apps, *l;
222 GList *app_list = NULL;
223 MMApplicationType supported_type;
224 MMApplication *app;
226 g_return_val_if_fail (m != NULL, NULL);
227 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
229 /* this should be fine anyway, as the hash table is created only when
230 * initializing the manager.
232 all_apps = g_hash_table_get_values (m->details->applications);
233 for (l = all_apps; l; l = l->next) {
234 app = l->data;
235 g_object_get (app,
236 "supported-type", &supported_type,
237 NULL);
238 if (supported_type == type) {
239 app_list = g_list_prepend (app_list, app);
243 g_list_free (all_apps);
245 return app_list;
248 GList *
249 mm_manager_get_application_list (MMManager *m)
251 g_return_val_if_fail (m != NULL, NULL);
252 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
254 /* see the comment in _get_application_list_for_type () */
255 return g_hash_table_get_values (m->details->applications);
258 MMModuleManager *
259 mm_manager_get_module_manager (MMManager *m)
261 g_return_val_if_fail (m != NULL, NULL);
262 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
264 return m->details->module_manager;