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 "dialogs/input.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 sp_marshal_NONE__POINTER_UINT,
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 sp_marshal_NONE__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 sp_marshal_NONE__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 sp_marshal_NONE__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 sp_marshal_NONE__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 sp_marshal_NONE__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 sp_marshal_NONE__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 GSList *savednames = NULL;
599 GSList *failednames = NULL;
600 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
601 iter != inkscape->document_set.end();
602 ++iter) {
603 SPDocument *doc = iter->first;
604 Inkscape::XML::Node *repr;
605 repr = sp_document_repr_root (doc);
606 if (doc->isModifiedSinceSave()) {
607 const gchar *docname, *d0, *d;
608 gchar n[64], c[1024];
609 FILE *file;
611 /* originally, the document name was retrieved from
612 * the sodipod:docname attribute */
613 docname = doc->name;
614 if (docname) {
615 /* fixme: Quick hack to remove emergency file suffix */
616 d0 = strrchr ((char*)docname, '.');
617 if (d0 && (d0 > docname)) {
618 d0 = strrchr ((char*)(d0 - 1), '.');
619 if (d0 && (d0 > docname)) {
620 d = d0;
621 while (isdigit (*d) || (*d == '.') || (*d == '_')) d += 1;
622 if (*d) {
623 memcpy (n, docname, MIN (d0 - docname - 1, 64));
624 n[63] = '\0';
625 docname = n;
626 }
627 }
628 }
629 }
631 if (!docname || !*docname) docname = "emergency";
632 // try saving to the profile location
633 g_snprintf (c, 1024, "%.256s.%s.%d.svg", docname, sptstr, count);
634 gchar * location = homedir_path(c);
635 Inkscape::IO::dump_fopen_call(location, "E");
636 file = Inkscape::IO::fopen_utf8name(location, "w");
637 g_snprintf (c, 1024, "%s", location); // we want the complete path to be stored in c (for reporting purposes)
638 g_free(location);
639 if (!file) {
640 // try saving to /tmp
641 g_snprintf (c, 1024, "/tmp/inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
642 Inkscape::IO::dump_fopen_call(c, "G");
643 file = Inkscape::IO::fopen_utf8name(c, "w");
644 }
645 if (!file) {
646 // try saving to the current directory
647 gchar *curdir = g_get_current_dir();
648 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
649 Inkscape::IO::dump_fopen_call(c, "F");
650 file = Inkscape::IO::fopen_utf8name(c, "w");
651 // store the complete path in c so that it can be reported later
652 gchar * location = g_build_filename(curdir, c, NULL);
653 g_snprintf (c, 1024, "%s", location);
654 g_free(location);
655 }
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 }
667 savednames = g_slist_reverse (savednames);
668 failednames = g_slist_reverse (failednames);
669 if (savednames) {
670 fprintf (stderr, "\nEmergency save document locations:\n");
671 for (GSList *l = savednames; l != NULL; l = l->next) {
672 fprintf (stderr, " %s\n", (gchar *) l->data);
673 }
674 }
675 if (failednames) {
676 fprintf (stderr, "\nFailed to do emergency save for documents:\n");
677 for (GSList *l = failednames; l != NULL; l = l->next) {
678 fprintf (stderr, " %s\n", (gchar *) l->data);
679 }
680 }
682 // do not save the preferences since they can be in a corrupted state
683 Inkscape::Preferences::unload(false);
685 fprintf (stderr, "Emergency save completed. Inkscape will close now.\n");
686 fprintf (stderr, "If you can reproduce this crash, please file a bug at www.inkscape.org\n");
687 fprintf (stderr, "with a detailed description of the steps leading to the crash, so we can fix it.\n");
689 /* Show nice dialog box */
691 char const *istr = _("Inkscape encountered an internal error and will close now.\n");
692 char const *sstr = _("Automatic backups of unsaved documents were done to the following locations:\n");
693 char const *fstr = _("Automatic backup of the following documents failed:\n");
694 gint nllen = strlen ("\n");
695 gint len = strlen (istr) + strlen (sstr) + strlen (fstr);
696 for (GSList *l = savednames; l != NULL; l = l->next) {
697 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
698 }
699 for (GSList *l = failednames; l != NULL; l = l->next) {
700 len = len + SP_INDENT + strlen ((gchar *) l->data) + nllen;
701 }
702 len += 1;
703 gchar *b = g_new (gchar, len);
704 gint pos = 0;
705 len = strlen (istr);
706 memcpy (b + pos, istr, len);
707 pos += len;
708 if (savednames) {
709 len = strlen (sstr);
710 memcpy (b + pos, sstr, len);
711 pos += len;
712 for (GSList *l = savednames; l != NULL; l = l->next) {
713 memset (b + pos, ' ', SP_INDENT);
714 pos += SP_INDENT;
715 len = strlen ((gchar *) l->data);
716 memcpy (b + pos, l->data, len);
717 pos += len;
718 memcpy (b + pos, "\n", nllen);
719 pos += nllen;
720 }
721 }
722 if (failednames) {
723 len = strlen (fstr);
724 memcpy (b + pos, fstr, len);
725 pos += len;
726 for (GSList *l = failednames; l != NULL; l = l->next) {
727 memset (b + pos, ' ', SP_INDENT);
728 pos += SP_INDENT;
729 len = strlen ((gchar *) l->data);
730 memcpy (b + pos, l->data, len);
731 pos += len;
732 memcpy (b + pos, "\n", nllen);
733 pos += nllen;
734 }
735 }
736 *(b + pos) = '\0';
738 if ( inkscape_get_instance() && inkscape_app_use_gui( inkscape_get_instance() ) ) {
739 GtkWidget *msgbox = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", b);
740 gtk_dialog_run (GTK_DIALOG (msgbox));
741 gtk_widget_destroy (msgbox);
742 }
743 else
744 {
745 g_message( "Error: %s", b );
746 }
747 g_free (b);
749 tracker.clear();
750 Logger::shutdown();
752 /* on exit, allow restored signal handler to take over and crash us */
753 }
756 class InkErrorHandler : public Inkscape::ErrorReporter {
757 public:
758 InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(),
759 _useGui(useGui)
760 {}
761 virtual ~InkErrorHandler() {}
763 virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const
764 {
765 if (_useGui) {
766 Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
767 err.set_secondary_text(secondary);
768 err.run();
769 } else {
770 g_message("%s", primary.data());
771 g_message("%s", secondary.data());
772 }
773 }
775 private:
776 bool _useGui;
777 };
779 void
780 inkscape_application_init (const gchar *argv0, gboolean use_gui)
781 {
782 inkscape = (Inkscape::Application *)g_object_new (SP_TYPE_INKSCAPE, NULL);
783 /* fixme: load application defaults */
785 segv_handler = signal (SIGSEGV, inkscape_crash_handler);
786 abrt_handler = signal (SIGABRT, inkscape_crash_handler);
787 fpe_handler = signal (SIGFPE, inkscape_crash_handler);
788 ill_handler = signal (SIGILL, inkscape_crash_handler);
789 #ifndef WIN32
790 bus_handler = signal (SIGBUS, inkscape_crash_handler);
791 #endif
793 inkscape->use_gui = use_gui;
794 inkscape->argv0 = g_strdup(argv0);
796 /* Load the preferences and menus */
797 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
798 InkErrorHandler* handler = new InkErrorHandler(use_gui);
799 prefs->setErrorHandler(handler);
800 {
801 Glib::ustring msg;
802 Glib::ustring secondary;
803 if (prefs->getLastError( msg, secondary )) {
804 handler->handleError(msg, secondary);
805 }
806 }
808 inkscape_load_menus(inkscape);
809 sp_input_load_from_preferences();
811 /* set language for user interface according setting in preferences */
812 Glib::ustring ui_language = prefs->getString("/ui/language");
813 if(!ui_language.empty())
814 {
815 setenv("LANGUAGE", ui_language, 1);
816 }
818 /* DebugDialog redirection. On Linux, default to OFF, on Win32, default to ON.
819 * Use only if use_gui is enabled
820 */
821 #ifdef WIN32
822 #define DEFAULT_LOG_REDIRECT true
823 #else
824 #define DEFAULT_LOG_REDIRECT false
825 #endif
827 if (use_gui == TRUE && prefs->getBool("/dialogs/debug/redirect", DEFAULT_LOG_REDIRECT))
828 {
829 Inkscape::UI::Dialog::DebugDialog::getInstance()->captureLogMessages();
830 }
832 /* Check for global remapping of Alt key */
833 if (use_gui)
834 {
835 inkscape_mapalt(guint(prefs->getInt("/options/mapalt/value", 0)));
836 inkscape_trackalt(guint(prefs->getInt("/options/trackalt/value", 0)));
837 }
839 /* Initialize the extensions */
840 Inkscape::Extension::init();
842 inkscape_autosave_init();
844 return;
845 }
847 /**
848 * Returns the current Inkscape::Application global object
849 */
850 Inkscape::Application *
851 inkscape_get_instance()
852 {
853 return inkscape;
854 }
856 gboolean inkscape_app_use_gui( Inkscape::Application const * app )
857 {
858 return app->use_gui;
859 }
861 /**
862 * Menus management
863 *
864 */
865 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
866 {
867 // TODO fix that fn is being leaked
868 gchar *fn = profile_path(MENUS_FILE);
869 gchar *menus_xml = NULL;
870 gsize len = 0;
872 if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
873 // load the menus_xml file
874 INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
875 g_free(menus_xml);
876 if (INKSCAPE->menus) {
877 return true;
878 }
879 }
880 INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
881 return (INKSCAPE->menus != 0);
882 }
885 void
886 inkscape_selection_modified (Inkscape::Selection *selection, guint flags)
887 {
888 if (Inkscape::NSApplication::Application::getNewGui()) {
889 Inkscape::NSApplication::Editor::selectionModified (selection, flags);
890 return;
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 if (Inkscape::NSApplication::Application::getNewGui()) {
904 Inkscape::NSApplication::Editor::selectionChanged (selection);
905 return;
906 }
907 g_return_if_fail (selection != NULL);
909 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
910 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
911 }
912 }
914 void
915 inkscape_subselection_changed (SPDesktop *desktop)
916 {
917 if (Inkscape::NSApplication::Application::getNewGui()) {
918 Inkscape::NSApplication::Editor::subSelectionChanged (desktop);
919 return;
920 }
921 g_return_if_fail (desktop != NULL);
923 if (DESKTOP_IS_ACTIVE (desktop)) {
924 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SUBSELECTION], 0, desktop);
925 }
926 }
929 void
930 inkscape_selection_set (Inkscape::Selection * selection)
931 {
932 if (Inkscape::NSApplication::Application::getNewGui()) {
933 Inkscape::NSApplication::Editor::selectionSet (selection);
934 return;
935 }
936 g_return_if_fail (selection != NULL);
938 if (DESKTOP_IS_ACTIVE (selection->desktop())) {
939 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, selection);
940 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, selection);
941 }
942 }
945 void
946 inkscape_eventcontext_set (SPEventContext * eventcontext)
947 {
948 if (Inkscape::NSApplication::Application::getNewGui()) {
949 Inkscape::NSApplication::Editor::eventContextSet (eventcontext);
950 return;
951 }
952 g_return_if_fail (eventcontext != NULL);
953 g_return_if_fail (SP_IS_EVENT_CONTEXT (eventcontext));
955 if (DESKTOP_IS_ACTIVE (eventcontext->desktop)) {
956 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, eventcontext);
957 }
958 }
961 void
962 inkscape_add_desktop (SPDesktop * desktop)
963 {
964 g_return_if_fail (desktop != NULL);
966 if (Inkscape::NSApplication::Application::getNewGui())
967 {
968 Inkscape::NSApplication::Editor::addDesktop (desktop);
969 return;
970 }
971 g_return_if_fail (inkscape != NULL);
973 g_assert (!g_slist_find (inkscape->desktops, desktop));
975 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
977 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
978 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
979 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
980 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
981 }
985 void
986 inkscape_remove_desktop (SPDesktop * desktop)
987 {
988 g_return_if_fail (desktop != NULL);
989 if (Inkscape::NSApplication::Application::getNewGui())
990 {
991 Inkscape::NSApplication::Editor::removeDesktop (desktop);
992 return;
993 }
994 g_return_if_fail (inkscape != NULL);
996 g_assert (g_slist_find (inkscape->desktops, desktop));
998 if (DESKTOP_IS_ACTIVE (desktop)) {
999 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, desktop);
1000 if (inkscape->desktops->next != NULL) {
1001 SPDesktop * new_desktop = (SPDesktop *) inkscape->desktops->next->data;
1002 inkscape->desktops = g_slist_remove (inkscape->desktops, new_desktop);
1003 inkscape->desktops = g_slist_prepend (inkscape->desktops, new_desktop);
1004 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, new_desktop);
1005 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (new_desktop));
1006 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (new_desktop));
1007 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (new_desktop));
1008 } else {
1009 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, NULL);
1010 if (sp_desktop_selection(desktop))
1011 sp_desktop_selection(desktop)->clear();
1012 }
1013 }
1015 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1017 // if this was the last desktop, shut down the program
1018 if (inkscape->desktops == NULL) {
1019 inkscape_exit (inkscape);
1020 }
1021 }
1025 void
1026 inkscape_activate_desktop (SPDesktop * desktop)
1027 {
1028 g_return_if_fail (desktop != NULL);
1029 if (Inkscape::NSApplication::Application::getNewGui())
1030 {
1031 Inkscape::NSApplication::Editor::activateDesktop (desktop);
1032 return;
1033 }
1034 g_return_if_fail (inkscape != NULL);
1036 if (DESKTOP_IS_ACTIVE (desktop)) {
1037 return;
1038 }
1040 g_assert (g_slist_find (inkscape->desktops, desktop));
1042 SPDesktop *current = (SPDesktop *) inkscape->desktops->data;
1044 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DEACTIVATE_DESKTOP], 0, current);
1046 inkscape->desktops = g_slist_remove (inkscape->desktops, desktop);
1047 inkscape->desktops = g_slist_prepend (inkscape->desktops, desktop);
1049 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1050 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_EVENTCONTEXT], 0, sp_desktop_event_context (desktop));
1051 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[SET_SELECTION], 0, sp_desktop_selection (desktop));
1052 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[CHANGE_SELECTION], 0, sp_desktop_selection (desktop));
1053 }
1056 /**
1057 * Resends ACTIVATE_DESKTOP for current desktop; needed when a new desktop has got its window that dialogs will transientize to
1058 */
1059 void
1060 inkscape_reactivate_desktop (SPDesktop * desktop)
1061 {
1062 g_return_if_fail (desktop != NULL);
1063 if (Inkscape::NSApplication::Application::getNewGui())
1064 {
1065 Inkscape::NSApplication::Editor::reactivateDesktop (desktop);
1066 return;
1067 }
1068 g_return_if_fail (inkscape != NULL);
1070 if (DESKTOP_IS_ACTIVE (desktop))
1071 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[ACTIVATE_DESKTOP], 0, desktop);
1072 }
1076 SPDesktop *
1077 inkscape_find_desktop_by_dkey (unsigned int dkey)
1078 {
1079 for (GSList *r = inkscape->desktops; r; r = r->next) {
1080 if (((SPDesktop *) r->data)->dkey == dkey)
1081 return ((SPDesktop *) r->data);
1082 }
1083 return NULL;
1084 }
1089 unsigned int
1090 inkscape_maximum_dkey()
1091 {
1092 unsigned int dkey = 0;
1094 for (GSList *r = inkscape->desktops; r; r = r->next) {
1095 if (((SPDesktop *) r->data)->dkey > dkey)
1096 dkey = ((SPDesktop *) r->data)->dkey;
1097 }
1099 return dkey;
1100 }
1104 SPDesktop *
1105 inkscape_next_desktop ()
1106 {
1107 SPDesktop *d = NULL;
1108 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1110 if (dkey_current < inkscape_maximum_dkey()) {
1111 // find next existing
1112 for (unsigned int i = dkey_current + 1; i <= inkscape_maximum_dkey(); i++) {
1113 d = inkscape_find_desktop_by_dkey (i);
1114 if (d) {
1115 break;
1116 }
1117 }
1118 } else {
1119 // find first existing
1120 for (unsigned int i = 0; i <= inkscape_maximum_dkey(); i++) {
1121 d = inkscape_find_desktop_by_dkey (i);
1122 if (d) {
1123 break;
1124 }
1125 }
1126 }
1128 g_assert (d);
1130 return d;
1131 }
1135 SPDesktop *
1136 inkscape_prev_desktop ()
1137 {
1138 SPDesktop *d = NULL;
1139 unsigned int dkey_current = ((SPDesktop *) inkscape->desktops->data)->dkey;
1141 if (dkey_current > 0) {
1142 // find prev existing
1143 for (signed int i = dkey_current - 1; i >= 0; i--) {
1144 d = inkscape_find_desktop_by_dkey (i);
1145 if (d) {
1146 break;
1147 }
1148 }
1149 }
1150 if (!d) {
1151 // find last existing
1152 d = inkscape_find_desktop_by_dkey (inkscape_maximum_dkey());
1153 }
1155 g_assert (d);
1157 return d;
1158 }
1162 void
1163 inkscape_switch_desktops_next ()
1164 {
1165 inkscape_next_desktop()->presentWindow();
1166 }
1170 void
1171 inkscape_switch_desktops_prev ()
1172 {
1173 inkscape_prev_desktop()->presentWindow();
1174 }
1178 void
1179 inkscape_dialogs_hide ()
1180 {
1181 if (Inkscape::NSApplication::Application::getNewGui())
1182 Inkscape::NSApplication::Editor::hideDialogs();
1183 else
1184 {
1185 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_HIDE], 0);
1186 inkscape->dialogs_toggle = FALSE;
1187 }
1188 }
1192 void
1193 inkscape_dialogs_unhide ()
1194 {
1195 if (Inkscape::NSApplication::Application::getNewGui())
1196 Inkscape::NSApplication::Editor::unhideDialogs();
1197 else
1198 {
1199 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[DIALOGS_UNHIDE], 0);
1200 inkscape->dialogs_toggle = TRUE;
1201 }
1202 }
1206 void
1207 inkscape_dialogs_toggle ()
1208 {
1209 if (inkscape->dialogs_toggle) {
1210 inkscape_dialogs_hide ();
1211 } else {
1212 inkscape_dialogs_unhide ();
1213 }
1214 }
1216 void
1217 inkscape_external_change ()
1218 {
1219 g_return_if_fail (inkscape != NULL);
1221 g_signal_emit (G_OBJECT (inkscape), inkscape_signals[EXTERNAL_CHANGE], 0);
1222 }
1224 /**
1225 * fixme: These need probably signals too
1226 */
1227 void
1228 inkscape_add_document (SPDocument *document)
1229 {
1230 g_return_if_fail (document != NULL);
1232 if (!Inkscape::NSApplication::Application::getNewGui())
1233 {
1234 // try to insert the pair into the list
1235 if (!(inkscape->document_set.insert(std::make_pair(document, 1)).second)) {
1236 //insert failed, this key (document) is already in the list
1237 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1238 iter != inkscape->document_set.end();
1239 ++iter) {
1240 if (iter->first == document) {
1241 // found this document in list, increase its count
1242 iter->second ++;
1243 }
1244 }
1245 }
1246 }
1247 else
1248 {
1249 Inkscape::NSApplication::Editor::addDocument (document);
1250 }
1251 }
1254 // returns true if this was last reference to this document, so you can delete it
1255 bool
1256 inkscape_remove_document (SPDocument *document)
1257 {
1258 g_return_val_if_fail (document != NULL, false);
1260 if (!Inkscape::NSApplication::Application::getNewGui())
1261 {
1262 for (std::map<SPDocument*,int>::iterator iter = inkscape->document_set.begin();
1263 iter != inkscape->document_set.end();
1264 ++iter) {
1265 if (iter->first == document) {
1266 // found this document in list, decrease its count
1267 iter->second --;
1268 if (iter->second < 1) {
1269 // this was the last one, remove the pair from list
1270 inkscape->document_set.erase (iter);
1271 return true;
1272 } else {
1273 return false;
1274 }
1275 }
1276 }
1277 }
1278 else
1279 {
1280 Inkscape::NSApplication::Editor::removeDocument (document);
1281 }
1283 return false;
1284 }
1286 SPDesktop *
1287 inkscape_active_desktop (void)
1288 {
1289 if (Inkscape::NSApplication::Application::getNewGui())
1290 return Inkscape::NSApplication::Editor::getActiveDesktop();
1292 if (inkscape->desktops == NULL) {
1293 return NULL;
1294 }
1296 return (SPDesktop *) inkscape->desktops->data;
1297 }
1299 SPDocument *
1300 inkscape_active_document (void)
1301 {
1302 if (Inkscape::NSApplication::Application::getNewGui())
1303 return Inkscape::NSApplication::Editor::getActiveDocument();
1305 if (SP_ACTIVE_DESKTOP) {
1306 return sp_desktop_document (SP_ACTIVE_DESKTOP);
1307 }
1309 return NULL;
1310 }
1312 bool inkscape_is_sole_desktop_for_document(SPDesktop const &desktop) {
1313 SPDocument const* document = desktop.doc();
1314 if (!document) {
1315 return false;
1316 }
1317 for ( GSList *iter = inkscape->desktops ; iter ; iter = iter->next ) {
1318 SPDesktop *other_desktop=(SPDesktop *)iter->data;
1319 SPDocument *other_document=other_desktop->doc();
1320 if ( other_document == document && other_desktop != &desktop ) {
1321 return false;
1322 }
1323 }
1324 return true;
1325 }
1327 SPEventContext *
1328 inkscape_active_event_context (void)
1329 {
1330 if (SP_ACTIVE_DESKTOP) {
1331 return sp_desktop_event_context (SP_ACTIVE_DESKTOP);
1332 }
1334 return NULL;
1335 }
1339 /*#####################
1340 # HELPERS
1341 #####################*/
1343 void
1344 inkscape_refresh_display (Inkscape::Application *inkscape)
1345 {
1346 for (GSList *l = inkscape->desktops; l != NULL; l = l->next) {
1347 (static_cast<Inkscape::UI::View::View*>(l->data))->requestRedraw();
1348 }
1349 }
1352 /**
1353 * Handler for Inkscape's Exit verb. This emits the shutdown signal,
1354 * saves the preferences if appropriate, and quits.
1355 */
1356 void
1357 inkscape_exit (Inkscape::Application */*inkscape*/)
1358 {
1359 g_assert (INKSCAPE);
1361 //emit shutdown signal so that dialogs could remember layout
1362 g_signal_emit (G_OBJECT (INKSCAPE), inkscape_signals[SHUTDOWN_SIGNAL], 0);
1364 Inkscape::Preferences::unload();
1365 gtk_main_quit ();
1366 }
1368 char *
1369 homedir_path(const char *filename)
1370 {
1371 static const gchar *homedir = NULL;
1372 if (!homedir) {
1373 homedir = g_get_home_dir();
1374 }
1375 if (!homedir) {
1376 homedir = g_path_get_dirname(INKSCAPE->argv0);
1377 }
1378 return g_build_filename(homedir, filename, NULL);
1379 }
1382 /**
1383 * Get, or guess, or decide the location where the preferences.xml
1384 * file should be located.
1385 */
1386 gchar *
1387 profile_path(const char *filename)
1388 {
1389 static const gchar *prefdir = NULL;
1392 if (!prefdir) {
1393 // First check for a custom environment variable for a "portable app"
1394 gchar const *val = g_getenv("INKSCAPE_PORTABLE_PROFILE_DIR");
1395 if (val) {
1396 prefdir = g_strdup(val);
1397 }
1399 #ifdef HAS_SHGetSpecialFolderLocation
1400 // prefer c:\Documents and Settings\UserName\Application Data\ to
1401 // c:\Documents and Settings\userName\;
1402 if (!prefdir) {
1403 ITEMIDLIST *pidl = 0;
1404 if ( SHGetSpecialFolderLocation( NULL, CSIDL_APPDATA, &pidl ) == NOERROR ) {
1405 gchar * utf8Path = NULL;
1407 if ( PrintWin32::is_os_wide() ) {
1408 wchar_t pathBuf[MAX_PATH+1];
1409 g_assert(sizeof(wchar_t) == sizeof(gunichar2));
1411 if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
1412 utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
1413 }
1414 } else {
1415 char pathBuf[MAX_PATH+1];
1417 if ( SHGetPathFromIDListA( pidl, pathBuf ) ) {
1418 utf8Path = g_filename_to_utf8( pathBuf, -1, NULL, NULL, NULL );
1419 }
1420 }
1422 if ( utf8Path ) {
1423 if (!g_utf8_validate(utf8Path, -1, NULL)) {
1424 g_warning( "SHGetPathFromIDList%c() resulted in invalid UTF-8", (PrintWin32::is_os_wide() ? 'W' : 'A') );
1425 g_free( utf8Path );
1426 utf8Path = 0;
1427 } else {
1428 prefdir = utf8Path;
1429 }
1430 }
1433 /* not compiling yet...
1435 // Remember to free the list pointer
1436 IMalloc * imalloc = 0;
1437 if ( SHGetMalloc(&imalloc) == NOERROR) {
1438 imalloc->lpVtbl->Free( imalloc, pidl );
1439 imalloc->lpVtbl->Release( imalloc );
1440 }
1441 */
1442 }
1444 if (prefdir) {
1445 prefdir = g_build_filename(prefdir, INKSCAPE_PROFILE_DIR, NULL);
1446 }
1447 }
1448 #endif
1449 if (!prefdir) {
1450 prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
1451 gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
1452 gchar * dev47Dir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR_047DEV, NULL);
1454 bool needsMigration = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) );
1455 if (needsMigration) {
1456 // TODO here is a point to hook in preference migration
1457 g_warning("Preferences need to be migrated from 0.46 or older %s to %s", legacyDir, prefdir);
1458 }
1460 bool needsRenameWarning = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( dev47Dir, G_FILE_TEST_EXISTS ) );
1461 if (needsRenameWarning) {
1462 g_warning("Preferences need to be copied from %s to %s", legacyDir, prefdir);
1463 }
1465 g_free(legacyDir);
1466 legacyDir = 0;
1467 g_free(dev47Dir);
1468 dev47Dir = 0;
1469 // In case the XDG user config dir of the moment does not yet exist...
1470 int mode = S_IRWXU;
1471 #ifdef S_IRGRP
1472 mode |= S_IRGRP;
1473 #endif
1474 #ifdef S_IXGRP
1475 mode |= S_IXGRP;
1476 #endif
1477 #ifdef S_IXOTH
1478 mode |= S_IXOTH;
1479 #endif
1480 if ( g_mkdir_with_parents(prefdir, mode) == -1 ) {
1481 int problem = errno;
1482 g_warning("Unable to create profile directory (%s) (%d)", g_strerror(problem), problem);
1483 }
1484 }
1485 }
1486 return g_build_filename(prefdir, filename, NULL);
1487 }
1489 Inkscape::XML::Node *
1490 inkscape_get_menus (Inkscape::Application * inkscape)
1491 {
1492 Inkscape::XML::Node *repr = inkscape->menus->root();
1493 g_assert (!(strcmp (repr->name(), "inkscape")));
1494 return repr->firstChild();
1495 }
1497 void
1498 inkscape_get_all_desktops(std::list< SPDesktop* >& listbuf)
1499 {
1500 for(GSList* l = inkscape->desktops; l != NULL; l = l->next) {
1501 listbuf.push_back(static_cast< SPDesktop* >(l->data));
1502 }
1503 }
1505 /*
1506 Local Variables:
1507 mode:c++
1508 c-file-style:"stroustrup"
1509 c-file-offsets:((innamespace . 0)(inline-open . 0))
1510 indent-tabs-mode:nil
1511 fill-column:99
1512 End:
1513 */
1514 // vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :