Add doc files to dist and fix version number in NEWS.
[mmediamanager.git] / libmmanager / mm-manager.c
blob5f6131417497b2738533892a3e898615a36605ed
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, MMManagerPrivate))
34 MMManager* mm_manager_singleton = NULL;
36 struct _MMManagerPrivate {
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 (MMManagerPrivate));
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 MMManagerPrivate *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 */
208 * mm_manager_get:
210 * Gets the #MMManager singleton instance.
212 * Return value: a #MMManager object. The object is owned by the library and
213 * you should not modify or unref it.
216 MMManager *
217 mm_manager_get (void)
219 if (!mm_manager_singleton) {
220 mm_manager_singleton = MM_MANAGER (g_object_new (MM_TYPE_MANAGER, NULL));
223 return mm_manager_singleton;
227 * mm_manager_get_application_list_for_type:
228 * @manager: a #MMManager.
229 * @type: the media type you're interested in.
231 * Gets a #GList of #MMApplication objects supporting the specified type.
233 * Return value: a #GList of #MMApplication objects. Use #g_list_free when
234 * done with it.
237 GList *
238 mm_manager_get_application_list_for_type (MMManager *manager,
239 MMApplicationType type)
241 GList *all_apps, *l;
242 GList *app_list = NULL;
243 MMApplicationType supported_type;
244 MMApplication *app;
246 g_return_val_if_fail (manager != NULL, NULL);
247 g_return_val_if_fail (MM_IS_MANAGER (manager), NULL);
249 /* this should be fine anyway, as the hash table is created only when
250 * initializing the manager.
252 all_apps = g_hash_table_get_values (manager->details->applications);
253 for (l = all_apps; l; l = l->next) {
254 app = l->data;
255 g_object_get (app,
256 "supported-type", &supported_type,
257 NULL);
258 if (supported_type == type) {
259 app_list = g_list_prepend (app_list, app);
263 g_list_free (all_apps);
265 return app_list;
269 * mm_manager_get_application_list:
270 * @manager: a #MMManager.
272 * Gets a list of all the #MMApplication objects known to the manager.
274 * Return value: a #GList of #MMApplication objects. Use #g_list_free when done
275 * with it.
278 GList *
279 mm_manager_get_application_list (MMManager *manager)
281 g_return_val_if_fail (manager != NULL, NULL);
282 g_return_val_if_fail (MM_IS_MANAGER (manager), NULL);
284 /* see the comment in _get_application_list_for_type () */
285 return g_hash_table_get_values (manager->details->applications);
289 * mm_manager_get_module_manager:
290 * @manager: a #MMManager.
292 * Gets the #MMModuleManager singleton instance.
294 * Return value: a #MMModuleManager. The object is owned by the library and
295 * you should not unref or modify it.
298 MMModuleManager *
299 mm_manager_get_module_manager (MMManager *manager)
301 g_return_val_if_fail (manager != NULL, NULL);
302 g_return_val_if_fail (MM_IS_MANAGER (manager), NULL);
304 return manager->details->module_manager;