Code

Implement warning of prior errors.
[inkscape.git] / src / inkscape.cpp
1 /** @file
2  * @brief Legacy interface to main application
3  */
4 /* Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   bulia byak <buliabyak@users.sf.net>
7  *
8  * Copyright (C) 1999-2005 authors
9  * g++ port Copyright (C) 2003 Nathan Hurst
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
19 #include <map>
20 #include "debug/simple-event.h"
21 #include "debug/event-tracker.h"
23 #ifndef WIN32
24 # define HAS_PROC_SELF_EXE  //to get path of executable
25 #else
27 // For now to get at is_os_wide().
28 # include "extension/internal/win32.h"
29 using Inkscape::Extension::Internal::PrintWin32;
31 #define _WIN32_IE 0x0400
32 //#define HAS_SHGetSpecialFolderPath
33 #define HAS_SHGetSpecialFolderLocation
34 #define HAS_GetModuleFileName
35 # include <shlobj.h>
36 #endif
38 #include <cstring>
39 #include <glib/gstdio.h>
40 #include <glib.h>
41 #include <glibmm/i18n.h>
42 #include <gtk/gtkmain.h>
43 #include <gtk/gtkmessagedialog.h>
44 #include <gtkmm/messagedialog.h>
45 #include <signal.h>
46 #include <string>
47 #include "application/application.h"
48 #include "application/editor.h"
49 #include "desktop.h"
50 #include "desktop-handles.h"
51 #include "dialogs/input.h"
52 #include "document.h"
53 #include "event-context.h"
54 #include "extension/db.h"
55 #include "extension/init.h"
56 #include "extension/output.h"
57 #include "extension/system.h"
58 #include "helper/sp-marshal.h"
59 #include "inkscape-private.h"
60 #include "io/sys.h"
61 #include "message-stack.h"
62 #include "preferences.h"
63 #include "selection.h"
64 #include "ui/dialog/debug.h"
65 #include "xml/repr.h"
67 static Inkscape::Application *inkscape = NULL;
69 /* Backbones of configuration xml data */
70 #include "menus-skeleton.h"
72 enum {
73     MODIFY_SELECTION, // global: one of selections modified
74     CHANGE_SELECTION, // global: one of selections changed
75     CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
76     SET_SELECTION, // global: one of selections set
77     SET_EVENTCONTEXT, // tool switched
78     ACTIVATE_DESKTOP, // some desktop got focus
79     DEACTIVATE_DESKTOP, // some desktop lost focus
80     SHUTDOWN_SIGNAL, // inkscape is quitting
81     DIALOGS_HIDE, // user pressed F12
82     DIALOGS_UNHIDE, // user pressed F12
83     EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
84                      // may not be reflected by a selection change and thus needs a separate signal
85     LAST_SIGNAL
86 };
88 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
91 /*################################
92 # FORWARD DECLARATIONS
93 ################################*/
95 gboolean inkscape_app_use_gui( Inkscape::Application const * app );
97 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
98 static void inkscape_init (SPObject *object);
99 static void inkscape_dispose (GObject *object);
101 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
102 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
104 struct Inkscape::Application {
105     GObject object;
106     Inkscape::XML::Document *menus;
107     std::map<SPDocument *, int> document_set;
108     GSList *desktops;
109     gchar *argv0;
110     gboolean dialogs_toggle;
111     gboolean use_gui;         // may want to consider a virtual function
112                               // for overriding things like the warning dlg's
113     guint mapalt;
114     guint trackalt;
115 };
117 struct Inkscape::ApplicationClass {
118     GObjectClass object_class;
120     /* Signals */
121     void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
122     void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
123     void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
124     void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
125     void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
126     void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
127     void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
128     void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
129     void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
130     void (* shut_down) (Inkscape::Application *inkscape);
131     void (* dialogs_hide) (Inkscape::Application *inkscape);
132     void (* dialogs_unhide) (Inkscape::Application *inkscape);
133     void (* external_change) (Inkscape::Application *inkscape);
134 };
136 static GObjectClass * parent_class;
137 static guint inkscape_signals[LAST_SIGNAL] = {0};
139 static void (* segv_handler) (int) = SIG_DFL;
140 static void (* abrt_handler) (int) = SIG_DFL;
141 static void (* fpe_handler)  (int) = SIG_DFL;
142 static void (* ill_handler)  (int) = SIG_DFL;
143 static void (* bus_handler)  (int) = SIG_DFL;
145 #define INKSCAPE_PROFILE_DIR "Inkscape"
146 #define INKSCAPE_LEGACY_PROFILE_DIR ".inkscape"
147 #define MENUS_FILE "menus.xml"
150 /**
151  *  Retrieves the GType for the Inkscape Application object.
152  */
153 GType
154 inkscape_get_type (void)
156     static GType type = 0;
157     if (!type) {
158         GTypeInfo info = {
159             sizeof (Inkscape::ApplicationClass),
160             NULL, NULL,
161             (GClassInitFunc) inkscape_class_init,
162             NULL, NULL,
163             sizeof (Inkscape::Application),
164             4,
165             (GInstanceInitFunc) inkscape_init,
166             NULL
167         };
168         type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
169     }
170     return type;
174 /**
175  *  Initializes the inkscape class, registering all of its signal handlers
176  *  and virtual functions
177  */
178 static void
179 inkscape_class_init (Inkscape::ApplicationClass * klass)
181     GObjectClass * object_class;
183     object_class = (GObjectClass *) klass;
185     parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
187     inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
188                                G_TYPE_FROM_CLASS (klass),
189                                G_SIGNAL_RUN_FIRST,
190                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
191                                NULL, NULL,
192                                sp_marshal_NONE__POINTER_UINT,
193                                G_TYPE_NONE, 2,
194                                G_TYPE_POINTER, G_TYPE_UINT);
195     inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
196                                G_TYPE_FROM_CLASS (klass),
197                                G_SIGNAL_RUN_FIRST,
198                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
199                                NULL, NULL,
200                                sp_marshal_NONE__POINTER,
201                                G_TYPE_NONE, 1,
202                                G_TYPE_POINTER);
203     inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
204                                G_TYPE_FROM_CLASS (klass),
205                                G_SIGNAL_RUN_FIRST,
206                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
207                                NULL, NULL,
208                                sp_marshal_NONE__POINTER,
209                                G_TYPE_NONE, 1,
210                                G_TYPE_POINTER);
211     inkscape_signals[SET_SELECTION] =    g_signal_new ("set_selection",
212                                G_TYPE_FROM_CLASS (klass),
213                                G_SIGNAL_RUN_FIRST,
214                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
215                                NULL, NULL,
216                                sp_marshal_NONE__POINTER,
217                                G_TYPE_NONE, 1,
218                                G_TYPE_POINTER);
219     inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
220                                G_TYPE_FROM_CLASS (klass),
221                                G_SIGNAL_RUN_FIRST,
222                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
223                                NULL, NULL,
224                                sp_marshal_NONE__POINTER,
225                                G_TYPE_NONE, 1,
226                                G_TYPE_POINTER);
227     inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
228                                G_TYPE_FROM_CLASS (klass),
229                                G_SIGNAL_RUN_FIRST,
230                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
231                                NULL, NULL,
232                                sp_marshal_NONE__POINTER,
233                                G_TYPE_NONE, 1,
234                                G_TYPE_POINTER);
235     inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
236                                G_TYPE_FROM_CLASS (klass),
237                                G_SIGNAL_RUN_FIRST,
238                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
239                                NULL, NULL,
240                                sp_marshal_NONE__POINTER,
241                                G_TYPE_NONE, 1,
242                                G_TYPE_POINTER);
243     inkscape_signals[SHUTDOWN_SIGNAL] =        g_signal_new ("shut_down",
244                                G_TYPE_FROM_CLASS (klass),
245                                G_SIGNAL_RUN_FIRST,
246                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
247                                NULL, NULL,
248                                g_cclosure_marshal_VOID__VOID,
249                                G_TYPE_NONE, 0);
250     inkscape_signals[DIALOGS_HIDE] =        g_signal_new ("dialogs_hide",
251                                G_TYPE_FROM_CLASS (klass),
252                                G_SIGNAL_RUN_FIRST,
253                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
254                                NULL, NULL,
255                                g_cclosure_marshal_VOID__VOID,
256                                G_TYPE_NONE, 0);
257     inkscape_signals[DIALOGS_UNHIDE] =        g_signal_new ("dialogs_unhide",
258                                G_TYPE_FROM_CLASS (klass),
259                                G_SIGNAL_RUN_FIRST,
260                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
261                                NULL, NULL,
262                                g_cclosure_marshal_VOID__VOID,
263                                G_TYPE_NONE, 0);
264     inkscape_signals[EXTERNAL_CHANGE] =   g_signal_new ("external_change",
265                                G_TYPE_FROM_CLASS (klass),
266                                G_SIGNAL_RUN_FIRST,
267                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
268                                NULL, NULL,
269                                g_cclosure_marshal_VOID__VOID,
270                                G_TYPE_NONE, 0);
272     object_class->dispose = inkscape_dispose;
274     klass->activate_desktop = inkscape_activate_desktop_private;
275     klass->deactivate_desktop = inkscape_deactivate_desktop_private;
278 #ifdef WIN32
279 typedef int uid_t;
280 #define getuid() 0
281 #endif
283 /**
284  * static gint inkscape_autosave(gpointer);
285  *
286  * Callback passed to g_timeout_add_seconds()
287  * Responsible for autosaving all open documents
288  */
289 static gint inkscape_autosave(gpointer)
291     if (inkscape->document_set.empty()) { // nothing to autosave
292         return TRUE;
293     }
294     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
296     // Use UID for separating autosave-documents between users if directory is multiuser
297     uid_t uid = getuid();
299     Glib::ustring autosave_dir;
300     {
301         Glib::ustring tmp = prefs->getString("/options/autosave/path");
302         if (!tmp.empty()) {
303             autosave_dir = tmp;
304         } else {
305             autosave_dir = Glib::get_tmp_dir();
306         }
307     }
309     GDir *autosave_dir_ptr = g_dir_open(autosave_dir.c_str(), 0, NULL);
310     if( !autosave_dir_ptr ){
311         g_warning("Cannot open autosave directory!");
312         return TRUE;
313     }
315     time_t sptime = time(NULL);
316     struct tm *sptm = localtime(&sptime);
317     gchar sptstr[256];
318     strftime(sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
320     gint autosave_max = prefs->getInt("/options/autosave/max", 10);
322     gint docnum = 0;
324     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosaving documents..."));
325     for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
326           iter != inkscape->document_set.end();
327           ++iter) {
329         SPDocument *doc = iter->first;
331         ++docnum;
333         Inkscape::XML::Node *repr = sp_document_repr_root(doc);
334         // g_debug("Document %d: \"%s\" %s", docnum, doc ? doc->name : "(null)", doc ? (doc->isModifiedSinceSave() ? "(dirty)" : "(clean)") : "(null)");
336         if (doc->isModifiedSinceSave()) {
337             gchar *oldest_autosave = 0;
338             const gchar  *filename = 0;
339             struct stat sb;
340             time_t min_time = 0;
341             gint count = 0;
343             // Look for previous autosaves
344             gchar* baseName = g_strdup_printf( "inkscape-autosave-%d", uid );
345             g_dir_rewind(autosave_dir_ptr);
346             while( (filename = g_dir_read_name(autosave_dir_ptr)) != NULL ){
347                 if ( strncmp(filename, baseName, strlen(baseName)) == 0 ){
348                     gchar* full_path = g_build_filename( autosave_dir.c_str(), filename, NULL );
349                     if ( g_stat(full_path, &sb) != -1 ) {
350                         if ( difftime(sb.st_ctime, min_time) < 0 || min_time == 0 ){
351                             min_time = sb.st_ctime;
352                             if ( oldest_autosave ) {
353                                 g_free(oldest_autosave);
354                             }
355                             oldest_autosave = g_strdup(full_path);
356                         }
357                         count ++;
358                     }
359                     g_free(full_path);
360                 }
361             }
363             // g_debug("%d previous autosaves exists. Max = %d", count, autosave_max);
365             // Have we reached the limit for number of autosaves?
366             if ( count >= autosave_max ){
367                 // Remove the oldest file
368                 if ( oldest_autosave ) {
369                     unlink(oldest_autosave);
370                 }
371             }
373             if ( oldest_autosave ) {
374                 g_free(oldest_autosave);
375                 oldest_autosave = 0;
376             }
379             // Set the filename we will actually save to
380             g_free(baseName);
381             baseName = g_strdup_printf("inkscape-autosave-%d-%s-%03d.svg", uid, sptstr, docnum);
382             gchar* full_path = g_build_filename(autosave_dir.c_str(), baseName, NULL);
383             g_free(baseName);
384             baseName = 0;
386             // g_debug("Filename: %s", full_path);
388             // Try to save the file
389             FILE *file = Inkscape::IO::fopen_utf8name(full_path, "w");
390             gchar *errortext = 0;
391             if (file) {
392                 try{
393                     sp_repr_save_stream(repr->document(), file, SP_SVG_NS_URI);
394                 } catch (Inkscape::Extension::Output::no_extension_found &e) {
395                     errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document."));
396                 } catch (Inkscape::Extension::Output::save_failed &e) {
397                     gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
398                     errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
399                     g_free(safeUri);
400                 }
401                 fclose(file);
402             }
403             else {
404                 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
405                 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
406                 g_free(safeUri);
407             }
409             if (errortext) {
410                 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, errortext);
411                 g_warning("%s", errortext);
412                 g_free(errortext);
413             }
415             g_free(full_path);
416         }
417     }
418     g_dir_close(autosave_dir_ptr);
420     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosave complete."));
422     return TRUE;
425 void inkscape_autosave_init()
427     static guint32 autosave_timeout_id = 0;
428     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
430     // Turn off any previously initiated timeouts
431     if ( autosave_timeout_id ) {
432         g_source_remove(autosave_timeout_id);
433         autosave_timeout_id = 0;
434     }
436     // g_debug("options.autosave.enable = %d", prefs->getBool("/options/autosave/enable", true));
437     // Is autosave enabled?
438     if (!prefs->getBool("/options/autosave/enable", true)){
439         autosave_timeout_id = 0;
440     } else {
441         // Turn on autosave
442         guint32 timeout = prefs->getInt("/options/autosave/interval", 10) * 60;
443         // g_debug("options.autosave.interval = %d", prefs->getInt("/options/autosave/interval", 10));
444 #if GLIB_CHECK_VERSION(2,14,0)
445         autosave_timeout_id = g_timeout_add_seconds(timeout, inkscape_autosave, NULL);
446 #else
447         autosave_timeout_id = g_timeout_add(timeout * 1000, inkscape_autosave, NULL);
448 #endif
449     }
453 static void
454 inkscape_init (SPObject * object)
456     if (!inkscape) {
457         inkscape = (Inkscape::Application *) object;
458     } else {
459         g_assert_not_reached ();
460     }
462     new (&inkscape->document_set) std::map<SPDocument *, int>();
464     inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
465     inkscape->desktops = NULL;
466     inkscape->dialogs_toggle = TRUE;
467     inkscape->mapalt = GDK_MOD1_MASK;
468     inkscape->trackalt = FALSE;
471 static void
472 inkscape_dispose (GObject *object)
474     Inkscape::Application *inkscape = (Inkscape::Application *) object;
476     g_assert (!inkscape->desktops);
478     Inkscape::Preferences::unload();
480     if (inkscape->menus) {
481         /* fixme: This is not the best place */
482         Inkscape::GC::release(inkscape->menus);
483         inkscape->menus = NULL;
484     }
486     inkscape->document_set.~map();
488     G_OBJECT_CLASS (parent_class)->dispose (object);
490     gtk_main_quit ();
494 void
495 inkscape_ref (void)
497     if (inkscape)
498         g_object_ref (G_OBJECT (inkscape));
502 void
503 inkscape_unref (void)
505     if (inkscape)
506         g_object_unref (G_OBJECT (inkscape));
509 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
510 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
511 guint
512 inkscape_mapalt() {
513     return inkscape->mapalt;
516 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
517 void inkscape_mapalt(guint maskvalue)
519     if(maskvalue<2 || maskvalue> 5 ){  /* MOD5 is the highest defined in gdktypes.h */
520         inkscape->mapalt=0;
521     }else{
522         inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
523     }
526 guint
527 inkscape_trackalt() {
528     return inkscape->trackalt;
531 void inkscape_trackalt(guint trackvalue)
533         inkscape->trackalt = trackvalue;
537 static void
538 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
540     desktop->set_active (true);
544 static void
545 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
547     desktop->set_active (false);
551 /* fixme: This is EVIL, and belongs to main after all */
553 #define SP_INDENT 8
556 static void
557 inkscape_crash_handler (int /*signum*/)
559     using Inkscape::Debug::SimpleEvent;
560     using Inkscape::Debug::EventTracker;
561     using Inkscape::Debug::Logger;
563     static gint recursion = FALSE;
565     /*
566      * reset all signal handlers: any further crashes should just be allowed
567      * to crash normally.
568      * */
569     signal (SIGSEGV, segv_handler );
570     signal (SIGABRT, abrt_handler );
571     signal (SIGFPE,  fpe_handler  );
572     signal (SIGILL,  ill_handler  );
573 #ifndef WIN32
574     signal (SIGBUS,  bus_handler  );
575 #endif
577     /* Stop bizarre loops */
578     if (recursion) {
579         abort ();
580     }
581     recursion = TRUE;
583     EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
584     tracker.set<SimpleEvent<> >("emergency-save");
586     fprintf(stderr, "\nEmergency save activated!\n");
588     time_t sptime = time (NULL);
589     struct tm *sptm = localtime (&sptime);
590     gchar sptstr[256];
591     strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
593     gint count = 0;
594     GSList *savednames = NULL;
595     GSList *failednames = NULL;
596     for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
597           iter != inkscape->document_set.end();
598           ++iter) {
599         SPDocument *doc = iter->first;
600         Inkscape::XML::Node *repr;
601         repr = sp_document_repr_root (doc);
602         if (doc->isModifiedSinceSave()) {
603             const gchar *docname, *d0, *d;
604             gchar n[64], c[1024];
605             FILE *file;
607             /* originally, the document name was retrieved from
608              * the sodipod:docname attribute */
609             docname = doc->name;
610             if (docname) {
611                 /* fixme: Quick hack to remove emergency file suffix */
612                 d0 = strrchr ((char*)docname, '.');
613                 if (d0 && (d0 > docname)) {
614                     d0 = strrchr ((char*)(d0 - 1), '.');
615                     if (d0 && (d0 > docname)) {
616                         d = d0;
617                         while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
618                         if (*d) {
619                             memcpy (n, docname, MIN (d0 - docname - 1, 64));
620                             n[63] = '\0';
621                             docname = n;
622                         }
623                     }
624                 }
625             }
627             if (!docname || !*docname) docname = "emergency";
628             // try saving to the profile location
629             g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
630             gchar * location = homedir_path(c);
631             Inkscape::IO::dump_fopen_call(location, "E");
632             file = Inkscape::IO::fopen_utf8name(location, "w");
633             g_free(location);
634             if (!file) {
635                 // try saving to /tmp
636                 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
637                 Inkscape::IO::dump_fopen_call(c, "G");
638                 file = Inkscape::IO::fopen_utf8name(c, "w");
639             }
640             if (!file) {
641                 // try saving to the current directory
642                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
643                 Inkscape::IO::dump_fopen_call(c, "F");
644                 file = Inkscape::IO::fopen_utf8name(c, "w");
645             }
646             if (file) {
647                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
648                 savednames = g_slist_prepend (savednames, g_strdup (c));
649                 fclose (file);
650             } else {
651                 failednames = g_slist_prepend (failednames, (doc->name) ? g_strdup (doc->name) : g_strdup (_("Untitled document")));
652             }
653             count++;
654         }
655     }
657     savednames = g_slist_reverse (savednames);
658     failednames = g_slist_reverse (failednames);
659     if (savednames) {
660         fprintf (stderr, "\nEmergency save document locations:\n");
661         for (GSList *l = savednames; l != NULL; l = l->next) {
662             fprintf (stderr, "  %s\n", (gchar *) l->data);
663         }
664     }
665     if (failednames) {
666         fprintf (stderr, "\nFailed to do emergency save for documents:\n");
667         for (GSList *l = failednames; l != NULL; l = l->next) {
668             fprintf (stderr, "  %s\n", (gchar *) l->data);
669         }
670     }
672     // do not save the preferences since they can be in a corrupted state
673     Inkscape::Preferences::unload(false);
675     fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
676     fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
677     fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
679     /* Show nice dialog box */
681     char const *istr = _("Inkscape encountered an internal error and will close now.\n");
682     char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
683     char const *fstr = _("Automatic backup of the following documents failed:\n");
684     gint nllen = strlen ("\n");
685     gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
686     for (GSList *l = savednames; l != NULL; l = l->next) {
687         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
688     }
689     for (GSList *l = failednames; l != NULL; l = l->next) {
690         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
691     }
692     len += 1;
693     gchar *b = g_new (gchar, len);
694     gint pos = 0;
695     len = strlen (istr);
696     memcpy (b + pos, istr, len);
697     pos += len;
698     if (savednames) {
699         len = strlen (sstr);
700         memcpy (b + pos, sstr, len);
701         pos += len;
702         for (GSList *l = savednames; l != NULL; l = l->next) {
703             memset (b + pos, ' ', SP_INDENT);
704             pos += SP_INDENT;
705             len = strlen ((gchar *) l->data);
706             memcpy (b + pos, l->data, len);
707             pos += len;
708             memcpy (b + pos, "\n", nllen);
709             pos += nllen;
710         }
711     }
712     if (failednames) {
713         len = strlen (fstr);
714         memcpy (b + pos, fstr, len);
715         pos += len;
716         for (GSList *l = failednames; l != NULL; l = l->next) {
717             memset (b + pos, ' ', SP_INDENT);
718             pos += SP_INDENT;
719             len = strlen ((gchar *) l->data);
720             memcpy (b + pos, l->data, len);
721             pos += len;
722             memcpy (b + pos, "\n", nllen);
723             pos += nllen;
724         }
725     }
726     *(b + pos) = '\0';
728     if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
729         GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
730         gtk_dialog_run (GTK_DIALOG (msgbox));
731         gtk_widget_destroy (msgbox);
732     }
733     else
734     {
735         g_message( "Error: %s", b );
736     }
737     g_free (b);
739     tracker.clear();
740     Logger::shutdown();
742     /* on exit, allow restored signal handler to take over and crash us */
746 class InkErrorHandler : public Inkscape::ErrorReporter {
747 public:
748     InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(),
749                                    _useGui(useGui)
750     {}
751     virtual ~InkErrorHandler() {}
753     virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const
754     {
755         if (_useGui) {
756             Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
757             err.set_secondary_text(secondary);
758             err.run();
759         } else {
760             g_message("%s", primary.data());
761             g_message("%s", secondary.data());
762         }
763     }
765 private:
766     bool _useGui;
767 };
769 void
770 inkscape_application_init (const gchar *argv0, gboolean use_gui)
772     inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
773     /* fixme: load application defaults */
775     segv_handler = signal (SIGSEGV, inkscape_crash_handler);
776     abrt_handler = signal (SIGABRT, inkscape_crash_handler);
777     fpe_handler  = signal (SIGFPE,  inkscape_crash_handler);
778     ill_handler  = signal (SIGILL,  inkscape_crash_handler);
779 #ifndef WIN32
780     bus_handler  = signal (SIGBUS,  inkscape_crash_handler);
781 #endif
783     inkscape->use_gui = use_gui;
784     inkscape->argv0 = g_strdup(argv0);
786     /* Load the preferences and menus */
787     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
788     InkErrorHandler* handler = new InkErrorHandler(use_gui);
789     prefs->setErrorHandler(handler);
790     {
791         Glib::ustring msg;
792         Glib::ustring secondary;
793         if (prefs->getLastError( msg, secondary )) {
794             handler->handleError(msg, secondary);
795         }
796     }
798     inkscape_load_menus(inkscape);
799     sp_input_load_from_preferences();
800     
801     /* set language for user interface according setting in preferences */
802     Glib::ustring ui_language = prefs->getString("/ui/language");
803     if(!ui_language.empty())
804     {
805         setenv("LANGUAGE", ui_language, 1);
806     }
808     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
809      * Use only if use_gui is enabled
810      */
811 #ifdef WIN32
812 #define DEFAULT_LOG_REDIRECT true
813 #else
814 #define DEFAULT_LOG_REDIRECT false
815 #endif
817     if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
818     {
819         Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
820     }
822     /* Check for global remapping of Alt key */
823     if (use_gui)
824     {
825         inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
826         inkscape_trackalt(guint(prefs->getInt("/options/trackalt/value", 0)));
827     }
829     /* Initialize the extensions */
830     Inkscape::Extension::init();
832     inkscape_autosave_init();
834     return;
837 /**
838  *  Returns the current Inkscape::Application global object
839  */
840 Inkscape::Application *
841 inkscape_get_instance()
843         return inkscape;
846 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
848     return app->use_gui;
851 /**
852  *  Menus management
853  *
854  */
855 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
857     gchar *fn = profile_path(MENUS_FILE);
858     gchar *menus_xml = NULL; gsize len = 0;
860     if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
861         // load the menus_xml file
862         INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
863         g_free(menus_xml);
864         if (INKSCAPE->menus) return true;
865     }
866     INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
867     if (INKSCAPE->menus) return true;
868     return false;
872 void
873 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
875     if (Inkscape::NSApplication::Application::getNewGui()) {
876         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
877         return;
878     }
879     g_return_if_fail (selection != NULL);
881     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
882         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
883     }
887 void
888 inkscape_selection_changed (Inkscape::Selection * selection)
890     if (Inkscape::NSApplication::Application::getNewGui()) {
891         Inkscape::NSApplication::Editor::selectionChanged (selection);
892         return;
893     }
894     g_return_if_fail (selection != NULL);
896     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
897         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
898     }
901 void
902 inkscape_subselection_changed (SPDesktop *desktop)
904     if (Inkscape::NSApplication::Application::getNewGui()) {
905         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
906         return;
907     }
908     g_return_if_fail (desktop != NULL);
910     if (DESKTOP_IS_ACTIVE (desktop)) {
911         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
912     }
916 void
917 inkscape_selection_set (Inkscape::Selection * selection)
919     if (Inkscape::NSApplication::Application::getNewGui()) {
920         Inkscape::NSApplication::Editor::selectionSet (selection);
921         return;
922     }
923     g_return_if_fail (selection != NULL);
925     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
926         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
927         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
928     }
932 void
933 inkscape_eventcontext_set (SPEventContext * eventcontext)
935     if (Inkscape::NSApplication::Application::getNewGui()) {
936         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
937         return;
938     }
939     g_return_if_fail (eventcontext != NULL);
940     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
942     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
943         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
944     }
948 void
949 inkscape_add_desktop (SPDesktop * desktop)
951     g_return_if_fail (desktop != NULL);
953     if (Inkscape::NSApplication::Application::getNewGui())
954     {
955         Inkscape::NSApplication::Editor::addDesktop (desktop);
956         return;
957     }
958     g_return_if_fail (inkscape != NULL);
960     g_assert (!g_slist_find (inkscape->desktops, desktop));
962     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
964     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
965     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
966     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
967     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
972 void
973 inkscape_remove_desktop (SPDesktop * desktop)
975     g_return_if_fail (desktop != NULL);
976     if (Inkscape::NSApplication::Application::getNewGui())
977     {
978         Inkscape::NSApplication::Editor::removeDesktop (desktop);
979         return;
980     }
981     g_return_if_fail (inkscape != NULL);
983     g_assert (g_slist_find (inkscape->desktops, desktop));
985     if (DESKTOP_IS_ACTIVE (desktop)) {
986         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
987         if (inkscape->desktops->next != NULL) {
988             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
989             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
990             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
991             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
992             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
993             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
994             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
995         } else {
996             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
997             if (sp_desktop_selection(desktop))
998                 sp_desktop_selection(desktop)->clear();
999         }
1000     }
1002     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1004     // if this was the last desktop, shut down the program
1005     if (inkscape->desktops == NULL) {
1006         inkscape_exit (inkscape);
1007     }
1012 void
1013 inkscape_activate_desktop (SPDesktop * desktop)
1015     g_return_if_fail (desktop != NULL);
1016     if (Inkscape::NSApplication::Application::getNewGui())
1017     {
1018         Inkscape::NSApplication::Editor::activateDesktop (desktop);
1019         return;
1020     }
1021     g_return_if_fail (inkscape != NULL);
1023     if (DESKTOP_IS_ACTIVE (desktop)) {
1024         return;
1025     }
1027     g_assert (g_slist_find (inkscape->desktops, desktop));
1029     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
1031     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
1033     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1034     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
1036     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1037     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
1038     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
1039     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
1043 /**
1044  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
1045  */
1046 void
1047 inkscape_reactivate_desktop (SPDesktop * desktop)
1049     g_return_if_fail (desktop != NULL);
1050     if (Inkscape::NSApplication::Application::getNewGui())
1051     {
1052         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1053         return;
1054     }
1055     g_return_if_fail (inkscape != NULL);
1057     if (DESKTOP_IS_ACTIVE (desktop))
1058         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1063 SPDesktop *
1064 inkscape_find_desktop_by_dkey (unsigned int dkey)
1066     for (GSList *r = inkscape->desktops; r; r = r->next) {
1067         if (((SPDesktop *) r->data)->dkey == dkey)
1068             return ((SPDesktop *) r->data);
1069     }
1070     return NULL;
1076 unsigned int
1077 inkscape_maximum_dkey()
1079     unsigned int dkey = 0;
1081     for (GSList *r = inkscape->desktops; r; r = r->next) {
1082         if (((SPDesktop *) r->data)->dkey > dkey)
1083             dkey = ((SPDesktop *) r->data)->dkey;
1084     }
1086     return dkey;
1091 SPDesktop *
1092 inkscape_next_desktop ()
1094     SPDesktop *d = NULL;
1095     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1097     if (dkey_current < inkscape_maximum_dkey()) {
1098         // find next existing
1099         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1100             d = inkscape_find_desktop_by_dkey (i);
1101             if (d) {
1102                 break;
1103             }
1104         }
1105     } else {
1106         // find first existing
1107         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1108             d = inkscape_find_desktop_by_dkey (i);
1109             if (d) {
1110                 break;
1111             }
1112         }
1113     }
1115     g_assert (d);
1117     return d;
1122 SPDesktop *
1123 inkscape_prev_desktop ()
1125     SPDesktop *d = NULL;
1126     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1128     if (dkey_current > 0) {
1129         // find prev existing
1130         for (signed int i = dkey_current - 1; i >= 0; i--) {
1131             d = inkscape_find_desktop_by_dkey (i);
1132             if (d) {
1133                 break;
1134             }
1135         }
1136     }
1137     if (!d) {
1138         // find last existing
1139         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1140     }
1142     g_assert (d);
1144     return d;
1149 void
1150 inkscape_switch_desktops_next ()
1152     inkscape_next_desktop()->presentWindow();
1157 void
1158 inkscape_switch_desktops_prev ()
1160     inkscape_prev_desktop()->presentWindow();
1165 void
1166 inkscape_dialogs_hide ()
1168     if (Inkscape::NSApplication::Application::getNewGui())
1169         Inkscape::NSApplication::Editor::hideDialogs();
1170     else
1171     {
1172         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1173         inkscape->dialogs_toggle = FALSE;
1174     }
1179 void
1180 inkscape_dialogs_unhide ()
1182     if (Inkscape::NSApplication::Application::getNewGui())
1183         Inkscape::NSApplication::Editor::unhideDialogs();
1184     else
1185     {
1186         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1187         inkscape->dialogs_toggle = TRUE;
1188     }
1193 void
1194 inkscape_dialogs_toggle ()
1196     if (inkscape->dialogs_toggle) {
1197         inkscape_dialogs_hide ();
1198     } else {
1199         inkscape_dialogs_unhide ();
1200     }
1203 void
1204 inkscape_external_change ()
1206     g_return_if_fail (inkscape != NULL);
1208     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1211 /**
1212  * fixme: These need probably signals too
1213  */
1214 void
1215 inkscape_add_document (SPDocument *document)
1217     g_return_if_fail (document != NULL);
1219     if (!Inkscape::NSApplication::Application::getNewGui())
1220     {
1221         // try to insert the pair into the list
1222         if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1223             //insert failed, this key (document) is already in the list
1224             for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1225                    iter != inkscape->document_set.end();
1226                    ++iter) {
1227                 if (iter->first == document) {
1228                     // found this document in list, increase its count
1229                     iter->second ++;
1230                 }
1231            }
1232         }
1233     }
1234     else
1235     {
1236         Inkscape::NSApplication::Editor::addDocument (document);
1237     }
1241 // returns true if this was last reference to this document, so you can delete it
1242 bool
1243 inkscape_remove_document (SPDocument *document)
1245     g_return_val_if_fail (document != NULL, false);
1247     if (!Inkscape::NSApplication::Application::getNewGui())
1248     {
1249         for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1250                   iter != inkscape->document_set.end();
1251                   ++iter) {
1252             if (iter->first == document) {
1253                 // found this document in list, decrease its count
1254                 iter->second --;
1255                 if (iter->second < 1) {
1256                     // this was the last one, remove the pair from list
1257                     inkscape->document_set.erase (iter);
1258                     return true;
1259                 } else {
1260                     return false;
1261                 }
1262             }
1263         }
1264     }
1265     else
1266     {
1267         Inkscape::NSApplication::Editor::removeDocument (document);
1268     }
1270     return false;
1273 SPDesktop *
1274 inkscape_active_desktop (void)
1276     if (Inkscape::NSApplication::Application::getNewGui())
1277         return Inkscape::NSApplication::Editor::getActiveDesktop();
1279     if (inkscape->desktops == NULL) {
1280         return NULL;
1281     }
1283     return (SPDesktop *) inkscape->desktops->data;
1286 SPDocument *
1287 inkscape_active_document (void)
1289     if (Inkscape::NSApplication::Application::getNewGui())
1290         return Inkscape::NSApplication::Editor::getActiveDocument();
1292     if (SP_ACTIVE_DESKTOP) {
1293         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1294     }
1296     return NULL;
1299 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1300     SPDocument const* document = desktop.doc();
1301     if (!document) {
1302         return false;
1303     }
1304     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1305         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1306         SPDocument *other_document=other_desktop->doc();
1307         if ( other_document == document && other_desktop != &desktop ) {
1308             return false;
1309         }
1310     }
1311     return true;
1314 SPEventContext *
1315 inkscape_active_event_context (void)
1317     if (SP_ACTIVE_DESKTOP) {
1318         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1319     }
1321     return NULL;
1326 /*#####################
1327 # HELPERS
1328 #####################*/
1330 void
1331 inkscape_refresh_display (Inkscape::Application *inkscape)
1333     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1334         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1335     }
1339 /**
1340  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1341  *  saves the preferences if appropriate, and quits.
1342  */
1343 void
1344 inkscape_exit (Inkscape::Application */*inkscape*/)
1346     g_assert (INKSCAPE);
1348     //emit shutdown signal so that dialogs could remember layout
1349     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1351     Inkscape::Preferences::unload();
1352     gtk_main_quit ();
1355 char *
1356 homedir_path(const char *filename)
1358     static const gchar *homedir = NULL;
1359     if (!homedir) {
1360         homedir = g_get_home_dir();
1361     }
1362     if (!homedir) {
1363         homedir = g_path_get_dirname(INKSCAPE->argv0);
1364     }
1365     return g_build_filename(homedir, filename, NULL);
1369 /**
1370  * Get, or guess, or decide the location where the preferences.xml
1371  * file should be located.
1372  */
1373 gchar *
1374 profile_path(const char *filename)
1376     static const gchar *prefdir = NULL;
1377     if (!prefdir) {
1378 #ifdef HAS_SHGetSpecialFolderLocation
1379         // prefer c:\Documents and Settings\UserName\Application Data\ to
1380         // c:\Documents and Settings\userName\;
1381         if (!prefdir) {
1382             ITEMIDLIST *pidl = 0;
1383             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1384                 gchar * utf8Path = NULL;
1386                 if ( PrintWin32::is_os_wide() ) {
1387                     wchar_t pathBuf[MAX_PATH+1];
1388                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1390                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1391                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1392                     }
1393                 } else {
1394                     char pathBuf[MAX_PATH+1];
1396                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1397                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1398                     }
1399                 }
1401                 if ( utf8Path ) {
1402                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1403                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1404                         g_free( utf8Path );
1405                         utf8Path = 0;
1406                     } else {
1407                         prefdir = utf8Path;
1408                     }
1409                 }
1412                 /* not compiling yet...
1414                 // Remember to free the list pointer
1415                 IMalloc * imalloc = 0;
1416                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1417                     imalloc->lpVtbl->Free( imalloc, pidl );
1418                     imalloc->lpVtbl->Release( imalloc );
1419                 }
1420                 */
1421             }
1423             if (prefdir) {
1424                 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1425             }
1426         }
1427 #endif
1428         if (!prefdir) {
1429             prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1430             gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1432             // TODO here is a point to hook in preference migration
1434             if ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) ) {
1435                 prefdir = legacyDir;
1436             } else {
1437                 g_free(legacyDir);
1438                 legacyDir = 0;
1439             }
1440         }
1441     }
1442     return g_build_filename(prefdir, filename, NULL);
1445 Inkscape::XML::Node *
1446 inkscape_get_menus (Inkscape::Application * inkscape)
1448     Inkscape::XML::Node *repr = inkscape->menus->root();
1449     g_assert (!(strcmp (repr->name(), "inkscape")));
1450     return repr->firstChild();
1453 void
1454 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1456     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1457         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1458     }
1461 /*
1462   Local Variables:
1463   mode:c++
1464   c-file-style:"stroustrup"
1465   c-file-offsets:((innamespace . 0)(inline-open . 0))
1466   indent-tabs-mode:nil
1467   fill-column:99
1468   End:
1469 */
1470 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :