Code

Merge from fe-moved
[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         if (repr->attribute("inkscape:output_extension")) {
257             saved_output_extension = g_strdup(repr->attribute("inkscape:output_extension"));
258         }
259         if (repr->attribute("inkscape:dataloss")) {
260             saved_dataloss = g_strdup(repr->attribute("inkscape:dataloss"));
261         }
262         if (doc->uri) {
263             saved_uri = g_strdup(doc->uri);    
264         }
265     }    
267     // update attributes:
268     bool saved = sp_document_get_undo_sensitive(doc);
269     sp_document_set_undo_sensitive (doc, false); 
270         // save the filename for next use
271         sp_document_set_uri(doc, fileName);
272         // also save the extension for next use
273         repr->setAttribute("inkscape:output_extension", omod->get_id());
274         // set the "dataloss" attribute if the chosen extension is lossy
275         repr->setAttribute("inkscape:dataloss", NULL);
276         if ( omod->causes_dataloss() ) {
277             repr->setAttribute("inkscape:dataloss", "true");
278         }
279     sp_document_set_undo_sensitive (doc, saved);
280     doc->setModifiedSinceSave(false);
282     omod->save(doc, fileName);
283     
284     // if it is an unofficial save, set the modified attributes back to what they were    
285     if ( !official) {
286         saved = sp_document_get_undo_sensitive(doc);
287         sp_document_set_undo_sensitive (doc, false);
288             repr->setAttribute("inkscape:output_extension", saved_output_extension);
289             repr->setAttribute("inkscape:dataloss", saved_dataloss);
290             sp_document_set_uri(doc, saved_uri);
291         sp_document_set_undo_sensitive (doc, saved);
292         doc->setModifiedSinceSave(saved_modified);
293     }
294     
295     if (saved_output_extension)  g_free(saved_output_extension);
296     if (saved_dataloss)          g_free(saved_dataloss);
297     if (saved_uri)               g_free(saved_uri);    
298     
299     g_free(fileName);
300     return;
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)
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;
350 Print *
351 get_print(gchar const *key)
353     return dynamic_cast<Print *>(db.get(key));
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)
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     /* sp_repr_print(repr); */
395     if (strcmp(repr->name(), INKSCAPE_EXTENSION_NS "inkscape-extension")) {
396         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());
397         return NULL;
398     }
400     Inkscape::XML::Node *child_repr = sp_repr_children(repr);
401     while (child_repr != NULL) {
402         char const *element_name = child_repr->name();
403         /* printf("Child: %s\n", child_repr->name()); */
404         if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "input")) {
405             module_functional_type = MODULE_INPUT;
406         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "output")) {
407             module_functional_type = MODULE_OUTPUT;
408         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "effect")) {
409             module_functional_type = MODULE_FILTER;
410         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "print")) {
411             module_functional_type = MODULE_PRINT;
412         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "path-effect")) {
413             module_functional_type = MODULE_PATH_EFFECT;
414         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "script")) {
415             module_implementation_type = MODULE_EXTENSION;
416         } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "xslt")) {
417             module_implementation_type = MODULE_XSLT;
418 #if 0
419         } else if (!strcmp(element_name, "plugin")) {
420             module_implementation_type = MODULE_PLUGIN;
421 #endif
422         }
424         //Inkscape::XML::Node *old_repr = child_repr;
425         child_repr = sp_repr_next(child_repr);
426         //Inkscape::GC::release(old_repr);
427     }
429     Implementation::Implementation *imp;
430     if (in_imp == NULL) {
431         switch (module_implementation_type) {
432             case MODULE_EXTENSION: {
433                 Implementation::Script *script = new Implementation::Script();
434                 imp = static_cast<Implementation::Implementation *>(script);
435                 break;
436             }
437             case MODULE_XSLT: {
438                 Implementation::XSLT *xslt = new Implementation::XSLT();
439                 imp = static_cast<Implementation::Implementation *>(xslt);
440                 break;
441             }
442 #if 0
443             case MODULE_PLUGIN: {
444                 Implementation::Plugin *plugin = new Implementation::Plugin();
445                 imp = static_cast<Implementation::Implementation *>(plugin);
446                 break;
447             }
448 #endif
449             default: {
450                 imp = NULL;
451                 break;
452             }
453         }
454     } else {
455         imp = in_imp;
456     }
458     Extension *module = NULL;
459     switch (module_functional_type) {
460         case MODULE_INPUT: {
461             module = new Input(repr, imp);
462             break;
463         }
464         case MODULE_OUTPUT: {
465             module = new Output(repr, imp);
466             break;
467         }
468         case MODULE_FILTER: {
469             module = new Effect(repr, imp);
470             break;
471         }
472         case MODULE_PRINT: {
473             module = new Print(repr, imp);
474             break;
475         }
476         case MODULE_PATH_EFFECT: {
477             module = new PathEffect(repr, imp);
478             break;
479         }
480         default: {
481             break;
482         }
483     }
485     return module;
488 /**
489  * \return   The module created
490  * \brief    This function creates a module from a filename of an
491  *           XML description.
492  * \param    filename  The file holding the XML description of the module.
493  *
494  * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
495  */
496 Extension *
497 build_from_file(gchar const *filename)
499     Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI);
500     Extension *ext = build_from_reprdoc(doc, NULL);
501     if (ext != NULL)
502         Inkscape::GC::release(doc);
503     else
504         g_warning("Unable to create extension from definition file %s.\n", filename);
505     return ext;
508 /**
509  * \return   The module created
510  * \brief    This function creates a module from a buffer holding an
511  *           XML description.
512  * \param    buffer  The buffer holding the XML description of the module.
513  *
514  * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc.  It
515  * finds the length of the buffer using strlen.
516  */
517 Extension *
518 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
520     Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), INKSCAPE_EXTENSION_URI);
521     Extension *ext = build_from_reprdoc(doc, in_imp);
522     Inkscape::GC::release(doc);
523     return ext;
527 } } /* namespace Inkscape::Extension */
529 /*
530   Local Variables:
531   mode:c++
532   c-file-style:"stroustrup"
533   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
534   indent-tabs-mode:nil
535   fill-column:99
536   End:
537 */
538 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :