Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
db.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Functions to keep a listing of all modules in the system. Has its
4 * own file mostly for abstraction reasons, but is pretty simple
5 * otherwise.
6 *
7 * Authors:
8 * Ted Gould <ted@gould.cx>
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 *
11 * Copyright (C) 2002-2004 Authors
12 *
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15
16#include "db.h"
17
18#include "effect.h"
20#include "input.h"
21#include "output.h"
22#include "template.h"
23
24/* Globals */
25
26/* Namespaces */
27
28namespace Inkscape {
29namespace Extension {
30
33
34/* Types */
35struct ModuleGenericCmp
36{
37 bool operator()(Extension *module1, Extension *module2) const
38 {
39 int n1 = module1->get_sort_priority();
40 int n2 = module2->get_sort_priority();
41 if (n1 != n2)
42 return (n1 < n2);
43 return (strcmp(module1->get_name(), module2->get_name()) <= 0);
44 }
45};
46
47struct ModuleInputCmp {
48 bool operator()(Input* module1, Input* module2) const {
49
50 int n1 = module1->get_sort_priority();
51 int n2 = module2->get_sort_priority();
52 // Treat zero as not-defined for purpose of comparison.
53 if (n1 || n2)
54 return n1 && n2 ? (n1 < n2) : !n2;
55
56 return (strcmp(module1->get_filetypename(), module2->get_filetypename()) <= 0);
57 }
58};
59
60
61struct ModuleOutputCmp {
62 bool operator()(Output* module1, Output* module2) const {
63
64 int n1 = module1->get_sort_priority();
65 int n2 = module2->get_sort_priority();
66 // Treat zero as not-defined for purpose of comparison.
67 if (n1 || n2)
68 return n1 && n2 ? (n1 < n2) : !n2;
69
70 // special case: two extensions for the same file type. I only one of them is a script, prefer the other one
71 if (Glib::ustring(module1->get_extension()).lowercase() == Glib::ustring(module2->get_extension()).lowercase()) {
72 bool module1_is_script = dynamic_cast<Inkscape::Extension::Implementation::Script *>(module1->get_imp());
73 bool module2_is_script = dynamic_cast<Inkscape::Extension::Implementation::Script *>(module2->get_imp());
74 if (module1_is_script != module2_is_script) {
75 return module1_is_script ? false : true;
76 }
77 }
78 return (strcmp(module1->get_filetypename(), module2->get_filetypename()) <= 0);
79 }
80};
81
86void DB::take_ownership(std::unique_ptr<Extension> module)
87{
88 if (module) {
89 moduledict[module->get_id()] = std::move(module);
90 }
91}
92
101Extension *DB::get(const gchar *key) const
102{
103 if (key == nullptr) return nullptr;
104
105 auto it = moduledict.find(key);
106 if (it == moduledict.end())
107 return nullptr;
108
109 Extension *mod = it->second.get();
110 assert(mod);
111
112 if (!mod || mod->deactivated())
113 return nullptr;
114
115 return mod;
116}
117
128void DB::foreach(void (*in_func)(Extension *, gpointer), gpointer in_data)
129{
130 for (auto const &item : moduledict) {
131 in_func(item.second.get(), in_data);
132 }
133}
134
142void DB::template_internal(Extension *in_plug, gpointer data)
143{
144 if (auto tmod = dynamic_cast<Template *>(in_plug)) {
145 auto tlist = reinterpret_cast<TemplateList *>(data);
146 tlist->push_back(tmod);
147 }
148}
149
161void
162DB::input_internal (Extension * in_plug, gpointer data)
163{
164 if (auto imod = dynamic_cast<Input *>(in_plug)) {
165 auto ilist = reinterpret_cast<InputList *>(data);
166 ilist->push_back(imod);
167 }
168}
169
181void
182DB::output_internal (Extension * in_plug, gpointer data)
183{
184 if (dynamic_cast<Output *>(in_plug)) {
185 OutputList * olist;
186 Output * omod;
187
188 omod = dynamic_cast<Output *>(in_plug);
189 olist = reinterpret_cast<OutputList *>(data);
190
191 olist->push_back(omod);
192 // printf("Added to output list: %s\n", omod->get_id());
193 }
194}
195
207void
208DB::effect_internal (Extension * in_plug, gpointer data)
209{
210 if (dynamic_cast<Effect *>(in_plug)) {
211 EffectList * elist;
212 Effect * emod;
213
214 emod = dynamic_cast<Effect *>(in_plug);
215 elist = reinterpret_cast<EffectList *>(data);
216
217 elist->push_back(emod);
218 // printf("Added to effect list: %s\n", emod->get_id());
219 }
220}
221
229{
230 foreach (template_internal, (gpointer)&ou_list);
231 ou_list.sort(ModuleGenericCmp());
232 return ou_list;
233}
234
243{
244 foreach(input_internal, (gpointer)&ou_list);
245 ou_list.sort( ModuleInputCmp() );
246 return ou_list;
247}
248
257{
258 foreach(output_internal, (gpointer)&ou_list);
259 ou_list.sort( ModuleOutputCmp() );
260 return ou_list;
261}
262
266std::vector<Effect*> DB::get_effect_list() {
267 std::vector<Effect*> out;
268 for (auto const &item : moduledict) {
269 auto ex = item.second.get();
270 if (auto effect = dynamic_cast<Effect*>(ex)) {
271 out.push_back(effect);
272 }
273 }
274 return out;
275}
276
277} } /* namespace Extension, Inkscape */
std::list< Output * > OutputList
Definition db.h:58
std::list< Effect * > EffectList
Definition db.h:60
static void output_internal(Extension *in_plug, gpointer data)
The function to look at each module and see if it is an output module, then add it to the list.
Definition db.cpp:182
void foreach(void(*in_func)(Extension *in_plug, gpointer in_data), gpointer in_data)
A function to execute another function with every entry in the database as a parameter.
Definition db.cpp:128
void take_ownership(std::unique_ptr< Extension > module)
Take the ownership of an extension to ensure that it is freed on program exit.
Definition db.cpp:86
std::vector< Effect * > get_effect_list()
Creates a list of all the Effect extensions.
Definition db.cpp:266
OutputList & get_output_list(OutputList &ou_list)
Creates a list of all the Output extensions.
Definition db.cpp:256
TemplateList & get_template_list(TemplateList &ou_list)
Create a list of all the Template extensions.
Definition db.cpp:228
std::unordered_map< std::string, std::unique_ptr< Extension > > moduledict
This is the actual database.
Definition db.h:39
std::list< Input * > InputList
Definition db.h:59
static void effect_internal(Extension *in_plug, gpointer data)
The function to look at each module and see if it is an effect module, then add it to the list.
Definition db.cpp:208
std::list< Template * > TemplateList
Definition db.h:57
Extension * get(const gchar *key) const
This function looks up a Inkscape::Extension::Extension by using its unique id. It then returns a ref...
Definition db.cpp:101
InputList & get_input_list(InputList &ou_list)
Creates a list of all the Input extensions.
Definition db.cpp:242
static void input_internal(Extension *in_plug, gpointer data)
The function to look at each module and see if it is an input module, then add it to the list.
Definition db.cpp:162
static void template_internal(Extension *in_plug, gpointer data)
The function to look at each module and see if it is a template module, then add it to the list.
Definition db.cpp:142
Effects are extensions that take a document and do something to it in place.
Definition effect.h:39
The object that is the basis for the Extension system.
Definition extension.h:133
Utility class used for loading and launching script extensions.
Definition script.h:52
SPItem * item
DB db
This is the actual database object.
Definition db.cpp:32
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
unsigned n1
unsigned n2
static const Point data[]