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, MMManagerDetails))
34 MMManager
* mm_manager_singleton
= NULL
;
36 struct _MMManagerDetails
{
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 (MMManagerDetails
));
60 insert_dbus_application (GHashTable
*applications
,
61 DBusGConnection
*connection
,
65 DBusGProxy
*app_proxy
;
67 char *desktop_id
= NULL
;
72 app_proxy
= dbus_g_proxy_new_for_name (connection
,
74 "org.gnome.MediaManager.Application");
75 res
= dbus_g_proxy_call (app_proxy
,
85 g_warning ("Unable to get application info for the app at path %s: %s", path
,
87 g_object_unref (app_proxy
);
92 app
= mm_dbus_application_new (desktop_id
, supported_type
,
94 g_hash_table_insert (applications
,
95 g_strdup (name
), /* key */
98 g_object_unref (app_proxy
);
102 insert_dbus_applications (GHashTable
*applications
)
104 DBusGConnection
*connection
;
105 DBusGProxy
*dbus_manager_proxy
;
106 GError
*error
= NULL
;
108 char **registered_names
= NULL
;
109 char **registered_paths
= NULL
;
112 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
, &error
);
114 g_warning ("Error while connecting to the session bus: %s", error
->message
);
115 g_error_free (error
);
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
,
133 g_warning ("Error while calling GetRegisteredApps on the dbus manager: %s",
135 g_object_unref (dbus_manager_proxy
);
136 g_error_free (error
);
140 if ((total_apps
= G_N_ELEMENTS (registered_names
)) != G_N_ELEMENTS (registered_paths
)) {
141 /* wtf, something is broken here, we'd better return */
145 for (idx
= 0; idx
< total_apps
; idx
++) {
146 insert_dbus_application (applications
, connection
,
147 registered_names
[idx
], registered_paths
[idx
]);
151 g_strfreev (registered_names
);
152 g_strfreev (registered_paths
);
153 g_object_unref (dbus_manager_proxy
);
157 populate_applications_table (MMManager
*manager
)
159 GHashTable
*applications
= manager
->details
->applications
;
160 GList
*app_providers
, *l
;
161 MMApplication
*application
;
162 MMApplicationProvider
*provider
;
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
);
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
,
189 populate_applications_table (manager
);
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
;
205 mm_manager_get_application_list_for_type (MMManager
*m
,
206 MMApplicationType type
)
209 GList
*app_list
= NULL
;
210 MMApplicationType supported_type
;
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
) {
223 "supported-type", &supported_type
,
225 if (supported_type
== type
) {
226 app_list
= g_list_prepend (app_list
, app
);
230 g_list_free (all_apps
);
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
);
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
;