b9976595ab67abdde468c008810edc68d0ad3045
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 "db.h"
24 #include "input.h"
25 #include "output.h"
26 #include "effect.h"
27 #include "patheffect.h"
28 #include "print.h"
29 #include "implementation/script.h"
30 #include "implementation/xslt.h"
31 /* #include "implementation/plugin.h" */
33 namespace Inkscape {
34 namespace Extension {
36 static void open_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
37 static void save_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
38 static Extension *build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp);
40 /**
41 * \return A new document created from the filename passed in
42 * \brief This is a generic function to use the open function of
43 * a module (including Autodetect)
44 * \param key Identifier of which module to use
45 * \param filename The file that should be opened
46 *
47 * First things first, are we looking at an autodetection? Well if that's the case then the module
48 * needs to be found, and that is done with a database lookup through the module DB. The foreach
49 * function is called, with the parameter being a gpointer array. It contains both the filename
50 * (to find its extension) and where to write the module when it is found.
51 *
52 * If there is no autodetection, then the module database is queried with the key given.
53 *
54 * If everything is cool at this point, the module is loaded, and there is possibility for
55 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
56 * After it is finished the function continues.
57 *
58 * Lastly, the open function is called in the module itself.
59 */
60 SPDocument *
61 open(Extension *key, gchar const *filename)
62 {
63 Input *imod = NULL;
64 if (key == NULL) {
65 gpointer parray[2];
66 parray[0] = (gpointer)filename;
67 parray[1] = (gpointer)&imod;
68 db.foreach(open_internal, (gpointer)&parray);
69 } else {
70 imod = dynamic_cast<Input *>(key);
71 }
73 bool last_chance_svg = false;
74 if (key == NULL && imod == NULL) {
75 last_chance_svg = true;
76 imod = dynamic_cast<Input *>(db.get(SP_MODULE_KEY_INPUT_SVG));
77 }
79 if (imod == NULL) {
80 throw Input::no_extension_found();
81 }
83 imod->set_state(Extension::STATE_LOADED);
85 if (!imod->loaded()) {
86 throw Input::open_failed();
87 }
89 if (!imod->prefs(filename))
90 return NULL;
92 SPDocument *doc = imod->open(filename);
93 if (!doc) {
94 return NULL;
95 }
97 if (last_chance_svg) {
98 /* We can't call sp_ui_error_dialog because we may be
99 running from the console, in which case calling sp_ui
100 routines will cause a segfault. See bug 1000350 - bryce */
101 // sp_ui_error_dialog(_("Format autodetect failed. The file is being opened as SVG."));
102 g_warning(_("Format autodetect failed. The file is being opened as SVG."));
103 }
105 /* This kinda overkill as most of these are already set, but I want
106 to make sure for this release -- TJG */
107 doc->setModifiedSinceSave(false);
109 sp_document_set_uri(doc, filename);
111 return doc;
112 }
114 /**
115 * \return none
116 * \brief This is the function that searches each module to see
117 * if it matches the filename for autodetection.
118 * \param in_plug The module to be tested
119 * \param in_data An array of pointers containing the filename, and
120 * the place to put a successfully found module.
121 *
122 * Basically this function only looks at input modules as it is part of the open function. If the
123 * module is an input module, it then starts to take it apart, and the data that is passed in.
124 * Because the data being passed in is in such a weird format, there are a few casts to make it
125 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
126 * compiler.
127 *
128 * First thing that is checked is if the filename is shorter than the extension itself. There is
129 * no way for a match in that case. If it's long enough then there is a string compare of the end
130 * of the filename (for the length of the extension), and the extension itself. If this passes
131 * then the pointer passed in is set to the current module.
132 */
133 static void
134 open_internal(Extension *in_plug, gpointer in_data)
135 {
136 if (!in_plug->deactivated() && dynamic_cast<Input *>(in_plug)) {
137 gpointer *parray = (gpointer *)in_data;
138 gchar const *filename = (gchar const *)parray[0];
139 Input **pimod = (Input **)parray[1];
141 // skip all the rest if we already found a function to open it
142 // since they're ordered by preference now.
143 if (!*pimod) {
144 gchar const *ext = dynamic_cast<Input *>(in_plug)->get_extension();
146 gchar *filenamelower = g_utf8_strdown(filename, -1);
147 gchar *extensionlower = g_utf8_strdown(ext, -1);
149 if (g_str_has_suffix(filenamelower, extensionlower)) {
150 *pimod = dynamic_cast<Input *>(in_plug);
151 }
153 g_free(filenamelower);
154 g_free(extensionlower);
155 }
156 }
158 return;
159 }
161 /**
162 * \return None
163 * \brief This is a generic function to use the save function of
164 * a module (including Autodetect)
165 * \param key Identifier of which module to use
166 * \param doc The document to be saved
167 * \param filename The file that the document should be saved to
168 * \param official (optional) whether to set :output_module and :modified in the
169 * document; is true for normal save, false for temporary saves
170 *
171 * First things first, are we looking at an autodetection? Well if that's the case then the module
172 * needs to be found, and that is done with a database lookup through the module DB. The foreach
173 * function is called, with the parameter being a gpointer array. It contains both the filename
174 * (to find its extension) and where to write the module when it is found.
175 *
176 * If there is no autodetection the module database is queried with the key given.
177 *
178 * If everything is cool at this point, the module is loaded, and there is possibility for
179 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
180 * After it is finished the function continues.
181 *
182 * Lastly, the save function is called in the module itself.
183 */
184 void
185 save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official)
186 {
187 Output *omod;
188 if (key == NULL) {
189 gpointer parray[2];
190 parray[0] = (gpointer)filename;
191 parray[1] = (gpointer)&omod;
192 omod = NULL;
193 db.foreach(save_internal, (gpointer)&parray);
195 /* This is a nasty hack, but it is required to ensure that
196 autodetect will always save with the Inkscape extensions
197 if they are available. */
198 if (omod != NULL && !strcmp(omod->get_id(), SP_MODULE_KEY_OUTPUT_SVG)) {
199 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
200 }
201 /* If autodetect fails, save as Inkscape SVG */
202 if (omod == NULL) {
203 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
204 }
205 } else {
206 omod = dynamic_cast<Output *>(key);
207 }
209 if (!dynamic_cast<Output *>(omod)) {
210 g_warning("Unable to find output module to handle file: %s\n", filename);
211 throw Output::no_extension_found();
212 return;
213 }
215 omod->set_state(Extension::STATE_LOADED);
216 if (!omod->loaded()) {
217 throw Output::save_failed();
218 }
220 if (!omod->prefs())
221 return;
223 gchar *fileName = NULL;
224 if (setextension) {
225 gchar *lowerfile = g_utf8_strdown(filename, -1);
226 gchar *lowerext = g_utf8_strdown(omod->get_extension(), -1);
228 if (!g_str_has_suffix(lowerfile, lowerext)) {
229 fileName = g_strdup_printf("%s%s", filename, omod->get_extension());
230 }
232 g_free(lowerfile);
233 g_free(lowerext);
234 }
236 if (fileName == NULL) {
237 fileName = g_strdup(filename);
238 }
240 if (check_overwrite && !sp_ui_overwrite_file(fileName)) {
241 g_free(fileName);
242 throw Output::no_overwrite();
243 }
245 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
247 // remember attributes in case this is an unofficial save
248 bool saved_modified = false;
249 gchar *saved_output_extension = NULL;
250 gchar *saved_dataloss = NULL;
251 gchar *saved_uri = NULL;
252 if (!official) {
253 saved_modified = doc->isModifiedSinceSave();
254 if (repr->attribute("inkscape:output_extension")) {
255 saved_output_extension = g_strdup(repr->attribute("inkscape:output_extension"));
256 }
257 if (repr->attribute("inkscape:dataloss")) {
258 saved_dataloss = g_strdup(repr->attribute("inkscape:dataloss"));
259 }
260 if (doc->uri) {
261 saved_uri = g_strdup(doc->uri);
262 }
263 }
265 // update attributes:
266 bool saved = sp_document_get_undo_sensitive(doc);
267 sp_document_set_undo_sensitive (doc, false);
268 // save the filename for next use
269 sp_document_set_uri(doc, fileName);
270 // also save the extension for next use
271 repr->setAttribute("inkscape:output_extension", omod->get_id());
272 // set the "dataloss" attribute if the chosen extension is lossy
273 repr->setAttribute("inkscape:dataloss", NULL);
274 if ( omod->causes_dataloss() ) {
275 repr->setAttribute("inkscape:dataloss", "true");
276 }
277 sp_document_set_undo_sensitive (doc, saved);
278 doc->setModifiedSinceSave(false);
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 saved = sp_document_get_undo_sensitive(doc);
285 sp_document_set_undo_sensitive (doc, false);
286 repr->setAttribute("inkscape:output_extension", saved_output_extension);
287 repr->setAttribute("inkscape:dataloss", saved_dataloss);
288 sp_document_set_uri(doc, saved_uri);
289 sp_document_set_undo_sensitive (doc, saved);
290 doc->setModifiedSinceSave(saved_modified);
291 }
293 if (saved_output_extension) g_free(saved_output_extension);
294 if (saved_dataloss) g_free(saved_dataloss);
295 if (saved_uri) g_free(saved_uri);
297 g_free(fileName);
298 return;
299 }
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)
322 {
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;
346 }
348 Print *
349 get_print(gchar const *key)
350 {
351 return dynamic_cast<Print *>(db.get(key));
352 }
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)
371 {
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")) {
394 g_warning("Extension definition started with <%s> instead of <inkscape-extension>. Extension will not be created.\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, "input")) {
403 module_functional_type = MODULE_INPUT;
404 } else if (!strcmp(element_name, "output")) {
405 module_functional_type = MODULE_OUTPUT;
406 } else if (!strcmp(element_name, "effect")) {
407 module_functional_type = MODULE_FILTER;
408 } else if (!strcmp(element_name, "print")) {
409 module_functional_type = MODULE_PRINT;
410 } else if (!strcmp(element_name, "path-effect")) {
411 module_functional_type = MODULE_PATH_EFFECT;
412 } else if (!strcmp(element_name, "script")) {
413 module_implementation_type = MODULE_EXTENSION;
414 } else if (!strcmp(element_name, "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 /* TODO: Need to define namespace here, need to write the
498 DTD in general for this stuff */
499 Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
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;
506 }
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)
519 {
520 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
521 Extension *ext = build_from_reprdoc(doc, in_imp);
522 Inkscape::GC::release(doc);
523 return ext;
524 }
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 :