d5700bb6e47186ada7a6c6307d9781ce802ba5e9
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 * Stephen Silver <sasilver@users.sourceforge.net>
12 *
13 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
14 * Copyright (C) 1999-2008 Authors
15 * Copyright (C) 2004 David Turner
16 * Copyright (C) 2001-2002 Ximian, Inc.
17 *
18 * Released under GNU GPL, read the file 'COPYING' for more information
19 */
21 /** @file
22 * @note This file needs to be cleaned up extensively.
23 * What it probably needs is to have one .h file for
24 * the API, and two or more .cpp files for the implementations.
25 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <gtk/gtk.h>
32 #include <glib/gmem.h>
33 #include <libnr/nr-pixops.h>
35 #include "document-private.h"
36 #include "selection-chemistry.h"
37 #include "ui/view/view-widget.h"
38 #include "dir-util.h"
39 #include "helper/png-write.h"
40 #include "dialogs/export.h"
41 #include <glibmm/i18n.h>
42 #include "inkscape.h"
43 #include "desktop.h"
44 #include "selection.h"
45 #include "interface.h"
46 #include "style.h"
47 #include "print.h"
48 #include "file.h"
49 #include "message.h"
50 #include "message-stack.h"
51 #include "ui/dialog/filedialog.h"
52 #include "ui/dialog/ocaldialogs.h"
53 #include "preferences.h"
54 #include "path-prefix.h"
56 #include "sp-namedview.h"
57 #include "desktop-handles.h"
59 #include "extension/db.h"
60 #include "extension/input.h"
61 #include "extension/output.h"
62 /* #include "extension/menu.h" */
63 #include "extension/system.h"
65 #include "io/sys.h"
66 #include "application/application.h"
67 #include "application/editor.h"
68 #include "inkscape.h"
69 #include "uri.h"
70 #include "id-clash.h"
71 #include "dialogs/rdf.h"
73 #ifdef WITH_GNOME_VFS
74 # include <libgnomevfs/gnome-vfs.h>
75 #endif
77 #ifdef WITH_INKBOARD
78 #include "jabber_whiteboard/session-manager.h"
79 #endif
81 #ifdef WIN32
82 #include <windows.h>
83 #endif
85 //#define INK_DUMP_FILENAME_CONV 1
86 #undef INK_DUMP_FILENAME_CONV
88 //#define INK_DUMP_FOPEN 1
89 #undef INK_DUMP_FOPEN
91 void dump_str(gchar const *str, gchar const *prefix);
92 void dump_ustr(Glib::ustring const &ustr);
94 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
95 static void sp_file_add_recent(gchar const *uri)
96 {
97 GtkRecentManager *recent = gtk_recent_manager_get_default();
98 gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
99 gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
100 gtk_recent_manager_add_item(recent, uri_to_add);
101 g_free(uri_to_add);
102 g_free(fn);
103 }
106 /*######################
107 ## N E W
108 ######################*/
110 /**
111 * Create a blank document and add it to the desktop
112 */
113 SPDesktop*
114 sp_file_new(const Glib::ustring &templ)
115 {
116 char *templName = NULL;
117 if (templ.size()>0)
118 templName = (char *)templ.c_str();
119 SPDocument *doc = sp_document_new(templName, TRUE, true);
120 g_return_val_if_fail(doc != NULL, NULL);
122 SPDesktop *dt;
123 if (Inkscape::NSApplication::Application::getNewGui())
124 {
125 dt = Inkscape::NSApplication::Editor::createDesktop (doc);
126 } else {
127 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
128 g_return_val_if_fail(dtw != NULL, NULL);
129 sp_document_unref(doc);
131 sp_create_window(dtw, TRUE);
132 dt = static_cast<SPDesktop*>(dtw->view);
133 sp_namedview_window_from_document(dt);
134 sp_namedview_update_layers_from_document(dt);
135 }
136 return dt;
137 }
139 SPDesktop*
140 sp_file_new_default()
141 {
142 std::list<gchar *> sources;
143 sources.push_back( profile_path("templates") ); // first try user's local dir
144 sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
146 while (!sources.empty()) {
147 gchar *dirname = sources.front();
148 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
150 // TRANSLATORS: default.svg is localizable - this is the name of the default document
151 // template. This way you can localize the default pagesize, translate the name of
152 // the default layer, etc. If you wish to localize this file, please create a
153 // localized share/templates/default.xx.svg file, where xx is your language code.
154 char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
155 if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
156 return sp_file_new(default_template);
157 }
158 }
159 g_free(dirname);
160 sources.pop_front();
161 }
163 return sp_file_new("");
164 }
167 /*######################
168 ## D E L E T E
169 ######################*/
171 /**
172 * Perform document closures preceding an exit()
173 */
174 void
175 sp_file_exit()
176 {
177 sp_ui_close_all();
178 // no need to call inkscape_exit here; last document being closed will take care of that
179 }
182 /*######################
183 ## O P E N
184 ######################*/
186 /**
187 * Open a file, add the document to the desktop
188 *
189 * \param replace_empty if true, and the current desktop is empty, this document
190 * will replace the empty one.
191 */
192 bool
193 sp_file_open(const Glib::ustring &uri,
194 Inkscape::Extension::Extension *key,
195 bool add_to_recent, bool replace_empty)
196 {
197 SPDocument *doc = NULL;
198 try {
199 doc = Inkscape::Extension::open(key, uri.c_str());
200 } catch (Inkscape::Extension::Input::no_extension_found &e) {
201 doc = NULL;
202 } catch (Inkscape::Extension::Input::open_failed &e) {
203 doc = NULL;
204 }
206 if (doc) {
207 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
208 SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
210 if (existing && existing->virgin && replace_empty) {
211 // If the current desktop is empty, open the document there
212 sp_document_ensure_up_to_date (doc);
213 desktop->change_document(doc);
214 sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
215 } else {
216 if (!Inkscape::NSApplication::Application::getNewGui()) {
217 // create a whole new desktop and window
218 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
219 sp_create_window(dtw, TRUE);
220 desktop = static_cast<SPDesktop*>(dtw->view);
221 } else {
222 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
223 }
224 }
226 doc->virgin = FALSE;
227 // everyone who cares now has a reference, get rid of ours
228 sp_document_unref(doc);
229 // resize the window to match the document properties
230 sp_namedview_window_from_document(desktop);
231 sp_namedview_update_layers_from_document(desktop);
233 if (add_to_recent) {
234 sp_file_add_recent(SP_DOCUMENT_URI(doc));
235 }
237 return TRUE;
238 } else {
239 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
240 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
241 sp_ui_error_dialog(text);
242 g_free(text);
243 g_free(safeUri);
244 return FALSE;
245 }
246 }
248 /**
249 * Handle prompting user for "do you want to revert"? Revert on "OK"
250 */
251 void
252 sp_file_revert_dialog()
253 {
254 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
255 g_assert(desktop != NULL);
257 SPDocument *doc = sp_desktop_document(desktop);
258 g_assert(doc != NULL);
260 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
261 g_assert(repr != NULL);
263 gchar const *uri = doc->uri;
264 if (!uri) {
265 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
266 return;
267 }
269 bool do_revert = true;
270 if (doc->isModifiedSinceSave()) {
271 gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
273 bool response = desktop->warnDialog (text);
274 g_free(text);
276 if (!response) {
277 do_revert = false;
278 }
279 }
281 bool reverted;
282 if (do_revert) {
283 // Allow overwriting of current document.
284 doc->virgin = TRUE;
286 // remember current zoom and view
287 double zoom = desktop->current_zoom();
288 Geom::Point c = desktop->get_display_area().midpoint();
290 reverted = sp_file_open(uri,NULL);
291 if (reverted) {
292 // restore zoom and view
293 desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
294 }
295 } else {
296 reverted = false;
297 }
299 if (reverted) {
300 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
301 } else {
302 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
303 }
304 }
306 void dump_str(gchar const *str, gchar const *prefix)
307 {
308 Glib::ustring tmp;
309 tmp = prefix;
310 tmp += " [";
311 size_t const total = strlen(str);
312 for (unsigned i = 0; i < total; i++) {
313 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
314 tmp += tmp2;
315 g_free(tmp2);
316 }
318 tmp += "]";
319 g_message("%s", tmp.c_str());
320 }
322 void dump_ustr(Glib::ustring const &ustr)
323 {
324 char const *cstr = ustr.c_str();
325 char const *data = ustr.data();
326 Glib::ustring::size_type const byteLen = ustr.bytes();
327 Glib::ustring::size_type const dataLen = ustr.length();
328 Glib::ustring::size_type const cstrLen = strlen(cstr);
330 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
331 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
332 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
333 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
335 try {
336 Glib::ustring tmp;
337 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
338 tmp = " ";
339 if (i < dataLen) {
340 Glib::ustring::value_type val = ustr.at(i);
341 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
342 tmp += tmp2;
343 g_free( tmp2 );
344 } else {
345 tmp += " ";
346 }
348 if (i < byteLen) {
349 int val = (0x0ff & data[i]);
350 gchar *tmp2 = g_strdup_printf(" %02x", val);
351 tmp += tmp2;
352 g_free( tmp2 );
353 if ( val > 32 && val < 127 ) {
354 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
355 tmp += tmp2;
356 g_free( tmp2 );
357 } else {
358 tmp += " . ";
359 }
360 } else {
361 tmp += " ";
362 }
364 if ( i < cstrLen ) {
365 int val = (0x0ff & cstr[i]);
366 gchar* tmp2 = g_strdup_printf(" %02x", val);
367 tmp += tmp2;
368 g_free(tmp2);
369 if ( val > 32 && val < 127 ) {
370 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
371 tmp += tmp2;
372 g_free( tmp2 );
373 } else {
374 tmp += " . ";
375 }
376 } else {
377 tmp += " ";
378 }
380 g_message( "%s", tmp.c_str() );
381 }
382 } catch (...) {
383 g_message("XXXXXXXXXXXXXXXXXX Exception" );
384 }
385 g_message("---------------");
386 }
388 /**
389 * Display an file Open selector. Open a document if OK is pressed.
390 * Can select single or multiple files for opening.
391 */
392 void
393 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
394 {
395 //# Get the current directory for finding files
396 static Glib::ustring open_path;
397 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
399 if(open_path.empty())
400 {
401 Glib::ustring attr = prefs->getString("/dialogs/open/path");
402 if (!attr.empty()) open_path = attr;
403 }
405 //# Test if the open_path directory exists
406 if (!Inkscape::IO::file_test(open_path.c_str(),
407 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
408 open_path = "";
410 #ifdef WIN32
411 //# If no open path, default to our win32 documents folder
412 if (open_path.empty())
413 {
414 // The path to the My Documents folder is read from the
415 // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
416 HKEY key = NULL;
417 if(RegOpenKeyExA(HKEY_CURRENT_USER,
418 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
419 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
420 {
421 WCHAR utf16path[_MAX_PATH];
422 DWORD value_type;
423 DWORD data_size = sizeof(utf16path);
424 if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
425 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
426 {
427 g_assert(value_type == REG_SZ);
428 gchar *utf8path = g_utf16_to_utf8(
429 (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
430 if(utf8path)
431 {
432 open_path = Glib::ustring(utf8path);
433 g_free(utf8path);
434 }
435 }
436 }
437 }
438 #endif
440 //# If no open path, default to our home directory
441 if (open_path.empty())
442 {
443 open_path = g_get_home_dir();
444 open_path.append(G_DIR_SEPARATOR_S);
445 }
447 //# Create a dialog if we don't already have one
448 Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
449 Inkscape::UI::Dialog::FileOpenDialog::create(
450 parentWindow, open_path,
451 Inkscape::UI::Dialog::SVG_TYPES,
452 _("Select file to open"));
454 //# Show the dialog
455 bool const success = openDialogInstance->show();
457 //# Save the folder the user selected for later
458 open_path = openDialogInstance->getCurrentDirectory();
460 if (!success)
461 {
462 delete openDialogInstance;
463 return;
464 }
466 //# User selected something. Get name and type
467 Glib::ustring fileName = openDialogInstance->getFilename();
469 Inkscape::Extension::Extension *selection =
470 openDialogInstance->getSelectionType();
472 //# Code to check & open if multiple files.
473 std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
475 //# We no longer need the file dialog object - delete it
476 delete openDialogInstance;
477 openDialogInstance = NULL;
479 //# Iterate through filenames if more than 1
480 if (flist.size() > 1)
481 {
482 for (unsigned int i = 0; i < flist.size(); i++)
483 {
484 fileName = flist[i];
486 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
487 if ( newFileName.size() > 0 )
488 fileName = newFileName;
489 else
490 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
492 #ifdef INK_DUMP_FILENAME_CONV
493 g_message("Opening File %s\n", fileName.c_str());
494 #endif
495 sp_file_open(fileName, selection);
496 }
498 return;
499 }
502 if (!fileName.empty())
503 {
504 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
506 if ( newFileName.size() > 0)
507 fileName = newFileName;
508 else
509 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
511 open_path = Glib::path_get_dirname (fileName);
512 open_path.append(G_DIR_SEPARATOR_S);
513 prefs->setString("/dialogs/open/path", open_path);
515 sp_file_open(fileName, selection);
516 }
518 return;
519 }
522 /*######################
523 ## V A C U U M
524 ######################*/
526 /**
527 * Remove unreferenced defs from the defs section of the document.
528 */
531 void
532 sp_file_vacuum()
533 {
534 SPDocument *doc = SP_ACTIVE_DOCUMENT;
536 unsigned int diff = vacuum_document (doc);
538 sp_document_done(doc, SP_VERB_FILE_VACUUM,
539 _("Vacuum <defs>"));
541 SPDesktop *dt = SP_ACTIVE_DESKTOP;
542 if (diff > 0) {
543 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
544 ngettext("Removed <b>%i</b> unused definition in <defs>.",
545 "Removed <b>%i</b> unused definitions in <defs>.",
546 diff),
547 diff);
548 } else {
549 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
550 }
551 }
555 /*######################
556 ## S A V E
557 ######################*/
559 /**
560 * This 'save' function called by the others below
561 *
562 * \param official whether to set :output_module and :modified in the
563 * document; is true for normal save, false for temporary saves
564 */
565 static bool
566 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
567 Inkscape::Extension::Extension *key, bool saveas, bool official)
568 {
569 if (!doc || uri.size()<1) //Safety check
570 return false;
572 try {
573 Inkscape::Extension::save(key, doc, uri.c_str(),
574 false,
575 saveas, official);
576 } catch (Inkscape::Extension::Output::no_extension_found &e) {
577 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
578 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
579 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
580 sp_ui_error_dialog(text);
581 g_free(text);
582 g_free(safeUri);
583 return FALSE;
584 } catch (Inkscape::Extension::Output::save_failed &e) {
585 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
586 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
587 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
588 sp_ui_error_dialog(text);
589 g_free(text);
590 g_free(safeUri);
591 return FALSE;
592 } catch (Inkscape::Extension::Output::no_overwrite &e) {
593 return sp_file_save_dialog(parentWindow, doc);
594 }
596 SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
597 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
598 return true;
599 }
601 /*
602 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
603 */
604 bool
605 file_save_remote(SPDocument */*doc*/,
606 #ifdef WITH_GNOME_VFS
607 const Glib::ustring &uri,
608 #else
609 const Glib::ustring &/*uri*/,
610 #endif
611 Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
612 {
613 #ifdef WITH_GNOME_VFS
615 #define BUF_SIZE 8192
616 gnome_vfs_init();
618 GnomeVFSHandle *from_handle = NULL;
619 GnomeVFSHandle *to_handle = NULL;
620 GnomeVFSFileSize bytes_read;
621 GnomeVFSFileSize bytes_written;
622 GnomeVFSResult result;
623 guint8 buffer[8192];
625 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
627 if ( uri_local == NULL ) {
628 g_warning( "Error converting filename to locale encoding.");
629 }
631 // Gets the temp file name.
632 Glib::ustring fileName = Glib::get_tmp_dir ();
633 fileName.append(G_DIR_SEPARATOR_S);
634 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
636 // Open the temp file to send.
637 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
639 if (result != GNOME_VFS_OK) {
640 g_warning("Could not find the temp saving.");
641 return false;
642 }
644 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
645 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
647 if (result != GNOME_VFS_OK) {
648 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
649 return false;
650 }
652 while (1) {
654 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
656 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
657 result = gnome_vfs_close (from_handle);
658 result = gnome_vfs_close (to_handle);
659 return true;
660 }
662 if (result != GNOME_VFS_OK) {
663 g_warning("%s", gnome_vfs_result_to_string(result));
664 return false;
665 }
666 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
667 if (result != GNOME_VFS_OK) {
668 g_warning("%s", gnome_vfs_result_to_string(result));
669 return false;
670 }
673 if (bytes_read != bytes_written){
674 return false;
675 }
677 }
678 return true;
679 #else
680 // in case we do not have GNOME_VFS
681 return false;
682 #endif
684 }
687 /**
688 * Display a SaveAs dialog. Save the document if OK pressed.
689 *
690 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
691 */
692 bool
693 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
694 {
696 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
697 Inkscape::Extension::Output *extension = 0;
698 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
700 //# Get the default extension name
701 Glib::ustring default_extension;
702 char *attr = (char *)repr->attribute("inkscape:output_extension");
703 if (!attr) {
704 Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
705 if(!attr2.empty()) default_extension = attr2;
706 } else {
707 default_extension = attr;
708 }
709 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
711 Glib::ustring save_path;
712 Glib::ustring save_loc;
714 if (doc->uri == NULL) {
715 char formatBuf[256];
716 int i = 1;
718 Glib::ustring filename_extension = ".svg";
719 extension = dynamic_cast<Inkscape::Extension::Output *>
720 (Inkscape::Extension::db.get(default_extension.c_str()));
721 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
722 if (extension)
723 filename_extension = extension->get_extension();
725 Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
726 if (!attr3.empty())
727 save_path = attr3;
729 if (!Inkscape::IO::file_test(save_path.c_str(),
730 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
731 save_path = "";
733 if (save_path.size()<1)
734 save_path = g_get_home_dir();
736 save_loc = save_path;
737 save_loc.append(G_DIR_SEPARATOR_S);
738 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
739 save_loc.append(formatBuf);
741 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
742 save_loc = save_path;
743 save_loc.append(G_DIR_SEPARATOR_S);
744 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
745 save_loc.append(formatBuf);
746 }
747 } else {
748 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
749 Glib::path_get_basename(doc->uri));
750 }
752 // convert save_loc from utf-8 to locale
753 // is this needed any more, now that everything is handled in
754 // Inkscape::IO?
755 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
757 if ( save_loc_local.size() > 0)
758 save_loc = save_loc_local;
760 //# Show the SaveAs dialog
761 char const * dialog_title;
762 if (is_copy) {
763 dialog_title = (char const *) _("Select file to save a copy to");
764 } else {
765 dialog_title = (char const *) _("Select file to save to");
766 }
767 gchar* doc_title = doc->root->title();
768 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
769 Inkscape::UI::Dialog::FileSaveDialog::create(
770 parentWindow,
771 save_loc,
772 Inkscape::UI::Dialog::SVG_TYPES,
773 dialog_title,
774 default_extension,
775 doc_title ? doc_title : ""
776 );
778 saveDialog->setSelectionType(extension);
780 bool success = saveDialog->show();
781 if (!success) {
782 delete saveDialog;
783 return success;
784 }
786 // set new title here (call RDF to ensure metadata and title element are updated)
787 rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
788 // free up old string
789 if(doc_title) g_free(doc_title);
791 Glib::ustring fileName = saveDialog->getFilename();
792 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
794 delete saveDialog;
796 saveDialog = 0;
798 if (fileName.size() > 0) {
799 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
801 if ( newFileName.size()>0 )
802 fileName = newFileName;
803 else
804 g_warning( "Error converting save filename to UTF-8." );
806 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
808 if (success) {
809 sp_file_add_recent(SP_DOCUMENT_URI(doc));
810 }
812 save_path = Glib::path_get_dirname(fileName);
813 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
814 prefs->setString("/dialogs/save_as/path", save_path);
816 return success;
817 }
820 return false;
821 }
824 /**
825 * Save a document, displaying a SaveAs dialog if necessary.
826 */
827 bool
828 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
829 {
830 bool success = true;
832 if (doc->isModifiedSinceSave()) {
833 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
834 if ( doc->uri == NULL
835 || repr->attribute("inkscape:output_extension") == NULL )
836 {
837 return sp_file_save_dialog(parentWindow, doc, FALSE);
838 } else {
839 gchar const *fn = g_strdup(doc->uri);
840 gchar const *ext = repr->attribute("inkscape:output_extension");
841 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
842 g_free((void *) fn);
843 }
844 } else {
845 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
846 success = TRUE;
847 }
849 return success;
850 }
853 /**
854 * Save a document.
855 */
856 bool
857 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
858 {
859 if (!SP_ACTIVE_DOCUMENT)
860 return false;
862 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
864 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
865 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
866 }
869 /**
870 * Save a document, always displaying the SaveAs dialog.
871 */
872 bool
873 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
874 {
875 if (!SP_ACTIVE_DOCUMENT)
876 return false;
877 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
878 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
879 }
883 /**
884 * Save a copy of a document, always displaying a sort of SaveAs dialog.
885 */
886 bool
887 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
888 {
889 if (!SP_ACTIVE_DOCUMENT)
890 return false;
891 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
892 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
893 }
896 /*######################
897 ## I M P O R T
898 ######################*/
900 /**
901 * Import a resource. Called by sp_file_import()
902 */
903 void
904 file_import(SPDocument *in_doc, const Glib::ustring &uri,
905 Inkscape::Extension::Extension *key)
906 {
907 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
909 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
910 SPDocument *doc;
911 try {
912 doc = Inkscape::Extension::open(key, uri.c_str());
913 } catch (Inkscape::Extension::Input::no_extension_found &e) {
914 doc = NULL;
915 } catch (Inkscape::Extension::Input::open_failed &e) {
916 doc = NULL;
917 }
919 if (doc != NULL) {
920 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
921 Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
923 prevent_id_clashes(doc, in_doc);
925 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
926 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
928 SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
930 // Count the number of top-level items in the imported document.
931 guint items_count = 0;
932 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
933 child != NULL; child = SP_OBJECT_NEXT(child))
934 {
935 if (SP_IS_ITEM(child)) items_count++;
936 }
938 // Create a new group if necessary.
939 Inkscape::XML::Node *newgroup = NULL;
940 if ((style && style->firstChild()) || items_count > 1) {
941 newgroup = xml_in_doc->createElement("svg:g");
942 sp_repr_css_set(newgroup, style, "style");
943 }
945 // Determine the place to insert the new object.
946 // This will be the current layer, if possible.
947 // FIXME: If there's no desktop (command line run?) we need
948 // a document:: method to return the current layer.
949 // For now, we just use the root in this case.
950 SPObject *place_to_insert;
951 if (desktop) place_to_insert = desktop->currentLayer();
952 else place_to_insert = SP_DOCUMENT_ROOT(in_doc);
954 // Construct a new object representing the imported image,
955 // and insert it into the current document.
956 SPObject *new_obj = NULL;
957 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
958 child != NULL; child = SP_OBJECT_NEXT(child) )
959 {
960 if (SP_IS_ITEM(child)) {
961 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
963 // convert layers to groups, and make sure they are unlocked
964 // FIXME: add "preserve layers" mode where each layer from
965 // import is copied to the same-named layer in host
966 newitem->setAttribute("inkscape:groupmode", NULL);
967 newitem->setAttribute("sodipodi:insensitive", NULL);
969 if (newgroup) newgroup->appendChild(newitem);
970 else new_obj = place_to_insert->appendChildRepr(newitem);
971 }
973 // don't lose top-level defs or style elements
974 else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
975 const gchar *tag = SP_OBJECT_REPR(child)->name();
976 if (!strcmp(tag, "svg:defs")) {
977 for (SPObject *x = sp_object_first_child(child);
978 x != NULL; x = SP_OBJECT_NEXT(x))
979 {
980 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
981 }
982 }
983 else if (!strcmp(tag, "svg:style")) {
984 SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
985 }
986 }
987 }
988 if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
990 // release some stuff
991 if (newgroup) Inkscape::GC::release(newgroup);
992 if (style) sp_repr_css_attr_unref(style);
994 // select and move the imported item
995 if (new_obj && SP_IS_ITEM(new_obj)) {
996 Inkscape::Selection *selection = sp_desktop_selection(desktop);
997 selection->set(SP_ITEM(new_obj));
999 // To move the imported object, we must temporarily set the "transform pattern with
1000 // object" option.
1001 {
1002 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1003 bool const saved_pref = prefs->getBool("/options/transform/pattern", true);
1004 prefs->setBool("/options/transform/pattern", true);
1005 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1006 boost::optional<Geom::Rect> sel_bbox = selection->bounds();
1007 if (sel_bbox) {
1008 Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1009 sp_selection_move_relative(selection, m);
1010 }
1011 prefs->setBool("/options/transform/pattern", saved_pref);
1012 }
1013 }
1015 sp_document_unref(doc);
1016 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1017 _("Import"));
1019 } else {
1020 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1021 sp_ui_error_dialog(text);
1022 g_free(text);
1023 }
1025 return;
1026 }
1029 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
1031 /**
1032 * Display an Open dialog, import a resource if OK pressed.
1033 */
1034 void
1035 sp_file_import(Gtk::Window &parentWindow)
1036 {
1037 static Glib::ustring import_path;
1039 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1040 if (!doc)
1041 return;
1043 if (!importDialogInstance) {
1044 importDialogInstance =
1045 Inkscape::UI::Dialog::FileOpenDialog::create(
1046 parentWindow,
1047 import_path,
1048 Inkscape::UI::Dialog::IMPORT_TYPES,
1049 (char const *)_("Select file to import"));
1050 }
1052 bool success = importDialogInstance->show();
1053 if (!success)
1054 return;
1056 //# Get file name and extension type
1057 Glib::ustring fileName = importDialogInstance->getFilename();
1058 Inkscape::Extension::Extension *selection =
1059 importDialogInstance->getSelectionType();
1062 if (fileName.size() > 0) {
1064 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1066 if ( newFileName.size() > 0)
1067 fileName = newFileName;
1068 else
1069 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1072 import_path = fileName;
1073 if (import_path.size()>0)
1074 import_path.append(G_DIR_SEPARATOR_S);
1076 file_import(doc, fileName, selection);
1077 }
1079 return;
1080 }
1084 /*######################
1085 ## E X P O R T
1086 ######################*/
1088 //#define NEW_EXPORT_DIALOG
1092 #ifdef NEW_EXPORT_DIALOG
1094 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1096 /**
1097 * Display an Export dialog, export as the selected type if OK pressed
1098 */
1099 bool
1100 sp_file_export_dialog(void *widget)
1101 {
1102 //# temp hack for 'doc' until we can switch to this dialog
1103 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1105 Glib::ustring export_path;
1106 Glib::ustring export_loc;
1108 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1109 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1110 Inkscape::Extension::Output *extension;
1112 //# Get the default extension name
1113 Glib::ustring default_extension;
1114 char *attr = (char *)repr->attribute("inkscape:output_extension");
1115 if (!attr) {
1116 Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
1117 if(!attr2.empty()) default_extension = attr2;
1118 } else {
1119 default_extension = attr;
1120 }
1121 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1123 if (doc->uri == NULL)
1124 {
1125 char formatBuf[256];
1127 Glib::ustring filename_extension = ".svg";
1128 extension = dynamic_cast<Inkscape::Extension::Output *>
1129 (Inkscape::Extension::db.get(default_extension.c_str()));
1130 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1131 if (extension)
1132 filename_extension = extension->get_extension();
1134 Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
1135 if (!attr3.empty())
1136 export_path = attr3;
1138 if (!Inkscape::IO::file_test(export_path.c_str(),
1139 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1140 export_path = "";
1142 if (export_path.size()<1)
1143 export_path = g_get_home_dir();
1145 export_loc = export_path;
1146 export_loc.append(G_DIR_SEPARATOR_S);
1147 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1148 export_loc.append(formatBuf);
1150 }
1151 else
1152 {
1153 export_path = Glib::path_get_dirname(doc->uri);
1154 }
1156 // convert save_loc from utf-8 to locale
1157 // is this needed any more, now that everything is handled in
1158 // Inkscape::IO?
1159 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1160 if ( export_path_local.size() > 0)
1161 export_path = export_path_local;
1163 //# Show the SaveAs dialog
1164 if (!exportDialogInstance)
1165 exportDialogInstance =
1166 Inkscape::UI::Dialog::FileExportDialog::create(
1167 export_path,
1168 Inkscape::UI::Dialog::EXPORT_TYPES,
1169 (char const *) _("Select file to export to"),
1170 default_extension
1171 );
1173 bool success = exportDialogInstance->show();
1174 if (!success)
1175 return success;
1177 Glib::ustring fileName = exportDialogInstance->getFilename();
1179 Inkscape::Extension::Extension *selectionType =
1180 exportDialogInstance->getSelectionType();
1183 if (fileName.size() > 0) {
1184 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1186 if ( newFileName.size()>0 )
1187 fileName = newFileName;
1188 else
1189 g_warning( "Error converting save filename to UTF-8." );
1191 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1193 if (success) {
1194 Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1195 recent->add_item(SP_DOCUMENT_URI(doc));
1196 }
1198 export_path = fileName;
1199 prefs->setString("/dialogs/save_as/path", export_path);
1201 return success;
1202 }
1205 return false;
1206 }
1208 #else
1210 /**
1211 *
1212 */
1213 bool
1214 sp_file_export_dialog(void */*widget*/)
1215 {
1216 sp_export_dialog();
1217 return true;
1218 }
1220 #endif
1222 /*######################
1223 ## E X P O R T T O O C A L
1224 ######################*/
1226 /**
1227 * Display an Export dialog, export as the selected type if OK pressed
1228 */
1229 bool
1230 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1231 {
1233 if (!SP_ACTIVE_DOCUMENT)
1234 return false;
1236 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1238 Glib::ustring export_path;
1239 Glib::ustring export_loc;
1240 Glib::ustring fileName;
1241 Inkscape::Extension::Extension *selectionType;
1243 bool success = false;
1245 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1246 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1247 static bool gotSuccess = false;
1249 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1250 (void)repr;
1252 if (!doc->uri && !doc->isModifiedSinceSave())
1253 return false;
1255 // Get the default extension name
1256 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1257 char formatBuf[256];
1259 Glib::ustring filename_extension = ".svg";
1260 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1262 export_path = Glib::get_tmp_dir ();
1264 export_loc = export_path;
1265 export_loc.append(G_DIR_SEPARATOR_S);
1266 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1267 export_loc.append(formatBuf);
1269 // convert save_loc from utf-8 to locale
1270 // is this needed any more, now that everything is handled in
1271 // Inkscape::IO?
1272 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1273 if ( export_path_local.size() > 0)
1274 export_path = export_path_local;
1276 // Show the Export To OCAL dialog
1277 if (!exportDialogInstance)
1278 exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
1279 parentWindow,
1280 Inkscape::UI::Dialog::EXPORT_TYPES,
1281 (char const *) _("Select file to export to")
1282 );
1284 success = exportDialogInstance->show();
1285 if (!success)
1286 return success;
1288 fileName = exportDialogInstance->getFilename();
1290 fileName.append(filename_extension.c_str());
1291 if (fileName.size() > 0) {
1292 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1294 if ( newFileName.size()>0 )
1295 fileName = newFileName;
1296 else
1297 g_warning( "Error converting save filename to UTF-8." );
1298 }
1299 Glib::ustring filePath = export_path;
1300 filePath.append(G_DIR_SEPARATOR_S);
1301 filePath.append(Glib::path_get_basename(fileName));
1303 fileName = filePath;
1305 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1307 if (!success){
1308 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1309 sp_ui_error_dialog(text);
1311 return success;
1312 }
1314 // Start now the submition
1316 // Create the uri
1317 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1318 Glib::ustring uri = "dav://";
1319 Glib::ustring username = prefs->getString("/options/ocalusername/str");
1320 Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1321 if (username.empty() || password.empty())
1322 {
1323 if(!gotSuccess)
1324 {
1325 if (!exportPasswordDialogInstance)
1326 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1327 parentWindow,
1328 (char const *) _("Open Clip Art Login"));
1329 success = exportPasswordDialogInstance->show();
1330 if (!success)
1331 return success;
1332 }
1333 username = exportPasswordDialogInstance->getUsername();
1334 password = exportPasswordDialogInstance->getPassword();
1335 }
1336 uri.append(username);
1337 uri.append(":");
1338 uri.append(password);
1339 uri.append("@");
1340 uri.append(prefs->getString("/options/ocalurl/str"));
1341 uri.append("/dav.php/");
1342 uri.append(Glib::path_get_basename(fileName));
1344 // Save as a remote file using the dav protocol.
1345 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1346 remove(fileName.c_str());
1347 if (!success)
1348 {
1349 gchar *text = g_strdup_printf(_("Error exporting the document. Verify if the server name, username and password are correct, if the server has support for webdav and verify if you didn't forget to choose a license."));
1350 sp_ui_error_dialog(text);
1351 }
1352 else
1353 gotSuccess = true;
1355 return success;
1356 }
1358 /**
1359 * Export the current document to OCAL
1360 */
1361 void
1362 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1363 {
1365 // Try to execute the new code and return;
1366 if (!SP_ACTIVE_DOCUMENT)
1367 return;
1368 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1369 if (success)
1370 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1371 }
1374 /*######################
1375 ## I M P O R T F R O M O C A L
1376 ######################*/
1378 /**
1379 * Display an ImportToOcal Dialog, and the selected document from OCAL
1380 */
1381 void
1382 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1383 {
1384 static Glib::ustring import_path;
1386 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1387 if (!doc)
1388 return;
1390 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1392 if (!importDialogInstance) {
1393 importDialogInstance = new
1394 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1395 parentWindow,
1396 import_path,
1397 Inkscape::UI::Dialog::IMPORT_TYPES,
1398 (char const *)_("Import From Open Clip Art Library"));
1399 }
1401 bool success = importDialogInstance->show();
1402 if (!success)
1403 return;
1405 // Get file name and extension type
1406 Glib::ustring fileName = importDialogInstance->getFilename();
1407 Inkscape::Extension::Extension *selection =
1408 importDialogInstance->getSelectionType();
1410 if (fileName.size() > 0) {
1412 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1414 if ( newFileName.size() > 0)
1415 fileName = newFileName;
1416 else
1417 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1419 import_path = fileName;
1420 if (import_path.size()>0)
1421 import_path.append(G_DIR_SEPARATOR_S);
1423 file_import(doc, fileName, selection);
1424 }
1426 return;
1427 }
1429 /*######################
1430 ## P R I N T
1431 ######################*/
1434 /**
1435 * Print the current document, if any.
1436 */
1437 void
1438 sp_file_print(Gtk::Window& parentWindow)
1439 {
1440 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1441 if (doc)
1442 sp_print_document(parentWindow, doc);
1443 }
1445 /**
1446 * Display what the drawing would look like, if
1447 * printed.
1448 */
1449 void
1450 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1451 {
1453 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1454 if (doc)
1455 sp_print_preview_document(doc);
1457 }
1459 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1460 {
1461 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1463 if ( 0 ) {
1464 gchar const* things[] = {
1465 "data:foo,bar",
1466 "http://www.google.com/image.png",
1467 "ftp://ssd.com/doo",
1468 "/foo/dee/bar.svg",
1469 "foo.svg",
1470 "file:/foo/dee/bar.svg",
1471 "file:///foo/dee/bar.svg",
1472 "file:foo.svg",
1473 "/foo/bar\xe1\x84\x92.svg",
1474 "file:///foo/bar\xe1\x84\x92.svg",
1475 "file:///foo/bar%e1%84%92.svg",
1476 "/foo/bar%e1%84%92.svg",
1477 "bar\xe1\x84\x92.svg",
1478 "bar%e1%84%92.svg",
1479 NULL
1480 };
1481 g_message("+------");
1482 for ( int i = 0; things[i]; i++ )
1483 {
1484 try
1485 {
1486 URI uri(things[i]);
1487 gboolean isAbs = g_path_is_absolute( things[i] );
1488 gchar *str = uri.toString();
1489 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1490 (int)uri.isRelative(),
1491 uri.getScheme(),
1492 uri.getPath(),
1493 uri.getOpaque(),
1494 things[i],
1495 str );
1496 g_free(str);
1497 }
1498 catch ( MalformedURIException err )
1499 {
1500 dump_str( things[i], "MalformedURIException" );
1501 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1502 g_message(" gone from [%s] to [%s]", things[i], redo );
1503 if ( redo == NULL )
1504 {
1505 URI again = URI::fromUtf8( things[i] );
1506 gboolean isAbs = g_path_is_absolute( things[i] );
1507 gchar *str = again.toString();
1508 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1509 (int)again.isRelative(),
1510 again.getScheme(),
1511 again.getPath(),
1512 again.getOpaque(),
1513 things[i],
1514 str );
1515 g_free(str);
1516 g_message(" ----");
1517 }
1518 }
1519 }
1520 g_message("+------");
1521 }
1523 GSList const *images = sp_document_get_resource_list(doc, "image");
1524 for (GSList const *l = images; l != NULL; l = l->next) {
1525 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1527 const gchar *href = ir->attribute("xlink:href");
1529 // First try to figure out an absolute path to the asset
1530 //g_message("image href [%s]", href );
1531 if (spns && !g_path_is_absolute(href)) {
1532 const gchar *absref = ir->attribute("sodipodi:absref");
1533 const gchar *base_href = g_build_filename(base, href, NULL);
1534 //g_message(" absr [%s]", absref );
1536 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1537 {
1538 // only switch over if the absref is valid while href is not
1539 href = absref;
1540 //g_message(" copied absref to href");
1541 }
1542 }
1544 // Once we have an absolute path, convert it relative to the new location
1545 if (href && g_path_is_absolute(href)) {
1546 const gchar *relname = sp_relative_path_from_path(href, base);
1547 //g_message(" setting to [%s]", relname );
1548 ir->setAttribute("xlink:href", relname);
1549 }
1550 // TODO next refinement is to make the first choice keeping the relative path as-is if
1551 // based on the new location it gives us a valid file.
1552 }
1553 }
1556 /*
1557 Local Variables:
1558 mode:c++
1559 c-file-style:"stroustrup"
1560 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1561 indent-tabs-mode:nil
1562 fill-column:99
1563 End:
1564 */
1565 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :