Code

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