Code

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