Update MMManager to list applications provided by DBus.
[mmediamanager.git] / src / mm-manager.c
blob806b45821434921a569a4e0df82bad916a53fc47
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 app_proxy = dbus_g_proxy_new_for_name (connection,
73 name, path,
74 "org.gnome.MediaManager.Application");
75 res = dbus_g_proxy_call (app_proxy,
76 "GetApplicationInfo",
77 &error,
78 G_TYPE_INVALID,
79 G_TYPE_UINT,
80 &supported_type,
81 G_TYPE_STRING,
82 &desktop_id,
83 G_TYPE_INVALID);
84 if (!res) {
85 g_warning ("Unable to get application info for the app at path %s: %s", path,
86 error->message);
87 g_object_unref (app_proxy);
88 g_error_free (error);
89 return;
92 app = mm_dbus_application_new (desktop_id, supported_type,
93 name, path);
94 g_hash_table_insert (applications,
95 g_strdup (name), /* key */
96 app); /* value */
97 g_free (desktop_id);
98 g_object_unref (app_proxy);
101 static void
102 insert_dbus_applications (GHashTable *applications)
104 DBusGConnection *connection;
105 DBusGProxy *dbus_manager_proxy;
106 GError *error = NULL;
107 gboolean res;
108 char **registered_names = NULL;
109 char **registered_paths = NULL;
110 int idx, total_apps;
112 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
113 if (error) {
114 g_warning ("Error while connecting to the session bus: %s", error->message);
115 g_error_free (error);
116 return;
119 dbus_manager_proxy = dbus_g_proxy_new_for_name (connection,
120 "org.gnome.MediaManager.Manager",
121 "/org/gnome/MediaManager/Manager",
122 "org.gnome.MediaManager.Manager");
123 res = dbus_g_proxy_call (dbus_manager_proxy,
124 "GetRegisteredApps",
125 &error,
126 G_TYPE_INVALID,
127 G_TYPE_STRV,
128 &registered_names,
129 G_TYPE_STRV,
130 &registered_paths,
131 G_TYPE_INVALID);
132 if (!res) {
133 g_warning ("Error while calling GetRegisteredApps on the dbus manager: %s",
134 error->message);
135 g_object_unref (dbus_manager_proxy);
136 g_error_free (error);
137 return;
140 if ((total_apps = G_N_ELEMENTS (registered_names)) != G_N_ELEMENTS (registered_paths)) {
141 /* wtf, something is broken here, we'd better return */
142 goto out;
145 for (idx = 0; idx < total_apps; idx++) {
146 insert_dbus_application (applications, connection,
147 registered_names[idx], registered_paths[idx]);
150 out:
151 g_strfreev (registered_names);
152 g_strfreev (registered_paths);
153 g_object_unref (dbus_manager_proxy);
156 static void
157 populate_applications_table (MMManager *manager)
159 GHashTable *applications = manager->details->applications;
160 GList *app_providers, *l;
161 MMApplication *application;
162 MMApplicationProvider *provider;
163 const char *id;
165 app_providers = mm_module_manager_get_all_application_providers (manager->details->module_manager);
166 for (l = app_providers; l; l = l->next) {
167 provider = MM_APPLICATION_PROVIDER (l->data);
168 application = mm_application_provider_get_application (provider);
169 id = mm_application_get_id (application);
170 g_hash_table_insert (applications,
171 g_strdup (id), /* key */
172 application); /* value */
175 /* now insert all the application registered on the DBus manager */
176 insert_dbus_applications (applications);
179 static void
180 mm_manager_init (MMManager *manager)
182 MMManagerDetails *details = manager->details = MM_MANAGER_GET_PRIVATE (manager);
184 details->module_manager = MM_MODULE_MANAGER (g_object_new (MM_TYPE_MODULE_MANAGER, NULL));
185 details->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
186 (GDestroyNotify) g_free,
187 NULL);
189 populate_applications_table (manager);
192 /* public methods */
194 MMManager *
195 mm_manager_get (void)
197 if (!mm_manager_singleton) {
198 mm_manager_singleton = MM_MANAGER (g_object_new (MM_TYPE_MANAGER, NULL));
201 return mm_manager_singleton;
204 GList *
205 mm_manager_get_application_list_for_type (MMManager *m,
206 MMApplicationType type)
208 GList *all_apps, *l;
209 GList *app_list = NULL;
210 MMApplicationType supported_type;
211 MMApplication *app;
213 g_return_val_if_fail (m != NULL, NULL);
214 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
216 /* this should be fine anyway, as the hash table is created only when
217 * initializing the manager.
219 all_apps = g_hash_table_get_values (m->details->applications);
220 for (l = all_apps; l; l = l->next) {
221 app = l->data;
222 g_object_get (app,
223 "supported-type", &supported_type,
224 NULL);
225 if (supported_type == type) {
226 app_list = g_list_prepend (app_list, app);
230 g_list_free (all_apps);
232 return app_list;
235 GList *
236 mm_manager_get_application_list (MMManager *m)
238 g_return_val_if_fail (m != NULL, NULL);
239 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
241 /* see the comment in _get_application_list_for_type () */
242 return g_hash_table_get_values (m->details->applications);
245 MMModuleManager *
246 mm_manager_get_module_manager (MMManager *m)
248 g_return_val_if_fail (m != NULL, NULL);
249 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
251 return m->details->module_manager;