Code

Fix self-snapping when dragging the transformation center of a selection containing...
[inkscape.git] / src / file.cpp
1 /** @file
2  * @brief File/Print operations
3  */
4 /* Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Chema Celorio <chema@celorio.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Bruno Dilly <bruno.dilly@gmail.com>
9  *   Stephen Silver <sasilver@users.sourceforge.net>
10  *
11  * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12  * Copyright (C) 1999-2008 Authors
13  * Copyright (C) 2004 David Turner
14  * Copyright (C) 2001-2002 Ximian, Inc.
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 /** @file
20  * @note This file needs to be cleaned up extensively.
21  * What it probably needs is to have one .h file for
22  * the API, and two or more .cpp files for the implementations.
23  */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <gtk/gtk.h>
30 #include <glib/gmem.h>
31 #include <glibmm/i18n.h>
32 #include <libnr/nr-pixops.h>
34 #include "application/application.h"
35 #include "application/editor.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
38 #include "dialogs/export.h"
39 #include "dir-util.h"
40 #include "document-private.h"
41 #include "extension/db.h"
42 #include "extension/input.h"
43 #include "extension/output.h"
44 #include "extension/system.h"
45 #include "file.h"
46 #include "helper/png-write.h"
47 #include "id-clash.h"
48 #include "inkscape.h"
49 #include "inkscape.h"
50 #include "interface.h"
51 #include "io/sys.h"
52 #include "message.h"
53 #include "message-stack.h"
54 #include "path-prefix.h"
55 #include "preferences.h"
56 #include "print.h"
57 #include "rdf.h"
58 #include "selection-chemistry.h"
59 #include "selection.h"
60 #include "sp-namedview.h"
61 #include "style.h"
62 #include "ui/dialog/ocaldialogs.h"
63 #include "ui/view/view-widget.h"
64 #include "uri.h"
65 #include "xml/rebase-hrefs.h"
67 #ifdef WITH_GNOME_VFS
68 # include <libgnomevfs/gnome-vfs.h>
69 #endif
71 #ifdef WITH_DBUS
72 #include "extension/dbus/dbus-init.h"
73 #endif
75 //#ifdef WITH_INKBOARD
76 //#include "jabber_whiteboard/session-manager.h"
77 //#endif
79 #ifdef WIN32
80 #include <windows.h>
81 #endif
83 //#define INK_DUMP_FILENAME_CONV 1
84 #undef INK_DUMP_FILENAME_CONV
86 //#define INK_DUMP_FOPEN 1
87 #undef INK_DUMP_FOPEN
89 void dump_str(gchar const *str, gchar const *prefix);
90 void dump_ustr(Glib::ustring const &ustr);
92 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
93 static void sp_file_add_recent(gchar const *uri)
94 {
95     if(uri == NULL) {
96         g_warning("sp_file_add_recent: uri == NULL");
97         return;
98     }
99     GtkRecentManager *recent = gtk_recent_manager_get_default();
100     gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
101     if (fn) {
102         gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
103         if (uri_to_add) {
104             gtk_recent_manager_add_item(recent, uri_to_add);
105             g_free(uri_to_add);
106         }
107         g_free(fn);
108     }
112 /*######################
113 ## N E W
114 ######################*/
116 /**
117  * Create a blank document and add it to the desktop
118  */
119 SPDesktop*
120 sp_file_new(const Glib::ustring &templ)
122     char *templName = NULL;
123     if (templ.size()>0)
124         templName = (char *)templ.c_str();
125     SPDocument *doc = sp_document_new(templName, TRUE, true);
126     g_return_val_if_fail(doc != NULL, NULL);
128     SPDesktop *dt;
129     if (Inkscape::NSApplication::Application::getNewGui())
130     {
131         dt = Inkscape::NSApplication::Editor::createDesktop (doc);
132     } else {
133         SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
134         g_return_val_if_fail(dtw != NULL, NULL);
135         sp_document_unref(doc);
137         sp_create_window(dtw, TRUE);
138         dt = static_cast<SPDesktop*>(dtw->view);
139         sp_namedview_window_from_document(dt);
140         sp_namedview_update_layers_from_document(dt);
141     }
143 #ifdef WITH_DBUS
144     Inkscape::Extension::Dbus::dbus_init_desktop_interface(dt);
145 #endif
147     return dt;
150 SPDesktop* sp_file_new_default()
152     std::list<gchar *> sources;
153     sources.push_back( profile_path("templates") ); // first try user's local dir
154     sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
155     std::list<gchar const*> baseNames;
156     gchar const* localized = _("default.svg");
157     if (strcmp("default.svg", localized) != 0) {
158         baseNames.push_back(localized);
159     }
160     baseNames.push_back("default.svg");
161     gchar *foundTemplate = 0;
163     for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
164         for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
165             gchar *dirname = *it;
166             if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
168                 // TRANSLATORS: default.svg is localizable - this is the name of the default document
169                 //  template. This way you can localize the default pagesize, translate the name of
170                 //  the default layer, etc. If you wish to localize this file, please create a
171                 //  localized share/templates/default.xx.svg file, where xx is your language code.
172                 char *tmp = g_build_filename(dirname, *nameIt, NULL);
173                 if (Inkscape::IO::file_test(tmp, G_FILE_TEST_IS_REGULAR)) {
174                     foundTemplate = tmp;
175                 } else {
176                     g_free(tmp);
177                 }
178             }
179         }
180     }
182     for (std::list<gchar *>::iterator it = sources.begin(); it != sources.end(); ++it) {
183         g_free(*it);
184     }
186     SPDesktop* desk = sp_file_new(foundTemplate ? foundTemplate : "");
187     if (foundTemplate) {
188         g_free(foundTemplate);
189         foundTemplate = 0;
190     }
191     return desk;
195 /*######################
196 ## D E L E T E
197 ######################*/
199 /**
200  *  Perform document closures preceding an exit()
201  */
202 void
203 sp_file_exit()
205     sp_ui_close_all();
206     // no need to call inkscape_exit here; last document being closed will take care of that
210 /*######################
211 ## O P E N
212 ######################*/
214 /**
215  *  Open a file, add the document to the desktop
216  *
217  *  \param replace_empty if true, and the current desktop is empty, this document
218  *  will replace the empty one.
219  */
220 bool
221 sp_file_open(const Glib::ustring &uri,
222              Inkscape::Extension::Extension *key,
223              bool add_to_recent, bool replace_empty)
225     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
226     if (desktop)
227         desktop->setWaitingCursor();
229     SPDocument *doc = NULL;
230     try {
231         doc = Inkscape::Extension::open(key, uri.c_str());
232     } catch (Inkscape::Extension::Input::no_extension_found &e) {
233         doc = NULL;
234     } catch (Inkscape::Extension::Input::open_failed &e) {
235         doc = NULL;
236     }
238     if (desktop)
239         desktop->clearWaitingCursor();
241     if (doc) {
242         SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
244         if (existing && existing->virgin && replace_empty) {
245             // If the current desktop is empty, open the document there
246             sp_document_ensure_up_to_date (doc);
247             desktop->change_document(doc);
248             sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
249         } else {
250             if (!Inkscape::NSApplication::Application::getNewGui()) {
251                 // create a whole new desktop and window
252                 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
253                 sp_create_window(dtw, TRUE);
254                 desktop = static_cast<SPDesktop*>(dtw->view);
255             } else {
256                 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
257             }
258         }
260         doc->virgin = FALSE;
261         // everyone who cares now has a reference, get rid of ours
262         sp_document_unref(doc);
263         // resize the window to match the document properties
264         sp_namedview_window_from_document(desktop);
265         sp_namedview_update_layers_from_document(desktop);
267         if (add_to_recent) {
268             sp_file_add_recent(SP_DOCUMENT_URI(doc));
269         }
271         return TRUE;
272     } else {
273         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
274         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
275         sp_ui_error_dialog(text);
276         g_free(text);
277         g_free(safeUri);
278         return FALSE;
279     }
282 /**
283  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
284  */
285 void
286 sp_file_revert_dialog()
288     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
289     g_assert(desktop != NULL);
291     SPDocument *doc = sp_desktop_document(desktop);
292     g_assert(doc != NULL);
294     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
295     g_assert(repr != NULL);
297     gchar const *uri = doc->uri;
298     if (!uri) {
299         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
300         return;
301     }
303     bool do_revert = true;
304     if (doc->isModifiedSinceSave()) {
305         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
307         bool response = desktop->warnDialog (text);
308         g_free(text);
310         if (!response) {
311             do_revert = false;
312         }
313     }
315     bool reverted;
316     if (do_revert) {
317         // Allow overwriting of current document.
318         doc->virgin = TRUE;
320         // remember current zoom and view
321         double zoom = desktop->current_zoom();
322         Geom::Point c = desktop->get_display_area().midpoint();
324         reverted = sp_file_open(uri,NULL);
325         if (reverted) {
326             // restore zoom and view
327             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
328         }
329     } else {
330         reverted = false;
331     }
333     if (reverted) {
334         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
335     } else {
336         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
337     }
340 void dump_str(gchar const *str, gchar const *prefix)
342     Glib::ustring tmp;
343     tmp = prefix;
344     tmp += " [";
345     size_t const total = strlen(str);
346     for (unsigned i = 0; i < total; i++) {
347         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
348         tmp += tmp2;
349         g_free(tmp2);
350     }
352     tmp += "]";
353     g_message("%s", tmp.c_str());
356 void dump_ustr(Glib::ustring const &ustr)
358     char const *cstr = ustr.c_str();
359     char const *data = ustr.data();
360     Glib::ustring::size_type const byteLen = ustr.bytes();
361     Glib::ustring::size_type const dataLen = ustr.length();
362     Glib::ustring::size_type const cstrLen = strlen(cstr);
364     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
365               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
366     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
367     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
369     try {
370         Glib::ustring tmp;
371         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
372             tmp = "    ";
373             if (i < dataLen) {
374                 Glib::ustring::value_type val = ustr.at(i);
375                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
376                 tmp += tmp2;
377                 g_free( tmp2 );
378             } else {
379                 tmp += "    ";
380             }
382             if (i < byteLen) {
383                 int val = (0x0ff & data[i]);
384                 gchar *tmp2 = g_strdup_printf("    %02x", val);
385                 tmp += tmp2;
386                 g_free( tmp2 );
387                 if ( val > 32 && val < 127 ) {
388                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
389                     tmp += tmp2;
390                     g_free( tmp2 );
391                 } else {
392                     tmp += "    . ";
393                 }
394             } else {
395                 tmp += "       ";
396             }
398             if ( i < cstrLen ) {
399                 int val = (0x0ff & cstr[i]);
400                 gchar* tmp2 = g_strdup_printf("    %02x", val);
401                 tmp += tmp2;
402                 g_free(tmp2);
403                 if ( val > 32 && val < 127 ) {
404                     tmp2 = g_strdup_printf("   '%c'", (gchar) val);
405                     tmp += tmp2;
406                     g_free( tmp2 );
407                 } else {
408                     tmp += "    . ";
409                 }
410             } else {
411                 tmp += "            ";
412             }
414             g_message( "%s", tmp.c_str() );
415         }
416     } catch (...) {
417         g_message("XXXXXXXXXXXXXXXXXX Exception" );
418     }
419     g_message("---------------");
422 /**
423  *  Display an file Open selector.  Open a document if OK is pressed.
424  *  Can select single or multiple files for opening.
425  */
426 void
427 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
429     //# Get the current directory for finding files
430     static Glib::ustring open_path;
431     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
433     if(open_path.empty())
434     {
435         Glib::ustring attr = prefs->getString("/dialogs/open/path");
436         if (!attr.empty()) open_path = attr;
437     }
439     //# Test if the open_path directory exists
440     if (!Inkscape::IO::file_test(open_path.c_str(),
441               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
442         open_path = "";
444 #ifdef WIN32
445     //# If no open path, default to our win32 documents folder
446     if (open_path.empty())
447     {
448         // The path to the My Documents folder is read from the
449         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
450         HKEY key = NULL;
451         if(RegOpenKeyExA(HKEY_CURRENT_USER,
452             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
453             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
454         {
455             WCHAR utf16path[_MAX_PATH];
456             DWORD value_type;
457             DWORD data_size = sizeof(utf16path);
458             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
459                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
460             {
461                 g_assert(value_type == REG_SZ);
462                 gchar *utf8path = g_utf16_to_utf8(
463                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
464                 if(utf8path)
465                 {
466                     open_path = Glib::ustring(utf8path);
467                     g_free(utf8path);
468                 }
469             }
470         }
471     }
472 #endif
474     //# If no open path, default to our home directory
475     if (open_path.empty())
476     {
477         open_path = g_get_home_dir();
478         open_path.append(G_DIR_SEPARATOR_S);
479     }
481     //# Create a dialog
482     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
483               Inkscape::UI::Dialog::FileOpenDialog::create(
484                  parentWindow, open_path,
485                  Inkscape::UI::Dialog::SVG_TYPES,
486                  _("Select file to open"));
488     //# Show the dialog
489     bool const success = openDialogInstance->show();
491     //# Save the folder the user selected for later
492     open_path = openDialogInstance->getCurrentDirectory();
494     if (!success)
495     {
496         delete openDialogInstance;
497         return;
498     }
500     //# User selected something.  Get name and type
501     Glib::ustring fileName = openDialogInstance->getFilename();
503     Inkscape::Extension::Extension *selection =
504             openDialogInstance->getSelectionType();
506     //# Code to check & open if multiple files.
507     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
509     //# We no longer need the file dialog object - delete it
510     delete openDialogInstance;
511     openDialogInstance = NULL;
513     //# Iterate through filenames if more than 1
514     if (flist.size() > 1)
515     {
516         for (unsigned int i = 0; i < flist.size(); i++)
517         {
518             fileName = flist[i];
520             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
521             if ( newFileName.size() > 0 )
522                 fileName = newFileName;
523             else
524                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
526 #ifdef INK_DUMP_FILENAME_CONV
527             g_message("Opening File %s\n", fileName.c_str());
528 #endif
529             sp_file_open(fileName, selection);
530         }
532         return;
533     }
536     if (!fileName.empty())
537     {
538         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
540         if ( newFileName.size() > 0)
541             fileName = newFileName;
542         else
543             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
545         open_path = Glib::path_get_dirname (fileName);
546         open_path.append(G_DIR_SEPARATOR_S);
547         prefs->setString("/dialogs/open/path", open_path);
549         sp_file_open(fileName, selection);
550     }
552     return;
556 /*######################
557 ## V A C U U M
558 ######################*/
560 /**
561  * Remove unreferenced defs from the defs section of the document.
562  */
565 void
566 sp_file_vacuum()
568     SPDocument *doc = SP_ACTIVE_DOCUMENT;
570     unsigned int diff = vacuum_document (doc);
572     sp_document_done(doc, SP_VERB_FILE_VACUUM,
573                      _("Vacuum &lt;defs&gt;"));
575     SPDesktop *dt = SP_ACTIVE_DESKTOP;
576     if (diff > 0) {
577         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
578                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
579                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
580                          diff),
581                 diff);
582     } else {
583         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
584     }
589 /*######################
590 ## S A V E
591 ######################*/
593 /**
594  * This 'save' function called by the others below
595  *
596  * \param    official  whether to set :output_module and :modified in the
597  *                     document; is true for normal save, false for temporary saves
598  */
599 static bool
600 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
601           Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
602           Inkscape::Extension::FileSaveMethod save_method)
604     if (!doc || uri.size()<1) //Safety check
605         return false;
607     try {
608         Inkscape::Extension::save(key, doc, uri.c_str(),
609                                   false,
610                                   checkoverwrite, official,
611                                   save_method);
612     } catch (Inkscape::Extension::Output::no_extension_found &e) {
613         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
614         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
615         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
616         sp_ui_error_dialog(text);
617         g_free(text);
618         g_free(safeUri);
619         return FALSE;
620     } catch (Inkscape::Extension::Output::file_read_only &e) {
621         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
622         gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
623         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
624         sp_ui_error_dialog(text);
625         g_free(text);
626         g_free(safeUri);
627         return FALSE;
628     } catch (Inkscape::Extension::Output::save_failed &e) {
629         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
630         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
631         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
632         sp_ui_error_dialog(text);
633         g_free(text);
634         g_free(safeUri);
635         return FALSE;
636     } catch (Inkscape::Extension::Output::save_cancelled &e) {
637         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
638         return FALSE;
639     } catch (Inkscape::Extension::Output::no_overwrite &e) {
640         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
641     } catch (...) {
642         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
643         return FALSE;
644     }
646     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
647     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
648     return true;
651 /*
652  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
653  */
654 bool
655 file_save_remote(SPDocument */*doc*/,
656     #ifdef WITH_GNOME_VFS
657                  const Glib::ustring &uri,
658     #else
659                  const Glib::ustring &/*uri*/,
660     #endif
661                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
663 #ifdef WITH_GNOME_VFS
665 #define BUF_SIZE 8192
666     gnome_vfs_init();
668     GnomeVFSHandle    *from_handle = NULL;
669     GnomeVFSHandle    *to_handle = NULL;
670     GnomeVFSFileSize  bytes_read;
671     GnomeVFSFileSize  bytes_written;
672     GnomeVFSResult    result;
673     guint8 buffer[8192];
675     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
677     if ( uri_local == NULL ) {
678         g_warning( "Error converting filename to locale encoding.");
679     }
681     // Gets the temp file name.
682     Glib::ustring fileName = Glib::get_tmp_dir ();
683     fileName.append(G_DIR_SEPARATOR_S);
684     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
686     // Open the temp file to send.
687     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
689     if (result != GNOME_VFS_OK) {
690         g_warning("Could not find the temp saving.");
691         return false;
692     }
694     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
695     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
697     if (result != GNOME_VFS_OK) {
698         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
699         return false;
700     }
702     while (1) {
704         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
706         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
707             result = gnome_vfs_close (from_handle);
708             result = gnome_vfs_close (to_handle);
709             return true;
710         }
712         if (result != GNOME_VFS_OK) {
713             g_warning("%s", gnome_vfs_result_to_string(result));
714             return false;
715         }
716         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
717         if (result != GNOME_VFS_OK) {
718             g_warning("%s", gnome_vfs_result_to_string(result));
719             return false;
720         }
723         if (bytes_read != bytes_written){
724             return false;
725         }
727     }
728     return true;
729 #else
730     // in case we do not have GNOME_VFS
731     return false;
732 #endif
737 /**
738  *  Display a SaveAs dialog.  Save the document if OK pressed.
739  */
740 bool
741 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
743     Inkscape::Extension::Output *extension = 0;
744     bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
746     // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
747     //       filename_extension only uses ".svg"
748     Glib::ustring default_extension;
749     Glib::ustring filename_extension = ".svg";
751     default_extension= Inkscape::Extension::get_file_save_extension(save_method);
752     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
754     extension = dynamic_cast<Inkscape::Extension::Output *>
755         (Inkscape::Extension::db.get(default_extension.c_str()));
757     if (extension)
758         filename_extension = extension->get_extension();
760     Glib::ustring save_path;
761     Glib::ustring save_loc;
763     save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
765     if (!Inkscape::IO::file_test(save_path.c_str(),
766           (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
767         save_path = "";
769     if (save_path.size()<1)
770         save_path = g_get_home_dir();
772     save_loc = save_path;
773     save_loc.append(G_DIR_SEPARATOR_S);
775     char formatBuf[256];
776     int i = 1;
777     if (!doc->uri) {
778         // We are saving for the first time; create a unique default filename
779         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
780         save_loc.append(formatBuf);
782         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
783             save_loc = save_path;
784             save_loc.append(G_DIR_SEPARATOR_S);
785             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
786             save_loc.append(formatBuf);
787         }
788     } else {
789         snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
790         save_loc.append(formatBuf);
791     }
793     // convert save_loc from utf-8 to locale
794     // is this needed any more, now that everything is handled in
795     // Inkscape::IO?
796     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
798     if ( save_loc_local.size() > 0)
799         save_loc = save_loc_local;
801     //# Show the SaveAs dialog
802     char const * dialog_title;
803     if (is_copy) {
804         dialog_title = (char const *) _("Select file to save a copy to");
805     } else {
806         dialog_title = (char const *) _("Select file to save to");
807     }
808     gchar* doc_title = doc->root->title();
809     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
810         Inkscape::UI::Dialog::FileSaveDialog::create(
811             parentWindow,
812             save_loc,
813             Inkscape::UI::Dialog::SVG_TYPES,
814             dialog_title,
815             default_extension,
816             doc_title ? doc_title : "",
817             save_method
818             );
820     saveDialog->setSelectionType(extension);
822     bool success = saveDialog->show();
823     if (!success) {
824         delete saveDialog;
825         return success;
826     }
828     // set new title here (call RDF to ensure metadata and title element are updated)
829     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
830     // free up old string
831     if(doc_title) g_free(doc_title);
833     Glib::ustring fileName = saveDialog->getFilename();
834     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
836     delete saveDialog;
838     saveDialog = 0;
840     if (fileName.size() > 0) {
841         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
843         if ( newFileName.size()>0 )
844             fileName = newFileName;
845         else
846             g_warning( "Error converting save filename to UTF-8." );
848         // FIXME: does the argument !is_copy really convey the correct meaning here?
849         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
851         if (success && SP_DOCUMENT_URI(doc)) {
852             sp_file_add_recent(SP_DOCUMENT_URI(doc));
853         }
855         save_path = Glib::path_get_dirname(fileName);
856         Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
858         return success;
859     }
862     return false;
866 /**
867  * Save a document, displaying a SaveAs dialog if necessary.
868  */
869 bool
870 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
872     bool success = true;
874     if (doc->isModifiedSinceSave()) {
875         if ( doc->uri == NULL )
876         {
877             // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
878             // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
879             // benutzte "Save as ..."-Endung
880             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
881         } else {
882             Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
883             Glib::ustring fn = g_strdup(doc->uri);
884             // Try to determine the extension from the uri; this may not lead to a valid extension,
885             // but this case is caught in the file_save method below (or rather in Extension::save()
886             // further down the line).
887             Glib::ustring ext = "";
888             Glib::ustring::size_type pos = fn.rfind('.');
889             if (pos != Glib::ustring::npos) {
890                 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
891                 // but hopefully it's a reasonable workaround for now
892                 ext = fn.substr( pos );
893             }
894             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
895             if (success == false) {
896                 // give the user the chance to change filename or extension
897                 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
898             }
899         }
900     } else {
901         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
902         success = TRUE;
903     }
905     return success;
909 /**
910  * Save a document.
911  */
912 bool
913 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
915     if (!SP_ACTIVE_DOCUMENT)
916         return false;
918     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
920     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
921     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
925 /**
926  *  Save a document, always displaying the SaveAs dialog.
927  */
928 bool
929 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
931     if (!SP_ACTIVE_DOCUMENT)
932         return false;
933     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
934     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
939 /**
940  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
941  */
942 bool
943 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
945     if (!SP_ACTIVE_DOCUMENT)
946         return false;
947     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
948     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
952 /*######################
953 ## I M P O R T
954 ######################*/
956 /**
957  *  Import a resource.  Called by sp_file_import()
958  */
959 void
960 file_import(SPDocument *in_doc, const Glib::ustring &uri,
961                Inkscape::Extension::Extension *key)
963     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
965     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
966     SPDocument *doc;
967     try {
968         doc = Inkscape::Extension::open(key, uri.c_str());
969     } catch (Inkscape::Extension::Input::no_extension_found &e) {
970         doc = NULL;
971     } catch (Inkscape::Extension::Input::open_failed &e) {
972         doc = NULL;
973     }
975     if (doc != NULL) {
976         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
977         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
979         prevent_id_clashes(doc, in_doc);
981         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
982         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
984         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
986         // Count the number of top-level items in the imported document.
987         guint items_count = 0;
988         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
989              child != NULL; child = SP_OBJECT_NEXT(child))
990         {
991             if (SP_IS_ITEM(child)) items_count++;
992         }
994         // Create a new group if necessary.
995         Inkscape::XML::Node *newgroup = NULL;
996         if ((style && style->firstChild()) || items_count > 1) {
997             newgroup = xml_in_doc->createElement("svg:g");
998             sp_repr_css_set(newgroup, style, "style");
999         }
1001         // Determine the place to insert the new object.
1002         // This will be the current layer, if possible.
1003         // FIXME: If there's no desktop (command line run?) we need
1004         //        a document:: method to return the current layer.
1005         //        For now, we just use the root in this case.
1006         SPObject *place_to_insert;
1007         if (desktop) {
1008             place_to_insert = desktop->currentLayer();
1009         } else {
1010             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
1011         }
1013         // Construct a new object representing the imported image,
1014         // and insert it into the current document.
1015         SPObject *new_obj = NULL;
1016         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
1017              child != NULL; child = SP_OBJECT_NEXT(child) )
1018         {
1019             if (SP_IS_ITEM(child)) {
1020                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
1022                 // convert layers to groups, and make sure they are unlocked
1023                 // FIXME: add "preserve layers" mode where each layer from
1024                 //        import is copied to the same-named layer in host
1025                 newitem->setAttribute("inkscape:groupmode", NULL);
1026                 newitem->setAttribute("sodipodi:insensitive", NULL);
1028                 if (newgroup) newgroup->appendChild(newitem);
1029                 else new_obj = place_to_insert->appendChildRepr(newitem);
1030             }
1032             // don't lose top-level defs or style elements
1033             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1034                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1035                 if (!strcmp(tag, "svg:defs")) {
1036                     for (SPObject *x = sp_object_first_child(child);
1037                          x != NULL; x = SP_OBJECT_NEXT(x))
1038                     {
1039                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1040                     }
1041                 }
1042                 else if (!strcmp(tag, "svg:style")) {
1043                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1044                 }
1045             }
1046         }
1047         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1049         // release some stuff
1050         if (newgroup) Inkscape::GC::release(newgroup);
1051         if (style) sp_repr_css_attr_unref(style);
1053         // select and move the imported item
1054         if (new_obj && SP_IS_ITEM(new_obj)) {
1055             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1056             selection->set(SP_ITEM(new_obj));
1058             // preserve parent and viewBox transformations
1059             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1060             sp_document_ensure_up_to_date(doc);
1061             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1062             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1064             // move to mouse pointer
1065             {
1066                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1067                 Geom::OptRect sel_bbox = selection->bounds();
1068                 if (sel_bbox) {
1069                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1070                     sp_selection_move_relative(selection, m, false);
1071                 }
1072             }
1073         }
1075         sp_document_unref(doc);
1076         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1077                          _("Import"));
1079     } else {
1080         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1081         sp_ui_error_dialog(text);
1082         g_free(text);
1083     }
1085     return;
1089 /**
1090  *  Display an Open dialog, import a resource if OK pressed.
1091  */
1092 void
1093 sp_file_import(Gtk::Window &parentWindow)
1095     static Glib::ustring import_path;
1097     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1098     if (!doc)
1099         return;
1101     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1103     if(import_path.empty())
1104     {
1105         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1106         if (!attr.empty()) import_path = attr;
1107     }
1109     //# Test if the import_path directory exists
1110     if (!Inkscape::IO::file_test(import_path.c_str(),
1111               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1112         import_path = "";
1114     //# If no open path, default to our home directory
1115     if (import_path.empty())
1116     {
1117         import_path = g_get_home_dir();
1118         import_path.append(G_DIR_SEPARATOR_S);
1119     }
1121     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1122     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1123              Inkscape::UI::Dialog::FileOpenDialog::create(
1124                  parentWindow,
1125                  import_path,
1126                  Inkscape::UI::Dialog::IMPORT_TYPES,
1127                  (char const *)_("Select file to import"));
1129     bool success = importDialogInstance->show();
1130     if (!success) {
1131         delete importDialogInstance;
1132         return;
1133     }
1135     //# Get file name and extension type
1136     Glib::ustring fileName = importDialogInstance->getFilename();
1137     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1139     delete importDialogInstance;
1140     importDialogInstance = NULL;
1142     if (fileName.size() > 0) {
1144         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1146         if ( newFileName.size() > 0)
1147             fileName = newFileName;
1148         else
1149             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1151         import_path = Glib::path_get_dirname (fileName);
1152         import_path.append(G_DIR_SEPARATOR_S);
1153         prefs->setString("/dialogs/import/path", import_path);
1155         file_import(doc, fileName, selection);
1156     }
1158     return;
1163 /*######################
1164 ## E X P O R T
1165 ######################*/
1168 #ifdef NEW_EXPORT_DIALOG
1170 /**
1171  *  Display an Export dialog, export as the selected type if OK pressed
1172  */
1173 bool
1174 sp_file_export_dialog(Gtk::Window &parentWindow)
1176     //# temp hack for 'doc' until we can switch to this dialog
1177     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1179     Glib::ustring export_path;
1180     Glib::ustring export_loc;
1182     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1183     Inkscape::Extension::Output *extension;
1185     //# Get the default extension name
1186     Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1187     if(default_extension.empty()) {
1188         // FIXME: Is this a good default? Should there be a macro for the string?
1189         default_extension = "org.inkscape.output.png.cairo";
1190     }
1191     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1193     if (doc->uri == NULL)
1194         {
1195         char formatBuf[256];
1197         Glib::ustring filename_extension = ".svg";
1198         extension = dynamic_cast<Inkscape::Extension::Output *>
1199               (Inkscape::Extension::db.get(default_extension.c_str()));
1200         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1201         if (extension)
1202             filename_extension = extension->get_extension();
1204         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1205         if (!attr3.empty())
1206             export_path = attr3;
1208         if (!Inkscape::IO::file_test(export_path.c_str(),
1209               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1210             export_path = "";
1212         if (export_path.size()<1)
1213             export_path = g_get_home_dir();
1215         export_loc = export_path;
1216         export_loc.append(G_DIR_SEPARATOR_S);
1217         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1218         export_loc.append(formatBuf);
1220         }
1221     else
1222         {
1223         export_path = Glib::path_get_dirname(doc->uri);
1224         }
1226     // convert save_loc from utf-8 to locale
1227     // is this needed any more, now that everything is handled in
1228     // Inkscape::IO?
1229     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1230     if ( export_path_local.size() > 0)
1231         export_path = export_path_local;
1233     //# Show the Export dialog
1234     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1235         Inkscape::UI::Dialog::FileExportDialog::create(
1236             parentWindow,
1237             export_path,
1238             Inkscape::UI::Dialog::EXPORT_TYPES,
1239             (char const *) _("Select file to export to"),
1240             default_extension
1241         );
1243     bool success = exportDialogInstance->show();
1244     if (!success) {
1245         delete exportDialogInstance;
1246         return success;
1247     }
1249     Glib::ustring fileName = exportDialogInstance->getFilename();
1251     Inkscape::Extension::Extension *selectionType =
1252         exportDialogInstance->getSelectionType();
1254     delete exportDialogInstance;
1255     exportDialogInstance = NULL;
1257     if (fileName.size() > 0) {
1258         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1260         if ( newFileName.size()>0 )
1261             fileName = newFileName;
1262         else
1263             g_warning( "Error converting save filename to UTF-8." );
1265         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1267         if (success) {
1268             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1269             recent->add_item(SP_DOCUMENT_URI(doc));
1270         }
1272         export_path = fileName;
1273         prefs->setString("/dialogs/save_export/path", export_path);
1275         return success;
1276     }
1279     return false;
1282 #else
1284 /**
1285  *
1286  */
1287 bool
1288 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1290     sp_export_dialog();
1291     return true;
1294 #endif
1296 /*######################
1297 ## E X P O R T  T O  O C A L
1298 ######################*/
1300 /**
1301  *  Display an Export dialog, export as the selected type if OK pressed
1302  */
1303 /*
1304 bool
1305 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1308    if (!SP_ACTIVE_DOCUMENT)
1309         return false;
1311     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1313     Glib::ustring export_path;
1314     Glib::ustring export_loc;
1315     Glib::ustring fileName;
1316     Inkscape::Extension::Extension *selectionType;
1318     bool success = false;
1320     static bool gotSuccess = false;
1322     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1323     (void)repr;
1325     if (!doc->uri && !doc->isModifiedSinceSave())
1326         return false;
1328     //  Get the default extension name
1329     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1330     char formatBuf[256];
1332     Glib::ustring filename_extension = ".svg";
1333     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1335     export_path = Glib::get_tmp_dir ();
1337     export_loc = export_path;
1338     export_loc.append(G_DIR_SEPARATOR_S);
1339     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1340     export_loc.append(formatBuf);
1342     // convert save_loc from utf-8 to locale
1343     // is this needed any more, now that everything is handled in
1344     // Inkscape::IO?
1345     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1346     if ( export_path_local.size() > 0)
1347         export_path = export_path_local;
1349     // Show the Export To OCAL dialog
1350     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1351         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1352                 parentWindow,
1353                 Inkscape::UI::Dialog::EXPORT_TYPES,
1354                 (char const *) _("Select file to export to")
1355                 );
1357     success = exportDialogInstance->show();
1358     if (!success) {
1359         delete exportDialogInstance;
1360         return success;
1361     }
1363     fileName = exportDialogInstance->getFilename();
1365     delete exportDialogInstance;
1366     exportDialogInstance = NULL;;
1368     fileName.append(filename_extension.c_str());
1369     if (fileName.size() > 0) {
1370         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1372         if ( newFileName.size()>0 )
1373             fileName = newFileName;
1374         else
1375             g_warning( "Error converting save filename to UTF-8." );
1376     }
1377     Glib::ustring filePath = export_path;
1378     filePath.append(G_DIR_SEPARATOR_S);
1379     filePath.append(Glib::path_get_basename(fileName));
1381     fileName = filePath;
1383     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1385     if (!success){
1386         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1387         sp_ui_error_dialog(text);
1389         return success;
1390     }
1392     // Start now the submition
1394     // Create the uri
1395     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1396     Glib::ustring uri = "dav://";
1397     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1398     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1399     if (username.empty() || password.empty())
1400     {
1401         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1402         if(!gotSuccess)
1403         {
1404             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1405                     parentWindow,
1406                     (char const *) _("Open Clip Art Login"));
1407             success = exportPasswordDialogInstance->show();
1408             if (!success) {
1409                 delete exportPasswordDialogInstance;
1410                 return success;
1411             }
1412         }
1413         username = exportPasswordDialogInstance->getUsername();
1414         password = exportPasswordDialogInstance->getPassword();
1416         delete exportPasswordDialogInstance;
1417         exportPasswordDialogInstance = NULL;
1418     }
1419     uri.append(username);
1420     uri.append(":");
1421     uri.append(password);
1422     uri.append("@");
1423     uri.append(prefs->getString("/options/ocalurl/str"));
1424     uri.append("/dav.php/");
1425     uri.append(Glib::path_get_basename(fileName));
1427     // Save as a remote file using the dav protocol.
1428     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1429     remove(fileName.c_str());
1430     if (!success)
1431     {
1432         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."));
1433         sp_ui_error_dialog(text);
1434     }
1435     else
1436         gotSuccess = true;
1438     return success;
1440 */
1441 /**
1442  * Export the current document to OCAL
1443  */
1444 /*
1445 void
1446 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1449     // Try to execute the new code and return;
1450     if (!SP_ACTIVE_DOCUMENT)
1451         return;
1452     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1453     if (success)
1454         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1456 */
1458 /*######################
1459 ## I M P O R T  F R O M  O C A L
1460 ######################*/
1462 /**
1463  * Display an ImportToOcal Dialog, and the selected document from OCAL
1464  */
1465 void
1466 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1468     static Glib::ustring import_path;
1470     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1471     if (!doc)
1472         return;
1474     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1476     if (!importDialogInstance) {
1477         importDialogInstance = new
1478              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1479                  parentWindow,
1480                  import_path,
1481                  Inkscape::UI::Dialog::IMPORT_TYPES,
1482                  (char const *)_("Import From Open Clip Art Library"));
1483     }
1485     bool success = importDialogInstance->show();
1486     if (!success) {
1487         delete importDialogInstance;
1488         return;
1489     }
1491     // Get file name and extension type
1492     Glib::ustring fileName = importDialogInstance->getFilename();
1493     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1495     delete importDialogInstance;
1496     importDialogInstance = NULL;
1498     if (fileName.size() > 0) {
1500         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1502         if ( newFileName.size() > 0)
1503             fileName = newFileName;
1504         else
1505             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1507         import_path = fileName;
1508         if (import_path.size()>0)
1509             import_path.append(G_DIR_SEPARATOR_S);
1511         file_import(doc, fileName, selection);
1512     }
1514     return;
1517 /*######################
1518 ## P R I N T
1519 ######################*/
1522 /**
1523  *  Print the current document, if any.
1524  */
1525 void
1526 sp_file_print(Gtk::Window& parentWindow)
1528     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1529     if (doc)
1530         sp_print_document(parentWindow, doc);
1533 /**
1534  * Display what the drawing would look like, if
1535  * printed.
1536  */
1537 void
1538 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1541     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1542     if (doc)
1543         sp_print_preview_document(doc);
1548 /*
1549   Local Variables:
1550   mode:c++
1551   c-file-style:"stroustrup"
1552   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1553   indent-tabs-mode:nil
1554   fill-column:99
1555   End:
1556 */
1557 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :