c913def611395cda3b992b95ab104763efe10e0e
1 /*
2 * This is what gets executed to initialize all of the modules. For
3 * the internal modules this invovles executing their initialization
4 * functions, for external ones it involves reading their .spmodule
5 * files and bringing them into Sodipodi.
6 *
7 * Authors:
8 * Ted Gould <ted@gould.cx>
9 *
10 * Copyright (C) 2002-2004 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "path-prefix.h"
21 #include "inkscape.h"
22 #include <glibmm/i18n.h>
24 #include "system.h"
25 #include "db.h"
26 #include "internal/svgz.h"
27 #include "internal/ps.h"
28 #ifdef WITH_GNOME_PRINT
29 # include "internal/gnome.h"
30 #endif
31 #ifdef WIN32
32 # include "internal/win32.h"
33 #endif
34 #include "internal/ps-out.h"
35 #include "internal/pov-out.h"
36 #include "internal/latex-pstricks-out.h"
37 #include "internal/latex-pstricks.h"
38 #include "internal/eps-out.h"
39 #include "internal/gdkpixbuf-input.h"
40 #include "internal/bluredge.h"
41 #include "internal/gimpgrad.h"
42 #include "internal/grid.h"
43 #include "prefs-utils.h"
44 #include "io/sys.h"
46 extern gboolean inkscape_app_use_gui( Inkscape::Application const *app );
48 namespace Inkscape {
49 namespace Extension {
51 /** This is the extention that all files are that are pulled from
52 the extension directory and parsed */
53 #define SP_MODULE_EXTENSION "inx"
55 static void build_module_from_dir(gchar const *dirname);
56 static void check_extensions();
58 /**
59 * \return none
60 * \brief Examines the given string preference and checks to see
61 * that at least one of the registered extensions matches
62 * it. If not, a default is assigned.
63 * \param pref_path Preference path to load
64 * \param pref_attr Attribute to load from the preference
65 * \param pref_default Default string to set
66 * \param extension_family List of extensions to search
67 */
68 static void
69 update_pref(gchar const *pref_path, gchar const *pref_attr,
70 gchar const *pref_default) // , GSList *extension_family)
71 {
72 gchar const *pref = prefs_get_string_attribute(pref_path,pref_attr);
73 /*
74 gboolean missing=TRUE;
75 for (GSList *list = extension_family; list; list = g_slist_next(list)) {
76 g_assert( list->data );
78 Inkscape::Extension *extension;
79 extension = reinterpret_cast<Inkscape::Extension *>(list->data);
81 if (!strcmp(extension->get_id(),pref)) missing=FALSE;
82 }
83 */
84 if (!Inkscape::Extension::db.get( pref ) /*missing*/) {
85 prefs_set_string_attribute(pref_path, pref_attr, pref_default);
86 }
87 }
89 /**
90 * Invokes the init routines for internal modules.
91 *
92 * This should be a list of all the internal modules that need to initialized. This is just a
93 * convinent place to put them. Also, this function calls build_module_from_dir to parse the
94 * Inkscape extensions directory.
95 */
96 void
97 init()
98 {
99 /* TODO: Change to Internal */
100 Internal::Svg::init();
101 Internal::Svgz::init();
102 Internal::PsOutput::init();
103 Internal::EpsOutput::init();
104 Internal::PrintPS::init();
105 #ifdef WITH_GNOME_PRINT
106 Internal::PrintGNOME::init();
107 #endif
108 #ifdef WIN32
109 Internal::PrintWin32::init();
110 #endif
111 Internal::PovOutput::init();
112 Internal::PrintLatex::init();
113 Internal::LatexOutput::init();
115 /* Effects */
116 Internal::BlurEdge::init();
117 Internal::GimpGrad::init();
118 Internal::Grid::init();
120 /* Load search path for extensions */
121 if (Inkscape::Extension::Extension::search_path.size() == 0)
122 {
123 Inkscape::Extension::Extension::search_path.push_back(profile_path("extensions"));
124 Inkscape::Extension::Extension::search_path.push_back(g_strdup(INKSCAPE_EXTENSIONDIR));
125 }
127 for (unsigned int i=0; i<Inkscape::Extension::Extension::search_path.size(); i++) {
128 build_module_from_dir(Inkscape::Extension::Extension::search_path[i]);
129 }
131 /* this is at the very end because it has several catch-alls
132 * that are possibly over-ridden by other extensions (such as
133 * svgz)
134 */
135 Internal::GdkpixbufInput::init();
137 /* now we need to check and make sure everyone is happy */
138 check_extensions();
140 /* This is a hack to deal with updating saved outdated module
141 * names in the prefs...
142 */
143 update_pref("dialogs.save_as", "default",
144 SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE
145 // Inkscape::Extension::db.get_output_list()
146 );
147 }
149 /**
150 * \return none
151 * \brief This function parses a directory for files of SP_MODULE_EXTENSION
152 * type and loads them.
153 * \param dirname The directory that should be searched for modules
154 *
155 * Here is just a basic function that moves through a directory. It looks at every entry, and
156 * compares its filename with SP_MODULE_EXTENSION. Of those that pass, build_from_file is called
157 * with their filenames.
158 */
159 static void
160 build_module_from_dir(gchar const *dirname)
161 {
162 if (!dirname) {
163 g_warning(_("Null external module directory name. Modules will not be loaded."));
164 return;
165 }
167 if (!Glib::file_test(std::string(dirname), Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
168 return;
169 }
171 //# Hopefully doing this the Glib way is portable
173 GError *err;
174 GDir *directory = g_dir_open(dirname, 0, &err);
175 if (!directory) {
176 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
177 g_warning(_("Modules directory (%s) is unavailable. External modules in that directory will not be loaded."), safeDir);
178 g_free(safeDir);
179 return;
180 }
182 gchar *filename;
183 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
185 if (strlen(filename) < strlen(SP_MODULE_EXTENSION)) {
186 continue;
187 }
189 if (strcmp(SP_MODULE_EXTENSION, filename + (strlen(filename) - strlen(SP_MODULE_EXTENSION)))) {
190 continue;
191 }
193 gchar *pathname = g_strdup_printf("%s/%s", dirname, filename);
194 build_from_file(pathname);
195 g_free(pathname);
196 }
198 g_dir_close(directory);
199 }
202 static void
203 check_extensions_internal(Extension *in_plug, gpointer in_data)
204 {
205 int *count = (int *)in_data;
207 if (in_plug == NULL) return;
208 if (!in_plug->deactivated() && !in_plug->check()) {
209 in_plug->deactivate();
210 (*count)++;
211 }
212 }
214 static void
215 check_extensions()
216 {
217 int count = 1;
218 bool anyfail = false;
219 // int pass = 0;
221 Inkscape::Extension::Extension::error_file_open();
222 while (count != 0) {
223 // printf("Check extensions pass %d\n", pass++);
224 count = 0;
225 db.foreach(check_extensions_internal, (gpointer)&count);
226 if (count != 0) anyfail = true;
227 }
228 Inkscape::Extension::Extension::error_file_close();
229 }
231 } } /* namespace Inkscape::Extension */
234 /*
235 Local Variables:
236 mode:c++
237 c-file-style:"stroustrup"
238 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239 indent-tabs-mode:nil
240 fill-column:99
241 End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :