Code

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