Code

fix pasting style after copying a text span
[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();
757     
758     /* set language for user interface according setting in preferences */
759     Glib::ustring ui_language = prefs->getString("/ui/language");
760     if(!ui_language.empty())
761     {
762         setenv("LANGUAGE", ui_language, 1);
763     }
765     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
766      * Use only if use_gui is enabled
767      */
768 #ifdef WIN32
769 #define DEFAULT_LOG_REDIRECT true
770 #else
771 #define DEFAULT_LOG_REDIRECT false
772 #endif
774     if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
775     {
776         Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
777     }
779     /* Check for global remapping of Alt key */
780     if(use_gui)
781     {
782         inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
783     }
785     /* Initialize the extensions */
786     Inkscape::Extension::init();
788     inkscape_autosave_init();
790     return;
793 /**
794  *  Returns the current Inkscape::Application global object
795  */
796 Inkscape::Application *
797 inkscape_get_instance()
799         return inkscape;
802 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
804     return app->use_gui;
807 /**
808  *  Menus management
809  *
810  */
811 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
813     gchar *fn = profile_path(MENUS_FILE);
814     gchar *menus_xml = NULL; gsize len = 0;
816     if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
817         // load the menus_xml file
818         INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
819         g_free(menus_xml);
820         if (INKSCAPE->menus) return true;
821     }
822     INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
823     if (INKSCAPE->menus) return true;
824     return false;
828 void
829 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
831     if (Inkscape::NSApplication::Application::getNewGui()) {
832         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
833         return;
834     }
835     g_return_if_fail (selection != NULL);
837     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
838         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
839     }
843 void
844 inkscape_selection_changed (Inkscape::Selection * selection)
846     if (Inkscape::NSApplication::Application::getNewGui()) {
847         Inkscape::NSApplication::Editor::selectionChanged (selection);
848         return;
849     }
850     g_return_if_fail (selection != NULL);
852     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
853         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
854     }
857 void
858 inkscape_subselection_changed (SPDesktop *desktop)
860     if (Inkscape::NSApplication::Application::getNewGui()) {
861         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
862         return;
863     }
864     g_return_if_fail (desktop != NULL);
866     if (DESKTOP_IS_ACTIVE (desktop)) {
867         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
868     }
872 void
873 inkscape_selection_set (Inkscape::Selection * selection)
875     if (Inkscape::NSApplication::Application::getNewGui()) {
876         Inkscape::NSApplication::Editor::selectionSet (selection);
877         return;
878     }
879     g_return_if_fail (selection != NULL);
881     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
882         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
883         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
884     }
888 void
889 inkscape_eventcontext_set (SPEventContext * eventcontext)
891     if (Inkscape::NSApplication::Application::getNewGui()) {
892         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
893         return;
894     }
895     g_return_if_fail (eventcontext != NULL);
896     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
898     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
899         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
900     }
904 void
905 inkscape_add_desktop (SPDesktop * desktop)
907     g_return_if_fail (desktop != NULL);
909     if (Inkscape::NSApplication::Application::getNewGui())
910     {
911         Inkscape::NSApplication::Editor::addDesktop (desktop);
912         return;
913     }
914     g_return_if_fail (inkscape != NULL);
916     g_assert (!g_slist_find (inkscape->desktops, desktop));
918     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
920     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
921     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
922     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
923     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
928 void
929 inkscape_remove_desktop (SPDesktop * desktop)
931     g_return_if_fail (desktop != NULL);
932     if (Inkscape::NSApplication::Application::getNewGui())
933     {
934         Inkscape::NSApplication::Editor::removeDesktop (desktop);
935         return;
936     }
937     g_return_if_fail (inkscape != NULL);
939     g_assert (g_slist_find (inkscape->desktops, desktop));
941     if (DESKTOP_IS_ACTIVE (desktop)) {
942         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
943         if (inkscape->desktops->next != NULL) {
944             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
945             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
946             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
947             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
948             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
949             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
950             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
951         } else {
952             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
953             if (sp_desktop_selection(desktop))
954                 sp_desktop_selection(desktop)->clear();
955         }
956     }
958     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
960     // if this was the last desktop, shut down the program
961     if (inkscape->desktops == NULL) {
962         inkscape_exit (inkscape);
963     }
968 void
969 inkscape_activate_desktop (SPDesktop * desktop)
971     g_return_if_fail (desktop != NULL);
972     if (Inkscape::NSApplication::Application::getNewGui())
973     {
974         Inkscape::NSApplication::Editor::activateDesktop (desktop);
975         return;
976     }
977     g_return_if_fail (inkscape != NULL);
979     if (DESKTOP_IS_ACTIVE (desktop)) {
980         return;
981     }
983     g_assert (g_slist_find (inkscape->desktops, desktop));
985     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
987     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
989     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
990     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
992     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
993     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
994     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
995     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
999 /**
1000  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
1001  */
1002 void
1003 inkscape_reactivate_desktop (SPDesktop * desktop)
1005     g_return_if_fail (desktop != NULL);
1006     if (Inkscape::NSApplication::Application::getNewGui())
1007     {
1008         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1009         return;
1010     }
1011     g_return_if_fail (inkscape != NULL);
1013     if (DESKTOP_IS_ACTIVE (desktop))
1014         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1019 SPDesktop *
1020 inkscape_find_desktop_by_dkey (unsigned int dkey)
1022     for (GSList *r = inkscape->desktops; r; r = r->next) {
1023         if (((SPDesktop *) r->data)->dkey == dkey)
1024             return ((SPDesktop *) r->data);
1025     }
1026     return NULL;
1032 unsigned int
1033 inkscape_maximum_dkey()
1035     unsigned int dkey = 0;
1037     for (GSList *r = inkscape->desktops; r; r = r->next) {
1038         if (((SPDesktop *) r->data)->dkey > dkey)
1039             dkey = ((SPDesktop *) r->data)->dkey;
1040     }
1042     return dkey;
1047 SPDesktop *
1048 inkscape_next_desktop ()
1050     SPDesktop *d = NULL;
1051     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1053     if (dkey_current < inkscape_maximum_dkey()) {
1054         // find next existing
1055         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1056             d = inkscape_find_desktop_by_dkey (i);
1057             if (d) {
1058                 break;
1059             }
1060         }
1061     } else {
1062         // find first existing
1063         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1064             d = inkscape_find_desktop_by_dkey (i);
1065             if (d) {
1066                 break;
1067             }
1068         }
1069     }
1071     g_assert (d);
1073     return d;
1078 SPDesktop *
1079 inkscape_prev_desktop ()
1081     SPDesktop *d = NULL;
1082     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1084     if (dkey_current > 0) {
1085         // find prev existing
1086         for (signed int i = dkey_current - 1; i >= 0; i--) {
1087             d = inkscape_find_desktop_by_dkey (i);
1088             if (d) {
1089                 break;
1090             }
1091         }
1092     }
1093     if (!d) {
1094         // find last existing
1095         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1096     }
1098     g_assert (d);
1100     return d;
1105 void
1106 inkscape_switch_desktops_next ()
1108     inkscape_next_desktop()->presentWindow();
1113 void
1114 inkscape_switch_desktops_prev ()
1116     inkscape_prev_desktop()->presentWindow();
1121 void
1122 inkscape_dialogs_hide ()
1124     if (Inkscape::NSApplication::Application::getNewGui())
1125         Inkscape::NSApplication::Editor::hideDialogs();
1126     else
1127     {
1128         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1129         inkscape->dialogs_toggle = FALSE;
1130     }
1135 void
1136 inkscape_dialogs_unhide ()
1138     if (Inkscape::NSApplication::Application::getNewGui())
1139         Inkscape::NSApplication::Editor::unhideDialogs();
1140     else
1141     {
1142         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1143         inkscape->dialogs_toggle = TRUE;
1144     }
1149 void
1150 inkscape_dialogs_toggle ()
1152     if (inkscape->dialogs_toggle) {
1153         inkscape_dialogs_hide ();
1154     } else {
1155         inkscape_dialogs_unhide ();
1156     }
1159 void
1160 inkscape_external_change ()
1162     g_return_if_fail (inkscape != NULL);
1164     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1167 /**
1168  * fixme: These need probably signals too
1169  */
1170 void
1171 inkscape_add_document (SPDocument *document)
1173     g_return_if_fail (document != NULL);
1175     if (!Inkscape::NSApplication::Application::getNewGui())
1176     {
1177         // try to insert the pair into the list
1178         if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1179             //insert failed, this key (document) is already in the list
1180             for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1181                    iter != inkscape->document_set.end();
1182                    ++iter) {
1183                 if (iter->first == document) {
1184                     // found this document in list, increase its count
1185                     iter->second ++;
1186                 }
1187            }
1188         }
1189     }
1190     else
1191     {
1192         Inkscape::NSApplication::Editor::addDocument (document);
1193     }
1197 // returns true if this was last reference to this document, so you can delete it
1198 bool
1199 inkscape_remove_document (SPDocument *document)
1201     g_return_val_if_fail (document != NULL, false);
1203     if (!Inkscape::NSApplication::Application::getNewGui())
1204     {
1205         for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1206                   iter != inkscape->document_set.end();
1207                   ++iter) {
1208             if (iter->first == document) {
1209                 // found this document in list, decrease its count
1210                 iter->second --;
1211                 if (iter->second < 1) {
1212                     // this was the last one, remove the pair from list
1213                     inkscape->document_set.erase (iter);
1214                     return true;
1215                 } else {
1216                     return false;
1217                 }
1218             }
1219         }
1220     }
1221     else
1222     {
1223         Inkscape::NSApplication::Editor::removeDocument (document);
1224     }
1226     return false;
1229 SPDesktop *
1230 inkscape_active_desktop (void)
1232     if (Inkscape::NSApplication::Application::getNewGui())
1233         return Inkscape::NSApplication::Editor::getActiveDesktop();
1235     if (inkscape->desktops == NULL) {
1236         return NULL;
1237     }
1239     return (SPDesktop *) inkscape->desktops->data;
1242 SPDocument *
1243 inkscape_active_document (void)
1245     if (Inkscape::NSApplication::Application::getNewGui())
1246         return Inkscape::NSApplication::Editor::getActiveDocument();
1248     if (SP_ACTIVE_DESKTOP) {
1249         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1250     }
1252     return NULL;
1255 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1256     SPDocument const* document = desktop.doc();
1257     if (!document) {
1258         return false;
1259     }
1260     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1261         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1262         SPDocument *other_document=other_desktop->doc();
1263         if ( other_document == document && other_desktop != &desktop ) {
1264             return false;
1265         }
1266     }
1267     return true;
1270 SPEventContext *
1271 inkscape_active_event_context (void)
1273     if (SP_ACTIVE_DESKTOP) {
1274         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1275     }
1277     return NULL;
1282 /*#####################
1283 # HELPERS
1284 #####################*/
1286 void
1287 inkscape_refresh_display (Inkscape::Application *inkscape)
1289     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1290         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1291     }
1295 /**
1296  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1297  *  saves the preferences if appropriate, and quits.
1298  */
1299 void
1300 inkscape_exit (Inkscape::Application */*inkscape*/)
1302     g_assert (INKSCAPE);
1304     //emit shutdown signal so that dialogs could remember layout
1305     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1307     Inkscape::Preferences::unload();
1308     gtk_main_quit ();
1311 char *
1312 homedir_path(const char *filename)
1314     static const gchar *homedir = NULL;
1315     if (!homedir) {
1316         homedir = g_get_home_dir();
1317     }
1318     if (!homedir) {
1319         homedir = g_path_get_dirname(INKSCAPE->argv0);
1320     }
1321     return g_build_filename(homedir, filename, NULL);
1325 /**
1326  * Get, or guess, or decide the location where the preferences.xml
1327  * file should be located.
1328  */
1329 gchar *
1330 profile_path(const char *filename)
1332     static const gchar *prefdir = NULL;
1333     if (!prefdir) {
1334 #ifdef HAS_SHGetSpecialFolderLocation
1335         // prefer c:\Documents and Settings\UserName\Application Data\ to
1336         // c:\Documents and Settings\userName\;
1337         if (!prefdir) {
1338             ITEMIDLIST *pidl = 0;
1339             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1340                 gchar * utf8Path = NULL;
1342                 if ( PrintWin32::is_os_wide() ) {
1343                     wchar_t pathBuf[MAX_PATH+1];
1344                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1346                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1347                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1348                     }
1349                 } else {
1350                     char pathBuf[MAX_PATH+1];
1352                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1353                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1354                     }
1355                 }
1357                 if ( utf8Path ) {
1358                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1359                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1360                         g_free( utf8Path );
1361                         utf8Path = 0;
1362                     } else {
1363                         prefdir = utf8Path;
1364                     }
1365                 }
1368                 /* not compiling yet...
1370                 // Remember to free the list pointer
1371                 IMalloc * imalloc = 0;
1372                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1373                     imalloc->lpVtbl->Free( imalloc, pidl );
1374                     imalloc->lpVtbl->Release( imalloc );
1375                 }
1376                 */
1377             }
1379             if (prefdir) {
1380                 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1381             }
1382         }
1383 #endif
1384         if (!prefdir) {
1385             prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1386             gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1388             // TODO here is a point to hook in preference migration
1390             if ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) ) {
1391                 prefdir = legacyDir;
1392             } else {
1393                 g_free(legacyDir);
1394                 legacyDir = 0;
1395             }
1396         }
1397     }
1398     return g_build_filename(prefdir, filename, NULL);
1401 Inkscape::XML::Node *
1402 inkscape_get_menus (Inkscape::Application * inkscape)
1404     Inkscape::XML::Node *repr = inkscape->menus->root();
1405     g_assert (!(strcmp (repr->name(), "inkscape")));
1406     return repr->firstChild();
1409 void
1410 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1412     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1413         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1414     }
1419 /*
1420   Local Variables:
1421   mode:c++
1422   c-file-style:"stroustrup"
1423   c-file-offsets:((innamespace . 0)(inline-open . 0))
1424   indent-tabs-mode:nil
1425   fill-column:99
1426   End:
1427 */
1428 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :