Code

Tentative fix for LP bug #168922: don't save prefs n crash handler
[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 <signal.h>
45 #include <string>
46 #include "application/application.h"
47 #include "application/editor.h"
48 #include "desktop.h"
49 #include "desktop-handles.h"
50 #include "dialogs/input.h"
51 #include "document.h"
52 #include "event-context.h"
53 #include "extension/db.h"
54 #include "extension/init.h"
55 #include "extension/output.h"
56 #include "extension/system.h"
57 #include "helper/sp-marshal.h"
58 #include "inkscape-private.h"
59 #include "io/sys.h"
60 #include "message-stack.h"
61 #include "preferences.h"
62 #include "selection.h"
63 #include "ui/dialog/debug.h"
64 #include "xml/repr.h"
66 static Inkscape::Application *inkscape = NULL;
68 /* Backbones of configuration xml data */
69 #include "menus-skeleton.h"
71 enum {
72     MODIFY_SELECTION, // global: one of selections modified
73     CHANGE_SELECTION, // global: one of selections changed
74     CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
75     SET_SELECTION, // global: one of selections set
76     SET_EVENTCONTEXT, // tool switched
77     ACTIVATE_DESKTOP, // some desktop got focus
78     DEACTIVATE_DESKTOP, // some desktop lost focus
79     SHUTDOWN_SIGNAL, // inkscape is quitting
80     DIALOGS_HIDE, // user pressed F12
81     DIALOGS_UNHIDE, // user pressed F12
82     EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
83                      // may not be reflected by a selection change and thus needs a separate signal
84     LAST_SIGNAL
85 };
87 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
90 /*################################
91 # FORWARD DECLARATIONS
92 ################################*/
94 gboolean inkscape_app_use_gui( Inkscape::Application const * app );
96 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
97 static void inkscape_init (SPObject *object);
98 static void inkscape_dispose (GObject *object);
100 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
101 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
103 struct Inkscape::Application {
104     GObject object;
105     Inkscape::XML::Document *menus;
106     std::map<SPDocument *, int> document_set;
107     GSList *desktops;
108     gchar *argv0;
109     gboolean dialogs_toggle;
110     gboolean use_gui;         // may want to consider a virtual function
111                               // for overriding things like the warning dlg's
112     guint mapalt;
113 };
115 struct Inkscape::ApplicationClass {
116     GObjectClass object_class;
118     /* Signals */
119     void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
120     void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
121     void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
122     void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
123     void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
124     void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
125     void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
126     void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
127     void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
128     void (* shut_down) (Inkscape::Application *inkscape);
129     void (* dialogs_hide) (Inkscape::Application *inkscape);
130     void (* dialogs_unhide) (Inkscape::Application *inkscape);
131     void (* external_change) (Inkscape::Application *inkscape);
132 };
134 static GObjectClass * parent_class;
135 static guint inkscape_signals[LAST_SIGNAL] = {0};
137 static void (* segv_handler) (int) = SIG_DFL;
138 static void (* abrt_handler) (int) = SIG_DFL;
139 static void (* fpe_handler)  (int) = SIG_DFL;
140 static void (* ill_handler)  (int) = SIG_DFL;
141 static void (* bus_handler)  (int) = SIG_DFL;
143 #define INKSCAPE_PROFILE_DIR "Inkscape"
144 #define INKSCAPE_LEGACY_PROFILE_DIR ".inkscape"
145 #define MENUS_FILE "menus.xml"
148 /**
149  *  Retrieves the GType for the Inkscape Application object.
150  */
151 GType
152 inkscape_get_type (void)
154     static GType type = 0;
155     if (!type) {
156         GTypeInfo info = {
157             sizeof (Inkscape::ApplicationClass),
158             NULL, NULL,
159             (GClassInitFunc) inkscape_class_init,
160             NULL, NULL,
161             sizeof (Inkscape::Application),
162             4,
163             (GInstanceInitFunc) inkscape_init,
164             NULL
165         };
166         type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
167     }
168     return type;
172 /**
173  *  Initializes the inkscape class, registering all of its signal handlers
174  *  and virtual functions
175  */
176 static void
177 inkscape_class_init (Inkscape::ApplicationClass * klass)
179     GObjectClass * object_class;
181     object_class = (GObjectClass *) klass;
183     parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
185     inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
186                                G_TYPE_FROM_CLASS (klass),
187                                G_SIGNAL_RUN_FIRST,
188                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
189                                NULL, NULL,
190                                sp_marshal_NONE__POINTER_UINT,
191                                G_TYPE_NONE, 2,
192                                G_TYPE_POINTER, G_TYPE_UINT);
193     inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
194                                G_TYPE_FROM_CLASS (klass),
195                                G_SIGNAL_RUN_FIRST,
196                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
197                                NULL, NULL,
198                                sp_marshal_NONE__POINTER,
199                                G_TYPE_NONE, 1,
200                                G_TYPE_POINTER);
201     inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
202                                G_TYPE_FROM_CLASS (klass),
203                                G_SIGNAL_RUN_FIRST,
204                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
205                                NULL, NULL,
206                                sp_marshal_NONE__POINTER,
207                                G_TYPE_NONE, 1,
208                                G_TYPE_POINTER);
209     inkscape_signals[SET_SELECTION] =    g_signal_new ("set_selection",
210                                G_TYPE_FROM_CLASS (klass),
211                                G_SIGNAL_RUN_FIRST,
212                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
213                                NULL, NULL,
214                                sp_marshal_NONE__POINTER,
215                                G_TYPE_NONE, 1,
216                                G_TYPE_POINTER);
217     inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
218                                G_TYPE_FROM_CLASS (klass),
219                                G_SIGNAL_RUN_FIRST,
220                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
221                                NULL, NULL,
222                                sp_marshal_NONE__POINTER,
223                                G_TYPE_NONE, 1,
224                                G_TYPE_POINTER);
225     inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
226                                G_TYPE_FROM_CLASS (klass),
227                                G_SIGNAL_RUN_FIRST,
228                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
229                                NULL, NULL,
230                                sp_marshal_NONE__POINTER,
231                                G_TYPE_NONE, 1,
232                                G_TYPE_POINTER);
233     inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
234                                G_TYPE_FROM_CLASS (klass),
235                                G_SIGNAL_RUN_FIRST,
236                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
237                                NULL, NULL,
238                                sp_marshal_NONE__POINTER,
239                                G_TYPE_NONE, 1,
240                                G_TYPE_POINTER);
241     inkscape_signals[SHUTDOWN_SIGNAL] =        g_signal_new ("shut_down",
242                                G_TYPE_FROM_CLASS (klass),
243                                G_SIGNAL_RUN_FIRST,
244                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
245                                NULL, NULL,
246                                g_cclosure_marshal_VOID__VOID,
247                                G_TYPE_NONE, 0);
248     inkscape_signals[DIALOGS_HIDE] =        g_signal_new ("dialogs_hide",
249                                G_TYPE_FROM_CLASS (klass),
250                                G_SIGNAL_RUN_FIRST,
251                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
252                                NULL, NULL,
253                                g_cclosure_marshal_VOID__VOID,
254                                G_TYPE_NONE, 0);
255     inkscape_signals[DIALOGS_UNHIDE] =        g_signal_new ("dialogs_unhide",
256                                G_TYPE_FROM_CLASS (klass),
257                                G_SIGNAL_RUN_FIRST,
258                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
259                                NULL, NULL,
260                                g_cclosure_marshal_VOID__VOID,
261                                G_TYPE_NONE, 0);
262     inkscape_signals[EXTERNAL_CHANGE] =   g_signal_new ("external_change",
263                                G_TYPE_FROM_CLASS (klass),
264                                G_SIGNAL_RUN_FIRST,
265                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
266                                NULL, NULL,
267                                g_cclosure_marshal_VOID__VOID,
268                                G_TYPE_NONE, 0);
270     object_class->dispose = inkscape_dispose;
272     klass->activate_desktop = inkscape_activate_desktop_private;
273     klass->deactivate_desktop = inkscape_deactivate_desktop_private;
276 #ifdef WIN32
277 typedef int uid_t;
278 #define getuid() 0
279 #endif
281 /**
282  * static gint inkscape_autosave(gpointer);
283  *
284  * Callback passed to g_timeout_add_seconds()
285  * Responsible for autosaving all open documents
286  */
287 static gint inkscape_autosave(gpointer)
289     if (inkscape->document_set.empty()) { // nothing to autosave
290         return TRUE;
291     }
292     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
294     // Use UID for separating autosave-documents between users if directory is multiuser
295     uid_t uid = getuid();
297     Glib::ustring autosave_dir;
298     {
299         Glib::ustring tmp = prefs->getString("/options/autosave/path");
300         if (!tmp.empty()) {
301             autosave_dir = tmp;
302         } else {
303             autosave_dir = Glib::get_tmp_dir();
304         }
305     }
307     GDir *autosave_dir_ptr = g_dir_open(autosave_dir.c_str(), 0, NULL);
308     if( !autosave_dir_ptr ){
309         g_warning("Cannot open autosave directory!");
310         return TRUE;
311     }
313     time_t sptime = time(NULL);
314     struct tm *sptm = localtime(&sptime);
315     gchar sptstr[256];
316     strftime(sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
318     gint autosave_max = prefs->getInt("/options/autosave/max", 10);
320     gint docnum = 0;
322     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosaving documents..."));
323     for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
324           iter != inkscape->document_set.end();
325           ++iter) {
327         SPDocument *doc = iter->first;
329         ++docnum;
331         Inkscape::XML::Node *repr = sp_document_repr_root(doc);
332         // g_debug("Document %d: \"%s\" %s", docnum, doc ? doc->name : "(null)", doc ? (doc->isModifiedSinceSave() ? "(dirty)" : "(clean)") : "(null)");
334         if (doc->isModifiedSinceSave()) {
335             gchar *oldest_autosave = 0;
336             const gchar  *filename = 0;
337             struct stat sb;
338             time_t min_time = 0;
339             gint count = 0;
341             // Look for previous autosaves
342             gchar* baseName = g_strdup_printf( "inkscape-autosave-%d", uid );
343             g_dir_rewind(autosave_dir_ptr);
344             while( (filename = g_dir_read_name(autosave_dir_ptr)) != NULL ){
345                 if ( strncmp(filename, baseName, strlen(baseName)) == 0 ){
346                     gchar* full_path = g_build_filename( autosave_dir.c_str(), filename, NULL );
347                     if ( g_stat(full_path, &sb) != -1 ) {
348                         if ( difftime(sb.st_ctime, min_time) < 0 || min_time == 0 ){
349                             min_time = sb.st_ctime;
350                             if ( oldest_autosave ) {
351                                 g_free(oldest_autosave);
352                             }
353                             oldest_autosave = g_strdup(full_path);
354                         }
355                         count ++;
356                     }
357                     g_free(full_path);
358                 }
359             }
361             // g_debug("%d previous autosaves exists. Max = %d", count, autosave_max);
363             // Have we reached the limit for number of autosaves?
364             if ( count >= autosave_max ){
365                 // Remove the oldest file
366                 if ( oldest_autosave ) {
367                     unlink(oldest_autosave);
368                 }
369             }
371             if ( oldest_autosave ) {
372                 g_free(oldest_autosave);
373                 oldest_autosave = 0;
374             }
377             // Set the filename we will actually save to
378             g_free(baseName);
379             baseName = g_strdup_printf("inkscape-autosave-%d-%s-%03d.svg", uid, sptstr, docnum);
380             gchar* full_path = g_build_filename(autosave_dir.c_str(), baseName, NULL);
381             g_free(baseName);
382             baseName = 0;
384             // g_debug("Filename: %s", full_path);
386             // Try to save the file
387             FILE *file = Inkscape::IO::fopen_utf8name(full_path, "w");
388             gchar *errortext = 0;
389             if (file) {
390                 try{
391                     sp_repr_save_stream(repr->document(), file, SP_SVG_NS_URI);
392                 } catch (Inkscape::Extension::Output::no_extension_found &e) {
393                     errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document."));
394                 } catch (Inkscape::Extension::Output::save_failed &e) {
395                     gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
396                     errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
397                     g_free(safeUri);
398                 }
399                 fclose(file);
400             }
401             else {
402                 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
403                 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
404                 g_free(safeUri);
405             }
407             if (errortext) {
408                 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, errortext);
409                 g_warning("%s", errortext);
410                 g_free(errortext);
411             }
413             g_free(full_path);
414         }
415     }
416     g_dir_close(autosave_dir_ptr);
418     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosave complete."));
420     return TRUE;
423 void inkscape_autosave_init()
425     static guint32 autosave_timeout_id = 0;
426     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
428     // Turn off any previously initiated timeouts
429     if ( autosave_timeout_id ) {
430         g_source_remove(autosave_timeout_id);
431         autosave_timeout_id = 0;
432     }
434     // g_debug("options.autosave.enable = %d", prefs->getBool("/options/autosave/enable", true));
435     // Is autosave enabled?
436     if (!prefs->getBool("/options/autosave/enable", true)){
437         autosave_timeout_id = 0;
438     } else {
439         // Turn on autosave
440         guint32 timeout = prefs->getInt("/options/autosave/interval", 10) * 60;
441         // g_debug("options.autosave.interval = %d", prefs->getInt("/options/autosave/interval", 10));
442 #if GLIB_CHECK_VERSION(2,14,0)
443         autosave_timeout_id = g_timeout_add_seconds(timeout, inkscape_autosave, NULL);
444 #else
445         autosave_timeout_id = g_timeout_add(timeout * 1000, inkscape_autosave, NULL);
446 #endif
447     }
451 static void
452 inkscape_init (SPObject * object)
454     if (!inkscape) {
455         inkscape = (Inkscape::Application *) object;
456     } else {
457         g_assert_not_reached ();
458     }
460     new (&inkscape->document_set) std::map<SPDocument *, int>();
462     inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
463     inkscape->desktops = NULL;
464     inkscape->dialogs_toggle = TRUE;
465     inkscape->mapalt=GDK_MOD1_MASK;
468 static void
469 inkscape_dispose (GObject *object)
471     Inkscape::Application *inkscape = (Inkscape::Application *) object;
473     g_assert (!inkscape->desktops);
475     Inkscape::Preferences::unload();
477     if (inkscape->menus) {
478         /* fixme: This is not the best place */
479         Inkscape::GC::release(inkscape->menus);
480         inkscape->menus = NULL;
481     }
483     inkscape->document_set.~map();
485     G_OBJECT_CLASS (parent_class)->dispose (object);
487     gtk_main_quit ();
491 void
492 inkscape_ref (void)
494     if (inkscape)
495         g_object_ref (G_OBJECT (inkscape));
499 void
500 inkscape_unref (void)
502     if (inkscape)
503         g_object_unref (G_OBJECT (inkscape));
506 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
507 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
508 guint
509 inkscape_mapalt() {
510     return inkscape->mapalt;
513 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
514 void inkscape_mapalt(guint maskvalue)
516     if(maskvalue<2 || maskvalue> 5 ){  /* MOD5 is the highest defined in gdktypes.h */
517         inkscape->mapalt=0;
518     }else{
519         inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
520     }
523 static void
524 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
526     desktop->set_active (true);
530 static void
531 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
533     desktop->set_active (false);
537 /* fixme: This is EVIL, and belongs to main after all */
539 #define SP_INDENT 8
542 static void
543 inkscape_crash_handler (int /*signum*/)
545     using Inkscape::Debug::SimpleEvent;
546     using Inkscape::Debug::EventTracker;
547     using Inkscape::Debug::Logger;
549     static gint recursion = FALSE;
551     /*
552      * reset all signal handlers: any further crashes should just be allowed
553      * to crash normally.
554      * */
555     signal (SIGSEGV, segv_handler );
556     signal (SIGABRT, abrt_handler );
557     signal (SIGFPE,  fpe_handler  );
558     signal (SIGILL,  ill_handler  );
559 #ifndef WIN32
560     signal (SIGBUS,  bus_handler  );
561 #endif
563     /* Stop bizarre loops */
564     if (recursion) {
565         abort ();
566     }
567     recursion = TRUE;
569     EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
570     tracker.set<SimpleEvent<> >("emergency-save");
572     fprintf(stderr, "\nEmergency save activated!\n");
574     time_t sptime = time (NULL);
575     struct tm *sptm = localtime (&sptime);
576     gchar sptstr[256];
577     strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
579     gint count = 0;
580     GSList *savednames = NULL;
581     GSList *failednames = NULL;
582     for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
583           iter != inkscape->document_set.end();
584           ++iter) {
585         SPDocument *doc = iter->first;
586         Inkscape::XML::Node *repr;
587         repr = sp_document_repr_root (doc);
588         if (doc->isModifiedSinceSave()) {
589             const gchar *docname, *d0, *d;
590             gchar n[64], c[1024];
591             FILE *file;
593             /* originally, the document name was retrieved from
594              * the sodipod:docname attribute */
595             docname = doc->name;
596             if (docname) {
597                 /* fixme: Quick hack to remove emergency file suffix */
598                 d0 = strrchr ((char*)docname, '.');
599                 if (d0 && (d0 > docname)) {
600                     d0 = strrchr ((char*)(d0 - 1), '.');
601                     if (d0 && (d0 > docname)) {
602                         d = d0;
603                         while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
604                         if (*d) {
605                             memcpy (n, docname, MIN (d0 - docname - 1, 64));
606                             n[63] = '\0';
607                             docname = n;
608                         }
609                     }
610                 }
611             }
613             if (!docname || !*docname) docname = "emergency";
614             // try saving to the profile location
615             g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
616             gchar * location = homedir_path(c);
617             Inkscape::IO::dump_fopen_call(location, "E");
618             file = Inkscape::IO::fopen_utf8name(location, "w");
619             g_free(location);
620             if (!file) {
621                 // try saving to /tmp
622                 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
623                 Inkscape::IO::dump_fopen_call(c, "G");
624                 file = Inkscape::IO::fopen_utf8name(c, "w");
625             }
626             if (!file) {
627                 // try saving to the current directory
628                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
629                 Inkscape::IO::dump_fopen_call(c, "F");
630                 file = Inkscape::IO::fopen_utf8name(c, "w");
631             }
632             if (file) {
633                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
634                 savednames = g_slist_prepend (savednames, g_strdup (c));
635                 fclose (file);
636             } else {
637                 failednames = g_slist_prepend (failednames, (doc->name) ? g_strdup (doc->name) : g_strdup (_("Untitled document")));
638             }
639             count++;
640         }
641     }
643     savednames = g_slist_reverse (savednames);
644     failednames = g_slist_reverse (failednames);
645     if (savednames) {
646         fprintf (stderr, "\nEmergency save document locations:\n");
647         for (GSList *l = savednames; l != NULL; l = l->next) {
648             fprintf (stderr, "  %s\n", (gchar *) l->data);
649         }
650     }
651     if (failednames) {
652         fprintf (stderr, "\nFailed to do emergency save for documents:\n");
653         for (GSList *l = failednames; l != NULL; l = l->next) {
654             fprintf (stderr, "  %s\n", (gchar *) l->data);
655         }
656     }
658     // do not save the preferences since they can be in a corrupted state
659     Inkscape::Preferences::unload(false);
661     fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
662     fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
663     fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
665     /* Show nice dialog box */
667     char const *istr = _("Inkscape encountered an internal error and will close now.\n");
668     char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
669     char const *fstr = _("Automatic backup of the following documents failed:\n");
670     gint nllen = strlen ("\n");
671     gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
672     for (GSList *l = savednames; l != NULL; l = l->next) {
673         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
674     }
675     for (GSList *l = failednames; l != NULL; l = l->next) {
676         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
677     }
678     len += 1;
679     gchar *b = g_new (gchar, len);
680     gint pos = 0;
681     len = strlen (istr);
682     memcpy (b + pos, istr, len);
683     pos += len;
684     if (savednames) {
685         len = strlen (sstr);
686         memcpy (b + pos, sstr, len);
687         pos += len;
688         for (GSList *l = savednames; l != NULL; l = l->next) {
689             memset (b + pos, ' ', SP_INDENT);
690             pos += SP_INDENT;
691             len = strlen ((gchar *) l->data);
692             memcpy (b + pos, l->data, len);
693             pos += len;
694             memcpy (b + pos, "\n", nllen);
695             pos += nllen;
696         }
697     }
698     if (failednames) {
699         len = strlen (fstr);
700         memcpy (b + pos, fstr, len);
701         pos += len;
702         for (GSList *l = failednames; 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     *(b + pos) = '\0';
714     if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
715         GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
716         gtk_dialog_run (GTK_DIALOG (msgbox));
717         gtk_widget_destroy (msgbox);
718     }
719     else
720     {
721         g_message( "Error: %s", b );
722     }
723     g_free (b);
725     tracker.clear();
726     Logger::shutdown();
728     /* on exit, allow restored signal handler to take over and crash us */
733 void
734 inkscape_application_init (const gchar *argv0, gboolean use_gui)
736     inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
737     /* fixme: load application defaults */
739     segv_handler = signal (SIGSEGV, inkscape_crash_handler);
740     abrt_handler = signal (SIGABRT, inkscape_crash_handler);
741     fpe_handler  = signal (SIGFPE,  inkscape_crash_handler);
742     ill_handler  = signal (SIGILL,  inkscape_crash_handler);
743 #ifndef WIN32
744     bus_handler  = signal (SIGBUS,  inkscape_crash_handler);
745 #endif
747     inkscape->use_gui = use_gui;
748     inkscape->argv0 = g_strdup(argv0);
750     /* Load the preferences and menus */
751     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
752     prefs->load(use_gui, false);
753     inkscape_load_menus(inkscape);
754     sp_input_load_from_preferences();
755     
756     /* set language for user interface according setting in preferences */
757     Glib::ustring ui_language = prefs->getString("/ui/language");
758     if(!ui_language.empty())
759     {
760         setenv("LANGUAGE", ui_language, 1);
761     }
763     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
764      * Use only if use_gui is enabled
765      */
766 #ifdef WIN32
767 #define DEFAULT_LOG_REDIRECT true
768 #else
769 #define DEFAULT_LOG_REDIRECT false
770 #endif
772     if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
773     {
774         Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
775     }
777     /* Check for global remapping of Alt key */
778     if(use_gui)
779     {
780         inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
781     }
783     /* Initialize the extensions */
784     Inkscape::Extension::init();
786     inkscape_autosave_init();
788     return;
791 /**
792  *  Returns the current Inkscape::Application global object
793  */
794 Inkscape::Application *
795 inkscape_get_instance()
797         return inkscape;
800 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
802     return app->use_gui;
805 /**
806  *  Menus management
807  *
808  */
809 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
811     gchar *fn = profile_path(MENUS_FILE);
812     gchar *menus_xml = NULL; gsize len = 0;
814     if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
815         // load the menus_xml file
816         INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
817         g_free(menus_xml);
818         if (INKSCAPE->menus) return true;
819     }
820     INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
821     if (INKSCAPE->menus) return true;
822     return false;
826 void
827 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
829     if (Inkscape::NSApplication::Application::getNewGui()) {
830         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
831         return;
832     }
833     g_return_if_fail (selection != NULL);
835     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
836         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
837     }
841 void
842 inkscape_selection_changed (Inkscape::Selection * selection)
844     if (Inkscape::NSApplication::Application::getNewGui()) {
845         Inkscape::NSApplication::Editor::selectionChanged (selection);
846         return;
847     }
848     g_return_if_fail (selection != NULL);
850     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
851         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
852     }
855 void
856 inkscape_subselection_changed (SPDesktop *desktop)
858     if (Inkscape::NSApplication::Application::getNewGui()) {
859         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
860         return;
861     }
862     g_return_if_fail (desktop != NULL);
864     if (DESKTOP_IS_ACTIVE (desktop)) {
865         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
866     }
870 void
871 inkscape_selection_set (Inkscape::Selection * selection)
873     if (Inkscape::NSApplication::Application::getNewGui()) {
874         Inkscape::NSApplication::Editor::selectionSet (selection);
875         return;
876     }
877     g_return_if_fail (selection != NULL);
879     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
880         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
881         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
882     }
886 void
887 inkscape_eventcontext_set (SPEventContext * eventcontext)
889     if (Inkscape::NSApplication::Application::getNewGui()) {
890         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
891         return;
892     }
893     g_return_if_fail (eventcontext != NULL);
894     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
896     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
897         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
898     }
902 void
903 inkscape_add_desktop (SPDesktop * desktop)
905     g_return_if_fail (desktop != NULL);
907     if (Inkscape::NSApplication::Application::getNewGui())
908     {
909         Inkscape::NSApplication::Editor::addDesktop (desktop);
910         return;
911     }
912     g_return_if_fail (inkscape != NULL);
914     g_assert (!g_slist_find (inkscape->desktops, desktop));
916     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
918     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
919     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
920     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
921     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
926 void
927 inkscape_remove_desktop (SPDesktop * desktop)
929     g_return_if_fail (desktop != NULL);
930     if (Inkscape::NSApplication::Application::getNewGui())
931     {
932         Inkscape::NSApplication::Editor::removeDesktop (desktop);
933         return;
934     }
935     g_return_if_fail (inkscape != NULL);
937     g_assert (g_slist_find (inkscape->desktops, desktop));
939     if (DESKTOP_IS_ACTIVE (desktop)) {
940         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
941         if (inkscape->desktops->next != NULL) {
942             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
943             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
944             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
945             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
946             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
947             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
948             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
949         } else {
950             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
951             if (sp_desktop_selection(desktop))
952                 sp_desktop_selection(desktop)->clear();
953         }
954     }
956     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
958     // if this was the last desktop, shut down the program
959     if (inkscape->desktops == NULL) {
960         inkscape_exit (inkscape);
961     }
966 void
967 inkscape_activate_desktop (SPDesktop * desktop)
969     g_return_if_fail (desktop != NULL);
970     if (Inkscape::NSApplication::Application::getNewGui())
971     {
972         Inkscape::NSApplication::Editor::activateDesktop (desktop);
973         return;
974     }
975     g_return_if_fail (inkscape != NULL);
977     if (DESKTOP_IS_ACTIVE (desktop)) {
978         return;
979     }
981     g_assert (g_slist_find (inkscape->desktops, desktop));
983     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
985     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
987     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
988     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
990     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
991     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
992     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
993     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
997 /**
998  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
999  */
1000 void
1001 inkscape_reactivate_desktop (SPDesktop * desktop)
1003     g_return_if_fail (desktop != NULL);
1004     if (Inkscape::NSApplication::Application::getNewGui())
1005     {
1006         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1007         return;
1008     }
1009     g_return_if_fail (inkscape != NULL);
1011     if (DESKTOP_IS_ACTIVE (desktop))
1012         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1017 SPDesktop *
1018 inkscape_find_desktop_by_dkey (unsigned int dkey)
1020     for (GSList *r = inkscape->desktops; r; r = r->next) {
1021         if (((SPDesktop *) r->data)->dkey == dkey)
1022             return ((SPDesktop *) r->data);
1023     }
1024     return NULL;
1030 unsigned int
1031 inkscape_maximum_dkey()
1033     unsigned int dkey = 0;
1035     for (GSList *r = inkscape->desktops; r; r = r->next) {
1036         if (((SPDesktop *) r->data)->dkey > dkey)
1037             dkey = ((SPDesktop *) r->data)->dkey;
1038     }
1040     return dkey;
1045 SPDesktop *
1046 inkscape_next_desktop ()
1048     SPDesktop *d = NULL;
1049     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1051     if (dkey_current < inkscape_maximum_dkey()) {
1052         // find next existing
1053         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1054             d = inkscape_find_desktop_by_dkey (i);
1055             if (d) {
1056                 break;
1057             }
1058         }
1059     } else {
1060         // find first existing
1061         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1062             d = inkscape_find_desktop_by_dkey (i);
1063             if (d) {
1064                 break;
1065             }
1066         }
1067     }
1069     g_assert (d);
1071     return d;
1076 SPDesktop *
1077 inkscape_prev_desktop ()
1079     SPDesktop *d = NULL;
1080     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1082     if (dkey_current > 0) {
1083         // find prev existing
1084         for (signed int i = dkey_current - 1; i >= 0; i--) {
1085             d = inkscape_find_desktop_by_dkey (i);
1086             if (d) {
1087                 break;
1088             }
1089         }
1090     }
1091     if (!d) {
1092         // find last existing
1093         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1094     }
1096     g_assert (d);
1098     return d;
1103 void
1104 inkscape_switch_desktops_next ()
1106     inkscape_next_desktop()->presentWindow();
1111 void
1112 inkscape_switch_desktops_prev ()
1114     inkscape_prev_desktop()->presentWindow();
1119 void
1120 inkscape_dialogs_hide ()
1122     if (Inkscape::NSApplication::Application::getNewGui())
1123         Inkscape::NSApplication::Editor::hideDialogs();
1124     else
1125     {
1126         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1127         inkscape->dialogs_toggle = FALSE;
1128     }
1133 void
1134 inkscape_dialogs_unhide ()
1136     if (Inkscape::NSApplication::Application::getNewGui())
1137         Inkscape::NSApplication::Editor::unhideDialogs();
1138     else
1139     {
1140         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1141         inkscape->dialogs_toggle = TRUE;
1142     }
1147 void
1148 inkscape_dialogs_toggle ()
1150     if (inkscape->dialogs_toggle) {
1151         inkscape_dialogs_hide ();
1152     } else {
1153         inkscape_dialogs_unhide ();
1154     }
1157 void
1158 inkscape_external_change ()
1160     g_return_if_fail (inkscape != NULL);
1162     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1165 /**
1166  * fixme: These need probably signals too
1167  */
1168 void
1169 inkscape_add_document (SPDocument *document)
1171     g_return_if_fail (document != NULL);
1173     if (!Inkscape::NSApplication::Application::getNewGui())
1174     {
1175         // try to insert the pair into the list
1176         if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1177             //insert failed, this key (document) is already in the list
1178             for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1179                    iter != inkscape->document_set.end();
1180                    ++iter) {
1181                 if (iter->first == document) {
1182                     // found this document in list, increase its count
1183                     iter->second ++;
1184                 }
1185            }
1186         }
1187     }
1188     else
1189     {
1190         Inkscape::NSApplication::Editor::addDocument (document);
1191     }
1195 // returns true if this was last reference to this document, so you can delete it
1196 bool
1197 inkscape_remove_document (SPDocument *document)
1199     g_return_val_if_fail (document != NULL, false);
1201     if (!Inkscape::NSApplication::Application::getNewGui())
1202     {
1203         for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1204                   iter != inkscape->document_set.end();
1205                   ++iter) {
1206             if (iter->first == document) {
1207                 // found this document in list, decrease its count
1208                 iter->second --;
1209                 if (iter->second < 1) {
1210                     // this was the last one, remove the pair from list
1211                     inkscape->document_set.erase (iter);
1212                     return true;
1213                 } else {
1214                     return false;
1215                 }
1216             }
1217         }
1218     }
1219     else
1220     {
1221         Inkscape::NSApplication::Editor::removeDocument (document);
1222     }
1224     return false;
1227 SPDesktop *
1228 inkscape_active_desktop (void)
1230     if (Inkscape::NSApplication::Application::getNewGui())
1231         return Inkscape::NSApplication::Editor::getActiveDesktop();
1233     if (inkscape->desktops == NULL) {
1234         return NULL;
1235     }
1237     return (SPDesktop *) inkscape->desktops->data;
1240 SPDocument *
1241 inkscape_active_document (void)
1243     if (Inkscape::NSApplication::Application::getNewGui())
1244         return Inkscape::NSApplication::Editor::getActiveDocument();
1246     if (SP_ACTIVE_DESKTOP) {
1247         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1248     }
1250     return NULL;
1253 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1254     SPDocument const* document = desktop.doc();
1255     if (!document) {
1256         return false;
1257     }
1258     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1259         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1260         SPDocument *other_document=other_desktop->doc();
1261         if ( other_document == document && other_desktop != &desktop ) {
1262             return false;
1263         }
1264     }
1265     return true;
1268 SPEventContext *
1269 inkscape_active_event_context (void)
1271     if (SP_ACTIVE_DESKTOP) {
1272         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1273     }
1275     return NULL;
1280 /*#####################
1281 # HELPERS
1282 #####################*/
1284 void
1285 inkscape_refresh_display (Inkscape::Application *inkscape)
1287     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1288         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1289     }
1293 /**
1294  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1295  *  saves the preferences if appropriate, and quits.
1296  */
1297 void
1298 inkscape_exit (Inkscape::Application */*inkscape*/)
1300     g_assert (INKSCAPE);
1302     //emit shutdown signal so that dialogs could remember layout
1303     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1305     Inkscape::Preferences::unload();
1306     gtk_main_quit ();
1309 char *
1310 homedir_path(const char *filename)
1312     static const gchar *homedir = NULL;
1313     if (!homedir) {
1314         homedir = g_get_home_dir();
1315     }
1316     if (!homedir) {
1317         homedir = g_path_get_dirname(INKSCAPE->argv0);
1318     }
1319     return g_build_filename(homedir, filename, NULL);
1323 /**
1324  * Get, or guess, or decide the location where the preferences.xml
1325  * file should be located.
1326  */
1327 gchar *
1328 profile_path(const char *filename)
1330     static const gchar *prefdir = NULL;
1331     if (!prefdir) {
1332 #ifdef HAS_SHGetSpecialFolderLocation
1333         // prefer c:\Documents and Settings\UserName\Application Data\ to
1334         // c:\Documents and Settings\userName\;
1335         if (!prefdir) {
1336             ITEMIDLIST *pidl = 0;
1337             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1338                 gchar * utf8Path = NULL;
1340                 if ( PrintWin32::is_os_wide() ) {
1341                     wchar_t pathBuf[MAX_PATH+1];
1342                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1344                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1345                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1346                     }
1347                 } else {
1348                     char pathBuf[MAX_PATH+1];
1350                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1351                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1352                     }
1353                 }
1355                 if ( utf8Path ) {
1356                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1357                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1358                         g_free( utf8Path );
1359                         utf8Path = 0;
1360                     } else {
1361                         prefdir = utf8Path;
1362                     }
1363                 }
1366                 /* not compiling yet...
1368                 // Remember to free the list pointer
1369                 IMalloc * imalloc = 0;
1370                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1371                     imalloc->lpVtbl->Free( imalloc, pidl );
1372                     imalloc->lpVtbl->Release( imalloc );
1373                 }
1374                 */
1375             }
1377             if (prefdir) {
1378                 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1379             }
1380         }
1381 #endif
1382         if (!prefdir) {
1383             prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1384             gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1386             // TODO here is a point to hook in preference migration
1388             if ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) ) {
1389                 prefdir = legacyDir;
1390             } else {
1391                 g_free(legacyDir);
1392                 legacyDir = 0;
1393             }
1394         }
1395     }
1396     return g_build_filename(prefdir, filename, NULL);
1399 Inkscape::XML::Node *
1400 inkscape_get_menus (Inkscape::Application * inkscape)
1402     Inkscape::XML::Node *repr = inkscape->menus->root();
1403     g_assert (!(strcmp (repr->name(), "inkscape")));
1404     return repr->firstChild();
1407 void
1408 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1410     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1411         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1412     }
1415 /*
1416   Local Variables:
1417   mode:c++
1418   c-file-style:"stroustrup"
1419   c-file-offsets:((innamespace . 0)(inline-open . 0))
1420   indent-tabs-mode:nil
1421   fill-column:99
1422   End:
1423 */
1424 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :