Edit data/Makefile.am, Makefile.am and configure.ac to pass make dist.
[mmediamanager.git] / src / mm-manager.c
blob7ad40b58136e896fb63ccc1efc1c30ffe6557b31
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 g_list_free (app_providers);
177 /* now insert all the application registered on the DBus manager */
178 insert_dbus_applications (applications);
181 static void
182 mm_manager_init (MMManager *manager)
184 MMManagerDetails *details = manager->details = MM_MANAGER_GET_PRIVATE (manager);
186 details->module_manager = MM_MODULE_MANAGER (g_object_new (MM_TYPE_MODULE_MANAGER, NULL));
187 details->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
188 (GDestroyNotify) g_free,
189 NULL);
191 populate_applications_table (manager);
194 /* public methods */
196 MMManager *
197 mm_manager_get (void)
199 if (!mm_manager_singleton) {
200 mm_manager_singleton = MM_MANAGER (g_object_new (MM_TYPE_MANAGER, NULL));
203 return mm_manager_singleton;
206 GList *
207 mm_manager_get_application_list_for_type (MMManager *m,
208 MMApplicationType type)
210 GList *all_apps, *l;
211 GList *app_list = NULL;
212 MMApplicationType supported_type;
213 MMApplication *app;
215 g_return_val_if_fail (m != NULL, NULL);
216 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
218 /* this should be fine anyway, as the hash table is created only when
219 * initializing the manager.
221 all_apps = g_hash_table_get_values (m->details->applications);
222 for (l = all_apps; l; l = l->next) {
223 app = l->data;
224 g_object_get (app,
225 "supported-type", &supported_type,
226 NULL);
227 if (supported_type == type) {
228 app_list = g_list_prepend (app_list, app);
232 g_list_free (all_apps);
234 return app_list;
237 GList *
238 mm_manager_get_application_list (MMManager *m)
240 g_return_val_if_fail (m != NULL, NULL);
241 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
243 /* see the comment in _get_application_list_for_type () */
244 return g_hash_table_get_values (m->details->applications);
247 MMModuleManager *
248 mm_manager_get_module_manager (MMManager *m)
250 g_return_val_if_fail (m != NULL, NULL);
251 g_return_val_if_fail (MM_IS_MANAGER (m), NULL);
253 return m->details->module_manager;