Code

fix crash bug: PDF export: crash in 'CairoRenderContext::_showGlyphs'
[inkscape.git] / src / extension / extension.cpp
1 #define __SP_MODULE_C__
2 /** \file
3  *
4  * Inkscape::Extension::Extension: 
5  * the ability to have features that are more modular so that they
6  * can be added and removed easily.  This is the basis for defining
7  * those actions.
8  */
10 /*
11  * Authors:
12  *   Ted Gould <ted@gould.cx>
13  *
14  * Copyright (C) 2002-2005 Authors
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
24 #include <glibmm/i18n.h>
25 #include <gtkmm/box.h>
26 #include <gtkmm/label.h>
27 #include <gtkmm/frame.h>
28 #include <gtkmm/table.h>
29 #include <gtkmm/tooltips.h>
31 #include "inkscape.h"
32 #include "extension/implementation/implementation.h"
34 #include "db.h"
35 #include "dependency.h"
36 #include "timer.h"
37 #include "param/parameter.h"
39 namespace Inkscape {
40 namespace Extension {
42 /* Inkscape::Extension::Extension */
44 std::vector<const gchar *> Extension::search_path;
45 std::ofstream Extension::error_file;
47 Parameter * param_shared (const gchar * name, GSList * list);
49 /**
50     \return  none
51     \brief   Constructs an Extension from a Inkscape::XML::Node
52     \param   in_repr    The repr that should be used to build it
54     This function is the basis of building an extension for Inkscape.  It
55     currently extracts the fields from the Repr that are used in the
56     extension.  The Repr will likely include other children that are
57     not related to the module directly.  If the Repr does not include
58     a name and an ID the module will be left in an errored state.
59 */
60 Extension::Extension (Inkscape::XML::Node * in_repr, Implementation::Implementation * in_imp)
61     : _help(NULL)
62     , _gui(true)
63 {
64     repr = in_repr;
65     Inkscape::GC::anchor(in_repr);
67     id = NULL;
68     name = NULL;
69     _state = STATE_UNLOADED;
70     parameters = NULL;
72     if (in_imp == NULL) {
73         imp = new Implementation::Implementation();
74     } else {
75         imp = in_imp;
76     }
78     // printf("Extension Constructor: ");
79     if (repr != NULL) {
80         Inkscape::XML::Node *child_repr = sp_repr_children(repr);
81         /* TODO: Handle what happens if we don't have these two */
82         while (child_repr != NULL) {
83             char const * chname = child_repr->name();
84                         if (!strncmp(chname, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) {
85                                 chname += strlen(INKSCAPE_EXTENSION_NS);
86                         }
87             if (chname[0] == '_') /* Allow _ for translation of tags */
88                 chname++;
89             if (!strcmp(chname, "id")) {
90                 gchar const *val = sp_repr_children(child_repr)->content();
91                 id = g_strdup (val);
92             } /* id */
93             if (!strcmp(chname, "name")) {
94                 name = g_strdup (sp_repr_children(child_repr)->content());
95             } /* name */
96             if (!strcmp(chname, "help")) {
97                 _help = g_strdup (sp_repr_children(child_repr)->content());
98             } /* name */
99             if (!strcmp(chname, "param") || !strcmp(chname, "_param")) {
100                 Parameter * param;
101                 param = Parameter::make(child_repr, this);
102                 if (param != NULL)
103                     parameters = g_slist_append(parameters, param);
104             } /* param || _param */
105             if (!strcmp(chname, "dependency")) {
106                 _deps.push_back(new Dependency(child_repr));
107             } /* dependency */
108             child_repr = sp_repr_next(child_repr);
109         }
111         db.register_ext (this);
112     }
113     // printf("%s\n", name);
114     timer = NULL;
116     return;
119 /**
120     \return   none
121     \brief    Destroys the Extension
123     This function frees all of the strings that could be attached
124     to the extension and also unreferences the repr.  This is better
125     than freeing it because it may (I wouldn't know why) be referenced
126     in another place.
127 */
128 Extension::~Extension (void)
130 //    printf("Extension Destructor: %s\n", name);
131     set_state(STATE_UNLOADED);
132     db.unregister_ext(this);
133     Inkscape::GC::release(repr);
134     g_free(id);
135     g_free(name);
136     delete timer;
137     timer = NULL;
138     /** \todo Need to do parameters here */
139     
140     // delete parameters: 
141     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
142         Parameter * param = reinterpret_cast<Parameter *>(list->data);
143         delete param;
144     }
145     g_slist_free(parameters);
146     
147     
148     for (unsigned int i = 0 ; i < _deps.size(); i++) {
149         delete _deps[i];
150     }
151     _deps.clear();
153     return;
156 /**
157     \return   none
158     \brief    A function to set whether the extension should be loaded
159               or unloaded
160     \param    in_state  Which state should the extension be in?
162     It checks to see if this is a state change or not.  If we're changing
163     states it will call the appropriate function in the implementation,
164     load or unload.  Currently, there is no error checking in this
165     function.  There should be.
166 */
167 void
168 Extension::set_state (state_t in_state)
170     if (_state == STATE_DEACTIVATED) return;
171     if (in_state != _state) {
172         /** \todo Need some more error checking here! */
173         switch (in_state) {
174             case STATE_LOADED:
175                 if (imp->load(this))
176                     _state = STATE_LOADED;
178                 if (timer != NULL) {
179                     delete timer;
180                 }
181                 timer = new ExpirationTimer(this);
183                 break;
184             case STATE_UNLOADED:
185                 // std::cout << "Unloading: " << name << std::endl;
186                 imp->unload(this);
187                 _state = STATE_UNLOADED;
189                 if (timer != NULL) {
190                     delete timer;
191                     timer = NULL;
192                 }
193                 break;
194             case STATE_DEACTIVATED:
195                 _state = STATE_DEACTIVATED;
197                 if (timer != NULL) {
198                     delete timer;
199                     timer = NULL;
200                 }
201                 break;
202             default:
203                 break;
204         }
205     }
207     return;
210 /**
211     \return   The state the extension is in
212     \brief    A getter for the state variable.
213 */
214 Extension::state_t
215 Extension::get_state (void)
217     return _state;
220 /**
221     \return  Whether the extension is loaded or not
222     \brief   A quick function to test the state of the extension
223 */
224 bool
225 Extension::loaded (void)
227     return get_state() == STATE_LOADED;
230 /**
231     \return  A boolean saying whether the extension passed the checks
232     \brief   A function to check the validity of the extension
234     This function chekcs to make sure that there is an id, a name, a
235     repr and an implemenation for this extension.  Then it checks all
236     of the dependencies to see if they pass.  Finally, it asks the
237     implmentation to do a check of itself.
239     On each check, if there is a failure, it will print a message to the
240     error log for that failure.  It is important to note that the function
241     keeps executing if it finds an error, to try and get as many of them
242     into the error log as possible.  This should help people debug
243     installations, and figure out what they need to get for the full
244     functionality of Inkscape to be available.
245 */
246 bool
247 Extension::check (void)
249     bool retval = true;
251     // static int i = 0;
252     // std::cout << "Checking module[" << i++ << "]: " << name << std::endl;
254     const char * inx_failure = _("  This is caused by an improper .inx file for this extension."
255                                  "  An improper .inx file could have been caused by a faulty installation of Inkscape.");
256     if (id == NULL) {
257         printFailure(Glib::ustring(_("an ID was not defined for it.")) + inx_failure);
258         retval = false;
259     }
260     if (name == NULL) {
261         printFailure(Glib::ustring(_("there was no name defined for it.")) + inx_failure);
262         retval = false;
263     }
264     if (repr == NULL) {
265         printFailure(Glib::ustring(_("the XML description of it got lost.")) + inx_failure);
266         retval = false;
267     }
268     if (imp == NULL) {
269         printFailure(Glib::ustring(_("no implementation was defined for the extension.")) + inx_failure);
270         retval = false;
271     }
273     for (unsigned int i = 0 ; i < _deps.size(); i++) {
274         if (_deps[i]->check() == FALSE) {
275             // std::cout << "Failed: " << *(_deps[i]) << std::endl;
276             printFailure(Glib::ustring(_("a dependency was not met.")));
277             error_file << *_deps[i] << std::endl;
278             retval = false;
279         }
280     }
282     if (retval)
283         return imp->check(this);
284     return retval;
287 /** \brief A quick function to print out a standard start of extension
288            errors in the log.
289     \param reason  A string explaining why this failed
291     Real simple, just put everything into \c error_file.
292 */
293 void
294 Extension::printFailure (Glib::ustring reason)
296     error_file << _("Extension \"") << name << _("\" failed to load because ");
297     error_file << reason.raw();
298     error_file << std::endl;
299     return;
302 /**
303     \return  The XML tree that is used to define the extension
304     \brief   A getter for the internal Repr, does not add a reference.
305 */
306 Inkscape::XML::Node *
307 Extension::get_repr (void)
309     return repr;
312 /**
313     \return  The textual id of this extension
314     \brief   Get the ID of this extension - not a copy don't delete!
315 */
316 gchar *
317 Extension::get_id (void)
319     return id;
322 /**
323     \return  The textual name of this extension
324     \brief   Get the name of this extension - not a copy don't delete!
325 */
326 gchar *
327 Extension::get_name (void)
329     return name;
332 /**
333     \return  None
334     \brief   This function diactivates the extension (which makes it
335              unusable, but not deleted)
337     This function is used to removed an extension from functioning, but
338     not delete it completely.  It sets the state to \c STATE_DEACTIVATED to
339     mark to the world that it has been deactivated.  It also removes
340     the current implementation and replaces it with a standard one.  This
341     makes it so that we don't have to continually check if there is an
342     implementation, but we are gauranteed to have a benign one.
344     \warning It is important to note that there is no 'activate' function.
345     Running this function is irreversable.
346 */
347 void
348 Extension::deactivate (void)
350     set_state(STATE_DEACTIVATED);
352     /* Removing the old implementation, and making this use the default. */
353     /* This should save some memory */
354     delete imp;
355     imp = new Implementation::Implementation();
357     return;
360 /**
361     \return  Whether the extension has been deactivated
362     \brief   Find out the status of the extension
363 */
364 bool
365 Extension::deactivated (void)
367     return get_state() == STATE_DEACTIVATED;
370 /**
371     \return    Parameter structure with a name of 'name'
372     \brief     This function looks through the linked list for a parameter
373                structure with the name of the passed in name
374     \param     name   The name to search for
375     \param     list   The list to look for
377     This is an inline function that is used by all the get_param and
378     set_param functions to find a param_t in the linked list with
379     the passed in name.  It is done as an inline so that it will be
380     optimized into a 'jump' by the compiler.
382     This function can throw a 'param_not_exist' exception if the
383     name is not found.
385     The first thing that this function checks is if the list is NULL.
386     It could be NULL because there are no parameters for this extension
387     or because all of them have been checked (I'll spoil the ending and
388     tell you that this function is called recursively).  If the list
389     is NULL then the 'param_not_exist' exception is thrown.
391     Otherwise, the function looks at the current param_t that the element
392     list points to.  If the name of that param_t matches the passed in
393     name then that param_t is returned.  Otherwise, this function is
394     called again with g_slist_next as a parameter.
395 */
396 Parameter *
397 param_shared (const gchar * name, GSList * list)
399     Parameter * output;
401     if (name == NULL) {
402         throw Extension::param_not_exist();
403     }
404     if (list == NULL) {
405         throw Extension::param_not_exist();
406     }
408     output = static_cast<Parameter *>(list->data);
409     if (!strcmp(output->name(), name)) {
410         return output;
411     }
413     return param_shared(name, g_slist_next(list));
416 /**
417     \return   A constant pointer to the string held by the parameters.
418     \brief    Gets a parameter identified by name with the string placed
419               in value.  It isn't duplicated into the value string.
420     \param    name    The name of the parameter to get
421     \param    doc    The document to look in for document specific parameters
422     \param    node   The node to look in for a specific parameter
424     Look up in the parameters list, then execute the function on that
425     found parameter.
426 */
427 const gchar *
428 Extension::get_param_string (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
430     Parameter * param;
432     param = param_shared(name, parameters);
433     return param->get_string(doc, node);
436 const gchar *
437 Extension::get_param_enum (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
439     Parameter* param = param_shared(name, parameters);
440     return param->get_enum(doc, node);
444 gchar const *Extension::get_param_optiongroup( gchar const * name, SPDocument const * doc, Inkscape::XML::Node const * node)
446     Parameter* param = param_shared(name, parameters);
447     return param->get_optiongroup(doc, node);
451 /**
452     \return   The value of the parameter identified by the name
453     \brief    Gets a parameter identified by name with the bool placed
454               in value.
455     \param    name    The name of the parameter to get
456     \param    doc    The document to look in for document specific parameters
457     \param    node   The node to look in for a specific parameter
459     Look up in the parameters list, then execute the function on that
460     found parameter.
461 */
462 bool
463 Extension::get_param_bool (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
465     Parameter * param;
467     param = param_shared(name, parameters);
468     return param->get_bool(doc, node);
471 /**
472     \return   The integer value for the parameter specified
473     \brief    Gets a parameter identified by name with the integer placed
474               in value.
475     \param    name    The name of the parameter to get
476     \param    doc    The document to look in for document specific parameters
477     \param    node   The node to look in for a specific parameter
479     Look up in the parameters list, then execute the function on that
480     found parameter.
481 */
482 int
483 Extension::get_param_int (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
485     Parameter * param;
487     param = param_shared(name, parameters);
488     return param->get_int(doc, node);
491 /**
492     \return   The float value for the parameter specified
493     \brief    Gets a parameter identified by name with the float placed
494               in value.
495     \param    name    The name of the parameter to get
496     \param    doc    The document to look in for document specific parameters
497     \param    node   The node to look in for a specific parameter
499     Look up in the parameters list, then execute the function on that
500     found parameter.
501 */
502 float
503 Extension::get_param_float (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
505     Parameter * param;
506     param = param_shared(name, parameters);
507     return param->get_float(doc, node);
510 /**
511     \return   The string value for the parameter specified
512     \brief    Gets a parameter identified by name with the float placed
513               in value.
514     \param    name    The name of the parameter to get
515     \param    doc    The document to look in for document specific parameters
516     \param    node   The node to look in for a specific parameter
518     Look up in the parameters list, then execute the function on that
519     found parameter.
520 */
521 guint32
522 Extension::get_param_color (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
524     Parameter* param = param_shared(name, parameters);
525     return param->get_color(doc, node);
528 /**
529     \return   The passed in value
530     \brief    Sets a parameter identified by name with the boolean
531               in the parameter value.
532     \param    name    The name of the parameter to set
533     \param    value   The value to set the parameter to
534     \param    doc    The document to look in for document specific parameters
535     \param    node   The node to look in for a specific parameter
537     Look up in the parameters list, then execute the function on that
538     found parameter.
539 */
540 bool
541 Extension::set_param_bool (const gchar * name, bool value, SPDocument * doc, Inkscape::XML::Node * node)
543     Parameter * param;
544     param = param_shared(name, parameters);
545     return param->set_bool(value, doc, node);
548 /**
549     \return   The passed in value
550     \brief    Sets a parameter identified by name with the integer
551               in the parameter value.
552     \param    name    The name of the parameter to set
553     \param    value   The value to set the parameter to
554     \param    doc    The document to look in for document specific parameters
555     \param    node   The node to look in for a specific parameter
557     Look up in the parameters list, then execute the function on that
558     found parameter.
559 */
560 int
561 Extension::set_param_int (const gchar * name, int value, SPDocument * doc, Inkscape::XML::Node * node)
563     Parameter * param;
564     param = param_shared(name, parameters);
565     return param->set_int(value, doc, node);
568 /**
569     \return   The passed in value
570     \brief    Sets a parameter identified by name with the integer
571               in the parameter value.
572     \param    name    The name of the parameter to set
573     \param    value   The value to set the parameter to
574     \param    doc    The document to look in for document specific parameters
575     \param    node   The node to look in for a specific parameter
577     Look up in the parameters list, then execute the function on that
578     found parameter.
579 */
580 float
581 Extension::set_param_float (const gchar * name, float value, SPDocument * doc, Inkscape::XML::Node * node)
583     Parameter * param;
584     param = param_shared(name, parameters);
585     return param->set_float(value, doc, node);
588 /**
589     \return   The passed in value
590     \brief    Sets a parameter identified by name with the string
591               in the parameter value.
592     \param    name    The name of the parameter to set
593     \param    value   The value to set the parameter to
594     \param    doc    The document to look in for document specific parameters
595     \param    node   The node to look in for a specific parameter
597     Look up in the parameters list, then execute the function on that
598     found parameter.
599 */
600 const gchar *
601 Extension::set_param_string (const gchar * name, const gchar * value, SPDocument * doc, Inkscape::XML::Node * node)
603     Parameter * param;
604     param = param_shared(name, parameters);
605     return param->set_string(value, doc, node);
608 gchar const * Extension::set_param_optiongroup(gchar const * name, gchar const * value, SPDocument * doc, Inkscape::XML::Node * node)
610     Parameter * param = param_shared(name, parameters);
611     return param->set_optiongroup(value, doc, node);
615 /**
616     \return   The passed in value
617     \brief    Sets a parameter identified by name with the string
618               in the parameter value.
619     \param    name    The name of the parameter to set
620     \param    value   The value to set the parameter to
621     \param    doc    The document to look in for document specific parameters
622     \param    node   The node to look in for a specific parameter
624     Look up in the parameters list, then execute the function on that
625     found parameter.
626 */
627 guint32
628 Extension::set_param_color (const gchar * name, guint32 color, SPDocument * doc, Inkscape::XML::Node * node)
630     Parameter* param = param_shared(name, parameters);
631     return param->set_color(color, doc, node);
634 /** \brief A function to open the error log file. */
635 void
636 Extension::error_file_open (void)
638     gchar * ext_error_file = profile_path(EXTENSION_ERROR_LOG_FILENAME);
639     gchar * filename = g_filename_from_utf8( ext_error_file, -1, NULL, NULL, NULL );
640     error_file.open(filename);
641     if (!error_file.is_open()) {
642         g_warning(_("Could not create extension error log file '%s'"),
643                   filename);
644     }
645     g_free(filename);
646     g_free(ext_error_file);
647 };
649 /** \brief A function to close the error log file. */
650 void
651 Extension::error_file_close (void)
653     error_file.close();
654 };
656 /** \brief  A widget to represent the inside of an AutoGUI widget */
657 class AutoGUI : public Gtk::VBox {
658     Gtk::Tooltips _tooltips;
659 public:
660     /** \brief  Create an AutoGUI object */
661     AutoGUI (void) : Gtk::VBox() {};
662     /** \brief  Adds a widget with a tool tip into the autogui
663         \param  widg  Widget to add
664         \param  tooltip   Tooltip for the widget
665         
666         If there is no widget, nothing happens.  Otherwise it is just
667         added into the VBox.  If there is a tooltip (non-NULL) then it
668         is placed on the widget.
669     */
670     void addWidget (Gtk::Widget * widg, gchar const * tooltip) {
671         if (widg == NULL) return;
672         this->pack_start(*widg, false, false, 2);
673         if (tooltip != NULL) {
674             _tooltips.set_tip(*widg, Glib::ustring(_(tooltip)));
675         }
676         return;
677     };
678 };
680 /** \brief  A function to automatically generate a GUI using the parameters
681     \return Generated widget
683     This function just goes through each parameter, and calls it's 'get_widget'
684     function to get each widget.  Then, each of those is placed into
685     a Gtk::VBox, which is then returned to the calling function.
687     If there are no visible parameters, this function just returns NULL.
688     If all parameters are gui_visible = false NULL is returned as well.    
689 */
690 Gtk::Widget *
691 Extension::autogui (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
693     if (!_gui || param_visible_count() == 0) return NULL;
695     AutoGUI * agui = Gtk::manage(new AutoGUI());
697     //go through the list of parameters to see if there are any non-hidden ones
698     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
699         Parameter * param = reinterpret_cast<Parameter *>(list->data);
700         if (param->get_gui_hidden()) continue; //Ignore hidden parameters
701         Gtk::Widget * widg = param->get_widget(doc, node, changeSignal);
702         gchar const * tip = param->get_tooltip();
703         agui->addWidget(widg, tip);
704     }    
705     
706     agui->show();
707     return agui;
708 };
710 /**
711     \brief  A function to get the parameters in a string form
712     \return An array with all the parameters in it.
714 */
715 void
716 Extension::paramListString (std::list <std::string> &retlist)
718     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
719         Parameter * param = reinterpret_cast<Parameter *>(list->data);
720         param->string(retlist);
721     }
723     return;
726 /* Extension editor dialog stuff */
728 Gtk::VBox *
729 Extension::get_info_widget(void)
731     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
733     Gtk::Frame * info = Gtk::manage(new Gtk::Frame("General Extension Information"));
734     retval->pack_start(*info, true, true, 5);
736     Gtk::Table * table = Gtk::manage(new Gtk::Table());
737     info->add(*table);
739     int row = 0;
740     add_val(_("Name:"), _(name), table, &row);
741     add_val(_("ID:"), id, table, &row);
742     add_val(_("State:"), _state == STATE_LOADED ? _("Loaded") : _state == STATE_UNLOADED ? _("Unloaded") : _("Deactivated"), table, &row);
745     retval->show_all();
746     return retval;
749 void
750 Extension::add_val(Glib::ustring labelstr, Glib::ustring valuestr, Gtk::Table * table, int * row)
752     Gtk::Label * label;
753     Gtk::Label * value;
755     (*row)++; 
756     label = Gtk::manage(new Gtk::Label(labelstr));
757     value = Gtk::manage(new Gtk::Label(valuestr));
758     table->attach(*label, 0, 1, (*row) - 1, *row);
759     table->attach(*value, 1, 2, (*row) - 1, *row);
761     label->show();
762     value->show();
764     return;
767 Gtk::VBox *
768 Extension::get_help_widget(void)
770     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
772     if (_help == NULL) {
773         Gtk::Label * content = Gtk::manage(new Gtk::Label(_("Currently there is no help available for this Extension.  Please look on the Inkscape website or ask on the mailing lists if you have questions regarding this extension.")));
774         retval->pack_start(*content, true, true, 5);
775         content->set_line_wrap(true);
776         content->show();
777     } else {
781     }
783     retval->show();
784     return retval;
787 Gtk::VBox *
788 Extension::get_params_widget(void)
790     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
791     Gtk::Widget * content = Gtk::manage(new Gtk::Label("Params"));
792     retval->pack_start(*content, true, true, 5);
793     content->show();
794     retval->show();
795     return retval;
798 unsigned int Extension::param_visible_count ( ) 
800     unsigned int _visible_count = 0;
801     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
802         Parameter * param = reinterpret_cast<Parameter *>(list->data);
803         if (!param->get_gui_hidden()) _visible_count++;
804     }    
805     return _visible_count;
808 }  /* namespace Extension */
809 }  /* namespace Inkscape */
812 /*
813   Local Variables:
814   mode:c++
815   c-file-style:"stroustrup"
816   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
817   indent-tabs-mode:nil
818   fill-column:99
819   End:
820 */
821 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :