Code

adds export to ocal feature without dialog window
[inkscape.git] / src / file.cpp
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;
120 SPDesktop*
121 sp_file_new_default()
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("");
148 /*######################
149 ## D E L E T E
150 ######################*/
152 /**
153  *  Perform document closures preceding an exit()
154  */
155 void
156 sp_file_exit()
158     sp_ui_close_all();
159     // no need to call inkscape_exit here; last document being closed will take care of that
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)
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     }
229 /**
230  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
231  */
232 void
233 sp_file_revert_dialog()
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     }
278 void dump_str(gchar const *str, gchar const *prefix)
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());
294 void dump_ustr(Glib::ustring const &ustr)
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("---------------");
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)
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;
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()
473     SPDocument *doc = SP_ACTIVE_DOCUMENT;
475     unsigned int diff = vacuum_document (doc);
477     sp_document_done(doc, SP_VERB_FILE_VACUUM, 
478                      _("Vacuum &lt;defs&gt;"));
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 &lt;defs&gt;.",
484                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
485                          diff),
486                 diff);
487     } else {
488         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
489     }
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  *
503  * \param    official  whether to set :output_module and :modified in the
504  *                     document; is true for normal save, false for temporary saves
505  */
506 static bool
507 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
508           Inkscape::Extension::Extension *key, bool saveas, bool official)
510     if (!doc || uri.size()<1) //Safety check
511         return false;
513 #ifdef WITH_GNOME_VFS
514     
515     if (gnome_vfs_initialized() && !gnome_vfs_uri_is_local(gnome_vfs_uri_new(uri.c_str()))) {
516         // Use VFS for this
517         bool success = file_save_remote(doc, uri, key, saveas, official);
518         if (!success) {
519             g_warning("Error:  Could not save file '%s' with VFS\n", uri.c_str());
520             return false;  
521         }    
522         else
523             return true;
524     }
525     else
526         return file_save_local(parentWindow, doc, uri, key, saveas, official);
527 #else
528     
529     return file_save_local(parentWindow, doc, uri, key, saveas, official);
530     
531 #endif
534 bool
535 file_save_local(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
536           Inkscape::Extension::Extension *key, bool saveas, bool official)
538     try {
539         Inkscape::Extension::save(key, doc, uri.c_str(),
540                  false,
541                  saveas, official); 
542     } catch (Inkscape::Extension::Output::no_extension_found &e) {
543         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
544         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
545         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
546         sp_ui_error_dialog(text);
547         g_free(text);
548         g_free(safeUri);
549         return FALSE;
550     } catch (Inkscape::Extension::Output::save_failed &e) {
551         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
552         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
553         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
554         sp_ui_error_dialog(text);
555         g_free(text);
556         g_free(safeUri);
557         return FALSE;
558     } catch (Inkscape::Extension::Output::no_overwrite &e) {
559         return sp_file_save_dialog(parentWindow, doc);
560     }
562     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
563     return true;
568 #ifdef WITH_GNOME_VFS
571 bool
572 file_save_remote(SPDocument *doc, const Glib::ustring &uri,
573                  Inkscape::Extension::Extension *key, bool saveas, bool official)
576 #define BUF_SIZE 8192
577     gnome_vfs_init();
579     GnomeVFSHandle    *from_handle = NULL;
580     GnomeVFSHandle    *to_handle = NULL;
581     GnomeVFSFileSize  bytes_read;
582     GnomeVFSFileSize  bytes_written;
583     GnomeVFSResult    result;
584     guint8 buffer[8192];
586     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
587     
588     if ( uri_local == NULL ) {
589         g_warning( "Error converting filename to locale encoding.");
590     }
592     // Gets the temp file name.
593     Glib::ustring fileName = Glib::get_tmp_dir ();
594     fileName.append(G_DIR_SEPARATOR_S);
595     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
597     // Open the temp file to send.
598     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
599     
600     if (result != GNOME_VFS_OK) {
601         g_warning("Could not find the temp saving.");
602         return false;
603     }
605     
606     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
607     
608         
609     if (result == GNOME_VFS_ERROR_NOT_FOUND){
610         result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
611     }
612     
613     if (result != GNOME_VFS_OK) {
614         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
615         return false;
616     }
618     ///g_warning("file_save_remote: temp dir: %s",fileName.c_str());
620     while (1) {
621         
622         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
623         //g_warning("bytes lidos: %d",bytes_read);
625         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
626             result = gnome_vfs_close (from_handle);
627             result = gnome_vfs_close (to_handle);
628             return true;
629         }
630         
631         //g_warning("while: %s", gnome_vfs_result_to_string(result));
633         if (result != GNOME_VFS_OK) {
634             g_warning("%s", gnome_vfs_result_to_string(result));
635             return false;
636         }
637         //g_warning("%s",buffer);
638         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
639         //g_warning("escritos: %d",bytes_written);
640         if (result != GNOME_VFS_OK) {
641             g_warning("%s", gnome_vfs_result_to_string(result));
642             return false;
643         }
644         
645         
646         if (bytes_read != bytes_written){
647             return false;
648         }
649         
650     }
651     return true;
653 #endif
656 /**
657  *  Display a SaveAs dialog.  Save the document if OK pressed.
658  *
659  * \param    ascopy  (optional) wether to set the documents->uri to the new filename or not
660  */
661 bool
662 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
665     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
667     Inkscape::Extension::Output *extension = 0;
669     //# Get the default extension name
670     Glib::ustring default_extension;
671     char *attr = (char *)repr->attribute("inkscape:output_extension");
672     if (!attr)
673         attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
674     if (attr)
675         default_extension = attr;
676     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
678     Glib::ustring save_path;
679     Glib::ustring save_loc;
681     if (doc->uri == NULL) {
682         char formatBuf[256];
683         int i = 1;
685         Glib::ustring filename_extension = ".svg";
686         extension = dynamic_cast<Inkscape::Extension::Output *>
687               (Inkscape::Extension::db.get(default_extension.c_str()));
688         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
689         if (extension)
690             filename_extension = extension->get_extension();
692         attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
693         if (attr)
694             save_path = attr;
696         if (!Inkscape::IO::file_test(save_path.c_str(),
697               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
698             save_path = "";
700         if (save_path.size()<1)
701             save_path = g_get_home_dir();
703         save_loc = save_path;
704         save_loc.append(G_DIR_SEPARATOR_S);
705         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
706         save_loc.append(formatBuf);
708         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
709             save_loc = save_path;
710             save_loc.append(G_DIR_SEPARATOR_S);
711             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
712             save_loc.append(formatBuf);
713         }
714     } else {
715         save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
716                                         Glib::path_get_basename(doc->uri));
717     }
719     // convert save_loc from utf-8 to locale
720     // is this needed any more, now that everything is handled in
721     // Inkscape::IO?
722     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
724     if ( save_loc_local.size() > 0) 
725         save_loc = save_loc_local;
727     //# Show the SaveAs dialog
728     char const * dialog_title;
729     if (is_copy) {
730         dialog_title = (char const *) _("Select file to save a copy to");
731     } else {
732         dialog_title = (char const *) _("Select file to save to");
733     }
734     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
735         Inkscape::UI::Dialog::FileSaveDialog::create(
736                 parentWindow, 
737             save_loc,
738             Inkscape::UI::Dialog::SVG_TYPES,
739             (char const *) _("Select file to save to"),
740             default_extension
741             );
743     saveDialog->change_title(dialog_title);
744     saveDialog->setSelectionType(extension);
746     // allow easy access to the user's own templates folder              
747     gchar *templates = profile_path ("templates");
748     if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
749         dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
750     }
751     g_free (templates);
753     bool success = saveDialog->show();
754     if (!success) {
755         delete saveDialog;
756         return success;
757     }
759     Glib::ustring fileName = saveDialog->getFilename();
760     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
762     delete saveDialog;
763         
764     saveDialog = 0;
766     if (fileName.size() > 0) {
767         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
769         if ( newFileName.size()>0 )
770             fileName = newFileName;
771         else
772             g_warning( "Error converting save filename to UTF-8." );
774         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
776         if (success)
777             prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
779         save_path = Glib::path_get_dirname(fileName);
780         prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
782         return success;
783     }
786     return false;
790 /**
791  * Save a document, displaying a SaveAs dialog if necessary.
792  */
793 bool
794 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
796     bool success = true;
798     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
800     gchar const *fn = repr->attribute("sodipodi:modified");
801     if (fn != NULL) {
802         if ( doc->uri == NULL
803             || repr->attribute("inkscape:output_extension") == NULL )
804         {
805             return sp_file_save_dialog(parentWindow, doc, FALSE);
806         } else {
807             fn = g_strdup(doc->uri);
808             gchar const *ext = repr->attribute("inkscape:output_extension");
809             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
810             g_free((void *) fn);
811         }
812     } else {
813         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
814         success = TRUE;
815     }
817     return success;
821 /**
822  * Save a document.
823  */
824 bool
825 sp_file_save(Gtk::Window &parentWindow, gpointer object, gpointer data)
827     if (!SP_ACTIVE_DOCUMENT)
828         return false;
830     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
832     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
833     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
837 /**
838  *  Save a document, always displaying the SaveAs dialog.
839  */
840 bool
841 sp_file_save_as(Gtk::Window &parentWindow, gpointer object, gpointer data)
843     if (!SP_ACTIVE_DOCUMENT)
844         return false;
845     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
846     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
851 /**
852  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
853  */
854 bool
855 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer object, gpointer data)
857     if (!SP_ACTIVE_DOCUMENT)
858         return false;
859     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
860     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
864 /*######################
865 ## I M P O R T
866 ######################*/
868 /**
869  *  Import a resource.  Called by sp_file_import()
870  */
871 void
872 file_import(SPDocument *in_doc, const Glib::ustring &uri,
873                Inkscape::Extension::Extension *key)
875     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
877     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
878     SPDocument *doc;
879     try {
880         doc = Inkscape::Extension::open(key, uri.c_str());
881     } catch (Inkscape::Extension::Input::no_extension_found &e) {
882         doc = NULL;
883     } catch (Inkscape::Extension::Input::open_failed &e) {
884         doc = NULL;
885     }
887     if (doc != NULL) {
888         // move imported defs to our document's defs
889         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
890         SPObject *defs = SP_DOCUMENT_DEFS(doc);
892         Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
893         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
895         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
896         for (SPObject *child = sp_object_first_child(defs);
897              child != NULL; child = SP_OBJECT_NEXT(child))
898         {
899             // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
900             SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
901         }
903         guint items_count = 0;
904         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
905              child != NULL; child = SP_OBJECT_NEXT(child)) {
906             if (SP_IS_ITEM(child))
907                 items_count ++;
908         }
909         SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
911         SPObject *new_obj = NULL;
913         if ((style && style->firstChild()) || items_count > 1) {
914             // create group
915             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
916             Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
917             sp_repr_css_set (newgroup, style, "style");
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 *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
923                     // convert layers to groups; FIXME: add "preserve layers" mode where each layer
924                     // from impot is copied to the same-named layer in host
925                     newchild->setAttribute("inkscape:groupmode", NULL);
927                     newgroup->appendChild(newchild);
928                 }
929             }
931             if (desktop) {
932                 // Add it to the current layer
933                 new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
934             } else {
935                 // There's no desktop (command line run?)
936                 // FIXME: For such cases we need a document:: method to return the current layer
937                 new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
938             }
940             Inkscape::GC::release(newgroup);
941         } else {
942             // just add one item
943             for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
944                 if (SP_IS_ITEM(child)) {
945                     Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
946                     newitem->setAttribute("inkscape:groupmode", NULL);
948                     if (desktop) {
949                         // Add it to the current layer
950                         new_obj = desktop->currentLayer()->appendChildRepr(newitem);
951                     } else {
952                         // There's no desktop (command line run?)
953                         // FIXME: For such cases we need a document:: method to return the current layer
954                         new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
955                     }
957                 }
958             }
959         }
961         if (style) sp_repr_css_attr_unref (style);
963         // select and move the imported item
964         if (new_obj && SP_IS_ITEM(new_obj)) {
965             Inkscape::Selection *selection = sp_desktop_selection(desktop);
966             selection->set(SP_ITEM(new_obj));
968             // To move the imported object, we must temporarily set the "transform pattern with
969             // object" option.
970             {
971                 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
972                 prefs_set_int_attribute("options.transform", "pattern", 1);
973                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
974                 NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
975                 if (sel_bbox) {
976                     NR::Point m( desktop->point() - sel_bbox->midpoint() );
977                     sp_selection_move_relative(selection, m);
978                 }
979                 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
980             }
981         }
983         sp_document_unref(doc);
984         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
985                          _("Import"));
987     } else {
988         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
989         sp_ui_error_dialog(text);
990         g_free(text);
991     }
993     return;
997 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
999 /**
1000  *  Display an Open dialog, import a resource if OK pressed.
1001  */
1002 void
1003 sp_file_import(Gtk::Window &parentWindow)
1005     static Glib::ustring import_path;
1007     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1008     if (!doc)
1009         return;
1011     if (!importDialogInstance) {
1012         importDialogInstance =
1013              Inkscape::UI::Dialog::FileOpenDialog::create(
1014                  parentWindow,
1015                  import_path,
1016                  Inkscape::UI::Dialog::IMPORT_TYPES,
1017                  (char const *)_("Select file to import"));
1018     }
1020     bool success = importDialogInstance->show();
1021     if (!success)
1022         return;
1024     //# Get file name and extension type
1025     Glib::ustring fileName = importDialogInstance->getFilename();
1026     Inkscape::Extension::Extension *selection =
1027         importDialogInstance->getSelectionType();
1030     if (fileName.size() > 0) {
1031  
1032         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1034         if ( newFileName.size() > 0)
1035             fileName = newFileName;
1036         else
1037             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1039         import_path = fileName;
1040         if (import_path.size()>0)
1041             import_path.append(G_DIR_SEPARATOR_S);
1043         file_import(doc, fileName, selection);
1044     }
1046     return;
1051 /*######################
1052 ## E X P O R T
1053 ######################*/
1055 //#define NEW_EXPORT_DIALOG
1059 #ifdef NEW_EXPORT_DIALOG
1061 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1063 /**
1064  *  Display an Export dialog, export as the selected type if OK pressed
1065  */
1066 bool
1067 sp_file_export_dialog(void *widget)
1069     //# temp hack for 'doc' until we can switch to this dialog
1070     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1072     Glib::ustring export_path; 
1073     Glib::ustring export_loc; 
1075     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1077     Inkscape::Extension::Output *extension;
1079     //# Get the default extension name
1080     Glib::ustring default_extension;
1081     char *attr = (char *)repr->attribute("inkscape:output_extension");
1082     if (!attr)
1083         attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1084     if (attr)
1085         default_extension = attr;
1086     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1088     if (doc->uri == NULL)
1089         {
1090         char formatBuf[256];
1092         Glib::ustring filename_extension = ".svg";
1093         extension = dynamic_cast<Inkscape::Extension::Output *>
1094               (Inkscape::Extension::db.get(default_extension.c_str()));
1095         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1096         if (extension)
1097             filename_extension = extension->get_extension();
1099         attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1100         if (attr)
1101             export_path = attr;
1103         if (!Inkscape::IO::file_test(export_path.c_str(),
1104               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1105             export_path = "";
1107         if (export_path.size()<1)
1108             export_path = g_get_home_dir();
1110         export_loc = export_path;
1111         export_loc.append(G_DIR_SEPARATOR_S);
1112         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1113         export_loc.append(formatBuf);
1115         }
1116     else
1117         {
1118         export_path = Glib::path_get_dirname(doc->uri);
1119         }
1121     // convert save_loc from utf-8 to locale
1122     // is this needed any more, now that everything is handled in
1123     // Inkscape::IO?
1124     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1125     if ( export_path_local.size() > 0) 
1126         export_path = export_path_local;
1128     //# Show the SaveAs dialog
1129     if (!exportDialogInstance)
1130         exportDialogInstance =
1131              Inkscape::UI::Dialog::FileExportDialog::create(
1132                  export_path,
1133                  Inkscape::UI::Dialog::EXPORT_TYPES,
1134                  (char const *) _("Select file to export to"),
1135                  default_extension
1136             );
1138     bool success = exportDialogInstance->show();
1139     if (!success)
1140         return success;
1142     Glib::ustring fileName = exportDialogInstance->getFilename();
1144     Inkscape::Extension::Extension *selectionType =
1145         exportDialogInstance->getSelectionType();
1148     if (fileName.size() > 0) {
1149         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1151         if ( newFileName.size()>0 )
1152             fileName = newFileName;
1153         else
1154             g_warning( "Error converting save filename to UTF-8." );
1156         success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1158         if (success)
1159             prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
1161         export_path = fileName;
1162         prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
1164         return success;
1165     }
1168     return false;
1177 #else
1181 /**
1182  *
1183  */
1184 bool
1185 sp_file_export_dialog(void *widget)
1187     sp_export_dialog();
1188     return true;
1191 #endif
1193 /*######################
1194 ## E X P O R T  T O  O C A L
1195 ######################*/
1198 /**
1199  * Export the current document to OCAL
1200  */
1203 /**
1204  *  Display an Export dialog, export as the selected type if OK pressed
1205  */
1206 bool
1207 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1209     //# temp hack for 'doc' until we can switch to this dialog
1210     
1211     if (!SP_ACTIVE_DOCUMENT)
1212         return false;
1214     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1216     Glib::ustring export_path; 
1217     Glib::ustring export_loc; 
1218     Glib::ustring fileName;
1219     Inkscape::Extension::Extension *selectionType;
1221     bool success = false;
1222     
1223     static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1224     
1225     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1226     // Verify whether the document is saved, so save this as temporary
1228     char *str = (char *) repr->attribute("sodipodi:modified");
1229     if ((!doc->uri) && (!str))
1230         return false;
1233     Inkscape::Extension::Output *extension;
1234         
1235     //# Get the default extension name
1236     Glib::ustring default_extension;
1237     char *attr = (char *)repr->attribute("inkscape:output_extension");
1238     if (!attr)
1239         attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1240     if (attr)
1241         default_extension = attr;
1242     
1243     char formatBuf[256];
1244     
1245     Glib::ustring filename_extension = ".svg";
1246     extension = dynamic_cast<Inkscape::Extension::Output *>
1247         (Inkscape::Extension::db.get(default_extension.c_str()));
1248     if (extension)
1249         filename_extension = extension->get_extension();
1250         
1251     export_path = Glib::get_tmp_dir ();
1252         
1253     export_loc = export_path;
1254     export_loc.append(G_DIR_SEPARATOR_S);
1255     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1256     export_loc.append(formatBuf);
1257     
1258     // convert save_loc from utf-8 to locale
1259     // is this needed any more, now that everything is handled in
1260     // Inkscape::IO?
1261     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1262     if ( export_path_local.size() > 0) 
1263         export_path = export_path_local;
1264         
1265     //# Show the Export To OCAL dialog
1266     // MADE A CHANGE TO SUBMIT CHANGES. IN THE FUTURE WILL CALL FileExportToOCALDialog
1267     if (!exportDialogInstance)
1268         exportDialogInstance = Inkscape::UI::Dialog::FileExportDialog::create(
1269                 parentWindow,
1270                 export_path,
1271                 Inkscape::UI::Dialog::EXPORT_TYPES,
1272                 (char const *) _("Select file to export to"),
1273                 default_extension
1274                 );
1275         
1276     success = exportDialogInstance->show();
1277     if (!success)
1278         return success;
1279     
1280     fileName = exportDialogInstance->getFilename();
1281     
1282     selectionType = exportDialogInstance->getSelectionType();
1283         
1284     if (fileName.size() > 0) {
1285         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1286             
1287         if ( newFileName.size()>0 )
1288             fileName = newFileName;
1289         else
1290             g_warning( "Error converting save filename to UTF-8." );
1291     }
1292     Glib::ustring filePath = export_path;
1293     filePath.append(G_DIR_SEPARATOR_S);
1294     filePath.append(Glib::path_get_basename(fileName));
1295     
1296     fileName = filePath;    
1297     
1298     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1300     if (!success){
1301         g_warning( "Error saving a temporary copy." );
1302         return success;
1303     }
1304     
1305     /* Start now the submition */
1306     
1307     // Create the uri
1308     Glib::ustring uri = "dav://";
1309     char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1310     char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str"); 
1311     if ((username != NULL) && (password != NULL)){
1312         uri.append(username);
1313         uri.append(":");
1314         uri.append(password);
1315         uri.append("@");
1316     } 
1317     uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
1318     uri.append(Glib::path_get_basename(fileName));
1320     success = file_save(parentWindow, doc, uri, selectionType, FALSE, FALSE);
1321     remove(fileName.c_str());
1322     if (!success)
1323         g_warning( "Error exporting the document." );
1325     return success;
1329 void
1330 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1332     
1333     // Try to execute the new code and return;
1334     if (!SP_ACTIVE_DOCUMENT)
1335         return;
1336     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1337     if (success)  
1338         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1339     
1343 /*######################
1344 ## P R I N T
1345 ######################*/
1348 /**
1349  *  Print the current document, if any.
1350  */
1351 void
1352 sp_file_print()
1354     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1355     if (doc)
1356         sp_print_document(doc, FALSE);
1360 /**
1361  *  Print the current document, if any.  Do not use
1362  *  the machine's print drivers.
1363  */
1364 void
1365 sp_file_print_direct()
1367     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1368     if (doc)
1369         sp_print_document(doc, TRUE);
1373 /**
1374  * Display what the drawing would look like, if
1375  * printed.
1376  */
1377 void
1378 sp_file_print_preview(gpointer object, gpointer data)
1381     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1382     if (doc)
1383         sp_print_preview_document(doc);
1387 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1389     //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1391     if ( 0 ) {
1392         gchar const* things[] = {
1393             "data:foo,bar",
1394             "http://www.google.com/image.png",
1395             "ftp://ssd.com/doo",
1396             "/foo/dee/bar.svg",
1397             "foo.svg",
1398             "file:/foo/dee/bar.svg",
1399             "file:///foo/dee/bar.svg",
1400             "file:foo.svg",
1401             "/foo/bar\xe1\x84\x92.svg",
1402             "file:///foo/bar\xe1\x84\x92.svg",
1403             "file:///foo/bar%e1%84%92.svg",
1404             "/foo/bar%e1%84%92.svg",
1405             "bar\xe1\x84\x92.svg",
1406             "bar%e1%84%92.svg",
1407             NULL
1408         };
1409         g_message("+------");
1410         for ( int i = 0; things[i]; i++ )
1411         {
1412             try
1413             {
1414                 URI uri(things[i]);
1415                 gboolean isAbs = g_path_is_absolute( things[i] );
1416                 gchar *str = uri.toString();
1417                 g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
1418                            (int)uri.isRelative(),
1419                            uri.getScheme(),
1420                            uri.getPath(),
1421                            uri.getOpaque(),
1422                            things[i],
1423                            str );
1424                 g_free(str);
1425             }
1426             catch ( MalformedURIException err )
1427             {
1428                 dump_str( things[i], "MalformedURIException" );
1429                 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1430                 g_message("    gone from [%s] to [%s]", things[i], redo );
1431                 if ( redo == NULL )
1432                 {
1433                     URI again = URI::fromUtf8( things[i] );
1434                     gboolean isAbs = g_path_is_absolute( things[i] );
1435                     gchar *str = again.toString();
1436                     g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
1437                                (int)again.isRelative(),
1438                                again.getScheme(),
1439                                again.getPath(),
1440                                again.getOpaque(),
1441                                things[i],
1442                                str );
1443                     g_free(str);
1444                     g_message("    ----");
1445                 }
1446             }
1447         }
1448         g_message("+------");
1449     }
1451     GSList const *images = sp_document_get_resource_list(doc, "image");
1452     for (GSList const *l = images; l != NULL; l = l->next) {
1453         Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1455         const gchar *href = ir->attribute("xlink:href");
1457         // First try to figure out an absolute path to the asset
1458         //g_message("image href [%s]", href );
1459         if (spns && !g_path_is_absolute(href)) {
1460             const gchar *absref = ir->attribute("sodipodi:absref");
1461             const gchar *base_href = g_build_filename(base, href, NULL);
1462             //g_message("      absr [%s]", absref );
1464             if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1465             {
1466                 // only switch over if the absref is valid while href is not
1467                 href = absref;
1468                 //g_message("     copied absref to href");
1469             }
1470         }
1472         // Once we have an absolute path, convert it relative to the new location
1473         if (href && g_path_is_absolute(href)) {
1474             const gchar *relname = sp_relative_path_from_path(href, base);
1475             //g_message("     setting to [%s]", relname );
1476             ir->setAttribute("xlink:href", relname);
1477         }
1478 // TODO next refinement is to make the first choice keeping the relative path as-is if
1479 //      based on the new location it gives us a valid file.
1480     }
1484 /*
1485   Local Variables:
1486   mode:c++
1487   c-file-style:"stroustrup"
1488   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1489   indent-tabs-mode:nil
1490   fill-column:99
1491   End:
1492 */
1493 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :