Code

Activate desktops upon creation; this fixes bug #195373 but please double check that...
[inkscape.git] / src / inkscape.cpp
1 #define __INKSCAPE_C__
3 /*
4  * Interface to main application
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 1999-2005 authors
11  * g++ port Copyright (C) 2003 Nathan Hurst
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <set>
22 #include "debug/simple-event.h"
23 #include "debug/event-tracker.h"
25 #ifndef WIN32
26 # define HAS_PROC_SELF_EXE  //to get path of executable
27 #else
29 // For now to get at is_os_wide().
30 # include "extension/internal/win32.h"
31 using Inkscape::Extension::Internal::PrintWin32;
33 #define _WIN32_IE 0x0400
34 //#define HAS_SHGetSpecialFolderPath
35 #define HAS_SHGetSpecialFolderLocation
36 #define HAS_GetModuleFileName
37 # include <shlobj.h>
38 #endif
40 #include <signal.h>
42 #include <gtk/gtkmain.h>
43 #include <gtk/gtkmessagedialog.h>
45 #include <glibmm/i18n.h>
46 #include <string>
47 #include <cstring>
48 #include "helper/sp-marshal.h"
49 #include "dialogs/debugdialog.h"
50 #include "application/application.h"
51 #include "application/editor.h"
52 #include "preferences.h"
55 #include "document.h"
56 #include "desktop.h"
57 #include "desktop-handles.h"
58 #include "selection.h"
59 #include "event-context.h"
60 #include "inkscape-private.h"
61 #include "prefs-utils.h"
62 #include "xml/repr.h"
63 #include "io/sys.h"
65 #include "extension/init.h"
67 static Inkscape::Application *inkscape = NULL;
69 /* Backbones of configuration xml data */
70 #include "menus-skeleton.h"
72 enum {
73     MODIFY_SELECTION, // global: one of selections modified
74     CHANGE_SELECTION, // global: one of selections changed
75     CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
76     SET_SELECTION, // global: one of selections set
77     SET_EVENTCONTEXT, // tool switched
78     ACTIVATE_DESKTOP, // some desktop got focus
79     DEACTIVATE_DESKTOP, // some desktop lost focus
80     SHUTDOWN_SIGNAL, // inkscape is quitting
81     DIALOGS_HIDE, // user pressed F12
82     DIALOGS_UNHIDE, // user pressed F12
83     EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
84                      // may not be reflected by a selection change and thus needs a separate signal
85     LAST_SIGNAL
86 };
88 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
91 /*################################
92 # FORWARD DECLARATIONS
93 ################################*/
95 gboolean inkscape_app_use_gui( Inkscape::Application const * app );
97 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
98 static void inkscape_init (SPObject *object);
99 static void inkscape_dispose (GObject *object);
101 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
102 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
104 static bool inkscape_init_config (Inkscape::XML::Document *doc, const gchar *config_name, const gchar *skeleton,
105                                   unsigned int skel_size,
106                                   const gchar *e_mkdir,
107                                   const gchar *e_notdir,
108                                   const gchar *e_ccf,
109                                   const gchar *e_cwf,
110                                   const gchar *warn);
112 struct Inkscape::Application {
113     GObject object;
114     Inkscape::XML::Document *menus;
115     std::multiset<SPDocument *> document_set;
116     GSList *documents;
117     GSList *desktops;
118     gchar *argv0;
119     gboolean dialogs_toggle;
120     gboolean use_gui;         // may want to consider a virtual function
121                               // for overriding things like the warning dlg's
122     guint mapalt;
123 };
125 struct Inkscape::ApplicationClass {
126     GObjectClass object_class;
128     /* Signals */
129     void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
130     void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
131     void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
132     void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
133     void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
134     void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
135     void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
136     void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
137     void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
138     void (* shut_down) (Inkscape::Application *inkscape);
139     void (* dialogs_hide) (Inkscape::Application *inkscape);
140     void (* dialogs_unhide) (Inkscape::Application *inkscape);
141     void (* external_change) (Inkscape::Application *inkscape);
142 };
144 static GObjectClass * parent_class;
145 static guint inkscape_signals[LAST_SIGNAL] = {0};
147 static void (* segv_handler) (int) = SIG_DFL;
148 static void (* abrt_handler) (int) = SIG_DFL;
149 static void (* fpe_handler)  (int) = SIG_DFL;
150 static void (* ill_handler)  (int) = SIG_DFL;
151 static void (* bus_handler)  (int) = SIG_DFL;
153 #ifdef WIN32
154 #define INKSCAPE_PROFILE_DIR "Inkscape"
155 #else
156 #define INKSCAPE_PROFILE_DIR ".inkscape"
157 #endif
159 #define MENUS_FILE "menus.xml"
162 /**
163  *  Retrieves the GType for the Inkscape Application object.
164  */
165 GType
166 inkscape_get_type (void)
168     static GType type = 0;
169     if (!type) {
170         GTypeInfo info = {
171             sizeof (Inkscape::ApplicationClass),
172             NULL, NULL,
173             (GClassInitFunc) inkscape_class_init,
174             NULL, NULL,
175             sizeof (Inkscape::Application),
176             4,
177             (GInstanceInitFunc) inkscape_init,
178             NULL
179         };
180         type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
181     }
182     return type;
186 /**
187  *  Initializes the inkscape class, registering all of its signal handlers
188  *  and virtual functions
189  */
190 static void
191 inkscape_class_init (Inkscape::ApplicationClass * klass)
193     GObjectClass * object_class;
195     object_class = (GObjectClass *) klass;
197     parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
199     inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
200                                G_TYPE_FROM_CLASS (klass),
201                                G_SIGNAL_RUN_FIRST,
202                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
203                                NULL, NULL,
204                                sp_marshal_NONE__POINTER_UINT,
205                                G_TYPE_NONE, 2,
206                                G_TYPE_POINTER, G_TYPE_UINT);
207     inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
208                                G_TYPE_FROM_CLASS (klass),
209                                G_SIGNAL_RUN_FIRST,
210                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
211                                NULL, NULL,
212                                sp_marshal_NONE__POINTER,
213                                G_TYPE_NONE, 1,
214                                G_TYPE_POINTER);
215     inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
216                                G_TYPE_FROM_CLASS (klass),
217                                G_SIGNAL_RUN_FIRST,
218                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
219                                NULL, NULL,
220                                sp_marshal_NONE__POINTER,
221                                G_TYPE_NONE, 1,
222                                G_TYPE_POINTER);
223     inkscape_signals[SET_SELECTION] =    g_signal_new ("set_selection",
224                                G_TYPE_FROM_CLASS (klass),
225                                G_SIGNAL_RUN_FIRST,
226                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
227                                NULL, NULL,
228                                sp_marshal_NONE__POINTER,
229                                G_TYPE_NONE, 1,
230                                G_TYPE_POINTER);
231     inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
232                                G_TYPE_FROM_CLASS (klass),
233                                G_SIGNAL_RUN_FIRST,
234                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
235                                NULL, NULL,
236                                sp_marshal_NONE__POINTER,
237                                G_TYPE_NONE, 1,
238                                G_TYPE_POINTER);
239     inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
240                                G_TYPE_FROM_CLASS (klass),
241                                G_SIGNAL_RUN_FIRST,
242                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
243                                NULL, NULL,
244                                sp_marshal_NONE__POINTER,
245                                G_TYPE_NONE, 1,
246                                G_TYPE_POINTER);
247     inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
248                                G_TYPE_FROM_CLASS (klass),
249                                G_SIGNAL_RUN_FIRST,
250                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
251                                NULL, NULL,
252                                sp_marshal_NONE__POINTER,
253                                G_TYPE_NONE, 1,
254                                G_TYPE_POINTER);
255     inkscape_signals[SHUTDOWN_SIGNAL] =        g_signal_new ("shut_down",
256                                G_TYPE_FROM_CLASS (klass),
257                                G_SIGNAL_RUN_FIRST,
258                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
259                                NULL, NULL,
260                                g_cclosure_marshal_VOID__VOID,
261                                G_TYPE_NONE, 0);
262     inkscape_signals[DIALOGS_HIDE] =        g_signal_new ("dialogs_hide",
263                                G_TYPE_FROM_CLASS (klass),
264                                G_SIGNAL_RUN_FIRST,
265                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
266                                NULL, NULL,
267                                g_cclosure_marshal_VOID__VOID,
268                                G_TYPE_NONE, 0);
269     inkscape_signals[DIALOGS_UNHIDE] =        g_signal_new ("dialogs_unhide",
270                                G_TYPE_FROM_CLASS (klass),
271                                G_SIGNAL_RUN_FIRST,
272                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
273                                NULL, NULL,
274                                g_cclosure_marshal_VOID__VOID,
275                                G_TYPE_NONE, 0);
276     inkscape_signals[EXTERNAL_CHANGE] =   g_signal_new ("external_change",
277                                G_TYPE_FROM_CLASS (klass),
278                                G_SIGNAL_RUN_FIRST,
279                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
280                                NULL, NULL,
281                                g_cclosure_marshal_VOID__VOID,
282                                G_TYPE_NONE, 0);
284     object_class->dispose = inkscape_dispose;
286     klass->activate_desktop = inkscape_activate_desktop_private;
287     klass->deactivate_desktop = inkscape_deactivate_desktop_private;
291 static void
292 inkscape_init (SPObject * object)
294     if (!inkscape) {
295         inkscape = (Inkscape::Application *) object;
296     } else {
297         g_assert_not_reached ();
298     }
300     new (&inkscape->document_set) std::multiset<SPDocument *>();
302     inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
304     inkscape->documents = NULL;
305     inkscape->desktops = NULL;
307     inkscape->dialogs_toggle = TRUE;
309     inkscape->mapalt=GDK_MOD1_MASK;    
312 static void
313 inkscape_dispose (GObject *object)
315     Inkscape::Application *inkscape = (Inkscape::Application *) object;
317     while (inkscape->documents) {
318         // we don't otherwise unref, so why here?
319         sp_document_unref((SPDocument *)inkscape->documents->data);
320     }
322     g_assert (!inkscape->desktops);
324     Inkscape::Preferences::save();
326     if (inkscape->menus) {
327         /* fixme: This is not the best place */
328         Inkscape::GC::release(inkscape->menus);
329         inkscape->menus = NULL;
330     }
332     inkscape->document_set.~multiset();
334     G_OBJECT_CLASS (parent_class)->dispose (object);
336     gtk_main_quit ();
340 void
341 inkscape_ref (void)
343     if (inkscape)
344         g_object_ref (G_OBJECT (inkscape));
348 void
349 inkscape_unref (void)
351     if (inkscape)
352         g_object_unref (G_OBJECT (inkscape));
355 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
356 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
357 guint
358 inkscape_mapalt() {
359     return inkscape->mapalt;
362 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
363 void inkscape_mapalt(guint maskvalue)
365     if(maskvalue<2 || maskvalue> 5 ){  /* MOD5 is the highest defined in gdktypes.h */
366         inkscape->mapalt=0;
367     }else{
368         inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
369     }
372 static void
373 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
375     desktop->set_active (true);
379 static void
380 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
382     desktop->set_active (false);
386 /* fixme: This is EVIL, and belongs to main after all */
388 #define SP_INDENT 8
391 static void
392 inkscape_crash_handler (int /*signum*/)
394     using Inkscape::Debug::SimpleEvent;
395     using Inkscape::Debug::EventTracker;
396     using Inkscape::Debug::Logger;
398     static gint recursion = FALSE;
400     /* 
401      * reset all signal handlers: any further crashes should just be allowed
402      * to crash normally.
403      * */
404     signal (SIGSEGV, segv_handler );
405     signal (SIGABRT, abrt_handler );
406     signal (SIGFPE,  fpe_handler  );
407     signal (SIGILL,  ill_handler  );
408 #ifndef WIN32
409     signal (SIGBUS,  bus_handler  );
410 #endif
411     
412     /* Stop bizarre loops */
413     if (recursion) {
414         abort ();
415     }
416     recursion = TRUE;
418     EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
419     tracker.set<SimpleEvent<> >("emergency-save");
421     fprintf(stderr, "\nEmergency save activated!\n");
423     time_t sptime = time (NULL);
424     struct tm *sptm = localtime (&sptime);
425     gchar sptstr[256];
426     strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
428     gint count = 0;
429     GSList *savednames = NULL;
430     GSList *failednames = NULL;
431     for (GSList *l = inkscape->documents; l != NULL; l = l->next) {
432         SPDocument *doc;
433         Inkscape::XML::Node *repr;
434         doc = (SPDocument *) l->data;
435         repr = sp_document_repr_root (doc);
436         if (doc->isModifiedSinceSave()) {
437             const gchar *docname, *d0, *d;
438             gchar n[64], c[1024];
439             FILE *file;
441             /* originally, the document name was retrieved from
442              * the sodipod:docname attribute */
443             docname = doc->name;
444             if (docname) {
445                 /* fixme: Quick hack to remove emergency file suffix */
446                 d0 = strrchr ((char*)docname, '.');
447                 if (d0 && (d0 > docname)) {
448                     d0 = strrchr ((char*)(d0 - 1), '.');
449                     if (d0 && (d0 > docname)) {
450                         d = d0;
451                         while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
452                         if (*d) {
453                             memcpy (n, docname, MIN (d0 - docname - 1, 64));
454                             n[63] = '\0';
455                             docname = n;
456                         }
457                     }
458                 }
459             }
461             if (!docname || !*docname) docname = "emergency";
462             // try saving to the profile location
463             g_snprintf (c, 1024, "%.256s.%s.%d", docname, sptstr, count);
464             gchar * location = homedir_path(c);
465             Inkscape::IO::dump_fopen_call(location, "E");
466             file = Inkscape::IO::fopen_utf8name(location, "w");
467             g_free(location);
468             if (!file) {
469                 // try saving to /tmp
470                 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d", docname, sptstr, count);
471                 Inkscape::IO::dump_fopen_call(c, "G");
472                 file = Inkscape::IO::fopen_utf8name(c, "w");
473             }
474             if (!file) {
475                 // try saving to the current directory
476                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d", docname, sptstr, count);
477                 Inkscape::IO::dump_fopen_call(c, "F");
478                 file = Inkscape::IO::fopen_utf8name(c, "w");
479             }
480             if (file) {
481                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
482                 savednames = g_slist_prepend (savednames, g_strdup (c));
483                 fclose (file);
484             } else {
485                 docname = repr->attribute("sodipodi:docname");
486                 failednames = g_slist_prepend (failednames, (docname) ? g_strdup (docname) : g_strdup (_("Untitled document")));
487             }
488             count++;
489         }
490     }
492     savednames = g_slist_reverse (savednames);
493     failednames = g_slist_reverse (failednames);
494     if (savednames) {
495         fprintf (stderr, "\nEmergency save document locations:\n");
496         for (GSList *l = savednames; l != NULL; l = l->next) {
497             fprintf (stderr, "  %s\n", (gchar *) l->data);
498         }
499     }
500     if (failednames) {
501         fprintf (stderr, "\nFailed to do emergency save for documents:\n");
502         for (GSList *l = failednames; l != NULL; l = l->next) {
503             fprintf (stderr, "  %s\n", (gchar *) l->data);
504         }
505     }
507     Inkscape::Preferences::save();
509     fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
510     fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
511     fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
513     /* Show nice dialog box */
515     char const *istr = _("Inkscape encountered an internal error and will close now.\n");
516     char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
517     char const *fstr = _("Automatic backup of the following documents failed:\n");
518     gint nllen = strlen ("\n");
519     gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
520     for (GSList *l = savednames; l != NULL; l = l->next) {
521         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
522     }
523     for (GSList *l = failednames; l != NULL; l = l->next) {
524         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
525     }
526     len += 1;
527     gchar *b = g_new (gchar, len);
528     gint pos = 0;
529     len = strlen (istr);
530     memcpy (b + pos, istr, len);
531     pos += len;
532     if (savednames) {
533         len = strlen (sstr);
534         memcpy (b + pos, sstr, len);
535         pos += len;
536         for (GSList *l = savednames; l != NULL; l = l->next) {
537             memset (b + pos, ' ', SP_INDENT);
538             pos += SP_INDENT;
539             len = strlen ((gchar *) l->data);
540             memcpy (b + pos, l->data, len);
541             pos += len;
542             memcpy (b + pos, "\n", nllen);
543             pos += nllen;
544         }
545     }
546     if (failednames) {
547         len = strlen (fstr);
548         memcpy (b + pos, fstr, len);
549         pos += len;
550         for (GSList *l = failednames; l != NULL; l = l->next) {
551             memset (b + pos, ' ', SP_INDENT);
552             pos += SP_INDENT;
553             len = strlen ((gchar *) l->data);
554             memcpy (b + pos, l->data, len);
555             pos += len;
556             memcpy (b + pos, "\n", nllen);
557             pos += nllen;
558         }
559     }
560     *(b + pos) = '\0';
562     if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
563         GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
564         gtk_dialog_run (GTK_DIALOG (msgbox));
565         gtk_widget_destroy (msgbox);
566     }
567     else
568     {
569         g_message( "Error: %s", b );
570     }
571     g_free (b);
573     tracker.clear();
574     Logger::shutdown();
576     /* on exit, allow restored signal handler to take over and crash us */
581 void
582 inkscape_application_init (const gchar *argv0, gboolean use_gui)
584     inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
585     /* fixme: load application defaults */
587     segv_handler = signal (SIGSEGV, inkscape_crash_handler);
588     abrt_handler = signal (SIGABRT, inkscape_crash_handler);
589     fpe_handler  = signal (SIGFPE,  inkscape_crash_handler);
590     ill_handler  = signal (SIGILL,  inkscape_crash_handler);
591 #ifndef WIN32
592     bus_handler  = signal (SIGBUS,  inkscape_crash_handler);
593 #endif
595     inkscape->use_gui = use_gui;
596     inkscape->argv0 = g_strdup(argv0);
598     /* Attempt to load the preferences, and set the save_preferences flag to TRUE
599        if we could, or FALSE if we couldn't */
600     Inkscape::Preferences::load();
601     inkscape_load_menus(inkscape);
603     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
604          * Use only if use_gui is enabled 
605          */
606 #ifdef WIN32
607 #define DEFAULT_LOG_REDIRECT true
608 #else
609 #define DEFAULT_LOG_REDIRECT false
610 #endif
612     if (use_gui == TRUE && prefs_get_int_attribute("dialogs.debug", "redirect", DEFAULT_LOG_REDIRECT))
613     {
614                 Inkscape::UI::Dialogs::DebugDialog::getInstance()->captureLogMessages();
615     }
617     /* Check for global remapping of Alt key */
618     if(use_gui)
619     {
620         inkscape_mapalt(guint(prefs_get_int_attribute("options.mapalt","value",0)));
621     }
623     /* Initialize the extensions */
624     Inkscape::Extension::init();
626     return;
629 /**
630  *  Returns the current Inkscape::Application global object
631  */
632 Inkscape::Application *
633 inkscape_get_instance()
635         return inkscape;
638 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
640     return app->use_gui;
643 /**
644  * Preference management
645  * We use '.' as separator
646  *
647  * Returns TRUE if the config file was successfully loaded, FALSE if not.
648  */
649 bool
650 inkscape_load_config (const gchar *filename, Inkscape::XML::Document *config, const gchar *skeleton,
651                       unsigned int skel_size, const gchar *e_notreg, const gchar *e_notxml,
652                       const gchar *e_notsp, const gchar *warn)
654     gchar *fn = profile_path(filename);
655     if (!Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
656         bool result;
657         /* No such file */
658         result = inkscape_init_config (config, filename, skeleton,
659                                        skel_size,
660                                        _("Cannot create directory %s.\n%s"),
661                                        _("%s is not a valid directory.\n%s"),
662                                        _("Cannot create file %s.\n%s"),
663                                        _("Cannot write file %s.\n%s"),
664                                        _("Although Inkscape will run, it will use default settings,\n"
665                                          "and any changes made in preferences will not be saved."));
666         g_free (fn);
667         return result;
668     }
670     if (!Inkscape::IO::file_test(fn, G_FILE_TEST_IS_REGULAR)) {
671         /* Not a regular file */
672         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
673         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notreg, safeFn, warn);
674         gtk_dialog_run (GTK_DIALOG (w));
675         gtk_widget_destroy (w);
676         g_free(safeFn);
677         g_free (fn);
678         return false;
679     }
681     Inkscape::XML::Document *doc = sp_repr_read_file (fn, NULL);
682     if (doc == NULL) {
683         /* Not an valid xml file */
684         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
685         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notxml, safeFn, warn);
686         gtk_dialog_run (GTK_DIALOG (w));
687         gtk_widget_destroy (w);
688         g_free(safeFn);
689         g_free (fn);
690         return false;
691     }
693     Inkscape::XML::Node *root = doc->root();
694     if (strcmp (root->name(), "inkscape")) {
695         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
696         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notsp, safeFn, warn);
697         gtk_dialog_run (GTK_DIALOG (w));
698         gtk_widget_destroy (w);
699         Inkscape::GC::release(doc);
700         g_free(safeFn);
701         g_free (fn);
702         return false;
703     }
705     /** \todo this is a hack, need to figure out how to get
706      *        a reasonable merge working with the menus.xml file */
707     if (skel_size == MENUS_SKELETON_SIZE) {
708         if (INKSCAPE)
709             INKSCAPE->menus = doc;
710         doc = config;
711     } else {
712         config->root()->mergeFrom(doc->root(), "id");
713     }
715     Inkscape::GC::release(doc);
716     g_free (fn);
717     return true;
720 /**
721  *  Menus management
722  *
723  */
724 bool
725 inkscape_load_menus (Inkscape::Application *inkscape)
727     gchar *fn = profile_path(MENUS_FILE);
728     bool retval = false;
729     if (Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
730         retval = inkscape_load_config (MENUS_FILE,
731                                  inkscape->menus,
732                                  menus_skeleton,
733                                  MENUS_SKELETON_SIZE,
734                                  _("%s is not a regular file.\n%s"),
735                                  _("%s not a valid XML file, or\n"
736                                    "you don't have read permissions on it.\n%s"),
737                                  _("%s is not a valid menus file.\n%s"),
738                                  _("Inkscape will run with default menus.\n"
739                                    "New menus will not be saved."));
740     } else {
741         INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
742         if (INKSCAPE->menus != NULL)
743             retval = true;
744     }
745     g_free(fn);
746     return retval;
749 /**
750  * We use '.' as separator
751  * \param inkscape Unused
752  */
753 Inkscape::XML::Node *
754 inkscape_get_repr (Inkscape::Application *inkscape, const gchar *key)
756     if ( (key == NULL) || (inkscape == NULL) ) {
757         return NULL;
758     }
760     Inkscape::XML::Node *prefs = Inkscape::Preferences::get();
761     if ( !prefs ) {
762         return NULL;
763     }
765     Inkscape::XML::Node *repr = prefs->root();
766     if (!repr) return NULL;
767     g_assert (!(strcmp (repr->name(), "inkscape")));
769     gchar const *s = key;
770     while ((s) && (*s)) {
772         /* Find next name */
773         gchar const *e = strchr (s, '.');
774         guint len;
775         if (e) {
776             len = e++ - s;
777         } else {
778             len = strlen (s);
779         }
781         Inkscape::XML::Node* child;
782         for (child = repr->firstChild(); child != NULL; child = child->next()) {
783             gchar const *id = child->attribute("id");
784             if ((id) && (strlen (id) == len) && (!strncmp (id, s, len)))
785             {
786                 break;
787             }
788         }
789         if (child == NULL) {
790             return NULL;
791         }
793         repr = child;
794         s = e;
795     }
796     return repr;
801 void
802 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
804     if (Inkscape::NSApplication::Application::getNewGui()) {
805         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
806         return;
807     }
808     g_return_if_fail (selection != NULL);
810     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
811         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
812     }
816 void
817 inkscape_selection_changed (Inkscape::Selection * selection)
819     if (Inkscape::NSApplication::Application::getNewGui()) {
820         Inkscape::NSApplication::Editor::selectionChanged (selection);
821         return;
822     }
823     g_return_if_fail (selection != NULL);
825     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
826         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
827     }
830 void
831 inkscape_subselection_changed (SPDesktop *desktop)
833     if (Inkscape::NSApplication::Application::getNewGui()) {
834         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
835         return;
836     }
837     g_return_if_fail (desktop != NULL);
839     if (DESKTOP_IS_ACTIVE (desktop)) {
840         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
841     }
845 void
846 inkscape_selection_set (Inkscape::Selection * selection)
848     if (Inkscape::NSApplication::Application::getNewGui()) {
849         Inkscape::NSApplication::Editor::selectionSet (selection);
850         return;
851     }
852     g_return_if_fail (selection != NULL);
854     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
855         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
856         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
857     }
861 void
862 inkscape_eventcontext_set (SPEventContext * eventcontext)
864     if (Inkscape::NSApplication::Application::getNewGui()) {
865         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
866         return;
867     }
868     g_return_if_fail (eventcontext != NULL);
869     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
871     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
872         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
873     }
877 void
878 inkscape_add_desktop (SPDesktop * desktop)
880     g_return_if_fail (desktop != NULL);
882     if (Inkscape::NSApplication::Application::getNewGui())
883     {
884         Inkscape::NSApplication::Editor::addDesktop (desktop);
885         return;
886     }
887     g_return_if_fail (inkscape != NULL);
889     g_assert (!g_slist_find (inkscape->desktops, desktop));
891     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
893     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
894     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
895     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
896     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
901 void
902 inkscape_remove_desktop (SPDesktop * desktop)
904     g_return_if_fail (desktop != NULL);
905     if (Inkscape::NSApplication::Application::getNewGui())
906     {
907         Inkscape::NSApplication::Editor::removeDesktop (desktop);
908         return;
909     }
910     g_return_if_fail (inkscape != NULL);
912     g_assert (g_slist_find (inkscape->desktops, desktop));
914     if (DESKTOP_IS_ACTIVE (desktop)) {
915         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
916         if (inkscape->desktops->next != NULL) {
917             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
918             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
919             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
920             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
921             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
922             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
923             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
924         } else {
925             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
926             if (sp_desktop_selection(desktop))
927                 sp_desktop_selection(desktop)->clear();
928         }
929     }
931     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
933     // if this was the last desktop, shut down the program
934     if (inkscape->desktops == NULL) {
935         inkscape_exit (inkscape);
936     }
941 void
942 inkscape_activate_desktop (SPDesktop * desktop)
944     g_return_if_fail (desktop != NULL);
945     if (Inkscape::NSApplication::Application::getNewGui())
946     {
947         Inkscape::NSApplication::Editor::activateDesktop (desktop);
948         return;
949     }
950     g_return_if_fail (inkscape != NULL);
952     if (DESKTOP_IS_ACTIVE (desktop)) {
953         return;
954     }
956     g_assert (g_slist_find (inkscape->desktops, desktop));
958     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
960     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
962     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
963     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
965     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
966     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
967     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
968     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
972 /**
973  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
974  */
975 void
976 inkscape_reactivate_desktop (SPDesktop * desktop)
978     g_return_if_fail (desktop != NULL);
979     if (Inkscape::NSApplication::Application::getNewGui())
980     {
981         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
982         return;
983     }
984     g_return_if_fail (inkscape != NULL);
986     if (DESKTOP_IS_ACTIVE (desktop))
987         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
992 SPDesktop *
993 inkscape_find_desktop_by_dkey (unsigned int dkey)
995     for (GSList *r = inkscape->desktops; r; r = r->next) {
996         if (((SPDesktop *) r->data)->dkey == dkey)
997             return ((SPDesktop *) r->data);
998     }
999     return NULL;
1005 unsigned int
1006 inkscape_maximum_dkey()
1008     unsigned int dkey = 0;
1010     for (GSList *r = inkscape->desktops; r; r = r->next) {
1011         if (((SPDesktop *) r->data)->dkey > dkey)
1012             dkey = ((SPDesktop *) r->data)->dkey;
1013     }
1015     return dkey;
1020 SPDesktop *
1021 inkscape_next_desktop ()
1023     SPDesktop *d = NULL;
1024     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1026     if (dkey_current < inkscape_maximum_dkey()) {
1027         // find next existing
1028         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1029             d = inkscape_find_desktop_by_dkey (i);
1030             if (d) {
1031                 break;
1032             }
1033         }
1034     } else {
1035         // find first existing
1036         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1037             d = inkscape_find_desktop_by_dkey (i);
1038             if (d) {
1039                 break;
1040             }
1041         }
1042     }
1044     g_assert (d);
1046     return d;
1051 SPDesktop *
1052 inkscape_prev_desktop ()
1054     SPDesktop *d = NULL;
1055     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1057     if (dkey_current > 0) {
1058         // find prev existing
1059         for (signed int i = dkey_current - 1; i >= 0; i--) {
1060             d = inkscape_find_desktop_by_dkey (i);
1061             if (d) {
1062                 break;
1063             }
1064         }
1065     }
1066     if (!d) {
1067         // find last existing
1068         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1069     }
1071     g_assert (d);
1073     return d;
1078 void
1079 inkscape_switch_desktops_next ()
1081     inkscape_next_desktop()->presentWindow();
1086 void
1087 inkscape_switch_desktops_prev ()
1089     inkscape_prev_desktop()->presentWindow();
1094 void
1095 inkscape_dialogs_hide ()
1097     if (Inkscape::NSApplication::Application::getNewGui())
1098         Inkscape::NSApplication::Editor::hideDialogs();
1099     else
1100     {
1101         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1102         inkscape->dialogs_toggle = FALSE;
1103     }
1108 void
1109 inkscape_dialogs_unhide ()
1111     if (Inkscape::NSApplication::Application::getNewGui())
1112         Inkscape::NSApplication::Editor::unhideDialogs();
1113     else
1114     {
1115         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1116         inkscape->dialogs_toggle = TRUE;
1117     }
1122 void
1123 inkscape_dialogs_toggle ()
1125     if (inkscape->dialogs_toggle) {
1126         inkscape_dialogs_hide ();
1127     } else {
1128         inkscape_dialogs_unhide ();
1129     }
1132 void
1133 inkscape_external_change ()
1135     g_return_if_fail (inkscape != NULL);
1137     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1140 /**
1141  * fixme: These need probably signals too
1142  */
1143 void
1144 inkscape_add_document (SPDocument *document)
1146     g_return_if_fail (document != NULL);
1148     if (!Inkscape::NSApplication::Application::getNewGui())
1149     {
1150         if ( inkscape->document_set.find(document) == inkscape->document_set.end() ) {
1151     
1152             inkscape->documents = g_slist_append (inkscape->documents, document);
1153         }
1154         inkscape->document_set.insert(document);
1155     }
1156     else
1157     {
1158         Inkscape::NSApplication::Editor::addDocument (document);
1159     }
1164 void
1165 inkscape_remove_document (SPDocument *document)
1167     g_return_if_fail (document != NULL);
1169     if (!Inkscape::NSApplication::Application::getNewGui())
1170     {
1171         inkscape->document_set.erase(document);
1172         if ( inkscape->document_set.find(document) != inkscape->document_set.end() ) {
1173             inkscape->documents = g_slist_remove (inkscape->documents, document);
1174         }
1175     }
1176     else
1177     {
1178         Inkscape::NSApplication::Editor::removeDocument (document);
1179     }
1181     return;
1184 SPDesktop *
1185 inkscape_active_desktop (void)
1187     if (Inkscape::NSApplication::Application::getNewGui())
1188         return Inkscape::NSApplication::Editor::getActiveDesktop();
1190     if (inkscape->desktops == NULL) {
1191         return NULL;
1192     }
1194     return (SPDesktop *) inkscape->desktops->data;
1197 SPDocument *
1198 inkscape_active_document (void)
1200     if (Inkscape::NSApplication::Application::getNewGui())
1201         return Inkscape::NSApplication::Editor::getActiveDocument();
1203     if (SP_ACTIVE_DESKTOP) {
1204         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1205     }
1207     return NULL;
1210 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1211     SPDocument const* document = desktop.doc();
1212     if (!document) {
1213         return false;
1214     }
1215     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1216         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1217         SPDocument *other_document=other_desktop->doc();
1218         if ( other_document == document && other_desktop != &desktop ) {
1219             return false;
1220         }
1221     }
1222     return true;
1225 SPEventContext *
1226 inkscape_active_event_context (void)
1228     if (SP_ACTIVE_DESKTOP) {
1229         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1230     }
1232     return NULL;
1237 /*#####################
1238 # HELPERS
1239 #####################*/
1241 static bool
1242 inkscape_init_config (Inkscape::XML::Document */*doc*/, const gchar *config_name, const gchar *skeleton,
1243                       unsigned int skel_size,
1244                       const gchar *e_mkdir,
1245                       const gchar *e_notdir,
1246                       const gchar *e_ccf,
1247                       const gchar *e_cwf,
1248                       const gchar *warn)
1250     gchar *dn = profile_path(NULL);
1251     bool use_gui = (Inkscape::NSApplication::Application::getNewGui())? Inkscape::NSApplication::Application::getUseGui() : inkscape->use_gui;
1252     if (!Inkscape::IO::file_test(dn, G_FILE_TEST_EXISTS)) {
1253         if (Inkscape::IO::mkdir_utf8name(dn))
1254         {
1255             if (use_gui) {
1256                 // Cannot create directory
1257                 gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1258                 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_mkdir, safeDn, warn);
1259                 gtk_dialog_run (GTK_DIALOG (w));
1260                 gtk_widget_destroy (w);
1261                 g_free(safeDn);
1262                 g_free (dn);
1263                 return false;
1264             } else {
1265                 g_warning(e_mkdir, dn, warn);
1266                 g_free (dn);
1267                 return false;
1268             }
1269         }
1271         // Also create (empty for now) subdirectories for the user's stuff
1272         {
1273             gchar *temp_dn = profile_path("templates");
1274             Inkscape::IO::mkdir_utf8name(temp_dn);
1275         }
1276         {
1277             gchar *temp_dn = profile_path("keys");
1278             Inkscape::IO::mkdir_utf8name(temp_dn);
1279         }
1280         {
1281             gchar *temp_dn = profile_path("icons");
1282             Inkscape::IO::mkdir_utf8name(temp_dn);
1283         }
1284         {
1285             gchar *temp_dn = profile_path("extensions");
1286             Inkscape::IO::mkdir_utf8name(temp_dn);
1287         }
1288         {
1289             gchar *temp_dn = profile_path("palettes");
1290             Inkscape::IO::mkdir_utf8name(temp_dn);
1291         }
1293     } else if (!Inkscape::IO::file_test(dn, G_FILE_TEST_IS_DIR)) {
1294         if (use_gui) {
1295             // Not a directory
1296             gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1297             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notdir, safeDn, warn);
1298             gtk_dialog_run (GTK_DIALOG (w));
1299             gtk_widget_destroy (w);
1300             g_free( safeDn );
1301             g_free (dn);
1302             return false;
1303         } else {
1304             g_warning(e_notdir, dn, warn);
1305             g_free(dn);
1306             return false;
1307         }
1308     }
1309     g_free (dn);
1311     gchar *fn = profile_path(config_name);
1313     Inkscape::IO::dump_fopen_call(fn, "H");
1314     FILE *fh = Inkscape::IO::fopen_utf8name(fn, "w");
1315     if (!fh) {
1316         if (use_gui) {
1317             /* Cannot create file */
1318             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1319             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_ccf, safeFn, warn);
1320             gtk_dialog_run (GTK_DIALOG (w));
1321             gtk_widget_destroy (w);
1322             g_free(safeFn);
1323             g_free (fn);
1324             return false;
1325         } else {
1326             g_warning(e_ccf, fn, warn);
1327             g_free(fn);
1328             return false;
1329         }
1330     }
1331     if ( fwrite(skeleton, 1, skel_size, fh) != skel_size ) {
1332         if (use_gui) {
1333             /* Cannot create file */
1334             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1335             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_cwf, safeFn, warn);
1336             gtk_dialog_run (GTK_DIALOG (w));
1337             gtk_widget_destroy (w);
1338             g_free(safeFn);
1339             g_free (fn);
1340             fclose(fh);
1341             return false;
1342         } else {
1343             g_warning(e_cwf, fn, warn);
1344             g_free(fn);
1345             fclose(fh);
1346             return false;
1347         }
1348     }
1350     g_free(fn);
1351     fclose(fh);
1352     return true;
1355 void
1356 inkscape_refresh_display (Inkscape::Application *inkscape)
1358     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1359         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1360     }
1364 /**
1365  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1366  *  saves the preferences if appropriate, and quits.
1367  */
1368 void
1369 inkscape_exit (Inkscape::Application */*inkscape*/)
1371     g_assert (INKSCAPE);
1373     //emit shutdown signal so that dialogs could remember layout
1374     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1376     Inkscape::Preferences::save();
1377     gtk_main_quit ();
1380 gchar *
1381 homedir_path(const char *filename)
1383     static const gchar *homedir = NULL;
1384     if (!homedir) {
1385         homedir = g_get_home_dir();
1386         gchar* utf8Path = g_filename_to_utf8( homedir, -1, NULL, NULL, NULL );
1387         if ( utf8Path )
1388         {
1389                 homedir = utf8Path;
1390                 if (!g_utf8_validate(homedir, -1, NULL)) {
1391                     g_warning( "g_get_home_dir() post A IS NOT UTF-8" );
1392                 }
1393         }
1394     }
1395     if (!homedir) {
1396         gchar * path = g_path_get_dirname(INKSCAPE->argv0);
1397         gchar* utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
1398         g_free(path);
1399         if ( utf8Path )
1400         {
1401             homedir = utf8Path;
1402             if (!g_utf8_validate(homedir, -1, NULL)) {
1403                 g_warning( "g_get_home_dir() post B IS NOT UTF-8" );
1404             }
1405         }
1406     }
1407     return g_build_filename(homedir, filename, NULL);
1411 /**
1412  * Get, or guess, or decide the location where the preferences.xml
1413  * file should be located.
1414  */
1415 gchar *
1416 profile_path(const char *filename)
1418     static const gchar *prefdir = NULL;
1419     if (!prefdir) {
1420 #ifdef HAS_SHGetSpecialFolderLocation
1421         // prefer c:\Documents and Settings\UserName\Application Data\ to
1422         // c:\Documents and Settings\userName\;
1423         if (!prefdir) {
1424             ITEMIDLIST *pidl = 0;
1425             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1426                 gchar * utf8Path = NULL;
1428                 if ( PrintWin32::is_os_wide() ) {
1429                     wchar_t pathBuf[MAX_PATH+1];
1430                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1432                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1433                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1434                     }
1435                 } else {
1436                     char pathBuf[MAX_PATH+1];
1438                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1439                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1440                     }
1441                 }
1443                 if ( utf8Path ) {
1444                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1445                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1446                         g_free( utf8Path );
1447                         utf8Path = 0;
1448                     } else {
1449                         prefdir = utf8Path;
1450                     }
1451                 }
1454                 /* not compiling yet...
1456                 // Remember to free the list pointer
1457                 IMalloc * imalloc = 0;
1458                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1459                     imalloc->lpVtbl->Free( imalloc, pidl );
1460                     imalloc->lpVtbl->Release( imalloc );
1461                 }
1462                 */
1463             }
1464         }
1465 #endif
1466         if (!prefdir) {
1467             prefdir = homedir_path(NULL);
1468         }
1469     }
1470     return g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, filename, NULL);
1473 Inkscape::XML::Node *
1474 inkscape_get_menus (Inkscape::Application * inkscape)
1476     Inkscape::XML::Node *repr = inkscape->menus->root();
1477     g_assert (!(strcmp (repr->name(), "inkscape")));
1478     return repr->firstChild();
1481 void
1482 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1484     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1485         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1486     }
1491 /*
1492   Local Variables:
1493   mode:c++
1494   c-file-style:"stroustrup"
1495   c-file-offsets:((innamespace . 0)(inline-open . 0))
1496   indent-tabs-mode:nil
1497   fill-column:99
1498   End:
1499 */
1500 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :