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 (doc->isModifiedSinceSave()) {
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 /**
371 * Display an file Open selector. Open a document if OK is pressed.
372 * Can select single or multiple files for opening.
373 */
374 void
375 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
376 {
377 //# Get the current directory for finding files
378 static Glib::ustring open_path;
380 if(open_path.empty())
381 {
382 gchar const *attr = prefs_get_string_attribute("dialogs.open", "path");
383 if (attr)
384 open_path = attr;
385 }
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.empty())
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 Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
401 Inkscape::UI::Dialog::FileOpenDialog::create(
402 parentWindow, open_path,
403 Inkscape::UI::Dialog::SVG_TYPES,
404 _("Select file to open"));
406 //# Show the dialog
407 bool const success = openDialogInstance->show();
409 //# Save the folder the user selected for later
410 open_path = openDialogInstance->getCurrentDirectory();
412 if (!success)
413 {
414 delete openDialogInstance;
415 return;
416 }
418 //# User selected something. Get name and type
419 Glib::ustring fileName = openDialogInstance->getFilename();
421 Inkscape::Extension::Extension *selection =
422 openDialogInstance->getSelectionType();
424 //# Code to check & open if multiple files.
425 std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
427 //# We no longer need the file dialog object - delete it
428 delete openDialogInstance;
429 openDialogInstance = NULL;
431 //# Iterate through filenames if more than 1
432 if (flist.size() > 1)
433 {
434 for (unsigned int i = 0; i < flist.size(); i++)
435 {
436 fileName = flist[i];
438 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
439 if ( newFileName.size() > 0 )
440 fileName = newFileName;
441 else
442 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
444 #ifdef INK_DUMP_FILENAME_CONV
445 g_message("Opening File %s\n", fileName.c_str());
446 #endif
447 sp_file_open(fileName, selection);
448 }
450 return;
451 }
454 if (!fileName.empty())
455 {
456 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
458 if ( newFileName.size() > 0)
459 fileName = newFileName;
460 else
461 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
463 open_path = fileName;
464 open_path.append(G_DIR_SEPARATOR_S);
465 prefs_set_string_attribute("dialogs.open", "path", open_path.c_str());
467 sp_file_open(fileName, selection);
468 }
470 return;
471 }
474 /*######################
475 ## V A C U U M
476 ######################*/
478 /**
479 * Remove unreferenced defs from the defs section of the document.
480 */
483 void
484 sp_file_vacuum()
485 {
486 SPDocument *doc = SP_ACTIVE_DOCUMENT;
488 unsigned int diff = vacuum_document (doc);
490 sp_document_done(doc, SP_VERB_FILE_VACUUM,
491 _("Vacuum <defs>"));
493 SPDesktop *dt = SP_ACTIVE_DESKTOP;
494 if (diff > 0) {
495 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
496 ngettext("Removed <b>%i</b> unused definition in <defs>.",
497 "Removed <b>%i</b> unused definitions in <defs>.",
498 diff),
499 diff);
500 } else {
501 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
502 }
503 }
507 /*######################
508 ## S A V E
509 ######################*/
511 /**
512 * This 'save' function called by the others below
513 *
514 * \param official whether to set :output_module and :modified in the
515 * document; is true for normal save, false for temporary saves
516 */
517 static bool
518 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
519 Inkscape::Extension::Extension *key, bool saveas, bool official)
520 {
521 if (!doc || uri.size()<1) //Safety check
522 return false;
524 try {
525 Inkscape::Extension::save(key, doc, uri.c_str(),
526 false,
527 saveas, official);
528 } catch (Inkscape::Extension::Output::no_extension_found &e) {
529 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
530 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
531 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
532 sp_ui_error_dialog(text);
533 g_free(text);
534 g_free(safeUri);
535 return FALSE;
536 } catch (Inkscape::Extension::Output::save_failed &e) {
537 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
538 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
539 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
540 sp_ui_error_dialog(text);
541 g_free(text);
542 g_free(safeUri);
543 return FALSE;
544 } catch (Inkscape::Extension::Output::no_overwrite &e) {
545 return sp_file_save_dialog(parentWindow, doc);
546 }
548 SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
549 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
550 return true;
551 }
553 /*
554 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
555 */
556 bool
557 file_save_remote(SPDocument */*doc*/,
558 #ifdef WITH_GNOME_VFS
559 const Glib::ustring &uri,
560 #else
561 const Glib::ustring &/*uri*/,
562 #endif
563 Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
564 {
565 #ifdef WITH_GNOME_VFS
567 #define BUF_SIZE 8192
568 gnome_vfs_init();
570 GnomeVFSHandle *from_handle = NULL;
571 GnomeVFSHandle *to_handle = NULL;
572 GnomeVFSFileSize bytes_read;
573 GnomeVFSFileSize bytes_written;
574 GnomeVFSResult result;
575 guint8 buffer[8192];
577 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
579 if ( uri_local == NULL ) {
580 g_warning( "Error converting filename to locale encoding.");
581 }
583 // Gets the temp file name.
584 Glib::ustring fileName = Glib::get_tmp_dir ();
585 fileName.append(G_DIR_SEPARATOR_S);
586 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
588 // Open the temp file to send.
589 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
591 if (result != GNOME_VFS_OK) {
592 g_warning("Could not find the temp saving.");
593 return false;
594 }
596 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
597 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
599 if (result != GNOME_VFS_OK) {
600 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
601 return false;
602 }
604 while (1) {
606 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
608 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
609 result = gnome_vfs_close (from_handle);
610 result = gnome_vfs_close (to_handle);
611 return true;
612 }
614 if (result != GNOME_VFS_OK) {
615 g_warning("%s", gnome_vfs_result_to_string(result));
616 return false;
617 }
618 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
619 if (result != GNOME_VFS_OK) {
620 g_warning("%s", gnome_vfs_result_to_string(result));
621 return false;
622 }
625 if (bytes_read != bytes_written){
626 return false;
627 }
629 }
630 return true;
631 #else
632 // in case we do not have GNOME_VFS
633 return false;
634 #endif
636 }
639 /**
640 * Display a SaveAs dialog. Save the document if OK pressed.
641 *
642 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
643 */
644 bool
645 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
646 {
648 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
650 Inkscape::Extension::Output *extension = 0;
652 //# Get the default extension name
653 Glib::ustring default_extension;
654 char *attr = (char *)repr->attribute("inkscape:output_extension");
655 if (!attr)
656 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
657 if (attr)
658 default_extension = attr;
659 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
661 Glib::ustring save_path;
662 Glib::ustring save_loc;
664 if (doc->uri == NULL) {
665 char formatBuf[256];
666 int i = 1;
668 Glib::ustring filename_extension = ".svg";
669 extension = dynamic_cast<Inkscape::Extension::Output *>
670 (Inkscape::Extension::db.get(default_extension.c_str()));
671 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
672 if (extension)
673 filename_extension = extension->get_extension();
675 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
676 if (attr)
677 save_path = attr;
679 if (!Inkscape::IO::file_test(save_path.c_str(),
680 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
681 save_path = "";
683 if (save_path.size()<1)
684 save_path = g_get_home_dir();
686 save_loc = save_path;
687 save_loc.append(G_DIR_SEPARATOR_S);
688 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
689 save_loc.append(formatBuf);
691 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
692 save_loc = save_path;
693 save_loc.append(G_DIR_SEPARATOR_S);
694 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
695 save_loc.append(formatBuf);
696 }
697 } else {
698 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
699 Glib::path_get_basename(doc->uri));
700 }
702 // convert save_loc from utf-8 to locale
703 // is this needed any more, now that everything is handled in
704 // Inkscape::IO?
705 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
707 if ( save_loc_local.size() > 0)
708 save_loc = save_loc_local;
710 //# Show the SaveAs dialog
711 char const * dialog_title;
712 if (is_copy) {
713 dialog_title = (char const *) _("Select file to save a copy to");
714 } else {
715 dialog_title = (char const *) _("Select file to save to");
716 }
717 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
718 Inkscape::UI::Dialog::FileSaveDialog::create(
719 parentWindow,
720 save_loc,
721 Inkscape::UI::Dialog::SVG_TYPES,
722 dialog_title,
723 default_extension
724 );
726 saveDialog->setSelectionType(extension);
728 bool success = saveDialog->show();
729 if (!success) {
730 delete saveDialog;
731 return success;
732 }
734 Glib::ustring fileName = saveDialog->getFilename();
735 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
737 delete saveDialog;
739 saveDialog = 0;
741 if (fileName.size() > 0) {
742 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
744 if ( newFileName.size()>0 )
745 fileName = newFileName;
746 else
747 g_warning( "Error converting save filename to UTF-8." );
749 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
751 if (success)
752 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
754 save_path = Glib::path_get_dirname(fileName);
755 prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
757 return success;
758 }
761 return false;
762 }
765 /**
766 * Save a document, displaying a SaveAs dialog if necessary.
767 */
768 bool
769 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
770 {
771 bool success = true;
773 if (doc->isModifiedSinceSave()) {
774 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
775 if ( doc->uri == NULL
776 || repr->attribute("inkscape:output_extension") == NULL )
777 {
778 return sp_file_save_dialog(parentWindow, doc, FALSE);
779 } else {
780 gchar const *fn = g_strdup(doc->uri);
781 gchar const *ext = repr->attribute("inkscape:output_extension");
782 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
783 g_free((void *) fn);
784 }
785 } else {
786 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
787 success = TRUE;
788 }
790 return success;
791 }
794 /**
795 * Save a document.
796 */
797 bool
798 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
799 {
800 if (!SP_ACTIVE_DOCUMENT)
801 return false;
803 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
805 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
806 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
807 }
810 /**
811 * Save a document, always displaying the SaveAs dialog.
812 */
813 bool
814 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
815 {
816 if (!SP_ACTIVE_DOCUMENT)
817 return false;
818 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
819 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
820 }
824 /**
825 * Save a copy of a document, always displaying a sort of SaveAs dialog.
826 */
827 bool
828 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
829 {
830 if (!SP_ACTIVE_DOCUMENT)
831 return false;
832 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
833 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
834 }
837 /*######################
838 ## I M P O R T
839 ######################*/
841 /**
842 * Import a resource. Called by sp_file_import()
843 */
844 void
845 file_import(SPDocument *in_doc, const Glib::ustring &uri,
846 Inkscape::Extension::Extension *key)
847 {
848 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
850 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
851 SPDocument *doc;
852 try {
853 doc = Inkscape::Extension::open(key, uri.c_str());
854 } catch (Inkscape::Extension::Input::no_extension_found &e) {
855 doc = NULL;
856 } catch (Inkscape::Extension::Input::open_failed &e) {
857 doc = NULL;
858 }
860 if (doc != NULL) {
861 // move imported defs to our document's defs
862 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
863 SPObject *defs = SP_DOCUMENT_DEFS(doc);
865 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
866 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
868 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
869 for (SPObject *child = sp_object_first_child(defs);
870 child != NULL; child = SP_OBJECT_NEXT(child))
871 {
872 // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
873 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
874 }
876 guint items_count = 0;
877 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
878 child != NULL; child = SP_OBJECT_NEXT(child)) {
879 if (SP_IS_ITEM(child))
880 items_count ++;
881 }
882 SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
884 SPObject *new_obj = NULL;
886 if ((style && style->firstChild()) || items_count > 1) {
887 // create group
888 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
889 Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
890 sp_repr_css_set (newgroup, style, "style");
892 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
893 if (SP_IS_ITEM(child)) {
894 Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
896 // convert layers to groups; FIXME: add "preserve layers" mode where each layer
897 // from impot is copied to the same-named layer in host
898 newchild->setAttribute("inkscape:groupmode", NULL);
900 newgroup->appendChild(newchild);
901 }
902 }
904 if (desktop) {
905 // Add it to the current layer
906 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
907 } else {
908 // There's no desktop (command line run?)
909 // FIXME: For such cases we need a document:: method to return the current layer
910 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
911 }
913 Inkscape::GC::release(newgroup);
914 } else {
915 // just add one item
916 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
917 if (SP_IS_ITEM(child)) {
918 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
919 newitem->setAttribute("inkscape:groupmode", NULL);
921 if (desktop) {
922 // Add it to the current layer
923 new_obj = desktop->currentLayer()->appendChildRepr(newitem);
924 } else {
925 // There's no desktop (command line run?)
926 // FIXME: For such cases we need a document:: method to return the current layer
927 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
928 }
930 }
931 }
932 }
934 if (style) sp_repr_css_attr_unref (style);
936 // select and move the imported item
937 if (new_obj && SP_IS_ITEM(new_obj)) {
938 Inkscape::Selection *selection = sp_desktop_selection(desktop);
939 selection->set(SP_ITEM(new_obj));
941 // To move the imported object, we must temporarily set the "transform pattern with
942 // object" option.
943 {
944 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
945 prefs_set_int_attribute("options.transform", "pattern", 1);
946 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
947 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
948 if (sel_bbox) {
949 NR::Point m( desktop->point() - sel_bbox->midpoint() );
950 sp_selection_move_relative(selection, m);
951 }
952 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
953 }
954 }
956 sp_document_unref(doc);
957 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
958 _("Import"));
960 } else {
961 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
962 sp_ui_error_dialog(text);
963 g_free(text);
964 }
966 return;
967 }
970 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
972 /**
973 * Display an Open dialog, import a resource if OK pressed.
974 */
975 void
976 sp_file_import(Gtk::Window &parentWindow)
977 {
978 static Glib::ustring import_path;
980 SPDocument *doc = SP_ACTIVE_DOCUMENT;
981 if (!doc)
982 return;
984 if (!importDialogInstance) {
985 importDialogInstance =
986 Inkscape::UI::Dialog::FileOpenDialog::create(
987 parentWindow,
988 import_path,
989 Inkscape::UI::Dialog::IMPORT_TYPES,
990 (char const *)_("Select file to import"));
991 }
993 bool success = importDialogInstance->show();
994 if (!success)
995 return;
997 //# Get file name and extension type
998 Glib::ustring fileName = importDialogInstance->getFilename();
999 Inkscape::Extension::Extension *selection =
1000 importDialogInstance->getSelectionType();
1003 if (fileName.size() > 0) {
1005 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1007 if ( newFileName.size() > 0)
1008 fileName = newFileName;
1009 else
1010 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1013 import_path = fileName;
1014 if (import_path.size()>0)
1015 import_path.append(G_DIR_SEPARATOR_S);
1017 file_import(doc, fileName, selection);
1018 }
1020 return;
1021 }
1025 /*######################
1026 ## E X P O R T
1027 ######################*/
1029 //#define NEW_EXPORT_DIALOG
1033 #ifdef NEW_EXPORT_DIALOG
1035 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1037 /**
1038 * Display an Export dialog, export as the selected type if OK pressed
1039 */
1040 bool
1041 sp_file_export_dialog(void *widget)
1042 {
1043 //# temp hack for 'doc' until we can switch to this dialog
1044 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1046 Glib::ustring export_path;
1047 Glib::ustring export_loc;
1049 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1051 Inkscape::Extension::Output *extension;
1053 //# Get the default extension name
1054 Glib::ustring default_extension;
1055 char *attr = (char *)repr->attribute("inkscape:output_extension");
1056 if (!attr)
1057 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1058 if (attr)
1059 default_extension = attr;
1060 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1062 if (doc->uri == NULL)
1063 {
1064 char formatBuf[256];
1066 Glib::ustring filename_extension = ".svg";
1067 extension = dynamic_cast<Inkscape::Extension::Output *>
1068 (Inkscape::Extension::db.get(default_extension.c_str()));
1069 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1070 if (extension)
1071 filename_extension = extension->get_extension();
1073 attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1074 if (attr)
1075 export_path = attr;
1077 if (!Inkscape::IO::file_test(export_path.c_str(),
1078 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1079 export_path = "";
1081 if (export_path.size()<1)
1082 export_path = g_get_home_dir();
1084 export_loc = export_path;
1085 export_loc.append(G_DIR_SEPARATOR_S);
1086 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1087 export_loc.append(formatBuf);
1089 }
1090 else
1091 {
1092 export_path = Glib::path_get_dirname(doc->uri);
1093 }
1095 // convert save_loc from utf-8 to locale
1096 // is this needed any more, now that everything is handled in
1097 // Inkscape::IO?
1098 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1099 if ( export_path_local.size() > 0)
1100 export_path = export_path_local;
1102 //# Show the SaveAs dialog
1103 if (!exportDialogInstance)
1104 exportDialogInstance =
1105 Inkscape::UI::Dialog::FileExportDialog::create(
1106 export_path,
1107 Inkscape::UI::Dialog::EXPORT_TYPES,
1108 (char const *) _("Select file to export to"),
1109 default_extension
1110 );
1112 bool success = exportDialogInstance->show();
1113 if (!success)
1114 return success;
1116 Glib::ustring fileName = exportDialogInstance->getFilename();
1118 Inkscape::Extension::Extension *selectionType =
1119 exportDialogInstance->getSelectionType();
1122 if (fileName.size() > 0) {
1123 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1125 if ( newFileName.size()>0 )
1126 fileName = newFileName;
1127 else
1128 g_warning( "Error converting save filename to UTF-8." );
1130 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1132 if (success)
1133 prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1135 export_path = fileName;
1136 prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1138 return success;
1139 }
1142 return false;
1143 }
1145 #else
1147 /**
1148 *
1149 */
1150 bool
1151 sp_file_export_dialog(void */*widget*/)
1152 {
1153 sp_export_dialog();
1154 return true;
1155 }
1157 #endif
1159 /*######################
1160 ## E X P O R T T O O C A L
1161 ######################*/
1163 /**
1164 * Display an Export dialog, export as the selected type if OK pressed
1165 */
1166 bool
1167 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1168 {
1170 if (!SP_ACTIVE_DOCUMENT)
1171 return false;
1173 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1175 Glib::ustring export_path;
1176 Glib::ustring export_loc;
1177 Glib::ustring fileName;
1178 Inkscape::Extension::Extension *selectionType;
1180 bool success = false;
1182 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1183 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1184 static bool gotSuccess = false;
1186 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1187 (void)repr;
1189 if (!doc->uri && !doc->isModifiedSinceSave())
1190 return false;
1192 // Get the default extension name
1193 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1194 char formatBuf[256];
1196 Glib::ustring filename_extension = ".svg";
1197 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1199 export_path = Glib::get_tmp_dir ();
1201 export_loc = export_path;
1202 export_loc.append(G_DIR_SEPARATOR_S);
1203 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1204 export_loc.append(formatBuf);
1206 // convert save_loc from utf-8 to locale
1207 // is this needed any more, now that everything is handled in
1208 // Inkscape::IO?
1209 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1210 if ( export_path_local.size() > 0)
1211 export_path = export_path_local;
1213 // Show the Export To OCAL dialog
1214 if (!exportDialogInstance)
1215 exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
1216 parentWindow,
1217 Inkscape::UI::Dialog::EXPORT_TYPES,
1218 (char const *) _("Select file to export to")
1219 );
1221 success = exportDialogInstance->show();
1222 if (!success)
1223 return success;
1225 fileName = exportDialogInstance->getFilename();
1227 fileName.append(filename_extension.c_str());
1228 if (fileName.size() > 0) {
1229 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1231 if ( newFileName.size()>0 )
1232 fileName = newFileName;
1233 else
1234 g_warning( "Error converting save filename to UTF-8." );
1235 }
1236 Glib::ustring filePath = export_path;
1237 filePath.append(G_DIR_SEPARATOR_S);
1238 filePath.append(Glib::path_get_basename(fileName));
1240 fileName = filePath;
1242 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1244 if (!success){
1245 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1246 sp_ui_error_dialog(text);
1248 return success;
1249 }
1251 // Start now the submition
1253 // Create the uri
1254 Glib::ustring uri = "dav://";
1255 char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1256 char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1257 if ((username == NULL) || (!strcmp(username, "")) || (password == NULL) || (!strcmp(password, "")))
1258 {
1259 if(!gotSuccess)
1260 {
1261 if (!exportPasswordDialogInstance)
1262 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1263 parentWindow,
1264 (char const *) _("Open Clip Art Login"));
1265 success = exportPasswordDialogInstance->show();
1266 if (!success)
1267 return success;
1268 }
1269 username = (char *)exportPasswordDialogInstance->getUsername().c_str();
1270 password = (char *)exportPasswordDialogInstance->getPassword().c_str();
1271 }
1272 uri.append(username);
1273 uri.append(":");
1274 uri.append(password);
1275 uri.append("@");
1276 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1277 uri.append("/dav.php/");
1278 uri.append(Glib::path_get_basename(fileName));
1280 // Save as a remote file using the dav protocol.
1281 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1282 remove(fileName.c_str());
1283 if (!success)
1284 {
1285 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."));
1286 sp_ui_error_dialog(text);
1287 }
1288 else
1289 gotSuccess = true;
1291 return success;
1292 }
1294 /**
1295 * Export the current document to OCAL
1296 */
1297 void
1298 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1299 {
1301 // Try to execute the new code and return;
1302 if (!SP_ACTIVE_DOCUMENT)
1303 return;
1304 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1305 if (success)
1306 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1307 }
1310 /*######################
1311 ## I M P O R T F R O M O C A L
1312 ######################*/
1314 /**
1315 * Display an ImportToOcal Dialog, and the selected document from OCAL
1316 */
1317 void
1318 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1319 {
1320 static Glib::ustring import_path;
1322 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1323 if (!doc)
1324 return;
1326 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1328 if (!importDialogInstance) {
1329 importDialogInstance = new
1330 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1331 parentWindow,
1332 import_path,
1333 Inkscape::UI::Dialog::IMPORT_TYPES,
1334 (char const *)_("Import From Open Clip Art Library"));
1335 }
1337 bool success = importDialogInstance->show();
1338 if (!success)
1339 return;
1341 // Get file name and extension type
1342 Glib::ustring fileName = importDialogInstance->getFilename();
1343 Inkscape::Extension::Extension *selection =
1344 importDialogInstance->getSelectionType();
1346 if (fileName.size() > 0) {
1348 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1350 if ( newFileName.size() > 0)
1351 fileName = newFileName;
1352 else
1353 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1355 import_path = fileName;
1356 if (import_path.size()>0)
1357 import_path.append(G_DIR_SEPARATOR_S);
1359 file_import(doc, fileName, selection);
1360 }
1362 return;
1363 }
1365 /*######################
1366 ## P R I N T
1367 ######################*/
1370 /**
1371 * Print the current document, if any.
1372 */
1373 void
1374 sp_file_print(Gtk::Window& parentWindow)
1375 {
1376 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1377 if (doc)
1378 sp_print_document(parentWindow, doc);
1379 }
1381 /**
1382 * Display what the drawing would look like, if
1383 * printed.
1384 */
1385 void
1386 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1387 {
1389 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1390 if (doc)
1391 sp_print_preview_document(doc);
1393 }
1395 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1396 {
1397 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1399 if ( 0 ) {
1400 gchar const* things[] = {
1401 "data:foo,bar",
1402 "http://www.google.com/image.png",
1403 "ftp://ssd.com/doo",
1404 "/foo/dee/bar.svg",
1405 "foo.svg",
1406 "file:/foo/dee/bar.svg",
1407 "file:///foo/dee/bar.svg",
1408 "file:foo.svg",
1409 "/foo/bar\xe1\x84\x92.svg",
1410 "file:///foo/bar\xe1\x84\x92.svg",
1411 "file:///foo/bar%e1%84%92.svg",
1412 "/foo/bar%e1%84%92.svg",
1413 "bar\xe1\x84\x92.svg",
1414 "bar%e1%84%92.svg",
1415 NULL
1416 };
1417 g_message("+------");
1418 for ( int i = 0; things[i]; i++ )
1419 {
1420 try
1421 {
1422 URI uri(things[i]);
1423 gboolean isAbs = g_path_is_absolute( things[i] );
1424 gchar *str = uri.toString();
1425 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1426 (int)uri.isRelative(),
1427 uri.getScheme(),
1428 uri.getPath(),
1429 uri.getOpaque(),
1430 things[i],
1431 str );
1432 g_free(str);
1433 }
1434 catch ( MalformedURIException err )
1435 {
1436 dump_str( things[i], "MalformedURIException" );
1437 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1438 g_message(" gone from [%s] to [%s]", things[i], redo );
1439 if ( redo == NULL )
1440 {
1441 URI again = URI::fromUtf8( things[i] );
1442 gboolean isAbs = g_path_is_absolute( things[i] );
1443 gchar *str = again.toString();
1444 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1445 (int)again.isRelative(),
1446 again.getScheme(),
1447 again.getPath(),
1448 again.getOpaque(),
1449 things[i],
1450 str );
1451 g_free(str);
1452 g_message(" ----");
1453 }
1454 }
1455 }
1456 g_message("+------");
1457 }
1459 GSList const *images = sp_document_get_resource_list(doc, "image");
1460 for (GSList const *l = images; l != NULL; l = l->next) {
1461 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1463 const gchar *href = ir->attribute("xlink:href");
1465 // First try to figure out an absolute path to the asset
1466 //g_message("image href [%s]", href );
1467 if (spns && !g_path_is_absolute(href)) {
1468 const gchar *absref = ir->attribute("sodipodi:absref");
1469 const gchar *base_href = g_build_filename(base, href, NULL);
1470 //g_message(" absr [%s]", absref );
1472 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1473 {
1474 // only switch over if the absref is valid while href is not
1475 href = absref;
1476 //g_message(" copied absref to href");
1477 }
1478 }
1480 // Once we have an absolute path, convert it relative to the new location
1481 if (href && g_path_is_absolute(href)) {
1482 const gchar *relname = sp_relative_path_from_path(href, base);
1483 //g_message(" setting to [%s]", relname );
1484 ir->setAttribute("xlink:href", relname);
1485 }
1486 // TODO next refinement is to make the first choice keeping the relative path as-is if
1487 // based on the new location it gives us a valid file.
1488 }
1489 }
1492 /*
1493 Local Variables:
1494 mode:c++
1495 c-file-style:"stroustrup"
1496 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1497 indent-tabs-mode:nil
1498 fill-column:99
1499 End:
1500 */
1501 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :