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;
268 // remember current zoom and view
269 double zoom = desktop->current_zoom();
270 NR::Point c = desktop->get_display_area().midpoint();
272 reverted = sp_file_open(uri,NULL);
273 if (reverted) {
274 // restore zoom and view
275 desktop->zoom_absolute(c[NR::X], c[NR::Y], zoom);
276 }
277 } else {
278 reverted = false;
279 }
281 if (reverted) {
282 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
283 } else {
284 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
285 }
286 }
288 void dump_str(gchar const *str, gchar const *prefix)
289 {
290 Glib::ustring tmp;
291 tmp = prefix;
292 tmp += " [";
293 size_t const total = strlen(str);
294 for (unsigned i = 0; i < total; i++) {
295 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
296 tmp += tmp2;
297 g_free(tmp2);
298 }
300 tmp += "]";
301 g_message("%s", tmp.c_str());
302 }
304 void dump_ustr(Glib::ustring const &ustr)
305 {
306 char const *cstr = ustr.c_str();
307 char const *data = ustr.data();
308 Glib::ustring::size_type const byteLen = ustr.bytes();
309 Glib::ustring::size_type const dataLen = ustr.length();
310 Glib::ustring::size_type const cstrLen = strlen(cstr);
312 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
313 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
314 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
315 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
317 try {
318 Glib::ustring tmp;
319 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
320 tmp = " ";
321 if (i < dataLen) {
322 Glib::ustring::value_type val = ustr.at(i);
323 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
324 tmp += tmp2;
325 g_free( tmp2 );
326 } else {
327 tmp += " ";
328 }
330 if (i < byteLen) {
331 int val = (0x0ff & data[i]);
332 gchar *tmp2 = g_strdup_printf(" %02x", val);
333 tmp += tmp2;
334 g_free( tmp2 );
335 if ( val > 32 && val < 127 ) {
336 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
337 tmp += tmp2;
338 g_free( tmp2 );
339 } else {
340 tmp += " . ";
341 }
342 } else {
343 tmp += " ";
344 }
346 if ( i < cstrLen ) {
347 int val = (0x0ff & cstr[i]);
348 gchar* tmp2 = g_strdup_printf(" %02x", val);
349 tmp += tmp2;
350 g_free(tmp2);
351 if ( val > 32 && val < 127 ) {
352 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
353 tmp += tmp2;
354 g_free( tmp2 );
355 } else {
356 tmp += " . ";
357 }
358 } else {
359 tmp += " ";
360 }
362 g_message( "%s", tmp.c_str() );
363 }
364 } catch (...) {
365 g_message("XXXXXXXXXXXXXXXXXX Exception" );
366 }
367 g_message("---------------");
368 }
370 static Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance = NULL;
372 /**
373 * Display an file Open selector. Open a document if OK is pressed.
374 * Can select single or multiple files for opening.
375 */
376 void
377 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
378 {
380 //# Get the current directory for finding files
381 Glib::ustring open_path;
382 char *attr = (char *)prefs_get_string_attribute("dialogs.open", "path");
383 if (attr)
384 open_path = attr;
387 //# Test if the open_path directory exists
388 if (!Inkscape::IO::file_test(open_path.c_str(),
389 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
390 open_path = "";
392 //# If no open path, default to our home directory
393 if (open_path.size() < 1)
394 {
395 open_path = g_get_home_dir();
396 open_path.append(G_DIR_SEPARATOR_S);
397 }
399 //# Create a dialog if we don't already have one
400 if (!openDialogInstance) {
401 openDialogInstance =
402 Inkscape::UI::Dialog::FileOpenDialog::create(
403 parentWindow,
404 open_path,
405 Inkscape::UI::Dialog::SVG_TYPES,
406 (char const *)_("Select file to open"));
407 }
410 //# Show the dialog
411 bool const success = openDialogInstance->show();
412 if (!success)
413 return;
415 //# User selected something. Get name and type
416 Glib::ustring fileName = openDialogInstance->getFilename();
417 Inkscape::Extension::Extension *selection =
418 openDialogInstance->getSelectionType();
420 //# Code to check & open iff multiple files.
421 std::vector<Glib::ustring> flist=openDialogInstance->getFilenames();
423 //# Iterate through filenames if more than 1
424 if (flist.size() > 1)
425 {
426 for (unsigned int i=1 ; i<flist.size() ; i++)
427 {
428 Glib::ustring fName = flist[i];
430 if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
431 Glib::ustring newFileName = Glib::filename_to_utf8(fName);
432 if ( newFileName.size() > 0 )
433 fName = newFileName;
434 else
435 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
437 #ifdef INK_DUMP_FILENAME_CONV
438 g_message("Opening File %s\n",fileName);
439 #endif
440 sp_file_open(fileName, selection);
441 }
442 }
443 return;
444 }
447 if (fileName.size() > 0) {
449 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
451 if ( newFileName.size() > 0)
452 fileName = newFileName;
453 else
454 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
456 open_path = fileName;
457 open_path.append(G_DIR_SEPARATOR_S);
458 prefs_set_string_attribute("dialogs.open", "path", open_path.c_str());
460 sp_file_open(fileName, selection);
461 }
463 return;
464 }
467 /*######################
468 ## V A C U U M
469 ######################*/
471 /**
472 * Remove unreferenced defs from the defs section of the document.
473 */
476 void
477 sp_file_vacuum()
478 {
479 SPDocument *doc = SP_ACTIVE_DOCUMENT;
481 unsigned int diff = vacuum_document (doc);
483 sp_document_done(doc, SP_VERB_FILE_VACUUM,
484 _("Vacuum <defs>"));
486 SPDesktop *dt = SP_ACTIVE_DESKTOP;
487 if (diff > 0) {
488 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
489 ngettext("Removed <b>%i</b> unused definition in <defs>.",
490 "Removed <b>%i</b> unused definitions in <defs>.",
491 diff),
492 diff);
493 } else {
494 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
495 }
496 }
500 /*######################
501 ## S A V E
502 ######################*/
504 /**
505 * This 'save' function called by the others below
506 *
507 * \param official whether to set :output_module and :modified in the
508 * document; is true for normal save, false for temporary saves
509 */
510 static bool
511 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
512 Inkscape::Extension::Extension *key, bool saveas, bool official)
513 {
514 if (!doc || uri.size()<1) //Safety check
515 return false;
517 try {
518 Inkscape::Extension::save(key, doc, uri.c_str(),
519 false,
520 saveas, official);
521 } catch (Inkscape::Extension::Output::no_extension_found &e) {
522 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
523 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
524 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
525 sp_ui_error_dialog(text);
526 g_free(text);
527 g_free(safeUri);
528 return FALSE;
529 } catch (Inkscape::Extension::Output::save_failed &e) {
530 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
531 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
532 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
533 sp_ui_error_dialog(text);
534 g_free(text);
535 g_free(safeUri);
536 return FALSE;
537 } catch (Inkscape::Extension::Output::no_overwrite &e) {
538 return sp_file_save_dialog(parentWindow, doc);
539 }
541 SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
542 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
543 return true;
544 }
546 /*
547 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
548 */
549 bool
550 file_save_remote(SPDocument */*doc*/,
551 #ifdef WITH_GNOME_VFS
552 const Glib::ustring &uri,
553 #else
554 const Glib::ustring &/*uri*/,
555 #endif
556 Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
557 {
558 #ifdef WITH_GNOME_VFS
560 #define BUF_SIZE 8192
561 gnome_vfs_init();
563 GnomeVFSHandle *from_handle = NULL;
564 GnomeVFSHandle *to_handle = NULL;
565 GnomeVFSFileSize bytes_read;
566 GnomeVFSFileSize bytes_written;
567 GnomeVFSResult result;
568 guint8 buffer[8192];
570 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
572 if ( uri_local == NULL ) {
573 g_warning( "Error converting filename to locale encoding.");
574 }
576 // Gets the temp file name.
577 Glib::ustring fileName = Glib::get_tmp_dir ();
578 fileName.append(G_DIR_SEPARATOR_S);
579 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
581 // Open the temp file to send.
582 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
584 if (result != GNOME_VFS_OK) {
585 g_warning("Could not find the temp saving.");
586 return false;
587 }
589 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
590 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
592 if (result != GNOME_VFS_OK) {
593 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
594 return false;
595 }
597 while (1) {
599 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
601 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
602 result = gnome_vfs_close (from_handle);
603 result = gnome_vfs_close (to_handle);
604 return true;
605 }
607 if (result != GNOME_VFS_OK) {
608 g_warning("%s", gnome_vfs_result_to_string(result));
609 return false;
610 }
611 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
612 if (result != GNOME_VFS_OK) {
613 g_warning("%s", gnome_vfs_result_to_string(result));
614 return false;
615 }
618 if (bytes_read != bytes_written){
619 return false;
620 }
622 }
623 return true;
624 #else
625 // in case we do not have GNOME_VFS
626 return false;
627 #endif
629 }
632 /**
633 * Display a SaveAs dialog. Save the document if OK pressed.
634 *
635 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
636 */
637 bool
638 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
639 {
641 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
643 Inkscape::Extension::Output *extension = 0;
645 //# Get the default extension name
646 Glib::ustring default_extension;
647 char *attr = (char *)repr->attribute("inkscape:output_extension");
648 if (!attr)
649 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
650 if (attr)
651 default_extension = attr;
652 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
654 Glib::ustring save_path;
655 Glib::ustring save_loc;
657 if (doc->uri == NULL) {
658 char formatBuf[256];
659 int i = 1;
661 Glib::ustring filename_extension = ".svg";
662 extension = dynamic_cast<Inkscape::Extension::Output *>
663 (Inkscape::Extension::db.get(default_extension.c_str()));
664 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
665 if (extension)
666 filename_extension = extension->get_extension();
668 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
669 if (attr)
670 save_path = attr;
672 if (!Inkscape::IO::file_test(save_path.c_str(),
673 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
674 save_path = "";
676 if (save_path.size()<1)
677 save_path = g_get_home_dir();
679 save_loc = save_path;
680 save_loc.append(G_DIR_SEPARATOR_S);
681 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
682 save_loc.append(formatBuf);
684 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
685 save_loc = save_path;
686 save_loc.append(G_DIR_SEPARATOR_S);
687 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
688 save_loc.append(formatBuf);
689 }
690 } else {
691 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
692 Glib::path_get_basename(doc->uri));
693 }
695 // convert save_loc from utf-8 to locale
696 // is this needed any more, now that everything is handled in
697 // Inkscape::IO?
698 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
700 if ( save_loc_local.size() > 0)
701 save_loc = save_loc_local;
703 //# Show the SaveAs dialog
704 char const * dialog_title;
705 if (is_copy) {
706 dialog_title = (char const *) _("Select file to save a copy to");
707 } else {
708 dialog_title = (char const *) _("Select file to save to");
709 }
710 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
711 Inkscape::UI::Dialog::FileSaveDialog::create(
712 parentWindow,
713 save_loc,
714 Inkscape::UI::Dialog::SVG_TYPES,
715 (char const *) _("Select file to save to"),
716 default_extension
717 );
719 saveDialog->change_title(dialog_title);
720 saveDialog->setSelectionType(extension);
722 // allow easy access to the user's own templates folder
723 gchar *templates = profile_path ("templates");
724 if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
725 dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
726 }
727 g_free (templates);
729 bool success = saveDialog->show();
730 if (!success) {
731 delete saveDialog;
732 return success;
733 }
735 Glib::ustring fileName = saveDialog->getFilename();
736 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
738 delete saveDialog;
740 saveDialog = 0;
742 if (fileName.size() > 0) {
743 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
745 if ( newFileName.size()>0 )
746 fileName = newFileName;
747 else
748 g_warning( "Error converting save filename to UTF-8." );
750 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
752 if (success)
753 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
755 save_path = Glib::path_get_dirname(fileName);
756 prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
758 return success;
759 }
762 return false;
763 }
766 /**
767 * Save a document, displaying a SaveAs dialog if necessary.
768 */
769 bool
770 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
771 {
772 bool success = true;
774 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
776 gchar const *fn = repr->attribute("sodipodi:modified");
777 if (fn != NULL) {
778 if ( doc->uri == NULL
779 || repr->attribute("inkscape:output_extension") == NULL )
780 {
781 return sp_file_save_dialog(parentWindow, doc, FALSE);
782 } else {
783 fn = g_strdup(doc->uri);
784 gchar const *ext = repr->attribute("inkscape:output_extension");
785 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
786 g_free((void *) fn);
787 }
788 } else {
789 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
790 success = TRUE;
791 }
793 return success;
794 }
797 /**
798 * Save a document.
799 */
800 bool
801 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
802 {
803 if (!SP_ACTIVE_DOCUMENT)
804 return false;
806 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
808 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
809 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
810 }
813 /**
814 * Save a document, always displaying the SaveAs dialog.
815 */
816 bool
817 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
818 {
819 if (!SP_ACTIVE_DOCUMENT)
820 return false;
821 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
822 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
823 }
827 /**
828 * Save a copy of a document, always displaying a sort of SaveAs dialog.
829 */
830 bool
831 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
832 {
833 if (!SP_ACTIVE_DOCUMENT)
834 return false;
835 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
836 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
837 }
840 /*######################
841 ## I M P O R T
842 ######################*/
844 /**
845 * Import a resource. Called by sp_file_import()
846 */
847 void
848 file_import(SPDocument *in_doc, const Glib::ustring &uri,
849 Inkscape::Extension::Extension *key)
850 {
851 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
853 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
854 SPDocument *doc;
855 try {
856 doc = Inkscape::Extension::open(key, uri.c_str());
857 } catch (Inkscape::Extension::Input::no_extension_found &e) {
858 doc = NULL;
859 } catch (Inkscape::Extension::Input::open_failed &e) {
860 doc = NULL;
861 }
863 if (doc != NULL) {
864 // move imported defs to our document's defs
865 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
866 SPObject *defs = SP_DOCUMENT_DEFS(doc);
868 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
869 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
871 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
872 for (SPObject *child = sp_object_first_child(defs);
873 child != NULL; child = SP_OBJECT_NEXT(child))
874 {
875 // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
876 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
877 }
879 guint items_count = 0;
880 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
881 child != NULL; child = SP_OBJECT_NEXT(child)) {
882 if (SP_IS_ITEM(child))
883 items_count ++;
884 }
885 SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
887 SPObject *new_obj = NULL;
889 if ((style && style->firstChild()) || items_count > 1) {
890 // create group
891 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
892 Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
893 sp_repr_css_set (newgroup, style, "style");
895 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
896 if (SP_IS_ITEM(child)) {
897 Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
899 // convert layers to groups; FIXME: add "preserve layers" mode where each layer
900 // from impot is copied to the same-named layer in host
901 newchild->setAttribute("inkscape:groupmode", NULL);
903 newgroup->appendChild(newchild);
904 }
905 }
907 if (desktop) {
908 // Add it to the current layer
909 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
910 } else {
911 // There's no desktop (command line run?)
912 // FIXME: For such cases we need a document:: method to return the current layer
913 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
914 }
916 Inkscape::GC::release(newgroup);
917 } else {
918 // just add one item
919 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
920 if (SP_IS_ITEM(child)) {
921 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
922 newitem->setAttribute("inkscape:groupmode", NULL);
924 if (desktop) {
925 // Add it to the current layer
926 new_obj = desktop->currentLayer()->appendChildRepr(newitem);
927 } else {
928 // There's no desktop (command line run?)
929 // FIXME: For such cases we need a document:: method to return the current layer
930 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
931 }
933 }
934 }
935 }
937 if (style) sp_repr_css_attr_unref (style);
939 // select and move the imported item
940 if (new_obj && SP_IS_ITEM(new_obj)) {
941 Inkscape::Selection *selection = sp_desktop_selection(desktop);
942 selection->set(SP_ITEM(new_obj));
944 // To move the imported object, we must temporarily set the "transform pattern with
945 // object" option.
946 {
947 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
948 prefs_set_int_attribute("options.transform", "pattern", 1);
949 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
950 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
951 if (sel_bbox) {
952 NR::Point m( desktop->point() - sel_bbox->midpoint() );
953 sp_selection_move_relative(selection, m);
954 }
955 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
956 }
957 }
959 sp_document_unref(doc);
960 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
961 _("Import"));
963 } else {
964 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
965 sp_ui_error_dialog(text);
966 g_free(text);
967 }
969 return;
970 }
973 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
975 /**
976 * Display an Open dialog, import a resource if OK pressed.
977 */
978 void
979 sp_file_import(Gtk::Window &parentWindow)
980 {
981 static Glib::ustring import_path;
983 SPDocument *doc = SP_ACTIVE_DOCUMENT;
984 if (!doc)
985 return;
987 if (!importDialogInstance) {
988 importDialogInstance =
989 Inkscape::UI::Dialog::FileOpenDialog::create(
990 parentWindow,
991 import_path,
992 Inkscape::UI::Dialog::IMPORT_TYPES,
993 (char const *)_("Select file to import"));
994 }
996 bool success = importDialogInstance->show();
997 if (!success)
998 return;
1000 //# Get file name and extension type
1001 Glib::ustring fileName = importDialogInstance->getFilename();
1002 Inkscape::Extension::Extension *selection =
1003 importDialogInstance->getSelectionType();
1006 if (fileName.size() > 0) {
1008 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1010 if ( newFileName.size() > 0)
1011 fileName = newFileName;
1012 else
1013 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1016 import_path = fileName;
1017 if (import_path.size()>0)
1018 import_path.append(G_DIR_SEPARATOR_S);
1020 file_import(doc, fileName, selection);
1021 }
1023 return;
1024 }
1028 /*######################
1029 ## E X P O R T
1030 ######################*/
1032 //#define NEW_EXPORT_DIALOG
1036 #ifdef NEW_EXPORT_DIALOG
1038 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1040 /**
1041 * Display an Export dialog, export as the selected type if OK pressed
1042 */
1043 bool
1044 sp_file_export_dialog(void *widget)
1045 {
1046 //# temp hack for 'doc' until we can switch to this dialog
1047 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1049 Glib::ustring export_path;
1050 Glib::ustring export_loc;
1052 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1054 Inkscape::Extension::Output *extension;
1056 //# Get the default extension name
1057 Glib::ustring default_extension;
1058 char *attr = (char *)repr->attribute("inkscape:output_extension");
1059 if (!attr)
1060 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1061 if (attr)
1062 default_extension = attr;
1063 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1065 if (doc->uri == NULL)
1066 {
1067 char formatBuf[256];
1069 Glib::ustring filename_extension = ".svg";
1070 extension = dynamic_cast<Inkscape::Extension::Output *>
1071 (Inkscape::Extension::db.get(default_extension.c_str()));
1072 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1073 if (extension)
1074 filename_extension = extension->get_extension();
1076 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1077 if (attr)
1078 export_path = attr;
1080 if (!Inkscape::IO::file_test(export_path.c_str(),
1081 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1082 export_path = "";
1084 if (export_path.size()<1)
1085 export_path = g_get_home_dir();
1087 export_loc = export_path;
1088 export_loc.append(G_DIR_SEPARATOR_S);
1089 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1090 export_loc.append(formatBuf);
1092 }
1093 else
1094 {
1095 export_path = Glib::path_get_dirname(doc->uri);
1096 }
1098 // convert save_loc from utf-8 to locale
1099 // is this needed any more, now that everything is handled in
1100 // Inkscape::IO?
1101 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1102 if ( export_path_local.size() > 0)
1103 export_path = export_path_local;
1105 //# Show the SaveAs dialog
1106 if (!exportDialogInstance)
1107 exportDialogInstance =
1108 Inkscape::UI::Dialog::FileExportDialog::create(
1109 export_path,
1110 Inkscape::UI::Dialog::EXPORT_TYPES,
1111 (char const *) _("Select file to export to"),
1112 default_extension
1113 );
1115 bool success = exportDialogInstance->show();
1116 if (!success)
1117 return success;
1119 Glib::ustring fileName = exportDialogInstance->getFilename();
1121 Inkscape::Extension::Extension *selectionType =
1122 exportDialogInstance->getSelectionType();
1125 if (fileName.size() > 0) {
1126 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1128 if ( newFileName.size()>0 )
1129 fileName = newFileName;
1130 else
1131 g_warning( "Error converting save filename to UTF-8." );
1133 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1135 if (success)
1136 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1138 export_path = fileName;
1139 prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1141 return success;
1142 }
1145 return false;
1146 }
1148 #else
1150 /**
1151 *
1152 */
1153 bool
1154 sp_file_export_dialog(void */*widget*/)
1155 {
1156 sp_export_dialog();
1157 return true;
1158 }
1160 #endif
1162 /*######################
1163 ## E X P O R T T O O C A L
1164 ######################*/
1166 /**
1167 * Display an Export dialog, export as the selected type if OK pressed
1168 */
1169 bool
1170 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1171 {
1173 if (!SP_ACTIVE_DOCUMENT)
1174 return false;
1176 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1178 Glib::ustring export_path;
1179 Glib::ustring export_loc;
1180 Glib::ustring fileName;
1181 Inkscape::Extension::Extension *selectionType;
1183 bool success = false;
1185 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1186 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1187 static bool gotSuccess = false;
1189 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1190 // Verify whether the document is saved, so save this as temporary
1192 char *str = (char *) repr->attribute("sodipodi:modified");
1193 if ((!doc->uri) && (!str))
1194 return false;
1196 // Get the default extension name
1197 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1198 char formatBuf[256];
1200 Glib::ustring filename_extension = ".svg";
1201 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1203 export_path = Glib::get_tmp_dir ();
1205 export_loc = export_path;
1206 export_loc.append(G_DIR_SEPARATOR_S);
1207 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1208 export_loc.append(formatBuf);
1210 // convert save_loc from utf-8 to locale
1211 // is this needed any more, now that everything is handled in
1212 // Inkscape::IO?
1213 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1214 if ( export_path_local.size() > 0)
1215 export_path = export_path_local;
1217 // Show the Export To OCAL dialog
1218 if (!exportDialogInstance)
1219 exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
1220 parentWindow,
1221 Inkscape::UI::Dialog::EXPORT_TYPES,
1222 (char const *) _("Select file to export to")
1223 );
1225 success = exportDialogInstance->show();
1226 if (!success)
1227 return success;
1229 fileName = exportDialogInstance->getFilename();
1231 fileName.append(filename_extension.c_str());
1232 if (fileName.size() > 0) {
1233 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1235 if ( newFileName.size()>0 )
1236 fileName = newFileName;
1237 else
1238 g_warning( "Error converting save filename to UTF-8." );
1239 }
1240 Glib::ustring filePath = export_path;
1241 filePath.append(G_DIR_SEPARATOR_S);
1242 filePath.append(Glib::path_get_basename(fileName));
1244 fileName = filePath;
1246 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1248 if (!success){
1249 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1250 sp_ui_error_dialog(text);
1252 return success;
1253 }
1255 // Start now the submition
1257 // Create the uri
1258 Glib::ustring uri = "dav://";
1259 char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1260 char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1261 if ((username == NULL) || (!strcmp(username, "")) || (password == NULL) || (!strcmp(password, "")))
1262 {
1263 if(!gotSuccess)
1264 {
1265 if (!exportPasswordDialogInstance)
1266 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1267 parentWindow,
1268 (char const *) _("Open Clip Art Login"));
1269 success = exportPasswordDialogInstance->show();
1270 if (!success)
1271 return success;
1272 }
1273 username = (char *)exportPasswordDialogInstance->getUsername().c_str();
1274 password = (char *)exportPasswordDialogInstance->getPassword().c_str();
1275 }
1276 uri.append(username);
1277 uri.append(":");
1278 uri.append(password);
1279 uri.append("@");
1280 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1281 uri.append("/dav.php/");
1282 uri.append(Glib::path_get_basename(fileName));
1284 // Save as a remote file using the dav protocol.
1285 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1286 remove(fileName.c_str());
1287 if (!success)
1288 {
1289 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."));
1290 sp_ui_error_dialog(text);
1291 }
1292 else
1293 gotSuccess = true;
1295 return success;
1296 }
1298 /**
1299 * Export the current document to OCAL
1300 */
1301 void
1302 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1303 {
1305 // Try to execute the new code and return;
1306 if (!SP_ACTIVE_DOCUMENT)
1307 return;
1308 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1309 if (success)
1310 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1311 }
1314 /*######################
1315 ## I M P O R T F R O M O C A L
1316 ######################*/
1318 /**
1319 * Display an ImportToOcal Dialog, and the selected document from OCAL
1320 */
1321 void
1322 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1323 {
1324 static Glib::ustring import_path;
1326 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1327 if (!doc)
1328 return;
1330 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1332 if (!importDialogInstance) {
1333 importDialogInstance = new
1334 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1335 parentWindow,
1336 import_path,
1337 Inkscape::UI::Dialog::IMPORT_TYPES,
1338 (char const *)_("Import From Open Clip Art Library"));
1339 }
1341 bool success = importDialogInstance->show();
1342 if (!success)
1343 return;
1345 // Get file name and extension type
1346 Glib::ustring fileName = importDialogInstance->getFilename();
1347 Inkscape::Extension::Extension *selection =
1348 importDialogInstance->getSelectionType();
1350 if (fileName.size() > 0) {
1352 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1354 if ( newFileName.size() > 0)
1355 fileName = newFileName;
1356 else
1357 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1359 import_path = fileName;
1360 if (import_path.size()>0)
1361 import_path.append(G_DIR_SEPARATOR_S);
1363 file_import(doc, fileName, selection);
1364 }
1366 return;
1367 }
1369 /*######################
1370 ## P R I N T
1371 ######################*/
1374 /**
1375 * Print the current document, if any.
1376 */
1377 void
1378 sp_file_print(Gtk::Window& parentWindow)
1379 {
1380 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1381 if (doc)
1382 sp_print_document(parentWindow, doc);
1383 }
1385 /**
1386 * Display what the drawing would look like, if
1387 * printed.
1388 */
1389 void
1390 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1391 {
1393 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1394 if (doc)
1395 sp_print_preview_document(doc);
1397 }
1399 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1400 {
1401 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1403 if ( 0 ) {
1404 gchar const* things[] = {
1405 "data:foo,bar",
1406 "http://www.google.com/image.png",
1407 "ftp://ssd.com/doo",
1408 "/foo/dee/bar.svg",
1409 "foo.svg",
1410 "file:/foo/dee/bar.svg",
1411 "file:///foo/dee/bar.svg",
1412 "file:foo.svg",
1413 "/foo/bar\xe1\x84\x92.svg",
1414 "file:///foo/bar\xe1\x84\x92.svg",
1415 "file:///foo/bar%e1%84%92.svg",
1416 "/foo/bar%e1%84%92.svg",
1417 "bar\xe1\x84\x92.svg",
1418 "bar%e1%84%92.svg",
1419 NULL
1420 };
1421 g_message("+------");
1422 for ( int i = 0; things[i]; i++ )
1423 {
1424 try
1425 {
1426 URI uri(things[i]);
1427 gboolean isAbs = g_path_is_absolute( things[i] );
1428 gchar *str = uri.toString();
1429 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1430 (int)uri.isRelative(),
1431 uri.getScheme(),
1432 uri.getPath(),
1433 uri.getOpaque(),
1434 things[i],
1435 str );
1436 g_free(str);
1437 }
1438 catch ( MalformedURIException err )
1439 {
1440 dump_str( things[i], "MalformedURIException" );
1441 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1442 g_message(" gone from [%s] to [%s]", things[i], redo );
1443 if ( redo == NULL )
1444 {
1445 URI again = URI::fromUtf8( things[i] );
1446 gboolean isAbs = g_path_is_absolute( things[i] );
1447 gchar *str = again.toString();
1448 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1449 (int)again.isRelative(),
1450 again.getScheme(),
1451 again.getPath(),
1452 again.getOpaque(),
1453 things[i],
1454 str );
1455 g_free(str);
1456 g_message(" ----");
1457 }
1458 }
1459 }
1460 g_message("+------");
1461 }
1463 GSList const *images = sp_document_get_resource_list(doc, "image");
1464 for (GSList const *l = images; l != NULL; l = l->next) {
1465 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1467 const gchar *href = ir->attribute("xlink:href");
1469 // First try to figure out an absolute path to the asset
1470 //g_message("image href [%s]", href );
1471 if (spns && !g_path_is_absolute(href)) {
1472 const gchar *absref = ir->attribute("sodipodi:absref");
1473 const gchar *base_href = g_build_filename(base, href, NULL);
1474 //g_message(" absr [%s]", absref );
1476 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1477 {
1478 // only switch over if the absref is valid while href is not
1479 href = absref;
1480 //g_message(" copied absref to href");
1481 }
1482 }
1484 // Once we have an absolute path, convert it relative to the new location
1485 if (href && g_path_is_absolute(href)) {
1486 const gchar *relname = sp_relative_path_from_path(href, base);
1487 //g_message(" setting to [%s]", relname );
1488 ir->setAttribute("xlink:href", relname);
1489 }
1490 // TODO next refinement is to make the first choice keeping the relative path as-is if
1491 // based on the new location it gives us a valid file.
1492 }
1493 }
1496 /*
1497 Local Variables:
1498 mode:c++
1499 c-file-style:"stroustrup"
1500 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1501 indent-tabs-mode:nil
1502 fill-column:99
1503 End:
1504 */
1505 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :