1 /*
2 * This is file is kind of the junk file. Basically everything that
3 * didn't fit in one of the other well defined areas, well, it's now
4 * here. Which is good in someways, but this file really needs some
5 * definition. Hopefully that will come ASAP.
6 *
7 * Authors:
8 * Ted Gould <ted@gould.cx>
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
19 #include <interface.h>
21 #include "db.h"
22 #include "input.h"
23 #include "output.h"
24 #include "effect.h"
25 #include "patheffect.h"
26 #include "print.h"
27 #include "implementation/script.h"
28 /* #include "implementation/plugin.h" */
30 namespace Inkscape {
31 namespace Extension {
33 static void open_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
34 static void save_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
35 static Extension *build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp);
37 /**
38 * \return A new document created from the filename passed in
39 * \brief This is a generic function to use the open function of
40 * a module (including Autodetect)
41 * \param key Identifier of which module to use
42 * \param filename The file that should be opened
43 *
44 * First things first, are we looking at an autodetection? Well if that's the case then the module
45 * needs to be found, and that is done with a database lookup through the module DB. The foreach
46 * function is called, with the parameter being a gpointer array. It contains both the filename
47 * (to find its extension) and where to write the module when it is found.
48 *
49 * If there is no autodetection, then the module database is queried with the key given.
50 *
51 * If everything is cool at this point, the module is loaded, and there is possibility for
52 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
53 * After it is finished the function continues.
54 *
55 * Lastly, the open function is called in the module itself.
56 */
57 SPDocument *
58 open(Extension *key, gchar const *filename)
59 {
60 Input *imod = NULL;
61 if (key == NULL) {
62 gpointer parray[2];
63 parray[0] = (gpointer)filename;
64 parray[1] = (gpointer)&imod;
65 db.foreach(open_internal, (gpointer)&parray);
66 } else {
67 imod = dynamic_cast<Input *>(key);
68 }
70 bool last_chance_svg = false;
71 if (key == NULL && imod == NULL) {
72 last_chance_svg = true;
73 imod = dynamic_cast<Input *>(db.get(SP_MODULE_KEY_INPUT_SVG));
74 }
76 if (imod == NULL) {
77 throw Input::no_extension_found();
78 }
80 imod->set_state(Extension::STATE_LOADED);
82 if (!imod->loaded()) {
83 throw Input::open_failed();
84 }
86 if (!imod->prefs(filename))
87 return NULL;
89 SPDocument *doc = imod->open(filename);
90 if (!doc) {
91 return NULL;
92 }
94 if (last_chance_svg) {
95 /* We can't call sp_ui_error_dialog because we may be
96 running from the console, in which case calling sp_ui
97 routines will cause a segfault. See bug 1000350 - bryce */
98 // sp_ui_error_dialog(_("Format autodetect failed. The file is being opened as SVG."));
99 g_warning(_("Format autodetect failed. The file is being opened as SVG."));
100 }
102 /* This kinda overkill as most of these are already set, but I want
103 to make sure for this release -- TJG */
104 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
105 gboolean saved = sp_document_get_undo_sensitive(doc);
106 sp_document_set_undo_sensitive(doc, FALSE);
107 repr->setAttribute("sodipodi:modified", NULL);
108 sp_document_set_undo_sensitive(doc, saved);
110 sp_document_set_uri(doc, filename);
112 return doc;
113 }
115 /**
116 * \return none
117 * \brief This is the function that searches each module to see
118 * if it matches the filename for autodetection.
119 * \param in_plug The module to be tested
120 * \param in_data An array of pointers containing the filename, and
121 * the place to put a successfully found module.
122 *
123 * Basically this function only looks at input modules as it is part of the open function. If the
124 * module is an input module, it then starts to take it apart, and the data that is passed in.
125 * Because the data being passed in is in such a weird format, there are a few casts to make it
126 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
127 * compiler.
128 *
129 * First thing that is checked is if the filename is shorter than the extension itself. There is
130 * no way for a match in that case. If it's long enough then there is a string compare of the end
131 * of the filename (for the length of the extension), and the extension itself. If this passes
132 * then the pointer passed in is set to the current module.
133 */
134 static void
135 open_internal(Extension *in_plug, gpointer in_data)
136 {
137 if (!in_plug->deactivated() && dynamic_cast<Input *>(in_plug)) {
138 gpointer *parray = (gpointer *)in_data;
139 gchar const *filename = (gchar const *)parray[0];
140 Input **pimod = (Input **)parray[1];
142 // skip all the rest if we already found a function to open it
143 // since they're ordered by preference now.
144 if (!*pimod) {
145 gchar const *ext = dynamic_cast<Input *>(in_plug)->get_extension();
147 gchar *filenamelower = g_utf8_strdown(filename, -1);
148 gchar *extensionlower = g_utf8_strdown(ext, -1);
150 if (g_str_has_suffix(filenamelower, extensionlower)) {
151 *pimod = dynamic_cast<Input *>(in_plug);
152 }
154 g_free(filenamelower);
155 g_free(extensionlower);
156 }
157 }
159 return;
160 }
162 /**
163 * \return None
164 * \brief This is a generic function to use the save function of
165 * a module (including Autodetect)
166 * \param key Identifier of which module to use
167 * \param doc The document to be saved
168 * \param filename The file that the document should be saved to
169 * \param official (optional) whether to set :output_module and :modified in the
170 * document; is true for normal save, false for temporary saves
171 *
172 * First things first, are we looking at an autodetection? Well if that's the case then the module
173 * needs to be found, and that is done with a database lookup through the module DB. The foreach
174 * function is called, with the parameter being a gpointer array. It contains both the filename
175 * (to find its extension) and where to write the module when it is found.
176 *
177 * If there is no autodetection the module database is queried with the key given.
178 *
179 * If everything is cool at this point, the module is loaded, and there is possibility for
180 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
181 * After it is finished the function continues.
182 *
183 * Lastly, the save function is called in the module itself.
184 */
185 void
186 save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official)
187 {
188 Output *omod;
189 if (key == NULL) {
190 gpointer parray[2];
191 parray[0] = (gpointer)filename;
192 parray[1] = (gpointer)&omod;
193 omod = NULL;
194 db.foreach(save_internal, (gpointer)&parray);
196 /* This is a nasty hack, but it is required to ensure that
197 autodetect will always save with the Inkscape extensions
198 if they are available. */
199 if (omod != NULL && !strcmp(omod->get_id(), SP_MODULE_KEY_OUTPUT_SVG)) {
200 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
201 }
202 /* If autodetect fails, save as Inkscape SVG */
203 if (omod == NULL) {
204 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
205 }
206 } else {
207 omod = dynamic_cast<Output *>(key);
208 }
210 if (!dynamic_cast<Output *>(omod)) {
211 g_warning("Unable to find output module to handle file: %s\n", filename);
212 throw Output::no_extension_found();
213 return;
214 }
216 omod->set_state(Extension::STATE_LOADED);
217 if (!omod->loaded()) {
218 throw Output::save_failed();
219 }
221 if (!omod->prefs())
222 return;
224 gchar *fileName = NULL;
225 if (setextension) {
226 gchar *lowerfile = g_utf8_strdown(filename, -1);
227 gchar *lowerext = g_utf8_strdown(omod->get_extension(), -1);
229 if (!g_str_has_suffix(lowerfile, lowerext)) {
230 fileName = g_strdup_printf("%s%s", filename, omod->get_extension());
231 }
233 g_free(lowerfile);
234 g_free(lowerext);
235 }
237 if (fileName == NULL) {
238 fileName = g_strdup(filename);
239 }
241 if (check_overwrite && !sp_ui_overwrite_file(fileName)) {
242 g_free(fileName);
243 throw Output::no_overwrite();
244 }
246 if (official) {
247 sp_document_set_uri(doc, fileName);
248 }
250 omod->save(doc, fileName);
252 g_free(fileName);
253 return;
254 }
256 /**
257 * \return none
258 * \brief This is the function that searches each module to see
259 * if it matches the filename for autodetection.
260 * \param in_plug The module to be tested
261 * \param in_data An array of pointers containing the filename, and
262 * the place to put a successfully found module.
263 *
264 * Basically this function only looks at output modules as it is part of the open function. If the
265 * module is an output module, it then starts to take it apart, and the data that is passed in.
266 * Because the data being passed in is in such a weird format, there are a few casts to make it
267 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
268 * compiler.
269 *
270 * First thing that is checked is if the filename is shorter than the extension itself. There is
271 * no way for a match in that case. If it's long enough then there is a string compare of the end
272 * of the filename (for the length of the extension), and the extension itself. If this passes
273 * then the pointer passed in is set to the current module.
274 */
275 static void
276 save_internal(Extension *in_plug, gpointer in_data)
277 {
278 if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
279 gpointer *parray = (gpointer *)in_data;
280 gchar const *filename = (gchar const *)parray[0];
281 Output **pomod = (Output **)parray[1];
283 // skip all the rest if we already found someone to save it
284 // since they're ordered by preference now.
285 if (!*pomod) {
286 gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
288 gchar *filenamelower = g_utf8_strdown(filename, -1);
289 gchar *extensionlower = g_utf8_strdown(ext, -1);
291 if (g_str_has_suffix(filenamelower, extensionlower)) {
292 *pomod = dynamic_cast<Output *>(in_plug);
293 }
295 g_free(filenamelower);
296 g_free(extensionlower);
297 }
298 }
300 return;
301 }
303 Print *
304 get_print(gchar const *key)
305 {
306 return dynamic_cast<Print *>(db.get(key));
307 }
309 /**
310 * \return The built module
311 * \brief Creates a module from a Inkscape::XML::Document describing the module
312 * \param doc The XML description of the module
313 *
314 * This function basically has two segments. The first is that it goes through the Repr tree
315 * provided, and determines what kind of of module this is, and what kind of implementation to use.
316 * All of these are then stored in two enums that are defined in this function. This makes it
317 * easier to add additional types (which will happen in the future, I'm sure).
318 *
319 * Second, there is case statements for these enums. The first one is the type of module. This is
320 * the one where the module is actually created. After that, then the implementation is applied to
321 * get the load and unload functions. If there is no implementation then these are not set. This
322 * case could apply to modules that are built in (like the SVG load/save functions).
323 */
324 static Extension *
325 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
326 {
327 enum {
328 MODULE_EXTENSION,
329 /* MODULE_PLUGIN, */
330 MODULE_UNKNOWN_IMP
331 } module_implementation_type = MODULE_UNKNOWN_IMP;
332 enum {
333 MODULE_INPUT,
334 MODULE_OUTPUT,
335 MODULE_FILTER,
336 MODULE_PRINT,
337 MODULE_PATH_EFFECT,
338 MODULE_UNKNOWN_FUNC
339 } module_functional_type = MODULE_UNKNOWN_FUNC;
341 g_return_val_if_fail(doc != NULL, NULL);
343 Inkscape::XML::Node *repr = sp_repr_document_root(doc);
345 /* sp_repr_print(repr); */
347 if (strcmp(repr->name(), "inkscape-extension")) {
348 g_warning("Extension definition started with <%s> instead of <inkscape-extension>. Extension will not be created.\n", repr->name());
349 return NULL;
350 }
352 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
353 while (child_repr != NULL) {
354 char const *element_name = child_repr->name();
355 /* printf("Child: %s\n", child_repr->name()); */
356 if (!strcmp(element_name, "input")) {
357 module_functional_type = MODULE_INPUT;
358 } else if (!strcmp(element_name, "output")) {
359 module_functional_type = MODULE_OUTPUT;
360 } else if (!strcmp(element_name, "effect")) {
361 module_functional_type = MODULE_FILTER;
362 } else if (!strcmp(element_name, "print")) {
363 module_functional_type = MODULE_PRINT;
364 } else if (!strcmp(element_name, "path-effect")) {
365 module_functional_type = MODULE_PATH_EFFECT;
366 } else if (!strcmp(element_name, "script")) {
367 module_implementation_type = MODULE_EXTENSION;
368 #if 0
369 } else if (!strcmp(element_name, "plugin")) {
370 module_implementation_type = MODULE_PLUGIN;
371 #endif
372 }
374 //Inkscape::XML::Node *old_repr = child_repr;
375 child_repr = sp_repr_next(child_repr);
376 //Inkscape::GC::release(old_repr);
377 }
379 Implementation::Implementation *imp;
380 if (in_imp == NULL) {
381 switch (module_implementation_type) {
382 case MODULE_EXTENSION: {
383 Implementation::Script *script = new Implementation::Script();
384 imp = static_cast<Implementation::Implementation *>(script);
385 break;
386 }
387 #if 0
388 case MODULE_PLUGIN: {
389 Implementation::Plugin *plugin = new Implementation::Plugin();
390 imp = static_cast<Implementation::Implementation *>(plugin);
391 break;
392 }
393 #endif
394 default: {
395 imp = NULL;
396 break;
397 }
398 }
399 } else {
400 imp = in_imp;
401 }
403 Extension *module = NULL;
404 switch (module_functional_type) {
405 case MODULE_INPUT: {
406 module = new Input(repr, imp);
407 break;
408 }
409 case MODULE_OUTPUT: {
410 module = new Output(repr, imp);
411 break;
412 }
413 case MODULE_FILTER: {
414 module = new Effect(repr, imp);
415 break;
416 }
417 case MODULE_PRINT: {
418 module = new Print(repr, imp);
419 break;
420 }
421 case MODULE_PATH_EFFECT: {
422 module = new PathEffect(repr, imp);
423 break;
424 }
425 default: {
426 break;
427 }
428 }
430 return module;
431 }
433 /**
434 * \return The module created
435 * \brief This function creates a module from a filename of an
436 * XML description.
437 * \param filename The file holding the XML description of the module.
438 *
439 * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
440 */
441 Extension *
442 build_from_file(gchar const *filename)
443 {
444 /* TODO: Need to define namespace here, need to write the
445 DTD in general for this stuff */
446 Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
447 Extension *ext = build_from_reprdoc(doc, NULL);
448 if (ext != NULL)
449 Inkscape::GC::release(doc);
450 else
451 g_warning("Unable to create extension from definition file %s.\n", filename);
452 return ext;
453 }
455 /**
456 * \return The module created
457 * \brief This function creates a module from a buffer holding an
458 * XML description.
459 * \param buffer The buffer holding the XML description of the module.
460 *
461 * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc. It
462 * finds the length of the buffer using strlen.
463 */
464 Extension *
465 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
466 {
467 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
468 Extension *ext = build_from_reprdoc(doc, in_imp);
469 Inkscape::GC::release(doc);
470 return ext;
471 }
474 } } /* namespace Inkscape::Extension */
476 /*
477 Local Variables:
478 mode:c++
479 c-file-style:"stroustrup"
480 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
481 indent-tabs-mode:nil
482 fill-column:99
483 End:
484 */
485 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :