9b0c990104e438e9ceea186a210a9fe5fcaaef87
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 bool saved = sp_document_get_undo_sensitive(doc);
108 sp_document_set_undo_sensitive(doc, false);
109 doc->setModified(false);
110 sp_document_set_undo_sensitive(doc, saved);
112 sp_document_set_uri(doc, filename);
114 return doc;
115 }
117 /**
118 * \return none
119 * \brief This is the function that searches each module to see
120 * if it matches the filename for autodetection.
121 * \param in_plug The module to be tested
122 * \param in_data An array of pointers containing the filename, and
123 * the place to put a successfully found module.
124 *
125 * Basically this function only looks at input modules as it is part of the open function. If the
126 * module is an input module, it then starts to take it apart, and the data that is passed in.
127 * Because the data being passed in is in such a weird format, there are a few casts to make it
128 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
129 * compiler.
130 *
131 * First thing that is checked is if the filename is shorter than the extension itself. There is
132 * no way for a match in that case. If it's long enough then there is a string compare of the end
133 * of the filename (for the length of the extension), and the extension itself. If this passes
134 * then the pointer passed in is set to the current module.
135 */
136 static void
137 open_internal(Extension *in_plug, gpointer in_data)
138 {
139 if (!in_plug->deactivated() && dynamic_cast<Input *>(in_plug)) {
140 gpointer *parray = (gpointer *)in_data;
141 gchar const *filename = (gchar const *)parray[0];
142 Input **pimod = (Input **)parray[1];
144 // skip all the rest if we already found a function to open it
145 // since they're ordered by preference now.
146 if (!*pimod) {
147 gchar const *ext = dynamic_cast<Input *>(in_plug)->get_extension();
149 gchar *filenamelower = g_utf8_strdown(filename, -1);
150 gchar *extensionlower = g_utf8_strdown(ext, -1);
152 if (g_str_has_suffix(filenamelower, extensionlower)) {
153 *pimod = dynamic_cast<Input *>(in_plug);
154 }
156 g_free(filenamelower);
157 g_free(extensionlower);
158 }
159 }
161 return;
162 }
164 /**
165 * \return None
166 * \brief This is a generic function to use the save function of
167 * a module (including Autodetect)
168 * \param key Identifier of which module to use
169 * \param doc The document to be saved
170 * \param filename The file that the document should be saved to
171 * \param official (optional) whether to set :output_module and :modified in the
172 * document; is true for normal save, false for temporary saves
173 *
174 * First things first, are we looking at an autodetection? Well if that's the case then the module
175 * needs to be found, and that is done with a database lookup through the module DB. The foreach
176 * function is called, with the parameter being a gpointer array. It contains both the filename
177 * (to find its extension) and where to write the module when it is found.
178 *
179 * If there is no autodetection the module database is queried with the key given.
180 *
181 * If everything is cool at this point, the module is loaded, and there is possibility for
182 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
183 * After it is finished the function continues.
184 *
185 * Lastly, the save function is called in the module itself.
186 */
187 void
188 save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official)
189 {
190 Output *omod;
191 if (key == NULL) {
192 gpointer parray[2];
193 parray[0] = (gpointer)filename;
194 parray[1] = (gpointer)&omod;
195 omod = NULL;
196 db.foreach(save_internal, (gpointer)&parray);
198 /* This is a nasty hack, but it is required to ensure that
199 autodetect will always save with the Inkscape extensions
200 if they are available. */
201 if (omod != NULL && !strcmp(omod->get_id(), SP_MODULE_KEY_OUTPUT_SVG)) {
202 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
203 }
204 /* If autodetect fails, save as Inkscape SVG */
205 if (omod == NULL) {
206 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
207 }
208 } else {
209 omod = dynamic_cast<Output *>(key);
210 }
212 if (!dynamic_cast<Output *>(omod)) {
213 g_warning("Unable to find output module to handle file: %s\n", filename);
214 throw Output::no_extension_found();
215 return;
216 }
218 omod->set_state(Extension::STATE_LOADED);
219 if (!omod->loaded()) {
220 throw Output::save_failed();
221 }
223 if (!omod->prefs())
224 return;
226 gchar *fileName = NULL;
227 if (setextension) {
228 gchar *lowerfile = g_utf8_strdown(filename, -1);
229 gchar *lowerext = g_utf8_strdown(omod->get_extension(), -1);
231 if (!g_str_has_suffix(lowerfile, lowerext)) {
232 fileName = g_strdup_printf("%s%s", filename, omod->get_extension());
233 }
235 g_free(lowerfile);
236 g_free(lowerext);
237 }
239 if (fileName == NULL) {
240 fileName = g_strdup(filename);
241 }
243 if (check_overwrite && !sp_ui_overwrite_file(fileName)) {
244 g_free(fileName);
245 throw Output::no_overwrite();
246 }
248 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
250 // remember attributes in case this is an unofficial save
251 bool saved_modified = false;
252 gchar *saved_output_extension = NULL;
253 gchar *saved_dataloss = NULL;
254 gchar *saved_uri = NULL;
255 if (!official) {
256 saved_modified = doc->isModified();
257 if (repr->attribute("inkscape:output_extension")) {
258 saved_output_extension = g_strdup(repr->attribute("inkscape:output_extension"));
259 }
260 if (repr->attribute("inkscape:dataloss")) {
261 saved_dataloss = g_strdup(repr->attribute("inkscape:dataloss"));
262 }
263 if (doc->uri) {
264 saved_uri = g_strdup(doc->uri);
265 }
266 }
268 // update attributes:
269 bool saved = sp_document_get_undo_sensitive(doc);
270 sp_document_set_undo_sensitive (doc, false);
271 doc->setModified(false);
272 // save the filename for next use
273 sp_document_set_uri(doc, fileName);
274 // also save the extension for next use
275 repr->setAttribute("inkscape:output_extension", omod->get_id());
276 // set the "dataloss" attribute if the chosen extension is lossy
277 repr->setAttribute("inkscape:dataloss", NULL);
278 if ( omod->causes_dataloss() ) {
279 repr->setAttribute("inkscape:dataloss", "true");
280 }
281 sp_document_set_undo_sensitive (doc, saved);
283 omod->save(doc, fileName);
285 // if it is an unofficial save, set the modified attributes back to what they were
286 if ( !official) {
287 saved = sp_document_get_undo_sensitive(doc);
288 sp_document_set_undo_sensitive (doc, false);
289 doc->setModified(saved_modified);
290 repr->setAttribute("inkscape:output_extension", saved_output_extension);
291 repr->setAttribute("inkscape:dataloss", saved_dataloss);
292 sp_document_set_uri(doc, saved_uri);
293 sp_document_set_undo_sensitive (doc, saved);
294 }
296 if (saved_output_extension) g_free(saved_output_extension);
297 if (saved_dataloss) g_free(saved_dataloss);
298 if (saved_uri) g_free(saved_uri);
300 g_free(fileName);
301 return;
302 }
304 /**
305 * \return none
306 * \brief This is the function that searches each module to see
307 * if it matches the filename for autodetection.
308 * \param in_plug The module to be tested
309 * \param in_data An array of pointers containing the filename, and
310 * the place to put a successfully found module.
311 *
312 * Basically this function only looks at output modules as it is part of the open function. If the
313 * module is an output module, it then starts to take it apart, and the data that is passed in.
314 * Because the data being passed in is in such a weird format, there are a few casts to make it
315 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
316 * compiler.
317 *
318 * First thing that is checked is if the filename is shorter than the extension itself. There is
319 * no way for a match in that case. If it's long enough then there is a string compare of the end
320 * of the filename (for the length of the extension), and the extension itself. If this passes
321 * then the pointer passed in is set to the current module.
322 */
323 static void
324 save_internal(Extension *in_plug, gpointer in_data)
325 {
326 if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
327 gpointer *parray = (gpointer *)in_data;
328 gchar const *filename = (gchar const *)parray[0];
329 Output **pomod = (Output **)parray[1];
331 // skip all the rest if we already found someone to save it
332 // since they're ordered by preference now.
333 if (!*pomod) {
334 gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
336 gchar *filenamelower = g_utf8_strdown(filename, -1);
337 gchar *extensionlower = g_utf8_strdown(ext, -1);
339 if (g_str_has_suffix(filenamelower, extensionlower)) {
340 *pomod = dynamic_cast<Output *>(in_plug);
341 }
343 g_free(filenamelower);
344 g_free(extensionlower);
345 }
346 }
348 return;
349 }
351 Print *
352 get_print(gchar const *key)
353 {
354 return dynamic_cast<Print *>(db.get(key));
355 }
357 /**
358 * \return The built module
359 * \brief Creates a module from a Inkscape::XML::Document describing the module
360 * \param doc The XML description of the module
361 *
362 * This function basically has two segments. The first is that it goes through the Repr tree
363 * provided, and determines what kind of of module this is, and what kind of implementation to use.
364 * All of these are then stored in two enums that are defined in this function. This makes it
365 * easier to add additional types (which will happen in the future, I'm sure).
366 *
367 * Second, there is case statements for these enums. The first one is the type of module. This is
368 * the one where the module is actually created. After that, then the implementation is applied to
369 * get the load and unload functions. If there is no implementation then these are not set. This
370 * case could apply to modules that are built in (like the SVG load/save functions).
371 */
372 static Extension *
373 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
374 {
375 enum {
376 MODULE_EXTENSION,
377 MODULE_XSLT,
378 /* MODULE_PLUGIN, */
379 MODULE_UNKNOWN_IMP
380 } module_implementation_type = MODULE_UNKNOWN_IMP;
381 enum {
382 MODULE_INPUT,
383 MODULE_OUTPUT,
384 MODULE_FILTER,
385 MODULE_PRINT,
386 MODULE_PATH_EFFECT,
387 MODULE_UNKNOWN_FUNC
388 } module_functional_type = MODULE_UNKNOWN_FUNC;
390 g_return_val_if_fail(doc != NULL, NULL);
392 Inkscape::XML::Node *repr = doc->root();
394 /* sp_repr_print(repr); */
396 if (strcmp(repr->name(), "inkscape-extension")) {
397 g_warning("Extension definition started with <%s> instead of <inkscape-extension>. Extension will not be created.\n", repr->name());
398 return NULL;
399 }
401 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
402 while (child_repr != NULL) {
403 char const *element_name = child_repr->name();
404 /* printf("Child: %s\n", child_repr->name()); */
405 if (!strcmp(element_name, "input")) {
406 module_functional_type = MODULE_INPUT;
407 } else if (!strcmp(element_name, "output")) {
408 module_functional_type = MODULE_OUTPUT;
409 } else if (!strcmp(element_name, "effect")) {
410 module_functional_type = MODULE_FILTER;
411 } else if (!strcmp(element_name, "print")) {
412 module_functional_type = MODULE_PRINT;
413 } else if (!strcmp(element_name, "path-effect")) {
414 module_functional_type = MODULE_PATH_EFFECT;
415 } else if (!strcmp(element_name, "script")) {
416 module_implementation_type = MODULE_EXTENSION;
417 } else if (!strcmp(element_name, "xslt")) {
418 module_implementation_type = MODULE_XSLT;
419 #if 0
420 } else if (!strcmp(element_name, "plugin")) {
421 module_implementation_type = MODULE_PLUGIN;
422 #endif
423 }
425 //Inkscape::XML::Node *old_repr = child_repr;
426 child_repr = sp_repr_next(child_repr);
427 //Inkscape::GC::release(old_repr);
428 }
430 Implementation::Implementation *imp;
431 if (in_imp == NULL) {
432 switch (module_implementation_type) {
433 case MODULE_EXTENSION: {
434 Implementation::Script *script = new Implementation::Script();
435 imp = static_cast<Implementation::Implementation *>(script);
436 break;
437 }
438 case MODULE_XSLT: {
439 Implementation::XSLT *xslt = new Implementation::XSLT();
440 imp = static_cast<Implementation::Implementation *>(xslt);
441 break;
442 }
443 #if 0
444 case MODULE_PLUGIN: {
445 Implementation::Plugin *plugin = new Implementation::Plugin();
446 imp = static_cast<Implementation::Implementation *>(plugin);
447 break;
448 }
449 #endif
450 default: {
451 imp = NULL;
452 break;
453 }
454 }
455 } else {
456 imp = in_imp;
457 }
459 Extension *module = NULL;
460 switch (module_functional_type) {
461 case MODULE_INPUT: {
462 module = new Input(repr, imp);
463 break;
464 }
465 case MODULE_OUTPUT: {
466 module = new Output(repr, imp);
467 break;
468 }
469 case MODULE_FILTER: {
470 module = new Effect(repr, imp);
471 break;
472 }
473 case MODULE_PRINT: {
474 module = new Print(repr, imp);
475 break;
476 }
477 case MODULE_PATH_EFFECT: {
478 module = new PathEffect(repr, imp);
479 break;
480 }
481 default: {
482 break;
483 }
484 }
486 return module;
487 }
489 /**
490 * \return The module created
491 * \brief This function creates a module from a filename of an
492 * XML description.
493 * \param filename The file holding the XML description of the module.
494 *
495 * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
496 */
497 Extension *
498 build_from_file(gchar const *filename)
499 {
500 /* TODO: Need to define namespace here, need to write the
501 DTD in general for this stuff */
502 Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
503 Extension *ext = build_from_reprdoc(doc, NULL);
504 if (ext != NULL)
505 Inkscape::GC::release(doc);
506 else
507 g_warning("Unable to create extension from definition file %s.\n", filename);
508 return ext;
509 }
511 /**
512 * \return The module created
513 * \brief This function creates a module from a buffer holding an
514 * XML description.
515 * \param buffer The buffer holding the XML description of the module.
516 *
517 * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc. It
518 * finds the length of the buffer using strlen.
519 */
520 Extension *
521 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
522 {
523 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
524 Extension *ext = build_from_reprdoc(doc, in_imp);
525 Inkscape::GC::release(doc);
526 return ext;
527 }
530 } } /* namespace Inkscape::Extension */
532 /*
533 Local Variables:
534 mode:c++
535 c-file-style:"stroustrup"
536 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
537 indent-tabs-mode:nil
538 fill-column:99
539 End:
540 */
541 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :