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 "prefs-utils.h"
51 #include "path-prefix.h"
53 #include "sp-namedview.h"
54 #include "desktop-handles.h"
56 #include "extension/db.h"
57 #include "extension/input.h"
58 #include "extension/output.h"
59 /* #include "extension/menu.h" */
60 #include "extension/system.h"
62 #include "io/sys.h"
63 #include "application/application.h"
64 #include "application/editor.h"
65 #include "inkscape.h"
66 #include "uri.h"
68 #ifdef WITH_GNOME_VFS
69 # include <libgnomevfs/gnome-vfs.h>
70 #endif
72 #ifdef WITH_INKBOARD
73 #include "jabber_whiteboard/session-manager.h"
74 #endif
77 //#define INK_DUMP_FILENAME_CONV 1
78 #undef INK_DUMP_FILENAME_CONV
80 //#define INK_DUMP_FOPEN 1
81 #undef INK_DUMP_FOPEN
83 void dump_str(gchar const *str, gchar const *prefix);
84 void dump_ustr(Glib::ustring const &ustr);
87 /*######################
88 ## N E W
89 ######################*/
91 /**
92 * Create a blank document and add it to the desktop
93 */
94 SPDesktop*
95 sp_file_new(const Glib::ustring &templ)
96 {
97 char *templName = NULL;
98 if (templ.size()>0)
99 templName = (char *)templ.c_str();
100 SPDocument *doc = sp_document_new(templName, TRUE, true);
101 g_return_val_if_fail(doc != NULL, NULL);
103 SPDesktop *dt;
104 if (Inkscape::NSApplication::Application::getNewGui())
105 {
106 dt = Inkscape::NSApplication::Editor::createDesktop (doc);
107 } else {
108 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
109 g_return_val_if_fail(dtw != NULL, NULL);
110 sp_document_unref(doc);
112 sp_create_window(dtw, TRUE);
113 dt = static_cast<SPDesktop*>(dtw->view);
114 sp_namedview_window_from_document(dt);
115 sp_namedview_update_layers_from_document(dt);
116 }
117 return dt;
118 }
120 SPDesktop*
121 sp_file_new_default()
122 {
123 std::list<gchar *> sources;
124 sources.push_back( profile_path("templates") ); // first try user's local dir
125 sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
127 while (!sources.empty()) {
128 gchar *dirname = sources.front();
129 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
131 // TRANSLATORS: default.svg is localizable - this is the name of the default document
132 // template. This way you can localize the default pagesize, translate the name of
133 // the default layer, etc. If you wish to localize this file, please create a
134 // localized share/templates/default.xx.svg file, where xx is your language code.
135 char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
136 if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
137 return sp_file_new(default_template);
138 }
139 }
140 g_free(dirname);
141 sources.pop_front();
142 }
144 return sp_file_new("");
145 }
148 /*######################
149 ## D E L E T E
150 ######################*/
152 /**
153 * Perform document closures preceding an exit()
154 */
155 void
156 sp_file_exit()
157 {
158 sp_ui_close_all();
159 // no need to call inkscape_exit here; last document being closed will take care of that
160 }
163 /*######################
164 ## O P E N
165 ######################*/
167 /**
168 * Open a file, add the document to the desktop
169 *
170 * \param replace_empty if true, and the current desktop is empty, this document
171 * will replace the empty one.
172 */
173 bool
174 sp_file_open(const Glib::ustring &uri,
175 Inkscape::Extension::Extension *key,
176 bool add_to_recent, bool replace_empty)
177 {
178 SPDocument *doc = NULL;
179 try {
180 doc = Inkscape::Extension::open(key, uri.c_str());
181 } catch (Inkscape::Extension::Input::no_extension_found &e) {
182 doc = NULL;
183 } catch (Inkscape::Extension::Input::open_failed &e) {
184 doc = NULL;
185 }
187 if (doc) {
188 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
189 SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
191 if (existing && existing->virgin && replace_empty) {
192 // If the current desktop is empty, open the document there
193 sp_document_ensure_up_to_date (doc);
194 desktop->change_document(doc);
195 sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
196 } else {
197 if (!Inkscape::NSApplication::Application::getNewGui()) {
198 // create a whole new desktop and window
199 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
200 sp_create_window(dtw, TRUE);
201 desktop = static_cast<SPDesktop*>(dtw->view);
202 } else {
203 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
204 }
205 }
207 doc->virgin = FALSE;
208 // everyone who cares now has a reference, get rid of ours
209 sp_document_unref(doc);
210 // resize the window to match the document properties
211 sp_namedview_window_from_document(desktop);
212 sp_namedview_update_layers_from_document(desktop);
214 if (add_to_recent) {
215 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
216 }
218 return TRUE;
219 } else {
220 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
221 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
222 sp_ui_error_dialog(text);
223 g_free(text);
224 g_free(safeUri);
225 return FALSE;
226 }
227 }
229 /**
230 * Handle prompting user for "do you want to revert"? Revert on "OK"
231 */
232 void
233 sp_file_revert_dialog()
234 {
235 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
236 g_assert(desktop != NULL);
238 SPDocument *doc = sp_desktop_document(desktop);
239 g_assert(doc != NULL);
241 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
242 g_assert(repr != NULL);
244 gchar const *uri = doc->uri;
245 if (!uri) {
246 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
247 return;
248 }
250 bool do_revert = true;
251 if (repr->attribute("sodipodi:modified") != NULL) {
252 gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
254 bool response = desktop->warnDialog (text);
255 g_free(text);
257 if (!response) {
258 do_revert = false;
259 }
260 }
262 bool reverted;
263 if (do_revert) {
264 // Allow overwriting of current document.
265 doc->virgin = TRUE;
266 reverted = sp_file_open(uri,NULL);
267 } else {
268 reverted = false;
269 }
271 if (reverted) {
272 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
273 } else {
274 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
275 }
276 }
278 void dump_str(gchar const *str, gchar const *prefix)
279 {
280 Glib::ustring tmp;
281 tmp = prefix;
282 tmp += " [";
283 size_t const total = strlen(str);
284 for (unsigned i = 0; i < total; i++) {
285 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
286 tmp += tmp2;
287 g_free(tmp2);
288 }
290 tmp += "]";
291 g_message("%s", tmp.c_str());
292 }
294 void dump_ustr(Glib::ustring const &ustr)
295 {
296 char const *cstr = ustr.c_str();
297 char const *data = ustr.data();
298 Glib::ustring::size_type const byteLen = ustr.bytes();
299 Glib::ustring::size_type const dataLen = ustr.length();
300 Glib::ustring::size_type const cstrLen = strlen(cstr);
302 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
303 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
304 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
305 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
307 try {
308 Glib::ustring tmp;
309 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
310 tmp = " ";
311 if (i < dataLen) {
312 Glib::ustring::value_type val = ustr.at(i);
313 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
314 tmp += tmp2;
315 g_free( tmp2 );
316 } else {
317 tmp += " ";
318 }
320 if (i < byteLen) {
321 int val = (0x0ff & data[i]);
322 gchar *tmp2 = g_strdup_printf(" %02x", val);
323 tmp += tmp2;
324 g_free( tmp2 );
325 if ( val > 32 && val < 127 ) {
326 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
327 tmp += tmp2;
328 g_free( tmp2 );
329 } else {
330 tmp += " . ";
331 }
332 } else {
333 tmp += " ";
334 }
336 if ( i < cstrLen ) {
337 int val = (0x0ff & cstr[i]);
338 gchar* tmp2 = g_strdup_printf(" %02x", val);
339 tmp += tmp2;
340 g_free(tmp2);
341 if ( val > 32 && val < 127 ) {
342 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
343 tmp += tmp2;
344 g_free( tmp2 );
345 } else {
346 tmp += " . ";
347 }
348 } else {
349 tmp += " ";
350 }
352 g_message( "%s", tmp.c_str() );
353 }
354 } catch (...) {
355 g_message("XXXXXXXXXXXXXXXXXX Exception" );
356 }
357 g_message("---------------");
358 }
360 static Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance = NULL;
362 /**
363 * Display an file Open selector. Open a document if OK is pressed.
364 * Can select single or multiple files for opening.
365 */
366 void
367 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer object, gpointer data)
368 {
370 //# Get the current directory for finding files
371 Glib::ustring open_path;
372 char *attr = (char *)prefs_get_string_attribute("dialogs.open", "path");
373 if (attr)
374 open_path = attr;
377 //# Test if the open_path directory exists
378 if (!Inkscape::IO::file_test(open_path.c_str(),
379 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
380 open_path = "";
382 //# If no open path, default to our home directory
383 if (open_path.size() < 1)
384 {
385 open_path = g_get_home_dir();
386 open_path.append(G_DIR_SEPARATOR_S);
387 }
389 //# Create a dialog if we don't already have one
390 if (!openDialogInstance) {
391 openDialogInstance =
392 Inkscape::UI::Dialog::FileOpenDialog::create(
393 parentWindow,
394 open_path,
395 Inkscape::UI::Dialog::SVG_TYPES,
396 (char const *)_("Select file to open"));
397 // allow easy access to our examples folder
398 if (Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
399 dynamic_cast<Gtk::FileChooser *>(openDialogInstance)->add_shortcut_folder(INKSCAPE_EXAMPLESDIR);
400 }
401 }
404 //# Show the dialog
405 bool const success = openDialogInstance->show();
406 if (!success)
407 return;
409 //# User selected something. Get name and type
410 Glib::ustring fileName = openDialogInstance->getFilename();
411 Inkscape::Extension::Extension *selection =
412 openDialogInstance->getSelectionType();
414 //# Code to check & open iff multiple files.
415 std::vector<Glib::ustring> flist=openDialogInstance->getFilenames();
417 //# Iterate through filenames if more than 1
418 if (flist.size() > 1)
419 {
420 for (unsigned int i=1 ; i<flist.size() ; i++)
421 {
422 Glib::ustring fName = flist[i];
424 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
425 Glib::ustring newFileName = Glib::filename_to_utf8(fName);
426 if ( newFileName.size() > 0 )
427 fName = newFileName;
428 else
429 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
431 #ifdef INK_DUMP_FILENAME_CONV
432 g_message("Opening File %s\n",fileName);
433 #endif
434 sp_file_open(fileName, selection);
435 }
436 }
437 return;
438 }
441 if (fileName.size() > 0) {
443 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
445 if ( newFileName.size() > 0)
446 fileName = newFileName;
447 else
448 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
450 open_path = fileName;
451 open_path.append(G_DIR_SEPARATOR_S);
452 prefs_set_string_attribute("dialogs.open", "path", open_path.c_str());
454 sp_file_open(fileName, selection);
455 }
457 return;
458 }
461 /*######################
462 ## V A C U U M
463 ######################*/
465 /**
466 * Remove unreferenced defs from the defs section of the document.
467 */
470 void
471 sp_file_vacuum()
472 {
473 SPDocument *doc = SP_ACTIVE_DOCUMENT;
475 unsigned int diff = vacuum_document (doc);
477 sp_document_done(doc, SP_VERB_FILE_VACUUM,
478 _("Vacuum <defs>"));
480 SPDesktop *dt = SP_ACTIVE_DESKTOP;
481 if (diff > 0) {
482 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
483 ngettext("Removed <b>%i</b> unused definition in <defs>.",
484 "Removed <b>%i</b> unused definitions in <defs>.",
485 diff),
486 diff);
487 } else {
488 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
489 }
490 }
494 /*######################
495 ## S A V E
496 ######################*/
498 /**
499 * This 'save' function called by the others below
500 * It was divided in file_save_local and file_save_remote
501 * to support remote saving too.
502 * Now file_save is calling only local saving, but it will be solved.
503 *
504 * \param official whether to set :output_module and :modified in the
505 * document; is true for normal save, false for temporary saves
506 */
507 static bool
508 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
509 Inkscape::Extension::Extension *key, bool saveas, bool official)
510 {
511 if (!doc || uri.size()<1) //Safety check
512 return false;
514 #ifdef WITH_GNOME_VFS
516 // if (gnome_vfs_initialized() && !gnome_vfs_uri_is_local(gnome_vfs_uri_new(uri.c_str()))) {
517 // // Use VFS for this
518 // bool success = file_save_remote(doc, uri, key, saveas, official);
519 // if (!success) {
520 // g_warning("Error: Could not save file '%s' with VFS\n", uri.c_str());
521 // return false;
522 // }
523 // else
524 // return true;
525 // }
526 // else
527 return file_save_local(parentWindow, doc, uri, key, saveas, official);
528 #else
530 return file_save_local(parentWindow, doc, uri, key, saveas, official);
532 #endif
533 }
535 bool
536 file_save_local(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
537 Inkscape::Extension::Extension *key, bool saveas, bool official)
538 {
539 try {
540 Inkscape::Extension::save(key, doc, uri.c_str(),
541 false,
542 saveas, official);
543 } catch (Inkscape::Extension::Output::no_extension_found &e) {
544 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
545 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
546 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
547 sp_ui_error_dialog(text);
548 g_free(text);
549 g_free(safeUri);
550 return FALSE;
551 } catch (Inkscape::Extension::Output::save_failed &e) {
552 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
553 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
554 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
555 sp_ui_error_dialog(text);
556 g_free(text);
557 g_free(safeUri);
558 return FALSE;
559 } catch (Inkscape::Extension::Output::no_overwrite &e) {
560 return sp_file_save_dialog(parentWindow, doc);
561 }
563 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
564 return true;
565 }
569 #ifdef WITH_GNOME_VFS
571 /*
572 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
573 */
574 bool
575 file_save_remote(SPDocument *doc, const Glib::ustring &uri,
576 Inkscape::Extension::Extension *key, bool saveas, bool official)
577 {
579 #define BUF_SIZE 8192
580 gnome_vfs_init();
582 GnomeVFSHandle *from_handle = NULL;
583 GnomeVFSHandle *to_handle = NULL;
584 GnomeVFSFileSize bytes_read;
585 GnomeVFSFileSize bytes_written;
586 GnomeVFSResult result;
587 guint8 buffer[8192];
589 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
591 if ( uri_local == NULL ) {
592 g_warning( "Error converting filename to locale encoding.");
593 }
595 // Gets the temp file name.
596 Glib::ustring fileName = Glib::get_tmp_dir ();
597 fileName.append(G_DIR_SEPARATOR_S);
598 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
600 // Open the temp file to send.
601 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
603 if (result != GNOME_VFS_OK) {
604 g_warning("Could not find the temp saving.");
605 return false;
606 }
609 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
612 if (result == GNOME_VFS_ERROR_NOT_FOUND){
613 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
614 }
616 if (result != GNOME_VFS_OK) {
617 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
618 return false;
619 }
621 ///g_warning("file_save_remote: temp dir: %s",fileName.c_str());
623 while (1) {
625 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
626 //g_warning("bytes lidos: %d",bytes_read);
628 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
629 result = gnome_vfs_close (from_handle);
630 result = gnome_vfs_close (to_handle);
631 return true;
632 }
634 //g_warning("while: %s", gnome_vfs_result_to_string(result));
636 if (result != GNOME_VFS_OK) {
637 g_warning("%s", gnome_vfs_result_to_string(result));
638 return false;
639 }
640 //g_warning("%s",buffer);
641 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
642 //g_warning("escritos: %d",bytes_written);
643 if (result != GNOME_VFS_OK) {
644 g_warning("%s", gnome_vfs_result_to_string(result));
645 return false;
646 }
649 if (bytes_read != bytes_written){
650 return false;
651 }
653 }
654 return true;
655 }
656 #endif
659 /**
660 * Display a SaveAs dialog. Save the document if OK pressed.
661 *
662 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
663 */
664 bool
665 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
666 {
668 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
670 Inkscape::Extension::Output *extension = 0;
672 //# Get the default extension name
673 Glib::ustring default_extension;
674 char *attr = (char *)repr->attribute("inkscape:output_extension");
675 if (!attr)
676 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
677 if (attr)
678 default_extension = attr;
679 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
681 Glib::ustring save_path;
682 Glib::ustring save_loc;
684 if (doc->uri == NULL) {
685 char formatBuf[256];
686 int i = 1;
688 Glib::ustring filename_extension = ".svg";
689 extension = dynamic_cast<Inkscape::Extension::Output *>
690 (Inkscape::Extension::db.get(default_extension.c_str()));
691 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
692 if (extension)
693 filename_extension = extension->get_extension();
695 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
696 if (attr)
697 save_path = attr;
699 if (!Inkscape::IO::file_test(save_path.c_str(),
700 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
701 save_path = "";
703 if (save_path.size()<1)
704 save_path = g_get_home_dir();
706 save_loc = save_path;
707 save_loc.append(G_DIR_SEPARATOR_S);
708 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
709 save_loc.append(formatBuf);
711 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
712 save_loc = save_path;
713 save_loc.append(G_DIR_SEPARATOR_S);
714 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
715 save_loc.append(formatBuf);
716 }
717 } else {
718 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
719 Glib::path_get_basename(doc->uri));
720 }
722 // convert save_loc from utf-8 to locale
723 // is this needed any more, now that everything is handled in
724 // Inkscape::IO?
725 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
727 if ( save_loc_local.size() > 0)
728 save_loc = save_loc_local;
730 //# Show the SaveAs dialog
731 char const * dialog_title;
732 if (is_copy) {
733 dialog_title = (char const *) _("Select file to save a copy to");
734 } else {
735 dialog_title = (char const *) _("Select file to save to");
736 }
737 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
738 Inkscape::UI::Dialog::FileSaveDialog::create(
739 parentWindow,
740 save_loc,
741 Inkscape::UI::Dialog::SVG_TYPES,
742 (char const *) _("Select file to save to"),
743 default_extension
744 );
746 saveDialog->change_title(dialog_title);
747 saveDialog->setSelectionType(extension);
749 // allow easy access to the user's own templates folder
750 gchar *templates = profile_path ("templates");
751 if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
752 dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
753 }
754 g_free (templates);
756 bool success = saveDialog->show();
757 if (!success) {
758 delete saveDialog;
759 return success;
760 }
762 Glib::ustring fileName = saveDialog->getFilename();
763 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
765 delete saveDialog;
767 saveDialog = 0;
769 if (fileName.size() > 0) {
770 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
772 if ( newFileName.size()>0 )
773 fileName = newFileName;
774 else
775 g_warning( "Error converting save filename to UTF-8." );
777 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
779 if (success)
780 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
782 save_path = Glib::path_get_dirname(fileName);
783 prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
785 return success;
786 }
789 return false;
790 }
793 /**
794 * Save a document, displaying a SaveAs dialog if necessary.
795 */
796 bool
797 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
798 {
799 bool success = true;
801 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
803 gchar const *fn = repr->attribute("sodipodi:modified");
804 if (fn != NULL) {
805 if ( doc->uri == NULL
806 || repr->attribute("inkscape:output_extension") == NULL )
807 {
808 return sp_file_save_dialog(parentWindow, doc, FALSE);
809 } else {
810 fn = g_strdup(doc->uri);
811 gchar const *ext = repr->attribute("inkscape:output_extension");
812 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
813 g_free((void *) fn);
814 }
815 } else {
816 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
817 success = TRUE;
818 }
820 return success;
821 }
824 /**
825 * Save a document.
826 */
827 bool
828 sp_file_save(Gtk::Window &parentWindow, gpointer object, gpointer data)
829 {
830 if (!SP_ACTIVE_DOCUMENT)
831 return false;
833 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
835 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
836 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
837 }
840 /**
841 * Save a document, always displaying the SaveAs dialog.
842 */
843 bool
844 sp_file_save_as(Gtk::Window &parentWindow, gpointer object, gpointer data)
845 {
846 if (!SP_ACTIVE_DOCUMENT)
847 return false;
848 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
849 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
850 }
854 /**
855 * Save a copy of a document, always displaying a sort of SaveAs dialog.
856 */
857 bool
858 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer object, gpointer data)
859 {
860 if (!SP_ACTIVE_DOCUMENT)
861 return false;
862 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
863 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
864 }
867 /*######################
868 ## I M P O R T
869 ######################*/
871 /**
872 * Import a resource. Called by sp_file_import()
873 */
874 void
875 file_import(SPDocument *in_doc, const Glib::ustring &uri,
876 Inkscape::Extension::Extension *key)
877 {
878 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
880 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
881 SPDocument *doc;
882 try {
883 doc = Inkscape::Extension::open(key, uri.c_str());
884 } catch (Inkscape::Extension::Input::no_extension_found &e) {
885 doc = NULL;
886 } catch (Inkscape::Extension::Input::open_failed &e) {
887 doc = NULL;
888 }
890 if (doc != NULL) {
891 // move imported defs to our document's defs
892 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
893 SPObject *defs = SP_DOCUMENT_DEFS(doc);
895 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
896 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
898 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
899 for (SPObject *child = sp_object_first_child(defs);
900 child != NULL; child = SP_OBJECT_NEXT(child))
901 {
902 // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
903 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
904 }
906 guint items_count = 0;
907 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
908 child != NULL; child = SP_OBJECT_NEXT(child)) {
909 if (SP_IS_ITEM(child))
910 items_count ++;
911 }
912 SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
914 SPObject *new_obj = NULL;
916 if ((style && style->firstChild()) || items_count > 1) {
917 // create group
918 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
919 Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
920 sp_repr_css_set (newgroup, style, "style");
922 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
923 if (SP_IS_ITEM(child)) {
924 Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
926 // convert layers to groups; FIXME: add "preserve layers" mode where each layer
927 // from impot is copied to the same-named layer in host
928 newchild->setAttribute("inkscape:groupmode", NULL);
930 newgroup->appendChild(newchild);
931 }
932 }
934 if (desktop) {
935 // Add it to the current layer
936 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
937 } else {
938 // There's no desktop (command line run?)
939 // FIXME: For such cases we need a document:: method to return the current layer
940 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
941 }
943 Inkscape::GC::release(newgroup);
944 } else {
945 // just add one item
946 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
947 if (SP_IS_ITEM(child)) {
948 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
949 newitem->setAttribute("inkscape:groupmode", NULL);
951 if (desktop) {
952 // Add it to the current layer
953 new_obj = desktop->currentLayer()->appendChildRepr(newitem);
954 } else {
955 // There's no desktop (command line run?)
956 // FIXME: For such cases we need a document:: method to return the current layer
957 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
958 }
960 }
961 }
962 }
964 if (style) sp_repr_css_attr_unref (style);
966 // select and move the imported item
967 if (new_obj && SP_IS_ITEM(new_obj)) {
968 Inkscape::Selection *selection = sp_desktop_selection(desktop);
969 selection->set(SP_ITEM(new_obj));
971 // To move the imported object, we must temporarily set the "transform pattern with
972 // object" option.
973 {
974 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
975 prefs_set_int_attribute("options.transform", "pattern", 1);
976 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
977 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
978 if (sel_bbox) {
979 NR::Point m( desktop->point() - sel_bbox->midpoint() );
980 sp_selection_move_relative(selection, m);
981 }
982 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
983 }
984 }
986 sp_document_unref(doc);
987 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
988 _("Import"));
990 } else {
991 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
992 sp_ui_error_dialog(text);
993 g_free(text);
994 }
996 return;
997 }
1000 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
1002 /**
1003 * Display an Open dialog, import a resource if OK pressed.
1004 */
1005 void
1006 sp_file_import(Gtk::Window &parentWindow)
1007 {
1008 static Glib::ustring import_path;
1010 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1011 if (!doc)
1012 return;
1014 if (!importDialogInstance) {
1015 importDialogInstance =
1016 Inkscape::UI::Dialog::FileOpenDialog::create(
1017 parentWindow,
1018 import_path,
1019 Inkscape::UI::Dialog::IMPORT_TYPES,
1020 (char const *)_("Select file to import"));
1021 }
1023 bool success = importDialogInstance->show();
1024 if (!success)
1025 return;
1027 //# Get file name and extension type
1028 Glib::ustring fileName = importDialogInstance->getFilename();
1029 Inkscape::Extension::Extension *selection =
1030 importDialogInstance->getSelectionType();
1033 if (fileName.size() > 0) {
1035 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1037 if ( newFileName.size() > 0)
1038 fileName = newFileName;
1039 else
1040 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1042 import_path = fileName;
1043 if (import_path.size()>0)
1044 import_path.append(G_DIR_SEPARATOR_S);
1046 file_import(doc, fileName, selection);
1047 }
1049 return;
1050 }
1054 /*######################
1055 ## E X P O R T
1056 ######################*/
1058 //#define NEW_EXPORT_DIALOG
1062 #ifdef NEW_EXPORT_DIALOG
1064 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1066 /**
1067 * Display an Export dialog, export as the selected type if OK pressed
1068 */
1069 bool
1070 sp_file_export_dialog(void *widget)
1071 {
1072 //# temp hack for 'doc' until we can switch to this dialog
1073 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1075 Glib::ustring export_path;
1076 Glib::ustring export_loc;
1078 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1080 Inkscape::Extension::Output *extension;
1082 //# Get the default extension name
1083 Glib::ustring default_extension;
1084 char *attr = (char *)repr->attribute("inkscape:output_extension");
1085 if (!attr)
1086 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1087 if (attr)
1088 default_extension = attr;
1089 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1091 if (doc->uri == NULL)
1092 {
1093 char formatBuf[256];
1095 Glib::ustring filename_extension = ".svg";
1096 extension = dynamic_cast<Inkscape::Extension::Output *>
1097 (Inkscape::Extension::db.get(default_extension.c_str()));
1098 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1099 if (extension)
1100 filename_extension = extension->get_extension();
1102 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1103 if (attr)
1104 export_path = attr;
1106 if (!Inkscape::IO::file_test(export_path.c_str(),
1107 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1108 export_path = "";
1110 if (export_path.size()<1)
1111 export_path = g_get_home_dir();
1113 export_loc = export_path;
1114 export_loc.append(G_DIR_SEPARATOR_S);
1115 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1116 export_loc.append(formatBuf);
1118 }
1119 else
1120 {
1121 export_path = Glib::path_get_dirname(doc->uri);
1122 }
1124 // convert save_loc from utf-8 to locale
1125 // is this needed any more, now that everything is handled in
1126 // Inkscape::IO?
1127 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1128 if ( export_path_local.size() > 0)
1129 export_path = export_path_local;
1131 //# Show the SaveAs dialog
1132 if (!exportDialogInstance)
1133 exportDialogInstance =
1134 Inkscape::UI::Dialog::FileExportDialog::create(
1135 export_path,
1136 Inkscape::UI::Dialog::EXPORT_TYPES,
1137 (char const *) _("Select file to export to"),
1138 default_extension
1139 );
1141 bool success = exportDialogInstance->show();
1142 if (!success)
1143 return success;
1145 Glib::ustring fileName = exportDialogInstance->getFilename();
1147 Inkscape::Extension::Extension *selectionType =
1148 exportDialogInstance->getSelectionType();
1151 if (fileName.size() > 0) {
1152 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1154 if ( newFileName.size()>0 )
1155 fileName = newFileName;
1156 else
1157 g_warning( "Error converting save filename to UTF-8." );
1159 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1161 if (success)
1162 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1164 export_path = fileName;
1165 prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1167 return success;
1168 }
1171 return false;
1172 }
1180 #else
1184 /**
1185 *
1186 */
1187 bool
1188 sp_file_export_dialog(void *widget)
1189 {
1190 sp_export_dialog();
1191 return true;
1192 }
1194 #endif
1196 /*######################
1197 ## E X P O R T T O O C A L
1198 ######################*/
1201 /**
1202 * Export the current document to OCAL
1203 */
1206 /**
1207 * Display an Export dialog, export as the selected type if OK pressed
1208 */
1209 bool
1210 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1211 {
1212 //# temp hack for 'doc' until we can switch to this dialog
1214 if (!SP_ACTIVE_DOCUMENT)
1215 return false;
1217 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1219 Glib::ustring export_path;
1220 Glib::ustring export_loc;
1221 Glib::ustring fileName;
1222 Inkscape::Extension::Extension *selectionType;
1224 bool success = false;
1226 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1228 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1229 // Verify whether the document is saved, so save this as temporary
1231 char *str = (char *) repr->attribute("sodipodi:modified");
1232 if ((!doc->uri) && (!str))
1233 return false;
1236 Inkscape::Extension::Output *extension;
1238 //# Get the default extension name
1239 Glib::ustring default_extension;
1240 char *attr = (char *)repr->attribute("inkscape:output_extension");
1241 if (!attr)
1242 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1243 if (attr)
1244 default_extension = attr;
1246 char formatBuf[256];
1248 Glib::ustring filename_extension = ".svg";
1249 extension = dynamic_cast<Inkscape::Extension::Output *>
1250 (Inkscape::Extension::db.get(default_extension.c_str()));
1251 if (extension)
1252 filename_extension = extension->get_extension();
1254 export_path = Glib::get_tmp_dir ();
1256 export_loc = export_path;
1257 export_loc.append(G_DIR_SEPARATOR_S);
1258 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1259 export_loc.append(formatBuf);
1261 // convert save_loc from utf-8 to locale
1262 // is this needed any more, now that everything is handled in
1263 // Inkscape::IO?
1264 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1265 if ( export_path_local.size() > 0)
1266 export_path = export_path_local;
1268 //# Show the Export To OCAL dialog
1269 // MADE A CHANGE TO SUBMIT CHANGES. IN THE FUTURE WILL CALL FileExportToOCALDialog
1270 if (!exportDialogInstance)
1271 exportDialogInstance = Inkscape::UI::Dialog::FileExportDialog::create(
1272 parentWindow,
1273 export_path,
1274 Inkscape::UI::Dialog::EXPORT_TYPES,
1275 (char const *) _("Select file to export to"),
1276 default_extension
1277 );
1279 success = exportDialogInstance->show();
1280 if (!success)
1281 return success;
1283 fileName = exportDialogInstance->getFilename();
1285 selectionType = exportDialogInstance->getSelectionType();
1287 if (fileName.size() > 0) {
1288 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1290 if ( newFileName.size()>0 )
1291 fileName = newFileName;
1292 else
1293 g_warning( "Error converting save filename to UTF-8." );
1294 }
1295 Glib::ustring filePath = export_path;
1296 filePath.append(G_DIR_SEPARATOR_S);
1297 filePath.append(Glib::path_get_basename(fileName));
1299 fileName = filePath;
1301 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1303 if (!success){
1304 g_warning( "Error saving a temporary copy." );
1305 return success;
1306 }
1308 /* Start now the submition */
1310 // Create the uri
1311 Glib::ustring uri = "dav://";
1312 char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1313 char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1314 if ((username != NULL) && (password != NULL)){
1315 uri.append(username);
1316 uri.append(":");
1317 uri.append(password);
1318 uri.append("@");
1319 }
1320 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1321 uri.append(Glib::path_get_basename(fileName));
1322 // Save as a remote file using the dav protocol.
1323 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1324 //remove(fileName.c_str());
1325 if (!success)
1326 g_warning( "Error exporting the document." );
1328 return success;
1329 }
1332 void
1333 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1334 {
1336 // Try to execute the new code and return;
1337 if (!SP_ACTIVE_DOCUMENT)
1338 return;
1339 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1340 if (success)
1341 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1342 }
1345 /*######################
1346 ## I M P O R T F R O M O C A L
1347 ######################*/
1349 /**
1350 * Import a document from OCAL
1351 */
1352 void
1353 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1354 {
1355 // Try to execute the new code and return;
1356 if (!SP_ACTIVE_DOCUMENT)
1357 return;
1358 //bool success = sp_file_import_from_ocal_dialog(parentWindow);
1359 //if (success)
1360 // SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document imported..."));
1361 }
1364 /*######################
1365 ## P R I N T
1366 ######################*/
1369 /**
1370 * Print the current document, if any.
1371 */
1372 void
1373 sp_file_print()
1374 {
1375 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1376 if (doc)
1377 sp_print_document(doc, FALSE);
1378 }
1381 /**
1382 * Print the current document, if any. Do not use
1383 * the machine's print drivers.
1384 */
1385 void
1386 sp_file_print_direct()
1387 {
1388 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1389 if (doc)
1390 sp_print_document(doc, TRUE);
1391 }
1394 /**
1395 * Display what the drawing would look like, if
1396 * printed.
1397 */
1398 void
1399 sp_file_print_preview(gpointer object, gpointer data)
1400 {
1402 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1403 if (doc)
1404 sp_print_preview_document(doc);
1406 }
1408 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1409 {
1410 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1412 if ( 0 ) {
1413 gchar const* things[] = {
1414 "data:foo,bar",
1415 "http://www.google.com/image.png",
1416 "ftp://ssd.com/doo",
1417 "/foo/dee/bar.svg",
1418 "foo.svg",
1419 "file:/foo/dee/bar.svg",
1420 "file:///foo/dee/bar.svg",
1421 "file:foo.svg",
1422 "/foo/bar\xe1\x84\x92.svg",
1423 "file:///foo/bar\xe1\x84\x92.svg",
1424 "file:///foo/bar%e1%84%92.svg",
1425 "/foo/bar%e1%84%92.svg",
1426 "bar\xe1\x84\x92.svg",
1427 "bar%e1%84%92.svg",
1428 NULL
1429 };
1430 g_message("+------");
1431 for ( int i = 0; things[i]; i++ )
1432 {
1433 try
1434 {
1435 URI uri(things[i]);
1436 gboolean isAbs = g_path_is_absolute( things[i] );
1437 gchar *str = uri.toString();
1438 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1439 (int)uri.isRelative(),
1440 uri.getScheme(),
1441 uri.getPath(),
1442 uri.getOpaque(),
1443 things[i],
1444 str );
1445 g_free(str);
1446 }
1447 catch ( MalformedURIException err )
1448 {
1449 dump_str( things[i], "MalformedURIException" );
1450 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1451 g_message(" gone from [%s] to [%s]", things[i], redo );
1452 if ( redo == NULL )
1453 {
1454 URI again = URI::fromUtf8( things[i] );
1455 gboolean isAbs = g_path_is_absolute( things[i] );
1456 gchar *str = again.toString();
1457 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1458 (int)again.isRelative(),
1459 again.getScheme(),
1460 again.getPath(),
1461 again.getOpaque(),
1462 things[i],
1463 str );
1464 g_free(str);
1465 g_message(" ----");
1466 }
1467 }
1468 }
1469 g_message("+------");
1470 }
1472 GSList const *images = sp_document_get_resource_list(doc, "image");
1473 for (GSList const *l = images; l != NULL; l = l->next) {
1474 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1476 const gchar *href = ir->attribute("xlink:href");
1478 // First try to figure out an absolute path to the asset
1479 //g_message("image href [%s]", href );
1480 if (spns && !g_path_is_absolute(href)) {
1481 const gchar *absref = ir->attribute("sodipodi:absref");
1482 const gchar *base_href = g_build_filename(base, href, NULL);
1483 //g_message(" absr [%s]", absref );
1485 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1486 {
1487 // only switch over if the absref is valid while href is not
1488 href = absref;
1489 //g_message(" copied absref to href");
1490 }
1491 }
1493 // Once we have an absolute path, convert it relative to the new location
1494 if (href && g_path_is_absolute(href)) {
1495 const gchar *relname = sp_relative_path_from_path(href, base);
1496 //g_message(" setting to [%s]", relname );
1497 ir->setAttribute("xlink:href", relname);
1498 }
1499 // TODO next refinement is to make the first choice keeping the relative path as-is if
1500 // based on the new location it gives us a valid file.
1501 }
1502 }
1505 /*
1506 Local Variables:
1507 mode:c++
1508 c-file-style:"stroustrup"
1509 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1510 indent-tabs-mode:nil
1511 fill-column:99
1512 End:
1513 */
1514 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :