1 /** @file
2 * @brief Legacy interface to main application
3 */
4 /* Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 *
8 * Copyright (C) 1999-2005 authors
9 * g++ port Copyright (C) 2003 Nathan Hurst
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include <errno.h>
20 #include <map>
21 #include "debug/simple-event.h"
22 #include "debug/event-tracker.h"
24 #ifndef WIN32
25 # define HAS_PROC_SELF_EXE //to get path of executable
26 #else
28 // For now to get at is_os_wide().
29 # include "extension/internal/win32.h"
30 using Inkscape::Extension::Internal::PrintWin32;
32 #define _WIN32_IE 0x0400
33 //#define HAS_SHGetSpecialFolderPath
34 #define HAS_SHGetSpecialFolderLocation
35 #define HAS_GetModuleFileName
36 # include <shlobj.h>
37 #endif
39 #include <cstring>
40 #include <glib/gstdio.h>
41 #include <glib.h>
42 #include <glibmm/i18n.h>
43 #include <gtk/gtkmain.h>
44 #include <gtk/gtkmessagedialog.h>
45 #include <gtkmm/messagedialog.h>
46 #include <signal.h>
47 #include <string>
48 #include "desktop.h"
49 #include "desktop-handles.h"
50 #include "device-manager.h"
51 #include "document.h"
52 #include "event-context.h"
53 #include "extension/db.h"
54 #include "extension/init.h"
55 #include "extension/output.h"
56 #include "extension/system.h"
57 //#include "helper/sp-marshal.h"
58 #include "inkscape-private.h"
59 #include "io/sys.h"
60 #include "message-stack.h"
61 #include "preferences.h"
62 #include "selection.h"
63 #include "ui/dialog/debug.h"
64 #include "xml/repr.h"
66 static Inkscape::Application *inkscape = NULL;
68 /* Backbones of configuration xml data */
69 #include "menus-skeleton.h"
71 enum {
72 MODIFY_SELECTION, // global: one of selections modified
73 CHANGE_SELECTION, // global: one of selections changed
74 CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
75 SET_SELECTION, // global: one of selections set
76 SET_EVENTCONTEXT, // tool switched
77 ACTIVATE_DESKTOP, // some desktop got focus
78 DEACTIVATE_DESKTOP, // some desktop lost focus
79 SHUTDOWN_SIGNAL, // inkscape is quitting
80 DIALOGS_HIDE, // user pressed F12
81 DIALOGS_UNHIDE, // user pressed F12
82 EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
83 // may not be reflected by a selection change and thus needs a separate signal
84 LAST_SIGNAL
85 };
87 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
90 /*################################
91 # FORWARD DECLARATIONS
92 ################################*/
94 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
95 static void inkscape_init (SPObject *object);
96 static void inkscape_dispose (GObject *object);
98 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
99 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
101 struct Inkscape::Application {
102 GObject object;
103 Inkscape::XML::Document *menus;
104 std::map<SPDocument *, int> document_set;
105 GSList *desktops;
106 gchar *argv0;
107 gboolean dialogs_toggle;
108 gboolean use_gui; // may want to consider a virtual function
109 // for overriding things like the warning dlg's
110 guint mapalt;
111 guint trackalt;
112 };
114 struct Inkscape::ApplicationClass {
115 GObjectClass object_class;
117 /* Signals */
118 void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
119 void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
120 void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
121 void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
122 void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
123 void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
124 void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
125 void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
126 void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
127 void (* shut_down) (Inkscape::Application *inkscape);
128 void (* dialogs_hide) (Inkscape::Application *inkscape);
129 void (* dialogs_unhide) (Inkscape::Application *inkscape);
130 void (* external_change) (Inkscape::Application *inkscape);
131 };
133 static GObjectClass * parent_class;
134 static guint inkscape_signals[LAST_SIGNAL] = {0};
136 static void (* segv_handler) (int) = SIG_DFL;
137 static void (* abrt_handler) (int) = SIG_DFL;
138 static void (* fpe_handler) (int) = SIG_DFL;
139 static void (* ill_handler) (int) = SIG_DFL;
140 #ifndef WIN32
141 static void (* bus_handler) (int) = SIG_DFL;
142 #endif
144 #define INKSCAPE_PROFILE_DIR "inkscape"
145 #define INKSCAPE_PROFILE_DIR_047DEV "Inkscape"
146 #define INKSCAPE_LEGACY_PROFILE_DIR ".inkscape"
147 #define MENUS_FILE "menus.xml"
150 /**
151 * Retrieves the GType for the Inkscape Application object.
152 */
153 GType
154 inkscape_get_type (void)
155 {
156 static GType type = 0;
157 if (!type) {
158 GTypeInfo info = {
159 sizeof (Inkscape::ApplicationClass),
160 NULL, NULL,
161 (GClassInitFunc) inkscape_class_init,
162 NULL, NULL,
163 sizeof (Inkscape::Application),
164 4,
165 (GInstanceInitFunc) inkscape_init,
166 NULL
167 };
168 type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
169 }
170 return type;
171 }
174 /**
175 * Initializes the inkscape class, registering all of its signal handlers
176 * and virtual functions
177 */
178 static void
179 inkscape_class_init (Inkscape::ApplicationClass * klass)
180 {
181 GObjectClass * object_class;
183 object_class = (GObjectClass *) klass;
185 parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
187 inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
188 G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_FIRST,
190 G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
191 NULL, NULL,
192 g_cclosure_marshal_VOID__UINT_POINTER,
193 G_TYPE_NONE, 2,
194 G_TYPE_POINTER, G_TYPE_UINT);
195 inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
196 G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_FIRST,
198 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
199 NULL, NULL,
200 g_cclosure_marshal_VOID__POINTER,
201 G_TYPE_NONE, 1,
202 G_TYPE_POINTER);
203 inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
204 G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_FIRST,
206 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
207 NULL, NULL,
208 g_cclosure_marshal_VOID__POINTER,
209 G_TYPE_NONE, 1,
210 G_TYPE_POINTER);
211 inkscape_signals[SET_SELECTION] = g_signal_new ("set_selection",
212 G_TYPE_FROM_CLASS (klass),
213 G_SIGNAL_RUN_FIRST,
214 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
215 NULL, NULL,
216 g_cclosure_marshal_VOID__POINTER,
217 G_TYPE_NONE, 1,
218 G_TYPE_POINTER);
219 inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
220 G_TYPE_FROM_CLASS (klass),
221 G_SIGNAL_RUN_FIRST,
222 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
223 NULL, NULL,
224 g_cclosure_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1,
226 G_TYPE_POINTER);
227 inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
228 G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_FIRST,
230 G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
231 NULL, NULL,
232 g_cclosure_marshal_VOID__POINTER,
233 G_TYPE_NONE, 1,
234 G_TYPE_POINTER);
235 inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
236 G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_FIRST,
238 G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
239 NULL, NULL,
240 g_cclosure_marshal_VOID__POINTER,
241 G_TYPE_NONE, 1,
242 G_TYPE_POINTER);
243 inkscape_signals[SHUTDOWN_SIGNAL] = g_signal_new ("shut_down",
244 G_TYPE_FROM_CLASS (klass),
245 G_SIGNAL_RUN_FIRST,
246 G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
247 NULL, NULL,
248 g_cclosure_marshal_VOID__VOID,
249 G_TYPE_NONE, 0);
250 inkscape_signals[DIALOGS_HIDE] = g_signal_new ("dialogs_hide",
251 G_TYPE_FROM_CLASS (klass),
252 G_SIGNAL_RUN_FIRST,
253 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
254 NULL, NULL,
255 g_cclosure_marshal_VOID__VOID,
256 G_TYPE_NONE, 0);
257 inkscape_signals[DIALOGS_UNHIDE] = g_signal_new ("dialogs_unhide",
258 G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_FIRST,
260 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
261 NULL, NULL,
262 g_cclosure_marshal_VOID__VOID,
263 G_TYPE_NONE, 0);
264 inkscape_signals[EXTERNAL_CHANGE] = g_signal_new ("external_change",
265 G_TYPE_FROM_CLASS (klass),
266 G_SIGNAL_RUN_FIRST,
267 G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
268 NULL, NULL,
269 g_cclosure_marshal_VOID__VOID,
270 G_TYPE_NONE, 0);
272 object_class->dispose = inkscape_dispose;
274 klass->activate_desktop = inkscape_activate_desktop_private;
275 klass->deactivate_desktop = inkscape_deactivate_desktop_private;
276 }
278 #ifdef WIN32
279 typedef int uid_t;
280 #define getuid() 0
281 #endif
283 /**
284 * static gint inkscape_autosave(gpointer);
285 *
286 * Callback passed to g_timeout_add_seconds()
287 * Responsible for autosaving all open documents
288 */
289 static gint inkscape_autosave(gpointer)
290 {
291 if (inkscape->document_set.empty()) { // nothing to autosave
292 return TRUE;
293 }
294 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
296 // Use UID for separating autosave-documents between users if directory is multiuser
297 uid_t uid = getuid();
299 Glib::ustring autosave_dir;
300 {
301 Glib::ustring tmp = prefs->getString("/options/autosave/path");
302 if (!tmp.empty()) {
303 autosave_dir = tmp;
304 } else {
305 autosave_dir = Glib::get_tmp_dir();
306 }
307 }
309 GDir *autosave_dir_ptr = g_dir_open(autosave_dir.c_str(), 0, NULL);
310 if( !autosave_dir_ptr ){
311 g_warning("Cannot open autosave directory!");
312 return TRUE;
313 }
315 time_t sptime = time(NULL);
316 struct tm *sptm = localtime(&sptime);
317 gchar sptstr[256];
318 strftime(sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
320 gint autosave_max = prefs->getInt("/options/autosave/max", 10);
322 gint docnum = 0;
324 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosaving documents..."));
325 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
326 iter != inkscape->document_set.end();
327 ++iter) {
329 SPDocument *doc = iter->first;
331 ++docnum;
333 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
334 // g_debug("Document %d: \"%s\" %s", docnum, doc ? doc->name : "(null)", doc ? (doc->isModifiedSinceSave() ? "(dirty)" : "(clean)") : "(null)");
336 if (doc->isModifiedSinceSave()) {
337 gchar *oldest_autosave = 0;
338 const gchar *filename = 0;
339 struct stat sb;
340 time_t min_time = 0;
341 gint count = 0;
343 // Look for previous autosaves
344 gchar* baseName = g_strdup_printf( "inkscape-autosave-%d", uid );
345 g_dir_rewind(autosave_dir_ptr);
346 while( (filename = g_dir_read_name(autosave_dir_ptr)) != NULL ){
347 if ( strncmp(filename, baseName, strlen(baseName)) == 0 ){
348 gchar* full_path = g_build_filename( autosave_dir.c_str(), filename, NULL );
349 if ( g_stat(full_path, &sb) != -1 ) {
350 if ( difftime(sb.st_ctime, min_time) < 0 || min_time == 0 ){
351 min_time = sb.st_ctime;
352 if ( oldest_autosave ) {
353 g_free(oldest_autosave);
354 }
355 oldest_autosave = g_strdup(full_path);
356 }
357 count ++;
358 }
359 g_free(full_path);
360 }
361 }
363 // g_debug("%d previous autosaves exists. Max = %d", count, autosave_max);
365 // Have we reached the limit for number of autosaves?
366 if ( count >= autosave_max ){
367 // Remove the oldest file
368 if ( oldest_autosave ) {
369 unlink(oldest_autosave);
370 }
371 }
373 if ( oldest_autosave ) {
374 g_free(oldest_autosave);
375 oldest_autosave = 0;
376 }
379 // Set the filename we will actually save to
380 g_free(baseName);
381 baseName = g_strdup_printf("inkscape-autosave-%d-%s-%03d.svg", uid, sptstr, docnum);
382 gchar* full_path = g_build_filename(autosave_dir.c_str(), baseName, NULL);
383 g_free(baseName);
384 baseName = 0;
386 // g_debug("Filename: %s", full_path);
388 // Try to save the file
389 FILE *file = Inkscape::IO::fopen_utf8name(full_path, "w");
390 gchar *errortext = 0;
391 if (file) {
392 try{
393 sp_repr_save_stream(repr->document(), file, SP_SVG_NS_URI);
394 } catch (Inkscape::Extension::Output::no_extension_found &e) {
395 errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document."));
396 } catch (Inkscape::Extension::Output::save_failed &e) {
397 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
398 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
399 g_free(safeUri);
400 }
401 fclose(file);
402 }
403 else {
404 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
405 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
406 g_free(safeUri);
407 }
409 if (errortext) {
410 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, errortext);
411 g_warning("%s", errortext);
412 g_free(errortext);
413 }
415 g_free(full_path);
416 }
417 }
418 g_dir_close(autosave_dir_ptr);
420 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosave complete."));
422 return TRUE;
423 }
425 void inkscape_autosave_init()
426 {
427 static guint32 autosave_timeout_id = 0;
428 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
430 // Turn off any previously initiated timeouts
431 if ( autosave_timeout_id ) {
432 g_source_remove(autosave_timeout_id);
433 autosave_timeout_id = 0;
434 }
436 // g_debug("options.autosave.enable = %d", prefs->getBool("/options/autosave/enable", true));
437 // Is autosave enabled?
438 if (!prefs->getBool("/options/autosave/enable", true)){
439 autosave_timeout_id = 0;
440 } else {
441 // Turn on autosave
442 guint32 timeout = prefs->getInt("/options/autosave/interval", 10) * 60;
443 // g_debug("options.autosave.interval = %d", prefs->getInt("/options/autosave/interval", 10));
444 #if GLIB_CHECK_VERSION(2,14,0)
445 autosave_timeout_id = g_timeout_add_seconds(timeout, inkscape_autosave, NULL);
446 #else
447 autosave_timeout_id = g_timeout_add(timeout * 1000, inkscape_autosave, NULL);
448 #endif
449 }
450 }
453 static void
454 inkscape_init (SPObject * object)
455 {
456 if (!inkscape) {
457 inkscape = (Inkscape::Application *) object;
458 } else {
459 g_assert_not_reached ();
460 }
462 new (&inkscape->document_set) std::map<SPDocument *, int>();
464 inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
465 inkscape->desktops = NULL;
466 inkscape->dialogs_toggle = TRUE;
467 inkscape->mapalt = GDK_MOD1_MASK;
468 inkscape->trackalt = FALSE;
469 }
471 static void
472 inkscape_dispose (GObject *object)
473 {
474 Inkscape::Application *inkscape = (Inkscape::Application *) object;
476 g_assert (!inkscape->desktops);
478 Inkscape::Preferences::unload();
480 if (inkscape->menus) {
481 /* fixme: This is not the best place */
482 Inkscape::GC::release(inkscape->menus);
483 inkscape->menus = NULL;
484 }
486 inkscape->document_set.~map();
488 G_OBJECT_CLASS (parent_class)->dispose (object);
490 gtk_main_quit ();
491 }
494 void
495 inkscape_ref (void)
496 {
497 if (inkscape)
498 g_object_ref (G_OBJECT (inkscape));
499 }
502 void
503 inkscape_unref (void)
504 {
505 if (inkscape)
506 g_object_unref (G_OBJECT (inkscape));
507 }
509 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
510 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
511 guint
512 inkscape_mapalt() {
513 return inkscape->mapalt;
514 }
516 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
517 void inkscape_mapalt(guint maskvalue)
518 {
519 if(maskvalue<2 || maskvalue> 5 ){ /* MOD5 is the highest defined in gdktypes.h */
520 inkscape->mapalt=0;
521 }else{
522 inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
523 }
524 }
526 guint
527 inkscape_trackalt() {
528 return inkscape->trackalt;
529 }
531 void inkscape_trackalt(guint trackvalue)
532 {
533 inkscape->trackalt = trackvalue;
534 }
537 static void
538 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
539 {
540 desktop->set_active (true);
541 }
544 static void
545 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
546 {
547 desktop->set_active (false);
548 }
551 /* fixme: This is EVIL, and belongs to main after all */
553 #define SP_INDENT 8
556 static void
557 inkscape_crash_handler (int /*signum*/)
558 {
559 using Inkscape::Debug::SimpleEvent;
560 using Inkscape::Debug::EventTracker;
561 using Inkscape::Debug::Logger;
563 static gint recursion = FALSE;
565 /*
566 * reset all signal handlers: any further crashes should just be allowed
567 * to crash normally.
568 * */
569 signal (SIGSEGV, segv_handler );
570 signal (SIGABRT, abrt_handler );
571 signal (SIGFPE, fpe_handler );
572 signal (SIGILL, ill_handler );
573 #ifndef WIN32
574 signal (SIGBUS, bus_handler );
575 #endif
577 /* Stop bizarre loops */
578 if (recursion) {
579 abort ();
580 }
581 recursion = TRUE;
583 EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
584 tracker.set<SimpleEvent<> >("emergency-save");
586 fprintf(stderr, "\nEmergency save activated!\n");
588 time_t sptime = time (NULL);
589 struct tm *sptm = localtime (&sptime);
590 gchar sptstr[256];
591 strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
593 gint count = 0;
594 gchar *curdir = g_get_current_dir(); // This one needs to be freed explicitly
595 gchar *inkscapedir = g_path_get_dirname(INKSCAPE->argv0); // Needs to be freed
596 GSList *savednames = NULL;
597 GSList *failednames = NULL;
598 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
599 iter != inkscape->document_set.end();
600 ++iter) {
601 SPDocument *doc = iter->first;
602 Inkscape::XML::Node *repr;
603 repr = sp_document_repr_root (doc);
604 if (doc->isModifiedSinceSave()) {
605 const gchar *docname;
607 /* originally, the document name was retrieved from
608 * the sodipod:docname attribute */
609 docname = doc->name;
610 if (docname) {
611 /* Removes an emergency save suffix if present: /(.*)\.[0-9_]*\.[0-9_]*\.[~\.]*$/\1/ */
612 const char* d0 = strrchr ((char*)docname, '.');
613 if (d0 && (d0 > docname)) {
614 const char* d = d0;
615 unsigned int dots = 0;
616 while ((isdigit (*d) || *d=='_' || *d=='.') && d>docname && dots<2) {
617 d -= 1;
618 if (*d=='.') dots++;
619 }
620 if (*d=='.' && d>docname && dots==2) {
621 char n[64];
622 size_t len = MIN (d - docname, 63);
623 memcpy (n, docname, len);
624 n[len] = '\0';
625 docname = n;
626 }
627 }
628 }
629 if (!docname || !*docname) docname = "emergency";
631 // Emergency filename
632 char c[1024];
633 g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
635 // Find a location
636 const char* locations[] = {
637 doc->base,
638 g_get_home_dir(),
639 g_get_tmp_dir(),
640 curdir,
641 inkscapedir
642 };
643 FILE *file = 0;
644 for(size_t i=0; i<sizeof(locations)/sizeof(*locations); i++) {
645 if (!locations[i]) continue; // It seems to be okay, but just in case
646 gchar * filename = g_build_filename(locations[i], c, NULL);
647 Inkscape::IO::dump_fopen_call(filename, "E");
648 file = Inkscape::IO::fopen_utf8name(filename, "w");
649 if (file) {
650 g_snprintf (c, 1024, "%s", filename); // we want the complete path to be stored in c (for reporting purposes)
651 break;
652 }
653 }
655 // Save
656 if (file) {
657 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
658 savednames = g_slist_prepend (savednames, g_strdup (c));
659 fclose (file);
660 } else {
661 failednames = g_slist_prepend (failednames, (doc->name) ? g_strdup (doc->name) : g_strdup (_("Untitled document")));
662 }
663 count++;
664 }
665 }
666 g_free(curdir);
667 g_free(inkscapedir);
669 savednames = g_slist_reverse (savednames);
670 failednames = g_slist_reverse (failednames);
671 if (savednames) {
672 fprintf (stderr, "\nEmergency save document locations:\n");
673 for (GSList *l = savednames; l != NULL; l = l->next) {
674 fprintf (stderr, " %s\n", (gchar *) l->data);
675 }
676 }
677 if (failednames) {
678 fprintf (stderr, "\nFailed to do emergency save for documents:\n");
679 for (GSList *l = failednames; l != NULL; l = l->next) {
680 fprintf (stderr, " %s\n", (gchar *) l->data);
681 }
682 }
684 // do not save the preferences since they can be in a corrupted state
685 Inkscape::Preferences::unload(false);
687 fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
688 fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
689 fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
691 /* Show nice dialog box */
693 char const *istr = _("Inkscape encountered an internal error and will close now.\n");
694 char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
695 char const *fstr = _("Automatic backup of the following documents failed:\n");
696 gint nllen = strlen ("\n");
697 gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
698 for (GSList *l = savednames; l != NULL; l = l->next) {
699 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
700 }
701 for (GSList *l = failednames; l != NULL; l = l->next) {
702 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
703 }
704 len += 1;
705 gchar *b = g_new (gchar, len);
706 gint pos = 0;
707 len = strlen (istr);
708 memcpy (b + pos, istr, len);
709 pos += len;
710 if (savednames) {
711 len = strlen (sstr);
712 memcpy (b + pos, sstr, len);
713 pos += len;
714 for (GSList *l = savednames; l != NULL; l = l->next) {
715 memset (b + pos, ' ', SP_INDENT);
716 pos += SP_INDENT;
717 len = strlen ((gchar *) l->data);
718 memcpy (b + pos, l->data, len);
719 pos += len;
720 memcpy (b + pos, "\n", nllen);
721 pos += nllen;
722 }
723 }
724 if (failednames) {
725 len = strlen (fstr);
726 memcpy (b + pos, fstr, len);
727 pos += len;
728 for (GSList *l = failednames; l != NULL; l = l->next) {
729 memset (b + pos, ' ', SP_INDENT);
730 pos += SP_INDENT;
731 len = strlen ((gchar *) l->data);
732 memcpy (b + pos, l->data, len);
733 pos += len;
734 memcpy (b + pos, "\n", nllen);
735 pos += nllen;
736 }
737 }
738 *(b + pos) = '\0';
740 if ( inkscape_get_instance() && inkscape_use_gui() ) {
741 GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
742 gtk_dialog_run (GTK_DIALOG (msgbox));
743 gtk_widget_destroy (msgbox);
744 }
745 else
746 {
747 g_message( "Error: %s", b );
748 }
749 g_free (b);
751 tracker.clear();
752 Logger::shutdown();
754 /* on exit, allow restored signal handler to take over and crash us */
755 }
758 class InkErrorHandler : public Inkscape::ErrorReporter {
759 public:
760 InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(),
761 _useGui(useGui)
762 {}
763 virtual ~InkErrorHandler() {}
765 virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const
766 {
767 if (_useGui) {
768 Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
769 err.set_secondary_text(secondary);
770 err.run();
771 } else {
772 g_message("%s", primary.data());
773 g_message("%s", secondary.data());
774 }
775 }
777 private:
778 bool _useGui;
779 };
781 void
782 inkscape_application_init (const gchar *argv0, gboolean use_gui)
783 {
784 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
785 /* fixme: load application defaults */
787 segv_handler = signal (SIGSEGV, inkscape_crash_handler);
788 abrt_handler = signal (SIGABRT, inkscape_crash_handler);
789 fpe_handler = signal (SIGFPE, inkscape_crash_handler);
790 ill_handler = signal (SIGILL, inkscape_crash_handler);
791 #ifndef WIN32
792 bus_handler = signal (SIGBUS, inkscape_crash_handler);
793 #endif
795 inkscape->use_gui = use_gui;
796 inkscape->argv0 = g_strdup(argv0);
798 /* Load the preferences and menus */
799 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
800 InkErrorHandler* handler = new InkErrorHandler(use_gui);
801 prefs->setErrorHandler(handler);
802 {
803 Glib::ustring msg;
804 Glib::ustring secondary;
805 if (prefs->getLastError( msg, secondary )) {
806 handler->handleError(msg, secondary);
807 }
808 }
810 if (use_gui) {
811 inkscape_load_menus(inkscape);
812 Inkscape::DeviceManager::getManager().loadConfig();
813 }
815 /* set language for user interface according setting in preferences */
816 Glib::ustring ui_language = prefs->getString("/ui/language");
817 if(!ui_language.empty())
818 {
819 setenv("LANGUAGE", ui_language, 1);
820 }
822 /* DebugDialog redirection. On Linux, default to OFF, on Win32, default to ON.
823 * Use only if use_gui is enabled
824 */
825 #ifdef WIN32
826 #define DEFAULT_LOG_REDIRECT true
827 #else
828 #define DEFAULT_LOG_REDIRECT false
829 #endif
831 if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
832 {
833 Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
834 }
836 /* Check for global remapping of Alt key */
837 if (use_gui)
838 {
839 inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
840 inkscape_trackalt(guint(prefs->getInt("/options/trackalt/value", 0)));
841 }
843 /* Initialize the extensions */
844 Inkscape::Extension::init();
846 inkscape_autosave_init();
848 return;
849 }
851 /**
852 * Returns the current Inkscape::Application global object
853 */
854 Inkscape::Application *
855 inkscape_get_instance()
856 {
857 return inkscape;
858 }
860 gboolean inkscape_use_gui()
861 {
862 return inkscape_get_instance()->use_gui;
863 }
865 /**
866 * Menus management
867 *
868 */
869 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
870 {
871 // TODO fix that fn is being leaked
872 gchar *fn = profile_path(MENUS_FILE);
873 gchar *menus_xml = NULL;
874 gsize len = 0;
876 if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
877 // load the menus_xml file
878 INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
879 g_free(menus_xml);
880 if (INKSCAPE->menus) {
881 return true;
882 }
883 }
884 INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
885 return (INKSCAPE->menus != 0);
886 }
889 void
890 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
891 {
892 g_return_if_fail (selection != NULL);
894 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
895 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
896 }
897 }
900 void
901 inkscape_selection_changed (Inkscape::Selection * selection)
902 {
903 g_return_if_fail (selection != NULL);
905 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
906 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
907 }
908 }
910 void
911 inkscape_subselection_changed (SPDesktop *desktop)
912 {
913 g_return_if_fail (desktop != NULL);
915 if (DESKTOP_IS_ACTIVE (desktop)) {
916 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
917 }
918 }
921 void
922 inkscape_selection_set (Inkscape::Selection * selection)
923 {
924 g_return_if_fail (selection != NULL);
926 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
927 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
928 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
929 }
930 }
933 void
934 inkscape_eventcontext_set (SPEventContext * eventcontext)
935 {
936 g_return_if_fail (eventcontext != NULL);
937 g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
939 if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
940 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
941 }
942 }
945 void
946 inkscape_add_desktop (SPDesktop * desktop)
947 {
948 g_return_if_fail (desktop != NULL);
949 g_return_if_fail (inkscape != NULL);
951 g_assert (!g_slist_find (inkscape->desktops, desktop));
953 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
955 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
956 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
957 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
958 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
959 }
963 void
964 inkscape_remove_desktop (SPDesktop * desktop)
965 {
966 g_return_if_fail (desktop != NULL);
967 g_return_if_fail (inkscape != NULL);
969 g_assert (g_slist_find (inkscape->desktops, desktop));
971 if (DESKTOP_IS_ACTIVE (desktop)) {
972 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
973 if (inkscape->desktops->next != NULL) {
974 SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
975 inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
976 inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
977 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
978 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
979 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
980 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
981 } else {
982 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
983 if (sp_desktop_selection(desktop))
984 sp_desktop_selection(desktop)->clear();
985 }
986 }
988 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
990 // if this was the last desktop, shut down the program
991 if (inkscape->desktops == NULL) {
992 inkscape_exit (inkscape);
993 }
994 }
998 void
999 inkscape_activate_desktop (SPDesktop * desktop)
1000 {
1001 g_return_if_fail (desktop != NULL);
1002 g_return_if_fail (inkscape != NULL);
1004 if (DESKTOP_IS_ACTIVE (desktop)) {
1005 return;
1006 }
1008 g_assert (g_slist_find (inkscape->desktops, desktop));
1010 SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
1012 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
1014 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1015 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
1017 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1018 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
1019 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
1020 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
1021 }
1024 /**
1025 * Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
1026 */
1027 void
1028 inkscape_reactivate_desktop (SPDesktop * desktop)
1029 {
1030 g_return_if_fail (desktop != NULL);
1031 g_return_if_fail (inkscape != NULL);
1033 if (DESKTOP_IS_ACTIVE (desktop))
1034 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1035 }
1039 SPDesktop *
1040 inkscape_find_desktop_by_dkey (unsigned int dkey)
1041 {
1042 for (GSList *r = inkscape->desktops; r; r = r->next) {
1043 if (((SPDesktop *) r->data)->dkey == dkey)
1044 return ((SPDesktop *) r->data);
1045 }
1046 return NULL;
1047 }
1052 unsigned int
1053 inkscape_maximum_dkey()
1054 {
1055 unsigned int dkey = 0;
1057 for (GSList *r = inkscape->desktops; r; r = r->next) {
1058 if (((SPDesktop *) r->data)->dkey > dkey)
1059 dkey = ((SPDesktop *) r->data)->dkey;
1060 }
1062 return dkey;
1063 }
1067 SPDesktop *
1068 inkscape_next_desktop ()
1069 {
1070 SPDesktop *d = NULL;
1071 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1073 if (dkey_current < inkscape_maximum_dkey()) {
1074 // find next existing
1075 for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1076 d = inkscape_find_desktop_by_dkey (i);
1077 if (d) {
1078 break;
1079 }
1080 }
1081 } else {
1082 // find first existing
1083 for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1084 d = inkscape_find_desktop_by_dkey (i);
1085 if (d) {
1086 break;
1087 }
1088 }
1089 }
1091 g_assert (d);
1093 return d;
1094 }
1098 SPDesktop *
1099 inkscape_prev_desktop ()
1100 {
1101 SPDesktop *d = NULL;
1102 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1104 if (dkey_current > 0) {
1105 // find prev existing
1106 for (signed int i = dkey_current - 1; i >= 0; i--) {
1107 d = inkscape_find_desktop_by_dkey (i);
1108 if (d) {
1109 break;
1110 }
1111 }
1112 }
1113 if (!d) {
1114 // find last existing
1115 d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1116 }
1118 g_assert (d);
1120 return d;
1121 }
1125 void
1126 inkscape_switch_desktops_next ()
1127 {
1128 inkscape_next_desktop()->presentWindow();
1129 }
1133 void
1134 inkscape_switch_desktops_prev ()
1135 {
1136 inkscape_prev_desktop()->presentWindow();
1137 }
1141 void
1142 inkscape_dialogs_hide ()
1143 {
1144 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1145 inkscape->dialogs_toggle = FALSE;
1146 }
1150 void
1151 inkscape_dialogs_unhide ()
1152 {
1153 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1154 inkscape->dialogs_toggle = TRUE;
1155 }
1159 void
1160 inkscape_dialogs_toggle ()
1161 {
1162 if (inkscape->dialogs_toggle) {
1163 inkscape_dialogs_hide ();
1164 } else {
1165 inkscape_dialogs_unhide ();
1166 }
1167 }
1169 void
1170 inkscape_external_change ()
1171 {
1172 g_return_if_fail (inkscape != NULL);
1174 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1175 }
1177 /**
1178 * fixme: These need probably signals too
1179 */
1180 void
1181 inkscape_add_document (SPDocument *document)
1182 {
1183 g_return_if_fail (document != NULL);
1185 // try to insert the pair into the list
1186 if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1187 //insert failed, this key (document) is already in the list
1188 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1189 iter != inkscape->document_set.end();
1190 ++iter) {
1191 if (iter->first == document) {
1192 // found this document in list, increase its count
1193 iter->second ++;
1194 }
1195 }
1196 }
1197 }
1200 // returns true if this was last reference to this document, so you can delete it
1201 bool
1202 inkscape_remove_document (SPDocument *document)
1203 {
1204 g_return_val_if_fail (document != NULL, false);
1206 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1207 iter != inkscape->document_set.end();
1208 ++iter) {
1209 if (iter->first == document) {
1210 // found this document in list, decrease its count
1211 iter->second --;
1212 if (iter->second < 1) {
1213 // this was the last one, remove the pair from list
1214 inkscape->document_set.erase (iter);
1215 return true;
1216 } else {
1217 return false;
1218 }
1219 }
1220 }
1222 return false;
1223 }
1225 SPDesktop *
1226 inkscape_active_desktop (void)
1227 {
1228 if (inkscape->desktops == NULL) {
1229 return NULL;
1230 }
1232 return (SPDesktop *) inkscape->desktops->data;
1233 }
1235 SPDocument *
1236 inkscape_active_document (void)
1237 {
1238 if (SP_ACTIVE_DESKTOP) {
1239 return sp_desktop_document (SP_ACTIVE_DESKTOP);
1240 }
1242 return NULL;
1243 }
1245 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1246 SPDocument const* document = desktop.doc();
1247 if (!document) {
1248 return false;
1249 }
1250 for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1251 SPDesktop *other_desktop=(SPDesktop *)iter->data;
1252 SPDocument *other_document=other_desktop->doc();
1253 if ( other_document == document && other_desktop != &desktop ) {
1254 return false;
1255 }
1256 }
1257 return true;
1258 }
1260 SPEventContext *
1261 inkscape_active_event_context (void)
1262 {
1263 if (SP_ACTIVE_DESKTOP) {
1264 return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1265 }
1267 return NULL;
1268 }
1272 /*#####################
1273 # HELPERS
1274 #####################*/
1276 void
1277 inkscape_refresh_display (Inkscape::Application *inkscape)
1278 {
1279 for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1280 (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1281 }
1282 }
1285 /**
1286 * Handler for Inkscape's Exit verb. This emits the shutdown signal,
1287 * saves the preferences if appropriate, and quits.
1288 */
1289 void
1290 inkscape_exit (Inkscape::Application */*inkscape*/)
1291 {
1292 g_assert (INKSCAPE);
1294 //emit shutdown signal so that dialogs could remember layout
1295 g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1297 Inkscape::Preferences::unload();
1298 gtk_main_quit ();
1299 }
1301 char *
1302 homedir_path(const char *filename)
1303 {
1304 static const gchar *homedir = NULL;
1305 if (!homedir) {
1306 homedir = g_get_home_dir();
1307 }
1308 if (!homedir) {
1309 homedir = g_path_get_dirname(INKSCAPE->argv0);
1310 }
1311 return g_build_filename(homedir, filename, NULL);
1312 }
1315 /**
1316 * Get, or guess, or decide the location where the preferences.xml
1317 * file should be located.
1318 */
1319 gchar *
1320 profile_path(const char *filename)
1321 {
1322 static const gchar *prefdir = NULL;
1325 if (!prefdir) {
1326 // First check for a custom environment variable for a "portable app"
1327 gchar const *val = g_getenv("INKSCAPE_PORTABLE_PROFILE_DIR");
1328 if (val) {
1329 prefdir = g_strdup(val);
1330 }
1332 #ifdef HAS_SHGetSpecialFolderLocation
1333 // prefer c:\Documents and Settings\UserName\Application Data\ to
1334 // c:\Documents and Settings\userName\;
1335 if (!prefdir) {
1336 ITEMIDLIST *pidl = 0;
1337 if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1338 gchar * utf8Path = NULL;
1340 if ( PrintWin32::is_os_wide() ) {
1341 wchar_t pathBuf[MAX_PATH+1];
1342 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1344 if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1345 utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1346 }
1347 } else {
1348 char pathBuf[MAX_PATH+1];
1350 if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1351 utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1352 }
1353 }
1355 if ( utf8Path ) {
1356 if (!g_utf8_validate(utf8Path, -1, NULL)) {
1357 g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1358 g_free( utf8Path );
1359 utf8Path = 0;
1360 } else {
1361 prefdir = utf8Path;
1362 }
1363 }
1366 /* not compiling yet...
1368 // Remember to free the list pointer
1369 IMalloc * imalloc = 0;
1370 if ( SHGetMalloc(&imalloc) == NOERROR) {
1371 imalloc->lpVtbl->Free( imalloc, pidl );
1372 imalloc->lpVtbl->Release( imalloc );
1373 }
1374 */
1375 }
1377 if (prefdir) {
1378 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1379 }
1380 }
1381 #endif
1382 if (!prefdir) {
1383 prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1384 gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1385 gchar * dev47Dir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR_047DEV, NULL);
1387 bool needsMigration = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) );
1388 if (needsMigration) {
1389 // TODO here is a point to hook in preference migration
1390 g_warning("Preferences need to be migrated from 0.46 or older %s to %s", legacyDir, prefdir);
1391 Inkscape::Preferences::migrate( legacyDir, prefdir );
1392 }
1394 bool needsRenameWarning = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( dev47Dir, G_FILE_TEST_EXISTS ) );
1395 if (needsRenameWarning) {
1396 g_warning("Preferences need to be copied from %s to %s", legacyDir, prefdir);
1397 }
1399 g_free(legacyDir);
1400 legacyDir = 0;
1401 g_free(dev47Dir);
1402 dev47Dir = 0;
1403 // In case the XDG user config dir of the moment does not yet exist...
1404 int mode = S_IRWXU;
1405 #ifdef S_IRGRP
1406 mode |= S_IRGRP;
1407 #endif
1408 #ifdef S_IXGRP
1409 mode |= S_IXGRP;
1410 #endif
1411 #ifdef S_IXOTH
1412 mode |= S_IXOTH;
1413 #endif
1414 if ( g_mkdir_with_parents(prefdir, mode) == -1 ) {
1415 int problem = errno;
1416 g_warning("Unable to create profile directory (%s) (%d)", g_strerror(problem), problem);
1417 } else {
1418 gchar const *userDirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
1419 for (gchar const** name = userDirs; *name; ++name) {
1420 gchar *dir = g_build_filename(prefdir, *name, NULL);
1421 g_mkdir_with_parents(dir, mode);
1422 g_free(dir);
1423 }
1424 }
1425 }
1426 }
1427 return g_build_filename(prefdir, filename, NULL);
1428 }
1430 Inkscape::XML::Node *
1431 inkscape_get_menus (Inkscape::Application * inkscape)
1432 {
1433 Inkscape::XML::Node *repr = inkscape->menus->root();
1434 g_assert (!(strcmp (repr->name(), "inkscape")));
1435 return repr->firstChild();
1436 }
1438 void
1439 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1440 {
1441 for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1442 listbuf.push_back(static_cast< SPDesktop* >(l->data));
1443 }
1444 }
1446 /*
1447 Local Variables:
1448 mode:c++
1449 c-file-style:"stroustrup"
1450 c-file-offsets:((innamespace . 0)(inline-open . 0))
1451 indent-tabs-mode:nil
1452 fill-column:99
1453 End:
1454 */
1455 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :