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 "application/application.h"
49 #include "application/editor.h"
50 #include "desktop.h"
51 #include "desktop-handles.h"
52 #include "device-manager.h"
53 #include "document.h"
54 #include "event-context.h"
55 #include "extension/db.h"
56 #include "extension/init.h"
57 #include "extension/output.h"
58 #include "extension/system.h"
59 //#include "helper/sp-marshal.h"
60 #include "inkscape-private.h"
61 #include "io/sys.h"
62 #include "message-stack.h"
63 #include "preferences.h"
64 #include "selection.h"
65 #include "ui/dialog/debug.h"
66 #include "xml/repr.h"
68 static Inkscape::Application *inkscape = NULL;
70 /* Backbones of configuration xml data */
71 #include "menus-skeleton.h"
73 enum {
74 MODIFY_SELECTION, // global: one of selections modified
75 CHANGE_SELECTION, // global: one of selections changed
76 CHANGE_SUBSELECTION, // global: one of subselections (text selection, gradient handle, etc) changed
77 SET_SELECTION, // global: one of selections set
78 SET_EVENTCONTEXT, // tool switched
79 ACTIVATE_DESKTOP, // some desktop got focus
80 DEACTIVATE_DESKTOP, // some desktop lost focus
81 SHUTDOWN_SIGNAL, // inkscape is quitting
82 DIALOGS_HIDE, // user pressed F12
83 DIALOGS_UNHIDE, // user pressed F12
84 EXTERNAL_CHANGE, // a document was changed by some external means (undo or XML editor); this
85 // may not be reflected by a selection change and thus needs a separate signal
86 LAST_SIGNAL
87 };
89 #define DESKTOP_IS_ACTIVE(d) ((d) == inkscape->desktops->data)
92 /*################################
93 # FORWARD DECLARATIONS
94 ################################*/
96 gboolean inkscape_app_use_gui( Inkscape::Application const * app );
98 static void inkscape_class_init (Inkscape::ApplicationClass *klass);
99 static void inkscape_init (SPObject *object);
100 static void inkscape_dispose (GObject *object);
102 static void inkscape_activate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
103 static void inkscape_deactivate_desktop_private (Inkscape::Application *inkscape, SPDesktop *desktop);
105 struct Inkscape::Application {
106 GObject object;
107 Inkscape::XML::Document *menus;
108 std::map<SPDocument *, int> document_set;
109 GSList *desktops;
110 gchar *argv0;
111 gboolean dialogs_toggle;
112 gboolean use_gui; // may want to consider a virtual function
113 // for overriding things like the warning dlg's
114 guint mapalt;
115 guint trackalt;
116 };
118 struct Inkscape::ApplicationClass {
119 GObjectClass object_class;
121 /* Signals */
122 void (* change_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
123 void (* change_subselection) (Inkscape::Application * inkscape, SPDesktop *desktop);
124 void (* modify_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection, guint flags);
125 void (* set_selection) (Inkscape::Application * inkscape, Inkscape::Selection * selection);
126 void (* set_eventcontext) (Inkscape::Application * inkscape, SPEventContext * eventcontext);
127 void (* activate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
128 void (* deactivate_desktop) (Inkscape::Application * inkscape, SPDesktop * desktop);
129 void (* destroy_document) (Inkscape::Application *inkscape, SPDocument *doc);
130 void (* color_set) (Inkscape::Application *inkscape, SPColor *color, double opacity);
131 void (* shut_down) (Inkscape::Application *inkscape);
132 void (* dialogs_hide) (Inkscape::Application *inkscape);
133 void (* dialogs_unhide) (Inkscape::Application *inkscape);
134 void (* external_change) (Inkscape::Application *inkscape);
135 };
137 static GObjectClass * parent_class;
138 static guint inkscape_signals[LAST_SIGNAL] = {0};
140 static void (* segv_handler) (int) = SIG_DFL;
141 static void (* abrt_handler) (int) = SIG_DFL;
142 static void (* fpe_handler) (int) = SIG_DFL;
143 static void (* ill_handler) (int) = SIG_DFL;
144 #ifndef WIN32
145 static void (* bus_handler) (int) = SIG_DFL;
146 #endif
148 #define INKSCAPE_PROFILE_DIR "inkscape"
149 #define INKSCAPE_PROFILE_DIR_047DEV "Inkscape"
150 #define INKSCAPE_LEGACY_PROFILE_DIR ".inkscape"
151 #define MENUS_FILE "menus.xml"
154 /**
155 * Retrieves the GType for the Inkscape Application object.
156 */
157 GType
158 inkscape_get_type (void)
159 {
160 static GType type = 0;
161 if (!type) {
162 GTypeInfo info = {
163 sizeof (Inkscape::ApplicationClass),
164 NULL, NULL,
165 (GClassInitFunc) inkscape_class_init,
166 NULL, NULL,
167 sizeof (Inkscape::Application),
168 4,
169 (GInstanceInitFunc) inkscape_init,
170 NULL
171 };
172 type = g_type_register_static (G_TYPE_OBJECT, "Inkscape_Application", &info, (GTypeFlags)0);
173 }
174 return type;
175 }
178 /**
179 * Initializes the inkscape class, registering all of its signal handlers
180 * and virtual functions
181 */
182 static void
183 inkscape_class_init (Inkscape::ApplicationClass * klass)
184 {
185 GObjectClass * object_class;
187 object_class = (GObjectClass *) klass;
189 parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
191 inkscape_signals[MODIFY_SELECTION] = g_signal_new ("modify_selection",
192 G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_FIRST,
194 G_STRUCT_OFFSET (Inkscape::ApplicationClass, modify_selection),
195 NULL, NULL,
196 g_cclosure_marshal_VOID__UINT_POINTER,
197 G_TYPE_NONE, 2,
198 G_TYPE_POINTER, G_TYPE_UINT);
199 inkscape_signals[CHANGE_SELECTION] = g_signal_new ("change_selection",
200 G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_FIRST,
202 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_selection),
203 NULL, NULL,
204 g_cclosure_marshal_VOID__POINTER,
205 G_TYPE_NONE, 1,
206 G_TYPE_POINTER);
207 inkscape_signals[CHANGE_SUBSELECTION] = g_signal_new ("change_subselection",
208 G_TYPE_FROM_CLASS (klass),
209 G_SIGNAL_RUN_FIRST,
210 G_STRUCT_OFFSET (Inkscape::ApplicationClass, change_subselection),
211 NULL, NULL,
212 g_cclosure_marshal_VOID__POINTER,
213 G_TYPE_NONE, 1,
214 G_TYPE_POINTER);
215 inkscape_signals[SET_SELECTION] = g_signal_new ("set_selection",
216 G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_FIRST,
218 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_selection),
219 NULL, NULL,
220 g_cclosure_marshal_VOID__POINTER,
221 G_TYPE_NONE, 1,
222 G_TYPE_POINTER);
223 inkscape_signals[SET_EVENTCONTEXT] = g_signal_new ("set_eventcontext",
224 G_TYPE_FROM_CLASS (klass),
225 G_SIGNAL_RUN_FIRST,
226 G_STRUCT_OFFSET (Inkscape::ApplicationClass, set_eventcontext),
227 NULL, NULL,
228 g_cclosure_marshal_VOID__POINTER,
229 G_TYPE_NONE, 1,
230 G_TYPE_POINTER);
231 inkscape_signals[ACTIVATE_DESKTOP] = g_signal_new ("activate_desktop",
232 G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_FIRST,
234 G_STRUCT_OFFSET (Inkscape::ApplicationClass, activate_desktop),
235 NULL, NULL,
236 g_cclosure_marshal_VOID__POINTER,
237 G_TYPE_NONE, 1,
238 G_TYPE_POINTER);
239 inkscape_signals[DEACTIVATE_DESKTOP] = g_signal_new ("deactivate_desktop",
240 G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_FIRST,
242 G_STRUCT_OFFSET (Inkscape::ApplicationClass, deactivate_desktop),
243 NULL, NULL,
244 g_cclosure_marshal_VOID__POINTER,
245 G_TYPE_NONE, 1,
246 G_TYPE_POINTER);
247 inkscape_signals[SHUTDOWN_SIGNAL] = g_signal_new ("shut_down",
248 G_TYPE_FROM_CLASS (klass),
249 G_SIGNAL_RUN_FIRST,
250 G_STRUCT_OFFSET (Inkscape::ApplicationClass, shut_down),
251 NULL, NULL,
252 g_cclosure_marshal_VOID__VOID,
253 G_TYPE_NONE, 0);
254 inkscape_signals[DIALOGS_HIDE] = g_signal_new ("dialogs_hide",
255 G_TYPE_FROM_CLASS (klass),
256 G_SIGNAL_RUN_FIRST,
257 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_hide),
258 NULL, NULL,
259 g_cclosure_marshal_VOID__VOID,
260 G_TYPE_NONE, 0);
261 inkscape_signals[DIALOGS_UNHIDE] = g_signal_new ("dialogs_unhide",
262 G_TYPE_FROM_CLASS (klass),
263 G_SIGNAL_RUN_FIRST,
264 G_STRUCT_OFFSET (Inkscape::ApplicationClass, dialogs_unhide),
265 NULL, NULL,
266 g_cclosure_marshal_VOID__VOID,
267 G_TYPE_NONE, 0);
268 inkscape_signals[EXTERNAL_CHANGE] = g_signal_new ("external_change",
269 G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_FIRST,
271 G_STRUCT_OFFSET (Inkscape::ApplicationClass, external_change),
272 NULL, NULL,
273 g_cclosure_marshal_VOID__VOID,
274 G_TYPE_NONE, 0);
276 object_class->dispose = inkscape_dispose;
278 klass->activate_desktop = inkscape_activate_desktop_private;
279 klass->deactivate_desktop = inkscape_deactivate_desktop_private;
280 }
282 #ifdef WIN32
283 typedef int uid_t;
284 #define getuid() 0
285 #endif
287 /**
288 * static gint inkscape_autosave(gpointer);
289 *
290 * Callback passed to g_timeout_add_seconds()
291 * Responsible for autosaving all open documents
292 */
293 static gint inkscape_autosave(gpointer)
294 {
295 if (inkscape->document_set.empty()) { // nothing to autosave
296 return TRUE;
297 }
298 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
300 // Use UID for separating autosave-documents between users if directory is multiuser
301 uid_t uid = getuid();
303 Glib::ustring autosave_dir;
304 {
305 Glib::ustring tmp = prefs->getString("/options/autosave/path");
306 if (!tmp.empty()) {
307 autosave_dir = tmp;
308 } else {
309 autosave_dir = Glib::get_tmp_dir();
310 }
311 }
313 GDir *autosave_dir_ptr = g_dir_open(autosave_dir.c_str(), 0, NULL);
314 if( !autosave_dir_ptr ){
315 g_warning("Cannot open autosave directory!");
316 return TRUE;
317 }
319 time_t sptime = time(NULL);
320 struct tm *sptm = localtime(&sptime);
321 gchar sptstr[256];
322 strftime(sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
324 gint autosave_max = prefs->getInt("/options/autosave/max", 10);
326 gint docnum = 0;
328 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosaving documents..."));
329 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
330 iter != inkscape->document_set.end();
331 ++iter) {
333 SPDocument *doc = iter->first;
335 ++docnum;
337 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
338 // g_debug("Document %d: \"%s\" %s", docnum, doc ? doc->name : "(null)", doc ? (doc->isModifiedSinceSave() ? "(dirty)" : "(clean)") : "(null)");
340 if (doc->isModifiedSinceSave()) {
341 gchar *oldest_autosave = 0;
342 const gchar *filename = 0;
343 struct stat sb;
344 time_t min_time = 0;
345 gint count = 0;
347 // Look for previous autosaves
348 gchar* baseName = g_strdup_printf( "inkscape-autosave-%d", uid );
349 g_dir_rewind(autosave_dir_ptr);
350 while( (filename = g_dir_read_name(autosave_dir_ptr)) != NULL ){
351 if ( strncmp(filename, baseName, strlen(baseName)) == 0 ){
352 gchar* full_path = g_build_filename( autosave_dir.c_str(), filename, NULL );
353 if ( g_stat(full_path, &sb) != -1 ) {
354 if ( difftime(sb.st_ctime, min_time) < 0 || min_time == 0 ){
355 min_time = sb.st_ctime;
356 if ( oldest_autosave ) {
357 g_free(oldest_autosave);
358 }
359 oldest_autosave = g_strdup(full_path);
360 }
361 count ++;
362 }
363 g_free(full_path);
364 }
365 }
367 // g_debug("%d previous autosaves exists. Max = %d", count, autosave_max);
369 // Have we reached the limit for number of autosaves?
370 if ( count >= autosave_max ){
371 // Remove the oldest file
372 if ( oldest_autosave ) {
373 unlink(oldest_autosave);
374 }
375 }
377 if ( oldest_autosave ) {
378 g_free(oldest_autosave);
379 oldest_autosave = 0;
380 }
383 // Set the filename we will actually save to
384 g_free(baseName);
385 baseName = g_strdup_printf("inkscape-autosave-%d-%s-%03d.svg", uid, sptstr, docnum);
386 gchar* full_path = g_build_filename(autosave_dir.c_str(), baseName, NULL);
387 g_free(baseName);
388 baseName = 0;
390 // g_debug("Filename: %s", full_path);
392 // Try to save the file
393 FILE *file = Inkscape::IO::fopen_utf8name(full_path, "w");
394 gchar *errortext = 0;
395 if (file) {
396 try{
397 sp_repr_save_stream(repr->document(), file, SP_SVG_NS_URI);
398 } catch (Inkscape::Extension::Output::no_extension_found &e) {
399 errortext = g_strdup(_("Autosave failed! Could not find inkscape extension to save document."));
400 } catch (Inkscape::Extension::Output::save_failed &e) {
401 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
402 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
403 g_free(safeUri);
404 }
405 fclose(file);
406 }
407 else {
408 gchar *safeUri = Inkscape::IO::sanitizeString(full_path);
409 errortext = g_strdup_printf(_("Autosave failed! File %s could not be saved."), safeUri);
410 g_free(safeUri);
411 }
413 if (errortext) {
414 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, errortext);
415 g_warning("%s", errortext);
416 g_free(errortext);
417 }
419 g_free(full_path);
420 }
421 }
422 g_dir_close(autosave_dir_ptr);
424 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Autosave complete."));
426 return TRUE;
427 }
429 void inkscape_autosave_init()
430 {
431 static guint32 autosave_timeout_id = 0;
432 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
434 // Turn off any previously initiated timeouts
435 if ( autosave_timeout_id ) {
436 g_source_remove(autosave_timeout_id);
437 autosave_timeout_id = 0;
438 }
440 // g_debug("options.autosave.enable = %d", prefs->getBool("/options/autosave/enable", true));
441 // Is autosave enabled?
442 if (!prefs->getBool("/options/autosave/enable", true)){
443 autosave_timeout_id = 0;
444 } else {
445 // Turn on autosave
446 guint32 timeout = prefs->getInt("/options/autosave/interval", 10) * 60;
447 // g_debug("options.autosave.interval = %d", prefs->getInt("/options/autosave/interval", 10));
448 #if GLIB_CHECK_VERSION(2,14,0)
449 autosave_timeout_id = g_timeout_add_seconds(timeout, inkscape_autosave, NULL);
450 #else
451 autosave_timeout_id = g_timeout_add(timeout * 1000, inkscape_autosave, NULL);
452 #endif
453 }
454 }
457 static void
458 inkscape_init (SPObject * object)
459 {
460 if (!inkscape) {
461 inkscape = (Inkscape::Application *) object;
462 } else {
463 g_assert_not_reached ();
464 }
466 new (&inkscape->document_set) std::map<SPDocument *, int>();
468 inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
469 inkscape->desktops = NULL;
470 inkscape->dialogs_toggle = TRUE;
471 inkscape->mapalt = GDK_MOD1_MASK;
472 inkscape->trackalt = FALSE;
473 }
475 static void
476 inkscape_dispose (GObject *object)
477 {
478 Inkscape::Application *inkscape = (Inkscape::Application *) object;
480 g_assert (!inkscape->desktops);
482 Inkscape::Preferences::unload();
484 if (inkscape->menus) {
485 /* fixme: This is not the best place */
486 Inkscape::GC::release(inkscape->menus);
487 inkscape->menus = NULL;
488 }
490 inkscape->document_set.~map();
492 G_OBJECT_CLASS (parent_class)->dispose (object);
494 gtk_main_quit ();
495 }
498 void
499 inkscape_ref (void)
500 {
501 if (inkscape)
502 g_object_ref (G_OBJECT (inkscape));
503 }
506 void
507 inkscape_unref (void)
508 {
509 if (inkscape)
510 g_object_unref (G_OBJECT (inkscape));
511 }
513 /* returns the mask of the keyboard modifier to map to Alt, zero if no mapping */
514 /* Needs to be a guint because gdktypes.h does not define a 'no-modifier' value */
515 guint
516 inkscape_mapalt() {
517 return inkscape->mapalt;
518 }
520 /* Sets the keyboard modifer to map to Alt. Zero switches off mapping, as does '1', which is the default */
521 void inkscape_mapalt(guint maskvalue)
522 {
523 if(maskvalue<2 || maskvalue> 5 ){ /* MOD5 is the highest defined in gdktypes.h */
524 inkscape->mapalt=0;
525 }else{
526 inkscape->mapalt=(GDK_MOD1_MASK << (maskvalue-1));
527 }
528 }
530 guint
531 inkscape_trackalt() {
532 return inkscape->trackalt;
533 }
535 void inkscape_trackalt(guint trackvalue)
536 {
537 inkscape->trackalt = trackvalue;
538 }
541 static void
542 inkscape_activate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
543 {
544 desktop->set_active (true);
545 }
548 static void
549 inkscape_deactivate_desktop_private (Inkscape::Application */*inkscape*/, SPDesktop *desktop)
550 {
551 desktop->set_active (false);
552 }
555 /* fixme: This is EVIL, and belongs to main after all */
557 #define SP_INDENT 8
560 static void
561 inkscape_crash_handler (int /*signum*/)
562 {
563 using Inkscape::Debug::SimpleEvent;
564 using Inkscape::Debug::EventTracker;
565 using Inkscape::Debug::Logger;
567 static gint recursion = FALSE;
569 /*
570 * reset all signal handlers: any further crashes should just be allowed
571 * to crash normally.
572 * */
573 signal (SIGSEGV, segv_handler );
574 signal (SIGABRT, abrt_handler );
575 signal (SIGFPE, fpe_handler );
576 signal (SIGILL, ill_handler );
577 #ifndef WIN32
578 signal (SIGBUS, bus_handler );
579 #endif
581 /* Stop bizarre loops */
582 if (recursion) {
583 abort ();
584 }
585 recursion = TRUE;
587 EventTracker<SimpleEvent<Inkscape::Debug::Event::CORE> > tracker("crash");
588 tracker.set<SimpleEvent<> >("emergency-save");
590 fprintf(stderr, "\nEmergency save activated!\n");
592 time_t sptime = time (NULL);
593 struct tm *sptm = localtime (&sptime);
594 gchar sptstr[256];
595 strftime (sptstr, 256, "%Y_%m_%d_%H_%M_%S", sptm);
597 gint count = 0;
598 gchar *curdir = g_get_current_dir(); // This one needs to be freed explicitly
599 gchar *inkscapedir = g_path_get_dirname(INKSCAPE->argv0); // Needs to be freed
600 GSList *savednames = NULL;
601 GSList *failednames = NULL;
602 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
603 iter != inkscape->document_set.end();
604 ++iter) {
605 SPDocument *doc = iter->first;
606 Inkscape::XML::Node *repr;
607 repr = sp_document_repr_root (doc);
608 if (doc->isModifiedSinceSave()) {
609 const gchar *docname;
611 /* originally, the document name was retrieved from
612 * the sodipod:docname attribute */
613 docname = doc->name;
614 if (docname) {
615 /* Removes an emergency save suffix if present: /(.*)\.[0-9_]*\.[0-9_]*\.[~\.]*$/\1/ */
616 const char* d0 = strrchr ((char*)docname, '.');
617 if (d0 && (d0 > docname)) {
618 const char* d = d0;
619 unsigned int dots = 0;
620 while ((isdigit (*d) || *d=='_' || *d=='.') && d>docname && dots<2) {
621 d -= 1;
622 if (*d=='.') dots++;
623 }
624 if (*d=='.' && d>docname && dots==2) {
625 char n[64];
626 size_t len = MIN (d - docname, 63);
627 memcpy (n, docname, len);
628 n[len] = '\0';
629 docname = n;
630 }
631 }
632 }
633 if (!docname || !*docname) docname = "emergency";
635 // Emergency filename
636 char c[1024];
637 g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
639 // Find a location
640 const char* locations[] = {
641 doc->base,
642 g_get_home_dir(),
643 g_get_tmp_dir(),
644 curdir,
645 inkscapedir
646 };
647 FILE *file = 0;
648 for(size_t i=0; i<sizeof(locations)/sizeof(*locations); i++) {
649 if (!locations[i]) continue; // It seems to be okay, but just in case
650 gchar * filename = g_build_filename(locations[i], c, NULL);
651 Inkscape::IO::dump_fopen_call(filename, "E");
652 file = Inkscape::IO::fopen_utf8name(filename, "w");
653 if (file) {
654 g_snprintf (c, 1024, "%s", filename); // we want the complete path to be stored in c (for reporting purposes)
655 break;
656 }
657 }
659 // Save
660 if (file) {
661 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
662 savednames = g_slist_prepend (savednames, g_strdup (c));
663 fclose (file);
664 } else {
665 failednames = g_slist_prepend (failednames, (doc->name) ? g_strdup (doc->name) : g_strdup (_("Untitled document")));
666 }
667 count++;
668 }
669 }
670 g_free(curdir);
671 g_free(inkscapedir);
673 savednames = g_slist_reverse (savednames);
674 failednames = g_slist_reverse (failednames);
675 if (savednames) {
676 fprintf (stderr, "\nEmergency save document locations:\n");
677 for (GSList *l = savednames; l != NULL; l = l->next) {
678 fprintf (stderr, " %s\n", (gchar *) l->data);
679 }
680 }
681 if (failednames) {
682 fprintf (stderr, "\nFailed to do emergency save for documents:\n");
683 for (GSList *l = failednames; l != NULL; l = l->next) {
684 fprintf (stderr, " %s\n", (gchar *) l->data);
685 }
686 }
688 // do not save the preferences since they can be in a corrupted state
689 Inkscape::Preferences::unload(false);
691 fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
692 fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
693 fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
695 /* Show nice dialog box */
697 char const *istr = _("Inkscape encountered an internal error and will close now.\n");
698 char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
699 char const *fstr = _("Automatic backup of the following documents failed:\n");
700 gint nllen = strlen ("\n");
701 gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
702 for (GSList *l = savednames; l != NULL; l = l->next) {
703 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
704 }
705 for (GSList *l = failednames; l != NULL; l = l->next) {
706 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
707 }
708 len += 1;
709 gchar *b = g_new (gchar, len);
710 gint pos = 0;
711 len = strlen (istr);
712 memcpy (b + pos, istr, len);
713 pos += len;
714 if (savednames) {
715 len = strlen (sstr);
716 memcpy (b + pos, sstr, len);
717 pos += len;
718 for (GSList *l = savednames; l != NULL; l = l->next) {
719 memset (b + pos, ' ', SP_INDENT);
720 pos += SP_INDENT;
721 len = strlen ((gchar *) l->data);
722 memcpy (b + pos, l->data, len);
723 pos += len;
724 memcpy (b + pos, "\n", nllen);
725 pos += nllen;
726 }
727 }
728 if (failednames) {
729 len = strlen (fstr);
730 memcpy (b + pos, fstr, len);
731 pos += len;
732 for (GSList *l = failednames; l != NULL; l = l->next) {
733 memset (b + pos, ' ', SP_INDENT);
734 pos += SP_INDENT;
735 len = strlen ((gchar *) l->data);
736 memcpy (b + pos, l->data, len);
737 pos += len;
738 memcpy (b + pos, "\n", nllen);
739 pos += nllen;
740 }
741 }
742 *(b + pos) = '\0';
744 if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
745 GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
746 gtk_dialog_run (GTK_DIALOG (msgbox));
747 gtk_widget_destroy (msgbox);
748 }
749 else
750 {
751 g_message( "Error: %s", b );
752 }
753 g_free (b);
755 tracker.clear();
756 Logger::shutdown();
758 /* on exit, allow restored signal handler to take over and crash us */
759 }
762 class InkErrorHandler : public Inkscape::ErrorReporter {
763 public:
764 InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(),
765 _useGui(useGui)
766 {}
767 virtual ~InkErrorHandler() {}
769 virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const
770 {
771 if (_useGui) {
772 Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
773 err.set_secondary_text(secondary);
774 err.run();
775 } else {
776 g_message("%s", primary.data());
777 g_message("%s", secondary.data());
778 }
779 }
781 private:
782 bool _useGui;
783 };
785 void
786 inkscape_application_init (const gchar *argv0, gboolean use_gui)
787 {
788 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
789 /* fixme: load application defaults */
791 segv_handler = signal (SIGSEGV, inkscape_crash_handler);
792 abrt_handler = signal (SIGABRT, inkscape_crash_handler);
793 fpe_handler = signal (SIGFPE, inkscape_crash_handler);
794 ill_handler = signal (SIGILL, inkscape_crash_handler);
795 #ifndef WIN32
796 bus_handler = signal (SIGBUS, inkscape_crash_handler);
797 #endif
799 inkscape->use_gui = use_gui;
800 inkscape->argv0 = g_strdup(argv0);
802 /* Load the preferences and menus */
803 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
804 InkErrorHandler* handler = new InkErrorHandler(use_gui);
805 prefs->setErrorHandler(handler);
806 {
807 Glib::ustring msg;
808 Glib::ustring secondary;
809 if (prefs->getLastError( msg, secondary )) {
810 handler->handleError(msg, secondary);
811 }
812 }
814 if (use_gui) {
815 inkscape_load_menus(inkscape);
816 Inkscape::DeviceManager::getManager().loadConfig();
817 }
819 /* set language for user interface according setting in preferences */
820 Glib::ustring ui_language = prefs->getString("/ui/language");
821 if(!ui_language.empty())
822 {
823 setenv("LANGUAGE", ui_language, 1);
824 }
826 /* DebugDialog redirection. On Linux, default to OFF, on Win32, default to ON.
827 * Use only if use_gui is enabled
828 */
829 #ifdef WIN32
830 #define DEFAULT_LOG_REDIRECT true
831 #else
832 #define DEFAULT_LOG_REDIRECT false
833 #endif
835 if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
836 {
837 Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
838 }
840 /* Check for global remapping of Alt key */
841 if (use_gui)
842 {
843 inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
844 inkscape_trackalt(guint(prefs->getInt("/options/trackalt/value", 0)));
845 }
847 /* Initialize the extensions */
848 Inkscape::Extension::init();
850 inkscape_autosave_init();
852 return;
853 }
855 /**
856 * Returns the current Inkscape::Application global object
857 */
858 Inkscape::Application *
859 inkscape_get_instance()
860 {
861 return inkscape;
862 }
864 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
865 {
866 return app->use_gui;
867 }
869 /**
870 * Menus management
871 *
872 */
873 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
874 {
875 // TODO fix that fn is being leaked
876 gchar *fn = profile_path(MENUS_FILE);
877 gchar *menus_xml = NULL;
878 gsize len = 0;
880 if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
881 // load the menus_xml file
882 INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
883 g_free(menus_xml);
884 if (INKSCAPE->menus) {
885 return true;
886 }
887 }
888 INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
889 return (INKSCAPE->menus != 0);
890 }
893 void
894 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
895 {
896 if (Inkscape::NSApplication::Application::getNewGui()) {
897 Inkscape::NSApplication::Editor::selectionModified (selection, flags);
898 return;
899 }
900 g_return_if_fail (selection != NULL);
902 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
903 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[MODIFY_SELECTION], 0, selection, flags);
904 }
905 }
908 void
909 inkscape_selection_changed (Inkscape::Selection * selection)
910 {
911 if (Inkscape::NSApplication::Application::getNewGui()) {
912 Inkscape::NSApplication::Editor::selectionChanged (selection);
913 return;
914 }
915 g_return_if_fail (selection != NULL);
917 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
918 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
919 }
920 }
922 void
923 inkscape_subselection_changed (SPDesktop *desktop)
924 {
925 if (Inkscape::NSApplication::Application::getNewGui()) {
926 Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
927 return;
928 }
929 g_return_if_fail (desktop != NULL);
931 if (DESKTOP_IS_ACTIVE (desktop)) {
932 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
933 }
934 }
937 void
938 inkscape_selection_set (Inkscape::Selection * selection)
939 {
940 if (Inkscape::NSApplication::Application::getNewGui()) {
941 Inkscape::NSApplication::Editor::selectionSet (selection);
942 return;
943 }
944 g_return_if_fail (selection != NULL);
946 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
947 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
948 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
949 }
950 }
953 void
954 inkscape_eventcontext_set (SPEventContext * eventcontext)
955 {
956 if (Inkscape::NSApplication::Application::getNewGui()) {
957 Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
958 return;
959 }
960 g_return_if_fail (eventcontext != NULL);
961 g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
963 if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
964 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
965 }
966 }
969 void
970 inkscape_add_desktop (SPDesktop * desktop)
971 {
972 g_return_if_fail (desktop != NULL);
974 if (Inkscape::NSApplication::Application::getNewGui())
975 {
976 Inkscape::NSApplication::Editor::addDesktop (desktop);
977 return;
978 }
979 g_return_if_fail (inkscape != NULL);
981 g_assert (!g_slist_find (inkscape->desktops, desktop));
983 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
985 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
986 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
987 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
988 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
989 }
993 void
994 inkscape_remove_desktop (SPDesktop * desktop)
995 {
996 g_return_if_fail (desktop != NULL);
997 if (Inkscape::NSApplication::Application::getNewGui())
998 {
999 Inkscape::NSApplication::Editor::removeDesktop (desktop);
1000 return;
1001 }
1002 g_return_if_fail (inkscape != NULL);
1004 g_assert (g_slist_find (inkscape->desktops, desktop));
1006 if (DESKTOP_IS_ACTIVE (desktop)) {
1007 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
1008 if (inkscape->desktops->next != NULL) {
1009 SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
1010 inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
1011 inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
1012 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
1013 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
1014 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
1015 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
1016 } else {
1017 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
1018 if (sp_desktop_selection(desktop))
1019 sp_desktop_selection(desktop)->clear();
1020 }
1021 }
1023 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1025 // if this was the last desktop, shut down the program
1026 if (inkscape->desktops == NULL) {
1027 inkscape_exit (inkscape);
1028 }
1029 }
1033 void
1034 inkscape_activate_desktop (SPDesktop * desktop)
1035 {
1036 g_return_if_fail (desktop != NULL);
1037 if (Inkscape::NSApplication::Application::getNewGui())
1038 {
1039 Inkscape::NSApplication::Editor::activateDesktop (desktop);
1040 return;
1041 }
1042 g_return_if_fail (inkscape != NULL);
1044 if (DESKTOP_IS_ACTIVE (desktop)) {
1045 return;
1046 }
1048 g_assert (g_slist_find (inkscape->desktops, desktop));
1050 SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
1052 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
1054 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1055 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
1057 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1058 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
1059 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
1060 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
1061 }
1064 /**
1065 * Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
1066 */
1067 void
1068 inkscape_reactivate_desktop (SPDesktop * desktop)
1069 {
1070 g_return_if_fail (desktop != NULL);
1071 if (Inkscape::NSApplication::Application::getNewGui())
1072 {
1073 Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1074 return;
1075 }
1076 g_return_if_fail (inkscape != NULL);
1078 if (DESKTOP_IS_ACTIVE (desktop))
1079 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1080 }
1084 SPDesktop *
1085 inkscape_find_desktop_by_dkey (unsigned int dkey)
1086 {
1087 for (GSList *r = inkscape->desktops; r; r = r->next) {
1088 if (((SPDesktop *) r->data)->dkey == dkey)
1089 return ((SPDesktop *) r->data);
1090 }
1091 return NULL;
1092 }
1097 unsigned int
1098 inkscape_maximum_dkey()
1099 {
1100 unsigned int dkey = 0;
1102 for (GSList *r = inkscape->desktops; r; r = r->next) {
1103 if (((SPDesktop *) r->data)->dkey > dkey)
1104 dkey = ((SPDesktop *) r->data)->dkey;
1105 }
1107 return dkey;
1108 }
1112 SPDesktop *
1113 inkscape_next_desktop ()
1114 {
1115 SPDesktop *d = NULL;
1116 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1118 if (dkey_current < inkscape_maximum_dkey()) {
1119 // find next existing
1120 for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1121 d = inkscape_find_desktop_by_dkey (i);
1122 if (d) {
1123 break;
1124 }
1125 }
1126 } else {
1127 // find first existing
1128 for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1129 d = inkscape_find_desktop_by_dkey (i);
1130 if (d) {
1131 break;
1132 }
1133 }
1134 }
1136 g_assert (d);
1138 return d;
1139 }
1143 SPDesktop *
1144 inkscape_prev_desktop ()
1145 {
1146 SPDesktop *d = NULL;
1147 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1149 if (dkey_current > 0) {
1150 // find prev existing
1151 for (signed int i = dkey_current - 1; i >= 0; i--) {
1152 d = inkscape_find_desktop_by_dkey (i);
1153 if (d) {
1154 break;
1155 }
1156 }
1157 }
1158 if (!d) {
1159 // find last existing
1160 d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1161 }
1163 g_assert (d);
1165 return d;
1166 }
1170 void
1171 inkscape_switch_desktops_next ()
1172 {
1173 inkscape_next_desktop()->presentWindow();
1174 }
1178 void
1179 inkscape_switch_desktops_prev ()
1180 {
1181 inkscape_prev_desktop()->presentWindow();
1182 }
1186 void
1187 inkscape_dialogs_hide ()
1188 {
1189 if (Inkscape::NSApplication::Application::getNewGui())
1190 Inkscape::NSApplication::Editor::hideDialogs();
1191 else
1192 {
1193 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1194 inkscape->dialogs_toggle = FALSE;
1195 }
1196 }
1200 void
1201 inkscape_dialogs_unhide ()
1202 {
1203 if (Inkscape::NSApplication::Application::getNewGui())
1204 Inkscape::NSApplication::Editor::unhideDialogs();
1205 else
1206 {
1207 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1208 inkscape->dialogs_toggle = TRUE;
1209 }
1210 }
1214 void
1215 inkscape_dialogs_toggle ()
1216 {
1217 if (inkscape->dialogs_toggle) {
1218 inkscape_dialogs_hide ();
1219 } else {
1220 inkscape_dialogs_unhide ();
1221 }
1222 }
1224 void
1225 inkscape_external_change ()
1226 {
1227 g_return_if_fail (inkscape != NULL);
1229 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1230 }
1232 /**
1233 * fixme: These need probably signals too
1234 */
1235 void
1236 inkscape_add_document (SPDocument *document)
1237 {
1238 g_return_if_fail (document != NULL);
1240 if (!Inkscape::NSApplication::Application::getNewGui())
1241 {
1242 // try to insert the pair into the list
1243 if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1244 //insert failed, this key (document) is already in the list
1245 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1246 iter != inkscape->document_set.end();
1247 ++iter) {
1248 if (iter->first == document) {
1249 // found this document in list, increase its count
1250 iter->second ++;
1251 }
1252 }
1253 }
1254 }
1255 else
1256 {
1257 Inkscape::NSApplication::Editor::addDocument (document);
1258 }
1259 }
1262 // returns true if this was last reference to this document, so you can delete it
1263 bool
1264 inkscape_remove_document (SPDocument *document)
1265 {
1266 g_return_val_if_fail (document != NULL, false);
1268 if (!Inkscape::NSApplication::Application::getNewGui())
1269 {
1270 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1271 iter != inkscape->document_set.end();
1272 ++iter) {
1273 if (iter->first == document) {
1274 // found this document in list, decrease its count
1275 iter->second --;
1276 if (iter->second < 1) {
1277 // this was the last one, remove the pair from list
1278 inkscape->document_set.erase (iter);
1279 return true;
1280 } else {
1281 return false;
1282 }
1283 }
1284 }
1285 }
1286 else
1287 {
1288 Inkscape::NSApplication::Editor::removeDocument (document);
1289 }
1291 return false;
1292 }
1294 SPDesktop *
1295 inkscape_active_desktop (void)
1296 {
1297 if (Inkscape::NSApplication::Application::getNewGui())
1298 return Inkscape::NSApplication::Editor::getActiveDesktop();
1300 if (inkscape->desktops == NULL) {
1301 return NULL;
1302 }
1304 return (SPDesktop *) inkscape->desktops->data;
1305 }
1307 SPDocument *
1308 inkscape_active_document (void)
1309 {
1310 if (Inkscape::NSApplication::Application::getNewGui())
1311 return Inkscape::NSApplication::Editor::getActiveDocument();
1313 if (SP_ACTIVE_DESKTOP) {
1314 return sp_desktop_document (SP_ACTIVE_DESKTOP);
1315 }
1317 return NULL;
1318 }
1320 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1321 SPDocument const* document = desktop.doc();
1322 if (!document) {
1323 return false;
1324 }
1325 for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1326 SPDesktop *other_desktop=(SPDesktop *)iter->data;
1327 SPDocument *other_document=other_desktop->doc();
1328 if ( other_document == document && other_desktop != &desktop ) {
1329 return false;
1330 }
1331 }
1332 return true;
1333 }
1335 SPEventContext *
1336 inkscape_active_event_context (void)
1337 {
1338 if (SP_ACTIVE_DESKTOP) {
1339 return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1340 }
1342 return NULL;
1343 }
1347 /*#####################
1348 # HELPERS
1349 #####################*/
1351 void
1352 inkscape_refresh_display (Inkscape::Application *inkscape)
1353 {
1354 for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1355 (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1356 }
1357 }
1360 /**
1361 * Handler for Inkscape's Exit verb. This emits the shutdown signal,
1362 * saves the preferences if appropriate, and quits.
1363 */
1364 void
1365 inkscape_exit (Inkscape::Application */*inkscape*/)
1366 {
1367 g_assert (INKSCAPE);
1369 //emit shutdown signal so that dialogs could remember layout
1370 g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1372 Inkscape::Preferences::unload();
1373 gtk_main_quit ();
1374 }
1376 char *
1377 homedir_path(const char *filename)
1378 {
1379 static const gchar *homedir = NULL;
1380 if (!homedir) {
1381 homedir = g_get_home_dir();
1382 }
1383 if (!homedir) {
1384 homedir = g_path_get_dirname(INKSCAPE->argv0);
1385 }
1386 return g_build_filename(homedir, filename, NULL);
1387 }
1390 /**
1391 * Get, or guess, or decide the location where the preferences.xml
1392 * file should be located.
1393 */
1394 gchar *
1395 profile_path(const char *filename)
1396 {
1397 static const gchar *prefdir = NULL;
1400 if (!prefdir) {
1401 // First check for a custom environment variable for a "portable app"
1402 gchar const *val = g_getenv("INKSCAPE_PORTABLE_PROFILE_DIR");
1403 if (val) {
1404 prefdir = g_strdup(val);
1405 }
1407 #ifdef HAS_SHGetSpecialFolderLocation
1408 // prefer c:\Documents and Settings\UserName\Application Data\ to
1409 // c:\Documents and Settings\userName\;
1410 if (!prefdir) {
1411 ITEMIDLIST *pidl = 0;
1412 if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1413 gchar * utf8Path = NULL;
1415 if ( PrintWin32::is_os_wide() ) {
1416 wchar_t pathBuf[MAX_PATH+1];
1417 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1419 if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1420 utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1421 }
1422 } else {
1423 char pathBuf[MAX_PATH+1];
1425 if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1426 utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1427 }
1428 }
1430 if ( utf8Path ) {
1431 if (!g_utf8_validate(utf8Path, -1, NULL)) {
1432 g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1433 g_free( utf8Path );
1434 utf8Path = 0;
1435 } else {
1436 prefdir = utf8Path;
1437 }
1438 }
1441 /* not compiling yet...
1443 // Remember to free the list pointer
1444 IMalloc * imalloc = 0;
1445 if ( SHGetMalloc(&imalloc) == NOERROR) {
1446 imalloc->lpVtbl->Free( imalloc, pidl );
1447 imalloc->lpVtbl->Release( imalloc );
1448 }
1449 */
1450 }
1452 if (prefdir) {
1453 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1454 }
1455 }
1456 #endif
1457 if (!prefdir) {
1458 prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1459 gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1460 gchar * dev47Dir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR_047DEV, NULL);
1462 bool needsMigration = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) );
1463 if (needsMigration) {
1464 // TODO here is a point to hook in preference migration
1465 g_warning("Preferences need to be migrated from 0.46 or older %s to %s", legacyDir, prefdir);
1466 Inkscape::Preferences::migrate( legacyDir, prefdir );
1467 }
1469 bool needsRenameWarning = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( dev47Dir, G_FILE_TEST_EXISTS ) );
1470 if (needsRenameWarning) {
1471 g_warning("Preferences need to be copied from %s to %s", legacyDir, prefdir);
1472 }
1474 g_free(legacyDir);
1475 legacyDir = 0;
1476 g_free(dev47Dir);
1477 dev47Dir = 0;
1478 // In case the XDG user config dir of the moment does not yet exist...
1479 int mode = S_IRWXU;
1480 #ifdef S_IRGRP
1481 mode |= S_IRGRP;
1482 #endif
1483 #ifdef S_IXGRP
1484 mode |= S_IXGRP;
1485 #endif
1486 #ifdef S_IXOTH
1487 mode |= S_IXOTH;
1488 #endif
1489 if ( g_mkdir_with_parents(prefdir, mode) == -1 ) {
1490 int problem = errno;
1491 g_warning("Unable to create profile directory (%s) (%d)", g_strerror(problem), problem);
1492 } else {
1493 gchar const *userDirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
1494 for (gchar const** name = userDirs; *name; ++name) {
1495 gchar *dir = g_build_filename(prefdir, *name, NULL);
1496 g_mkdir_with_parents(dir, mode);
1497 g_free(dir);
1498 }
1499 }
1500 }
1501 }
1502 return g_build_filename(prefdir, filename, NULL);
1503 }
1505 Inkscape::XML::Node *
1506 inkscape_get_menus (Inkscape::Application * inkscape)
1507 {
1508 Inkscape::XML::Node *repr = inkscape->menus->root();
1509 g_assert (!(strcmp (repr->name(), "inkscape")));
1510 return repr->firstChild();
1511 }
1513 void
1514 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1515 {
1516 for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1517 listbuf.push_back(static_cast< SPDesktop* >(l->data));
1518 }
1519 }
1521 /*
1522 Local Variables:
1523 mode:c++
1524 c-file-style:"stroustrup"
1525 c-file-offsets:((innamespace . 0)(inline-open . 0))
1526 indent-tabs-mode:nil
1527 fill-column:99
1528 End:
1529 */
1530 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :