Fix some bugs in the D-Bus code.
[mmediamanager.git] / src / mm-manager.c
blob5a4a41664126abc479a1f45f7c5ef591fce8a694
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 g_debug ("insert dbus app: name %s path %s", name, path);
74 app_proxy = dbus_g_proxy_new_for_name (connection,
75 name, path,
76 "org.gnome.MediaManager.Application");
77 res = dbus_g_proxy_call (app_proxy,
78 "GetApplicationInfo",
79 &error,
80 G_TYPE_INVALID,
81 G_TYPE_UINT,
82 &supported_type,
83 G_TYPE_STRING,
84 &desktop_id,
85 G_TYPE_INVALID);
86 if (!res) {
87 g_warning ("Unable to get application info for the app at path %s: %s", path,
88 error->message);
89 g_object_unref (app_proxy);
90 g_error_free (error);
91 return;
94 app = mm_dbus_application_new (desktop_id, supported_type,
95 name, path);
96 g_hash_table_insert (applications,
97 g_strdup (name), /* key */
98 app); /* value */
99 g_free (desktop_id);
100 g_object_unref (app_proxy);
103 static void
104 insert_dbus_applications (GHashTable *applications)
106 DBusGConnection *connection;
107 DBusGProxy *dbus_manager_proxy;
108 GError *error = NULL;
109 gboolean res;
110 char **registered_names = NULL;
111 char **registered_paths = NULL;
112 int idx, total_apps;
114 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
115 if (error) {
116 g_warning ("Error while connecting to the session bus: %s", error->message);
117 g_error_free (error);
118 return;
121 dbus_manager_proxy = dbus_g_proxy_new_for_name (connection,
122 "org.gnome.MediaManager",
123 "/org/gnome/MediaManager/Manager",
124 "org.gnome.MediaManager.Manager");
125 res = dbus_g_proxy_call (dbus_manager_proxy,
126 "GetRegisteredApps",
127 &error,
128 G_TYPE_INVALID,
129 G_TYPE_STRV,
130 &registered_names,
131 G_TYPE_STRV,
132 &registered_paths,
133 G_TYPE_INVALID);
134 if (!res) {
135 g_warning ("Error while calling GetRegisteredApps on the dbus manager: %s",
136 error->message);
137 g_object_unref (dbus_manager_proxy);
138 g_error_free (error);
139 return;
142 if ((total_apps = G_N_ELEMENTS (registered_names)) != G_N_ELEMENTS (registered_paths)) {
143 /* wtf, something is broken here, we'd better return */
144 goto out;
147 if (total_apps == 1) {
148 /* array is NULL-terminated, so in this case there are no
149 * applications.
151 goto out;
154 for (idx = 0; idx < total_apps - 1; idx++) {
155 insert_dbus_application (applications, connection,
156 registered_names[idx], registered_paths[idx]);
159 out:
160 g_strfreev (registered_names);
161 g_strfreev (registered_paths);
162 g_object_unref (dbus_manager_proxy);
165 static void
166 populate_applications_table (MMManager *manager)
168 GHashTable *applications = manager->details->applications;
169 GList *app_providers, *l;
170 MMApplication *application;
171 MMApplicationProvider *provider;
172 const char *id;
174 app_providers = mm_module_manager_get_all_application_providers (manager->details->module_manager);
175 for (l = app_providers; l; l = l->next) {
176 provider = MM_APPLICATION_PROVIDER (l->data);
177 application = mm_application_provider_get_application (provider);
178 id = mm_application_get_id (application);
179 g_hash_table_insert (applications,
180 g_strdup (id), /* key */
181 application); /* value */
184 g_list_free (app_providers);
186 /* now insert all the application registered on the DBus manager */
187 insert_dbus_applications (applications);
190 static void
191 mm_manager_init (MMManager *manager)
193 MMManagerDetails *details = manager->details = MM_MANAGER_GET_PRIVATE (manager);
195 details->module_manager = MM_MODULE_MANAGER (g_object_new (MM_TYPE_MODULE_MANAGER, NULL));
196 details->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
197 (GDestroyNotify) g_free,
198 NULL);
200 populate_applications_table (manager);
203 /* public methods */
205 MMManager *
206 mm_manager_get (void)
208 if (!mm_manager_singleton) {
209 mm_manager_singleton = MM_MANAGER (g_object_new (MM_TYPE_MANAGER, NULL));
212 return mm_manager_singleton;
215 GList *
216 mm_manager_get_application_list_for_type (MMManager *m,
217 MMApplicationType type)
219 GList *all_apps, *l;
220 GList *app_list = NULL;
221 MMApplicationType supported_type;
222 MMApplication *app;
224 g_return_val_if_fail (m != NULL, NULL);
225 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
227 /* this should be fine anyway, as the hash table is created only when
228 * initializing the manager.
230 all_apps = g_hash_table_get_values (m->details->applications);
231 for (l = all_apps; l; l = l->next) {
232 app = l->data;
233 g_object_get (app,
234 "supported-type", &supported_type,
235 NULL);
236 if (supported_type == type) {
237 app_list = g_list_prepend (app_list, app);
241 g_list_free (all_apps);
243 return app_list;
246 GList *
247 mm_manager_get_application_list (MMManager *m)
249 g_return_val_if_fail (m != NULL, NULL);
250 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
252 /* see the comment in _get_application_list_for_type () */
253 return g_hash_table_get_values (m->details->applications);
256 MMModuleManager *
257 mm_manager_get_module_manager (MMManager *m)
259 g_return_val_if_fail (m != NULL, NULL);
260 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
262 return m->details->module_manager;