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 guint mapalt;
121 };
123 struct Inkscape::ApplicationClass {
124 GObjectClass object_class;
126 /* Signals */
127 void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
128 void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
129 void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
130 void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
131 void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
132 void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
133 void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
134 void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
135 void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
136 void (* shut_down) (Inkscape::Application *inkscape);
137 void (* dialogs_hide) (Inkscape::Application *inkscape);
138 void (* dialogs_unhide) (Inkscape::Application *inkscape);
139 void (* external_change) (Inkscape::Application *inkscape);
140 };
142 static GObjectClass * parent_class;
143 static guint inkscape_signals[LAST_SIGNAL] = {0};
145 static void (* segv_handler) (int) = SIG_DFL;
146 static void (* abrt_handler) (int) = SIG_DFL;
147 static void (* fpe_handler) (int) = SIG_DFL;
148 static void (* ill_handler) (int) = SIG_DFL;
149 static void (* bus_handler) (int) = SIG_DFL;
151 #ifdef WIN32
152 #define INKSCAPE_PROFILE_DIR "Inkscape"
153 #else
154 #define INKSCAPE_PROFILE_DIR ".inkscape"
155 #endif
157 #define MENUS_FILE "menus.xml"
160 /**
161 * Retrieves the GType for the Inkscape Application object.
162 */
163 GType
164 inkscape_get_type (void)
165 {
166 static GType type = 0;
167 if (!type) {
168 GTypeInfo info = {
169 sizeof (Inkscape::ApplicationClass),
170 NULL, NULL,
171 (GClassInitFunc) inkscape_class_init,
172 NULL, NULL,
173 sizeof (Inkscape::Application),
174 4,
175 (GInstanceInitFunc) inkscape_init,
176 NULL
177 };
178 type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
179 }
180 return type;
181 }
184 /**
185 * Initializes the inkscape class, registering all of its signal handlers
186 * and virtual functions
187 */
188 static void
189 inkscape_class_init (Inkscape::ApplicationClass * klass)
190 {
191 GObjectClass * object_class;
193 object_class = (GObjectClass *) klass;
195 parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
197 inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
198 G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_FIRST,
200 G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
201 NULL, NULL,
202 sp_marshal_NONE__POINTER_UINT,
203 G_TYPE_NONE, 2,
204 G_TYPE_POINTER, G_TYPE_UINT);
205 inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
206 G_TYPE_FROM_CLASS (klass),
207 G_SIGNAL_RUN_FIRST,
208 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
209 NULL, NULL,
210 sp_marshal_NONE__POINTER,
211 G_TYPE_NONE, 1,
212 G_TYPE_POINTER);
213 inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
214 G_TYPE_FROM_CLASS (klass),
215 G_SIGNAL_RUN_FIRST,
216 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
217 NULL, NULL,
218 sp_marshal_NONE__POINTER,
219 G_TYPE_NONE, 1,
220 G_TYPE_POINTER);
221 inkscape_signals[SET_SELECTION] = g_signal_new ("set_selection",
222 G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_FIRST,
224 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
225 NULL, NULL,
226 sp_marshal_NONE__POINTER,
227 G_TYPE_NONE, 1,
228 G_TYPE_POINTER);
229 inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
230 G_TYPE_FROM_CLASS (klass),
231 G_SIGNAL_RUN_FIRST,
232 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
233 NULL, NULL,
234 sp_marshal_NONE__POINTER,
235 G_TYPE_NONE, 1,
236 G_TYPE_POINTER);
237 inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
238 G_TYPE_FROM_CLASS (klass),
239 G_SIGNAL_RUN_FIRST,
240 G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
241 NULL, NULL,
242 sp_marshal_NONE__POINTER,
243 G_TYPE_NONE, 1,
244 G_TYPE_POINTER);
245 inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
246 G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_FIRST,
248 G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
249 NULL, NULL,
250 sp_marshal_NONE__POINTER,
251 G_TYPE_NONE, 1,
252 G_TYPE_POINTER);
253 inkscape_signals[SHUTDOWN_SIGNAL] = g_signal_new ("shut_down",
254 G_TYPE_FROM_CLASS (klass),
255 G_SIGNAL_RUN_FIRST,
256 G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
257 NULL, NULL,
258 g_cclosure_marshal_VOID__VOID,
259 G_TYPE_NONE, 0);
260 inkscape_signals[DIALOGS_HIDE] = g_signal_new ("dialogs_hide",
261 G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_FIRST,
263 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
264 NULL, NULL,
265 g_cclosure_marshal_VOID__VOID,
266 G_TYPE_NONE, 0);
267 inkscape_signals[DIALOGS_UNHIDE] = g_signal_new ("dialogs_unhide",
268 G_TYPE_FROM_CLASS (klass),
269 G_SIGNAL_RUN_FIRST,
270 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
271 NULL, NULL,
272 g_cclosure_marshal_VOID__VOID,
273 G_TYPE_NONE, 0);
274 inkscape_signals[EXTERNAL_CHANGE] = g_signal_new ("external_change",
275 G_TYPE_FROM_CLASS (klass),
276 G_SIGNAL_RUN_FIRST,
277 G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
278 NULL, NULL,
279 g_cclosure_marshal_VOID__VOID,
280 G_TYPE_NONE, 0);
282 object_class->dispose = inkscape_dispose;
284 klass->activate_desktop = inkscape_activate_desktop_private;
285 klass->deactivate_desktop = inkscape_deactivate_desktop_private;
286 }
289 static void
290 inkscape_init (SPObject * object)
291 {
292 if (!inkscape) {
293 inkscape = (Inkscape::Application *) object;
294 } else {
295 g_assert_not_reached ();
296 }
298 new (&inkscape->document_set) std::multiset<SPDocument *>();
300 inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
302 inkscape->documents = NULL;
303 inkscape->desktops = NULL;
305 inkscape->dialogs_toggle = TRUE;
307 inkscape->mapalt=GDK_MOD1_MASK;
308 }
310 static void
311 inkscape_dispose (GObject *object)
312 {
313 Inkscape::Application *inkscape = (Inkscape::Application *) object;
315 while (inkscape->documents) {
316 // we don't otherwise unref, so why here?
317 sp_document_unref((SPDocument *)inkscape->documents->data);
318 }
320 g_assert (!inkscape->desktops);
322 Inkscape::Preferences::save();
324 if (inkscape->menus) {
325 /* fixme: This is not the best place */
326 Inkscape::GC::release(inkscape->menus);
327 inkscape->menus = NULL;
328 }
330 inkscape->document_set.~multiset();
332 G_OBJECT_CLASS (parent_class)->dispose (object);
334 gtk_main_quit ();
335 }
338 void
339 inkscape_ref (void)
340 {
341 if (inkscape)
342 g_object_ref (G_OBJECT (inkscape));
343 }
346 void
347 inkscape_unref (void)
348 {
349 if (inkscape)
350 g_object_unref (G_OBJECT (inkscape));
351 }
353 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
354 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
355 guint
356 inkscape_mapalt() {
357 return inkscape->mapalt;
358 }
360 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
361 void inkscape_mapalt(guint maskvalue)
362 {
363 if(maskvalue<2 || maskvalue> 5 ){ /* MOD5 is the highest defined in gdktypes.h */
364 inkscape->mapalt=0;
365 }else{
366 inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
367 }
368 }
370 static void
371 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
372 {
373 desktop->set_active (true);
374 }
377 static void
378 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
379 {
380 desktop->set_active (false);
381 }
384 /* fixme: This is EVIL, and belongs to main after all */
386 #define SP_INDENT 8
389 static void
390 inkscape_crash_handler (int /*signum*/)
391 {
392 using Inkscape::Debug::SimpleEvent;
393 using Inkscape::Debug::EventTracker;
394 using Inkscape::Debug::Logger;
396 static gint recursion = FALSE;
398 /*
399 * reset all signal handlers: any further crashes should just be allowed
400 * to crash normally.
401 * */
402 signal (SIGSEGV, segv_handler );
403 signal (SIGABRT, abrt_handler );
404 signal (SIGFPE, fpe_handler );
405 signal (SIGILL, ill_handler );
406 #ifndef WIN32
407 signal (SIGBUS, bus_handler );
408 #endif
410 /* Stop bizarre loops */
411 if (recursion) {
412 abort ();
413 }
414 recursion = TRUE;
416 EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
417 tracker.set<SimpleEvent<> >("emergency-save");
419 fprintf(stderr, "\nEmergency save activated!\n");
421 time_t sptime = time (NULL);
422 struct tm *sptm = localtime (&sptime);
423 gchar sptstr[256];
424 strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
426 gint count = 0;
427 GSList *savednames = NULL;
428 GSList *failednames = NULL;
429 for (GSList *l = inkscape->documents; l != NULL; l = l->next) {
430 SPDocument *doc;
431 Inkscape::XML::Node *repr;
432 doc = (SPDocument *) l->data;
433 repr = sp_document_repr_root (doc);
434 if (repr->attribute("sodipodi:modified")) {
435 const gchar *docname, *d0, *d;
436 gchar n[64], c[1024];
437 FILE *file;
439 /* originally, the document name was retrieved from
440 * the sodipod:docname attribute */
441 docname = doc->name;
442 if (docname) {
443 /* fixme: Quick hack to remove emergency file suffix */
444 d0 = strrchr ((char*)docname, '.');
445 if (d0 && (d0 > docname)) {
446 d0 = strrchr ((char*)(d0 - 1), '.');
447 if (d0 && (d0 > docname)) {
448 d = d0;
449 while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
450 if (*d) {
451 memcpy (n, docname, MIN (d0 - docname - 1, 64));
452 n[63] = '\0';
453 docname = n;
454 }
455 }
456 }
457 }
459 if (!docname || !*docname) docname = "emergency";
460 // try saving to the profile location
461 g_snprintf (c, 1024, "%.256s.%s.%d", docname, sptstr, count);
462 gchar * location = homedir_path(c);
463 Inkscape::IO::dump_fopen_call(location, "E");
464 file = Inkscape::IO::fopen_utf8name(location, "w");
465 g_free(location);
466 if (!file) {
467 // try saving to /tmp
468 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d", docname, sptstr, count);
469 Inkscape::IO::dump_fopen_call(c, "G");
470 file = Inkscape::IO::fopen_utf8name(c, "w");
471 }
472 if (!file) {
473 // try saving to the current directory
474 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d", docname, sptstr, count);
475 Inkscape::IO::dump_fopen_call(c, "F");
476 file = Inkscape::IO::fopen_utf8name(c, "w");
477 }
478 if (file) {
479 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
480 savednames = g_slist_prepend (savednames, g_strdup (c));
481 fclose (file);
482 } else {
483 docname = repr->attribute("sodipodi:docname");
484 failednames = g_slist_prepend (failednames, (docname) ? g_strdup (docname) : g_strdup (_("Untitled document")));
485 }
486 count++;
487 }
488 }
490 savednames = g_slist_reverse (savednames);
491 failednames = g_slist_reverse (failednames);
492 if (savednames) {
493 fprintf (stderr, "\nEmergency save document locations:\n");
494 for (GSList *l = savednames; l != NULL; l = l->next) {
495 fprintf (stderr, " %s\n", (gchar *) l->data);
496 }
497 }
498 if (failednames) {
499 fprintf (stderr, "\nFailed to do emergency save for documents:\n");
500 for (GSList *l = failednames; l != NULL; l = l->next) {
501 fprintf (stderr, " %s\n", (gchar *) l->data);
502 }
503 }
505 Inkscape::Preferences::save();
507 fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
508 fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
509 fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
511 /* Show nice dialog box */
513 char const *istr = _("Inkscape encountered an internal error and will close now.\n");
514 char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
515 char const *fstr = _("Automatic backup of the following documents failed:\n");
516 gint nllen = strlen ("\n");
517 gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
518 for (GSList *l = savednames; l != NULL; l = l->next) {
519 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
520 }
521 for (GSList *l = failednames; l != NULL; l = l->next) {
522 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
523 }
524 len += 1;
525 gchar *b = g_new (gchar, len);
526 gint pos = 0;
527 len = strlen (istr);
528 memcpy (b + pos, istr, len);
529 pos += len;
530 if (savednames) {
531 len = strlen (sstr);
532 memcpy (b + pos, sstr, len);
533 pos += len;
534 for (GSList *l = savednames; l != NULL; l = l->next) {
535 memset (b + pos, ' ', SP_INDENT);
536 pos += SP_INDENT;
537 len = strlen ((gchar *) l->data);
538 memcpy (b + pos, l->data, len);
539 pos += len;
540 memcpy (b + pos, "\n", nllen);
541 pos += nllen;
542 }
543 }
544 if (failednames) {
545 len = strlen (fstr);
546 memcpy (b + pos, fstr, len);
547 pos += len;
548 for (GSList *l = failednames; l != NULL; l = l->next) {
549 memset (b + pos, ' ', SP_INDENT);
550 pos += SP_INDENT;
551 len = strlen ((gchar *) l->data);
552 memcpy (b + pos, l->data, len);
553 pos += len;
554 memcpy (b + pos, "\n", nllen);
555 pos += nllen;
556 }
557 }
558 *(b + pos) = '\0';
560 if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
561 GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
562 gtk_dialog_run (GTK_DIALOG (msgbox));
563 gtk_widget_destroy (msgbox);
564 }
565 else
566 {
567 g_message( "Error: %s", b );
568 }
569 g_free (b);
571 tracker.clear();
572 Logger::shutdown();
574 /* on exit, allow restored signal handler to take over and crash us */
575 }
579 void
580 inkscape_application_init (const gchar *argv0, gboolean use_gui)
581 {
582 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
583 /* fixme: load application defaults */
585 segv_handler = signal (SIGSEGV, inkscape_crash_handler);
586 abrt_handler = signal (SIGABRT, inkscape_crash_handler);
587 fpe_handler = signal (SIGFPE, inkscape_crash_handler);
588 ill_handler = signal (SIGILL, inkscape_crash_handler);
589 #ifndef WIN32
590 bus_handler = signal (SIGBUS, inkscape_crash_handler);
591 #endif
593 inkscape->use_gui = use_gui;
594 inkscape->argv0 = g_strdup(argv0);
596 /* Attempt to load the preferences, and set the save_preferences flag to TRUE
597 if we could, or FALSE if we couldn't */
598 Inkscape::Preferences::load();
599 inkscape_load_menus(inkscape);
601 /* DebugDialog redirection. On Linux, default to OFF, on Win32, default to ON.
602 * Use only if use_gui is enabled
603 */
604 #ifdef WIN32
605 #define DEFAULT_LOG_REDIRECT true
606 #else
607 #define DEFAULT_LOG_REDIRECT false
608 #endif
610 if (use_gui == TRUE && prefs_get_int_attribute("dialogs.debug", "redirect", DEFAULT_LOG_REDIRECT))
611 {
612 Inkscape::UI::Dialogs::DebugDialog::getInstance()->captureLogMessages();
613 }
615 /* Check for global remapping of Alt key */
616 if(use_gui)
617 {
618 inkscape_mapalt(guint(prefs_get_int_attribute("options.mapalt","value",0)));
619 }
621 /* Initialize the extensions */
622 Inkscape::Extension::init();
624 return;
625 }
627 /**
628 * Returns the current Inkscape::Application global object
629 */
630 Inkscape::Application *
631 inkscape_get_instance()
632 {
633 return inkscape;
634 }
636 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
637 {
638 return app->use_gui;
639 }
641 /**
642 * Preference management
643 * We use '.' as separator
644 *
645 * Returns TRUE if the config file was successfully loaded, FALSE if not.
646 */
647 bool
648 inkscape_load_config (const gchar *filename, Inkscape::XML::Document *config, const gchar *skeleton,
649 unsigned int skel_size, const gchar *e_notreg, const gchar *e_notxml,
650 const gchar *e_notsp, const gchar *warn)
651 {
652 gchar *fn = profile_path(filename);
653 if (!Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
654 bool result;
655 /* No such file */
656 result = inkscape_init_config (config, filename, skeleton,
657 skel_size,
658 _("Cannot create directory %s.\n%s"),
659 _("%s is not a valid directory.\n%s"),
660 _("Cannot create file %s.\n%s"),
661 _("Cannot write file %s.\n%s"),
662 _("Although Inkscape will run, it will use default settings,\n"
663 "and any changes made in preferences will not be saved."));
664 g_free (fn);
665 return result;
666 }
668 if (!Inkscape::IO::file_test(fn, G_FILE_TEST_IS_REGULAR)) {
669 /* Not a regular file */
670 gchar *safeFn = Inkscape::IO::sanitizeString(fn);
671 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notreg, safeFn, warn);
672 gtk_dialog_run (GTK_DIALOG (w));
673 gtk_widget_destroy (w);
674 g_free(safeFn);
675 g_free (fn);
676 return false;
677 }
679 Inkscape::XML::Document *doc = sp_repr_read_file (fn, NULL);
680 if (doc == NULL) {
681 /* Not an valid xml file */
682 gchar *safeFn = Inkscape::IO::sanitizeString(fn);
683 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notxml, safeFn, warn);
684 gtk_dialog_run (GTK_DIALOG (w));
685 gtk_widget_destroy (w);
686 g_free(safeFn);
687 g_free (fn);
688 return false;
689 }
691 Inkscape::XML::Node *root = doc->root();
692 if (strcmp (root->name(), "inkscape")) {
693 gchar *safeFn = Inkscape::IO::sanitizeString(fn);
694 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notsp, safeFn, warn);
695 gtk_dialog_run (GTK_DIALOG (w));
696 gtk_widget_destroy (w);
697 Inkscape::GC::release(doc);
698 g_free(safeFn);
699 g_free (fn);
700 return false;
701 }
703 /** \todo this is a hack, need to figure out how to get
704 * a reasonable merge working with the menus.xml file */
705 if (skel_size == MENUS_SKELETON_SIZE) {
706 if (INKSCAPE)
707 INKSCAPE->menus = doc;
708 doc = config;
709 } else {
710 config->root()->mergeFrom(doc->root(), "id");
711 }
713 Inkscape::GC::release(doc);
714 g_free (fn);
715 return true;
716 }
718 /**
719 * Menus management
720 *
721 */
722 bool
723 inkscape_load_menus (Inkscape::Application *inkscape)
724 {
725 gchar *fn = profile_path(MENUS_FILE);
726 bool retval = false;
727 if (Inkscape::IO::file_test(fn, G_FILE_TEST_EXISTS)) {
728 retval = inkscape_load_config (MENUS_FILE,
729 inkscape->menus,
730 menus_skeleton,
731 MENUS_SKELETON_SIZE,
732 _("%s is not a regular file.\n%s"),
733 _("%s not a valid XML file, or\n"
734 "you don't have read permissions on it.\n%s"),
735 _("%s is not a valid menus file.\n%s"),
736 _("Inkscape will run with default menus.\n"
737 "New menus will not be saved."));
738 } else {
739 INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
740 if (INKSCAPE->menus != NULL)
741 retval = true;
742 }
743 g_free(fn);
744 return retval;
745 }
747 /**
748 * We use '.' as separator
749 * \param inkscape Unused
750 */
751 Inkscape::XML::Node *
752 inkscape_get_repr (Inkscape::Application *inkscape, const gchar *key)
753 {
754 if ( (key == NULL) || (inkscape == NULL) ) {
755 return NULL;
756 }
758 Inkscape::XML::Node *prefs = Inkscape::Preferences::get();
759 if ( !prefs ) {
760 return NULL;
761 }
763 Inkscape::XML::Node *repr = prefs->root();
764 if (!repr) return NULL;
765 g_assert (!(strcmp (repr->name(), "inkscape")));
767 gchar const *s = key;
768 while ((s) && (*s)) {
770 /* Find next name */
771 gchar const *e = strchr (s, '.');
772 guint len;
773 if (e) {
774 len = e++ - s;
775 } else {
776 len = strlen (s);
777 }
779 Inkscape::XML::Node* child;
780 for (child = repr->firstChild(); child != NULL; child = child->next()) {
781 gchar const *id = child->attribute("id");
782 if ((id) && (strlen (id) == len) && (!strncmp (id, s, len)))
783 {
784 break;
785 }
786 }
787 if (child == NULL) {
788 return NULL;
789 }
791 repr = child;
792 s = e;
793 }
794 return repr;
795 }
799 void
800 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
801 {
802 if (Inkscape::NSApplication::Application::getNewGui()) {
803 Inkscape::NSApplication::Editor::selectionModified (selection, flags);
804 return;
805 }
806 g_return_if_fail (selection != NULL);
808 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
809 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
810 }
811 }
814 void
815 inkscape_selection_changed (Inkscape::Selection * selection)
816 {
817 if (Inkscape::NSApplication::Application::getNewGui()) {
818 Inkscape::NSApplication::Editor::selectionChanged (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[CHANGE_SELECTION], 0, selection);
825 }
826 }
828 void
829 inkscape_subselection_changed (SPDesktop *desktop)
830 {
831 if (Inkscape::NSApplication::Application::getNewGui()) {
832 Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
833 return;
834 }
835 g_return_if_fail (desktop != NULL);
837 if (DESKTOP_IS_ACTIVE (desktop)) {
838 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
839 }
840 }
843 void
844 inkscape_selection_set (Inkscape::Selection * selection)
845 {
846 if (Inkscape::NSApplication::Application::getNewGui()) {
847 Inkscape::NSApplication::Editor::selectionSet (selection);
848 return;
849 }
850 g_return_if_fail (selection != NULL);
852 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
853 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
854 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
855 }
856 }
859 void
860 inkscape_eventcontext_set (SPEventContext * eventcontext)
861 {
862 if (Inkscape::NSApplication::Application::getNewGui()) {
863 Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
864 return;
865 }
866 g_return_if_fail (eventcontext != NULL);
867 g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
869 if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
870 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
871 }
872 }
875 void
876 inkscape_add_desktop (SPDesktop * desktop)
877 {
878 g_return_if_fail (desktop != NULL);
880 if (Inkscape::NSApplication::Application::getNewGui())
881 {
882 Inkscape::NSApplication::Editor::addDesktop (desktop);
883 return;
884 }
885 g_return_if_fail (inkscape != NULL);
887 g_assert (!g_slist_find (inkscape->desktops, desktop));
889 inkscape->desktops = g_slist_append (inkscape->desktops, desktop);
891 if (DESKTOP_IS_ACTIVE (desktop)) {
892 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
893 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
894 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
895 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
896 }
897 }
901 void
902 inkscape_remove_desktop (SPDesktop * desktop)
903 {
904 g_return_if_fail (desktop != NULL);
905 if (Inkscape::NSApplication::Application::getNewGui())
906 {
907 Inkscape::NSApplication::Editor::removeDesktop (desktop);
908 return;
909 }
910 g_return_if_fail (inkscape != NULL);
912 g_assert (g_slist_find (inkscape->desktops, desktop));
914 if (DESKTOP_IS_ACTIVE (desktop)) {
915 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
916 if (inkscape->desktops->next != NULL) {
917 SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
918 inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
919 inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
920 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
921 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
922 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
923 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
924 } else {
925 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
926 if (sp_desktop_selection(desktop))
927 sp_desktop_selection(desktop)->clear();
928 }
929 }
931 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
933 // if this was the last desktop, shut down the program
934 if (inkscape->desktops == NULL) {
935 inkscape_exit (inkscape);
936 }
937 }
941 void
942 inkscape_activate_desktop (SPDesktop * desktop)
943 {
944 g_return_if_fail (desktop != NULL);
945 if (Inkscape::NSApplication::Application::getNewGui())
946 {
947 Inkscape::NSApplication::Editor::activateDesktop (desktop);
948 return;
949 }
950 g_return_if_fail (inkscape != NULL);
952 if (DESKTOP_IS_ACTIVE (desktop)) {
953 return;
954 }
956 g_assert (g_slist_find (inkscape->desktops, desktop));
958 SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
960 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
962 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
963 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
965 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
966 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
967 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
968 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
969 }
972 /**
973 * Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
974 */
975 void
976 inkscape_reactivate_desktop (SPDesktop * desktop)
977 {
978 g_return_if_fail (desktop != NULL);
979 if (Inkscape::NSApplication::Application::getNewGui())
980 {
981 Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
982 return;
983 }
984 g_return_if_fail (inkscape != NULL);
986 if (DESKTOP_IS_ACTIVE (desktop))
987 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
988 }
992 SPDesktop *
993 inkscape_find_desktop_by_dkey (unsigned int dkey)
994 {
995 for (GSList *r = inkscape->desktops; r; r = r->next) {
996 if (((SPDesktop *) r->data)->dkey == dkey)
997 return ((SPDesktop *) r->data);
998 }
999 return NULL;
1000 }
1005 unsigned int
1006 inkscape_maximum_dkey()
1007 {
1008 unsigned int dkey = 0;
1010 for (GSList *r = inkscape->desktops; r; r = r->next) {
1011 if (((SPDesktop *) r->data)->dkey > dkey)
1012 dkey = ((SPDesktop *) r->data)->dkey;
1013 }
1015 return dkey;
1016 }
1020 SPDesktop *
1021 inkscape_next_desktop ()
1022 {
1023 SPDesktop *d = NULL;
1024 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1026 if (dkey_current < inkscape_maximum_dkey()) {
1027 // find next existing
1028 for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1029 d = inkscape_find_desktop_by_dkey (i);
1030 if (d) {
1031 break;
1032 }
1033 }
1034 } else {
1035 // find first existing
1036 for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1037 d = inkscape_find_desktop_by_dkey (i);
1038 if (d) {
1039 break;
1040 }
1041 }
1042 }
1044 g_assert (d);
1046 return d;
1047 }
1051 SPDesktop *
1052 inkscape_prev_desktop ()
1053 {
1054 SPDesktop *d = NULL;
1055 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1057 if (dkey_current > 0) {
1058 // find prev existing
1059 for (signed int i = dkey_current - 1; i >= 0; i--) {
1060 d = inkscape_find_desktop_by_dkey (i);
1061 if (d) {
1062 break;
1063 }
1064 }
1065 }
1066 if (!d) {
1067 // find last existing
1068 d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1069 }
1071 g_assert (d);
1073 return d;
1074 }
1078 void
1079 inkscape_switch_desktops_next ()
1080 {
1081 inkscape_next_desktop()->presentWindow();
1082 }
1086 void
1087 inkscape_switch_desktops_prev ()
1088 {
1089 inkscape_prev_desktop()->presentWindow();
1090 }
1094 void
1095 inkscape_dialogs_hide ()
1096 {
1097 if (Inkscape::NSApplication::Application::getNewGui())
1098 Inkscape::NSApplication::Editor::hideDialogs();
1099 else
1100 {
1101 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1102 inkscape->dialogs_toggle = FALSE;
1103 }
1104 }
1108 void
1109 inkscape_dialogs_unhide ()
1110 {
1111 if (Inkscape::NSApplication::Application::getNewGui())
1112 Inkscape::NSApplication::Editor::unhideDialogs();
1113 else
1114 {
1115 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1116 inkscape->dialogs_toggle = TRUE;
1117 }
1118 }
1122 void
1123 inkscape_dialogs_toggle ()
1124 {
1125 if (inkscape->dialogs_toggle) {
1126 inkscape_dialogs_hide ();
1127 } else {
1128 inkscape_dialogs_unhide ();
1129 }
1130 }
1132 void
1133 inkscape_external_change ()
1134 {
1135 g_return_if_fail (inkscape != NULL);
1137 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1138 }
1140 /**
1141 * fixme: These need probably signals too
1142 */
1143 void
1144 inkscape_add_document (SPDocument *document)
1145 {
1146 g_return_if_fail (document != NULL);
1148 if (!Inkscape::NSApplication::Application::getNewGui())
1149 {
1150 if ( inkscape->document_set.find(document) != inkscape->document_set.end() ) {
1152 inkscape->documents = g_slist_append (inkscape->documents, document);
1153 }
1154 inkscape->document_set.insert(document);
1155 }
1156 else
1157 {
1158 Inkscape::NSApplication::Editor::addDocument (document);
1159 }
1160 }
1164 void
1165 inkscape_remove_document (SPDocument *document)
1166 {
1167 g_return_if_fail (document != NULL);
1169 if (!Inkscape::NSApplication::Application::getNewGui())
1170 {
1171 inkscape->document_set.erase(document);
1172 if ( inkscape->document_set.find(document) == inkscape->document_set.end() ) {
1173 inkscape->documents = g_slist_remove (inkscape->documents, document);
1174 }
1175 }
1176 else
1177 {
1178 Inkscape::NSApplication::Editor::removeDocument (document);
1179 }
1181 return;
1182 }
1184 SPDesktop *
1185 inkscape_active_desktop (void)
1186 {
1187 if (Inkscape::NSApplication::Application::getNewGui())
1188 return Inkscape::NSApplication::Editor::getActiveDesktop();
1190 if (inkscape->desktops == NULL) {
1191 return NULL;
1192 }
1194 return (SPDesktop *) inkscape->desktops->data;
1195 }
1197 SPDocument *
1198 inkscape_active_document (void)
1199 {
1200 if (Inkscape::NSApplication::Application::getNewGui())
1201 return Inkscape::NSApplication::Editor::getActiveDocument();
1203 if (SP_ACTIVE_DESKTOP) {
1204 return sp_desktop_document (SP_ACTIVE_DESKTOP);
1205 }
1207 return NULL;
1208 }
1210 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1211 SPDocument const* document = desktop.doc();
1212 if (!document) {
1213 return false;
1214 }
1215 for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1216 SPDesktop *other_desktop=(SPDesktop *)iter->data;
1217 SPDocument *other_document=other_desktop->doc();
1218 if ( other_document == document && other_desktop != &desktop ) {
1219 return false;
1220 }
1221 }
1222 return true;
1223 }
1225 SPEventContext *
1226 inkscape_active_event_context (void)
1227 {
1228 if (SP_ACTIVE_DESKTOP) {
1229 return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1230 }
1232 return NULL;
1233 }
1237 /*#####################
1238 # HELPERS
1239 #####################*/
1241 static bool
1242 inkscape_init_config (Inkscape::XML::Document */*doc*/, const gchar *config_name, const gchar *skeleton,
1243 unsigned int skel_size,
1244 const gchar *e_mkdir,
1245 const gchar *e_notdir,
1246 const gchar *e_ccf,
1247 const gchar *e_cwf,
1248 const gchar *warn)
1249 {
1250 gchar *dn = profile_path(NULL);
1251 bool use_gui = (Inkscape::NSApplication::Application::getNewGui())? Inkscape::NSApplication::Application::getUseGui() : inkscape->use_gui;
1252 if (!Inkscape::IO::file_test(dn, G_FILE_TEST_EXISTS)) {
1253 if (Inkscape::IO::mkdir_utf8name(dn))
1254 {
1255 if (use_gui) {
1256 // Cannot create directory
1257 gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1258 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_mkdir, safeDn, warn);
1259 gtk_dialog_run (GTK_DIALOG (w));
1260 gtk_widget_destroy (w);
1261 g_free(safeDn);
1262 g_free (dn);
1263 return false;
1264 } else {
1265 g_warning(e_mkdir, dn, warn);
1266 g_free (dn);
1267 return false;
1268 }
1269 }
1271 // Also create (empty for now) subdirectories for the user's stuff
1272 {
1273 gchar *temp_dn = profile_path("templates");
1274 Inkscape::IO::mkdir_utf8name(temp_dn);
1275 }
1276 {
1277 gchar *temp_dn = profile_path("keys");
1278 Inkscape::IO::mkdir_utf8name(temp_dn);
1279 }
1280 {
1281 gchar *temp_dn = profile_path("icons");
1282 Inkscape::IO::mkdir_utf8name(temp_dn);
1283 }
1284 {
1285 gchar *temp_dn = profile_path("extensions");
1286 Inkscape::IO::mkdir_utf8name(temp_dn);
1287 }
1288 {
1289 gchar *temp_dn = profile_path("palettes");
1290 Inkscape::IO::mkdir_utf8name(temp_dn);
1291 }
1293 } else if (!Inkscape::IO::file_test(dn, G_FILE_TEST_IS_DIR)) {
1294 if (use_gui) {
1295 // Not a directory
1296 gchar *safeDn = Inkscape::IO::sanitizeString(dn);
1297 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_notdir, safeDn, warn);
1298 gtk_dialog_run (GTK_DIALOG (w));
1299 gtk_widget_destroy (w);
1300 g_free( safeDn );
1301 g_free (dn);
1302 return false;
1303 } else {
1304 g_warning(e_notdir, dn, warn);
1305 g_free(dn);
1306 return false;
1307 }
1308 }
1309 g_free (dn);
1311 gchar *fn = profile_path(config_name);
1313 Inkscape::IO::dump_fopen_call(fn, "H");
1314 FILE *fh = Inkscape::IO::fopen_utf8name(fn, "w");
1315 if (!fh) {
1316 if (use_gui) {
1317 /* Cannot create file */
1318 gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1319 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_ccf, safeFn, warn);
1320 gtk_dialog_run (GTK_DIALOG (w));
1321 gtk_widget_destroy (w);
1322 g_free(safeFn);
1323 g_free (fn);
1324 return false;
1325 } else {
1326 g_warning(e_ccf, fn, warn);
1327 g_free(fn);
1328 return false;
1329 }
1330 }
1331 if ( fwrite(skeleton, 1, skel_size, fh) != skel_size ) {
1332 if (use_gui) {
1333 /* Cannot create file */
1334 gchar *safeFn = Inkscape::IO::sanitizeString(fn);
1335 GtkWidget *w = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, e_cwf, safeFn, warn);
1336 gtk_dialog_run (GTK_DIALOG (w));
1337 gtk_widget_destroy (w);
1338 g_free(safeFn);
1339 g_free (fn);
1340 fclose(fh);
1341 return false;
1342 } else {
1343 g_warning(e_cwf, fn, warn);
1344 g_free(fn);
1345 fclose(fh);
1346 return false;
1347 }
1348 }
1350 g_free(fn);
1351 fclose(fh);
1352 return true;
1353 }
1355 void
1356 inkscape_refresh_display (Inkscape::Application *inkscape)
1357 {
1358 for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1359 (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1360 }
1361 }
1364 /**
1365 * Handler for Inkscape's Exit verb. This emits the shutdown signal,
1366 * saves the preferences if appropriate, and quits.
1367 */
1368 void
1369 inkscape_exit (Inkscape::Application */*inkscape*/)
1370 {
1371 g_assert (INKSCAPE);
1373 //emit shutdown signal so that dialogs could remember layout
1374 g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1376 Inkscape::Preferences::save();
1377 gtk_main_quit ();
1378 }
1380 gchar *
1381 homedir_path(const char *filename)
1382 {
1383 static const gchar *homedir = NULL;
1384 if (!homedir) {
1385 homedir = g_get_home_dir();
1386 gchar* utf8Path = g_filename_to_utf8( homedir, -1, NULL, NULL, NULL );
1387 if ( utf8Path )
1388 {
1389 homedir = utf8Path;
1390 if (!g_utf8_validate(homedir, -1, NULL)) {
1391 g_warning( "g_get_home_dir() post A IS NOT UTF-8" );
1392 }
1393 }
1394 }
1395 if (!homedir) {
1396 gchar * path = g_path_get_dirname(INKSCAPE->argv0);
1397 gchar* utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
1398 g_free(path);
1399 if ( utf8Path )
1400 {
1401 homedir = utf8Path;
1402 if (!g_utf8_validate(homedir, -1, NULL)) {
1403 g_warning( "g_get_home_dir() post B IS NOT UTF-8" );
1404 }
1405 }
1406 }
1407 return g_build_filename(homedir, filename, NULL);
1408 }
1411 /**
1412 * Get, or guess, or decide the location where the preferences.xml
1413 * file should be located.
1414 */
1415 gchar *
1416 profile_path(const char *filename)
1417 {
1418 static const gchar *prefdir = NULL;
1419 if (!prefdir) {
1420 #ifdef HAS_SHGetSpecialFolderLocation
1421 // prefer c:\Documents and Settings\UserName\Application Data\ to
1422 // c:\Documents and Settings\userName\;
1423 if (!prefdir) {
1424 ITEMIDLIST *pidl = 0;
1425 if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1426 gchar * utf8Path = NULL;
1428 if ( PrintWin32::is_os_wide() ) {
1429 wchar_t pathBuf[MAX_PATH+1];
1430 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1432 if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1433 utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1434 }
1435 } else {
1436 char pathBuf[MAX_PATH+1];
1438 if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1439 utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1440 }
1441 }
1443 if ( utf8Path ) {
1444 if (!g_utf8_validate(utf8Path, -1, NULL)) {
1445 g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1446 g_free( utf8Path );
1447 utf8Path = 0;
1448 } else {
1449 prefdir = utf8Path;
1450 }
1451 }
1454 /* not compiling yet...
1456 // Remember to free the list pointer
1457 IMalloc * imalloc = 0;
1458 if ( SHGetMalloc(&imalloc) == NOERROR) {
1459 imalloc->lpVtbl->Free( imalloc, pidl );
1460 imalloc->lpVtbl->Release( imalloc );
1461 }
1462 */
1463 }
1464 }
1465 #endif
1466 if (!prefdir) {
1467 prefdir = homedir_path(NULL);
1468 }
1469 }
1470 return g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, filename, NULL);
1471 }
1473 Inkscape::XML::Node *
1474 inkscape_get_menus (Inkscape::Application * inkscape)
1475 {
1476 Inkscape::XML::Node *repr = inkscape->menus->root();
1477 g_assert (!(strcmp (repr->name(), "inkscape")));
1478 return repr->firstChild();
1479 }
1481 void
1482 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1483 {
1484 for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1485 listbuf.push_back(static_cast< SPDesktop* >(l->data));
1486 }
1487 }
1491 /*
1492 Local Variables:
1493 mode:c++
1494 c-file-style:"stroustrup"
1495 c-file-offsets:((innamespace . 0)(inline-open . 0))
1496 indent-tabs-mode:nil
1497 fill-column:99
1498 End:
1499 */
1500 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :