Code

From trunk
[inkscape.git] / src / extension / init.cpp
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/ustring.h>
23 #include <glibmm/i18n.h>
25 #include "system.h"
26 #include "db.h"
27 #include "internal/svgz.h"
28 #include "internal/ps.h"
29 #ifdef HAVE_CAIRO_PDF
30 # include "internal/pdf-cairo.h"
31 #endif
32 #ifdef WIN32
33 # include "internal/win32.h"
34 # include "internal/emf-win32-inout.h"
35 # include "internal/emf-win32-print.h"
36 #endif
37 #include "internal/ps-out.h"
38 #ifdef HAVE_CAIRO_PDF
39 # include "internal/cairo-pdf-out.h"
40 # include "internal/cairo-renderer-pdf-out.h"
41 # include "internal/cairo-png-out.h"
42 # include "internal/cairo-ps-out.h"
43 #endif
44 #ifdef HAVE_POPPLER
45 # include "internal/pdfinput/pdf-input.h"
46 #endif
47 #ifdef HAVE_POPPLER_GLIB
48 # include "internal/pdf-input-cairo.h"
49 #endif
50 #include "internal/pov-out.h"
51 #include "internal/javafx-out.h"
52 #include "internal/odf.h"
53 #include "internal/latex-pstricks-out.h"
54 #include "internal/latex-pstricks.h"
55 #include "internal/eps-out.h"
56 #include "internal/gdkpixbuf-input.h"
57 #include "internal/bluredge.h"
58 #include "internal/gimpgrad.h"
59 #include "internal/grid.h"
60 #ifdef WITH_LIBWPG
61 #include "internal/wpg-input.h"
62 #endif
63 #include "preferences.h"
64 #include "io/sys.h"
66 #ifdef WITH_IMAGE_MAGICK
67 #include "internal/bitmap/adaptiveThreshold.h"
68 #include "internal/bitmap/addNoise.h"
69 #include "internal/bitmap/blur.h"
70 #include "internal/bitmap/channel.h"
71 #include "internal/bitmap/charcoal.h"
72 #include "internal/bitmap/colorize.h"
73 #include "internal/bitmap/contrast.h"
74 #include "internal/bitmap/cycleColormap.h"
75 #include "internal/bitmap/despeckle.h"
76 #include "internal/bitmap/edge.h"
77 #include "internal/bitmap/emboss.h"
78 #include "internal/bitmap/enhance.h"
79 #include "internal/bitmap/equalize.h"
80 #include "internal/bitmap/gaussianBlur.h"
81 #include "internal/bitmap/implode.h"
82 #include "internal/bitmap/level.h"
83 #include "internal/bitmap/levelChannel.h"
84 #include "internal/bitmap/medianFilter.h"
85 #include "internal/bitmap/modulate.h"
86 #include "internal/bitmap/negate.h"
87 #include "internal/bitmap/normalize.h"
88 #include "internal/bitmap/oilPaint.h"
89 #include "internal/bitmap/opacity.h"
90 #include "internal/bitmap/raise.h"
91 #include "internal/bitmap/reduceNoise.h"
92 #include "internal/bitmap/sample.h"
93 #include "internal/bitmap/shade.h"
94 #include "internal/bitmap/sharpen.h"
95 #include "internal/bitmap/solarize.h"
96 #include "internal/bitmap/spread.h"
97 #include "internal/bitmap/swirl.h"
98 //#include "internal/bitmap/threshold.h"
99 #include "internal/bitmap/unsharpmask.h"
100 #include "internal/bitmap/wave.h"
101 #endif /* WITH_IMAGE_MAGICK */
103 #include "internal/filter/filter.h"
105 extern gboolean inkscape_app_use_gui( Inkscape::Application const *app );
107 namespace Inkscape {
108 namespace Extension {
110 /** This is the extention that all files are that are pulled from
111     the extension directory and parsed */
112 #define SP_MODULE_EXTENSION  "inx"
114 static void build_module_from_dir(gchar const *dirname);
115 static void check_extensions();
117 /**
118  * \return    none
119  * \brief     Examines the given string preference and checks to see
120  *            that at least one of the registered extensions matches
121  *            it.  If not, a default is assigned.
122  * \param     pref_path        Preference path to update
123  * \param     pref_default     Default string to set
124  * \param     extension_family List of extensions to search
125  */
126 static void
127 update_pref(Glib::ustring const &pref_path,
128             gchar const *pref_default) // , GSList *extension_family)
130     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
131     Glib::ustring pref = prefs->getString(pref_path);
132     /*
133     gboolean missing=TRUE;
134     for (GSList *list = extension_family; list; list = g_slist_next(list)) {
135         g_assert( list->data );
137         Inkscape::Extension *extension;
138         extension = reinterpret_cast<Inkscape::Extension *>(list->data);
140         if (!strcmp(extension->get_id(),pref)) missing=FALSE;
141     }
142     */
143     if (!Inkscape::Extension::db.get( pref.data() ) /*missing*/) {
144         prefs->setString(pref_path, pref_default);
145     }
148 /**
149  * Invokes the init routines for internal modules.
150  *
151  * This should be a list of all the internal modules that need to initialized.  This is just a
152  * convinent place to put them.  Also, this function calls build_module_from_dir to parse the
153  * Inkscape extensions directory.
154  */
155 void
156 init()
158     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
159     
160     /* TODO: Change to Internal */
161     Internal::Svg::init();
162     Internal::Svgz::init();
163     //Internal::PsOutput::init(); // disabled, to be deleted, replaced by CairoPsOutput
164     //Internal::EpsOutput::init(); // disabled, to be deleted, replaced by CairoEpsOutput
165     Internal::PrintPS::init();
166 #ifdef HAVE_CAIRO_PDF
167     if (prefs->getBool("/options/useoldpdfexporter/value")) {
168     //g_print ("Using CairoPdfOutput: old pdf exporter\n");
169     Internal::CairoPdfOutput::init();
170     Internal::PrintCairoPDF::init();
171     } else {
172     //g_print ("Using CairoRendererPdfOutput: new pdf exporter\n");
173     Internal::CairoRendererPdfOutput::init();
174     Internal::CairoRendererOutput::init();
175     }
176     Internal::CairoPsOutput::init();
177     Internal::CairoEpsOutput::init();
178 #endif
179 #ifdef HAVE_POPPLER
180     Internal::PdfInput::init();
181 #endif
182 #ifdef HAVE_POPPLER_GLIB
183     if (0) {
184     Internal::PdfInputCairo::init();
185     }
186 #endif
187 #ifdef WIN32
188     Internal::PrintWin32::init();
189     Internal::PrintEmfWin32::init();
190     Internal::EmfWin32::init();
191 #endif
192     Internal::PovOutput::init();
193     Internal::JavaFXOutput::init();
194     Internal::OdfOutput::init();
195     Internal::PrintLatex::init();
196     Internal::LatexOutput::init();
197 #ifdef WITH_LIBWPG
198     Internal::WpgInput::init();
199 #endif
201     /* Effects */
202     Internal::BlurEdge::init();
203     Internal::GimpGrad::init();
204     Internal::Grid::init();
205         
206     /* Raster Effects */
207 #ifdef WITH_IMAGE_MAGICK
208     Internal::Bitmap::AdaptiveThreshold::init();
209     Internal::Bitmap::AddNoise::init();
210     Internal::Bitmap::Blur::init();
211     Internal::Bitmap::Channel::init();
212     Internal::Bitmap::Charcoal::init();
213     Internal::Bitmap::Colorize::init();
214     Internal::Bitmap::Contrast::init();
215     Internal::Bitmap::CycleColormap::init();
216     Internal::Bitmap::Edge::init();
217     Internal::Bitmap::Despeckle::init();
218     Internal::Bitmap::Emboss::init();
219     Internal::Bitmap::Enhance::init();
220     Internal::Bitmap::Equalize::init();
221     Internal::Bitmap::GaussianBlur::init();
222     Internal::Bitmap::Implode::init();
223     Internal::Bitmap::Level::init();
224     Internal::Bitmap::LevelChannel::init();
225     Internal::Bitmap::MedianFilter::init();
226     Internal::Bitmap::Modulate::init();
227     Internal::Bitmap::Negate::init();
228     Internal::Bitmap::Normalize::init();
229     Internal::Bitmap::OilPaint::init();
230     Internal::Bitmap::Opacity::init();
231     Internal::Bitmap::Raise::init();
232     Internal::Bitmap::ReduceNoise::init();
233     Internal::Bitmap::Sample::init();
234     Internal::Bitmap::Shade::init();
235     Internal::Bitmap::Sharpen::init();
236     Internal::Bitmap::Solarize::init();
237     Internal::Bitmap::Spread::init();
238     Internal::Bitmap::Swirl::init();
239     //Internal::Bitmap::Threshold::init();
240     Internal::Bitmap::Unsharpmask::init();
241     Internal::Bitmap::Wave::init();
242 #endif /* WITH_IMAGE_MAGICK */
244         Internal::Filter::Filter::filters_all();
246     /* Load search path for extensions */
247     if (Inkscape::Extension::Extension::search_path.size() == 0)
248     {
249         Inkscape::Extension::Extension::search_path.push_back(profile_path("extensions"));
250         
251         Inkscape::Extension::Extension::search_path.push_back(g_strdup(INKSCAPE_EXTENSIONDIR));
253     }
255     for (unsigned int i=0; i<Inkscape::Extension::Extension::search_path.size(); i++) {
256         build_module_from_dir(Inkscape::Extension::Extension::search_path[i]);
257     }
259     /* this is at the very end because it has several catch-alls
260      * that are possibly over-ridden by other extensions (such as
261      * svgz)
262      */
263     Internal::GdkpixbufInput::init();
265     /* now we need to check and make sure everyone is happy */
266     check_extensions();
268     /* This is a hack to deal with updating saved outdated module
269      * names in the prefs...
270      */
271     update_pref("/dialogs/save_as/default",
272                 SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE
273                 // Inkscape::Extension::db.get_output_list()
274         );
277 /**
278  * \return    none
279  * \brief     This function parses a directory for files of SP_MODULE_EXTENSION
280  *            type and loads them.
281  * \param     dirname  The directory that should be searched for modules
282  *
283  * Here is just a basic function that moves through a directory.  It looks at every entry, and
284  * compares its filename with SP_MODULE_EXTENSION.  Of those that pass, build_from_file is called
285  * with their filenames.
286  */
287 static void
288 build_module_from_dir(gchar const *dirname)
290     if (!dirname) {
291         g_warning(_("Null external module directory name.  Modules will not be loaded."));
292         return;
293     }
295     if (!Glib::file_test(std::string(dirname), Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
296         return;
297     }
299     //# Hopefully doing this the Glib way is portable
301     GError *err;
302     GDir *directory = g_dir_open(dirname, 0, &err);
303     if (!directory) {
304         gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
305         g_warning(_("Modules directory (%s) is unavailable.  External modules in that directory will not be loaded."), safeDir);
306         g_free(safeDir);
307         return;
308     }
310     gchar *filename;
311     while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
312         if (strlen(filename) < strlen(SP_MODULE_EXTENSION)) {
313             continue;
314         }
316         if (strcmp(SP_MODULE_EXTENSION, filename + (strlen(filename) - strlen(SP_MODULE_EXTENSION)))) {
317             continue;
318         }
320         gchar *pathname = g_build_filename(dirname, filename, NULL);
321         build_from_file(pathname);
322         g_free(pathname);
323     }
325     g_dir_close(directory);
327         return;
331 static void
332 check_extensions_internal(Extension *in_plug, gpointer in_data)
334     int *count = (int *)in_data;
336     if (in_plug == NULL) return;
337     if (!in_plug->deactivated() && !in_plug->check()) {
338          in_plug->deactivate();
339         (*count)++;
340     }
343 static void
344 check_extensions()
346     int count = 1;
347     bool anyfail = false;
348     // int pass = 0;
350     Inkscape::Extension::Extension::error_file_open();
351     while (count != 0) {
352         count = 0;
353         db.foreach(check_extensions_internal, (gpointer)&count);
354         if (count != 0) anyfail = true;
355     }
356     Inkscape::Extension::Extension::error_file_close();
359 } } /* namespace Inkscape::Extension */
362 /*
363   Local Variables:
364   mode:c++
365   c-file-style:"stroustrup"
366   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
367   indent-tabs-mode:nil
368   fill-column:99
369   End:
370 */
371 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :