1 /*
2  * Copyright (C) 2010, 2011 Igalia S.L.
3  *
4  * Contact: Iago Toral Quiroga <itoral@igalia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22 
23 #if !defined (_GRILO_H_INSIDE_) && !defined (GRILO_COMPILATION)
24 #error "Only <grilo.h> can be included directly."
25 #endif
26 
27 #ifndef _GRL_REGISTRY_H_
28 #define _GRL_REGISTRY_H_
29 
30 #include <glib.h>
31 #include <glib-object.h>
32 #include <gmodule.h>
33 
34 #include <grl-source.h>
35 #include <grl-metadata-key.h>
36 #include <grl-config.h>
37 #include <grl-definitions.h>
38 #include <grl-plugin.h>
39 
40 #define GRL_PLUGIN_PATH_VAR "GRL_PLUGIN_PATH"
41 #define GRL_PLUGIN_LIST_VAR "GRL_PLUGIN_LIST"
42 #define GRL_PLUGIN_RANKS_VAR "GRL_PLUGIN_RANKS"
43 #define GRL_CONFIG_PATH_VAR "GRL_CONFIG_PATH"
44 
45 /* Macros */
46 
47 #define GRL_TYPE_REGISTRY                       \
48   (grl_registry_get_type ())
49 
50 #define GRL_REGISTRY(obj)                                       \
51   (G_TYPE_CHECK_INSTANCE_CAST ((obj),                           \
52                                GRL_TYPE_REGISTRY,               \
53                                GrlRegistry))
54 
55 #define GRL_IS_REGISTRY(obj)                                    \
56   (G_TYPE_CHECK_INSTANCE_TYPE ((obj),                           \
57                                GRL_TYPE_REGISTRY))
58 
59 #define GRL_REGISTRY_CLASS(klass)                       \
60   (G_TYPE_CHECK_CLASS_CAST((klass),                     \
61                            GRL_TYPE_REGISTRY,           \
62                            GrlRegistryClass))
63 
64 #define GRL_IS_REGISTRY_CLASS(klass)                    \
65   (G_TYPE_CHECK_CLASS_TYPE((klass),                     \
66                            GRL_TYPE_REGISTRY))
67 
68 #define GRL_REGISTRY_GET_CLASS(obj)                     \
69   (G_TYPE_INSTANCE_GET_CLASS ((obj),                    \
70                               GRL_TYPE_REGISTRY,        \
71                               GrlRegistryClass))
72 
73 /* Plugin registration */
74 
75 /**
76 * GRL_PLUGIN_DEFINE:
77 * @major: the major version number of core that plugin was compiled for, as an integer
78 * @minor: the minor version number of core that plugin was compiled for, as an integer
79 * @id: the plugin identifier
80 * @name: name of plugin
81 * @description: description of plugin
82 * @author: author of plugin
83 * @version: version of plugin
84 * @license: license of plugin
85 * @site: URL to provider of plugin
86 * @init: the module initialization. It shall instantiate
87 * the #GrlPlugins provided
88 * @deinit: (allow-none): function to execute when the registry needs to dispose
89 * the module.
90 * @register_keys: (allow-none): function to execute before loading the
91 * plugin. It's aim is to register new keys
92 *
93 * Since: 0.3.0
94 */
95 #define GRL_PLUGIN_DEFINE(major, minor, id, name, description, author, version, license, site, init, deinit, register_keys) \
96   G_MODULE_EXPORT GrlPluginDescriptor GRL_PLUGIN_DESCRIPTOR = { \
97     major,                                                      \
98     minor,                                                      \
99     id,                                                         \
100     name,                                                       \
101     description,                                                \
102     author,                                                     \
103     version,                                                    \
104     license,                                                    \
105     site,                                                       \
106     init,                                                       \
107     deinit,                                                     \
108     register_keys                                               \
109   }
110 
111 /* Plugin descriptor */
112 
113 typedef struct _GrlRegistry GrlRegistry;
114 
115 typedef struct _GrlPluginDescriptor  GrlPluginDescriptor;
116 
117 typedef gboolean (*GrlPluginInitFunc) (GrlRegistry *registry,
118                                        GrlPlugin *plugin,
119                                        GList *configs);
120 
121 typedef void (*GrlPluginDeinitFunc) (GrlPlugin *plugin);
122 
123 typedef void (*GrlPluginRegisterKeysFunc) (GrlRegistry *registry,
124                                            GrlPlugin *plugin);
125 
126 /**
127 * GrlPluginDescriptor:
128 * @major_version: the major version number of core that plugin was compiled for
129 * @minor_version: the minor version number of core that plugin was compiled for
130 * @id: the plugin identifier
131 * @name: name of plugin
132 * @description: description of plugin
133 * @author: author of plugin
134 * @version: version of plugin
135 * @license: license of plugin
136 * @site: URL to provider of plugin
137 * @init: the module initialization. It shall instantiate
138 * the #GrlPlugins provided
139 * @deinit: function to execute when the registry needs
140 * to dispose the module.
141 * @register_keys: function to execute before loading the plugin. It's aim
142 * is to register new keys
143 *
144 * This structure is used for the module loader
145 *
146 * Since: 0.3.0
147 */
148 struct _GrlPluginDescriptor {
149   gint major_version;
150   gint minor_version;
151   gchar *id;
152   gchar *name;
153   gchar *description;
154   gchar *author;
155   gchar *version;
156   gchar *license;
157   gchar *site;
158 
159   GrlPluginInitFunc init;
160   GrlPluginDeinitFunc deinit;
161   GrlPluginRegisterKeysFunc register_keys;
162 
163   /*< private >*/
164   gpointer _grl_reserved[GRL_PADDING];
165 };
166 
167 /* Plugin ranks */
168 
169 /**
170  * GrlRank:
171  * @GRL_RANK_LOWEST: will be chosen last or not at all
172  * @GRL_RANK_LOW: unlikely to be chosen
173  * @GRL_RANK_DEFAULT: likely to be chosen
174  * @GRL_RANK_HIGH: will be chosen
175  * @GRL_RANK_HIGHEST: will be chosen first
176  *
177  * Source priority ranks. Defines the order in which the resolver
178  * (or similar rank-picking mechanisms) will choose this source
179  * over an alternative one with the same function.
180  *
181  * These constants serve as a rough guidance for defining the rank
182  * of a GrlSource. Any value is valid, including values bigger
183  * than GRL_RANK_HIGHEST.
184  */
185 typedef enum {
186   GRL_RANK_LOWEST  = -64,
187   GRL_RANK_LOW     = -32,
188   GRL_RANK_DEFAULT =   0,
189   GRL_RANK_HIGH    =  32,
190   GRL_RANK_HIGHEST =  64
191 } GrlRank;
192 
193 /* GrlRegistry object */
194 
195 typedef struct _GrlRegistryPrivate GrlRegistryPrivate;
196 
197 struct _GrlRegistry {
198 
199   GObject parent;
200 
201   /*< private >*/
202   GrlRegistryPrivate *priv;
203 
204   gpointer _grl_reserved[GRL_PADDING];
205 };
206 
207 /* GrlRegistry class */
208 
209 typedef struct _GrlRegistryClass GrlRegistryClass;
210 
211 /**
212  * GrlRegistryClass:
213  * @parent_class: the parent class structure
214  *
215  * Grilo Registry class. Dynamic loader of plugins.
216  */
217 struct _GrlRegistryClass {
218 
219   GObjectClass parent_class;
220 
221   /*< private >*/
222   gpointer _grl_reserved[GRL_PADDING];
223 };
224 
225 G_BEGIN_DECLS
226 
227 GType grl_registry_get_type (void);
228 
229 GrlRegistry *grl_registry_get_default (void);
230 
231 void grl_registry_add_directory (GrlRegistry *registry,
232                                  const gchar *path);
233 
234 gboolean grl_registry_load_plugin (GrlRegistry *registry,
235                                    const gchar *library_filename,
236                                    GError **error);
237 
238 gboolean grl_registry_load_plugin_from_desc (GrlRegistry *registry,
239                                              GrlPluginDescriptor *plugin_desc,
240                                              GError **error);
241 
242 gboolean grl_registry_load_plugin_directory (GrlRegistry *registry,
243                                              const gchar *path,
244                                              GError **error);
245 
246 gboolean grl_registry_unload_plugin (GrlRegistry *registry,
247                                      const gchar *plugin_id,
248                                      GError **error);
249 
250 gboolean grl_registry_activate_all_plugins (GrlRegistry *registry);
251 
252 gboolean grl_registry_load_all_plugins (GrlRegistry *registry,
253                                         gboolean activate,
254                                         GError **error);
255 
256 gboolean grl_registry_activate_plugin_by_id (GrlRegistry *registry,
257                                              const gchar *plugin_id,
258                                              GError **error);
259 
260 gboolean grl_registry_register_source (GrlRegistry *registry,
261                                        GrlPlugin *plugin,
262                                        GrlSource *source,
263                                        GError **error);
264 
265 gboolean grl_registry_unregister_source (GrlRegistry *registry,
266                                          GrlSource *source,
267                                          GError **error);
268 
269 GrlSource *grl_registry_lookup_source (GrlRegistry *registry,
270                                        const gchar *source_id);
271 
272 GList *grl_registry_get_sources (GrlRegistry *registry,
273                                  gboolean ranked);
274 
275 GList *grl_registry_get_sources_by_operations (GrlRegistry *registry,
276                                                GrlSupportedOps ops,
277                                                gboolean ranked);
278 
279 GrlPlugin *grl_registry_lookup_plugin (GrlRegistry *registry,
280                                        const gchar *plugin_id);
281 
282 GList *grl_registry_get_plugins (GrlRegistry *registry,
283                                  gboolean only_loaded);
284 
285 
286 GrlKeyID grl_registry_register_metadata_key (GrlRegistry *registry,
287                                              GParamSpec *param_spec,
288                                              GrlKeyID bind_key,
289                                              GError **error);
290 
291 GrlKeyID grl_registry_lookup_metadata_key (GrlRegistry *registry,
292                                            const gchar *key_name);
293 
294 const GList *grl_registry_lookup_metadata_key_relation (GrlRegistry *registry,
295                                                         GrlKeyID key);
296 
297 const gchar *grl_registry_lookup_metadata_key_name (GrlRegistry *registry,
298                                                     GrlKeyID key);
299 
300 const gchar *grl_registry_lookup_metadata_key_desc (GrlRegistry *registry,
301                                                     GrlKeyID key);
302 
303 GType grl_registry_lookup_metadata_key_type (GrlRegistry *registry,
304                                              GrlKeyID key);
305 
306 gboolean grl_registry_metadata_key_validate (GrlRegistry *registry,
307                                              GrlKeyID key,
308                                              GValue *value);
309 
310 GList *grl_registry_get_metadata_keys (GrlRegistry *registry);
311 
312 gboolean grl_registry_add_config (GrlRegistry *registry,
313                                   GrlConfig *config,
314                                   GError **error);
315 
316 gboolean grl_registry_add_config_from_file (GrlRegistry *registry,
317                                             const gchar *config_file,
318                                             GError **error);
319 
320 gboolean grl_registry_add_config_from_resource (GrlRegistry *registry,
321                                                 const gchar *resource_path,
322                                                 GError **error);
323 
324 G_END_DECLS
325 
326 #endif /* _GRL_REGISTRY_H_ */
327