1 #define __SP_FILE_C__
3 /*
4 * File/Print operations
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Chema Celorio <chema@celorio.com>
9 * bulia byak <buliabyak@users.sf.net>
10 * Bruno Dilly <bruno.dilly@gmail.com>
11 *
12 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
13 * Copyright (C) 1999-2005 Authors
14 * Copyright (C) 2004 David Turner
15 * Copyright (C) 2001-2002 Ximian, Inc.
16 *
17 * Released under GNU GPL, read the file 'COPYING' for more information
18 */
20 /**
21 * Note: This file needs to be cleaned up extensively.
22 * What it probably needs is to have one .h file for
23 * the API, and two or more .cpp files for the implementations.
24 */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <glib/gmem.h>
31 #include <libnr/nr-pixops.h>
33 #include "document-private.h"
34 #include "selection-chemistry.h"
35 #include "ui/view/view-widget.h"
36 #include "dir-util.h"
37 #include "helper/png-write.h"
38 #include "dialogs/export.h"
39 #include <glibmm/i18n.h>
40 #include "inkscape.h"
41 #include "desktop.h"
42 #include "selection.h"
43 #include "interface.h"
44 #include "style.h"
45 #include "print.h"
46 #include "file.h"
47 #include "message.h"
48 #include "message-stack.h"
49 #include "ui/dialog/filedialog.h"
50 #include "ui/dialog/ocaldialogs.h"
51 #include "prefs-utils.h"
52 #include "path-prefix.h"
54 #include "sp-namedview.h"
55 #include "desktop-handles.h"
57 #include "extension/db.h"
58 #include "extension/input.h"
59 #include "extension/output.h"
60 /* #include "extension/menu.h" */
61 #include "extension/system.h"
63 #include "io/sys.h"
64 #include "application/application.h"
65 #include "application/editor.h"
66 #include "inkscape.h"
67 #include "uri.h"
69 #ifdef WITH_GNOME_VFS
70 # include <libgnomevfs/gnome-vfs.h>
71 #endif
73 #ifdef WITH_INKBOARD
74 #include "jabber_whiteboard/session-manager.h"
75 #endif
78 //#define INK_DUMP_FILENAME_CONV 1
79 #undef INK_DUMP_FILENAME_CONV
81 //#define INK_DUMP_FOPEN 1
82 #undef INK_DUMP_FOPEN
84 void dump_str(gchar const *str, gchar const *prefix);
85 void dump_ustr(Glib::ustring const &ustr);
88 /*######################
89 ## N E W
90 ######################*/
92 /**
93 * Create a blank document and add it to the desktop
94 */
95 SPDesktop*
96 sp_file_new(const Glib::ustring &templ)
97 {
98 char *templName = NULL;
99 if (templ.size()>0)
100 templName = (char *)templ.c_str();
101 SPDocument *doc = sp_document_new(templName, TRUE, true);
102 g_return_val_if_fail(doc != NULL, NULL);
104 SPDesktop *dt;
105 if (Inkscape::NSApplication::Application::getNewGui())
106 {
107 dt = Inkscape::NSApplication::Editor::createDesktop (doc);
108 } else {
109 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
110 g_return_val_if_fail(dtw != NULL, NULL);
111 sp_document_unref(doc);
113 sp_create_window(dtw, TRUE);
114 dt = static_cast<SPDesktop*>(dtw->view);
115 sp_namedview_window_from_document(dt);
116 sp_namedview_update_layers_from_document(dt);
117 }
118 return dt;
119 }
121 SPDesktop*
122 sp_file_new_default()
123 {
124 std::list<gchar *> sources;
125 sources.push_back( profile_path("templates") ); // first try user's local dir
126 sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
128 while (!sources.empty()) {
129 gchar *dirname = sources.front();
130 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
132 // TRANSLATORS: default.svg is localizable - this is the name of the default document
133 // template. This way you can localize the default pagesize, translate the name of
134 // the default layer, etc. If you wish to localize this file, please create a
135 // localized share/templates/default.xx.svg file, where xx is your language code.
136 char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
137 if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
138 return sp_file_new(default_template);
139 }
140 }
141 g_free(dirname);
142 sources.pop_front();
143 }
145 return sp_file_new("");
146 }
149 /*######################
150 ## D E L E T E
151 ######################*/
153 /**
154 * Perform document closures preceding an exit()
155 */
156 void
157 sp_file_exit()
158 {
159 sp_ui_close_all();
160 // no need to call inkscape_exit here; last document being closed will take care of that
161 }
164 /*######################
165 ## O P E N
166 ######################*/
168 /**
169 * Open a file, add the document to the desktop
170 *
171 * \param replace_empty if true, and the current desktop is empty, this document
172 * will replace the empty one.
173 */
174 bool
175 sp_file_open(const Glib::ustring &uri,
176 Inkscape::Extension::Extension *key,
177 bool add_to_recent, bool replace_empty)
178 {
179 SPDocument *doc = NULL;
180 try {
181 doc = Inkscape::Extension::open(key, uri.c_str());
182 } catch (Inkscape::Extension::Input::no_extension_found &e) {
183 doc = NULL;
184 } catch (Inkscape::Extension::Input::open_failed &e) {
185 doc = NULL;
186 }
188 if (doc) {
189 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
190 SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
192 if (existing && existing->virgin && replace_empty) {
193 // If the current desktop is empty, open the document there
194 sp_document_ensure_up_to_date (doc);
195 desktop->change_document(doc);
196 sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
197 } else {
198 if (!Inkscape::NSApplication::Application::getNewGui()) {
199 // create a whole new desktop and window
200 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
201 sp_create_window(dtw, TRUE);
202 desktop = static_cast<SPDesktop*>(dtw->view);
203 } else {
204 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
205 }
206 }
208 doc->virgin = FALSE;
209 // everyone who cares now has a reference, get rid of ours
210 sp_document_unref(doc);
211 // resize the window to match the document properties
212 sp_namedview_window_from_document(desktop);
213 sp_namedview_update_layers_from_document(desktop);
215 if (add_to_recent) {
216 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
217 }
219 return TRUE;
220 } else {
221 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
222 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
223 sp_ui_error_dialog(text);
224 g_free(text);
225 g_free(safeUri);
226 return FALSE;
227 }
228 }
230 /**
231 * Handle prompting user for "do you want to revert"? Revert on "OK"
232 */
233 void
234 sp_file_revert_dialog()
235 {
236 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
237 g_assert(desktop != NULL);
239 SPDocument *doc = sp_desktop_document(desktop);
240 g_assert(doc != NULL);
242 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
243 g_assert(repr != NULL);
245 gchar const *uri = doc->uri;
246 if (!uri) {
247 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
248 return;
249 }
251 bool do_revert = true;
252 if (repr->attribute("sodipodi:modified") != NULL) {
253 gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
255 bool response = desktop->warnDialog (text);
256 g_free(text);
258 if (!response) {
259 do_revert = false;
260 }
261 }
263 bool reverted;
264 if (do_revert) {
265 // Allow overwriting of current document.
266 doc->virgin = TRUE;
267 reverted = sp_file_open(uri,NULL);
268 } else {
269 reverted = false;
270 }
272 if (reverted) {
273 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
274 } else {
275 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
276 }
277 }
279 void dump_str(gchar const *str, gchar const *prefix)
280 {
281 Glib::ustring tmp;
282 tmp = prefix;
283 tmp += " [";
284 size_t const total = strlen(str);
285 for (unsigned i = 0; i < total; i++) {
286 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
287 tmp += tmp2;
288 g_free(tmp2);
289 }
291 tmp += "]";
292 g_message("%s", tmp.c_str());
293 }
295 void dump_ustr(Glib::ustring const &ustr)
296 {
297 char const *cstr = ustr.c_str();
298 char const *data = ustr.data();
299 Glib::ustring::size_type const byteLen = ustr.bytes();
300 Glib::ustring::size_type const dataLen = ustr.length();
301 Glib::ustring::size_type const cstrLen = strlen(cstr);
303 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
304 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
305 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
306 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
308 try {
309 Glib::ustring tmp;
310 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
311 tmp = " ";
312 if (i < dataLen) {
313 Glib::ustring::value_type val = ustr.at(i);
314 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
315 tmp += tmp2;
316 g_free( tmp2 );
317 } else {
318 tmp += " ";
319 }
321 if (i < byteLen) {
322 int val = (0x0ff & data[i]);
323 gchar *tmp2 = g_strdup_printf(" %02x", val);
324 tmp += tmp2;
325 g_free( tmp2 );
326 if ( val > 32 && val < 127 ) {
327 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
328 tmp += tmp2;
329 g_free( tmp2 );
330 } else {
331 tmp += " . ";
332 }
333 } else {
334 tmp += " ";
335 }
337 if ( i < cstrLen ) {
338 int val = (0x0ff & cstr[i]);
339 gchar* tmp2 = g_strdup_printf(" %02x", val);
340 tmp += tmp2;
341 g_free(tmp2);
342 if ( val > 32 && val < 127 ) {
343 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
344 tmp += tmp2;
345 g_free( tmp2 );
346 } else {
347 tmp += " . ";
348 }
349 } else {
350 tmp += " ";
351 }
353 g_message( "%s", tmp.c_str() );
354 }
355 } catch (...) {
356 g_message("XXXXXXXXXXXXXXXXXX Exception" );
357 }
358 g_message("---------------");
359 }
361 static Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance = NULL;
363 /**
364 * Display an file Open selector. Open a document if OK is pressed.
365 * Can select single or multiple files for opening.
366 */
367 void
368 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer object, gpointer data)
369 {
371 //# Get the current directory for finding files
372 Glib::ustring open_path;
373 char *attr = (char *)prefs_get_string_attribute("dialogs.open", "path");
374 if (attr)
375 open_path = attr;
378 //# Test if the open_path directory exists
379 if (!Inkscape::IO::file_test(open_path.c_str(),
380 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
381 open_path = "";
383 //# If no open path, default to our home directory
384 if (open_path.size() < 1)
385 {
386 open_path = g_get_home_dir();
387 open_path.append(G_DIR_SEPARATOR_S);
388 }
390 //# Create a dialog if we don't already have one
391 if (!openDialogInstance) {
392 openDialogInstance =
393 Inkscape::UI::Dialog::FileOpenDialog::create(
394 parentWindow,
395 open_path,
396 Inkscape::UI::Dialog::SVG_TYPES,
397 (char const *)_("Select file to open"));
398 }
401 //# Show the dialog
402 bool const success = openDialogInstance->show();
403 if (!success)
404 return;
406 //# User selected something. Get name and type
407 Glib::ustring fileName = openDialogInstance->getFilename();
408 Inkscape::Extension::Extension *selection =
409 openDialogInstance->getSelectionType();
411 //# Code to check & open iff multiple files.
412 std::vector<Glib::ustring> flist=openDialogInstance->getFilenames();
414 //# Iterate through filenames if more than 1
415 if (flist.size() > 1)
416 {
417 for (unsigned int i=1 ; i<flist.size() ; i++)
418 {
419 Glib::ustring fName = flist[i];
421 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
422 Glib::ustring newFileName = Glib::filename_to_utf8(fName);
423 if ( newFileName.size() > 0 )
424 fName = newFileName;
425 else
426 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
428 #ifdef INK_DUMP_FILENAME_CONV
429 g_message("Opening File %s\n",fileName);
430 #endif
431 sp_file_open(fileName, selection);
432 }
433 }
434 return;
435 }
438 if (fileName.size() > 0) {
440 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
442 if ( newFileName.size() > 0)
443 fileName = newFileName;
444 else
445 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
447 open_path = fileName;
448 open_path.append(G_DIR_SEPARATOR_S);
449 prefs_set_string_attribute("dialogs.open", "path", open_path.c_str());
451 sp_file_open(fileName, selection);
452 }
454 return;
455 }
458 /*######################
459 ## V A C U U M
460 ######################*/
462 /**
463 * Remove unreferenced defs from the defs section of the document.
464 */
467 void
468 sp_file_vacuum()
469 {
470 SPDocument *doc = SP_ACTIVE_DOCUMENT;
472 unsigned int diff = vacuum_document (doc);
474 sp_document_done(doc, SP_VERB_FILE_VACUUM,
475 _("Vacuum <defs>"));
477 SPDesktop *dt = SP_ACTIVE_DESKTOP;
478 if (diff > 0) {
479 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
480 ngettext("Removed <b>%i</b> unused definition in <defs>.",
481 "Removed <b>%i</b> unused definitions in <defs>.",
482 diff),
483 diff);
484 } else {
485 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
486 }
487 }
491 /*######################
492 ## S A V E
493 ######################*/
495 /**
496 * This 'save' function called by the others below
497 *
498 * \param official whether to set :output_module and :modified in the
499 * document; is true for normal save, false for temporary saves
500 */
501 static bool
502 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
503 Inkscape::Extension::Extension *key, bool saveas, bool official)
504 {
505 if (!doc || uri.size()<1) //Safety check
506 return false;
508 try {
509 Inkscape::Extension::save(key, doc, uri.c_str(),
510 false,
511 saveas, official);
512 } catch (Inkscape::Extension::Output::no_extension_found &e) {
513 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
514 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
515 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
516 sp_ui_error_dialog(text);
517 g_free(text);
518 g_free(safeUri);
519 return FALSE;
520 } catch (Inkscape::Extension::Output::save_failed &e) {
521 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
522 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
523 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
524 sp_ui_error_dialog(text);
525 g_free(text);
526 g_free(safeUri);
527 return FALSE;
528 } catch (Inkscape::Extension::Output::no_overwrite &e) {
529 return sp_file_save_dialog(parentWindow, doc);
530 }
532 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
533 return true;
534 }
536 /*
537 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
538 */
539 bool
540 file_save_remote(SPDocument *doc, const Glib::ustring &uri,
541 Inkscape::Extension::Extension *key, bool saveas, bool official)
542 {
543 #ifdef WITH_GNOME_VFS
545 #define BUF_SIZE 8192
546 gnome_vfs_init();
548 GnomeVFSHandle *from_handle = NULL;
549 GnomeVFSHandle *to_handle = NULL;
550 GnomeVFSFileSize bytes_read;
551 GnomeVFSFileSize bytes_written;
552 GnomeVFSResult result;
553 guint8 buffer[8192];
555 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
557 if ( uri_local == NULL ) {
558 g_warning( "Error converting filename to locale encoding.");
559 }
561 // Gets the temp file name.
562 Glib::ustring fileName = Glib::get_tmp_dir ();
563 fileName.append(G_DIR_SEPARATOR_S);
564 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
566 // Open the temp file to send.
567 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
569 if (result != GNOME_VFS_OK) {
570 g_warning("Could not find the temp saving.");
571 return false;
572 }
575 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
578 if (result == GNOME_VFS_ERROR_NOT_FOUND){
579 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
580 }
582 if (result != GNOME_VFS_OK) {
583 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
584 return false;
585 }
587 while (1) {
589 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
591 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
592 result = gnome_vfs_close (from_handle);
593 result = gnome_vfs_close (to_handle);
594 return true;
595 }
597 if (result != GNOME_VFS_OK) {
598 g_warning("%s", gnome_vfs_result_to_string(result));
599 return false;
600 }
601 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
602 if (result != GNOME_VFS_OK) {
603 g_warning("%s", gnome_vfs_result_to_string(result));
604 return false;
605 }
608 if (bytes_read != bytes_written){
609 return false;
610 }
612 }
613 return true;
614 #else
615 // in case we do not have GNOME_VFS
616 return false;
617 #endif
619 }
622 /**
623 * Display a SaveAs dialog. Save the document if OK pressed.
624 *
625 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
626 */
627 bool
628 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
629 {
631 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
633 Inkscape::Extension::Output *extension = 0;
635 //# Get the default extension name
636 Glib::ustring default_extension;
637 char *attr = (char *)repr->attribute("inkscape:output_extension");
638 if (!attr)
639 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
640 if (attr)
641 default_extension = attr;
642 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
644 Glib::ustring save_path;
645 Glib::ustring save_loc;
647 if (doc->uri == NULL) {
648 char formatBuf[256];
649 int i = 1;
651 Glib::ustring filename_extension = ".svg";
652 extension = dynamic_cast<Inkscape::Extension::Output *>
653 (Inkscape::Extension::db.get(default_extension.c_str()));
654 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
655 if (extension)
656 filename_extension = extension->get_extension();
658 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
659 if (attr)
660 save_path = attr;
662 if (!Inkscape::IO::file_test(save_path.c_str(),
663 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
664 save_path = "";
666 if (save_path.size()<1)
667 save_path = g_get_home_dir();
669 save_loc = save_path;
670 save_loc.append(G_DIR_SEPARATOR_S);
671 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
672 save_loc.append(formatBuf);
674 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
675 save_loc = save_path;
676 save_loc.append(G_DIR_SEPARATOR_S);
677 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
678 save_loc.append(formatBuf);
679 }
680 } else {
681 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
682 Glib::path_get_basename(doc->uri));
683 }
685 // convert save_loc from utf-8 to locale
686 // is this needed any more, now that everything is handled in
687 // Inkscape::IO?
688 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
690 if ( save_loc_local.size() > 0)
691 save_loc = save_loc_local;
693 //# Show the SaveAs dialog
694 char const * dialog_title;
695 if (is_copy) {
696 dialog_title = (char const *) _("Select file to save a copy to");
697 } else {
698 dialog_title = (char const *) _("Select file to save to");
699 }
700 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
701 Inkscape::UI::Dialog::FileSaveDialog::create(
702 parentWindow,
703 save_loc,
704 Inkscape::UI::Dialog::SVG_TYPES,
705 (char const *) _("Select file to save to"),
706 default_extension
707 );
709 saveDialog->change_title(dialog_title);
710 saveDialog->setSelectionType(extension);
712 // allow easy access to the user's own templates folder
713 gchar *templates = profile_path ("templates");
714 if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
715 dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
716 }
717 g_free (templates);
719 bool success = saveDialog->show();
720 if (!success) {
721 delete saveDialog;
722 return success;
723 }
725 Glib::ustring fileName = saveDialog->getFilename();
726 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
728 delete saveDialog;
730 saveDialog = 0;
732 if (fileName.size() > 0) {
733 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
735 if ( newFileName.size()>0 )
736 fileName = newFileName;
737 else
738 g_warning( "Error converting save filename to UTF-8." );
740 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
742 if (success)
743 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
745 save_path = Glib::path_get_dirname(fileName);
746 prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
748 return success;
749 }
752 return false;
753 }
756 /**
757 * Save a document, displaying a SaveAs dialog if necessary.
758 */
759 bool
760 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
761 {
762 bool success = true;
764 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
766 gchar const *fn = repr->attribute("sodipodi:modified");
767 if (fn != NULL) {
768 if ( doc->uri == NULL
769 || repr->attribute("inkscape:output_extension") == NULL )
770 {
771 return sp_file_save_dialog(parentWindow, doc, FALSE);
772 } else {
773 fn = g_strdup(doc->uri);
774 gchar const *ext = repr->attribute("inkscape:output_extension");
775 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
776 g_free((void *) fn);
777 }
778 } else {
779 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
780 success = TRUE;
781 }
783 return success;
784 }
787 /**
788 * Save a document.
789 */
790 bool
791 sp_file_save(Gtk::Window &parentWindow, gpointer object, gpointer data)
792 {
793 if (!SP_ACTIVE_DOCUMENT)
794 return false;
796 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
798 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
799 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
800 }
803 /**
804 * Save a document, always displaying the SaveAs dialog.
805 */
806 bool
807 sp_file_save_as(Gtk::Window &parentWindow, gpointer object, gpointer data)
808 {
809 if (!SP_ACTIVE_DOCUMENT)
810 return false;
811 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
812 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
813 }
817 /**
818 * Save a copy of a document, always displaying a sort of SaveAs dialog.
819 */
820 bool
821 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer object, gpointer data)
822 {
823 if (!SP_ACTIVE_DOCUMENT)
824 return false;
825 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
826 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
827 }
830 /*######################
831 ## I M P O R T
832 ######################*/
834 /**
835 * Import a resource. Called by sp_file_import()
836 */
837 void
838 file_import(SPDocument *in_doc, const Glib::ustring &uri,
839 Inkscape::Extension::Extension *key)
840 {
841 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
843 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
844 SPDocument *doc;
845 try {
846 doc = Inkscape::Extension::open(key, uri.c_str());
847 } catch (Inkscape::Extension::Input::no_extension_found &e) {
848 doc = NULL;
849 } catch (Inkscape::Extension::Input::open_failed &e) {
850 doc = NULL;
851 }
853 if (doc != NULL) {
854 // move imported defs to our document's defs
855 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
856 SPObject *defs = SP_DOCUMENT_DEFS(doc);
858 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
859 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
861 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
862 for (SPObject *child = sp_object_first_child(defs);
863 child != NULL; child = SP_OBJECT_NEXT(child))
864 {
865 // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
866 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
867 }
869 guint items_count = 0;
870 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
871 child != NULL; child = SP_OBJECT_NEXT(child)) {
872 if (SP_IS_ITEM(child))
873 items_count ++;
874 }
875 SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
877 SPObject *new_obj = NULL;
879 if ((style && style->firstChild()) || items_count > 1) {
880 // create group
881 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
882 Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
883 sp_repr_css_set (newgroup, style, "style");
885 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
886 if (SP_IS_ITEM(child)) {
887 Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
889 // convert layers to groups; FIXME: add "preserve layers" mode where each layer
890 // from impot is copied to the same-named layer in host
891 newchild->setAttribute("inkscape:groupmode", NULL);
893 newgroup->appendChild(newchild);
894 }
895 }
897 if (desktop) {
898 // Add it to the current layer
899 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
900 } else {
901 // There's no desktop (command line run?)
902 // FIXME: For such cases we need a document:: method to return the current layer
903 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
904 }
906 Inkscape::GC::release(newgroup);
907 } else {
908 // just add one item
909 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
910 if (SP_IS_ITEM(child)) {
911 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
912 newitem->setAttribute("inkscape:groupmode", NULL);
914 if (desktop) {
915 // Add it to the current layer
916 new_obj = desktop->currentLayer()->appendChildRepr(newitem);
917 } else {
918 // There's no desktop (command line run?)
919 // FIXME: For such cases we need a document:: method to return the current layer
920 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
921 }
923 }
924 }
925 }
927 if (style) sp_repr_css_attr_unref (style);
929 // select and move the imported item
930 if (new_obj && SP_IS_ITEM(new_obj)) {
931 Inkscape::Selection *selection = sp_desktop_selection(desktop);
932 selection->set(SP_ITEM(new_obj));
934 // To move the imported object, we must temporarily set the "transform pattern with
935 // object" option.
936 {
937 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
938 prefs_set_int_attribute("options.transform", "pattern", 1);
939 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
940 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
941 if (sel_bbox) {
942 NR::Point m( desktop->point() - sel_bbox->midpoint() );
943 sp_selection_move_relative(selection, m);
944 }
945 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
946 }
947 }
949 sp_document_unref(doc);
950 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
951 _("Import"));
953 } else {
954 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
955 sp_ui_error_dialog(text);
956 g_free(text);
957 }
959 return;
960 }
963 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
965 /**
966 * Display an Open dialog, import a resource if OK pressed.
967 */
968 void
969 sp_file_import(Gtk::Window &parentWindow)
970 {
971 static Glib::ustring import_path;
973 SPDocument *doc = SP_ACTIVE_DOCUMENT;
974 if (!doc)
975 return;
977 if (!importDialogInstance) {
978 importDialogInstance =
979 Inkscape::UI::Dialog::FileOpenDialog::create(
980 parentWindow,
981 import_path,
982 Inkscape::UI::Dialog::IMPORT_TYPES,
983 (char const *)_("Select file to import"));
984 }
986 bool success = importDialogInstance->show();
987 if (!success)
988 return;
990 //# Get file name and extension type
991 Glib::ustring fileName = importDialogInstance->getFilename();
992 Inkscape::Extension::Extension *selection =
993 importDialogInstance->getSelectionType();
996 if (fileName.size() > 0) {
998 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1000 if ( newFileName.size() > 0)
1001 fileName = newFileName;
1002 else
1003 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1006 import_path = fileName;
1007 if (import_path.size()>0)
1008 import_path.append(G_DIR_SEPARATOR_S);
1010 file_import(doc, fileName, selection);
1011 }
1013 return;
1014 }
1018 /*######################
1019 ## E X P O R T
1020 ######################*/
1022 //#define NEW_EXPORT_DIALOG
1026 #ifdef NEW_EXPORT_DIALOG
1028 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1030 /**
1031 * Display an Export dialog, export as the selected type if OK pressed
1032 */
1033 bool
1034 sp_file_export_dialog(void *widget)
1035 {
1036 //# temp hack for 'doc' until we can switch to this dialog
1037 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1039 Glib::ustring export_path;
1040 Glib::ustring export_loc;
1042 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1044 Inkscape::Extension::Output *extension;
1046 //# Get the default extension name
1047 Glib::ustring default_extension;
1048 char *attr = (char *)repr->attribute("inkscape:output_extension");
1049 if (!attr)
1050 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1051 if (attr)
1052 default_extension = attr;
1053 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1055 if (doc->uri == NULL)
1056 {
1057 char formatBuf[256];
1059 Glib::ustring filename_extension = ".svg";
1060 extension = dynamic_cast<Inkscape::Extension::Output *>
1061 (Inkscape::Extension::db.get(default_extension.c_str()));
1062 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1063 if (extension)
1064 filename_extension = extension->get_extension();
1066 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1067 if (attr)
1068 export_path = attr;
1070 if (!Inkscape::IO::file_test(export_path.c_str(),
1071 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1072 export_path = "";
1074 if (export_path.size()<1)
1075 export_path = g_get_home_dir();
1077 export_loc = export_path;
1078 export_loc.append(G_DIR_SEPARATOR_S);
1079 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1080 export_loc.append(formatBuf);
1082 }
1083 else
1084 {
1085 export_path = Glib::path_get_dirname(doc->uri);
1086 }
1088 // convert save_loc from utf-8 to locale
1089 // is this needed any more, now that everything is handled in
1090 // Inkscape::IO?
1091 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1092 if ( export_path_local.size() > 0)
1093 export_path = export_path_local;
1095 //# Show the SaveAs dialog
1096 if (!exportDialogInstance)
1097 exportDialogInstance =
1098 Inkscape::UI::Dialog::FileExportDialog::create(
1099 export_path,
1100 Inkscape::UI::Dialog::EXPORT_TYPES,
1101 (char const *) _("Select file to export to"),
1102 default_extension
1103 );
1105 bool success = exportDialogInstance->show();
1106 if (!success)
1107 return success;
1109 Glib::ustring fileName = exportDialogInstance->getFilename();
1111 Inkscape::Extension::Extension *selectionType =
1112 exportDialogInstance->getSelectionType();
1115 if (fileName.size() > 0) {
1116 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1118 if ( newFileName.size()>0 )
1119 fileName = newFileName;
1120 else
1121 g_warning( "Error converting save filename to UTF-8." );
1123 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1125 if (success)
1126 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1128 export_path = fileName;
1129 prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1131 return success;
1132 }
1135 return false;
1136 }
1138 #else
1140 /**
1141 *
1142 */
1143 bool
1144 sp_file_export_dialog(void *widget)
1145 {
1146 sp_export_dialog();
1147 return true;
1148 }
1150 #endif
1152 /*######################
1153 ## E X P O R T T O O C A L
1154 ######################*/
1156 /**
1157 * Display an Export dialog, export as the selected type if OK pressed
1158 */
1159 bool
1160 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1161 {
1163 if (!SP_ACTIVE_DOCUMENT)
1164 return false;
1166 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1168 Glib::ustring export_path;
1169 Glib::ustring export_loc;
1170 Glib::ustring fileName;
1171 Inkscape::Extension::Extension *selectionType;
1173 bool success = false;
1175 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1176 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1177 static bool gotSuccess = false;
1179 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1180 // Verify whether the document is saved, so save this as temporary
1182 char *str = (char *) repr->attribute("sodipodi:modified");
1183 if ((!doc->uri) && (!str))
1184 return false;
1186 // Get the default extension name
1187 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1188 char formatBuf[256];
1190 Glib::ustring filename_extension = ".svg";
1191 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1193 export_path = Glib::get_tmp_dir ();
1195 export_loc = export_path;
1196 export_loc.append(G_DIR_SEPARATOR_S);
1197 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1198 export_loc.append(formatBuf);
1200 // convert save_loc from utf-8 to locale
1201 // is this needed any more, now that everything is handled in
1202 // Inkscape::IO?
1203 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1204 if ( export_path_local.size() > 0)
1205 export_path = export_path_local;
1207 // Show the Export To OCAL dialog
1208 if (!exportDialogInstance)
1209 exportDialogInstance = Inkscape::UI::Dialog::FileExportToOCALDialog::create(
1210 parentWindow,
1211 Inkscape::UI::Dialog::EXPORT_TYPES,
1212 (char const *) _("Select file to export to")
1213 );
1215 success = exportDialogInstance->show();
1216 if (!success)
1217 return success;
1219 fileName = exportDialogInstance->getFilename();
1221 fileName.append(filename_extension.c_str());
1222 if (fileName.size() > 0) {
1223 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1225 if ( newFileName.size()>0 )
1226 fileName = newFileName;
1227 else
1228 g_warning( "Error converting save filename to UTF-8." );
1229 }
1230 Glib::ustring filePath = export_path;
1231 filePath.append(G_DIR_SEPARATOR_S);
1232 filePath.append(Glib::path_get_basename(fileName));
1234 fileName = filePath;
1236 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1238 if (!success){
1239 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1240 sp_ui_error_dialog(text);
1242 return success;
1243 }
1245 // Start now the submition
1247 // Create the uri
1248 Glib::ustring uri = "dav://";
1249 char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1250 char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1251 if ((username == NULL) || (!strcmp(username, "")) || (password == NULL) || (!strcmp(password, "")))
1252 {
1253 if(!gotSuccess)
1254 {
1255 if (!exportPasswordDialogInstance)
1256 exportPasswordDialogInstance = Inkscape::UI::Dialog::FileExportToOCALPasswordDialog::create(
1257 parentWindow,
1258 (char const *) _("Open Clip Art Login"));
1259 success = exportPasswordDialogInstance->show();
1260 if (!success)
1261 return success;
1262 }
1263 username = (char *)exportPasswordDialogInstance->getUsername().c_str();
1264 password = (char *)exportPasswordDialogInstance->getPassword().c_str();
1265 }
1266 uri.append(username);
1267 uri.append(":");
1268 uri.append(password);
1269 uri.append("@");
1270 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1271 uri.append("/dav.php/");
1272 uri.append(Glib::path_get_basename(fileName));
1274 // Save as a remote file using the dav protocol.
1275 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1276 remove(fileName.c_str());
1277 if (!success)
1278 {
1279 gchar *text = g_strdup_printf(_("Error exporting the document. Verify if the server name, username and password are correct. If the server have support for webdav and verify if you didn't forget to choose a license too."));
1280 sp_ui_error_dialog(text);
1281 }
1282 else
1283 gotSuccess = true;
1285 return success;
1286 }
1288 /**
1289 * Export the current document to OCAL
1290 */
1291 void
1292 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1293 {
1295 // Try to execute the new code and return;
1296 if (!SP_ACTIVE_DOCUMENT)
1297 return;
1298 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1299 if (success)
1300 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1301 }
1304 /*######################
1305 ## I M P O R T F R O M O C A L
1306 ######################*/
1308 /**
1309 * Display an ImportToOcal Dialog, and the selected document from OCAL
1310 */
1311 void
1312 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1313 {
1314 static Glib::ustring import_path;
1316 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1317 if (!doc)
1318 return;
1320 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1322 if (!importDialogInstance) {
1323 importDialogInstance =
1324 Inkscape::UI::Dialog::FileImportFromOCALDialog::create(
1325 parentWindow,
1326 import_path,
1327 Inkscape::UI::Dialog::IMPORT_TYPES,
1328 (char const *)_("Import From Open Clip Art Library"));
1329 }
1331 bool success = importDialogInstance->show();
1332 if (!success)
1333 return;
1335 // Get file name and extension type
1336 Glib::ustring fileName = importDialogInstance->getFilename();
1337 Inkscape::Extension::Extension *selection =
1338 importDialogInstance->getSelectionType();
1340 if (fileName.size() > 0) {
1342 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1344 if ( newFileName.size() > 0)
1345 fileName = newFileName;
1346 else
1347 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1349 import_path = fileName;
1350 if (import_path.size()>0)
1351 import_path.append(G_DIR_SEPARATOR_S);
1353 file_import(doc, fileName, selection);
1354 }
1356 return;
1357 }
1359 /*######################
1360 ## P R I N T
1361 ######################*/
1364 /**
1365 * Print the current document, if any.
1366 */
1367 void
1368 sp_file_print()
1369 {
1370 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1371 if (doc)
1372 sp_print_document(doc, FALSE);
1373 }
1376 /**
1377 * Print the current document, if any. Do not use
1378 * the machine's print drivers.
1379 */
1380 void
1381 sp_file_print_direct()
1382 {
1383 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1384 if (doc)
1385 sp_print_document(doc, TRUE);
1386 }
1389 /**
1390 * Display what the drawing would look like, if
1391 * printed.
1392 */
1393 void
1394 sp_file_print_preview(gpointer object, gpointer data)
1395 {
1397 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1398 if (doc)
1399 sp_print_preview_document(doc);
1401 }
1403 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1404 {
1405 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1407 if ( 0 ) {
1408 gchar const* things[] = {
1409 "data:foo,bar",
1410 "http://www.google.com/image.png",
1411 "ftp://ssd.com/doo",
1412 "/foo/dee/bar.svg",
1413 "foo.svg",
1414 "file:/foo/dee/bar.svg",
1415 "file:///foo/dee/bar.svg",
1416 "file:foo.svg",
1417 "/foo/bar\xe1\x84\x92.svg",
1418 "file:///foo/bar\xe1\x84\x92.svg",
1419 "file:///foo/bar%e1%84%92.svg",
1420 "/foo/bar%e1%84%92.svg",
1421 "bar\xe1\x84\x92.svg",
1422 "bar%e1%84%92.svg",
1423 NULL
1424 };
1425 g_message("+------");
1426 for ( int i = 0; things[i]; i++ )
1427 {
1428 try
1429 {
1430 URI uri(things[i]);
1431 gboolean isAbs = g_path_is_absolute( things[i] );
1432 gchar *str = uri.toString();
1433 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1434 (int)uri.isRelative(),
1435 uri.getScheme(),
1436 uri.getPath(),
1437 uri.getOpaque(),
1438 things[i],
1439 str );
1440 g_free(str);
1441 }
1442 catch ( MalformedURIException err )
1443 {
1444 dump_str( things[i], "MalformedURIException" );
1445 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1446 g_message(" gone from [%s] to [%s]", things[i], redo );
1447 if ( redo == NULL )
1448 {
1449 URI again = URI::fromUtf8( things[i] );
1450 gboolean isAbs = g_path_is_absolute( things[i] );
1451 gchar *str = again.toString();
1452 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1453 (int)again.isRelative(),
1454 again.getScheme(),
1455 again.getPath(),
1456 again.getOpaque(),
1457 things[i],
1458 str );
1459 g_free(str);
1460 g_message(" ----");
1461 }
1462 }
1463 }
1464 g_message("+------");
1465 }
1467 GSList const *images = sp_document_get_resource_list(doc, "image");
1468 for (GSList const *l = images; l != NULL; l = l->next) {
1469 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1471 const gchar *href = ir->attribute("xlink:href");
1473 // First try to figure out an absolute path to the asset
1474 //g_message("image href [%s]", href );
1475 if (spns && !g_path_is_absolute(href)) {
1476 const gchar *absref = ir->attribute("sodipodi:absref");
1477 const gchar *base_href = g_build_filename(base, href, NULL);
1478 //g_message(" absr [%s]", absref );
1480 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1481 {
1482 // only switch over if the absref is valid while href is not
1483 href = absref;
1484 //g_message(" copied absref to href");
1485 }
1486 }
1488 // Once we have an absolute path, convert it relative to the new location
1489 if (href && g_path_is_absolute(href)) {
1490 const gchar *relname = sp_relative_path_from_path(href, base);
1491 //g_message(" setting to [%s]", relname );
1492 ir->setAttribute("xlink:href", relname);
1493 }
1494 // TODO next refinement is to make the first choice keeping the relative path as-is if
1495 // based on the new location it gives us a valid file.
1496 }
1497 }
1500 /*
1501 Local Variables:
1502 mode:c++
1503 c-file-style:"stroustrup"
1504 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1505 indent-tabs-mode:nil
1506 fill-column:99
1507 End:
1508 */
1509 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :