6442f2f7eb3638dbc66508eef550f73ab663acdc
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 * Johan Engelen <johan@shouraizou.nl>
10 *
11 * Copyright (C) 2006-2007 Johan Engelen
12 * Copyright (C) 2002-2004 Ted Gould
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <interface.h>
23 #include "extension.h"
24 #include "db.h"
25 #include "input.h"
26 #include "output.h"
27 #include "effect.h"
28 #include "patheffect.h"
29 #include "print.h"
30 #include "implementation/script.h"
31 #include "implementation/xslt.h"
32 /* #include "implementation/plugin.h" */
34 namespace Inkscape {
35 namespace Extension {
37 static void open_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
38 static void save_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
39 static Extension *build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp);
41 /**
42 * \return A new document created from the filename passed in
43 * \brief This is a generic function to use the open function of
44 * a module (including Autodetect)
45 * \param key Identifier of which module to use
46 * \param filename The file that should be opened
47 *
48 * First things first, are we looking at an autodetection? Well if that's the case then the module
49 * needs to be found, and that is done with a database lookup through the module DB. The foreach
50 * function is called, with the parameter being a gpointer array. It contains both the filename
51 * (to find its extension) and where to write the module when it is found.
52 *
53 * If there is no autodetection, then the module database is queried with the key given.
54 *
55 * If everything is cool at this point, the module is loaded, and there is possibility for
56 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
57 * After it is finished the function continues.
58 *
59 * Lastly, the open function is called in the module itself.
60 */
61 SPDocument *
62 open(Extension *key, gchar const *filename)
63 {
64 Input *imod = NULL;
65 if (key == NULL) {
66 gpointer parray[2];
67 parray[0] = (gpointer)filename;
68 parray[1] = (gpointer)&imod;
69 db.foreach(open_internal, (gpointer)&parray);
70 } else {
71 imod = dynamic_cast<Input *>(key);
72 }
74 bool last_chance_svg = false;
75 if (key == NULL && imod == NULL) {
76 last_chance_svg = true;
77 imod = dynamic_cast<Input *>(db.get(SP_MODULE_KEY_INPUT_SVG));
78 }
80 if (imod == NULL) {
81 throw Input::no_extension_found();
82 }
84 imod->set_state(Extension::STATE_LOADED);
86 if (!imod->loaded()) {
87 throw Input::open_failed();
88 }
90 if (!imod->prefs(filename))
91 return NULL;
93 SPDocument *doc = imod->open(filename);
94 if (!doc) {
95 return NULL;
96 }
98 if (last_chance_svg) {
99 /* We can't call sp_ui_error_dialog because we may be
100 running from the console, in which case calling sp_ui
101 routines will cause a segfault. See bug 1000350 - bryce */
102 // sp_ui_error_dialog(_("Format autodetect failed. The file is being opened as SVG."));
103 g_warning(_("Format autodetect failed. The file is being opened as SVG."));
104 }
106 /* This kinda overkill as most of these are already set, but I want
107 to make sure for this release -- TJG */
108 doc->setModifiedSinceSave(false);
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 throw Output::save_cancelled();
223 }
225 gchar *fileName = NULL;
226 if (setextension) {
227 gchar *lowerfile = g_utf8_strdown(filename, -1);
228 gchar *lowerext = g_utf8_strdown(omod->get_extension(), -1);
230 if (!g_str_has_suffix(lowerfile, lowerext)) {
231 fileName = g_strdup_printf("%s%s", filename, omod->get_extension());
232 }
234 g_free(lowerfile);
235 g_free(lowerext);
236 }
238 if (fileName == NULL) {
239 fileName = g_strdup(filename);
240 }
242 if (check_overwrite && !sp_ui_overwrite_file(fileName)) {
243 g_free(fileName);
244 throw Output::no_overwrite();
245 }
247 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
249 // remember attributes in case this is an unofficial save
250 bool saved_modified = false;
251 gchar *saved_output_extension = NULL;
252 gchar *saved_dataloss = NULL;
253 gchar *saved_uri = NULL;
254 if (!official) {
255 saved_modified = doc->isModifiedSinceSave();
256 saved_output_extension = g_strdup(repr->attribute("inkscape:output_extension"));
257 saved_dataloss = g_strdup(repr->attribute("inkscape:dataloss"));
258 saved_uri = g_strdup(doc->uri);
259 }
261 // Update attributes:
262 {
263 bool const saved = sp_document_get_undo_sensitive(doc);
264 sp_document_set_undo_sensitive(doc, false);
265 {
266 // save the filename for next use
267 sp_document_set_uri(doc, fileName);
268 // also save the extension for next use
269 repr->setAttribute("inkscape:output_extension", omod->get_id());
270 // set the "dataloss" attribute if the chosen extension is lossy
271 repr->setAttribute("inkscape:dataloss", NULL);
272 if (omod->causes_dataloss()) {
273 repr->setAttribute("inkscape:dataloss", "true");
274 }
275 }
276 sp_document_set_undo_sensitive(doc, saved);
277 doc->setModifiedSinceSave(false);
278 }
280 omod->save(doc, fileName);
282 // If it is an unofficial save, set the modified attributes back to what they were.
283 if ( !official) {
284 bool const saved = sp_document_get_undo_sensitive(doc);
285 sp_document_set_undo_sensitive(doc, false);
286 {
287 repr->setAttribute("inkscape:output_extension", saved_output_extension);
288 repr->setAttribute("inkscape:dataloss", saved_dataloss);
289 sp_document_set_uri(doc, saved_uri);
290 }
291 sp_document_set_undo_sensitive(doc, saved);
292 doc->setModifiedSinceSave(saved_modified);
294 g_free(saved_output_extension);
295 g_free(saved_dataloss);
296 g_free(saved_uri);
297 }
299 g_free(fileName);
300 return;
301 }
303 /**
304 * \return none
305 * \brief This is the function that searches each module to see
306 * if it matches the filename for autodetection.
307 * \param in_plug The module to be tested
308 * \param in_data An array of pointers containing the filename, and
309 * the place to put a successfully found module.
310 *
311 * Basically this function only looks at output modules as it is part of the open function. If the
312 * module is an output module, it then starts to take it apart, and the data that is passed in.
313 * Because the data being passed in is in such a weird format, there are a few casts to make it
314 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
315 * compiler.
316 *
317 * First thing that is checked is if the filename is shorter than the extension itself. There is
318 * no way for a match in that case. If it's long enough then there is a string compare of the end
319 * of the filename (for the length of the extension), and the extension itself. If this passes
320 * then the pointer passed in is set to the current module.
321 */
322 static void
323 save_internal(Extension *in_plug, gpointer in_data)
324 {
325 if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
326 gpointer *parray = (gpointer *)in_data;
327 gchar const *filename = (gchar const *)parray[0];
328 Output **pomod = (Output **)parray[1];
330 // skip all the rest if we already found someone to save it
331 // since they're ordered by preference now.
332 if (!*pomod) {
333 gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
335 gchar *filenamelower = g_utf8_strdown(filename, -1);
336 gchar *extensionlower = g_utf8_strdown(ext, -1);
338 if (g_str_has_suffix(filenamelower, extensionlower)) {
339 *pomod = dynamic_cast<Output *>(in_plug);
340 }
342 g_free(filenamelower);
343 g_free(extensionlower);
344 }
345 }
347 return;
348 }
350 Print *
351 get_print(gchar const *key)
352 {
353 return dynamic_cast<Print *>(db.get(key));
354 }
356 /**
357 * \return The built module
358 * \brief Creates a module from a Inkscape::XML::Document describing the module
359 * \param doc The XML description of the module
360 *
361 * This function basically has two segments. The first is that it goes through the Repr tree
362 * provided, and determines what kind of of module this is, and what kind of implementation to use.
363 * All of these are then stored in two enums that are defined in this function. This makes it
364 * easier to add additional types (which will happen in the future, I'm sure).
365 *
366 * Second, there is case statements for these enums. The first one is the type of module. This is
367 * the one where the module is actually created. After that, then the implementation is applied to
368 * get the load and unload functions. If there is no implementation then these are not set. This
369 * case could apply to modules that are built in (like the SVG load/save functions).
370 */
371 static Extension *
372 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
373 {
374 enum {
375 MODULE_EXTENSION,
376 MODULE_XSLT,
377 /* MODULE_PLUGIN, */
378 MODULE_UNKNOWN_IMP
379 } module_implementation_type = MODULE_UNKNOWN_IMP;
380 enum {
381 MODULE_INPUT,
382 MODULE_OUTPUT,
383 MODULE_FILTER,
384 MODULE_PRINT,
385 MODULE_PATH_EFFECT,
386 MODULE_UNKNOWN_FUNC
387 } module_functional_type = MODULE_UNKNOWN_FUNC;
389 g_return_val_if_fail(doc != NULL, NULL);
391 Inkscape::XML::Node *repr = doc->root();
393 if (strcmp(repr->name(), INKSCAPE_EXTENSION_NS "inkscape-extension")) {
394 g_warning("Extension definition started with <%s> instead of <" INKSCAPE_EXTENSION_NS "inkscape-extension>. Extension will not be created. See http://wiki.inkscape.org/wiki/index.php/Extensions for reference.\n", repr->name());
395 return NULL;
396 }
398 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
399 while (child_repr != NULL) {
400 char const *element_name = child_repr->name();
401 /* printf("Child: %s\n", child_repr->name()); */
402 if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "input")) {
403 module_functional_type = MODULE_INPUT;
404 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "output")) {
405 module_functional_type = MODULE_OUTPUT;
406 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "effect")) {
407 module_functional_type = MODULE_FILTER;
408 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "print")) {
409 module_functional_type = MODULE_PRINT;
410 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "path-effect")) {
411 module_functional_type = MODULE_PATH_EFFECT;
412 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "script")) {
413 module_implementation_type = MODULE_EXTENSION;
414 } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "xslt")) {
415 module_implementation_type = MODULE_XSLT;
416 #if 0
417 } else if (!strcmp(element_name, "plugin")) {
418 module_implementation_type = MODULE_PLUGIN;
419 #endif
420 }
422 //Inkscape::XML::Node *old_repr = child_repr;
423 child_repr = sp_repr_next(child_repr);
424 //Inkscape::GC::release(old_repr);
425 }
427 Implementation::Implementation *imp;
428 if (in_imp == NULL) {
429 switch (module_implementation_type) {
430 case MODULE_EXTENSION: {
431 Implementation::Script *script = new Implementation::Script();
432 imp = static_cast<Implementation::Implementation *>(script);
433 break;
434 }
435 case MODULE_XSLT: {
436 Implementation::XSLT *xslt = new Implementation::XSLT();
437 imp = static_cast<Implementation::Implementation *>(xslt);
438 break;
439 }
440 #if 0
441 case MODULE_PLUGIN: {
442 Implementation::Plugin *plugin = new Implementation::Plugin();
443 imp = static_cast<Implementation::Implementation *>(plugin);
444 break;
445 }
446 #endif
447 default: {
448 imp = NULL;
449 break;
450 }
451 }
452 } else {
453 imp = in_imp;
454 }
456 Extension *module = NULL;
457 switch (module_functional_type) {
458 case MODULE_INPUT: {
459 module = new Input(repr, imp);
460 break;
461 }
462 case MODULE_OUTPUT: {
463 module = new Output(repr, imp);
464 break;
465 }
466 case MODULE_FILTER: {
467 module = new Effect(repr, imp);
468 break;
469 }
470 case MODULE_PRINT: {
471 module = new Print(repr, imp);
472 break;
473 }
474 case MODULE_PATH_EFFECT: {
475 module = new PathEffect(repr, imp);
476 break;
477 }
478 default: {
479 break;
480 }
481 }
483 return module;
484 }
486 /**
487 * \return The module created
488 * \brief This function creates a module from a filename of an
489 * XML description.
490 * \param filename The file holding the XML description of the module.
491 *
492 * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
493 */
494 Extension *
495 build_from_file(gchar const *filename)
496 {
497 Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI);
498 Extension *ext = build_from_reprdoc(doc, NULL);
499 if (ext != NULL)
500 Inkscape::GC::release(doc);
501 else
502 g_warning("Unable to create extension from definition file %s.\n", filename);
503 return ext;
504 }
506 /**
507 * \return The module created
508 * \brief This function creates a module from a buffer holding an
509 * XML description.
510 * \param buffer The buffer holding the XML description of the module.
511 *
512 * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc. It
513 * finds the length of the buffer using strlen.
514 */
515 Extension *
516 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
517 {
518 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), INKSCAPE_EXTENSION_URI);
519 Extension *ext = build_from_reprdoc(doc, in_imp);
520 Inkscape::GC::release(doc);
521 return ext;
522 }
525 } } /* namespace Inkscape::Extension */
527 /*
528 Local Variables:
529 mode:c++
530 c-file-style:"stroustrup"
531 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
532 indent-tabs-mode:nil
533 fill-column:99
534 End:
535 */
536 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :