Code

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