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