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 *
10 * Copyright (C) 2002-2004 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <interface.h>
21 #include "db.h"
22 #include "input.h"
23 #include "output.h"
24 #include "effect.h"
25 #include "patheffect.h"
26 #include "print.h"
27 #include "implementation/script.h"
28 #include "implementation/xslt.h"
29 /* #include "implementation/plugin.h" */
31 namespace Inkscape {
32 namespace Extension {
34 static void open_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
35 static void save_internal(Inkscape::Extension::Extension *in_plug, gpointer in_data);
36 static Extension *build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp);
38 /**
39 * \return A new document created from the filename passed in
40 * \brief This is a generic function to use the open function of
41 * a module (including Autodetect)
42 * \param key Identifier of which module to use
43 * \param filename The file that should be opened
44 *
45 * First things first, are we looking at an autodetection? Well if that's the case then the module
46 * needs to be found, and that is done with a database lookup through the module DB. The foreach
47 * function is called, with the parameter being a gpointer array. It contains both the filename
48 * (to find its extension) and where to write the module when it is found.
49 *
50 * If there is no autodetection, then the module database is queried with the key given.
51 *
52 * If everything is cool at this point, the module is loaded, and there is possibility for
53 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
54 * After it is finished the function continues.
55 *
56 * Lastly, the open function is called in the module itself.
57 */
58 SPDocument *
59 open(Extension *key, gchar const *filename)
60 {
61 Input *imod = NULL;
62 if (key == NULL) {
63 gpointer parray[2];
64 parray[0] = (gpointer)filename;
65 parray[1] = (gpointer)&imod;
66 db.foreach(open_internal, (gpointer)&parray);
67 } else {
68 imod = dynamic_cast<Input *>(key);
69 }
71 bool last_chance_svg = false;
72 if (key == NULL && imod == NULL) {
73 last_chance_svg = true;
74 imod = dynamic_cast<Input *>(db.get(SP_MODULE_KEY_INPUT_SVG));
75 }
77 if (imod == NULL) {
78 throw Input::no_extension_found();
79 }
81 imod->set_state(Extension::STATE_LOADED);
83 if (!imod->loaded()) {
84 throw Input::open_failed();
85 }
87 if (!imod->prefs(filename))
88 return NULL;
90 SPDocument *doc = imod->open(filename);
91 if (!doc) {
92 return NULL;
93 }
95 if (last_chance_svg) {
96 /* We can't call sp_ui_error_dialog because we may be
97 running from the console, in which case calling sp_ui
98 routines will cause a segfault. See bug 1000350 - bryce */
99 // sp_ui_error_dialog(_("Format autodetect failed. The file is being opened as SVG."));
100 g_warning(_("Format autodetect failed. The file is being opened as SVG."));
101 }
103 /* This kinda overkill as most of these are already set, but I want
104 to make sure for this release -- TJG */
105 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
106 gboolean saved = sp_document_get_undo_sensitive(doc);
107 sp_document_set_undo_sensitive(doc, FALSE);
108 repr->setAttribute("sodipodi:modified", NULL);
109 sp_document_set_undo_sensitive(doc, saved);
111 sp_document_set_uri(doc, filename);
113 return doc;
114 }
116 /**
117 * \return none
118 * \brief This is the function that searches each module to see
119 * if it matches the filename for autodetection.
120 * \param in_plug The module to be tested
121 * \param in_data An array of pointers containing the filename, and
122 * the place to put a successfully found module.
123 *
124 * Basically this function only looks at input modules as it is part of the open function. If the
125 * module is an input module, it then starts to take it apart, and the data that is passed in.
126 * Because the data being passed in is in such a weird format, there are a few casts to make it
127 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
128 * compiler.
129 *
130 * First thing that is checked is if the filename is shorter than the extension itself. There is
131 * no way for a match in that case. If it's long enough then there is a string compare of the end
132 * of the filename (for the length of the extension), and the extension itself. If this passes
133 * then the pointer passed in is set to the current module.
134 */
135 static void
136 open_internal(Extension *in_plug, gpointer in_data)
137 {
138 if (!in_plug->deactivated() && dynamic_cast<Input *>(in_plug)) {
139 gpointer *parray = (gpointer *)in_data;
140 gchar const *filename = (gchar const *)parray[0];
141 Input **pimod = (Input **)parray[1];
143 // skip all the rest if we already found a function to open it
144 // since they're ordered by preference now.
145 if (!*pimod) {
146 gchar const *ext = dynamic_cast<Input *>(in_plug)->get_extension();
148 gchar *filenamelower = g_utf8_strdown(filename, -1);
149 gchar *extensionlower = g_utf8_strdown(ext, -1);
151 if (g_str_has_suffix(filenamelower, extensionlower)) {
152 *pimod = dynamic_cast<Input *>(in_plug);
153 }
155 g_free(filenamelower);
156 g_free(extensionlower);
157 }
158 }
160 return;
161 }
163 /**
164 * \return None
165 * \brief This is a generic function to use the save function of
166 * a module (including Autodetect)
167 * \param key Identifier of which module to use
168 * \param doc The document to be saved
169 * \param filename The file that the document should be saved to
170 * \param official (optional) whether to set :output_module and :modified in the
171 * document; is true for normal save, false for temporary saves
172 *
173 * First things first, are we looking at an autodetection? Well if that's the case then the module
174 * needs to be found, and that is done with a database lookup through the module DB. The foreach
175 * function is called, with the parameter being a gpointer array. It contains both the filename
176 * (to find its extension) and where to write the module when it is found.
177 *
178 * If there is no autodetection the module database is queried with the key given.
179 *
180 * If everything is cool at this point, the module is loaded, and there is possibility for
181 * preferences. If there is a function, then it is executed to get the dialog to be displayed.
182 * After it is finished the function continues.
183 *
184 * Lastly, the save function is called in the module itself.
185 */
186 void
187 save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official)
188 {
189 Output *omod;
190 if (key == NULL) {
191 gpointer parray[2];
192 parray[0] = (gpointer)filename;
193 parray[1] = (gpointer)&omod;
194 omod = NULL;
195 db.foreach(save_internal, (gpointer)&parray);
197 /* This is a nasty hack, but it is required to ensure that
198 autodetect will always save with the Inkscape extensions
199 if they are available. */
200 if (omod != NULL && !strcmp(omod->get_id(), SP_MODULE_KEY_OUTPUT_SVG)) {
201 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
202 }
203 /* If autodetect fails, save as Inkscape SVG */
204 if (omod == NULL) {
205 omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
206 }
207 } else {
208 omod = dynamic_cast<Output *>(key);
209 }
211 if (!dynamic_cast<Output *>(omod)) {
212 g_warning("Unable to find output module to handle file: %s\n", filename);
213 throw Output::no_extension_found();
214 return;
215 }
217 omod->set_state(Extension::STATE_LOADED);
218 if (!omod->loaded()) {
219 throw Output::save_failed();
220 }
222 if (!omod->prefs())
223 return;
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 // if 'official' save the filename and extension for future saves.
248 if (official) {
249 // save the filename for next use
250 sp_document_set_uri(doc, fileName);
251 // also save the extension for next use
252 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
253 repr->setAttribute("inkscape:output_extension", omod->get_id());
254 }
256 omod->save(doc, fileName);
258 g_free(fileName);
259 return;
260 }
262 /**
263 * \return none
264 * \brief This is the function that searches each module to see
265 * if it matches the filename for autodetection.
266 * \param in_plug The module to be tested
267 * \param in_data An array of pointers containing the filename, and
268 * the place to put a successfully found module.
269 *
270 * Basically this function only looks at output modules as it is part of the open function. If the
271 * module is an output module, it then starts to take it apart, and the data that is passed in.
272 * Because the data being passed in is in such a weird format, there are a few casts to make it
273 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
274 * compiler.
275 *
276 * First thing that is checked is if the filename is shorter than the extension itself. There is
277 * no way for a match in that case. If it's long enough then there is a string compare of the end
278 * of the filename (for the length of the extension), and the extension itself. If this passes
279 * then the pointer passed in is set to the current module.
280 */
281 static void
282 save_internal(Extension *in_plug, gpointer in_data)
283 {
284 if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
285 gpointer *parray = (gpointer *)in_data;
286 gchar const *filename = (gchar const *)parray[0];
287 Output **pomod = (Output **)parray[1];
289 // skip all the rest if we already found someone to save it
290 // since they're ordered by preference now.
291 if (!*pomod) {
292 gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
294 gchar *filenamelower = g_utf8_strdown(filename, -1);
295 gchar *extensionlower = g_utf8_strdown(ext, -1);
297 if (g_str_has_suffix(filenamelower, extensionlower)) {
298 *pomod = dynamic_cast<Output *>(in_plug);
299 }
301 g_free(filenamelower);
302 g_free(extensionlower);
303 }
304 }
306 return;
307 }
309 Print *
310 get_print(gchar const *key)
311 {
312 return dynamic_cast<Print *>(db.get(key));
313 }
315 /**
316 * \return The built module
317 * \brief Creates a module from a Inkscape::XML::Document describing the module
318 * \param doc The XML description of the module
319 *
320 * This function basically has two segments. The first is that it goes through the Repr tree
321 * provided, and determines what kind of of module this is, and what kind of implementation to use.
322 * All of these are then stored in two enums that are defined in this function. This makes it
323 * easier to add additional types (which will happen in the future, I'm sure).
324 *
325 * Second, there is case statements for these enums. The first one is the type of module. This is
326 * the one where the module is actually created. After that, then the implementation is applied to
327 * get the load and unload functions. If there is no implementation then these are not set. This
328 * case could apply to modules that are built in (like the SVG load/save functions).
329 */
330 static Extension *
331 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
332 {
333 enum {
334 MODULE_EXTENSION,
335 MODULE_XSLT,
336 /* MODULE_PLUGIN, */
337 MODULE_UNKNOWN_IMP
338 } module_implementation_type = MODULE_UNKNOWN_IMP;
339 enum {
340 MODULE_INPUT,
341 MODULE_OUTPUT,
342 MODULE_FILTER,
343 MODULE_PRINT,
344 MODULE_PATH_EFFECT,
345 MODULE_UNKNOWN_FUNC
346 } module_functional_type = MODULE_UNKNOWN_FUNC;
348 g_return_val_if_fail(doc != NULL, NULL);
350 Inkscape::XML::Node *repr = sp_repr_document_root(doc);
352 /* sp_repr_print(repr); */
354 if (strcmp(repr->name(), "inkscape-extension")) {
355 g_warning("Extension definition started with <%s> instead of <inkscape-extension>. Extension will not be created.\n", repr->name());
356 return NULL;
357 }
359 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
360 while (child_repr != NULL) {
361 char const *element_name = child_repr->name();
362 /* printf("Child: %s\n", child_repr->name()); */
363 if (!strcmp(element_name, "input")) {
364 module_functional_type = MODULE_INPUT;
365 } else if (!strcmp(element_name, "output")) {
366 module_functional_type = MODULE_OUTPUT;
367 } else if (!strcmp(element_name, "effect")) {
368 module_functional_type = MODULE_FILTER;
369 } else if (!strcmp(element_name, "print")) {
370 module_functional_type = MODULE_PRINT;
371 } else if (!strcmp(element_name, "path-effect")) {
372 module_functional_type = MODULE_PATH_EFFECT;
373 } else if (!strcmp(element_name, "script")) {
374 module_implementation_type = MODULE_EXTENSION;
375 } else if (!strcmp(element_name, "xslt")) {
376 module_implementation_type = MODULE_XSLT;
377 #if 0
378 } else if (!strcmp(element_name, "plugin")) {
379 module_implementation_type = MODULE_PLUGIN;
380 #endif
381 }
383 //Inkscape::XML::Node *old_repr = child_repr;
384 child_repr = sp_repr_next(child_repr);
385 //Inkscape::GC::release(old_repr);
386 }
388 Implementation::Implementation *imp;
389 if (in_imp == NULL) {
390 switch (module_implementation_type) {
391 case MODULE_EXTENSION: {
392 Implementation::Script *script = new Implementation::Script();
393 imp = static_cast<Implementation::Implementation *>(script);
394 break;
395 }
396 case MODULE_XSLT: {
397 Implementation::XSLT *xslt = new Implementation::XSLT();
398 imp = static_cast<Implementation::Implementation *>(xslt);
399 break;
400 }
401 #if 0
402 case MODULE_PLUGIN: {
403 Implementation::Plugin *plugin = new Implementation::Plugin();
404 imp = static_cast<Implementation::Implementation *>(plugin);
405 break;
406 }
407 #endif
408 default: {
409 imp = NULL;
410 break;
411 }
412 }
413 } else {
414 imp = in_imp;
415 }
417 Extension *module = NULL;
418 switch (module_functional_type) {
419 case MODULE_INPUT: {
420 module = new Input(repr, imp);
421 break;
422 }
423 case MODULE_OUTPUT: {
424 module = new Output(repr, imp);
425 break;
426 }
427 case MODULE_FILTER: {
428 module = new Effect(repr, imp);
429 break;
430 }
431 case MODULE_PRINT: {
432 module = new Print(repr, imp);
433 break;
434 }
435 case MODULE_PATH_EFFECT: {
436 module = new PathEffect(repr, imp);
437 break;
438 }
439 default: {
440 break;
441 }
442 }
444 return module;
445 }
447 /**
448 * \return The module created
449 * \brief This function creates a module from a filename of an
450 * XML description.
451 * \param filename The file holding the XML description of the module.
452 *
453 * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
454 */
455 Extension *
456 build_from_file(gchar const *filename)
457 {
458 /* TODO: Need to define namespace here, need to write the
459 DTD in general for this stuff */
460 Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
461 Extension *ext = build_from_reprdoc(doc, NULL);
462 if (ext != NULL)
463 Inkscape::GC::release(doc);
464 else
465 g_warning("Unable to create extension from definition file %s.\n", filename);
466 return ext;
467 }
469 /**
470 * \return The module created
471 * \brief This function creates a module from a buffer holding an
472 * XML description.
473 * \param buffer The buffer holding the XML description of the module.
474 *
475 * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc. It
476 * finds the length of the buffer using strlen.
477 */
478 Extension *
479 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
480 {
481 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
482 Extension *ext = build_from_reprdoc(doc, in_imp);
483 Inkscape::GC::release(doc);
484 return ext;
485 }
488 } } /* namespace Inkscape::Extension */
490 /*
491 Local Variables:
492 mode:c++
493 c-file-style:"stroustrup"
494 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
495 indent-tabs-mode:nil
496 fill-column:99
497 End:
498 */
499 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :