Code

edbe42dac48f1d157314e5d868ab971323d64348
[inkscape.git] / src / extension / system.cpp
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;
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)
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;
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)
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     bool saved = sp_document_get_undo_sensitive(doc);
263     sp_document_set_undo_sensitive (doc, false); 
264     {
265         // save the filename for next use
266         sp_document_set_uri(doc, fileName);
267         // also save the extension for next use
268         repr->setAttribute("inkscape:output_extension", omod->get_id());
269         // set the "dataloss" attribute if the chosen extension is lossy
270         repr->setAttribute("inkscape:dataloss", NULL);
271         if ( omod->causes_dataloss() ) {
272             repr->setAttribute("inkscape:dataloss", "true");
273         }
274     }
275     sp_document_set_undo_sensitive (doc, saved);
276     doc->setModifiedSinceSave(false);
278     omod->save(doc, fileName);
279     
280     // if it is an unofficial save, set the modified attributes back to what they were    
281     if ( !official) {
282         saved = sp_document_get_undo_sensitive(doc);
283         sp_document_set_undo_sensitive (doc, false);
284         {
285             repr->setAttribute("inkscape:output_extension", saved_output_extension);
286             repr->setAttribute("inkscape:dataloss", saved_dataloss);
287             sp_document_set_uri(doc, saved_uri);
288         }
289         sp_document_set_undo_sensitive (doc, saved);
290         doc->setModifiedSinceSave(saved_modified);
292         g_free(saved_output_extension);
293         g_free(saved_dataloss);
294         g_free(saved_uri);
295     }
296     
297     g_free(fileName);
298     return;
301 /**
302  * \return   none
303  * \brief    This is the function that searches each module to see
304  *           if it matches the filename for autodetection.
305  * \param    in_plug  The module to be tested
306  * \param    in_data  An array of pointers containing the filename, and
307  *                    the place to put a successfully found module.
308  *
309  * Basically this function only looks at output modules as it is part of the open function.  If the
310  * module is an output module, it then starts to take it apart, and the data that is passed in.
311  * Because the data being passed in is in such a weird format, there are a few casts to make it
312  * easier to use.  While it looks like a lot of local variables, they'll all get removed by the
313  * compiler.
314  *
315  * First thing that is checked is if the filename is shorter than the extension itself.  There is
316  * no way for a match in that case.  If it's long enough then there is a string compare of the end
317  * of the filename (for the length of the extension), and the extension itself.  If this passes
318  * then the pointer passed in is set to the current module.
319  */
320 static void
321 save_internal(Extension *in_plug, gpointer in_data)
323     if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
324         gpointer *parray = (gpointer *)in_data;
325         gchar const *filename = (gchar const *)parray[0];
326         Output **pomod = (Output **)parray[1];
328         // skip all the rest if we already found someone to save it
329         // since they're ordered by preference now.
330         if (!*pomod) {
331             gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
333             gchar *filenamelower = g_utf8_strdown(filename, -1);
334             gchar *extensionlower = g_utf8_strdown(ext, -1);
336             if (g_str_has_suffix(filenamelower, extensionlower)) {
337                 *pomod = dynamic_cast<Output *>(in_plug);
338             }
340             g_free(filenamelower);
341             g_free(extensionlower);
342         }
343     }
345     return;
348 Print *
349 get_print(gchar const *key)
351     return dynamic_cast<Print *>(db.get(key));
354 /**
355  * \return   The built module
356  * \brief    Creates a module from a Inkscape::XML::Document describing the module
357  * \param    doc  The XML description of the module
358  *
359  * This function basically has two segments.  The first is that it goes through the Repr tree
360  * provided, and determines what kind of of module this is, and what kind of implementation to use.
361  * All of these are then stored in two enums that are defined in this function.  This makes it
362  * easier to add additional types (which will happen in the future, I'm sure).
363  *
364  * Second, there is case statements for these enums.  The first one is the type of module.  This is
365  * the one where the module is actually created.  After that, then the implementation is applied to
366  * get the load and unload functions.  If there is no implementation then these are not set.  This
367  * case could apply to modules that are built in (like the SVG load/save functions).
368  */
369 static Extension *
370 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
372     enum {
373         MODULE_EXTENSION,
374         MODULE_XSLT,
375         /* MODULE_PLUGIN, */
376         MODULE_UNKNOWN_IMP
377     } module_implementation_type = MODULE_UNKNOWN_IMP;
378     enum {
379         MODULE_INPUT,
380         MODULE_OUTPUT,
381         MODULE_FILTER,
382         MODULE_PRINT,
383         MODULE_PATH_EFFECT,
384         MODULE_UNKNOWN_FUNC
385     } module_functional_type = MODULE_UNKNOWN_FUNC;
387     g_return_val_if_fail(doc != NULL, NULL);
389     Inkscape::XML::Node *repr = doc->root();
391     /* sp_repr_print(repr); */
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;
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)
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;
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)
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;
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 :