Code

af6ef4f05a125c5718c0f76a88e64e7a535072f0
[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) {
731         return NULL;
732     }
734     Inkscape::XML::Node *repr = Inkscape::Preferences::get()->root();
735     if (!repr) return NULL;
736     g_assert (!(strcmp (repr->name(), "inkscape")));
738     gchar const *s = key;
739     while ((s) && (*s)) {
741         /* Find next name */
742         gchar const *e = strchr (s, '.');
743         guint len;
744         if (e) {
745             len = e++ - s;
746         } else {
747             len = strlen (s);
748         }
750         Inkscape::XML::Node* child;
751         for (child = repr->firstChild(); child != NULL; child = child->next()) {
752             gchar const *id = child->attribute("id");
753             if ((id) && (strlen (id) == len) && (!strncmp (id, s, len)))
754             {
755                 break;
756             }
757         }
758         if (child == NULL) {
759             return NULL;
760         }
762         repr = child;
763         s = e;
764     }
765     return repr;
770 void
771 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
773     if (Inkscape::NSApplication::Application::getNewGui()) {
774         Inkscape::NSApplication::Editor::selectionModified (selection, flags);
775         return;
776     }
777     g_return_if_fail (selection != NULL);
779     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
780         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
781     }
785 void
786 inkscape_selection_changed (Inkscape::Selection * selection)
788     if (Inkscape::NSApplication::Application::getNewGui()) {
789         Inkscape::NSApplication::Editor::selectionChanged (selection);
790         return;
791     }
792     g_return_if_fail (selection != NULL);
794     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
795         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
796     }
799 void
800 inkscape_subselection_changed (SPDesktop *desktop)
802     if (Inkscape::NSApplication::Application::getNewGui()) {
803         Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
804         return;
805     }
806     g_return_if_fail (desktop != NULL);
808     if (DESKTOP_IS_ACTIVE (desktop)) {
809         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
810     }
814 void
815 inkscape_selection_set (Inkscape::Selection * selection)
817     if (Inkscape::NSApplication::Application::getNewGui()) {
818         Inkscape::NSApplication::Editor::selectionSet (selection);
819         return;
820     }
821     g_return_if_fail (selection != NULL);
823     if (DESKTOP_IS_ACTIVE (selection->desktop())) {
824         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
825         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
826     }
830 void
831 inkscape_eventcontext_set (SPEventContext * eventcontext)
833     if (Inkscape::NSApplication::Application::getNewGui()) {
834         Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
835         return;
836     }
837     g_return_if_fail (eventcontext != NULL);
838     g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
840     if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
841         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
842     }
846 void
847 inkscape_add_desktop (SPDesktop * desktop)
849     g_return_if_fail (desktop != NULL);
851     if (Inkscape::NSApplication::Application::getNewGui())
852     {
853         Inkscape::NSApplication::Editor::addDesktop (desktop);
854         return;
855     }
856     g_return_if_fail (inkscape != NULL);
858     g_assert (!g_slist_find (inkscape->desktops, desktop));
860     inkscape->desktops = g_slist_append (inkscape->desktops, desktop);
862     if (DESKTOP_IS_ACTIVE (desktop)) {
863         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
864         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
865         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
866         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
867     }
872 void
873 inkscape_remove_desktop (SPDesktop * desktop)
875     g_return_if_fail (desktop != NULL);
876     if (Inkscape::NSApplication::Application::getNewGui())
877     {
878         Inkscape::NSApplication::Editor::removeDesktop (desktop);
879         return;
880     }
881     g_return_if_fail (inkscape != NULL);
883     g_assert (g_slist_find (inkscape->desktops, desktop));
885     if (DESKTOP_IS_ACTIVE (desktop)) {
886         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
887         if (inkscape->desktops->next != NULL) {
888             SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
889             inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
890             inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
891             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
892             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
893             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
894             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
895         } else {
896             g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
897             if (sp_desktop_selection(desktop))
898                 sp_desktop_selection(desktop)->clear();
899         }
900     }
902     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
904     // if this was the last desktop, shut down the program
905     if (inkscape->desktops == NULL) {
906         inkscape_exit (inkscape);
907     }
912 void
913 inkscape_activate_desktop (SPDesktop * desktop)
915     g_return_if_fail (desktop != NULL);
916     if (Inkscape::NSApplication::Application::getNewGui())
917     {
918         Inkscape::NSApplication::Editor::activateDesktop (desktop);
919         return;
920     }
921     g_return_if_fail (inkscape != NULL);
923     if (DESKTOP_IS_ACTIVE (desktop)) {
924         return;
925     }
927     g_assert (g_slist_find (inkscape->desktops, desktop));
929     SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
931     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
933     inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
934     inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
936     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
937     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
938     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
939     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
943 /**
944  *  Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
945  */
946 void
947 inkscape_reactivate_desktop (SPDesktop * desktop)
949     g_return_if_fail (desktop != NULL);
950     if (Inkscape::NSApplication::Application::getNewGui())
951     {
952         Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
953         return;
954     }
955     g_return_if_fail (inkscape != NULL);
957     if (DESKTOP_IS_ACTIVE (desktop))
958         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
963 SPDesktop *
964 inkscape_find_desktop_by_dkey (unsigned int dkey)
966     for (GSList *r = inkscape->desktops; r; r = r->next) {
967         if (((SPDesktop *) r->data)->dkey == dkey)
968             return ((SPDesktop *) r->data);
969     }
970     return NULL;
976 unsigned int
977 inkscape_maximum_dkey()
979     unsigned int dkey = 0;
981     for (GSList *r = inkscape->desktops; r; r = r->next) {
982         if (((SPDesktop *) r->data)->dkey > dkey)
983             dkey = ((SPDesktop *) r->data)->dkey;
984     }
986     return dkey;
991 SPDesktop *
992 inkscape_next_desktop ()
994     SPDesktop *d = NULL;
995     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
997     if (dkey_current < inkscape_maximum_dkey()) {
998         // find next existing
999         for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1000             d = inkscape_find_desktop_by_dkey (i);
1001             if (d) {
1002                 break;
1003             }
1004         }
1005     } else {
1006         // find first existing
1007         for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1008             d = inkscape_find_desktop_by_dkey (i);
1009             if (d) {
1010                 break;
1011             }
1012         }
1013     }
1015     g_assert (d);
1017     return d;
1022 SPDesktop *
1023 inkscape_prev_desktop ()
1025     SPDesktop *d = NULL;
1026     unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1028     if (dkey_current > 0) {
1029         // find prev existing
1030         for (signed int i = dkey_current - 1; i >= 0; i--) {
1031             d = inkscape_find_desktop_by_dkey (i);
1032             if (d) {
1033                 break;
1034             }
1035         }
1036     }
1037     if (!d) {
1038         // find last existing
1039         d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1040     }
1042     g_assert (d);
1044     return d;
1049 void
1050 inkscape_switch_desktops_next ()
1052     inkscape_next_desktop()->presentWindow();
1057 void
1058 inkscape_switch_desktops_prev ()
1060     inkscape_prev_desktop()->presentWindow();
1065 void
1066 inkscape_dialogs_hide ()
1068     if (Inkscape::NSApplication::Application::getNewGui())
1069         Inkscape::NSApplication::Editor::hideDialogs();
1070     else
1071     {
1072         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1073         inkscape->dialogs_toggle = FALSE;
1074     }
1079 void
1080 inkscape_dialogs_unhide ()
1082     if (Inkscape::NSApplication::Application::getNewGui())
1083         Inkscape::NSApplication::Editor::unhideDialogs();
1084     else
1085     {
1086         g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1087         inkscape->dialogs_toggle = TRUE;
1088     }
1093 void
1094 inkscape_dialogs_toggle ()
1096     if (inkscape->dialogs_toggle) {
1097         inkscape_dialogs_hide ();
1098     } else {
1099         inkscape_dialogs_unhide ();
1100     }
1103 void
1104 inkscape_external_change ()
1106     g_return_if_fail (inkscape != NULL);
1108     g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1111 /**
1112  * fixme: These need probably signals too
1113  */
1114 void
1115 inkscape_add_document (SPDocument *document)
1117     g_return_if_fail (document != NULL);
1119     if (!Inkscape::NSApplication::Application::getNewGui())
1120     {
1121         if ( inkscape->document_set.find(document) != inkscape->document_set.end() ) {
1122     
1123             inkscape->documents = g_slist_append (inkscape->documents, document);
1124         }
1125         inkscape->document_set.insert(document);
1126     }
1127     else
1128     {
1129         Inkscape::NSApplication::Editor::addDocument (document);
1130     }
1135 void
1136 inkscape_remove_document (SPDocument *document)
1138     g_return_if_fail (document != NULL);
1140     if (!Inkscape::NSApplication::Application::getNewGui())
1141     {
1142         inkscape->document_set.erase(document);
1143         if ( inkscape->document_set.find(document) == inkscape->document_set.end() ) {
1144             inkscape->documents = g_slist_remove (inkscape->documents, document);
1145         }
1146     }
1147     else
1148     {
1149         Inkscape::NSApplication::Editor::removeDocument (document);
1150     }
1152     return;
1155 SPDesktop *
1156 inkscape_active_desktop (void)
1158     if (Inkscape::NSApplication::Application::getNewGui())
1159         return Inkscape::NSApplication::Editor::getActiveDesktop();
1161     if (inkscape->desktops == NULL) {
1162         return NULL;
1163     }
1165     return (SPDesktop *) inkscape->desktops->data;
1168 SPDocument *
1169 inkscape_active_document (void)
1171     if (Inkscape::NSApplication::Application::getNewGui())
1172         return Inkscape::NSApplication::Editor::getActiveDocument();
1174     if (SP_ACTIVE_DESKTOP) {
1175         return sp_desktop_document (SP_ACTIVE_DESKTOP);
1176     }
1178     return NULL;
1181 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1182     SPDocument const* document = desktop.doc();
1183     if (!document) {
1184         return false;
1185     }
1186     for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1187         SPDesktop *other_desktop=(SPDesktop *)iter->data;
1188         SPDocument *other_document=other_desktop->doc();
1189         if ( other_document == document && other_desktop != &desktop ) {
1190             return false;
1191         }
1192     }
1193     return true;
1196 SPEventContext *
1197 inkscape_active_event_context (void)
1199     if (SP_ACTIVE_DESKTOP) {
1200         return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1201     }
1203     return NULL;
1208 /*#####################
1209 # HELPERS
1210 #####################*/
1212 static bool
1213 inkscape_init_config (Inkscape::XML::Document *doc, const gchar *config_name, const gchar *skeleton,
1214                       unsigned int skel_size,
1215                       const gchar *e_mkdir,
1216                       const gchar *e_notdir,
1217                       const gchar *e_ccf,
1218                       const gchar *e_cwf,
1219                       const gchar *warn)
1221     gchar *dn = profile_path(NULL);
1222     bool use_gui = (Inkscape::NSApplication::Application::getNewGui())? Inkscape::NSApplication::Application::getUseGui() : inkscape->use_gui;
1223     if (!Inkscape::IO::file_test(dn, G_FILE_TEST_EXISTS)) {
1224         if (Inkscape::IO::mkdir_utf8name(dn))
1225         {
1226             if (use_gui) {
1227                 // Cannot create directory
1228                 gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1229                 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_mkdir, safeDn, warn);
1230                 gtk_dialog_run (GTK_DIALOG (w));
1231                 gtk_widget_destroy (w);
1232                 g_free(safeDn);
1233                 g_free (dn);
1234                 return false;
1235             } else {
1236                 g_warning(e_mkdir, dn, warn);
1237                 g_free (dn);
1238                 return false;
1239             }
1240         }
1242         // Also create (empty for now) subdirectories for the user's stuff
1243         {
1244             gchar *temp_dn = profile_path("templates");
1245             Inkscape::IO::mkdir_utf8name(temp_dn);
1246         }
1247         {
1248             gchar *temp_dn = profile_path("keys");
1249             Inkscape::IO::mkdir_utf8name(temp_dn);
1250         }
1251         {
1252             gchar *temp_dn = profile_path("icons");
1253             Inkscape::IO::mkdir_utf8name(temp_dn);
1254         }
1255         {
1256             gchar *temp_dn = profile_path("extensions");
1257             Inkscape::IO::mkdir_utf8name(temp_dn);
1258         }
1259         {
1260             gchar *temp_dn = profile_path("palettes");
1261             Inkscape::IO::mkdir_utf8name(temp_dn);
1262         }
1264     } else if (!Inkscape::IO::file_test(dn, G_FILE_TEST_IS_DIR)) {
1265         if (use_gui) {
1266             // Not a directory
1267             gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1268             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notdir, safeDn, warn);
1269             gtk_dialog_run (GTK_DIALOG (w));
1270             gtk_widget_destroy (w);
1271             g_free( safeDn );
1272             g_free (dn);
1273             return false;
1274         } else {
1275             g_warning(e_notdir, dn, warn);
1276             g_free(dn);
1277             return false;
1278         }
1279     }
1280     g_free (dn);
1282     gchar *fn = profile_path(config_name);
1284     Inkscape::IO::dump_fopen_call(fn, "H");
1285     FILE *fh = Inkscape::IO::fopen_utf8name(fn, "w");
1286     if (!fh) {
1287         if (use_gui) {
1288             /* Cannot create file */
1289             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1290             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_ccf, safeFn, warn);
1291             gtk_dialog_run (GTK_DIALOG (w));
1292             gtk_widget_destroy (w);
1293             g_free(safeFn);
1294             g_free (fn);
1295             return false;
1296         } else {
1297             g_warning(e_ccf, fn, warn);
1298             g_free(fn);
1299             return false;
1300         }
1301     }
1302     if ( fwrite(skeleton, 1, skel_size, fh) != skel_size ) {
1303         if (use_gui) {
1304             /* Cannot create file */
1305             gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1306             GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_cwf, safeFn, warn);
1307             gtk_dialog_run (GTK_DIALOG (w));
1308             gtk_widget_destroy (w);
1309             g_free(safeFn);
1310             g_free (fn);
1311             fclose(fh);
1312             return false;
1313         } else {
1314             g_warning(e_cwf, fn, warn);
1315             g_free(fn);
1316             fclose(fh);
1317             return false;
1318         }
1319     }
1321     g_free(fn);
1322     fclose(fh);
1323     return true;
1326 void
1327 inkscape_refresh_display (Inkscape::Application *inkscape)
1329     for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1330         (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1331     }
1335 /**
1336  *  Handler for Inkscape's Exit verb.  This emits the shutdown signal,
1337  *  saves the preferences if appropriate, and quits.
1338  */
1339 void
1340 inkscape_exit (Inkscape::Application *inkscape)
1342     g_assert (INKSCAPE);
1344     //emit shutdown signal so that dialogs could remember layout
1345     g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1347     Inkscape::Preferences::save();
1348     gtk_main_quit ();
1351 gchar *
1352 homedir_path(const char *filename)
1354     static const gchar *homedir = NULL;
1355     if (!homedir) {
1356         homedir = g_get_home_dir();
1357         gchar* utf8Path = g_filename_to_utf8( homedir, -1, NULL, NULL, NULL );
1358         if ( utf8Path )
1359         {
1360                 homedir = utf8Path;
1361                 if (!g_utf8_validate(homedir, -1, NULL)) {
1362                     g_warning( "g_get_home_dir() post A IS NOT UTF-8" );
1363                 }
1364         }
1365     }
1366     if (!homedir) {
1367         gchar * path = g_path_get_dirname(INKSCAPE->argv0);
1368         gchar* utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
1369         g_free(path);
1370         if ( utf8Path )
1371         {
1372             homedir = utf8Path;
1373             if (!g_utf8_validate(homedir, -1, NULL)) {
1374                 g_warning( "g_get_home_dir() post B IS NOT UTF-8" );
1375             }
1376         }
1377     }
1378     return g_build_filename(homedir, filename, NULL);
1382 /**
1383  * Get, or guess, or decide the location where the preferences.xml
1384  * file should be located.
1385  */
1386 gchar *
1387 profile_path(const char *filename)
1389     static const gchar *prefdir = NULL;
1390     if (!prefdir) {
1391 #ifdef HAS_SHGetSpecialFolderLocation
1392         // prefer c:\Documents and Settings\UserName\Application Data\ to
1393         // c:\Documents and Settings\userName\;
1394         if (!prefdir) {
1395             ITEMIDLIST *pidl = 0;
1396             if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1397                 gchar * utf8Path = NULL;
1399                 if ( PrintWin32::is_os_wide() ) {
1400                     wchar_t pathBuf[MAX_PATH+1];
1401                     g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1403                     if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1404                         utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1405                     }
1406                 } else {
1407                     char pathBuf[MAX_PATH+1];
1409                     if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1410                         utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1411                     }
1412                 }
1414                 if ( utf8Path ) {
1415                     if (!g_utf8_validate(utf8Path, -1, NULL)) {
1416                         g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1417                         g_free( utf8Path );
1418                         utf8Path = 0;
1419                     } else {
1420                         prefdir = utf8Path;
1421                     }
1422                 }
1425                 /* not compiling yet...
1427                 // Remember to free the list pointer
1428                 IMalloc * imalloc = 0;
1429                 if ( SHGetMalloc(&imalloc) == NOERROR) {
1430                     imalloc->lpVtbl->Free( imalloc, pidl );
1431                     imalloc->lpVtbl->Release( imalloc );
1432                 }
1433                 */
1434             }
1435         }
1436 #endif
1437         if (!prefdir) {
1438             prefdir = homedir_path(NULL);
1439         }
1440     }
1441     return g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, filename, NULL);
1444 Inkscape::XML::Node *
1445 inkscape_get_menus (Inkscape::Application * inkscape)
1447     Inkscape::XML::Node *repr = inkscape->menus->root();
1448     g_assert (!(strcmp (repr->name(), "inkscape")));
1449     return repr->firstChild();
1452 void
1453 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1455     for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1456         listbuf.push_back(static_cast< SPDesktop* >(l->data));
1457     }
1462 /*
1463   Local Variables:
1464   mode:c++
1465   c-file-style:"stroustrup"
1466   c-file-offsets:((innamespace . 0)(inline-open . 0))
1467   indent-tabs-mode:nil
1468   fill-column:99
1469   End:
1470 */
1471 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :