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)
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, "param")) {
88 Parameter * param;
89 param = Parameter::make(child_repr, this);
90 if (param != NULL)
91 parameters = g_slist_append(parameters, param);
92 } /* param */
93 if (!strcmp(chname, "dependency")) {
94 _deps.push_back(new Dependency(child_repr));
95 } /* param */
96 child_repr = sp_repr_next(child_repr);
97 }
99 db.register_ext (this);
100 }
101 // printf("%s\n", name);
102 timer = NULL;
104 return;
105 }
107 /**
108 \return none
109 \brief Destroys the Extension
111 This function frees all of the strings that could be attached
112 to the extension and also unreferences the repr. This is better
113 than freeing it because it may (I wouldn't know why) be referenced
114 in another place.
115 */
116 Extension::~Extension (void)
117 {
118 // printf("Extension Destructor: %s\n", name);
119 set_state(STATE_UNLOADED);
120 db.unregister_ext(this);
121 Inkscape::GC::release(repr);
122 g_free(id);
123 g_free(name);
124 delete timer;
125 timer = NULL;
126 /** \todo Need to do parameters here */
128 for (unsigned int i = 0 ; i < _deps.size(); i++) {
129 delete _deps[i];
130 }
131 _deps.clear();
133 return;
134 }
136 /**
137 \return none
138 \brief A function to set whether the extension should be loaded
139 or unloaded
140 \param in_state Which state should the extension be in?
142 It checks to see if this is a state change or not. If we're changing
143 states it will call the appropriate function in the implementation,
144 load or unload. Currently, there is no error checking in this
145 function. There should be.
146 */
147 void
148 Extension::set_state (state_t in_state)
149 {
150 if (_state == STATE_DEACTIVATED) return;
151 if (in_state != _state) {
152 /** \todo Need some more error checking here! */
153 switch (in_state) {
154 case STATE_LOADED:
155 if (imp->load(this))
156 _state = STATE_LOADED;
158 if (timer != NULL) {
159 delete timer;
160 }
161 timer = new ExpirationTimer(this);
163 break;
164 case STATE_UNLOADED:
165 // std::cout << "Unloading: " << name << std::endl;
166 imp->unload(this);
167 _state = STATE_UNLOADED;
169 if (timer != NULL) {
170 delete timer;
171 timer = NULL;
172 }
173 break;
174 case STATE_DEACTIVATED:
175 _state = STATE_DEACTIVATED;
177 if (timer != NULL) {
178 delete timer;
179 timer = NULL;
180 }
181 break;
182 default:
183 break;
184 }
185 }
187 return;
188 }
190 /**
191 \return The state the extension is in
192 \brief A getter for the state variable.
193 */
194 Extension::state_t
195 Extension::get_state (void)
196 {
197 return _state;
198 }
200 /**
201 \return Whether the extension is loaded or not
202 \brief A quick function to test the state of the extension
203 */
204 bool
205 Extension::loaded (void)
206 {
207 return get_state() == STATE_LOADED;
208 }
210 /**
211 \return A boolean saying whether the extension passed the checks
212 \brief A function to check the validity of the extension
214 This function chekcs to make sure that there is an id, a name, a
215 repr and an implemenation for this extension. Then it checks all
216 of the dependencies to see if they pass. Finally, it asks the
217 implmentation to do a check of itself.
219 On each check, if there is a failure, it will print a message to the
220 error log for that failure. It is important to note that the function
221 keeps executing if it finds an error, to try and get as many of them
222 into the error log as possible. This should help people debug
223 installations, and figure out what they need to get for the full
224 functionality of Inkscape to be available.
225 */
226 bool
227 Extension::check (void)
228 {
229 bool retval = true;
231 // static int i = 0;
232 // std::cout << "Checking module[" << i++ << "]: " << name << std::endl;
234 const char * inx_failure = _(" This is caused by an improper .inx file for this extension."
235 " An improper .inx file could have been caused by a faulty installation of Inkscape.");
236 if (id == NULL) {
237 printFailure(Glib::ustring(_("an ID was not defined for it.")) + inx_failure);
238 retval = false;
239 }
240 if (name == NULL) {
241 printFailure(Glib::ustring(_("there was no name defined for it.")) + inx_failure);
242 retval = false;
243 }
244 if (repr == NULL) {
245 printFailure(Glib::ustring(_("the XML description of it got lost.")) + inx_failure);
246 retval = false;
247 }
248 if (imp == NULL) {
249 printFailure(Glib::ustring(_("no implementation was defined for the extension.")) + inx_failure);
250 retval = false;
251 }
253 for (unsigned int i = 0 ; i < _deps.size(); i++) {
254 if (_deps[i]->check() == FALSE) {
255 // std::cout << "Failed: " << *(_deps[i]) << std::endl;
256 printFailure(Glib::ustring(_("a dependency was not met.")));
257 error_file << *_deps[i] << std::endl;
258 retval = false;
259 }
260 }
262 if (retval)
263 return imp->check(this);
264 return retval;
265 }
267 /** \brief A quick function to print out a standard start of extension
268 errors in the log.
269 \param reason A string explaining why this failed
271 Real simple, just put everything into \c error_file.
272 */
273 void
274 Extension::printFailure (Glib::ustring reason)
275 {
276 error_file << _("Extension \"") << name << _("\" failed to load because ");
277 error_file << reason;
278 error_file << std::endl;
279 return;
280 }
282 /**
283 \return The XML tree that is used to define the extension
284 \brief A getter for the internal Repr, does not add a reference.
285 */
286 Inkscape::XML::Node *
287 Extension::get_repr (void)
288 {
289 return repr;
290 }
292 /**
293 \return The textual id of this extension
294 \brief Get the ID of this extension - not a copy don't delete!
295 */
296 gchar *
297 Extension::get_id (void)
298 {
299 return id;
300 }
302 /**
303 \return The textual name of this extension
304 \brief Get the name of this extension - not a copy don't delete!
305 */
306 gchar *
307 Extension::get_name (void)
308 {
309 return name;
310 }
312 /**
313 \return None
314 \brief This function diactivates the extension (which makes it
315 unusable, but not deleted)
317 This function is used to removed an extension from functioning, but
318 not delete it completely. It sets the state to \c STATE_DEACTIVATED to
319 mark to the world that it has been deactivated. It also removes
320 the current implementation and replaces it with a standard one. This
321 makes it so that we don't have to continually check if there is an
322 implementation, but we are gauranteed to have a benign one.
324 \warning It is important to note that there is no 'activate' function.
325 Running this function is irreversable.
326 */
327 void
328 Extension::deactivate (void)
329 {
330 set_state(STATE_DEACTIVATED);
332 /* Removing the old implementation, and making this use the default. */
333 /* This should save some memory */
334 delete imp;
335 imp = new Implementation::Implementation();
337 return;
338 }
340 /**
341 \return Whether the extension has been deactivated
342 \brief Find out the status of the extension
343 */
344 bool
345 Extension::deactivated (void)
346 {
347 return get_state() == STATE_DEACTIVATED;
348 }
350 /**
351 \return Parameter structure with a name of 'name'
352 \brief This function looks through the linked list for a parameter
353 structure with the name of the passed in name
354 \param name The name to search for
355 \param list The list to look for
357 This is an inline function that is used by all the get_param and
358 set_param functions to find a param_t in the linked list with
359 the passed in name. It is done as an inline so that it will be
360 optimized into a 'jump' by the compiler.
362 This function can throw a 'param_not_exist' exception if the
363 name is not found.
365 The first thing that this function checks is if the list is NULL.
366 It could be NULL because there are no parameters for this extension
367 or because all of them have been checked (I'll spoil the ending and
368 tell you that this function is called recursively). If the list
369 is NULL then the 'param_not_exist' exception is thrown.
371 Otherwise, the function looks at the current param_t that the element
372 list points to. If the name of that param_t matches the passed in
373 name then that param_t is returned. Otherwise, this function is
374 called again with g_slist_next as a parameter.
375 */
376 Parameter *
377 param_shared (const gchar * name, GSList * list)
378 {
379 Parameter * output;
381 if (name == NULL) {
382 throw Extension::param_not_exist();
383 }
384 if (list == NULL) {
385 throw Extension::param_not_exist();
386 }
388 output = static_cast<Parameter *>(list->data);
389 if (!strcmp(output->name(), name)) {
390 return output;
391 }
393 return param_shared(name, g_slist_next(list));
394 }
396 /**
397 \return A constant pointer to the string held by the parameters.
398 \brief Gets a parameter identified by name with the string placed
399 in value. It isn't duplicated into the value string.
400 \param name The name of the parameter to get
401 \param doc The document to look in for document specific parameters
403 Look up in the parameters list, then execute the function on that
404 found parameter.
405 */
406 const gchar *
407 Extension::get_param_string (const gchar * name, const Inkscape::XML::Document * doc)
408 {
409 Parameter * param;
411 param = param_shared(name, parameters);
412 return param->get_string(doc);
413 }
415 /**
416 \return The value of the parameter identified by the name
417 \brief Gets a parameter identified by name with the bool placed
418 in value.
419 \param name The name of the parameter to get
420 \param doc The document to look in for document specific parameters
422 Look up in the parameters list, then execute the function on that
423 found parameter.
424 */
425 bool
426 Extension::get_param_bool (const gchar * name, const Inkscape::XML::Document * doc)
427 {
428 Parameter * param;
430 param = param_shared(name, parameters);
431 return param->get_bool(doc);
432 }
434 /**
435 \return The integer value for the parameter specified
436 \brief Gets a parameter identified by name with the integer placed
437 in value.
438 \param name The name of the parameter to get
439 \param doc The document to look in for document specific parameters
441 Look up in the parameters list, then execute the function on that
442 found parameter.
443 */
444 int
445 Extension::get_param_int (const gchar * name, const Inkscape::XML::Document * doc)
446 {
447 Parameter * param;
449 param = param_shared(name, parameters);
450 return param->get_int(doc);
451 }
453 /**
454 \return The float value for the parameter specified
455 \brief Gets a parameter identified by name with the float placed
456 in value.
457 \param name The name of the parameter to get
458 \param doc The document to look in for document specific parameters
460 Look up in the parameters list, then execute the function on that
461 found parameter.
462 */
463 float
464 Extension::get_param_float (const gchar * name, const Inkscape::XML::Document * doc)
465 {
466 Parameter * param;
467 param = param_shared(name, parameters);
468 return param->get_float(doc);
469 }
471 /**
472 \return The passed in value
473 \brief Sets a parameter identified by name with the boolean
474 in the parameter value.
475 \param name The name of the parameter to set
476 \param value The value to set the parameter to
477 \param doc The document to look in for document specific parameters
479 Look up in the parameters list, then execute the function on that
480 found parameter.
481 */
482 bool
483 Extension::set_param_bool (const gchar * name, bool value, Inkscape::XML::Document * doc)
484 {
485 Parameter * param;
486 param = param_shared(name, parameters);
487 return param->set_bool(value, doc);
488 }
490 /**
491 \return The passed in value
492 \brief Sets a parameter identified by name with the integer
493 in the parameter value.
494 \param name The name of the parameter to set
495 \param value The value to set the parameter to
496 \param doc The document to look in for document specific parameters
498 Look up in the parameters list, then execute the function on that
499 found parameter.
500 */
501 int
502 Extension::set_param_int (const gchar * name, int value, Inkscape::XML::Document * doc)
503 {
504 Parameter * param;
505 param = param_shared(name, parameters);
506 return param->set_int(value, doc);
507 }
509 /**
510 \return The passed in value
511 \brief Sets a parameter identified by name with the integer
512 in the parameter value.
513 \param name The name of the parameter to set
514 \param value The value to set the parameter to
515 \param doc The document to look in for document specific parameters
517 Look up in the parameters list, then execute the function on that
518 found parameter.
519 */
520 float
521 Extension::set_param_float (const gchar * name, float value, Inkscape::XML::Document * doc)
522 {
523 Parameter * param;
524 param = param_shared(name, parameters);
525 return param->set_float(value, doc);
526 }
528 /**
529 \return The passed in value
530 \brief Sets a parameter identified by name with the string
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
536 Look up in the parameters list, then execute the function on that
537 found parameter.
538 */
539 const gchar *
540 Extension::set_param_string (const gchar * name, const gchar * value, Inkscape::XML::Document * doc)
541 {
542 Parameter * param;
543 param = param_shared(name, parameters);
544 return param->set_string(value, doc);
545 }
547 /** \brief A function to open the error log file. */
548 void
549 Extension::error_file_open (void)
550 {
551 gchar * ext_error_file = profile_path(EXTENSION_ERROR_LOG_FILENAME);
552 gchar * filename = g_filename_from_utf8( ext_error_file, -1, NULL, NULL, NULL );
553 error_file.open(filename);
554 if (!error_file.is_open()) {
555 g_warning(_("Could not create extension error log file '%s'"),
556 filename);
557 }
558 g_free(filename);
559 g_free(ext_error_file);
560 };
562 /** \brief A function to close the error log file. */
563 void
564 Extension::error_file_close (void)
565 {
566 error_file.close();
567 };
569 /** \brief A function to automatically generate a GUI using the parameters
570 \return Generated widget
572 This function just goes through each parameter, and calls it's 'get_widget'
573 function to get each widget. Then, each of those is placed into
574 a Gtk::VBox, which is then returned to the calling function.
576 If there are no parameters, this function just returns NULL.
577 */
578 Gtk::Widget *
579 Extension::autogui (void)
580 {
581 if (g_slist_length(parameters) == 0) return NULL;
583 Gtk::VBox * vbox = new Gtk::VBox();
584 vbox = new Gtk::VBox();
586 for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
587 Parameter * param = reinterpret_cast<Parameter *>(list->data);
588 Gtk::Widget * widg = param->get_widget();
589 if (widg != NULL)
590 vbox->pack_start(*widg, true, true);
591 }
593 vbox->show();
594 return vbox;
595 };
597 /**
598 \brief A function to get the parameters in a string form
599 \return A string with all the parameters as command line arguements
601 I don't really like this function, but it works for now.
603 \todo Do this better.
604 */
605 Glib::ustring *
606 Extension::paramString (void)
607 {
608 Glib::ustring * param_string = new Glib::ustring("");
610 for (GSList * list = parameters; list != NULL; list = g_slist_next(list)) {
611 Parameter * param = reinterpret_cast<Parameter *>(list->data);
613 *param_string += " --";
614 *param_string += param->name();
615 *param_string += "=";
616 Glib::ustring * paramstr = param->string();
617 *param_string += *paramstr;
618 delete paramstr;
619 }
621 return param_string;
622 }
624 } /* namespace Extension */
625 } /* namespace Inkscape */
628 /*
629 Local Variables:
630 mode:c++
631 c-file-style:"stroustrup"
632 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
633 indent-tabs-mode:nil
634 fill-column:99
635 End:
636 */
637 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :