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>
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
;
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
);
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
));
60 insert_dbus_application (GHashTable
*applications
,
61 DBusGConnection
*connection
,
65 DBusGProxy
*app_proxy
;
67 char *desktop_id
= 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
,
78 "org.gnome.MediaManager.Application");
79 res
= dbus_g_proxy_call (app_proxy
,
89 g_warning ("Unable to get application info for the app at path %s: %s", path
,
91 g_object_unref (app_proxy
);
96 app
= mm_dbus_application_new (desktop_id
, supported_type
,
98 g_hash_table_insert (applications
,
99 g_strdup (name
), /* key */
102 g_object_unref (app_proxy
);
106 insert_dbus_applications (GHashTable
*applications
)
108 DBusGConnection
*connection
;
109 DBusGProxy
*dbus_manager_proxy
;
110 GError
*error
= NULL
;
112 char **registered_names
= NULL
;
113 char **registered_paths
= NULL
;
116 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
, &error
);
118 g_warning ("Error while connecting to the session bus: %s", error
->message
);
119 g_error_free (error
);
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
,
137 g_warning ("Error while calling GetRegisteredApps on the dbus manager: %s",
139 g_object_unref (dbus_manager_proxy
);
140 g_error_free (error
);
144 if ((total_apps
= G_N_ELEMENTS (registered_names
)) != G_N_ELEMENTS (registered_paths
)) {
145 /* wtf, something is broken here, we'd better return */
149 if (total_apps
== 1) {
150 /* array is NULL-terminated, so in this case there are no
156 for (idx
= 0; idx
< total_apps
- 1; idx
++) {
157 insert_dbus_application (applications
, connection
,
158 registered_names
[idx
], registered_paths
[idx
]);
162 g_strfreev (registered_names
);
163 g_strfreev (registered_paths
);
164 g_object_unref (dbus_manager_proxy
);
168 populate_applications_table (MMManager
*manager
)
170 GHashTable
*applications
= manager
->details
->applications
;
171 GList
*app_providers
, *l
;
172 MMApplication
*application
;
173 MMApplicationProvider
*provider
;
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
);
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
,
202 populate_applications_table (manager
);
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.
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
238 mm_manager_get_application_list_for_type (MMManager
*manager
,
239 MMApplicationType type
)
242 GList
*app_list
= NULL
;
243 MMApplicationType supported_type
;
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
) {
256 "supported-type", &supported_type
,
258 if (supported_type
== type
) {
259 app_list
= g_list_prepend (app_list
, app
);
263 g_list_free (all_apps
);
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
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.
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
;