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) {
248 sp_document_set_uri(doc, fileName);
249 }
251 omod->save(doc, fileName);
253 g_free(fileName);
254 return;
255 }
257 /**
258 * \return none
259 * \brief This is the function that searches each module to see
260 * if it matches the filename for autodetection.
261 * \param in_plug The module to be tested
262 * \param in_data An array of pointers containing the filename, and
263 * the place to put a successfully found module.
264 *
265 * Basically this function only looks at output modules as it is part of the open function. If the
266 * module is an output module, it then starts to take it apart, and the data that is passed in.
267 * Because the data being passed in is in such a weird format, there are a few casts to make it
268 * easier to use. While it looks like a lot of local variables, they'll all get removed by the
269 * compiler.
270 *
271 * First thing that is checked is if the filename is shorter than the extension itself. There is
272 * no way for a match in that case. If it's long enough then there is a string compare of the end
273 * of the filename (for the length of the extension), and the extension itself. If this passes
274 * then the pointer passed in is set to the current module.
275 */
276 static void
277 save_internal(Extension *in_plug, gpointer in_data)
278 {
279 if (!in_plug->deactivated() && dynamic_cast<Output *>(in_plug)) {
280 gpointer *parray = (gpointer *)in_data;
281 gchar const *filename = (gchar const *)parray[0];
282 Output **pomod = (Output **)parray[1];
284 // skip all the rest if we already found someone to save it
285 // since they're ordered by preference now.
286 if (!*pomod) {
287 gchar const *ext = dynamic_cast<Output *>(in_plug)->get_extension();
289 gchar *filenamelower = g_utf8_strdown(filename, -1);
290 gchar *extensionlower = g_utf8_strdown(ext, -1);
292 if (g_str_has_suffix(filenamelower, extensionlower)) {
293 *pomod = dynamic_cast<Output *>(in_plug);
294 }
296 g_free(filenamelower);
297 g_free(extensionlower);
298 }
299 }
301 return;
302 }
304 Print *
305 get_print(gchar const *key)
306 {
307 return dynamic_cast<Print *>(db.get(key));
308 }
310 /**
311 * \return The built module
312 * \brief Creates a module from a Inkscape::XML::Document describing the module
313 * \param doc The XML description of the module
314 *
315 * This function basically has two segments. The first is that it goes through the Repr tree
316 * provided, and determines what kind of of module this is, and what kind of implementation to use.
317 * All of these are then stored in two enums that are defined in this function. This makes it
318 * easier to add additional types (which will happen in the future, I'm sure).
319 *
320 * Second, there is case statements for these enums. The first one is the type of module. This is
321 * the one where the module is actually created. After that, then the implementation is applied to
322 * get the load and unload functions. If there is no implementation then these are not set. This
323 * case could apply to modules that are built in (like the SVG load/save functions).
324 */
325 static Extension *
326 build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation *in_imp)
327 {
328 enum {
329 MODULE_EXTENSION,
330 MODULE_XSLT,
331 /* MODULE_PLUGIN, */
332 MODULE_UNKNOWN_IMP
333 } module_implementation_type = MODULE_UNKNOWN_IMP;
334 enum {
335 MODULE_INPUT,
336 MODULE_OUTPUT,
337 MODULE_FILTER,
338 MODULE_PRINT,
339 MODULE_PATH_EFFECT,
340 MODULE_UNKNOWN_FUNC
341 } module_functional_type = MODULE_UNKNOWN_FUNC;
343 g_return_val_if_fail(doc != NULL, NULL);
345 Inkscape::XML::Node *repr = sp_repr_document_root(doc);
347 /* sp_repr_print(repr); */
349 if (strcmp(repr->name(), "inkscape-extension")) {
350 g_warning("Extension definition started with <%s> instead of <inkscape-extension>. Extension will not be created.\n", repr->name());
351 return NULL;
352 }
354 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
355 while (child_repr != NULL) {
356 char const *element_name = child_repr->name();
357 /* printf("Child: %s\n", child_repr->name()); */
358 if (!strcmp(element_name, "input")) {
359 module_functional_type = MODULE_INPUT;
360 } else if (!strcmp(element_name, "output")) {
361 module_functional_type = MODULE_OUTPUT;
362 } else if (!strcmp(element_name, "effect")) {
363 module_functional_type = MODULE_FILTER;
364 } else if (!strcmp(element_name, "print")) {
365 module_functional_type = MODULE_PRINT;
366 } else if (!strcmp(element_name, "path-effect")) {
367 module_functional_type = MODULE_PATH_EFFECT;
368 } else if (!strcmp(element_name, "script")) {
369 module_implementation_type = MODULE_EXTENSION;
370 } else if (!strcmp(element_name, "xslt")) {
371 module_implementation_type = MODULE_XSLT;
372 #if 0
373 } else if (!strcmp(element_name, "plugin")) {
374 module_implementation_type = MODULE_PLUGIN;
375 #endif
376 }
378 //Inkscape::XML::Node *old_repr = child_repr;
379 child_repr = sp_repr_next(child_repr);
380 //Inkscape::GC::release(old_repr);
381 }
383 Implementation::Implementation *imp;
384 if (in_imp == NULL) {
385 switch (module_implementation_type) {
386 case MODULE_EXTENSION: {
387 Implementation::Script *script = new Implementation::Script();
388 imp = static_cast<Implementation::Implementation *>(script);
389 break;
390 }
391 case MODULE_XSLT: {
392 Implementation::XSLT *xslt = new Implementation::XSLT();
393 imp = static_cast<Implementation::Implementation *>(xslt);
394 break;
395 }
396 #if 0
397 case MODULE_PLUGIN: {
398 Implementation::Plugin *plugin = new Implementation::Plugin();
399 imp = static_cast<Implementation::Implementation *>(plugin);
400 break;
401 }
402 #endif
403 default: {
404 imp = NULL;
405 break;
406 }
407 }
408 } else {
409 imp = in_imp;
410 }
412 Extension *module = NULL;
413 switch (module_functional_type) {
414 case MODULE_INPUT: {
415 module = new Input(repr, imp);
416 break;
417 }
418 case MODULE_OUTPUT: {
419 module = new Output(repr, imp);
420 break;
421 }
422 case MODULE_FILTER: {
423 module = new Effect(repr, imp);
424 break;
425 }
426 case MODULE_PRINT: {
427 module = new Print(repr, imp);
428 break;
429 }
430 case MODULE_PATH_EFFECT: {
431 module = new PathEffect(repr, imp);
432 break;
433 }
434 default: {
435 break;
436 }
437 }
439 return module;
440 }
442 /**
443 * \return The module created
444 * \brief This function creates a module from a filename of an
445 * XML description.
446 * \param filename The file holding the XML description of the module.
447 *
448 * This function calls build_from_reprdoc with using sp_repr_read_file to create the reprdoc.
449 */
450 Extension *
451 build_from_file(gchar const *filename)
452 {
453 /* TODO: Need to define namespace here, need to write the
454 DTD in general for this stuff */
455 Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
456 Extension *ext = build_from_reprdoc(doc, NULL);
457 if (ext != NULL)
458 Inkscape::GC::release(doc);
459 else
460 g_warning("Unable to create extension from definition file %s.\n", filename);
461 return ext;
462 }
464 /**
465 * \return The module created
466 * \brief This function creates a module from a buffer holding an
467 * XML description.
468 * \param buffer The buffer holding the XML description of the module.
469 *
470 * This function calls build_from_reprdoc with using sp_repr_read_mem to create the reprdoc. It
471 * finds the length of the buffer using strlen.
472 */
473 Extension *
474 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
475 {
476 Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
477 Extension *ext = build_from_reprdoc(doc, in_imp);
478 Inkscape::GC::release(doc);
479 return ext;
480 }
483 } } /* namespace Inkscape::Extension */
485 /*
486 Local Variables:
487 mode:c++
488 c-file-style:"stroustrup"
489 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
490 indent-tabs-mode:nil
491 fill-column:99
492 End:
493 */
494 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :