1 /*
2 * Functions to keep a listing of all modules in the system. Has its
3 * own file mostly for abstraction reasons, but is pretty simple
4 * otherwise.
5 *
6 * Authors:
7 * Ted Gould <ted@gould.cx>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 *
10 * Copyright (C) 2002-2004 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18 #include "db.h"
19 #include "input.h"
20 #include "output.h"
21 #include "effect.h"
23 /* Globals */
25 /* Namespaces */
27 namespace Inkscape {
28 namespace Extension {
30 /** This is the actual database object. There is only one of these */
31 DB db;
33 /* Types */
35 DB::DB (void) {
36 }
38 /**
39 \brief Add a module to the module database
40 \param module The module to be registered.
41 */
42 void
43 DB::register_ext (Extension *module)
44 {
45 g_return_if_fail(module != NULL);
46 g_return_if_fail(module->get_id() != NULL);
48 // only add to list if it's a never-before-seen module
49 bool add_to_list =
50 ( moduledict.find(module->get_id()) == moduledict.end());
52 //printf("Registering: '%s' add:%d\n", module->get_id(), add_to_list);
53 moduledict[module->get_id()] = module;
55 if (add_to_list) modulelist.push_back(module);
56 }
58 /**
59 \brief This function removes a module from the database
60 \param module The module to be removed.
61 */
62 void
63 DB::unregister_ext (Extension * module)
64 {
65 g_return_if_fail(module != NULL);
66 g_return_if_fail(module->get_id() != NULL);
68 // printf("Extension DB: removing %s\n", module->get_id());
69 moduledict.erase(moduledict.find(module->get_id()));
70 // only remove if it's not there any more
71 if ( moduledict.find(module->get_id()) != moduledict.end())
72 modulelist.remove(module);
73 }
75 /**
76 \return A reference to the Inkscape::Extension::Extension specified by the input key.
77 \brief This function looks up a Inkscape::Extension::Extension by using its unique
78 id. It then returns a reference to that module.
79 \param key The unique ID of the module
81 Retrieves a module by name; if non-NULL, it refs the returned
82 module; the caller is responsible for releasing that reference
83 when it is no longer needed.
84 */
85 Extension *
86 DB::get (const gchar *key)
87 {
88 if (key == NULL) return NULL;
90 Extension *mod = moduledict[key];
91 if ( !mod || mod->deactivated() )
92 return NULL;
94 return mod;
95 }
97 /**
98 \return none
99 \brief A function to execute another function with every entry
100 in the database as a parameter.
101 \param in_func The function to execute for every module
102 \param in_data A data pointer that is also passed to in_func
104 Enumerates the modules currently in the database, calling a given
105 callback for each one.
106 */
107 void
108 DB::foreach (void (*in_func)(Extension * in_plug, gpointer in_data), gpointer in_data)
109 {
110 std::list <Extension *>::iterator cur;
112 for (cur = modulelist.begin(); cur != modulelist.end(); cur++) {
113 // printf("foreach: %s\n", (*cur)->get_id());
114 in_func((*cur), in_data);
115 }
116 }
118 /**
119 \return none
120 \brief The function to look at each module and see if it is
121 an input module, then add it to the list.
122 \param in_plug Module to be examined
123 \param data The list to be attached to
125 The first thing that is checked is if this module is an input
126 module. If it is, then it is added to the list which is passed
127 in through \c data.
128 */
129 void
130 DB::input_internal (Extension * in_plug, gpointer data)
131 {
132 if (dynamic_cast<Input *>(in_plug)) {
133 InputList * ilist;
134 Input * imod;
136 imod = dynamic_cast<Input *>(in_plug);
137 ilist = reinterpret_cast<InputList *>(data);
139 ilist->push_back(imod);
140 // printf("Added to input list: %s\n", imod->get_id());
141 }
142 }
144 /**
145 \return none
146 \brief The function to look at each module and see if it is
147 an output module, then add it to the list.
148 \param in_plug Module to be examined
149 \param data The list to be attached to
151 The first thing that is checked is if this module is an output
152 module. If it is, then it is added to the list which is passed
153 in through \c data.
154 */
155 void
156 DB::output_internal (Extension * in_plug, gpointer data)
157 {
158 if (dynamic_cast<Output *>(in_plug)) {
159 OutputList * olist;
160 Output * omod;
162 omod = dynamic_cast<Output *>(in_plug);
163 olist = reinterpret_cast<OutputList *>(data);
165 olist->push_back(omod);
166 // printf("Added to output list: %s\n", omod->get_id());
167 }
169 return;
170 }
172 /**
173 \return none
174 \brief The function to look at each module and see if it is
175 an effect module, then add it to the list.
176 \param in_plug Module to be examined
177 \param data The list to be attached to
179 The first thing that is checked is if this module is an effect
180 module. If it is, then it is added to the list which is passed
181 in through \c data.
182 */
183 void
184 DB::effect_internal (Extension * in_plug, gpointer data)
185 {
186 if (dynamic_cast<Effect *>(in_plug)) {
187 EffectList * elist;
188 Effect * emod;
190 emod = dynamic_cast<Effect *>(in_plug);
191 elist = reinterpret_cast<EffectList *>(data);
193 elist->push_back(emod);
194 // printf("Added to effect list: %s\n", emod->get_id());
195 }
197 return;
198 }
200 /**
201 \brief Creates a list of all the Input extensions
202 \param ou_list The list that is used to put all the extensions in
204 Calls the database \c foreach function with \c input_internal.
205 */
206 DB::InputList &
207 DB::get_input_list (DB::InputList &ou_list)
208 {
209 foreach(input_internal, (gpointer)&ou_list);
210 return ou_list;
211 }
213 /**
214 \brief Creates a list of all the Output extensions
215 \param ou_list The list that is used to put all the extensions in
217 Calls the database \c foreach function with \c output_internal.
218 */
219 DB::OutputList &
220 DB::get_output_list (DB::OutputList &ou_list)
221 {
222 foreach(output_internal, (gpointer)&ou_list);
223 return ou_list;
224 }
226 /**
227 \brief Creates a list of all the Effect extensions
228 \param ou_list The list that is used to put all the extensions in
230 Calls the database \c foreach function with \c effect_internal.
231 */
232 DB::EffectList &
233 DB::get_effect_list (DB::EffectList &ou_list)
234 {
235 foreach(effect_internal, (gpointer)&ou_list);
236 return ou_list;
237 }
239 } } /* namespace Extension, Inkscape */