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 }
574 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
575 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
577 if (result != GNOME_VFS_OK) {
578 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
579 return false;
580 }
582 while (1) {
584 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
586 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
587 result = gnome_vfs_close (from_handle);
588 result = gnome_vfs_close (to_handle);
589 return true;
590 }
592 if (result != GNOME_VFS_OK) {
593 g_warning("%s", gnome_vfs_result_to_string(result));
594 return false;
595 }
596 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
597 if (result != GNOME_VFS_OK) {
598 g_warning("%s", gnome_vfs_result_to_string(result));
599 return false;
600 }
603 if (bytes_read != bytes_written){
604 return false;
605 }
607 }
608 return true;
609 #else
610 // in case we do not have GNOME_VFS
611 return false;
612 #endif
614 }
617 /**
618 * Display a SaveAs dialog. Save the document if OK pressed.
619 *
620 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
621 */
622 bool
623 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
624 {
626 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
628 Inkscape::Extension::Output *extension = 0;
630 //# Get the default extension name
631 Glib::ustring default_extension;
632 char *attr = (char *)repr->attribute("inkscape:output_extension");
633 if (!attr)
634 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
635 if (attr)
636 default_extension = attr;
637 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
639 Glib::ustring save_path;
640 Glib::ustring save_loc;
642 if (doc->uri == NULL) {
643 char formatBuf[256];
644 int i = 1;
646 Glib::ustring filename_extension = ".svg";
647 extension = dynamic_cast<Inkscape::Extension::Output *>
648 (Inkscape::Extension::db.get(default_extension.c_str()));
649 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
650 if (extension)
651 filename_extension = extension->get_extension();
653 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
654 if (attr)
655 save_path = attr;
657 if (!Inkscape::IO::file_test(save_path.c_str(),
658 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
659 save_path = "";
661 if (save_path.size()<1)
662 save_path = g_get_home_dir();
664 save_loc = save_path;
665 save_loc.append(G_DIR_SEPARATOR_S);
666 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
667 save_loc.append(formatBuf);
669 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
670 save_loc = save_path;
671 save_loc.append(G_DIR_SEPARATOR_S);
672 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
673 save_loc.append(formatBuf);
674 }
675 } else {
676 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
677 Glib::path_get_basename(doc->uri));
678 }
680 // convert save_loc from utf-8 to locale
681 // is this needed any more, now that everything is handled in
682 // Inkscape::IO?
683 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
685 if ( save_loc_local.size() > 0)
686 save_loc = save_loc_local;
688 //# Show the SaveAs dialog
689 char const * dialog_title;
690 if (is_copy) {
691 dialog_title = (char const *) _("Select file to save a copy to");
692 } else {
693 dialog_title = (char const *) _("Select file to save to");
694 }
695 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
696 Inkscape::UI::Dialog::FileSaveDialog::create(
697 parentWindow,
698 save_loc,
699 Inkscape::UI::Dialog::SVG_TYPES,
700 (char const *) _("Select file to save to"),
701 default_extension
702 );
704 saveDialog->change_title(dialog_title);
705 saveDialog->setSelectionType(extension);
707 // allow easy access to the user's own templates folder
708 gchar *templates = profile_path ("templates");
709 if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
710 dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
711 }
712 g_free (templates);
714 bool success = saveDialog->show();
715 if (!success) {
716 delete saveDialog;
717 return success;
718 }
720 Glib::ustring fileName = saveDialog->getFilename();
721 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
723 delete saveDialog;
725 saveDialog = 0;
727 if (fileName.size() > 0) {
728 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
730 if ( newFileName.size()>0 )
731 fileName = newFileName;
732 else
733 g_warning( "Error converting save filename to UTF-8." );
735 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
737 if (success)
738 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
740 save_path = Glib::path_get_dirname(fileName);
741 prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
743 return success;
744 }
747 return false;
748 }
751 /**
752 * Save a document, displaying a SaveAs dialog if necessary.
753 */
754 bool
755 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
756 {
757 bool success = true;
759 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
761 gchar const *fn = repr->attribute("sodipodi:modified");
762 if (fn != NULL) {
763 if ( doc->uri == NULL
764 || repr->attribute("inkscape:output_extension") == NULL )
765 {
766 return sp_file_save_dialog(parentWindow, doc, FALSE);
767 } else {
768 fn = g_strdup(doc->uri);
769 gchar const *ext = repr->attribute("inkscape:output_extension");
770 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
771 g_free((void *) fn);
772 }
773 } else {
774 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
775 success = TRUE;
776 }
778 return success;
779 }
782 /**
783 * Save a document.
784 */
785 bool
786 sp_file_save(Gtk::Window &parentWindow, gpointer object, gpointer data)
787 {
788 if (!SP_ACTIVE_DOCUMENT)
789 return false;
791 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
793 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
794 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
795 }
798 /**
799 * Save a document, always displaying the SaveAs dialog.
800 */
801 bool
802 sp_file_save_as(Gtk::Window &parentWindow, gpointer object, gpointer data)
803 {
804 if (!SP_ACTIVE_DOCUMENT)
805 return false;
806 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
807 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
808 }
812 /**
813 * Save a copy of a document, always displaying a sort of SaveAs dialog.
814 */
815 bool
816 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer object, gpointer data)
817 {
818 if (!SP_ACTIVE_DOCUMENT)
819 return false;
820 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
821 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
822 }
825 /*######################
826 ## I M P O R T
827 ######################*/
829 /**
830 * Import a resource. Called by sp_file_import()
831 */
832 void
833 file_import(SPDocument *in_doc, const Glib::ustring &uri,
834 Inkscape::Extension::Extension *key)
835 {
836 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
838 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
839 SPDocument *doc;
840 try {
841 doc = Inkscape::Extension::open(key, uri.c_str());
842 } catch (Inkscape::Extension::Input::no_extension_found &e) {
843 doc = NULL;
844 } catch (Inkscape::Extension::Input::open_failed &e) {
845 doc = NULL;
846 }
848 if (doc != NULL) {
849 // move imported defs to our document's defs
850 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
851 SPObject *defs = SP_DOCUMENT_DEFS(doc);
853 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
854 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
856 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
857 for (SPObject *child = sp_object_first_child(defs);
858 child != NULL; child = SP_OBJECT_NEXT(child))
859 {
860 // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
861 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
862 }
864 guint items_count = 0;
865 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
866 child != NULL; child = SP_OBJECT_NEXT(child)) {
867 if (SP_IS_ITEM(child))
868 items_count ++;
869 }
870 SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
872 SPObject *new_obj = NULL;
874 if ((style && style->firstChild()) || items_count > 1) {
875 // create group
876 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
877 Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
878 sp_repr_css_set (newgroup, style, "style");
880 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
881 if (SP_IS_ITEM(child)) {
882 Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
884 // convert layers to groups; FIXME: add "preserve layers" mode where each layer
885 // from impot is copied to the same-named layer in host
886 newchild->setAttribute("inkscape:groupmode", NULL);
888 newgroup->appendChild(newchild);
889 }
890 }
892 if (desktop) {
893 // Add it to the current layer
894 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
895 } else {
896 // There's no desktop (command line run?)
897 // FIXME: For such cases we need a document:: method to return the current layer
898 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
899 }
901 Inkscape::GC::release(newgroup);
902 } else {
903 // just add one item
904 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
905 if (SP_IS_ITEM(child)) {
906 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
907 newitem->setAttribute("inkscape:groupmode", NULL);
909 if (desktop) {
910 // Add it to the current layer
911 new_obj = desktop->currentLayer()->appendChildRepr(newitem);
912 } else {
913 // There's no desktop (command line run?)
914 // FIXME: For such cases we need a document:: method to return the current layer
915 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
916 }
918 }
919 }
920 }
922 if (style) sp_repr_css_attr_unref (style);
924 // select and move the imported item
925 if (new_obj && SP_IS_ITEM(new_obj)) {
926 Inkscape::Selection *selection = sp_desktop_selection(desktop);
927 selection->set(SP_ITEM(new_obj));
929 // To move the imported object, we must temporarily set the "transform pattern with
930 // object" option.
931 {
932 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
933 prefs_set_int_attribute("options.transform", "pattern", 1);
934 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
935 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
936 if (sel_bbox) {
937 NR::Point m( desktop->point() - sel_bbox->midpoint() );
938 sp_selection_move_relative(selection, m);
939 }
940 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
941 }
942 }
944 sp_document_unref(doc);
945 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
946 _("Import"));
948 } else {
949 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
950 sp_ui_error_dialog(text);
951 g_free(text);
952 }
954 return;
955 }
958 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
960 /**
961 * Display an Open dialog, import a resource if OK pressed.
962 */
963 void
964 sp_file_import(Gtk::Window &parentWindow)
965 {
966 static Glib::ustring import_path;
968 SPDocument *doc = SP_ACTIVE_DOCUMENT;
969 if (!doc)
970 return;
972 if (!importDialogInstance) {
973 importDialogInstance =
974 Inkscape::UI::Dialog::FileOpenDialog::create(
975 parentWindow,
976 import_path,
977 Inkscape::UI::Dialog::IMPORT_TYPES,
978 (char const *)_("Select file to import"));
979 }
981 bool success = importDialogInstance->show();
982 if (!success)
983 return;
985 //# Get file name and extension type
986 Glib::ustring fileName = importDialogInstance->getFilename();
987 Inkscape::Extension::Extension *selection =
988 importDialogInstance->getSelectionType();
991 if (fileName.size() > 0) {
993 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
995 if ( newFileName.size() > 0)
996 fileName = newFileName;
997 else
998 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1001 import_path = fileName;
1002 if (import_path.size()>0)
1003 import_path.append(G_DIR_SEPARATOR_S);
1005 file_import(doc, fileName, selection);
1006 }
1008 return;
1009 }
1013 /*######################
1014 ## E X P O R T
1015 ######################*/
1017 //#define NEW_EXPORT_DIALOG
1021 #ifdef NEW_EXPORT_DIALOG
1023 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1025 /**
1026 * Display an Export dialog, export as the selected type if OK pressed
1027 */
1028 bool
1029 sp_file_export_dialog(void *widget)
1030 {
1031 //# temp hack for 'doc' until we can switch to this dialog
1032 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1034 Glib::ustring export_path;
1035 Glib::ustring export_loc;
1037 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1039 Inkscape::Extension::Output *extension;
1041 //# Get the default extension name
1042 Glib::ustring default_extension;
1043 char *attr = (char *)repr->attribute("inkscape:output_extension");
1044 if (!attr)
1045 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1046 if (attr)
1047 default_extension = attr;
1048 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1050 if (doc->uri == NULL)
1051 {
1052 char formatBuf[256];
1054 Glib::ustring filename_extension = ".svg";
1055 extension = dynamic_cast<Inkscape::Extension::Output *>
1056 (Inkscape::Extension::db.get(default_extension.c_str()));
1057 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1058 if (extension)
1059 filename_extension = extension->get_extension();
1061 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1062 if (attr)
1063 export_path = attr;
1065 if (!Inkscape::IO::file_test(export_path.c_str(),
1066 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1067 export_path = "";
1069 if (export_path.size()<1)
1070 export_path = g_get_home_dir();
1072 export_loc = export_path;
1073 export_loc.append(G_DIR_SEPARATOR_S);
1074 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1075 export_loc.append(formatBuf);
1077 }
1078 else
1079 {
1080 export_path = Glib::path_get_dirname(doc->uri);
1081 }
1083 // convert save_loc from utf-8 to locale
1084 // is this needed any more, now that everything is handled in
1085 // Inkscape::IO?
1086 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1087 if ( export_path_local.size() > 0)
1088 export_path = export_path_local;
1090 //# Show the SaveAs dialog
1091 if (!exportDialogInstance)
1092 exportDialogInstance =
1093 Inkscape::UI::Dialog::FileExportDialog::create(
1094 export_path,
1095 Inkscape::UI::Dialog::EXPORT_TYPES,
1096 (char const *) _("Select file to export to"),
1097 default_extension
1098 );
1100 bool success = exportDialogInstance->show();
1101 if (!success)
1102 return success;
1104 Glib::ustring fileName = exportDialogInstance->getFilename();
1106 Inkscape::Extension::Extension *selectionType =
1107 exportDialogInstance->getSelectionType();
1110 if (fileName.size() > 0) {
1111 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1113 if ( newFileName.size()>0 )
1114 fileName = newFileName;
1115 else
1116 g_warning( "Error converting save filename to UTF-8." );
1118 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1120 if (success)
1121 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1123 export_path = fileName;
1124 prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1126 return success;
1127 }
1130 return false;
1131 }
1133 #else
1135 /**
1136 *
1137 */
1138 bool
1139 sp_file_export_dialog(void *widget)
1140 {
1141 sp_export_dialog();
1142 return true;
1143 }
1145 #endif
1147 /*######################
1148 ## E X P O R T T O O C A L
1149 ######################*/
1151 /**
1152 * Display an Export dialog, export as the selected type if OK pressed
1153 */
1154 bool
1155 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1156 {
1158 if (!SP_ACTIVE_DOCUMENT)
1159 return false;
1161 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1163 Glib::ustring export_path;
1164 Glib::ustring export_loc;
1165 Glib::ustring fileName;
1166 Inkscape::Extension::Extension *selectionType;
1168 bool success = false;
1170 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1171 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1172 static bool gotSuccess = false;
1174 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1175 // Verify whether the document is saved, so save this as temporary
1177 char *str = (char *) repr->attribute("sodipodi:modified");
1178 if ((!doc->uri) && (!str))
1179 return false;
1181 // Get the default extension name
1182 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1183 char formatBuf[256];
1185 Glib::ustring filename_extension = ".svg";
1186 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1188 export_path = Glib::get_tmp_dir ();
1190 export_loc = export_path;
1191 export_loc.append(G_DIR_SEPARATOR_S);
1192 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1193 export_loc.append(formatBuf);
1195 // convert save_loc from utf-8 to locale
1196 // is this needed any more, now that everything is handled in
1197 // Inkscape::IO?
1198 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1199 if ( export_path_local.size() > 0)
1200 export_path = export_path_local;
1202 // Show the Export To OCAL dialog
1203 if (!exportDialogInstance)
1204 exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
1205 parentWindow,
1206 Inkscape::UI::Dialog::EXPORT_TYPES,
1207 (char const *) _("Select file to export to")
1208 );
1210 success = exportDialogInstance->show();
1211 if (!success)
1212 return success;
1214 fileName = exportDialogInstance->getFilename();
1216 fileName.append(filename_extension.c_str());
1217 if (fileName.size() > 0) {
1218 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1220 if ( newFileName.size()>0 )
1221 fileName = newFileName;
1222 else
1223 g_warning( "Error converting save filename to UTF-8." );
1224 }
1225 Glib::ustring filePath = export_path;
1226 filePath.append(G_DIR_SEPARATOR_S);
1227 filePath.append(Glib::path_get_basename(fileName));
1229 fileName = filePath;
1231 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1233 if (!success){
1234 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1235 sp_ui_error_dialog(text);
1237 return success;
1238 }
1240 // Start now the submition
1242 // Create the uri
1243 Glib::ustring uri = "dav://";
1244 char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1245 char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1246 if ((username == NULL) || (!strcmp(username, "")) || (password == NULL) || (!strcmp(password, "")))
1247 {
1248 if(!gotSuccess)
1249 {
1250 if (!exportPasswordDialogInstance)
1251 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1252 parentWindow,
1253 (char const *) _("Open Clip Art Login"));
1254 success = exportPasswordDialogInstance->show();
1255 if (!success)
1256 return success;
1257 }
1258 username = (char *)exportPasswordDialogInstance->getUsername().c_str();
1259 password = (char *)exportPasswordDialogInstance->getPassword().c_str();
1260 }
1261 uri.append(username);
1262 uri.append(":");
1263 uri.append(password);
1264 uri.append("@");
1265 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1266 uri.append("/dav.php/");
1267 uri.append(Glib::path_get_basename(fileName));
1269 // Save as a remote file using the dav protocol.
1270 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1271 remove(fileName.c_str());
1272 if (!success)
1273 {
1274 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."));
1275 sp_ui_error_dialog(text);
1276 }
1277 else
1278 gotSuccess = true;
1280 return success;
1281 }
1283 /**
1284 * Export the current document to OCAL
1285 */
1286 void
1287 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1288 {
1290 // Try to execute the new code and return;
1291 if (!SP_ACTIVE_DOCUMENT)
1292 return;
1293 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1294 if (success)
1295 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1296 }
1299 /*######################
1300 ## I M P O R T F R O M O C A L
1301 ######################*/
1303 /**
1304 * Display an ImportToOcal Dialog, and the selected document from OCAL
1305 */
1306 void
1307 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1308 {
1309 static Glib::ustring import_path;
1311 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1312 if (!doc)
1313 return;
1315 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1317 if (!importDialogInstance) {
1318 importDialogInstance = new
1319 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1320 parentWindow,
1321 import_path,
1322 Inkscape::UI::Dialog::IMPORT_TYPES,
1323 (char const *)_("Import From Open Clip Art Library"));
1324 }
1326 bool success = importDialogInstance->show();
1327 if (!success)
1328 return;
1330 // Get file name and extension type
1331 Glib::ustring fileName = importDialogInstance->getFilename();
1332 Inkscape::Extension::Extension *selection =
1333 importDialogInstance->getSelectionType();
1335 if (fileName.size() > 0) {
1337 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1339 if ( newFileName.size() > 0)
1340 fileName = newFileName;
1341 else
1342 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1344 import_path = fileName;
1345 if (import_path.size()>0)
1346 import_path.append(G_DIR_SEPARATOR_S);
1348 file_import(doc, fileName, selection);
1349 }
1351 return;
1352 }
1354 /*######################
1355 ## P R I N T
1356 ######################*/
1359 /**
1360 * Print the current document, if any.
1361 */
1362 void
1363 sp_file_print()
1364 {
1365 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1366 if (doc)
1367 sp_print_document(doc, FALSE);
1368 }
1371 /**
1372 * Print the current document, if any. Do not use
1373 * the machine's print drivers.
1374 */
1375 void
1376 sp_file_print_direct()
1377 {
1378 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1379 if (doc)
1380 sp_print_document(doc, TRUE);
1381 }
1384 /**
1385 * Display what the drawing would look like, if
1386 * printed.
1387 */
1388 void
1389 sp_file_print_preview(gpointer object, gpointer data)
1390 {
1392 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1393 if (doc)
1394 sp_print_preview_document(doc);
1396 }
1398 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1399 {
1400 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1402 if ( 0 ) {
1403 gchar const* things[] = {
1404 "data:foo,bar",
1405 "http://www.google.com/image.png",
1406 "ftp://ssd.com/doo",
1407 "/foo/dee/bar.svg",
1408 "foo.svg",
1409 "file:/foo/dee/bar.svg",
1410 "file:///foo/dee/bar.svg",
1411 "file:foo.svg",
1412 "/foo/bar\xe1\x84\x92.svg",
1413 "file:///foo/bar\xe1\x84\x92.svg",
1414 "file:///foo/bar%e1%84%92.svg",
1415 "/foo/bar%e1%84%92.svg",
1416 "bar\xe1\x84\x92.svg",
1417 "bar%e1%84%92.svg",
1418 NULL
1419 };
1420 g_message("+------");
1421 for ( int i = 0; things[i]; i++ )
1422 {
1423 try
1424 {
1425 URI uri(things[i]);
1426 gboolean isAbs = g_path_is_absolute( things[i] );
1427 gchar *str = uri.toString();
1428 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1429 (int)uri.isRelative(),
1430 uri.getScheme(),
1431 uri.getPath(),
1432 uri.getOpaque(),
1433 things[i],
1434 str );
1435 g_free(str);
1436 }
1437 catch ( MalformedURIException err )
1438 {
1439 dump_str( things[i], "MalformedURIException" );
1440 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1441 g_message(" gone from [%s] to [%s]", things[i], redo );
1442 if ( redo == NULL )
1443 {
1444 URI again = URI::fromUtf8( things[i] );
1445 gboolean isAbs = g_path_is_absolute( things[i] );
1446 gchar *str = again.toString();
1447 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1448 (int)again.isRelative(),
1449 again.getScheme(),
1450 again.getPath(),
1451 again.getOpaque(),
1452 things[i],
1453 str );
1454 g_free(str);
1455 g_message(" ----");
1456 }
1457 }
1458 }
1459 g_message("+------");
1460 }
1462 GSList const *images = sp_document_get_resource_list(doc, "image");
1463 for (GSList const *l = images; l != NULL; l = l->next) {
1464 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1466 const gchar *href = ir->attribute("xlink:href");
1468 // First try to figure out an absolute path to the asset
1469 //g_message("image href [%s]", href );
1470 if (spns && !g_path_is_absolute(href)) {
1471 const gchar *absref = ir->attribute("sodipodi:absref");
1472 const gchar *base_href = g_build_filename(base, href, NULL);
1473 //g_message(" absr [%s]", absref );
1475 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1476 {
1477 // only switch over if the absref is valid while href is not
1478 href = absref;
1479 //g_message(" copied absref to href");
1480 }
1481 }
1483 // Once we have an absolute path, convert it relative to the new location
1484 if (href && g_path_is_absolute(href)) {
1485 const gchar *relname = sp_relative_path_from_path(href, base);
1486 //g_message(" setting to [%s]", relname );
1487 ir->setAttribute("xlink:href", relname);
1488 }
1489 // TODO next refinement is to make the first choice keeping the relative path as-is if
1490 // based on the new location it gives us a valid file.
1491 }
1492 }
1495 /*
1496 Local Variables:
1497 mode:c++
1498 c-file-style:"stroustrup"
1499 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1500 indent-tabs-mode:nil
1501 fill-column:99
1502 End:
1503 */
1504 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :