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>
27 #include "inkscape.h"
28 #include "extension/implementation/implementation.h"
30 #include "db.h"
31 #include "dependency.h"
32 #include "timer.h"
33 #include "parameter.h"
35 namespace Inkscape {
36 namespace Extension {
38 /* Inkscape::Extension::Extension */
40 std::vector<const gchar *> Extension::search_path;
41 std::ofstream Extension::error_file;
43 Parameter * param_shared (const gchar * name, GSList * list);
45 /**
46 \return none
47 \brief Constructs an Extension from a Inkscape::XML::Node
48 \param in_repr The repr that should be used to build it
50 This function is the basis of building an extension for Inkscape. It
51 currently extracts the fields from the Repr that are used in the
52 extension. The Repr will likely include other children that are
53 not related to the module directly. If the Repr does not include
54 a name and an ID the module will be left in an errored state.
55 */
56 Extension::Extension (Inkscape::XML::Node * in_repr, Implementation::Implementation * in_imp) : _help(NULL)
57 {
58 repr = in_repr;
59 Inkscape::GC::anchor(in_repr);
61 id = NULL;
62 name = NULL;
63 _state = STATE_UNLOADED;
64 parameters = NULL;
66 if (in_imp == NULL) {
67 imp = new Implementation::Implementation();
68 } else {
69 imp = in_imp;
70 }
72 // printf("Extension Constructor: ");
73 if (repr != NULL) {
74 Inkscape::XML::Node *child_repr = sp_repr_children(repr);
75 /* TODO: Handle what happens if we don't have these two */
76 while (child_repr != NULL) {
77 char const * chname = child_repr->name();
78 if (chname[0] == '_') /* Allow _ for translation of tags */
79 chname++;
80 if (!strcmp(chname, "id")) {
81 gchar const *val = sp_repr_children(child_repr)->content();
82 id = g_strdup (val);
83 } /* id */
84 if (!strcmp(chname, "name")) {
85 name = g_strdup (sp_repr_children(child_repr)->content());
86 } /* name */
87 if (!strcmp(chname, "help")) {
88 _help = g_strdup (sp_repr_children(child_repr)->content());
89 } /* name */
90 if (!strcmp(chname, "param")) {
91 Parameter * param;
92 param = Parameter::make(child_repr, this);
93 if (param != NULL)
94 parameters = g_slist_append(parameters, param);
95 } /* param */
96 if (!strcmp(chname, "dependency")) {
97 _deps.push_back(new Dependency(child_repr));
98 } /* param */
99 child_repr = sp_repr_next(child_repr);
100 }
102 db.register_ext (this);
103 }
104 // printf("%s\n", name);
105 timer = NULL;
107 return;
108 }
110 /**
111 \return none
112 \brief Destroys the Extension
114 This function frees all of the strings that could be attached
115 to the extension and also unreferences the repr. This is better
116 than freeing it because it may (I wouldn't know why) be referenced
117 in another place.
118 */
119 Extension::~Extension (void)
120 {
121 // printf("Extension Destructor: %s\n", name);
122 set_state(STATE_UNLOADED);
123 db.unregister_ext(this);
124 Inkscape::GC::release(repr);
125 g_free(id);
126 g_free(name);
127 delete timer;
128 timer = NULL;
129 /** \todo Need to do parameters here */
131 for (unsigned int i = 0 ; i < _deps.size(); i++) {
132 delete _deps[i];
133 }
134 _deps.clear();
136 return;
137 }
139 /**
140 \return none
141 \brief A function to set whether the extension should be loaded
142 or unloaded
143 \param in_state Which state should the extension be in?
145 It checks to see if this is a state change or not. If we're changing
146 states it will call the appropriate function in the implementation,
147 load or unload. Currently, there is no error checking in this
148 function. There should be.
149 */
150 void
151 Extension::set_state (state_t in_state)
152 {
153 if (_state == STATE_DEACTIVATED) return;
154 if (in_state != _state) {
155 /** \todo Need some more error checking here! */
156 switch (in_state) {
157 case STATE_LOADED:
158 if (imp->load(this))
159 _state = STATE_LOADED;
161 if (timer != NULL) {
162 delete timer;
163 }
164 timer = new ExpirationTimer(this);
166 break;
167 case STATE_UNLOADED:
168 // std::cout << "Unloading: " << name << std::endl;
169 imp->unload(this);
170 _state = STATE_UNLOADED;
172 if (timer != NULL) {
173 delete timer;
174 timer = NULL;
175 }
176 break;
177 case STATE_DEACTIVATED:
178 _state = STATE_DEACTIVATED;
180 if (timer != NULL) {
181 delete timer;
182 timer = NULL;
183 }
184 break;
185 default:
186 break;
187 }
188 }
190 return;
191 }
193 /**
194 \return The state the extension is in
195 \brief A getter for the state variable.
196 */
197 Extension::state_t
198 Extension::get_state (void)
199 {
200 return _state;
201 }
203 /**
204 \return Whether the extension is loaded or not
205 \brief A quick function to test the state of the extension
206 */
207 bool
208 Extension::loaded (void)
209 {
210 return get_state() == STATE_LOADED;
211 }
213 /**
214 \return A boolean saying whether the extension passed the checks
215 \brief A function to check the validity of the extension
217 This function chekcs to make sure that there is an id, a name, a
218 repr and an implemenation for this extension. Then it checks all
219 of the dependencies to see if they pass. Finally, it asks the
220 implmentation to do a check of itself.
222 On each check, if there is a failure, it will print a message to the
223 error log for that failure. It is important to note that the function
224 keeps executing if it finds an error, to try and get as many of them
225 into the error log as possible. This should help people debug
226 installations, and figure out what they need to get for the full
227 functionality of Inkscape to be available.
228 */
229 bool
230 Extension::check (void)
231 {
232 bool retval = true;
234 // static int i = 0;
235 // std::cout << "Checking module[" << i++ << "]: " << name << std::endl;
237 const char * inx_failure = _(" This is caused by an improper .inx file for this extension."
238 " An improper .inx file could have been caused by a faulty installation of Inkscape.");
239 if (id == NULL) {
240 printFailure(Glib::ustring(_("an ID was not defined for it.")) + inx_failure);
241 retval = false;
242 }
243 if (name == NULL) {
244 printFailure(Glib::ustring(_("there was no name defined for it.")) + inx_failure);
245 retval = false;
246 }
247 if (repr == NULL) {
248 printFailure(Glib::ustring(_("the XML description of it got lost.")) + inx_failure);
249 retval = false;
250 }
251 if (imp == NULL) {
252 printFailure(Glib::ustring(_("no implementation was defined for the extension.")) + inx_failure);
253 retval = false;
254 }
256 for (unsigned int i = 0 ; i < _deps.size(); i++) {
257 if (_deps[i]->check() == FALSE) {
258 // std::cout << "Failed: " << *(_deps[i]) << std::endl;
259 printFailure(Glib::ustring(_("a dependency was not met.")));
260 error_file << *_deps[i] << std::endl;
261 retval = false;
262 }
263 }
265 if (retval)
266 return imp->check(this);
267 return retval;
268 }
270 /** \brief A quick function to print out a standard start of extension
271 errors in the log.
272 \param reason A string explaining why this failed
274 Real simple, just put everything into \c error_file.
275 */
276 void
277 Extension::printFailure (Glib::ustring reason)
278 {
279 error_file << _("Extension \"") << name << _("\" failed to load because ");
280 error_file << reason;
281 error_file << std::endl;
282 return;
283 }
285 /**
286 \return The XML tree that is used to define the extension
287 \brief A getter for the internal Repr, does not add a reference.
288 */
289 Inkscape::XML::Node *
290 Extension::get_repr (void)
291 {
292 return repr;
293 }
295 /**
296 \return The textual id of this extension
297 \brief Get the ID of this extension - not a copy don't delete!
298 */
299 gchar *
300 Extension::get_id (void)
301 {
302 return id;
303 }
305 /**
306 \return The textual name of this extension
307 \brief Get the name of this extension - not a copy don't delete!
308 */
309 gchar *
310 Extension::get_name (void)
311 {
312 return name;
313 }
315 /**
316 \return None
317 \brief This function diactivates the extension (which makes it
318 unusable, but not deleted)
320 This function is used to removed an extension from functioning, but
321 not delete it completely. It sets the state to \c STATE_DEACTIVATED to
322 mark to the world that it has been deactivated. It also removes
323 the current implementation and replaces it with a standard one. This
324 makes it so that we don't have to continually check if there is an
325 implementation, but we are gauranteed to have a benign one.
327 \warning It is important to note that there is no 'activate' function.
328 Running this function is irreversable.
329 */
330 void
331 Extension::deactivate (void)
332 {
333 set_state(STATE_DEACTIVATED);
335 /* Removing the old implementation, and making this use the default. */
336 /* This should save some memory */
337 delete imp;
338 imp = new Implementation::Implementation();
340 return;
341 }
343 /**
344 \return Whether the extension has been deactivated
345 \brief Find out the status of the extension
346 */
347 bool
348 Extension::deactivated (void)
349 {
350 return get_state() == STATE_DEACTIVATED;
351 }
353 /**
354 \return Parameter structure with a name of 'name'
355 \brief This function looks through the linked list for a parameter
356 structure with the name of the passed in name
357 \param name The name to search for
358 \param list The list to look for
360 This is an inline function that is used by all the get_param and
361 set_param functions to find a param_t in the linked list with
362 the passed in name. It is done as an inline so that it will be
363 optimized into a 'jump' by the compiler.
365 This function can throw a 'param_not_exist' exception if the
366 name is not found.
368 The first thing that this function checks is if the list is NULL.
369 It could be NULL because there are no parameters for this extension
370 or because all of them have been checked (I'll spoil the ending and
371 tell you that this function is called recursively). If the list
372 is NULL then the 'param_not_exist' exception is thrown.
374 Otherwise, the function looks at the current param_t that the element
375 list points to. If the name of that param_t matches the passed in
376 name then that param_t is returned. Otherwise, this function is
377 called again with g_slist_next as a parameter.
378 */
379 Parameter *
380 param_shared (const gchar * name, GSList * list)
381 {
382 Parameter * output;
384 if (name == NULL) {
385 throw Extension::param_not_exist();
386 }
387 if (list == NULL) {
388 throw Extension::param_not_exist();
389 }
391 output = static_cast<Parameter *>(list->data);
392 if (!strcmp(output->name(), name)) {
393 return output;
394 }
396 return param_shared(name, g_slist_next(list));
397 }
399 /**
400 \return A constant pointer to the string held by the parameters.
401 \brief Gets a parameter identified by name with the string placed
402 in value. It isn't duplicated into the value string.
403 \param name The name of the parameter to get
404 \param doc The document to look in for document specific parameters
406 Look up in the parameters list, then execute the function on that
407 found parameter.
408 */
409 const gchar *
410 Extension::get_param_string (const gchar * name, const Inkscape::XML::Document * doc)
411 {
412 Parameter * param;
414 param = param_shared(name, parameters);
415 return param->get_string(doc);
416 }
418 /**
419 \return The value of the parameter identified by the name
420 \brief Gets a parameter identified by name with the bool placed
421 in value.
422 \param name The name of the parameter to get
423 \param doc The document to look in for document specific parameters
425 Look up in the parameters list, then execute the function on that
426 found parameter.
427 */
428 bool
429 Extension::get_param_bool (const gchar * name, const Inkscape::XML::Document * doc)
430 {
431 Parameter * param;
433 param = param_shared(name, parameters);
434 return param->get_bool(doc);
435 }
437 /**
438 \return The integer value for the parameter specified
439 \brief Gets a parameter identified by name with the integer placed
440 in value.
441 \param name The name of the parameter to get
442 \param doc The document to look in for document specific parameters
444 Look up in the parameters list, then execute the function on that
445 found parameter.
446 */
447 int
448 Extension::get_param_int (const gchar * name, const Inkscape::XML::Document * doc)
449 {
450 Parameter * param;
452 param = param_shared(name, parameters);
453 return param->get_int(doc);
454 }
456 /**
457 \return The float value for the parameter specified
458 \brief Gets a parameter identified by name with the float placed
459 in value.
460 \param name The name of the parameter to get
461 \param doc The document to look in for document specific parameters
463 Look up in the parameters list, then execute the function on that
464 found parameter.
465 */
466 float
467 Extension::get_param_float (const gchar * name, const Inkscape::XML::Document * doc)
468 {
469 Parameter * param;
470 param = param_shared(name, parameters);
471 return param->get_float(doc);
472 }
474 /**
475 \return The passed in value
476 \brief Sets a parameter identified by name with the boolean
477 in the parameter value.
478 \param name The name of the parameter to set
479 \param value The value to set the parameter to
480 \param doc The document to look in for document specific parameters
482 Look up in the parameters list, then execute the function on that
483 found parameter.
484 */
485 bool
486 Extension::set_param_bool (const gchar * name, bool value, Inkscape::XML::Document * doc)
487 {
488 Parameter * param;
489 param = param_shared(name, parameters);
490 return param->set_bool(value, doc);
491 }
493 /**
494 \return The passed in value
495 \brief Sets a parameter identified by name with the integer
496 in the parameter value.
497 \param name The name of the parameter to set
498 \param value The value to set the parameter to
499 \param doc The document to look in for document specific parameters
501 Look up in the parameters list, then execute the function on that
502 found parameter.
503 */
504 int
505 Extension::set_param_int (const gchar * name, int value, Inkscape::XML::Document * doc)
506 {
507 Parameter * param;
508 param = param_shared(name, parameters);
509 return param->set_int(value, doc);
510 }
512 /**
513 \return The passed in value
514 \brief Sets a parameter identified by name with the integer
515 in the parameter value.
516 \param name The name of the parameter to set
517 \param value The value to set the parameter to
518 \param doc The document to look in for document specific parameters
520 Look up in the parameters list, then execute the function on that
521 found parameter.
522 */
523 float
524 Extension::set_param_float (const gchar * name, float value, Inkscape::XML::Document * doc)
525 {
526 Parameter * param;
527 param = param_shared(name, parameters);
528 return param->set_float(value, doc);
529 }
531 /**
532 \return The passed in value
533 \brief Sets a parameter identified by name with the string
534 in the parameter value.
535 \param name The name of the parameter to set
536 \param value The value to set the parameter to
537 \param doc The document to look in for document specific parameters
539 Look up in the parameters list, then execute the function on that
540 found parameter.
541 */
542 const gchar *
543 Extension::set_param_string (const gchar * name, const gchar * value, Inkscape::XML::Document * doc)
544 {
545 Parameter * param;
546 param = param_shared(name, parameters);
547 return param->set_string(value, doc);
548 }
550 /** \brief A function to open the error log file. */
551 void
552 Extension::error_file_open (void)
553 {
554 gchar * ext_error_file = profile_path(EXTENSION_ERROR_LOG_FILENAME);
555 gchar * filename = g_filename_from_utf8( ext_error_file, -1, NULL, NULL, NULL );
556 error_file.open(filename);
557 if (!error_file.is_open()) {
558 g_warning(_("Could not create extension error log file '%s'"),
559 filename);
560 }
561 g_free(filename);
562 g_free(ext_error_file);
563 };
565 /** \brief A function to close the error log file. */
566 void
567 Extension::error_file_close (void)
568 {
569 error_file.close();
570 };
572 /** \brief A function to automatically generate a GUI using the parameters
573 \return Generated widget
575 This function just goes through each parameter, and calls it's 'get_widget'
576 function to get each widget. Then, each of those is placed into
577 a Gtk::VBox, which is then returned to the calling function.
579 If there are no parameters, this function just returns NULL.
580 */
581 Gtk::Widget *
582 Extension::autogui (void)
583 {
584 if (g_slist_length(parameters) == 0) return NULL;
586 Gtk::VBox * vbox = new Gtk::VBox();
587 vbox = new Gtk::VBox();
589 for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
590 Parameter * param = reinterpret_cast<Parameter *>(list->data);
591 Gtk::Widget * widg = param->get_widget();
592 if (widg != NULL)
593 vbox->pack_start(*widg, true, true);
594 }
596 vbox->show();
597 return vbox;
598 };
600 /**
601 \brief A function to get the parameters in a string form
602 \return A string with all the parameters as command line arguements
604 I don't really like this function, but it works for now.
606 \todo Do this better.
607 */
608 Glib::ustring *
609 Extension::paramString (void)
610 {
611 Glib::ustring * param_string = new Glib::ustring("");
613 for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
614 Parameter * param = reinterpret_cast<Parameter *>(list->data);
616 *param_string += " --";
617 *param_string += param->name();
618 *param_string += "=";
619 Glib::ustring * paramstr = param->string();
620 *param_string += *paramstr;
621 delete paramstr;
622 }
624 return param_string;
625 }
627 } /* namespace Extension */
628 } /* namespace Inkscape */
631 /*
632 Local Variables:
633 mode:c++
634 c-file-style:"stroustrup"
635 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
636 indent-tabs-mode:nil
637 fill-column:99
638 End:
639 */
640 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :