Code

Change the way preferences are loaded to simplify unit testing
[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);
464     inkscape->desktops = NULL;
466     inkscape->dialogs_toggle = TRUE;
468     inkscape->mapalt=GDK_MOD1_MASK;
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 static void
527 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
529     desktop->set_active (true);
533 static void
534 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
536     desktop->set_active (false);
540 /* fixme: This is EVIL, and belongs to main after all */
542 #define SP_INDENT 8
545 static void
546 inkscape_crash_handler (int /*signum*/)
548     using Inkscape::Debug::SimpleEvent;
549     using Inkscape::Debug::EventTracker;
550     using Inkscape::Debug::Logger;
552     static gint recursion = FALSE;
554     /*
555      * reset all signal handlers: any further crashes should just be allowed
556      * to crash normally.
557      * */
558     signal (SIGSEGV, segv_handler );
559     signal (SIGABRT, abrt_handler );
560     signal (SIGFPE,  fpe_handler  );
561     signal (SIGILL,  ill_handler  );
562 #ifndef WIN32
563     signal (SIGBUS,  bus_handler  );
564 #endif
566     /* Stop bizarre loops */
567     if (recursion) {
568         abort ();
569     }
570     recursion = TRUE;
572     EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
573     tracker.set<SimpleEvent<> >("emergency-save");
575     fprintf(stderr, "\nEmergency save activated!\n");
577     time_t sptime = time (NULL);
578     struct tm *sptm = localtime (&sptime);
579     gchar sptstr[256];
580     strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
582     gint count = 0;
583     GSList *savednames = NULL;
584     GSList *failednames = NULL;
585     for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
586           iter != inkscape->document_set.end();
587           ++iter) {
588         SPDocument *doc = iter->first;
589         Inkscape::XML::Node *repr;
590         repr = sp_document_repr_root (doc);
591         if (doc->isModifiedSinceSave()) {
592             const gchar *docname, *d0, *d;
593             gchar n[64], c[1024];
594             FILE *file;
596             /* originally, the document name was retrieved from
597              * the sodipod:docname attribute */
598             docname = doc->name;
599             if (docname) {
600                 /* fixme: Quick hack to remove emergency file suffix */
601                 d0 = strrchr ((char*)docname, '.');
602                 if (d0 && (d0 > docname)) {
603                     d0 = strrchr ((char*)(d0 - 1), '.');
604                     if (d0 && (d0 > docname)) {
605                         d = d0;
606                         while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
607                         if (*d) {
608                             memcpy (n, docname, MIN (d0 - docname - 1, 64));
609                             n[63] = '\0';
610                             docname = n;
611                         }
612                     }
613                 }
614             }
616             if (!docname || !*docname) docname = "emergency";
617             // try saving to the profile location
618             g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
619             gchar * location = homedir_path(c);
620             Inkscape::IO::dump_fopen_call(location, "E");
621             file = Inkscape::IO::fopen_utf8name(location, "w");
622             g_free(location);
623             if (!file) {
624                 // try saving to /tmp
625                 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
626                 Inkscape::IO::dump_fopen_call(c, "G");
627                 file = Inkscape::IO::fopen_utf8name(c, "w");
628             }
629             if (!file) {
630                 // try saving to the current directory
631                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
632                 Inkscape::IO::dump_fopen_call(c, "F");
633                 file = Inkscape::IO::fopen_utf8name(c, "w");
634             }
635             if (file) {
636                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
637                 savednames = g_slist_prepend (savednames, g_strdup (c));
638                 fclose (file);
639             } else {
640                 failednames = g_slist_prepend (failednames, (doc->name) ? g_strdup (doc->name) : g_strdup (_("Untitled document")));
641             }
642             count++;
643         }
644     }
646     savednames = g_slist_reverse (savednames);
647     failednames = g_slist_reverse (failednames);
648     if (savednames) {
649         fprintf (stderr, "\nEmergency save document locations:\n");
650         for (GSList *l = savednames; l != NULL; l = l->next) {
651             fprintf (stderr, "  %s\n", (gchar *) l->data);
652         }
653     }
654     if (failednames) {
655         fprintf (stderr, "\nFailed to do emergency save for documents:\n");
656         for (GSList *l = failednames; l != NULL; l = l->next) {
657             fprintf (stderr, "  %s\n", (gchar *) l->data);
658         }
659     }
661     Inkscape::Preferences::unload();
663     fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
664     fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
665     fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
667     /* Show nice dialog box */
669     char const *istr = _("Inkscape encountered an internal error and will close now.\n");
670     char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
671     char const *fstr = _("Automatic backup of the following documents failed:\n");
672     gint nllen = strlen ("\n");
673     gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
674     for (GSList *l = savednames; l != NULL; l = l->next) {
675         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
676     }
677     for (GSList *l = failednames; l != NULL; l = l->next) {
678         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
679     }
680     len += 1;
681     gchar *b = g_new (gchar, len);
682     gint pos = 0;
683     len = strlen (istr);
684     memcpy (b + pos, istr, len);
685     pos += len;
686     if (savednames) {
687         len = strlen (sstr);
688         memcpy (b + pos, sstr, len);
689         pos += len;
690         for (GSList *l = savednames; l != NULL; l = l->next) {
691             memset (b + pos, ' ', SP_INDENT);
692             pos += SP_INDENT;
693             len = strlen ((gchar *) l->data);
694             memcpy (b + pos, l->data, len);
695             pos += len;
696             memcpy (b + pos, "\n", nllen);
697             pos += nllen;
698         }
699     }
700     if (failednames) {
701         len = strlen (fstr);
702         memcpy (b + pos, fstr, len);
703         pos += len;
704         for (GSList *l = failednames; l != NULL; l = l->next) {
705             memset (b + pos, ' ', SP_INDENT);
706             pos += SP_INDENT;
707             len = strlen ((gchar *) l->data);
708             memcpy (b + pos, l->data, len);
709             pos += len;
710             memcpy (b + pos, "\n", nllen);
711             pos += nllen;
712         }
713     }
714     *(b + pos) = '\0';
716     if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
717         GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
718         gtk_dialog_run (GTK_DIALOG (msgbox));
719         gtk_widget_destroy (msgbox);
720     }
721     else
722     {
723         g_message( "Error: %s", b );
724     }
725     g_free (b);
727     tracker.clear();
728     Logger::shutdown();
730     /* on exit, allow restored signal handler to take over and crash us */
735 void
736 inkscape_application_init (const gchar *argv0, gboolean use_gui)
738     inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
739     /* fixme: load application defaults */
741     segv_handler = signal (SIGSEGV, inkscape_crash_handler);
742     abrt_handler = signal (SIGABRT, inkscape_crash_handler);
743     fpe_handler  = signal (SIGFPE,  inkscape_crash_handler);
744     ill_handler  = signal (SIGILL,  inkscape_crash_handler);
745 #ifndef WIN32
746     bus_handler  = signal (SIGBUS,  inkscape_crash_handler);
747 #endif
749     inkscape->use_gui = use_gui;
750     inkscape->argv0 = g_strdup(argv0);
752     /* Load the preferences and menus; Later menu layout should be merged into prefs */
753     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
754     prefs->load(use_gui, false);
755     inkscape_load_menus(inkscape);
756     sp_input_load_from_preferences();
758     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
759      * Use only if use_gui is enabled
760      */
761 #ifdef WIN32
762 #define DEFAULT_LOG_REDIRECT true
763 #else
764 #define DEFAULT_LOG_REDIRECT false
765 #endif
767     if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
768     {
769         Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
770     }
772     /* Check for global remapping of Alt key */
773     if(use_gui)
774     {
775         inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
776     }
778     /* Initialize the extensions */
779     Inkscape::Extension::init();
781     inkscape_autosave_init();
783     return;
786 /**
787  *  Returns the current Inkscape::Application global object
788  */
789 Inkscape::Application *
790 inkscape_get_instance()
792         return inkscape;
795 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
797     return app->use_gui;
800 /**
801  *  Menus management
802  *
803  */
804 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
806     gchar *fn = profile_path(MENUS_FILE);
807     gchar *menus_xml = NULL; gsize len = 0;
809     if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
810         // load the menus_xml file
811         INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
812         g_free(menus_xml);
813         if (INKSCAPE->menus) return true;
814     }
815     INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
816     if (INKSCAPE->menus) return true;
817     return false;
821 void
822 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
824     if (Inkscape::NSApplication::Application::getNewGui()) {
825         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
826         return;
827     }
828     g_return_if_fail (selection != NULL);
830     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
831         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
832     }
836 void
837 inkscape_selection_changed (Inkscape::Selection * selection)
839     if (Inkscape::NSApplication::Application::getNewGui()) {
840         Inkscape::NSApplication::Editor::selectionChanged (selection);
841         return;
842     }
843     g_return_if_fail (selection != NULL);
845     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
846         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
847     }
850 void
851 inkscape_subselection_changed (SPDesktop *desktop)
853     if (Inkscape::NSApplication::Application::getNewGui()) {
854         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
855         return;
856     }
857     g_return_if_fail (desktop != NULL);
859     if (DESKTOP_IS_ACTIVE (desktop)) {
860         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
861     }
865 void
866 inkscape_selection_set (Inkscape::Selection * selection)
868     if (Inkscape::NSApplication::Application::getNewGui()) {
869         Inkscape::NSApplication::Editor::selectionSet (selection);
870         return;
871     }
872     g_return_if_fail (selection != NULL);
874     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
875         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
876         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
877     }
881 void
882 inkscape_eventcontext_set (SPEventContext * eventcontext)
884     if (Inkscape::NSApplication::Application::getNewGui()) {
885         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
886         return;
887     }
888     g_return_if_fail (eventcontext != NULL);
889     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
891     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
892         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
893     }
897 void
898 inkscape_add_desktop (SPDesktop * desktop)
900     g_return_if_fail (desktop != NULL);
902     if (Inkscape::NSApplication::Application::getNewGui())
903     {
904         Inkscape::NSApplication::Editor::addDesktop (desktop);
905         return;
906     }
907     g_return_if_fail (inkscape != NULL);
909     g_assert (!g_slist_find (inkscape->desktops, desktop));
911     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
913     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
914     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
915     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
916     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
921 void
922 inkscape_remove_desktop (SPDesktop * desktop)
924     g_return_if_fail (desktop != NULL);
925     if (Inkscape::NSApplication::Application::getNewGui())
926     {
927         Inkscape::NSApplication::Editor::removeDesktop (desktop);
928         return;
929     }
930     g_return_if_fail (inkscape != NULL);
932     g_assert (g_slist_find (inkscape->desktops, desktop));
934     if (DESKTOP_IS_ACTIVE (desktop)) {
935         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
936         if (inkscape->desktops->next != NULL) {
937             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
938             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
939             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
940             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
941             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
942             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
943             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
944         } else {
945             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
946             if (sp_desktop_selection(desktop))
947                 sp_desktop_selection(desktop)->clear();
948         }
949     }
951     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
953     // if this was the last desktop, shut down the program
954     if (inkscape->desktops == NULL) {
955         inkscape_exit (inkscape);
956     }
961 void
962 inkscape_activate_desktop (SPDesktop * desktop)
964     g_return_if_fail (desktop != NULL);
965     if (Inkscape::NSApplication::Application::getNewGui())
966     {
967         Inkscape::NSApplication::Editor::activateDesktop (desktop);
968         return;
969     }
970     g_return_if_fail (inkscape != NULL);
972     if (DESKTOP_IS_ACTIVE (desktop)) {
973         return;
974     }
976     g_assert (g_slist_find (inkscape->desktops, desktop));
978     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
980     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
982     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
983     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
985     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
986     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
987     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
988     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
992 /**
993  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
994  */
995 void
996 inkscape_reactivate_desktop (SPDesktop * desktop)
998     g_return_if_fail (desktop != NULL);
999     if (Inkscape::NSApplication::Application::getNewGui())
1000     {
1001         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1002         return;
1003     }
1004     g_return_if_fail (inkscape != NULL);
1006     if (DESKTOP_IS_ACTIVE (desktop))
1007         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1012 SPDesktop *
1013 inkscape_find_desktop_by_dkey (unsigned int dkey)
1015     for (GSList *r = inkscape->desktops; r; r = r->next) {
1016         if (((SPDesktop *) r->data)->dkey == dkey)
1017             return ((SPDesktop *) r->data);
1018     }
1019     return NULL;
1025 unsigned int
1026 inkscape_maximum_dkey()
1028     unsigned int dkey = 0;
1030     for (GSList *r = inkscape->desktops; r; r = r->next) {
1031         if (((SPDesktop *) r->data)->dkey > dkey)
1032             dkey = ((SPDesktop *) r->data)->dkey;
1033     }
1035     return dkey;
1040 SPDesktop *
1041 inkscape_next_desktop ()
1043     SPDesktop *d = NULL;
1044     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1046     if (dkey_current < inkscape_maximum_dkey()) {
1047         // find next existing
1048         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1049             d = inkscape_find_desktop_by_dkey (i);
1050             if (d) {
1051                 break;
1052             }
1053         }
1054     } else {
1055         // find first existing
1056         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1057             d = inkscape_find_desktop_by_dkey (i);
1058             if (d) {
1059                 break;
1060             }
1061         }
1062     }
1064     g_assert (d);
1066     return d;
1071 SPDesktop *
1072 inkscape_prev_desktop ()
1074     SPDesktop *d = NULL;
1075     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1077     if (dkey_current > 0) {
1078         // find prev existing
1079         for (signed int i = dkey_current - 1; i >= 0; i--) {
1080             d = inkscape_find_desktop_by_dkey (i);
1081             if (d) {
1082                 break;
1083             }
1084         }
1085     }
1086     if (!d) {
1087         // find last existing
1088         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1089     }
1091     g_assert (d);
1093     return d;
1098 void
1099 inkscape_switch_desktops_next ()
1101     inkscape_next_desktop()->presentWindow();
1106 void
1107 inkscape_switch_desktops_prev ()
1109     inkscape_prev_desktop()->presentWindow();
1114 void
1115 inkscape_dialogs_hide ()
1117     if (Inkscape::NSApplication::Application::getNewGui())
1118         Inkscape::NSApplication::Editor::hideDialogs();
1119     else
1120     {
1121         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1122         inkscape->dialogs_toggle = FALSE;
1123     }
1128 void
1129 inkscape_dialogs_unhide ()
1131     if (Inkscape::NSApplication::Application::getNewGui())
1132         Inkscape::NSApplication::Editor::unhideDialogs();
1133     else
1134     {
1135         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1136         inkscape->dialogs_toggle = TRUE;
1137     }
1142 void
1143 inkscape_dialogs_toggle ()
1145     if (inkscape->dialogs_toggle) {
1146         inkscape_dialogs_hide ();
1147     } else {
1148         inkscape_dialogs_unhide ();
1149     }
1152 void
1153 inkscape_external_change ()
1155     g_return_if_fail (inkscape != NULL);
1157     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1160 /**
1161  * fixme: These need probably signals too
1162  */
1163 void
1164 inkscape_add_document (SPDocument *document)
1166     g_return_if_fail (document != NULL);
1168     if (!Inkscape::NSApplication::Application::getNewGui())
1169     {
1170         // try to insert the pair into the list
1171         if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1172             //insert failed, this key (document) is already in the list
1173             for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1174                    iter != inkscape->document_set.end();
1175                    ++iter) {
1176                 if (iter->first == document) {
1177                     // found this document in list, increase its count
1178                     iter->second ++;
1179                 }
1180            }
1181         }
1182     }
1183     else
1184     {
1185         Inkscape::NSApplication::Editor::addDocument (document);
1186     }
1190 // returns true if this was last reference to this document, so you can delete it
1191 bool
1192 inkscape_remove_document (SPDocument *document)
1194     g_return_val_if_fail (document != NULL, false);
1196     if (!Inkscape::NSApplication::Application::getNewGui())
1197     {
1198         for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1199                   iter != inkscape->document_set.end();
1200                   ++iter) {
1201             if (iter->first == document) {
1202                 // found this document in list, decrease its count
1203                 iter->second --;
1204                 if (iter->second < 1) {
1205                     // this was the last one, remove the pair from list
1206                     inkscape->document_set.erase (iter);
1207                     return true;
1208                 } else {
1209                     return false;
1210                 }
1211             }
1212         }
1213     }
1214     else
1215     {
1216         Inkscape::NSApplication::Editor::removeDocument (document);
1217     }
1219     return false;
1222 SPDesktop *
1223 inkscape_active_desktop (void)
1225     if (Inkscape::NSApplication::Application::getNewGui())
1226         return Inkscape::NSApplication::Editor::getActiveDesktop();
1228     if (inkscape->desktops == NULL) {
1229         return NULL;
1230     }
1232     return (SPDesktop *) inkscape->desktops->data;
1235 SPDocument *
1236 inkscape_active_document (void)
1238     if (Inkscape::NSApplication::Application::getNewGui())
1239         return Inkscape::NSApplication::Editor::getActiveDocument();
1241     if (SP_ACTIVE_DESKTOP) {
1242         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1243     }
1245     return NULL;
1248 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1249     SPDocument const* document = desktop.doc();
1250     if (!document) {
1251         return false;
1252     }
1253     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1254         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1255         SPDocument *other_document=other_desktop->doc();
1256         if ( other_document == document && other_desktop != &desktop ) {
1257             return false;
1258         }
1259     }
1260     return true;
1263 SPEventContext *
1264 inkscape_active_event_context (void)
1266     if (SP_ACTIVE_DESKTOP) {
1267         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1268     }
1270     return NULL;
1275 /*#####################
1276 # HELPERS
1277 #####################*/
1279 void
1280 inkscape_refresh_display (Inkscape::Application *inkscape)
1282     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1283         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1284     }
1288 /**
1289  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1290  *  saves the preferences if appropriate, and quits.
1291  */
1292 void
1293 inkscape_exit (Inkscape::Application */*inkscape*/)
1295     g_assert (INKSCAPE);
1297     //emit shutdown signal so that dialogs could remember layout
1298     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1300     Inkscape::Preferences::unload();
1301     gtk_main_quit ();
1304 char *
1305 homedir_path(const char *filename)
1307     static const gchar *homedir = NULL;
1308     if (!homedir) {
1309         homedir = g_get_home_dir();
1310     }
1311     if (!homedir) {
1312         homedir = g_path_get_dirname(INKSCAPE->argv0);
1313     }
1314     return g_build_filename(homedir, filename, NULL);
1318 /**
1319  * Get, or guess, or decide the location where the preferences.xml
1320  * file should be located.
1321  */
1322 gchar *
1323 profile_path(const char *filename)
1325     static const gchar *prefdir = NULL;
1326     if (!prefdir) {
1327 #ifdef HAS_SHGetSpecialFolderLocation
1328         // prefer c:\Documents and Settings\UserName\Application Data\ to
1329         // c:\Documents and Settings\userName\;
1330         if (!prefdir) {
1331             ITEMIDLIST *pidl = 0;
1332             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1333                 gchar * utf8Path = NULL;
1335                 if ( PrintWin32::is_os_wide() ) {
1336                     wchar_t pathBuf[MAX_PATH+1];
1337                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1339                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1340                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1341                     }
1342                 } else {
1343                     char pathBuf[MAX_PATH+1];
1345                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1346                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1347                     }
1348                 }
1350                 if ( utf8Path ) {
1351                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1352                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1353                         g_free( utf8Path );
1354                         utf8Path = 0;
1355                     } else {
1356                         prefdir = utf8Path;
1357                     }
1358                 }
1361                 /* not compiling yet...
1363                 // Remember to free the list pointer
1364                 IMalloc * imalloc = 0;
1365                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1366                     imalloc->lpVtbl->Free( imalloc, pidl );
1367                     imalloc->lpVtbl->Release( imalloc );
1368                 }
1369                 */
1370             }
1372             if (prefdir) {
1373                 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1374             }
1375         }
1376 #endif
1377         if (!prefdir) {
1378             prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1379             gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1381             // TODO here is a point to hook in preference migration
1383             if ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) ) {
1384                 prefdir = legacyDir;
1385             } else {
1386                 g_free(legacyDir);
1387                 legacyDir = 0;
1388             }
1389         }
1390     }
1391     return g_build_filename(prefdir, filename, NULL);
1394 Inkscape::XML::Node *
1395 inkscape_get_menus (Inkscape::Application * inkscape)
1397     Inkscape::XML::Node *repr = inkscape->menus->root();
1398     g_assert (!(strcmp (repr->name(), "inkscape")));
1399     return repr->firstChild();
1402 void
1403 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1405     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1406         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1407     }
1412 /*
1413   Local Variables:
1414   mode:c++
1415   c-file-style:"stroustrup"
1416   c-file-offsets:((innamespace . 0)(inline-open . 0))
1417   indent-tabs-mode:nil
1418   fill-column:99
1419   End:
1420 */
1421 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :