Code

implemented proper error checking
[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 * get_param (const gchar * name);
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
376     This is an inline function that is used by all the get_param and
377     set_param functions to find a param_t in the linked list with
378     the passed in name.
380     This function can throw a 'param_not_exist' exception if the
381     name is not found.
383     The first thing that this function checks is if the list is NULL.
384     It could be NULL because there are no parameters for this extension
385     or because all of them have been checked.  If the list
386     is NULL then the 'param_not_exist' exception is thrown.
387 */
388 Parameter *
389 Extension::get_param (const gchar * name)
391     if (name == NULL) {
392         throw Extension::param_not_exist();
393     }
394     if (this->parameters == NULL) {
395         // the list of parameters is empty
396         throw Extension::param_not_exist();
397     }
399     for (GSList * list = this->parameters; list != NULL; list =
400 g_slist_next(list)) {
401         Parameter * param = static_cast<Parameter*>(list->data);
402         if (!strcmp(param->name(), name)) {
403             return param;
404         } else {
405             Parameter * subparam = param->get_param(name);
406             if (subparam) {
407                 return subparam;
408             }
409         }
410     }
412     // if execution reaches here, no parameter matching 'name' was found
413     throw Extension::param_not_exist();
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;
431     param = get_param(name);
432     return param->get_string(doc, node);
435 const gchar *
436 Extension::get_param_enum (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
438     Parameter* param = get_param(name);
439     return param->get_enum(doc, node);
442 gchar const *
443 Extension::get_param_optiongroup( gchar const * name, SPDocument const * doc, Inkscape::XML::Node const * node)
445     Parameter* param = get_param(name);
446     return param->get_optiongroup(doc, node);
450 /**
451     \return   The value of the parameter identified by the name
452     \brief    Gets a parameter identified by name with the bool placed
453               in value.
454     \param    name    The name of the parameter to get
455     \param    doc    The document to look in for document specific parameters
456     \param    node   The node to look in for a specific parameter
458     Look up in the parameters list, then execute the function on that
459     found parameter.
460 */
461 bool
462 Extension::get_param_bool (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
464     Parameter * param;
465     param = get_param(name);
466     return param->get_bool(doc, node);
469 /**
470     \return   The integer value for the parameter specified
471     \brief    Gets a parameter identified by name with the integer placed
472               in value.
473     \param    name    The name of the parameter to get
474     \param    doc    The document to look in for document specific parameters
475     \param    node   The node to look in for a specific parameter
477     Look up in the parameters list, then execute the function on that
478     found parameter.
479 */
480 int
481 Extension::get_param_int (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
483     Parameter * param;
484     param = get_param(name);
485     return param->get_int(doc, node);
488 /**
489     \return   The float value for the parameter specified
490     \brief    Gets a parameter identified by name with the float placed
491               in value.
492     \param    name    The name of the parameter to get
493     \param    doc    The document to look in for document specific parameters
494     \param    node   The node to look in for a specific parameter
496     Look up in the parameters list, then execute the function on that
497     found parameter.
498 */
499 float
500 Extension::get_param_float (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
502     Parameter * param;
503     param = get_param(name);
504     return param->get_float(doc, node);
507 /**
508     \return   The string value for the parameter specified
509     \brief    Gets a parameter identified by name with the float placed
510               in value.
511     \param    name    The name of the parameter to get
512     \param    doc    The document to look in for document specific parameters
513     \param    node   The node to look in for a specific parameter
515     Look up in the parameters list, then execute the function on that
516     found parameter.
517 */
518 guint32
519 Extension::get_param_color (const gchar * name, const SPDocument * doc, const Inkscape::XML::Node * node)
521     Parameter* param = get_param(name);
522     return param->get_color(doc, node);
525 /**
526     \return   The passed in value
527     \brief    Sets a parameter identified by name with the boolean
528               in the parameter value.
529     \param    name    The name of the parameter to set
530     \param    value   The value to set the parameter to
531     \param    doc    The document to look in for document specific parameters
532     \param    node   The node to look in for a specific parameter
534     Look up in the parameters list, then execute the function on that
535     found parameter.
536 */
537 bool
538 Extension::set_param_bool (const gchar * name, bool value, SPDocument * doc, Inkscape::XML::Node * node)
540     Parameter * param;
541     param = get_param(name);
542     return param->set_bool(value, doc, node);
545 /**
546     \return   The passed in value
547     \brief    Sets a parameter identified by name with the integer
548               in the parameter value.
549     \param    name    The name of the parameter to set
550     \param    value   The value to set the parameter to
551     \param    doc    The document to look in for document specific parameters
552     \param    node   The node to look in for a specific parameter
554     Look up in the parameters list, then execute the function on that
555     found parameter.
556 */
557 int
558 Extension::set_param_int (const gchar * name, int value, SPDocument * doc, Inkscape::XML::Node * node)
560     Parameter * param;
561     param = get_param(name);
562     return param->set_int(value, doc, node);
565 /**
566     \return   The passed in value
567     \brief    Sets a parameter identified by name with the integer
568               in the parameter value.
569     \param    name    The name of the parameter to set
570     \param    value   The value to set the parameter to
571     \param    doc    The document to look in for document specific parameters
572     \param    node   The node to look in for a specific parameter
574     Look up in the parameters list, then execute the function on that
575     found parameter.
576 */
577 float
578 Extension::set_param_float (const gchar * name, float value, SPDocument * doc, Inkscape::XML::Node * node)
580     Parameter * param;
581     param = get_param(name);
582     return param->set_float(value, doc, node);
585 /**
586     \return   The passed in value
587     \brief    Sets a parameter identified by name with the string
588               in the parameter value.
589     \param    name    The name of the parameter to set
590     \param    value   The value to set the parameter to
591     \param    doc    The document to look in for document specific parameters
592     \param    node   The node to look in for a specific parameter
594     Look up in the parameters list, then execute the function on that
595     found parameter.
596 */
597 const gchar *
598 Extension::set_param_string (const gchar * name, const gchar * value, SPDocument * doc, Inkscape::XML::Node * node)
600     Parameter * param;
601     param = get_param(name);
602     return param->set_string(value, doc, node);
605 gchar const *
606 Extension::set_param_optiongroup(gchar const * name, gchar const * value, SPDocument * doc, Inkscape::XML::Node * node)
608     Parameter * param = get_param(name);
609     return param->set_optiongroup(value, doc, node);
613 /**
614     \return   The passed in value
615     \brief    Sets a parameter identified by name with the string
616               in the parameter value.
617     \param    name    The name of the parameter to set
618     \param    value   The value to set the parameter to
619     \param    doc    The document to look in for document specific parameters
620     \param    node   The node to look in for a specific parameter
622     Look up in the parameters list, then execute the function on that
623     found parameter.
624 */
625 guint32
626 Extension::set_param_color (const gchar * name, guint32 color, SPDocument * doc, Inkscape::XML::Node * node)
628     Parameter* param = get_param(name);
629     return param->set_color(color, doc, node);
632 /** \brief A function to open the error log file. */
633 void
634 Extension::error_file_open (void)
636     gchar * ext_error_file = profile_path(EXTENSION_ERROR_LOG_FILENAME);
637     gchar * filename = g_filename_from_utf8( ext_error_file, -1, NULL, NULL, NULL );
638     error_file.open(filename);
639     if (!error_file.is_open()) {
640         g_warning(_("Could not create extension error log file '%s'"),
641                   filename);
642     }
643     g_free(filename);
644     g_free(ext_error_file);
645 };
647 /** \brief A function to close the error log file. */
648 void
649 Extension::error_file_close (void)
651     error_file.close();
652 };
654 /** \brief  A widget to represent the inside of an AutoGUI widget */
655 class AutoGUI : public Gtk::VBox {
656     Gtk::Tooltips _tooltips;
657 public:
658     /** \brief  Create an AutoGUI object */
659     AutoGUI (void) : Gtk::VBox() {};
660     /** \brief  Adds a widget with a tool tip into the autogui
661         \param  widg  Widget to add
662         \param  tooltip   Tooltip for the widget
663         
664         If there is no widget, nothing happens.  Otherwise it is just
665         added into the VBox.  If there is a tooltip (non-NULL) then it
666         is placed on the widget.
667     */
668     void addWidget (Gtk::Widget * widg, gchar const * tooltip) {
669         if (widg == NULL) return;
670         this->pack_start(*widg, false, false, 2);
671         if (tooltip != NULL) {
672             _tooltips.set_tip(*widg, Glib::ustring(_(tooltip)));
673         }
674         return;
675     };
676 };
678 /** \brief  A function to automatically generate a GUI using the parameters
679     \return Generated widget
681     This function just goes through each parameter, and calls it's 'get_widget'
682     function to get each widget.  Then, each of those is placed into
683     a Gtk::VBox, which is then returned to the calling function.
685     If there are no visible parameters, this function just returns NULL.
686     If all parameters are gui_visible = false NULL is returned as well.    
687 */
688 Gtk::Widget *
689 Extension::autogui (SPDocument * doc, Inkscape::XML::Node * node, sigc::signal<void> * changeSignal)
691     if (!_gui || param_visible_count() == 0) return NULL;
693     AutoGUI * agui = Gtk::manage(new AutoGUI());
695     //go through the list of parameters to see if there are any non-hidden ones
696     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
697         Parameter * param = reinterpret_cast<Parameter *>(list->data);
698         if (param->get_gui_hidden()) continue; //Ignore hidden parameters
699         Gtk::Widget * widg = param->get_widget(doc, node, changeSignal);
700         gchar const * tip = param->get_tooltip();
701         agui->addWidget(widg, tip);
702     }    
703     
704     agui->show();
705     return agui;
706 };
708 /**
709     \brief  A function to get the parameters in a string form
710     \return An array with all the parameters in it.
712 */
713 void
714 Extension::paramListString (std::list <std::string> &retlist)
716     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
717         Parameter * param = reinterpret_cast<Parameter *>(list->data);
718         param->string(retlist);
719     }
721     return;
724 /* Extension editor dialog stuff */
726 Gtk::VBox *
727 Extension::get_info_widget(void)
729     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
731     Gtk::Frame * info = Gtk::manage(new Gtk::Frame("General Extension Information"));
732     retval->pack_start(*info, true, true, 5);
734     Gtk::Table * table = Gtk::manage(new Gtk::Table());
735     info->add(*table);
737     int row = 0;
738     add_val(_("Name:"), _(name), table, &row);
739     add_val(_("ID:"), id, table, &row);
740     add_val(_("State:"), _state == STATE_LOADED ? _("Loaded") : _state == STATE_UNLOADED ? _("Unloaded") : _("Deactivated"), table, &row);
743     retval->show_all();
744     return retval;
747 void
748 Extension::add_val(Glib::ustring labelstr, Glib::ustring valuestr, Gtk::Table * table, int * row)
750     Gtk::Label * label;
751     Gtk::Label * value;
753     (*row)++; 
754     label = Gtk::manage(new Gtk::Label(labelstr));
755     value = Gtk::manage(new Gtk::Label(valuestr));
756     table->attach(*label, 0, 1, (*row) - 1, *row);
757     table->attach(*value, 1, 2, (*row) - 1, *row);
759     label->show();
760     value->show();
762     return;
765 Gtk::VBox *
766 Extension::get_help_widget(void)
768     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
770     if (_help == NULL) {
771         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.")));
772         retval->pack_start(*content, true, true, 5);
773         content->set_line_wrap(true);
774         content->show();
775     } else {
779     }
781     retval->show();
782     return retval;
785 Gtk::VBox *
786 Extension::get_params_widget(void)
788     Gtk::VBox * retval = Gtk::manage(new Gtk::VBox());
789     Gtk::Widget * content = Gtk::manage(new Gtk::Label("Params"));
790     retval->pack_start(*content, true, true, 5);
791     content->show();
792     retval->show();
793     return retval;
796 unsigned int Extension::param_visible_count ( ) 
798     unsigned int _visible_count = 0;
799     for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
800         Parameter * param = reinterpret_cast<Parameter *>(list->data);
801         if (!param->get_gui_hidden()) _visible_count++;
802     }    
803     return _visible_count;
806 }  /* namespace Extension */
807 }  /* namespace Inkscape */
810 /*
811   Local Variables:
812   mode:c++
813   c-file-style:"stroustrup"
814   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
815   indent-tabs-mode:nil
816   fill-column:99
817   End:
818 */
819 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :