Code

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