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 HAVE_CAIRO_PDF
29 # include "internal/pdf-cairo.h"
30 #endif
31 #ifdef WITH_GNOME_PRINT
32 # include "internal/gnome.h"
33 #endif
34 #ifdef WIN32
35 # include "internal/win32.h"
36 # include "internal/emf-win32-inout.h"
37 # include "internal/emf-win32-print.h"
38 #endif
39 #include "internal/ps-out.h"
40 #ifdef HAVE_CAIRO_PDF
41 # include "internal/cairo-pdf-out.h"
42 # include "internal/cairo-renderer-pdf-out.h"
43 # include "internal/cairo-png-out.h"
44 #endif
45 #include "internal/pov-out.h"
46 #include "internal/odf.h"
47 #include "internal/latex-pstricks-out.h"
48 #include "internal/latex-pstricks.h"
49 #include "internal/eps-out.h"
50 #include "internal/gdkpixbuf-input.h"
51 #include "internal/bluredge.h"
52 #include "internal/gimpgrad.h"
53 #include "internal/grid.h"
54 #include "internal/wpg-input.h"
55 #include "prefs-utils.h"
56 #include "io/sys.h"
58 extern gboolean inkscape_app_use_gui( Inkscape::Application const *app );
60 namespace Inkscape {
61 namespace Extension {
63 /** This is the extention that all files are that are pulled from
64 the extension directory and parsed */
65 #define SP_MODULE_EXTENSION "inx"
67 static void build_module_from_dir(gchar const *dirname);
68 static void check_extensions();
70 /**
71 * \return none
72 * \brief Examines the given string preference and checks to see
73 * that at least one of the registered extensions matches
74 * it. If not, a default is assigned.
75 * \param pref_path Preference path to load
76 * \param pref_attr Attribute to load from the preference
77 * \param pref_default Default string to set
78 * \param extension_family List of extensions to search
79 */
80 static void
81 update_pref(gchar const *pref_path, gchar const *pref_attr,
82 gchar const *pref_default) // , GSList *extension_family)
83 {
84 gchar const *pref = prefs_get_string_attribute(pref_path,pref_attr);
85 /*
86 gboolean missing=TRUE;
87 for (GSList *list = extension_family; list; list = g_slist_next(list)) {
88 g_assert( list->data );
90 Inkscape::Extension *extension;
91 extension = reinterpret_cast<Inkscape::Extension *>(list->data);
93 if (!strcmp(extension->get_id(),pref)) missing=FALSE;
94 }
95 */
96 if (!Inkscape::Extension::db.get( pref ) /*missing*/) {
97 prefs_set_string_attribute(pref_path, pref_attr, pref_default);
98 }
99 }
101 /**
102 * Invokes the init routines for internal modules.
103 *
104 * This should be a list of all the internal modules that need to initialized. This is just a
105 * convinent place to put them. Also, this function calls build_module_from_dir to parse the
106 * Inkscape extensions directory.
107 */
108 void
109 init()
110 {
111 /* TODO: Change to Internal */
112 Internal::Svg::init();
113 Internal::Svgz::init();
114 Internal::PsOutput::init();
115 Internal::EpsOutput::init();
116 Internal::PrintPS::init();
117 #ifdef HAVE_CAIRO_PDF
118 Internal::CairoPdfOutput::init();
119 Internal::PrintCairoPDF::init();
120 if (0) {
121 Internal::CairoRendererPdfOutput::init();
122 Internal::CairoRendererOutput::init();
123 }
124 #endif
125 #ifdef WITH_GNOME_PRINT
126 Internal::PrintGNOME::init();
127 #endif
128 #ifdef WIN32
129 Internal::PrintWin32::init();
130 Internal::PrintEmfWin32::init();
131 Internal::EmfWin32::init();
132 #endif
133 Internal::PovOutput::init();
134 Internal::OdfOutput::init();
135 Internal::PrintLatex::init();
136 Internal::LatexOutput::init();
137 Internal::WpgInput::init();
139 /* Effects */
140 Internal::BlurEdge::init();
141 Internal::GimpGrad::init();
142 Internal::Grid::init();
144 /* Load search path for extensions */
145 if (Inkscape::Extension::Extension::search_path.size() == 0)
146 {
147 Inkscape::Extension::Extension::search_path.push_back(profile_path("extensions"));
148 Inkscape::Extension::Extension::search_path.push_back(g_strdup(INKSCAPE_EXTENSIONDIR));
149 }
151 for (unsigned int i=0; i<Inkscape::Extension::Extension::search_path.size(); i++) {
152 build_module_from_dir(Inkscape::Extension::Extension::search_path[i]);
153 }
155 /* this is at the very end because it has several catch-alls
156 * that are possibly over-ridden by other extensions (such as
157 * svgz)
158 */
159 Internal::GdkpixbufInput::init();
161 /* now we need to check and make sure everyone is happy */
162 check_extensions();
164 /* This is a hack to deal with updating saved outdated module
165 * names in the prefs...
166 */
167 update_pref("dialogs.save_as", "default",
168 SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE
169 // Inkscape::Extension::db.get_output_list()
170 );
171 }
173 /**
174 * \return none
175 * \brief This function parses a directory for files of SP_MODULE_EXTENSION
176 * type and loads them.
177 * \param dirname The directory that should be searched for modules
178 *
179 * Here is just a basic function that moves through a directory. It looks at every entry, and
180 * compares its filename with SP_MODULE_EXTENSION. Of those that pass, build_from_file is called
181 * with their filenames.
182 */
183 static void
184 build_module_from_dir(gchar const *dirname)
185 {
186 if (!dirname) {
187 g_warning(_("Null external module directory name. Modules will not be loaded."));
188 return;
189 }
191 if (!Glib::file_test(std::string(dirname), Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
192 return;
193 }
195 //# Hopefully doing this the Glib way is portable
197 GError *err;
198 GDir *directory = g_dir_open(dirname, 0, &err);
199 if (!directory) {
200 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
201 g_warning(_("Modules directory (%s) is unavailable. External modules in that directory will not be loaded."), safeDir);
202 g_free(safeDir);
203 return;
204 }
206 gchar *filename;
207 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
209 if (strlen(filename) < strlen(SP_MODULE_EXTENSION)) {
210 continue;
211 }
213 if (strcmp(SP_MODULE_EXTENSION, filename + (strlen(filename) - strlen(SP_MODULE_EXTENSION)))) {
214 continue;
215 }
217 gchar *pathname = g_strdup_printf("%s/%s", dirname, filename);
218 build_from_file(pathname);
219 g_free(pathname);
220 }
222 g_dir_close(directory);
223 }
226 static void
227 check_extensions_internal(Extension *in_plug, gpointer in_data)
228 {
229 int *count = (int *)in_data;
231 if (in_plug == NULL) return;
232 if (!in_plug->deactivated() && !in_plug->check()) {
233 in_plug->deactivate();
234 (*count)++;
235 }
236 }
238 static void
239 check_extensions()
240 {
241 int count = 1;
242 bool anyfail = false;
243 // int pass = 0;
245 Inkscape::Extension::Extension::error_file_open();
246 while (count != 0) {
247 // printf("Check extensions pass %d\n", pass++);
248 count = 0;
249 db.foreach(check_extensions_internal, (gpointer)&count);
250 if (count != 0) anyfail = true;
251 }
252 Inkscape::Extension::Extension::error_file_close();
253 }
255 } } /* namespace Inkscape::Extension */
258 /*
259 Local Variables:
260 mode:c++
261 c-file-style:"stroustrup"
262 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
263 indent-tabs-mode:nil
264 fill-column:99
265 End:
266 */
267 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :