5c729eeeb65b1032a8c61cb0ca255d6bdbcad34a
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 if (fn) {
100 gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
101 if (uri_to_add) {
102 gtk_recent_manager_add_item(recent, uri_to_add);
103 g_free(uri_to_add);
104 }
105 g_free(fn);
106 }
107 }
110 /*######################
111 ## N E W
112 ######################*/
114 /**
115 * Create a blank document and add it to the desktop
116 */
117 SPDesktop*
118 sp_file_new(const Glib::ustring &templ)
119 {
120 char *templName = NULL;
121 if (templ.size()>0)
122 templName = (char *)templ.c_str();
123 SPDocument *doc = sp_document_new(templName, TRUE, true);
124 g_return_val_if_fail(doc != NULL, NULL);
126 SPDesktop *dt;
127 if (Inkscape::NSApplication::Application::getNewGui())
128 {
129 dt = Inkscape::NSApplication::Editor::createDesktop (doc);
130 } else {
131 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
132 g_return_val_if_fail(dtw != NULL, NULL);
133 sp_document_unref(doc);
135 sp_create_window(dtw, TRUE);
136 dt = static_cast<SPDesktop*>(dtw->view);
137 sp_namedview_window_from_document(dt);
138 sp_namedview_update_layers_from_document(dt);
139 }
140 return dt;
141 }
143 SPDesktop*
144 sp_file_new_default()
145 {
146 std::list<gchar *> sources;
147 sources.push_back( profile_path("templates") ); // first try user's local dir
148 sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
150 while (!sources.empty()) {
151 gchar *dirname = sources.front();
152 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
154 // TRANSLATORS: default.svg is localizable - this is the name of the default document
155 // template. This way you can localize the default pagesize, translate the name of
156 // the default layer, etc. If you wish to localize this file, please create a
157 // localized share/templates/default.xx.svg file, where xx is your language code.
158 char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
159 if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
160 return sp_file_new(default_template);
161 }
162 }
163 g_free(dirname);
164 sources.pop_front();
165 }
167 return sp_file_new("");
168 }
171 /*######################
172 ## D E L E T E
173 ######################*/
175 /**
176 * Perform document closures preceding an exit()
177 */
178 void
179 sp_file_exit()
180 {
181 sp_ui_close_all();
182 // no need to call inkscape_exit here; last document being closed will take care of that
183 }
186 /*######################
187 ## O P E N
188 ######################*/
190 /**
191 * Open a file, add the document to the desktop
192 *
193 * \param replace_empty if true, and the current desktop is empty, this document
194 * will replace the empty one.
195 */
196 bool
197 sp_file_open(const Glib::ustring &uri,
198 Inkscape::Extension::Extension *key,
199 bool add_to_recent, bool replace_empty)
200 {
201 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
202 if (desktop)
203 desktop->setWaitingCursor();
205 SPDocument *doc = NULL;
206 try {
207 doc = Inkscape::Extension::open(key, uri.c_str());
208 } catch (Inkscape::Extension::Input::no_extension_found &e) {
209 doc = NULL;
210 } catch (Inkscape::Extension::Input::open_failed &e) {
211 doc = NULL;
212 }
214 if (desktop)
215 desktop->clearWaitingCursor();
217 if (doc) {
218 SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
220 if (existing && existing->virgin && replace_empty) {
221 // If the current desktop is empty, open the document there
222 sp_document_ensure_up_to_date (doc);
223 desktop->change_document(doc);
224 sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
225 } else {
226 if (!Inkscape::NSApplication::Application::getNewGui()) {
227 // create a whole new desktop and window
228 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
229 sp_create_window(dtw, TRUE);
230 desktop = static_cast<SPDesktop*>(dtw->view);
231 } else {
232 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
233 }
234 }
236 doc->virgin = FALSE;
237 // everyone who cares now has a reference, get rid of ours
238 sp_document_unref(doc);
239 // resize the window to match the document properties
240 sp_namedview_window_from_document(desktop);
241 sp_namedview_update_layers_from_document(desktop);
243 if (add_to_recent) {
244 sp_file_add_recent(SP_DOCUMENT_URI(doc));
245 }
247 return TRUE;
248 } else {
249 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
250 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
251 sp_ui_error_dialog(text);
252 g_free(text);
253 g_free(safeUri);
254 return FALSE;
255 }
256 }
258 /**
259 * Handle prompting user for "do you want to revert"? Revert on "OK"
260 */
261 void
262 sp_file_revert_dialog()
263 {
264 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
265 g_assert(desktop != NULL);
267 SPDocument *doc = sp_desktop_document(desktop);
268 g_assert(doc != NULL);
270 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
271 g_assert(repr != NULL);
273 gchar const *uri = doc->uri;
274 if (!uri) {
275 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
276 return;
277 }
279 bool do_revert = true;
280 if (doc->isModifiedSinceSave()) {
281 gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
283 bool response = desktop->warnDialog (text);
284 g_free(text);
286 if (!response) {
287 do_revert = false;
288 }
289 }
291 bool reverted;
292 if (do_revert) {
293 // Allow overwriting of current document.
294 doc->virgin = TRUE;
296 // remember current zoom and view
297 double zoom = desktop->current_zoom();
298 Geom::Point c = desktop->get_display_area().midpoint();
300 reverted = sp_file_open(uri,NULL);
301 if (reverted) {
302 // restore zoom and view
303 desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
304 }
305 } else {
306 reverted = false;
307 }
309 if (reverted) {
310 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
311 } else {
312 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
313 }
314 }
316 void dump_str(gchar const *str, gchar const *prefix)
317 {
318 Glib::ustring tmp;
319 tmp = prefix;
320 tmp += " [";
321 size_t const total = strlen(str);
322 for (unsigned i = 0; i < total; i++) {
323 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
324 tmp += tmp2;
325 g_free(tmp2);
326 }
328 tmp += "]";
329 g_message("%s", tmp.c_str());
330 }
332 void dump_ustr(Glib::ustring const &ustr)
333 {
334 char const *cstr = ustr.c_str();
335 char const *data = ustr.data();
336 Glib::ustring::size_type const byteLen = ustr.bytes();
337 Glib::ustring::size_type const dataLen = ustr.length();
338 Glib::ustring::size_type const cstrLen = strlen(cstr);
340 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
341 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
342 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
343 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
345 try {
346 Glib::ustring tmp;
347 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
348 tmp = " ";
349 if (i < dataLen) {
350 Glib::ustring::value_type val = ustr.at(i);
351 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
352 tmp += tmp2;
353 g_free( tmp2 );
354 } else {
355 tmp += " ";
356 }
358 if (i < byteLen) {
359 int val = (0x0ff & data[i]);
360 gchar *tmp2 = g_strdup_printf(" %02x", val);
361 tmp += tmp2;
362 g_free( tmp2 );
363 if ( val > 32 && val < 127 ) {
364 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
365 tmp += tmp2;
366 g_free( tmp2 );
367 } else {
368 tmp += " . ";
369 }
370 } else {
371 tmp += " ";
372 }
374 if ( i < cstrLen ) {
375 int val = (0x0ff & cstr[i]);
376 gchar* tmp2 = g_strdup_printf(" %02x", val);
377 tmp += tmp2;
378 g_free(tmp2);
379 if ( val > 32 && val < 127 ) {
380 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
381 tmp += tmp2;
382 g_free( tmp2 );
383 } else {
384 tmp += " . ";
385 }
386 } else {
387 tmp += " ";
388 }
390 g_message( "%s", tmp.c_str() );
391 }
392 } catch (...) {
393 g_message("XXXXXXXXXXXXXXXXXX Exception" );
394 }
395 g_message("---------------");
396 }
398 /**
399 * Display an file Open selector. Open a document if OK is pressed.
400 * Can select single or multiple files for opening.
401 */
402 void
403 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
404 {
405 //# Get the current directory for finding files
406 static Glib::ustring open_path;
407 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
409 if(open_path.empty())
410 {
411 Glib::ustring attr = prefs->getString("/dialogs/open/path");
412 if (!attr.empty()) open_path = attr;
413 }
415 //# Test if the open_path directory exists
416 if (!Inkscape::IO::file_test(open_path.c_str(),
417 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
418 open_path = "";
420 #ifdef WIN32
421 //# If no open path, default to our win32 documents folder
422 if (open_path.empty())
423 {
424 // The path to the My Documents folder is read from the
425 // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
426 HKEY key = NULL;
427 if(RegOpenKeyExA(HKEY_CURRENT_USER,
428 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
429 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
430 {
431 WCHAR utf16path[_MAX_PATH];
432 DWORD value_type;
433 DWORD data_size = sizeof(utf16path);
434 if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
435 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
436 {
437 g_assert(value_type == REG_SZ);
438 gchar *utf8path = g_utf16_to_utf8(
439 (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
440 if(utf8path)
441 {
442 open_path = Glib::ustring(utf8path);
443 g_free(utf8path);
444 }
445 }
446 }
447 }
448 #endif
450 //# If no open path, default to our home directory
451 if (open_path.empty())
452 {
453 open_path = g_get_home_dir();
454 open_path.append(G_DIR_SEPARATOR_S);
455 }
457 //# Create a dialog if we don't already have one
458 Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
459 Inkscape::UI::Dialog::FileOpenDialog::create(
460 parentWindow, open_path,
461 Inkscape::UI::Dialog::SVG_TYPES,
462 _("Select file to open"));
464 //# Show the dialog
465 bool const success = openDialogInstance->show();
467 //# Save the folder the user selected for later
468 open_path = openDialogInstance->getCurrentDirectory();
470 if (!success)
471 {
472 delete openDialogInstance;
473 return;
474 }
476 //# User selected something. Get name and type
477 Glib::ustring fileName = openDialogInstance->getFilename();
479 Inkscape::Extension::Extension *selection =
480 openDialogInstance->getSelectionType();
482 //# Code to check & open if multiple files.
483 std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
485 //# We no longer need the file dialog object - delete it
486 delete openDialogInstance;
487 openDialogInstance = NULL;
489 //# Iterate through filenames if more than 1
490 if (flist.size() > 1)
491 {
492 for (unsigned int i = 0; i < flist.size(); i++)
493 {
494 fileName = flist[i];
496 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
497 if ( newFileName.size() > 0 )
498 fileName = newFileName;
499 else
500 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
502 #ifdef INK_DUMP_FILENAME_CONV
503 g_message("Opening File %s\n", fileName.c_str());
504 #endif
505 sp_file_open(fileName, selection);
506 }
508 return;
509 }
512 if (!fileName.empty())
513 {
514 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
516 if ( newFileName.size() > 0)
517 fileName = newFileName;
518 else
519 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
521 open_path = Glib::path_get_dirname (fileName);
522 open_path.append(G_DIR_SEPARATOR_S);
523 prefs->setString("/dialogs/open/path", open_path);
525 sp_file_open(fileName, selection);
526 }
528 return;
529 }
532 /*######################
533 ## V A C U U M
534 ######################*/
536 /**
537 * Remove unreferenced defs from the defs section of the document.
538 */
541 void
542 sp_file_vacuum()
543 {
544 SPDocument *doc = SP_ACTIVE_DOCUMENT;
546 unsigned int diff = vacuum_document (doc);
548 sp_document_done(doc, SP_VERB_FILE_VACUUM,
549 _("Vacuum <defs>"));
551 SPDesktop *dt = SP_ACTIVE_DESKTOP;
552 if (diff > 0) {
553 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
554 ngettext("Removed <b>%i</b> unused definition in <defs>.",
555 "Removed <b>%i</b> unused definitions in <defs>.",
556 diff),
557 diff);
558 } else {
559 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
560 }
561 }
565 /*######################
566 ## S A V E
567 ######################*/
569 /**
570 * This 'save' function called by the others below
571 *
572 * \param official whether to set :output_module and :modified in the
573 * document; is true for normal save, false for temporary saves
574 */
575 static bool
576 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
577 Inkscape::Extension::Extension *key, bool saveas, bool official)
578 {
579 if (!doc || uri.size()<1) //Safety check
580 return false;
582 try {
583 Inkscape::Extension::save(key, doc, uri.c_str(),
584 false,
585 saveas, official);
586 } catch (Inkscape::Extension::Output::no_extension_found &e) {
587 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
588 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
589 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
590 sp_ui_error_dialog(text);
591 g_free(text);
592 g_free(safeUri);
593 return FALSE;
594 } catch (Inkscape::Extension::Output::save_failed &e) {
595 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
596 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
597 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
598 sp_ui_error_dialog(text);
599 g_free(text);
600 g_free(safeUri);
601 return FALSE;
602 } catch (Inkscape::Extension::Output::save_cancelled &e) {
603 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
604 return FALSE;
605 } catch (Inkscape::Extension::Output::no_overwrite &e) {
606 return sp_file_save_dialog(parentWindow, doc);
607 }
609 SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
610 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
611 return true;
612 }
614 /*
615 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
616 */
617 bool
618 file_save_remote(SPDocument */*doc*/,
619 #ifdef WITH_GNOME_VFS
620 const Glib::ustring &uri,
621 #else
622 const Glib::ustring &/*uri*/,
623 #endif
624 Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
625 {
626 #ifdef WITH_GNOME_VFS
628 #define BUF_SIZE 8192
629 gnome_vfs_init();
631 GnomeVFSHandle *from_handle = NULL;
632 GnomeVFSHandle *to_handle = NULL;
633 GnomeVFSFileSize bytes_read;
634 GnomeVFSFileSize bytes_written;
635 GnomeVFSResult result;
636 guint8 buffer[8192];
638 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
640 if ( uri_local == NULL ) {
641 g_warning( "Error converting filename to locale encoding.");
642 }
644 // Gets the temp file name.
645 Glib::ustring fileName = Glib::get_tmp_dir ();
646 fileName.append(G_DIR_SEPARATOR_S);
647 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
649 // Open the temp file to send.
650 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
652 if (result != GNOME_VFS_OK) {
653 g_warning("Could not find the temp saving.");
654 return false;
655 }
657 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
658 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
660 if (result != GNOME_VFS_OK) {
661 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
662 return false;
663 }
665 while (1) {
667 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
669 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
670 result = gnome_vfs_close (from_handle);
671 result = gnome_vfs_close (to_handle);
672 return true;
673 }
675 if (result != GNOME_VFS_OK) {
676 g_warning("%s", gnome_vfs_result_to_string(result));
677 return false;
678 }
679 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
680 if (result != GNOME_VFS_OK) {
681 g_warning("%s", gnome_vfs_result_to_string(result));
682 return false;
683 }
686 if (bytes_read != bytes_written){
687 return false;
688 }
690 }
691 return true;
692 #else
693 // in case we do not have GNOME_VFS
694 return false;
695 #endif
697 }
700 /**
701 * Display a SaveAs dialog. Save the document if OK pressed.
702 *
703 * \param ascopy (optional) wether to set the documents->uri to the new filename or not
704 */
705 bool
706 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
707 {
709 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
710 Inkscape::Extension::Output *extension = 0;
711 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
713 //# Get the default extension name
714 Glib::ustring default_extension;
715 char *attr = (char *)repr->attribute("inkscape:output_extension");
716 if (!attr) {
717 Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
718 if(!attr2.empty()) default_extension = attr2;
719 } else {
720 default_extension = attr;
721 }
722 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
724 Glib::ustring save_path;
725 Glib::ustring save_loc;
727 if (doc->uri == NULL) {
728 char formatBuf[256];
729 int i = 1;
731 Glib::ustring filename_extension = ".svg";
732 extension = dynamic_cast<Inkscape::Extension::Output *>
733 (Inkscape::Extension::db.get(default_extension.c_str()));
734 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
735 if (extension)
736 filename_extension = extension->get_extension();
738 Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
739 if (!attr3.empty())
740 save_path = attr3;
742 if (!Inkscape::IO::file_test(save_path.c_str(),
743 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
744 save_path = "";
746 if (save_path.size()<1)
747 save_path = g_get_home_dir();
749 save_loc = save_path;
750 save_loc.append(G_DIR_SEPARATOR_S);
751 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
752 save_loc.append(formatBuf);
754 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
755 save_loc = save_path;
756 save_loc.append(G_DIR_SEPARATOR_S);
757 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
758 save_loc.append(formatBuf);
759 }
760 } else {
761 save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
762 Glib::path_get_basename(doc->uri));
763 }
765 // convert save_loc from utf-8 to locale
766 // is this needed any more, now that everything is handled in
767 // Inkscape::IO?
768 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
770 if ( save_loc_local.size() > 0)
771 save_loc = save_loc_local;
773 //# Show the SaveAs dialog
774 char const * dialog_title;
775 if (is_copy) {
776 dialog_title = (char const *) _("Select file to save a copy to");
777 } else {
778 dialog_title = (char const *) _("Select file to save to");
779 }
780 gchar* doc_title = doc->root->title();
781 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
782 Inkscape::UI::Dialog::FileSaveDialog::create(
783 parentWindow,
784 save_loc,
785 Inkscape::UI::Dialog::SVG_TYPES,
786 dialog_title,
787 default_extension,
788 doc_title ? doc_title : ""
789 );
791 saveDialog->setSelectionType(extension);
793 bool success = saveDialog->show();
794 if (!success) {
795 delete saveDialog;
796 return success;
797 }
799 // set new title here (call RDF to ensure metadata and title element are updated)
800 rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
801 // free up old string
802 if(doc_title) g_free(doc_title);
804 Glib::ustring fileName = saveDialog->getFilename();
805 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
807 delete saveDialog;
809 saveDialog = 0;
811 if (fileName.size() > 0) {
812 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
814 if ( newFileName.size()>0 )
815 fileName = newFileName;
816 else
817 g_warning( "Error converting save filename to UTF-8." );
819 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
821 if (success) {
822 sp_file_add_recent(SP_DOCUMENT_URI(doc));
823 }
825 save_path = Glib::path_get_dirname(fileName);
826 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
827 prefs->setString("/dialogs/save_as/path", save_path);
829 return success;
830 }
833 return false;
834 }
837 /**
838 * Save a document, displaying a SaveAs dialog if necessary.
839 */
840 bool
841 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
842 {
843 bool success = true;
845 if (doc->isModifiedSinceSave()) {
846 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
847 if ( doc->uri == NULL
848 || repr->attribute("inkscape:output_extension") == NULL )
849 {
850 return sp_file_save_dialog(parentWindow, doc, FALSE);
851 } else {
852 gchar const *fn = g_strdup(doc->uri);
853 gchar const *ext = repr->attribute("inkscape:output_extension");
854 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
855 g_free((void *) fn);
856 }
857 } else {
858 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
859 success = TRUE;
860 }
862 return success;
863 }
866 /**
867 * Save a document.
868 */
869 bool
870 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
871 {
872 if (!SP_ACTIVE_DOCUMENT)
873 return false;
875 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
877 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
878 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
879 }
882 /**
883 * Save a document, always displaying the SaveAs dialog.
884 */
885 bool
886 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
887 {
888 if (!SP_ACTIVE_DOCUMENT)
889 return false;
890 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
891 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
892 }
896 /**
897 * Save a copy of a document, always displaying a sort of SaveAs dialog.
898 */
899 bool
900 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
901 {
902 if (!SP_ACTIVE_DOCUMENT)
903 return false;
904 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
905 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
906 }
909 /*######################
910 ## I M P O R T
911 ######################*/
913 /**
914 * Import a resource. Called by sp_file_import()
915 */
916 void
917 file_import(SPDocument *in_doc, const Glib::ustring &uri,
918 Inkscape::Extension::Extension *key)
919 {
920 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
922 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
923 SPDocument *doc;
924 try {
925 doc = Inkscape::Extension::open(key, uri.c_str());
926 } catch (Inkscape::Extension::Input::no_extension_found &e) {
927 doc = NULL;
928 } catch (Inkscape::Extension::Input::open_failed &e) {
929 doc = NULL;
930 }
932 if (doc != NULL) {
933 Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
934 Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
936 prevent_id_clashes(doc, in_doc);
938 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
939 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
941 SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
943 // Count the number of top-level items in the imported document.
944 guint items_count = 0;
945 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
946 child != NULL; child = SP_OBJECT_NEXT(child))
947 {
948 if (SP_IS_ITEM(child)) items_count++;
949 }
951 // Create a new group if necessary.
952 Inkscape::XML::Node *newgroup = NULL;
953 if ((style && style->firstChild()) || items_count > 1) {
954 newgroup = xml_in_doc->createElement("svg:g");
955 sp_repr_css_set(newgroup, style, "style");
956 }
958 // Determine the place to insert the new object.
959 // This will be the current layer, if possible.
960 // FIXME: If there's no desktop (command line run?) we need
961 // a document:: method to return the current layer.
962 // For now, we just use the root in this case.
963 SPObject *place_to_insert;
964 if (desktop) place_to_insert = desktop->currentLayer();
965 else place_to_insert = SP_DOCUMENT_ROOT(in_doc);
967 // Construct a new object representing the imported image,
968 // and insert it into the current document.
969 SPObject *new_obj = NULL;
970 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
971 child != NULL; child = SP_OBJECT_NEXT(child) )
972 {
973 if (SP_IS_ITEM(child)) {
974 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
976 // convert layers to groups, and make sure they are unlocked
977 // FIXME: add "preserve layers" mode where each layer from
978 // import is copied to the same-named layer in host
979 newitem->setAttribute("inkscape:groupmode", NULL);
980 newitem->setAttribute("sodipodi:insensitive", NULL);
982 if (newgroup) newgroup->appendChild(newitem);
983 else new_obj = place_to_insert->appendChildRepr(newitem);
984 }
986 // don't lose top-level defs or style elements
987 else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
988 const gchar *tag = SP_OBJECT_REPR(child)->name();
989 if (!strcmp(tag, "svg:defs")) {
990 for (SPObject *x = sp_object_first_child(child);
991 x != NULL; x = SP_OBJECT_NEXT(x))
992 {
993 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
994 }
995 }
996 else if (!strcmp(tag, "svg:style")) {
997 SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
998 }
999 }
1000 }
1001 if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1003 // release some stuff
1004 if (newgroup) Inkscape::GC::release(newgroup);
1005 if (style) sp_repr_css_attr_unref(style);
1007 // select and move the imported item
1008 if (new_obj && SP_IS_ITEM(new_obj)) {
1009 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1010 selection->set(SP_ITEM(new_obj));
1012 // To move the imported object, we must temporarily set the "transform pattern with
1013 // object" option.
1014 {
1015 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1016 bool const saved_pref = prefs->getBool("/options/transform/pattern", true);
1017 prefs->setBool("/options/transform/pattern", true);
1018 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1019 Geom::OptRect sel_bbox = selection->bounds();
1020 if (sel_bbox) {
1021 Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1022 sp_selection_move_relative(selection, m);
1023 }
1024 prefs->setBool("/options/transform/pattern", saved_pref);
1025 }
1026 }
1028 sp_document_unref(doc);
1029 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1030 _("Import"));
1032 } else {
1033 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1034 sp_ui_error_dialog(text);
1035 g_free(text);
1036 }
1038 return;
1039 }
1042 static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
1044 /**
1045 * Display an Open dialog, import a resource if OK pressed.
1046 */
1047 void
1048 sp_file_import(Gtk::Window &parentWindow)
1049 {
1050 static Glib::ustring import_path;
1052 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1053 if (!doc)
1054 return;
1056 if (!importDialogInstance) {
1057 importDialogInstance =
1058 Inkscape::UI::Dialog::FileOpenDialog::create(
1059 parentWindow,
1060 import_path,
1061 Inkscape::UI::Dialog::IMPORT_TYPES,
1062 (char const *)_("Select file to import"));
1063 }
1065 bool success = importDialogInstance->show();
1066 if (!success)
1067 return;
1069 //# Get file name and extension type
1070 Glib::ustring fileName = importDialogInstance->getFilename();
1071 Inkscape::Extension::Extension *selection =
1072 importDialogInstance->getSelectionType();
1075 if (fileName.size() > 0) {
1077 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1079 if ( newFileName.size() > 0)
1080 fileName = newFileName;
1081 else
1082 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1085 import_path = fileName;
1086 if (import_path.size()>0)
1087 import_path.append(G_DIR_SEPARATOR_S);
1089 file_import(doc, fileName, selection);
1090 }
1092 return;
1093 }
1097 /*######################
1098 ## E X P O R T
1099 ######################*/
1101 //#define NEW_EXPORT_DIALOG
1105 #ifdef NEW_EXPORT_DIALOG
1107 static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1109 /**
1110 * Display an Export dialog, export as the selected type if OK pressed
1111 */
1112 bool
1113 sp_file_export_dialog(void *widget)
1114 {
1115 //# temp hack for 'doc' until we can switch to this dialog
1116 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1118 Glib::ustring export_path;
1119 Glib::ustring export_loc;
1121 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1122 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1123 Inkscape::Extension::Output *extension;
1125 //# Get the default extension name
1126 Glib::ustring default_extension;
1127 char *attr = (char *)repr->attribute("inkscape:output_extension");
1128 if (!attr) {
1129 Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
1130 if(!attr2.empty()) default_extension = attr2;
1131 } else {
1132 default_extension = attr;
1133 }
1134 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1136 if (doc->uri == NULL)
1137 {
1138 char formatBuf[256];
1140 Glib::ustring filename_extension = ".svg";
1141 extension = dynamic_cast<Inkscape::Extension::Output *>
1142 (Inkscape::Extension::db.get(default_extension.c_str()));
1143 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1144 if (extension)
1145 filename_extension = extension->get_extension();
1147 Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
1148 if (!attr3.empty())
1149 export_path = attr3;
1151 if (!Inkscape::IO::file_test(export_path.c_str(),
1152 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1153 export_path = "";
1155 if (export_path.size()<1)
1156 export_path = g_get_home_dir();
1158 export_loc = export_path;
1159 export_loc.append(G_DIR_SEPARATOR_S);
1160 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1161 export_loc.append(formatBuf);
1163 }
1164 else
1165 {
1166 export_path = Glib::path_get_dirname(doc->uri);
1167 }
1169 // convert save_loc from utf-8 to locale
1170 // is this needed any more, now that everything is handled in
1171 // Inkscape::IO?
1172 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1173 if ( export_path_local.size() > 0)
1174 export_path = export_path_local;
1176 //# Show the SaveAs dialog
1177 if (!exportDialogInstance)
1178 exportDialogInstance =
1179 Inkscape::UI::Dialog::FileExportDialog::create(
1180 export_path,
1181 Inkscape::UI::Dialog::EXPORT_TYPES,
1182 (char const *) _("Select file to export to"),
1183 default_extension
1184 );
1186 bool success = exportDialogInstance->show();
1187 if (!success)
1188 return success;
1190 Glib::ustring fileName = exportDialogInstance->getFilename();
1192 Inkscape::Extension::Extension *selectionType =
1193 exportDialogInstance->getSelectionType();
1196 if (fileName.size() > 0) {
1197 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1199 if ( newFileName.size()>0 )
1200 fileName = newFileName;
1201 else
1202 g_warning( "Error converting save filename to UTF-8." );
1204 success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1206 if (success) {
1207 Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1208 recent->add_item(SP_DOCUMENT_URI(doc));
1209 }
1211 export_path = fileName;
1212 prefs->setString("/dialogs/save_as/path", export_path);
1214 return success;
1215 }
1218 return false;
1219 }
1221 #else
1223 /**
1224 *
1225 */
1226 bool
1227 sp_file_export_dialog(void */*widget*/)
1228 {
1229 sp_export_dialog();
1230 return true;
1231 }
1233 #endif
1235 /*######################
1236 ## E X P O R T T O O C A L
1237 ######################*/
1239 /**
1240 * Display an Export dialog, export as the selected type if OK pressed
1241 */
1242 bool
1243 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1244 {
1246 if (!SP_ACTIVE_DOCUMENT)
1247 return false;
1249 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1251 Glib::ustring export_path;
1252 Glib::ustring export_loc;
1253 Glib::ustring fileName;
1254 Inkscape::Extension::Extension *selectionType;
1256 bool success = false;
1258 static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1259 static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1260 static bool gotSuccess = false;
1262 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1263 (void)repr;
1265 if (!doc->uri && !doc->isModifiedSinceSave())
1266 return false;
1268 // Get the default extension name
1269 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1270 char formatBuf[256];
1272 Glib::ustring filename_extension = ".svg";
1273 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1275 export_path = Glib::get_tmp_dir ();
1277 export_loc = export_path;
1278 export_loc.append(G_DIR_SEPARATOR_S);
1279 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1280 export_loc.append(formatBuf);
1282 // convert save_loc from utf-8 to locale
1283 // is this needed any more, now that everything is handled in
1284 // Inkscape::IO?
1285 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1286 if ( export_path_local.size() > 0)
1287 export_path = export_path_local;
1289 // Show the Export To OCAL dialog
1290 if (!exportDialogInstance)
1291 exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
1292 parentWindow,
1293 Inkscape::UI::Dialog::EXPORT_TYPES,
1294 (char const *) _("Select file to export to")
1295 );
1297 success = exportDialogInstance->show();
1298 if (!success)
1299 return success;
1301 fileName = exportDialogInstance->getFilename();
1303 fileName.append(filename_extension.c_str());
1304 if (fileName.size() > 0) {
1305 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1307 if ( newFileName.size()>0 )
1308 fileName = newFileName;
1309 else
1310 g_warning( "Error converting save filename to UTF-8." );
1311 }
1312 Glib::ustring filePath = export_path;
1313 filePath.append(G_DIR_SEPARATOR_S);
1314 filePath.append(Glib::path_get_basename(fileName));
1316 fileName = filePath;
1318 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1320 if (!success){
1321 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1322 sp_ui_error_dialog(text);
1324 return success;
1325 }
1327 // Start now the submition
1329 // Create the uri
1330 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1331 Glib::ustring uri = "dav://";
1332 Glib::ustring username = prefs->getString("/options/ocalusername/str");
1333 Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1334 if (username.empty() || password.empty())
1335 {
1336 if(!gotSuccess)
1337 {
1338 if (!exportPasswordDialogInstance)
1339 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1340 parentWindow,
1341 (char const *) _("Open Clip Art Login"));
1342 success = exportPasswordDialogInstance->show();
1343 if (!success)
1344 return success;
1345 }
1346 username = exportPasswordDialogInstance->getUsername();
1347 password = exportPasswordDialogInstance->getPassword();
1348 }
1349 uri.append(username);
1350 uri.append(":");
1351 uri.append(password);
1352 uri.append("@");
1353 uri.append(prefs->getString("/options/ocalurl/str"));
1354 uri.append("/dav.php/");
1355 uri.append(Glib::path_get_basename(fileName));
1357 // Save as a remote file using the dav protocol.
1358 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1359 remove(fileName.c_str());
1360 if (!success)
1361 {
1362 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."));
1363 sp_ui_error_dialog(text);
1364 }
1365 else
1366 gotSuccess = true;
1368 return success;
1369 }
1371 /**
1372 * Export the current document to OCAL
1373 */
1374 void
1375 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1376 {
1378 // Try to execute the new code and return;
1379 if (!SP_ACTIVE_DOCUMENT)
1380 return;
1381 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1382 if (success)
1383 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1384 }
1387 /*######################
1388 ## I M P O R T F R O M O C A L
1389 ######################*/
1391 /**
1392 * Display an ImportToOcal Dialog, and the selected document from OCAL
1393 */
1394 void
1395 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1396 {
1397 static Glib::ustring import_path;
1399 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1400 if (!doc)
1401 return;
1403 static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1405 if (!importDialogInstance) {
1406 importDialogInstance = new
1407 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1408 parentWindow,
1409 import_path,
1410 Inkscape::UI::Dialog::IMPORT_TYPES,
1411 (char const *)_("Import From Open Clip Art Library"));
1412 }
1414 bool success = importDialogInstance->show();
1415 if (!success)
1416 return;
1418 // Get file name and extension type
1419 Glib::ustring fileName = importDialogInstance->getFilename();
1420 Inkscape::Extension::Extension *selection =
1421 importDialogInstance->getSelectionType();
1423 if (fileName.size() > 0) {
1425 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1427 if ( newFileName.size() > 0)
1428 fileName = newFileName;
1429 else
1430 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1432 import_path = fileName;
1433 if (import_path.size()>0)
1434 import_path.append(G_DIR_SEPARATOR_S);
1436 file_import(doc, fileName, selection);
1437 }
1439 return;
1440 }
1442 /*######################
1443 ## P R I N T
1444 ######################*/
1447 /**
1448 * Print the current document, if any.
1449 */
1450 void
1451 sp_file_print(Gtk::Window& parentWindow)
1452 {
1453 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1454 if (doc)
1455 sp_print_document(parentWindow, doc);
1456 }
1458 /**
1459 * Display what the drawing would look like, if
1460 * printed.
1461 */
1462 void
1463 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1464 {
1466 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1467 if (doc)
1468 sp_print_preview_document(doc);
1470 }
1472 void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1473 {
1474 //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1476 if ( 0 ) {
1477 gchar const* things[] = {
1478 "data:foo,bar",
1479 "http://www.google.com/image.png",
1480 "ftp://ssd.com/doo",
1481 "/foo/dee/bar.svg",
1482 "foo.svg",
1483 "file:/foo/dee/bar.svg",
1484 "file:///foo/dee/bar.svg",
1485 "file:foo.svg",
1486 "/foo/bar\xe1\x84\x92.svg",
1487 "file:///foo/bar\xe1\x84\x92.svg",
1488 "file:///foo/bar%e1%84%92.svg",
1489 "/foo/bar%e1%84%92.svg",
1490 "bar\xe1\x84\x92.svg",
1491 "bar%e1%84%92.svg",
1492 NULL
1493 };
1494 g_message("+------");
1495 for ( int i = 0; things[i]; i++ )
1496 {
1497 try
1498 {
1499 URI uri(things[i]);
1500 gboolean isAbs = g_path_is_absolute( things[i] );
1501 gchar *str = uri.toString();
1502 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1503 (int)uri.isRelative(),
1504 uri.getScheme(),
1505 uri.getPath(),
1506 uri.getOpaque(),
1507 things[i],
1508 str );
1509 g_free(str);
1510 }
1511 catch ( MalformedURIException err )
1512 {
1513 dump_str( things[i], "MalformedURIException" );
1514 xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1515 g_message(" gone from [%s] to [%s]", things[i], redo );
1516 if ( redo == NULL )
1517 {
1518 URI again = URI::fromUtf8( things[i] );
1519 gboolean isAbs = g_path_is_absolute( things[i] );
1520 gchar *str = again.toString();
1521 g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1522 (int)again.isRelative(),
1523 again.getScheme(),
1524 again.getPath(),
1525 again.getOpaque(),
1526 things[i],
1527 str );
1528 g_free(str);
1529 g_message(" ----");
1530 }
1531 }
1532 }
1533 g_message("+------");
1534 }
1536 GSList const *images = sp_document_get_resource_list(doc, "image");
1537 for (GSList const *l = images; l != NULL; l = l->next) {
1538 Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1540 const gchar *href = ir->attribute("xlink:href");
1542 // First try to figure out an absolute path to the asset
1543 //g_message("image href [%s]", href );
1544 if (spns && !g_path_is_absolute(href)) {
1545 const gchar *absref = ir->attribute("sodipodi:absref");
1546 const gchar *base_href = g_build_filename(base, href, NULL);
1547 //g_message(" absr [%s]", absref );
1549 if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1550 {
1551 // only switch over if the absref is valid while href is not
1552 href = absref;
1553 //g_message(" copied absref to href");
1554 }
1555 }
1557 // Once we have an absolute path, convert it relative to the new location
1558 if (href && g_path_is_absolute(href)) {
1559 const gchar *relname = sp_relative_path_from_path(href, base);
1560 //g_message(" setting to [%s]", relname );
1561 ir->setAttribute("xlink:href", relname);
1562 }
1563 // TODO next refinement is to make the first choice keeping the relative path as-is if
1564 // based on the new location it gives us a valid file.
1565 }
1566 }
1569 /*
1570 Local Variables:
1571 mode:c++
1572 c-file-style:"stroustrup"
1573 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1574 indent-tabs-mode:nil
1575 fill-column:99
1576 End:
1577 */
1578 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :