Code

Hold perspectives on document level rather than globally; this corrects the changes...
[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 "helper/sp-marshal.h"
47 #include "dialogs/debugdialog.h"
48 #include "application/application.h"
49 #include "application/editor.h"
50 #include "preferences.h"
53 #include "document.h"
54 #include "desktop.h"
55 #include "desktop-handles.h"
56 #include "selection.h"
57 #include "event-context.h"
58 #include "inkscape-private.h"
59 #include "prefs-utils.h"
60 #include "xml/repr.h"
61 #include "io/sys.h"
63 #include "extension/init.h"
65 static Inkscape::Application *inkscape = NULL;
67 /* Backbones of configuration xml data */
68 #include "menus-skeleton.h"
70 enum {
71     MODIFY_SELECTION, // global: one of selections modified
72     CHANGE_SELECTION, // global: one of selections changed
73     CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
74     SET_SELECTION, // global: one of selections set
75     SET_EVENTCONTEXT, // tool switched
76     ACTIVATE_DESKTOP, // some desktop got focus
77     DEACTIVATE_DESKTOP, // some desktop lost focus
78     SHUTDOWN_SIGNAL, // inkscape is quitting
79     DIALOGS_HIDE, // user pressed F12
80     DIALOGS_UNHIDE, // user pressed F12
81     EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
82                      // may not be reflected by a selection change and thus needs a separate signal
83     LAST_SIGNAL
84 };
86 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
89 /*################################
90 # FORWARD DECLARATIONS
91 ################################*/
93 gboolean inkscape_app_use_gui( Inkscape::Application const * app );
95 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
96 static void inkscape_init (SPObject *object);
97 static void inkscape_dispose (GObject *object);
99 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
100 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
102 static bool inkscape_init_config (Inkscape::XML::Document *doc, const gchar *config_name, const gchar *skeleton,
103                                   unsigned int skel_size,
104                                   const gchar *e_mkdir,
105                                   const gchar *e_notdir,
106                                   const gchar *e_ccf,
107                                   const gchar *e_cwf,
108                                   const gchar *warn);
110 struct Inkscape::Application {
111     GObject object;
112     Inkscape::XML::Document *menus;
113     std::multiset<SPDocument *> document_set;
114     GSList *documents;
115     GSList *desktops;
116     gchar *argv0;
117     gboolean dialogs_toggle;
118     gboolean use_gui;         // may want to consider a virtual function
119                               // for overriding things like the warning dlg's
120 };
122 struct Inkscape::ApplicationClass {
123     GObjectClass object_class;
125     /* Signals */
126     void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
127     void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
128     void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
129     void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
130     void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
131     void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
132     void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
133     void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
134     void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
135     void (* shut_down) (Inkscape::Application *inkscape);
136     void (* dialogs_hide) (Inkscape::Application *inkscape);
137     void (* dialogs_unhide) (Inkscape::Application *inkscape);
138     void (* external_change) (Inkscape::Application *inkscape);
139 };
141 static GObjectClass * parent_class;
142 static guint inkscape_signals[LAST_SIGNAL] = {0};
144 static void (* segv_handler) (int) = SIG_DFL;
145 static void (* abrt_handler) (int) = SIG_DFL;
146 static void (* fpe_handler)  (int) = SIG_DFL;
147 static void (* ill_handler)  (int) = SIG_DFL;
148 static void (* bus_handler)  (int) = SIG_DFL;
150 #ifdef WIN32
151 #define INKSCAPE_PROFILE_DIR "Inkscape"
152 #else
153 #define INKSCAPE_PROFILE_DIR ".inkscape"
154 #endif
156 #define MENUS_FILE "menus.xml"
159 /**
160  *  Retrieves the GType for the Inkscape Application object.
161  */
162 GType
163 inkscape_get_type (void)
165     static GType type = 0;
166     if (!type) {
167         GTypeInfo info = {
168             sizeof (Inkscape::ApplicationClass),
169             NULL, NULL,
170             (GClassInitFunc) inkscape_class_init,
171             NULL, NULL,
172             sizeof (Inkscape::Application),
173             4,
174             (GInstanceInitFunc) inkscape_init,
175             NULL
176         };
177         type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
178     }
179     return type;
183 /**
184  *  Initializes the inkscape class, registering all of its signal handlers
185  *  and virtual functions
186  */
187 static void
188 inkscape_class_init (Inkscape::ApplicationClass * klass)
190     GObjectClass * object_class;
192     object_class = (GObjectClass *) klass;
194     parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
196     inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
197                                G_TYPE_FROM_CLASS (klass),
198                                G_SIGNAL_RUN_FIRST,
199                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
200                                NULL, NULL,
201                                sp_marshal_NONE__POINTER_UINT,
202                                G_TYPE_NONE, 2,
203                                G_TYPE_POINTER, G_TYPE_UINT);
204     inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
205                                G_TYPE_FROM_CLASS (klass),
206                                G_SIGNAL_RUN_FIRST,
207                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
208                                NULL, NULL,
209                                sp_marshal_NONE__POINTER,
210                                G_TYPE_NONE, 1,
211                                G_TYPE_POINTER);
212     inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
213                                G_TYPE_FROM_CLASS (klass),
214                                G_SIGNAL_RUN_FIRST,
215                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
216                                NULL, NULL,
217                                sp_marshal_NONE__POINTER,
218                                G_TYPE_NONE, 1,
219                                G_TYPE_POINTER);
220     inkscape_signals[SET_SELECTION] =    g_signal_new ("set_selection",
221                                G_TYPE_FROM_CLASS (klass),
222                                G_SIGNAL_RUN_FIRST,
223                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
224                                NULL, NULL,
225                                sp_marshal_NONE__POINTER,
226                                G_TYPE_NONE, 1,
227                                G_TYPE_POINTER);
228     inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
229                                G_TYPE_FROM_CLASS (klass),
230                                G_SIGNAL_RUN_FIRST,
231                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
232                                NULL, NULL,
233                                sp_marshal_NONE__POINTER,
234                                G_TYPE_NONE, 1,
235                                G_TYPE_POINTER);
236     inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
237                                G_TYPE_FROM_CLASS (klass),
238                                G_SIGNAL_RUN_FIRST,
239                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
240                                NULL, NULL,
241                                sp_marshal_NONE__POINTER,
242                                G_TYPE_NONE, 1,
243                                G_TYPE_POINTER);
244     inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
245                                G_TYPE_FROM_CLASS (klass),
246                                G_SIGNAL_RUN_FIRST,
247                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
248                                NULL, NULL,
249                                sp_marshal_NONE__POINTER,
250                                G_TYPE_NONE, 1,
251                                G_TYPE_POINTER);
252     inkscape_signals[SHUTDOWN_SIGNAL] =        g_signal_new ("shut_down",
253                                G_TYPE_FROM_CLASS (klass),
254                                G_SIGNAL_RUN_FIRST,
255                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
256                                NULL, NULL,
257                                g_cclosure_marshal_VOID__VOID,
258                                G_TYPE_NONE, 0);
259     inkscape_signals[DIALOGS_HIDE] =        g_signal_new ("dialogs_hide",
260                                G_TYPE_FROM_CLASS (klass),
261                                G_SIGNAL_RUN_FIRST,
262                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
263                                NULL, NULL,
264                                g_cclosure_marshal_VOID__VOID,
265                                G_TYPE_NONE, 0);
266     inkscape_signals[DIALOGS_UNHIDE] =        g_signal_new ("dialogs_unhide",
267                                G_TYPE_FROM_CLASS (klass),
268                                G_SIGNAL_RUN_FIRST,
269                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
270                                NULL, NULL,
271                                g_cclosure_marshal_VOID__VOID,
272                                G_TYPE_NONE, 0);
273     inkscape_signals[EXTERNAL_CHANGE] =   g_signal_new ("external_change",
274                                G_TYPE_FROM_CLASS (klass),
275                                G_SIGNAL_RUN_FIRST,
276                                G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
277                                NULL, NULL,
278                                g_cclosure_marshal_VOID__VOID,
279                                G_TYPE_NONE, 0);
281     object_class->dispose = inkscape_dispose;
283     klass->activate_desktop = inkscape_activate_desktop_private;
284     klass->deactivate_desktop = inkscape_deactivate_desktop_private;
288 static void
289 inkscape_init (SPObject * object)
291     if (!inkscape) {
292         inkscape = (Inkscape::Application *) object;
293     } else {
294         g_assert_not_reached ();
295     }
297     new (&inkscape->document_set) std::multiset<SPDocument *>();
299     inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
301     inkscape->documents = NULL;
302     inkscape->desktops = NULL;
304     inkscape->dialogs_toggle = TRUE;
308 static void
309 inkscape_dispose (GObject *object)
311     Inkscape::Application *inkscape = (Inkscape::Application *) object;
313     while (inkscape->documents) {
314         // we don't otherwise unref, so why here?
315         sp_document_unref((SPDocument *)inkscape->documents->data);
316     }
318     g_assert (!inkscape->desktops);
320     Inkscape::Preferences::save();
322     if (inkscape->menus) {
323         /* fixme: This is not the best place */
324         Inkscape::GC::release(inkscape->menus);
325         inkscape->menus = NULL;
326     }
328     inkscape->document_set.~multiset();
330     G_OBJECT_CLASS (parent_class)->dispose (object);
332     gtk_main_quit ();
336 void
337 inkscape_ref (void)
339     if (inkscape)
340         g_object_ref (G_OBJECT (inkscape));
344 void
345 inkscape_unref (void)
347     if (inkscape)
348         g_object_unref (G_OBJECT (inkscape));
352 static void
353 inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop)
355     desktop->set_active (true);
359 static void
360 inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop)
362     desktop->set_active (false);
366 /* fixme: This is EVIL, and belongs to main after all */
368 #define SP_INDENT 8
371 static void
372 inkscape_crash_handler (int signum)
374     using Inkscape::Debug::SimpleEvent;
375     using Inkscape::Debug::EventTracker;
376     using Inkscape::Debug::Logger;
378     static gint recursion = FALSE;
380     /* 
381      * reset all signal handlers: any further crashes should just be allowed
382      * to crash normally.
383      * */
384     signal (SIGSEGV, segv_handler );
385     signal (SIGABRT, abrt_handler );
386     signal (SIGFPE,  fpe_handler  );
387     signal (SIGILL,  ill_handler  );
388 #ifndef WIN32
389     signal (SIGBUS,  bus_handler  );
390 #endif
391     
392     /* Stop bizarre loops */
393     if (recursion) {
394         abort ();
395     }
396     recursion = TRUE;
398     EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
399     tracker.set<SimpleEvent<> >("emergency-save");
401     fprintf(stderr, "\nEmergency save activated!\n");
403     time_t sptime = time (NULL);
404     struct tm *sptm = localtime (&sptime);
405     gchar sptstr[256];
406     strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
408     gint count = 0;
409     GSList *savednames = NULL;
410     GSList *failednames = NULL;
411     for (GSList *l = inkscape->documents; l != NULL; l = l->next) {
412         SPDocument *doc;
413         Inkscape::XML::Node *repr;
414         doc = (SPDocument *) l->data;
415         repr = sp_document_repr_root (doc);
416         if (repr->attribute("sodipodi:modified")) {
417             const gchar *docname, *d0, *d;
418             gchar n[64], c[1024];
419             FILE *file;
421             /* originally, the document name was retrieved from
422              * the sodipod:docname attribute */
423             docname = doc->name;
424             if (docname) {
425                 /* fixme: Quick hack to remove emergency file suffix */
426                 d0 = strrchr ((char*)docname, '.');
427                 if (d0 && (d0 > docname)) {
428                     d0 = strrchr ((char*)(d0 - 1), '.');
429                     if (d0 && (d0 > docname)) {
430                         d = d0;
431                         while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
432                         if (*d) {
433                             memcpy (n, docname, MIN (d0 - docname - 1, 64));
434                             n[63] = '\0';
435                             docname = n;
436                         }
437                     }
438                 }
439             }
441             if (!docname || !*docname) docname = "emergency";
442             // try saving to the profile location
443             g_snprintf (c, 1024, "%.256s.%s.%d", docname, sptstr, count);
444             gchar * location = homedir_path(c);
445             Inkscape::IO::dump_fopen_call(location, "E");
446             file = Inkscape::IO::fopen_utf8name(location, "w");
447             g_free(location);
448             if (!file) {
449                 // try saving to /tmp
450                 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d", docname, sptstr, count);
451                 Inkscape::IO::dump_fopen_call(c, "G");
452                 file = Inkscape::IO::fopen_utf8name(c, "w");
453             }
454             if (!file) {
455                 // try saving to the current directory
456                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d", docname, sptstr, count);
457                 Inkscape::IO::dump_fopen_call(c, "F");
458                 file = Inkscape::IO::fopen_utf8name(c, "w");
459             }
460             if (file) {
461                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
462                 savednames = g_slist_prepend (savednames, g_strdup (c));
463                 fclose (file);
464             } else {
465                 docname = repr->attribute("sodipodi:docname");
466                 failednames = g_slist_prepend (failednames, (docname) ? g_strdup (docname) : g_strdup (_("Untitled document")));
467             }
468             count++;
469         }
470     }
472     savednames = g_slist_reverse (savednames);
473     failednames = g_slist_reverse (failednames);
474     if (savednames) {
475         fprintf (stderr, "\nEmergency save document locations:\n");
476         for (GSList *l = savednames; l != NULL; l = l->next) {
477             fprintf (stderr, "  %s\n", (gchar *) l->data);
478         }
479     }
480     if (failednames) {
481         fprintf (stderr, "\nFailed to do emergency save for documents:\n");
482         for (GSList *l = failednames; l != NULL; l = l->next) {
483             fprintf (stderr, "  %s\n", (gchar *) l->data);
484         }
485     }
487     Inkscape::Preferences::save();
489     fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
490     fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
491     fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
493     /* Show nice dialog box */
495     char const *istr = _("Inkscape encountered an internal error and will close now.\n");
496     char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
497     char const *fstr = _("Automatic backup of the following documents failed:\n");
498     gint nllen = strlen ("\n");
499     gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
500     for (GSList *l = savednames; l != NULL; l = l->next) {
501         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
502     }
503     for (GSList *l = failednames; l != NULL; l = l->next) {
504         len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
505     }
506     len += 1;
507     gchar *b = g_new (gchar, len);
508     gint pos = 0;
509     len = strlen (istr);
510     memcpy (b + pos, istr, len);
511     pos += len;
512     if (savednames) {
513         len = strlen (sstr);
514         memcpy (b + pos, sstr, len);
515         pos += len;
516         for (GSList *l = savednames; l != NULL; l = l->next) {
517             memset (b + pos, ' ', SP_INDENT);
518             pos += SP_INDENT;
519             len = strlen ((gchar *) l->data);
520             memcpy (b + pos, l->data, len);
521             pos += len;
522             memcpy (b + pos, "\n", nllen);
523             pos += nllen;
524         }
525     }
526     if (failednames) {
527         len = strlen (fstr);
528         memcpy (b + pos, fstr, len);
529         pos += len;
530         for (GSList *l = failednames; l != NULL; l = l->next) {
531             memset (b + pos, ' ', SP_INDENT);
532             pos += SP_INDENT;
533             len = strlen ((gchar *) l->data);
534             memcpy (b + pos, l->data, len);
535             pos += len;
536             memcpy (b + pos, "\n", nllen);
537             pos += nllen;
538         }
539     }
540     *(b + pos) = '\0';
542     if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
543         GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
544         gtk_dialog_run (GTK_DIALOG (msgbox));
545         gtk_widget_destroy (msgbox);
546     }
547     else
548     {
549         g_message( "Error: %s", b );
550     }
551     g_free (b);
553     tracker.clear();
554     Logger::shutdown();
556     /* on exit, allow restored signal handler to take over and crash us */
561 void
562 inkscape_application_init (const gchar *argv0, gboolean use_gui)
564     inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
565     /* fixme: load application defaults */
567     segv_handler = signal (SIGSEGV, inkscape_crash_handler);
568     abrt_handler = signal (SIGABRT, inkscape_crash_handler);
569     fpe_handler  = signal (SIGFPE,  inkscape_crash_handler);
570     ill_handler  = signal (SIGILL,  inkscape_crash_handler);
571 #ifndef WIN32
572     bus_handler  = signal (SIGBUS,  inkscape_crash_handler);
573 #endif
575     inkscape->use_gui = use_gui;
576     inkscape->argv0 = g_strdup(argv0);
578     /* Attempt to load the preferences, and set the save_preferences flag to TRUE
579        if we could, or FALSE if we couldn't */
580     Inkscape::Preferences::load();
581     inkscape_load_menus(inkscape);
583     /* DebugDialog redirection.  On Linux, default to OFF, on Win32, default to ON.
584          * Use only if use_gui is enabled 
585          */
586 #ifdef WIN32
587 #define DEFAULT_LOG_REDIRECT true
588 #else
589 #define DEFAULT_LOG_REDIRECT false
590 #endif
592     if (use_gui == TRUE && prefs_get_int_attribute("dialogs.debug", "redirect", DEFAULT_LOG_REDIRECT))
593     {
594                 Inkscape::UI::Dialogs::DebugDialog::getInstance()->captureLogMessages();
595     }
597     /* Initialize the extensions */
598     Inkscape::Extension::init();
600     return;
603 /**
604  *  Returns the current Inkscape::Application global object
605  */
606 Inkscape::Application *
607 inkscape_get_instance()
609         return inkscape;
612 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
614     return app->use_gui;
617 /**
618  * Preference management
619  * We use '.' as separator
620  *
621  * Returns TRUE if the config file was successfully loaded, FALSE if not.
622  */
623 bool
624 inkscape_load_config (const gchar *filename, Inkscape::XML::Document *config, const gchar *skeleton,
625                       unsigned int skel_size, const gchar *e_notreg, const gchar *e_notxml,
626                       const gchar *e_notsp, const gchar *warn)
628     gchar *fn = profile_path(filename);
629     if (!Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
630         bool result;
631         /* No such file */
632         result = inkscape_init_config (config, filename, skeleton,
633                                        skel_size,
634                                        _("Cannot create directory %s.\n%s"),
635                                        _("%s is not a valid directory.\n%s"),
636                                        _("Cannot create file %s.\n%s"),
637                                        _("Cannot write file %s.\n%s"),
638                                        _("Although Inkscape will run, it will use default settings,\n"
639                                          "and any changes made in preferences will not be saved."));
640         g_free (fn);
641         return result;
642     }
644     if (!Inkscape::IO::file_test(fn, G_FILE_TEST_IS_REGULAR)) {
645         /* Not a regular file */
646         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
647         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notreg, safeFn, warn);
648         gtk_dialog_run (GTK_DIALOG (w));
649         gtk_widget_destroy (w);
650         g_free(safeFn);
651         g_free (fn);
652         return false;
653     }
655     Inkscape::XML::Document *doc = sp_repr_read_file (fn, NULL);
656     if (doc == NULL) {
657         /* Not an valid xml file */
658         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
659         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notxml, safeFn, warn);
660         gtk_dialog_run (GTK_DIALOG (w));
661         gtk_widget_destroy (w);
662         g_free(safeFn);
663         g_free (fn);
664         return false;
665     }
667     Inkscape::XML::Node *root = doc->root();
668     if (strcmp (root->name(), "inkscape")) {
669         gchar *safeFn = Inkscape::IO::sanitizeString(fn);
670         GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notsp, safeFn, warn);
671         gtk_dialog_run (GTK_DIALOG (w));
672         gtk_widget_destroy (w);
673         Inkscape::GC::release(doc);
674         g_free(safeFn);
675         g_free (fn);
676         return false;
677     }
679     /** \todo this is a hack, need to figure out how to get
680      *        a reasonable merge working with the menus.xml file */
681     if (skel_size == MENUS_SKELETON_SIZE) {
682         if (INKSCAPE)
683             INKSCAPE->menus = doc;
684         doc = config;
685     } else {
686         config->root()->mergeFrom(doc->root(), "id");
687     }
689     Inkscape::GC::release(doc);
690     g_free (fn);
691     return true;
694 /**
695  *  Menus management
696  *
697  */
698 bool
699 inkscape_load_menus (Inkscape::Application *inkscape)
701     gchar *fn = profile_path(MENUS_FILE);
702     bool retval = false;
703     if (Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
704         retval = inkscape_load_config (MENUS_FILE,
705                                  inkscape->menus,
706                                  menus_skeleton,
707                                  MENUS_SKELETON_SIZE,
708                                  _("%s is not a regular file.\n%s"),
709                                  _("%s not a valid XML file, or\n"
710                                    "you don't have read permissions on it.\n%s"),
711                                  _("%s is not a valid menus file.\n%s"),
712                                  _("Inkscape will run with default menus.\n"
713                                    "New menus will not be saved."));
714     } else {
715         INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
716         if (INKSCAPE->menus != NULL)
717             retval = true;
718     }
719     g_free(fn);
720     return retval;
723 /**
724  * We use '.' as separator
725  * \param inkscape Unused
726  */
727 Inkscape::XML::Node *
728 inkscape_get_repr (Inkscape::Application *inkscape, const gchar *key)
730     if ( (key == NULL) || (inkscape == NULL) ) {
731         return NULL;
732     }
734     Inkscape::XML::Node *prefs = Inkscape::Preferences::get();
735     if ( !prefs ) {
736         return NULL;
737     }
739     Inkscape::XML::Node *repr = prefs->root();
740     if (!repr) return NULL;
741     g_assert (!(strcmp (repr->name(), "inkscape")));
743     gchar const *s = key;
744     while ((s) && (*s)) {
746         /* Find next name */
747         gchar const *e = strchr (s, '.');
748         guint len;
749         if (e) {
750             len = e++ - s;
751         } else {
752             len = strlen (s);
753         }
755         Inkscape::XML::Node* child;
756         for (child = repr->firstChild(); child != NULL; child = child->next()) {
757             gchar const *id = child->attribute("id");
758             if ((id) && (strlen (id) == len) && (!strncmp (id, s, len)))
759             {
760                 break;
761             }
762         }
763         if (child == NULL) {
764             return NULL;
765         }
767         repr = child;
768         s = e;
769     }
770     return repr;
775 void
776 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
778     if (Inkscape::NSApplication::Application::getNewGui()) {
779         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
780         return;
781     }
782     g_return_if_fail (selection != NULL);
784     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
785         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
786     }
790 void
791 inkscape_selection_changed (Inkscape::Selection * selection)
793     if (Inkscape::NSApplication::Application::getNewGui()) {
794         Inkscape::NSApplication::Editor::selectionChanged (selection);
795         return;
796     }
797     g_return_if_fail (selection != NULL);
799     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
800         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
801     }
804 void
805 inkscape_subselection_changed (SPDesktop *desktop)
807     if (Inkscape::NSApplication::Application::getNewGui()) {
808         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
809         return;
810     }
811     g_return_if_fail (desktop != NULL);
813     if (DESKTOP_IS_ACTIVE (desktop)) {
814         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
815     }
819 void
820 inkscape_selection_set (Inkscape::Selection * selection)
822     if (Inkscape::NSApplication::Application::getNewGui()) {
823         Inkscape::NSApplication::Editor::selectionSet (selection);
824         return;
825     }
826     g_return_if_fail (selection != NULL);
828     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
829         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
830         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
831     }
835 void
836 inkscape_eventcontext_set (SPEventContext * eventcontext)
838     if (Inkscape::NSApplication::Application::getNewGui()) {
839         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
840         return;
841     }
842     g_return_if_fail (eventcontext != NULL);
843     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
845     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
846         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
847     }
851 void
852 inkscape_add_desktop (SPDesktop * desktop)
854     g_return_if_fail (desktop != NULL);
856     if (Inkscape::NSApplication::Application::getNewGui())
857     {
858         Inkscape::NSApplication::Editor::addDesktop (desktop);
859         return;
860     }
861     g_return_if_fail (inkscape != NULL);
863     g_assert (!g_slist_find (inkscape->desktops, desktop));
865     inkscape->desktops = g_slist_append (inkscape->desktops, desktop);
867     if (DESKTOP_IS_ACTIVE (desktop)) {
868         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
869         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
870         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
871         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
872     }
877 void
878 inkscape_remove_desktop (SPDesktop * desktop)
880     g_return_if_fail (desktop != NULL);
881     if (Inkscape::NSApplication::Application::getNewGui())
882     {
883         Inkscape::NSApplication::Editor::removeDesktop (desktop);
884         return;
885     }
886     g_return_if_fail (inkscape != NULL);
888     g_assert (g_slist_find (inkscape->desktops, desktop));
890     if (DESKTOP_IS_ACTIVE (desktop)) {
891         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
892         if (inkscape->desktops->next != NULL) {
893             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
894             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
895             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
896             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
897             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
898             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
899             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
900         } else {
901             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
902             if (sp_desktop_selection(desktop))
903                 sp_desktop_selection(desktop)->clear();
904         }
905     }
907     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
909     // if this was the last desktop, shut down the program
910     if (inkscape->desktops == NULL) {
911         inkscape_exit (inkscape);
912     }
917 void
918 inkscape_activate_desktop (SPDesktop * desktop)
920     g_return_if_fail (desktop != NULL);
921     if (Inkscape::NSApplication::Application::getNewGui())
922     {
923         Inkscape::NSApplication::Editor::activateDesktop (desktop);
924         return;
925     }
926     g_return_if_fail (inkscape != NULL);
928     if (DESKTOP_IS_ACTIVE (desktop)) {
929         return;
930     }
932     g_assert (g_slist_find (inkscape->desktops, desktop));
934     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
936     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
938     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
939     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
941     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
942     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
943     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
944     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
948 /**
949  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
950  */
951 void
952 inkscape_reactivate_desktop (SPDesktop * desktop)
954     g_return_if_fail (desktop != NULL);
955     if (Inkscape::NSApplication::Application::getNewGui())
956     {
957         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
958         return;
959     }
960     g_return_if_fail (inkscape != NULL);
962     if (DESKTOP_IS_ACTIVE (desktop))
963         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
968 SPDesktop *
969 inkscape_find_desktop_by_dkey (unsigned int dkey)
971     for (GSList *r = inkscape->desktops; r; r = r->next) {
972         if (((SPDesktop *) r->data)->dkey == dkey)
973             return ((SPDesktop *) r->data);
974     }
975     return NULL;
981 unsigned int
982 inkscape_maximum_dkey()
984     unsigned int dkey = 0;
986     for (GSList *r = inkscape->desktops; r; r = r->next) {
987         if (((SPDesktop *) r->data)->dkey > dkey)
988             dkey = ((SPDesktop *) r->data)->dkey;
989     }
991     return dkey;
996 SPDesktop *
997 inkscape_next_desktop ()
999     SPDesktop *d = NULL;
1000     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1002     if (dkey_current < inkscape_maximum_dkey()) {
1003         // find next existing
1004         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1005             d = inkscape_find_desktop_by_dkey (i);
1006             if (d) {
1007                 break;
1008             }
1009         }
1010     } else {
1011         // find first existing
1012         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1013             d = inkscape_find_desktop_by_dkey (i);
1014             if (d) {
1015                 break;
1016             }
1017         }
1018     }
1020     g_assert (d);
1022     return d;
1027 SPDesktop *
1028 inkscape_prev_desktop ()
1030     SPDesktop *d = NULL;
1031     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1033     if (dkey_current > 0) {
1034         // find prev existing
1035         for (signed int i = dkey_current - 1; i >= 0; i--) {
1036             d = inkscape_find_desktop_by_dkey (i);
1037             if (d) {
1038                 break;
1039             }
1040         }
1041     }
1042     if (!d) {
1043         // find last existing
1044         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1045     }
1047     g_assert (d);
1049     return d;
1054 void
1055 inkscape_switch_desktops_next ()
1057     inkscape_next_desktop()->presentWindow();
1062 void
1063 inkscape_switch_desktops_prev ()
1065     inkscape_prev_desktop()->presentWindow();
1070 void
1071 inkscape_dialogs_hide ()
1073     if (Inkscape::NSApplication::Application::getNewGui())
1074         Inkscape::NSApplication::Editor::hideDialogs();
1075     else
1076     {
1077         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1078         inkscape->dialogs_toggle = FALSE;
1079     }
1084 void
1085 inkscape_dialogs_unhide ()
1087     if (Inkscape::NSApplication::Application::getNewGui())
1088         Inkscape::NSApplication::Editor::unhideDialogs();
1089     else
1090     {
1091         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1092         inkscape->dialogs_toggle = TRUE;
1093     }
1098 void
1099 inkscape_dialogs_toggle ()
1101     if (inkscape->dialogs_toggle) {
1102         inkscape_dialogs_hide ();
1103     } else {
1104         inkscape_dialogs_unhide ();
1105     }
1108 void
1109 inkscape_external_change ()
1111     g_return_if_fail (inkscape != NULL);
1113     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1116 /**
1117  * fixme: These need probably signals too
1118  */
1119 void
1120 inkscape_add_document (SPDocument *document)
1122     g_return_if_fail (document != NULL);
1124     if (!Inkscape::NSApplication::Application::getNewGui())
1125     {
1126         if ( inkscape->document_set.find(document) != inkscape->document_set.end() ) {
1127     
1128             inkscape->documents = g_slist_append (inkscape->documents, document);
1129         }
1130         inkscape->document_set.insert(document);
1131     }
1132     else
1133     {
1134         Inkscape::NSApplication::Editor::addDocument (document);
1135     }
1140 void
1141 inkscape_remove_document (SPDocument *document)
1143     g_return_if_fail (document != NULL);
1145     if (!Inkscape::NSApplication::Application::getNewGui())
1146     {
1147         inkscape->document_set.erase(document);
1148         if ( inkscape->document_set.find(document) == inkscape->document_set.end() ) {
1149             inkscape->documents = g_slist_remove (inkscape->documents, document);
1150         }
1151     }
1152     else
1153     {
1154         Inkscape::NSApplication::Editor::removeDocument (document);
1155     }
1157     return;
1160 SPDesktop *
1161 inkscape_active_desktop (void)
1163     if (Inkscape::NSApplication::Application::getNewGui())
1164         return Inkscape::NSApplication::Editor::getActiveDesktop();
1166     if (inkscape->desktops == NULL) {
1167         return NULL;
1168     }
1170     return (SPDesktop *) inkscape->desktops->data;
1173 SPDocument *
1174 inkscape_active_document (void)
1176     if (Inkscape::NSApplication::Application::getNewGui())
1177         return Inkscape::NSApplication::Editor::getActiveDocument();
1179     if (SP_ACTIVE_DESKTOP) {
1180         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1181     }
1183     return NULL;
1186 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1187     SPDocument const* document = desktop.doc();
1188     if (!document) {
1189         return false;
1190     }
1191     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1192         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1193         SPDocument *other_document=other_desktop->doc();
1194         if ( other_document == document && other_desktop != &desktop ) {
1195             return false;
1196         }
1197     }
1198     return true;
1201 SPEventContext *
1202 inkscape_active_event_context (void)
1204     if (SP_ACTIVE_DESKTOP) {
1205         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1206     }
1208     return NULL;
1213 /*#####################
1214 # HELPERS
1215 #####################*/
1217 static bool
1218 inkscape_init_config (Inkscape::XML::Document *doc, const gchar *config_name, const gchar *skeleton,
1219                       unsigned int skel_size,
1220                       const gchar *e_mkdir,
1221                       const gchar *e_notdir,
1222                       const gchar *e_ccf,
1223                       const gchar *e_cwf,
1224                       const gchar *warn)
1226     gchar *dn = profile_path(NULL);
1227     bool use_gui = (Inkscape::NSApplication::Application::getNewGui())? Inkscape::NSApplication::Application::getUseGui() : inkscape->use_gui;
1228     if (!Inkscape::IO::file_test(dn, G_FILE_TEST_EXISTS)) {
1229         if (Inkscape::IO::mkdir_utf8name(dn))
1230         {
1231             if (use_gui) {
1232                 // Cannot create directory
1233                 gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1234                 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_mkdir, safeDn, warn);
1235                 gtk_dialog_run (GTK_DIALOG (w));
1236                 gtk_widget_destroy (w);
1237                 g_free(safeDn);
1238                 g_free (dn);
1239                 return false;
1240             } else {
1241                 g_warning(e_mkdir, dn, warn);
1242                 g_free (dn);
1243                 return false;
1244             }
1245         }
1247         // Also create (empty for now) subdirectories for the user's stuff
1248         {
1249             gchar *temp_dn = profile_path("templates");
1250             Inkscape::IO::mkdir_utf8name(temp_dn);
1251         }
1252         {
1253             gchar *temp_dn = profile_path("keys");
1254             Inkscape::IO::mkdir_utf8name(temp_dn);
1255         }
1256         {
1257             gchar *temp_dn = profile_path("icons");
1258             Inkscape::IO::mkdir_utf8name(temp_dn);
1259         }
1260         {
1261             gchar *temp_dn = profile_path("extensions");
1262             Inkscape::IO::mkdir_utf8name(temp_dn);
1263         }
1264         {
1265             gchar *temp_dn = profile_path("palettes");
1266             Inkscape::IO::mkdir_utf8name(temp_dn);
1267         }
1269     } else if (!Inkscape::IO::file_test(dn, G_FILE_TEST_IS_DIR)) {
1270         if (use_gui) {
1271             // Not a directory
1272             gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1273             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notdir, safeDn, warn);
1274             gtk_dialog_run (GTK_DIALOG (w));
1275             gtk_widget_destroy (w);
1276             g_free( safeDn );
1277             g_free (dn);
1278             return false;
1279         } else {
1280             g_warning(e_notdir, dn, warn);
1281             g_free(dn);
1282             return false;
1283         }
1284     }
1285     g_free (dn);
1287     gchar *fn = profile_path(config_name);
1289     Inkscape::IO::dump_fopen_call(fn, "H");
1290     FILE *fh = Inkscape::IO::fopen_utf8name(fn, "w");
1291     if (!fh) {
1292         if (use_gui) {
1293             /* Cannot create file */
1294             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1295             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_ccf, safeFn, warn);
1296             gtk_dialog_run (GTK_DIALOG (w));
1297             gtk_widget_destroy (w);
1298             g_free(safeFn);
1299             g_free (fn);
1300             return false;
1301         } else {
1302             g_warning(e_ccf, fn, warn);
1303             g_free(fn);
1304             return false;
1305         }
1306     }
1307     if ( fwrite(skeleton, 1, skel_size, fh) != skel_size ) {
1308         if (use_gui) {
1309             /* Cannot create file */
1310             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1311             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_cwf, safeFn, warn);
1312             gtk_dialog_run (GTK_DIALOG (w));
1313             gtk_widget_destroy (w);
1314             g_free(safeFn);
1315             g_free (fn);
1316             fclose(fh);
1317             return false;
1318         } else {
1319             g_warning(e_cwf, fn, warn);
1320             g_free(fn);
1321             fclose(fh);
1322             return false;
1323         }
1324     }
1326     g_free(fn);
1327     fclose(fh);
1328     return true;
1331 void
1332 inkscape_refresh_display (Inkscape::Application *inkscape)
1334     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1335         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1336     }
1340 /**
1341  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1342  *  saves the preferences if appropriate, and quits.
1343  */
1344 void
1345 inkscape_exit (Inkscape::Application *inkscape)
1347     g_assert (INKSCAPE);
1349     //emit shutdown signal so that dialogs could remember layout
1350     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1352     Inkscape::Preferences::save();
1353     gtk_main_quit ();
1356 gchar *
1357 homedir_path(const char *filename)
1359     static const gchar *homedir = NULL;
1360     if (!homedir) {
1361         homedir = g_get_home_dir();
1362         gchar* utf8Path = g_filename_to_utf8( homedir, -1, NULL, NULL, NULL );
1363         if ( utf8Path )
1364         {
1365                 homedir = utf8Path;
1366                 if (!g_utf8_validate(homedir, -1, NULL)) {
1367                     g_warning( "g_get_home_dir() post A IS NOT UTF-8" );
1368                 }
1369         }
1370     }
1371     if (!homedir) {
1372         gchar * path = g_path_get_dirname(INKSCAPE->argv0);
1373         gchar* utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
1374         g_free(path);
1375         if ( utf8Path )
1376         {
1377             homedir = utf8Path;
1378             if (!g_utf8_validate(homedir, -1, NULL)) {
1379                 g_warning( "g_get_home_dir() post B IS NOT UTF-8" );
1380             }
1381         }
1382     }
1383     return g_build_filename(homedir, filename, NULL);
1387 /**
1388  * Get, or guess, or decide the location where the preferences.xml
1389  * file should be located.
1390  */
1391 gchar *
1392 profile_path(const char *filename)
1394     static const gchar *prefdir = NULL;
1395     if (!prefdir) {
1396 #ifdef HAS_SHGetSpecialFolderLocation
1397         // prefer c:\Documents and Settings\UserName\Application Data\ to
1398         // c:\Documents and Settings\userName\;
1399         if (!prefdir) {
1400             ITEMIDLIST *pidl = 0;
1401             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1402                 gchar * utf8Path = NULL;
1404                 if ( PrintWin32::is_os_wide() ) {
1405                     wchar_t pathBuf[MAX_PATH+1];
1406                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1408                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1409                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1410                     }
1411                 } else {
1412                     char pathBuf[MAX_PATH+1];
1414                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1415                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1416                     }
1417                 }
1419                 if ( utf8Path ) {
1420                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1421                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1422                         g_free( utf8Path );
1423                         utf8Path = 0;
1424                     } else {
1425                         prefdir = utf8Path;
1426                     }
1427                 }
1430                 /* not compiling yet...
1432                 // Remember to free the list pointer
1433                 IMalloc * imalloc = 0;
1434                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1435                     imalloc->lpVtbl->Free( imalloc, pidl );
1436                     imalloc->lpVtbl->Release( imalloc );
1437                 }
1438                 */
1439             }
1440         }
1441 #endif
1442         if (!prefdir) {
1443             prefdir = homedir_path(NULL);
1444         }
1445     }
1446     return g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, filename, NULL);
1449 Inkscape::XML::Node *
1450 inkscape_get_menus (Inkscape::Application * inkscape)
1452     Inkscape::XML::Node *repr = inkscape->menus->root();
1453     g_assert (!(strcmp (repr->name(), "inkscape")));
1454     return repr->firstChild();
1457 void
1458 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1460     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1461         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1462     }
1467 /*
1468   Local Variables:
1469   mode:c++
1470   c-file-style:"stroustrup"
1471   c-file-offsets:((innamespace . 0)(inline-open . 0))
1472   indent-tabs-mode:nil
1473   fill-column:99
1474   End:
1475 */
1476 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :