Pass distcheck! \o/
[mmediamanager.git] / libmmanager / mm-module-manager.c
blob8b7ce3d84f0717e43c7e3d67a63490d360ef7c73
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.h>
22 #include <glib-object.h>
24 #include "mm-module-manager.h"
25 #include "mm-application-provider.h"
26 #include "mm-category-provider.h"
27 #include "mm-hit-collection-provider.h"
28 #include "mm-module.h"
30 #define MM_MODULE_MANAGER_GET_PRIVATE(o) \
31 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_MODULE_MANAGER, MMModuleManagerPrivate))
33 G_DEFINE_TYPE (MMModuleManager, mm_module_manager, G_TYPE_OBJECT);
35 struct _MMModuleManagerPrivate {
36 GList *modules;
37 char *path;
40 static void
41 load_modules (MMModuleManager *manager)
43 MMModuleManagerPrivate *details = manager->details;
44 GDir *dir;
45 GError *error = NULL;
47 dir = g_dir_open (details->path, 0, &error);
48 if (!dir) {
49 g_warning ("Unable to load modules from %s: %s",
50 details->path, error->message);
51 } else {
52 const char *name;
54 while ((name = g_dir_read_name (dir))) {
55 if (g_str_has_suffix (name, "." G_MODULE_SUFFIX)) {
56 char *filename;
57 MMModule *module;
59 filename = g_build_filename (details->path, name, NULL);
60 module = mm_module_load_file (filename);
61 g_free (filename);
63 if (module) {
64 details->modules = g_list_prepend (details->modules, module);
68 g_dir_close (dir);
70 if (error) {
71 g_error_free (error);
75 static void
76 mm_module_manager_finalize (GObject *o)
78 MMModuleManager *manager = MM_MODULE_MANAGER (o);
80 g_free (manager->details->path);
81 g_list_free (manager->details->modules);
83 G_OBJECT_CLASS (mm_module_manager_parent_class)->finalize (o);
86 static void
87 mm_module_manager_init (MMModuleManager *manager)
89 MMModuleManagerPrivate *details = manager->details =
90 MM_MODULE_MANAGER_GET_PRIVATE (manager);
92 details->path = g_strdup (MMEDIAMANAGER_EXTENSIONDIR);
93 details->modules = NULL;
94 load_modules (manager);
97 static void
98 mm_module_manager_class_init (MMModuleManagerClass *klass)
100 G_OBJECT_CLASS (klass)->finalize = mm_module_manager_finalize;
102 g_type_class_add_private (klass, sizeof (MMModuleManagerPrivate));
105 static MMModule *
106 find_module_for_name (MMModuleManager *manager,
107 const char *name)
109 MMModule *module;
110 char *module_name;
111 GList *l;
112 gboolean found = FALSE;
114 for (l = manager->details->modules; l; l = l->next) {
115 module = l->data;
116 module_name = mm_module_get_name (module);
117 if (g_strcmp0 (name, module_name) == 0) {
118 found = TRUE;
120 if (found) {
121 break;
125 if (!found) {
126 g_warning ("Can't find a module named %s", name);
127 module = NULL;
130 return module;
133 /* public methods */
136 * mm_module_manager_get_all_application_providers:
137 * @manager: a #MMModuleManager.
139 * Gets a list of all the #MMApplicationProvider objects known to @manager.
141 * Return value: a #GList of #MMApplicationProvider objects. Use #g_list_free
142 * when done with it.
145 GList *
146 mm_module_manager_get_all_application_providers (MMModuleManager *manager)
148 GList *l;
149 GList *application_providers = NULL;
151 for (l = manager->details->modules; l; l = l->next) {
152 application_providers = g_list_prepend (application_providers,
153 mm_module_get_application_provider (MM_MODULE (l->data)));
156 return application_providers;
160 * mm_module_manager_get_category_provider_for_application:
161 * @manager: a #MMModuleManager.
162 * @id: the application id.
164 * Gets the #MMCategoryProvider for the application specified by @id.
166 * Return value: a #MMCategoryProvider or %NULL.
169 MMCategoryProvider *
170 mm_module_manager_get_category_provider_for_application (MMModuleManager *manager,
171 const char *id)
173 MMCategoryProvider *provider = NULL;
174 MMModule *module;
176 module = find_module_for_name (manager, id);
177 if (!module) {
178 return NULL;
181 provider = mm_module_get_category_provider (module);
183 return provider;
187 * mm_module_manager_get_hit_collection_provider_for_application:
188 * @manager: a #MMModuleManager.
189 * @id: the application id.
191 * Gets the #MMHitCollectionProvider for the application specified by @id.
193 * Return value: a #MMHitCollectionProvider or %NULL.
196 MMHitCollectionProvider *
197 mm_module_manager_get_hit_collection_provider_for_application (MMModuleManager *manager,
198 const char *id)
200 MMHitCollectionProvider *provider = NULL;
201 MMModule *module;
203 module = find_module_for_name (manager, id);
204 if (!module) {
205 return NULL;
208 provider = mm_module_get_hit_collection_provider (module);
210 return provider;