Code

Pulling in a more recent trunk
[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 "extension/dbus/dbus-init.h"
46 #include "file.h"
47 #include "helper/png-write.h"
48 #include "id-clash.h"
49 #include "inkscape.h"
50 #include "inkscape.h"
51 #include "interface.h"
52 #include "io/sys.h"
53 #include "message.h"
54 #include "message-stack.h"
55 #include "path-prefix.h"
56 #include "preferences.h"
57 #include "print.h"
58 #include "rdf.h"
59 #include "selection-chemistry.h"
60 #include "selection.h"
61 #include "sp-namedview.h"
62 #include "style.h"
63 #include "ui/dialog/ocaldialogs.h"
64 #include "ui/view/view-widget.h"
65 #include "uri.h"
66 #include "xml/rebase-hrefs.h"
68 #ifdef WITH_GNOME_VFS
69 # include <libgnomevfs/gnome-vfs.h>
70 #endif
72 //#ifdef WITH_INKBOARD
73 //#include "jabber_whiteboard/session-manager.h"
74 //#endif
76 #ifdef WIN32
77 #include <windows.h>
78 #endif
80 //#define INK_DUMP_FILENAME_CONV 1
81 #undef INK_DUMP_FILENAME_CONV
83 //#define INK_DUMP_FOPEN 1
84 #undef INK_DUMP_FOPEN
86 void dump_str(gchar const *str, gchar const *prefix);
87 void dump_ustr(Glib::ustring const &ustr);
89 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
90 static void sp_file_add_recent(gchar const *uri)
91 {
92     if(uri == NULL) {
93         g_warning("sp_file_add_recent: uri == NULL");
94         return;
95     }
96     GtkRecentManager *recent = gtk_recent_manager_get_default();
97     gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
98     if (fn) {
99         gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
100         if (uri_to_add) {
101             gtk_recent_manager_add_item(recent, uri_to_add);
102             g_free(uri_to_add);
103         }
104         g_free(fn);
105     }
109 /*######################
110 ## N E W
111 ######################*/
113 /**
114  * Create a blank document and add it to the desktop
115  */
116 SPDesktop*
117 sp_file_new(const Glib::ustring &templ)
119     char *templName = NULL;
120     if (templ.size()>0)
121         templName = (char *)templ.c_str();
122     SPDocument *doc = sp_document_new(templName, TRUE, true);
123     g_return_val_if_fail(doc != NULL, NULL);
125     SPDesktop *dt;
126     if (Inkscape::NSApplication::Application::getNewGui())
127     {
128         dt = Inkscape::NSApplication::Editor::createDesktop (doc);
129     } else {
130         SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
131         g_return_val_if_fail(dtw != NULL, NULL);
132         sp_document_unref(doc);
134         sp_create_window(dtw, TRUE);
135         dt = static_cast<SPDesktop*>(dtw->view);
136         sp_namedview_window_from_document(dt);
137         sp_namedview_update_layers_from_document(dt);
138     }
139     Inkscape::Extension::Dbus::dbus_init_desktop_interface(dt);
140     return dt;
143 SPDesktop* sp_file_new_default()
145     std::list<gchar *> sources;
146     sources.push_back( profile_path("templates") ); // first try user's local dir
147     sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
148     std::list<gchar const*> baseNames;
149     gchar const* localized = _("default.svg");
150     if (strcmp("default.svg", localized) != 0) {
151         baseNames.push_back(localized);
152     }
153     baseNames.push_back("default.svg");
154     gchar *foundTemplate = 0;
156     for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
157         for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
158             gchar *dirname = *it;
159             if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
161                 // TRANSLATORS: default.svg is localizable - this is the name of the default document
162                 //  template. This way you can localize the default pagesize, translate the name of
163                 //  the default layer, etc. If you wish to localize this file, please create a
164                 //  localized share/templates/default.xx.svg file, where xx is your language code.
165                 char *tmp = g_build_filename(dirname, *nameIt, NULL);
166                 if (Inkscape::IO::file_test(tmp, G_FILE_TEST_IS_REGULAR)) {
167                     foundTemplate = tmp;
168                 } else {
169                     g_free(tmp);
170                 }
171             }
172         }
173     }
175     for (std::list<gchar *>::iterator it = sources.begin(); it != sources.end(); ++it) {
176         g_free(*it);
177     }
179     SPDesktop* desk = sp_file_new(foundTemplate ? foundTemplate : "");
180     if (foundTemplate) {
181         g_free(foundTemplate);
182         foundTemplate = 0;
183     }
184     return desk;
188 /*######################
189 ## D E L E T E
190 ######################*/
192 /**
193  *  Perform document closures preceding an exit()
194  */
195 void
196 sp_file_exit()
198     sp_ui_close_all();
199     // no need to call inkscape_exit here; last document being closed will take care of that
203 /*######################
204 ## O P E N
205 ######################*/
207 /**
208  *  Open a file, add the document to the desktop
209  *
210  *  \param replace_empty if true, and the current desktop is empty, this document
211  *  will replace the empty one.
212  */
213 bool
214 sp_file_open(const Glib::ustring &uri,
215              Inkscape::Extension::Extension *key,
216              bool add_to_recent, bool replace_empty)
218     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
219     if (desktop)
220         desktop->setWaitingCursor();
222     SPDocument *doc = NULL;
223     try {
224         doc = Inkscape::Extension::open(key, uri.c_str());
225     } catch (Inkscape::Extension::Input::no_extension_found &e) {
226         doc = NULL;
227     } catch (Inkscape::Extension::Input::open_failed &e) {
228         doc = NULL;
229     }
231     if (desktop)
232         desktop->clearWaitingCursor();
234     if (doc) {
235         SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
237         if (existing && existing->virgin && replace_empty) {
238             // If the current desktop is empty, open the document there
239             sp_document_ensure_up_to_date (doc);
240             desktop->change_document(doc);
241             sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
242         } else {
243             if (!Inkscape::NSApplication::Application::getNewGui()) {
244                 // create a whole new desktop and window
245                 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
246                 sp_create_window(dtw, TRUE);
247                 desktop = static_cast<SPDesktop*>(dtw->view);
248             } else {
249                 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
250             }
251         }
253         doc->virgin = FALSE;
254         // everyone who cares now has a reference, get rid of ours
255         sp_document_unref(doc);
256         // resize the window to match the document properties
257         sp_namedview_window_from_document(desktop);
258         sp_namedview_update_layers_from_document(desktop);
260         if (add_to_recent) {
261             sp_file_add_recent(SP_DOCUMENT_URI(doc));
262         }
264         return TRUE;
265     } else {
266         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
267         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
268         sp_ui_error_dialog(text);
269         g_free(text);
270         g_free(safeUri);
271         return FALSE;
272     }
275 /**
276  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
277  */
278 void
279 sp_file_revert_dialog()
281     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
282     g_assert(desktop != NULL);
284     SPDocument *doc = sp_desktop_document(desktop);
285     g_assert(doc != NULL);
287     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
288     g_assert(repr != NULL);
290     gchar const *uri = doc->uri;
291     if (!uri) {
292         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
293         return;
294     }
296     bool do_revert = true;
297     if (doc->isModifiedSinceSave()) {
298         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
300         bool response = desktop->warnDialog (text);
301         g_free(text);
303         if (!response) {
304             do_revert = false;
305         }
306     }
308     bool reverted;
309     if (do_revert) {
310         // Allow overwriting of current document.
311         doc->virgin = TRUE;
313         // remember current zoom and view
314         double zoom = desktop->current_zoom();
315         Geom::Point c = desktop->get_display_area().midpoint();
317         reverted = sp_file_open(uri,NULL);
318         if (reverted) {
319             // restore zoom and view
320             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
321         }
322     } else {
323         reverted = false;
324     }
326     if (reverted) {
327         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
328     } else {
329         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
330     }
333 void dump_str(gchar const *str, gchar const *prefix)
335     Glib::ustring tmp;
336     tmp = prefix;
337     tmp += " [";
338     size_t const total = strlen(str);
339     for (unsigned i = 0; i < total; i++) {
340         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
341         tmp += tmp2;
342         g_free(tmp2);
343     }
345     tmp += "]";
346     g_message("%s", tmp.c_str());
349 void dump_ustr(Glib::ustring const &ustr)
351     char const *cstr = ustr.c_str();
352     char const *data = ustr.data();
353     Glib::ustring::size_type const byteLen = ustr.bytes();
354     Glib::ustring::size_type const dataLen = ustr.length();
355     Glib::ustring::size_type const cstrLen = strlen(cstr);
357     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
358               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
359     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
360     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
362     try {
363         Glib::ustring tmp;
364         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
365             tmp = "    ";
366             if (i < dataLen) {
367                 Glib::ustring::value_type val = ustr.at(i);
368                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
369                 tmp += tmp2;
370                 g_free( tmp2 );
371             } else {
372                 tmp += "    ";
373             }
375             if (i < byteLen) {
376                 int val = (0x0ff & data[i]);
377                 gchar *tmp2 = g_strdup_printf("    %02x", val);
378                 tmp += tmp2;
379                 g_free( tmp2 );
380                 if ( val > 32 && val < 127 ) {
381                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
382                     tmp += tmp2;
383                     g_free( tmp2 );
384                 } else {
385                     tmp += "    . ";
386                 }
387             } else {
388                 tmp += "       ";
389             }
391             if ( i < cstrLen ) {
392                 int val = (0x0ff & cstr[i]);
393                 gchar* tmp2 = g_strdup_printf("    %02x", val);
394                 tmp += tmp2;
395                 g_free(tmp2);
396                 if ( val > 32 && val < 127 ) {
397                     tmp2 = g_strdup_printf("   '%c'", (gchar) val);
398                     tmp += tmp2;
399                     g_free( tmp2 );
400                 } else {
401                     tmp += "    . ";
402                 }
403             } else {
404                 tmp += "            ";
405             }
407             g_message( "%s", tmp.c_str() );
408         }
409     } catch (...) {
410         g_message("XXXXXXXXXXXXXXXXXX Exception" );
411     }
412     g_message("---------------");
415 /**
416  *  Display an file Open selector.  Open a document if OK is pressed.
417  *  Can select single or multiple files for opening.
418  */
419 void
420 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
422     //# Get the current directory for finding files
423     static Glib::ustring open_path;
424     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
426     if(open_path.empty())
427     {
428         Glib::ustring attr = prefs->getString("/dialogs/open/path");
429         if (!attr.empty()) open_path = attr;
430     }
432     //# Test if the open_path directory exists
433     if (!Inkscape::IO::file_test(open_path.c_str(),
434               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
435         open_path = "";
437 #ifdef WIN32
438     //# If no open path, default to our win32 documents folder
439     if (open_path.empty())
440     {
441         // The path to the My Documents folder is read from the
442         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
443         HKEY key = NULL;
444         if(RegOpenKeyExA(HKEY_CURRENT_USER,
445             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
446             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
447         {
448             WCHAR utf16path[_MAX_PATH];
449             DWORD value_type;
450             DWORD data_size = sizeof(utf16path);
451             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
452                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
453             {
454                 g_assert(value_type == REG_SZ);
455                 gchar *utf8path = g_utf16_to_utf8(
456                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
457                 if(utf8path)
458                 {
459                     open_path = Glib::ustring(utf8path);
460                     g_free(utf8path);
461                 }
462             }
463         }
464     }
465 #endif
467     //# If no open path, default to our home directory
468     if (open_path.empty())
469     {
470         open_path = g_get_home_dir();
471         open_path.append(G_DIR_SEPARATOR_S);
472     }
474     //# Create a dialog
475     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
476               Inkscape::UI::Dialog::FileOpenDialog::create(
477                  parentWindow, open_path,
478                  Inkscape::UI::Dialog::SVG_TYPES,
479                  _("Select file to open"));
481     //# Show the dialog
482     bool const success = openDialogInstance->show();
484     //# Save the folder the user selected for later
485     open_path = openDialogInstance->getCurrentDirectory();
487     if (!success)
488     {
489         delete openDialogInstance;
490         return;
491     }
493     //# User selected something.  Get name and type
494     Glib::ustring fileName = openDialogInstance->getFilename();
496     Inkscape::Extension::Extension *selection =
497             openDialogInstance->getSelectionType();
499     //# Code to check & open if multiple files.
500     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
502     //# We no longer need the file dialog object - delete it
503     delete openDialogInstance;
504     openDialogInstance = NULL;
506     //# Iterate through filenames if more than 1
507     if (flist.size() > 1)
508     {
509         for (unsigned int i = 0; i < flist.size(); i++)
510         {
511             fileName = flist[i];
513             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
514             if ( newFileName.size() > 0 )
515                 fileName = newFileName;
516             else
517                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
519 #ifdef INK_DUMP_FILENAME_CONV
520             g_message("Opening File %s\n", fileName.c_str());
521 #endif
522             sp_file_open(fileName, selection);
523         }
525         return;
526     }
529     if (!fileName.empty())
530     {
531         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
533         if ( newFileName.size() > 0)
534             fileName = newFileName;
535         else
536             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
538         open_path = Glib::path_get_dirname (fileName);
539         open_path.append(G_DIR_SEPARATOR_S);
540         prefs->setString("/dialogs/open/path", open_path);
542         sp_file_open(fileName, selection);
543     }
545     return;
549 /*######################
550 ## V A C U U M
551 ######################*/
553 /**
554  * Remove unreferenced defs from the defs section of the document.
555  */
558 void
559 sp_file_vacuum()
561     SPDocument *doc = SP_ACTIVE_DOCUMENT;
563     unsigned int diff = vacuum_document (doc);
565     sp_document_done(doc, SP_VERB_FILE_VACUUM,
566                      _("Vacuum &lt;defs&gt;"));
568     SPDesktop *dt = SP_ACTIVE_DESKTOP;
569     if (diff > 0) {
570         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
571                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
572                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
573                          diff),
574                 diff);
575     } else {
576         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
577     }
582 /*######################
583 ## S A V E
584 ######################*/
586 /**
587  * This 'save' function called by the others below
588  *
589  * \param    official  whether to set :output_module and :modified in the
590  *                     document; is true for normal save, false for temporary saves
591  */
592 static bool
593 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
594           Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
595           Inkscape::Extension::FileSaveMethod save_method)
597     if (!doc || uri.size()<1) //Safety check
598         return false;
600     try {
601         Inkscape::Extension::save(key, doc, uri.c_str(),
602                                   false,
603                                   checkoverwrite, official,
604                                   save_method);
605     } catch (Inkscape::Extension::Output::no_extension_found &e) {
606         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
607         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
608         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
609         sp_ui_error_dialog(text);
610         g_free(text);
611         g_free(safeUri);
612         return FALSE;
613     } catch (Inkscape::Extension::Output::file_read_only &e) {
614         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
615         gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
616         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
617         sp_ui_error_dialog(text);
618         g_free(text);
619         g_free(safeUri);
620         return FALSE;
621     } catch (Inkscape::Extension::Output::save_failed &e) {
622         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
623         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
624         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
625         sp_ui_error_dialog(text);
626         g_free(text);
627         g_free(safeUri);
628         return FALSE;
629     } catch (Inkscape::Extension::Output::save_cancelled &e) {
630         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
631         return FALSE;
632     } catch (Inkscape::Extension::Output::no_overwrite &e) {
633         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
634     } catch (...) {
635         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
636         return FALSE;
637     }
639     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
640     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
641     return true;
644 /*
645  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
646  */
647 bool
648 file_save_remote(SPDocument */*doc*/,
649     #ifdef WITH_GNOME_VFS
650                  const Glib::ustring &uri,
651     #else
652                  const Glib::ustring &/*uri*/,
653     #endif
654                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
656 #ifdef WITH_GNOME_VFS
658 #define BUF_SIZE 8192
659     gnome_vfs_init();
661     GnomeVFSHandle    *from_handle = NULL;
662     GnomeVFSHandle    *to_handle = NULL;
663     GnomeVFSFileSize  bytes_read;
664     GnomeVFSFileSize  bytes_written;
665     GnomeVFSResult    result;
666     guint8 buffer[8192];
668     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
670     if ( uri_local == NULL ) {
671         g_warning( "Error converting filename to locale encoding.");
672     }
674     // Gets the temp file name.
675     Glib::ustring fileName = Glib::get_tmp_dir ();
676     fileName.append(G_DIR_SEPARATOR_S);
677     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
679     // Open the temp file to send.
680     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
682     if (result != GNOME_VFS_OK) {
683         g_warning("Could not find the temp saving.");
684         return false;
685     }
687     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
688     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
690     if (result != GNOME_VFS_OK) {
691         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
692         return false;
693     }
695     while (1) {
697         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
699         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
700             result = gnome_vfs_close (from_handle);
701             result = gnome_vfs_close (to_handle);
702             return true;
703         }
705         if (result != GNOME_VFS_OK) {
706             g_warning("%s", gnome_vfs_result_to_string(result));
707             return false;
708         }
709         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
710         if (result != GNOME_VFS_OK) {
711             g_warning("%s", gnome_vfs_result_to_string(result));
712             return false;
713         }
716         if (bytes_read != bytes_written){
717             return false;
718         }
720     }
721     return true;
722 #else
723     // in case we do not have GNOME_VFS
724     return false;
725 #endif
730 /**
731  *  Display a SaveAs dialog.  Save the document if OK pressed.
732  */
733 bool
734 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
736     Inkscape::Extension::Output *extension = 0;
737     bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
739     // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
740     //       filename_extension only uses ".svg"
741     Glib::ustring default_extension;
742     Glib::ustring filename_extension = ".svg";
744     default_extension= Inkscape::Extension::get_file_save_extension(save_method);
745     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
747     extension = dynamic_cast<Inkscape::Extension::Output *>
748         (Inkscape::Extension::db.get(default_extension.c_str()));
750     if (extension)
751         filename_extension = extension->get_extension();
753     Glib::ustring save_path;
754     Glib::ustring save_loc;
756     save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
758     if (!Inkscape::IO::file_test(save_path.c_str(),
759           (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
760         save_path = "";
762     if (save_path.size()<1)
763         save_path = g_get_home_dir();
765     save_loc = save_path;
766     save_loc.append(G_DIR_SEPARATOR_S);
768     char formatBuf[256];
769     int i = 1;
770     if (!doc->uri) {
771         // We are saving for the first time; create a unique default filename
772         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
773         save_loc.append(formatBuf);
775         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
776             save_loc = save_path;
777             save_loc.append(G_DIR_SEPARATOR_S);
778             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
779             save_loc.append(formatBuf);
780         }
781     } else {
782         snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
783         save_loc.append(formatBuf);
784     }
786     // convert save_loc from utf-8 to locale
787     // is this needed any more, now that everything is handled in
788     // Inkscape::IO?
789     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
791     if ( save_loc_local.size() > 0)
792         save_loc = save_loc_local;
794     //# Show the SaveAs dialog
795     char const * dialog_title;
796     if (is_copy) {
797         dialog_title = (char const *) _("Select file to save a copy to");
798     } else {
799         dialog_title = (char const *) _("Select file to save to");
800     }
801     gchar* doc_title = doc->root->title();
802     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
803         Inkscape::UI::Dialog::FileSaveDialog::create(
804             parentWindow,
805             save_loc,
806             Inkscape::UI::Dialog::SVG_TYPES,
807             dialog_title,
808             default_extension,
809             doc_title ? doc_title : "",
810             save_method
811             );
813     saveDialog->setSelectionType(extension);
815     bool success = saveDialog->show();
816     if (!success) {
817         delete saveDialog;
818         return success;
819     }
821     // set new title here (call RDF to ensure metadata and title element are updated)
822     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
823     // free up old string
824     if(doc_title) g_free(doc_title);
826     Glib::ustring fileName = saveDialog->getFilename();
827     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
829     delete saveDialog;
831     saveDialog = 0;
833     if (fileName.size() > 0) {
834         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
836         if ( newFileName.size()>0 )
837             fileName = newFileName;
838         else
839             g_warning( "Error converting save filename to UTF-8." );
841         // FIXME: does the argument !is_copy really convey the correct meaning here?
842         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
844         if (success && SP_DOCUMENT_URI(doc)) {
845             sp_file_add_recent(SP_DOCUMENT_URI(doc));
846         }
848         save_path = Glib::path_get_dirname(fileName);
849         Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
851         return success;
852     }
855     return false;
859 /**
860  * Save a document, displaying a SaveAs dialog if necessary.
861  */
862 bool
863 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
865     bool success = true;
867     if (doc->isModifiedSinceSave()) {
868         if ( doc->uri == NULL )
869         {
870             // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
871             // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
872             // benutzte "Save as ..."-Endung
873             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
874         } else {
875             Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
876             Glib::ustring fn = g_strdup(doc->uri);
877             // Try to determine the extension from the uri; this may not lead to a valid extension,
878             // but this case is caught in the file_save method below (or rather in Extension::save()
879             // further down the line).
880             Glib::ustring ext = "";
881             Glib::ustring::size_type pos = fn.rfind('.');
882             if (pos != Glib::ustring::npos) {
883                 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
884                 // but hopefully it's a reasonable workaround for now
885                 ext = fn.substr( pos );
886             }
887             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
888             if (success == false) {
889                 // give the user the chance to change filename or extension
890                 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
891             }
892         }
893     } else {
894         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
895         success = TRUE;
896     }
898     return success;
902 /**
903  * Save a document.
904  */
905 bool
906 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
908     if (!SP_ACTIVE_DOCUMENT)
909         return false;
911     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
913     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
914     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
918 /**
919  *  Save a document, always displaying the SaveAs dialog.
920  */
921 bool
922 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
924     if (!SP_ACTIVE_DOCUMENT)
925         return false;
926     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
927     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
932 /**
933  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
934  */
935 bool
936 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
938     if (!SP_ACTIVE_DOCUMENT)
939         return false;
940     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
941     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
945 /*######################
946 ## I M P O R T
947 ######################*/
949 /**
950  *  Import a resource.  Called by sp_file_import()
951  */
952 void
953 file_import(SPDocument *in_doc, const Glib::ustring &uri,
954                Inkscape::Extension::Extension *key)
956     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
958     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
959     SPDocument *doc;
960     try {
961         doc = Inkscape::Extension::open(key, uri.c_str());
962     } catch (Inkscape::Extension::Input::no_extension_found &e) {
963         doc = NULL;
964     } catch (Inkscape::Extension::Input::open_failed &e) {
965         doc = NULL;
966     }
968     if (doc != NULL) {
969         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
970         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
972         prevent_id_clashes(doc, in_doc);
974         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
975         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
977         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
979         // Count the number of top-level items in the imported document.
980         guint items_count = 0;
981         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
982              child != NULL; child = SP_OBJECT_NEXT(child))
983         {
984             if (SP_IS_ITEM(child)) items_count++;
985         }
987         // Create a new group if necessary.
988         Inkscape::XML::Node *newgroup = NULL;
989         if ((style && style->firstChild()) || items_count > 1) {
990             newgroup = xml_in_doc->createElement("svg:g");
991             sp_repr_css_set(newgroup, style, "style");
992         }
994         // Determine the place to insert the new object.
995         // This will be the current layer, if possible.
996         // FIXME: If there's no desktop (command line run?) we need
997         //        a document:: method to return the current layer.
998         //        For now, we just use the root in this case.
999         SPObject *place_to_insert;
1000         if (desktop) {
1001             place_to_insert = desktop->currentLayer();
1002         } else {
1003             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
1004         }
1006         // Construct a new object representing the imported image,
1007         // and insert it into the current document.
1008         SPObject *new_obj = NULL;
1009         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
1010              child != NULL; child = SP_OBJECT_NEXT(child) )
1011         {
1012             if (SP_IS_ITEM(child)) {
1013                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
1015                 // convert layers to groups, and make sure they are unlocked
1016                 // FIXME: add "preserve layers" mode where each layer from
1017                 //        import is copied to the same-named layer in host
1018                 newitem->setAttribute("inkscape:groupmode", NULL);
1019                 newitem->setAttribute("sodipodi:insensitive", NULL);
1021                 if (newgroup) newgroup->appendChild(newitem);
1022                 else new_obj = place_to_insert->appendChildRepr(newitem);
1023             }
1025             // don't lose top-level defs or style elements
1026             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1027                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1028                 if (!strcmp(tag, "svg:defs")) {
1029                     for (SPObject *x = sp_object_first_child(child);
1030                          x != NULL; x = SP_OBJECT_NEXT(x))
1031                     {
1032                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1033                     }
1034                 }
1035                 else if (!strcmp(tag, "svg:style")) {
1036                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1037                 }
1038             }
1039         }
1040         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1042         // release some stuff
1043         if (newgroup) Inkscape::GC::release(newgroup);
1044         if (style) sp_repr_css_attr_unref(style);
1046         // select and move the imported item
1047         if (new_obj && SP_IS_ITEM(new_obj)) {
1048             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1049             selection->set(SP_ITEM(new_obj));
1051             // preserve parent and viewBox transformations
1052             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1053             sp_document_ensure_up_to_date(doc);
1054             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1055             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1057             // move to mouse pointer
1058             {
1059                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1060                 Geom::OptRect sel_bbox = selection->bounds();
1061                 if (sel_bbox) {
1062                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1063                     sp_selection_move_relative(selection, m, false);
1064                 }
1065             }
1066         }
1068         sp_document_unref(doc);
1069         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1070                          _("Import"));
1072     } else {
1073         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1074         sp_ui_error_dialog(text);
1075         g_free(text);
1076     }
1078     return;
1082 /**
1083  *  Display an Open dialog, import a resource if OK pressed.
1084  */
1085 void
1086 sp_file_import(Gtk::Window &parentWindow)
1088     static Glib::ustring import_path;
1090     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1091     if (!doc)
1092         return;
1094     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1096     if(import_path.empty())
1097     {
1098         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1099         if (!attr.empty()) import_path = attr;
1100     }
1102     //# Test if the import_path directory exists
1103     if (!Inkscape::IO::file_test(import_path.c_str(),
1104               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1105         import_path = "";
1107     //# If no open path, default to our home directory
1108     if (import_path.empty())
1109     {
1110         import_path = g_get_home_dir();
1111         import_path.append(G_DIR_SEPARATOR_S);
1112     }
1114     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1115     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1116              Inkscape::UI::Dialog::FileOpenDialog::create(
1117                  parentWindow,
1118                  import_path,
1119                  Inkscape::UI::Dialog::IMPORT_TYPES,
1120                  (char const *)_("Select file to import"));
1122     bool success = importDialogInstance->show();
1123     if (!success) {
1124         delete importDialogInstance;
1125         return;
1126     }
1128     //# Get file name and extension type
1129     Glib::ustring fileName = importDialogInstance->getFilename();
1130     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1132     delete importDialogInstance;
1133     importDialogInstance = NULL;
1135     if (fileName.size() > 0) {
1137         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1139         if ( newFileName.size() > 0)
1140             fileName = newFileName;
1141         else
1142             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1144         import_path = Glib::path_get_dirname (fileName);
1145         import_path.append(G_DIR_SEPARATOR_S);
1146         prefs->setString("/dialogs/import/path", import_path);
1148         file_import(doc, fileName, selection);
1149     }
1151     return;
1156 /*######################
1157 ## E X P O R T
1158 ######################*/
1161 #ifdef NEW_EXPORT_DIALOG
1163 /**
1164  *  Display an Export dialog, export as the selected type if OK pressed
1165  */
1166 bool
1167 sp_file_export_dialog(Gtk::Window &parentWindow)
1169     //# temp hack for 'doc' until we can switch to this dialog
1170     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1172     Glib::ustring export_path;
1173     Glib::ustring export_loc;
1175     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1176     Inkscape::Extension::Output *extension;
1178     //# Get the default extension name
1179     Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1180     if(default_extension.empty()) {
1181         // FIXME: Is this a good default? Should there be a macro for the string?
1182         default_extension = "org.inkscape.output.png.cairo";
1183     }
1184     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1186     if (doc->uri == NULL)
1187         {
1188         char formatBuf[256];
1190         Glib::ustring filename_extension = ".svg";
1191         extension = dynamic_cast<Inkscape::Extension::Output *>
1192               (Inkscape::Extension::db.get(default_extension.c_str()));
1193         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1194         if (extension)
1195             filename_extension = extension->get_extension();
1197         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1198         if (!attr3.empty())
1199             export_path = attr3;
1201         if (!Inkscape::IO::file_test(export_path.c_str(),
1202               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1203             export_path = "";
1205         if (export_path.size()<1)
1206             export_path = g_get_home_dir();
1208         export_loc = export_path;
1209         export_loc.append(G_DIR_SEPARATOR_S);
1210         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1211         export_loc.append(formatBuf);
1213         }
1214     else
1215         {
1216         export_path = Glib::path_get_dirname(doc->uri);
1217         }
1219     // convert save_loc from utf-8 to locale
1220     // is this needed any more, now that everything is handled in
1221     // Inkscape::IO?
1222     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1223     if ( export_path_local.size() > 0)
1224         export_path = export_path_local;
1226     //# Show the Export dialog
1227     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1228         Inkscape::UI::Dialog::FileExportDialog::create(
1229             parentWindow,
1230             export_path,
1231             Inkscape::UI::Dialog::EXPORT_TYPES,
1232             (char const *) _("Select file to export to"),
1233             default_extension
1234         );
1236     bool success = exportDialogInstance->show();
1237     if (!success) {
1238         delete exportDialogInstance;
1239         return success;
1240     }
1242     Glib::ustring fileName = exportDialogInstance->getFilename();
1244     Inkscape::Extension::Extension *selectionType =
1245         exportDialogInstance->getSelectionType();
1247     delete exportDialogInstance;
1248     exportDialogInstance = NULL;
1250     if (fileName.size() > 0) {
1251         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1253         if ( newFileName.size()>0 )
1254             fileName = newFileName;
1255         else
1256             g_warning( "Error converting save filename to UTF-8." );
1258         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1260         if (success) {
1261             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1262             recent->add_item(SP_DOCUMENT_URI(doc));
1263         }
1265         export_path = fileName;
1266         prefs->setString("/dialogs/save_export/path", export_path);
1268         return success;
1269     }
1272     return false;
1275 #else
1277 /**
1278  *
1279  */
1280 bool
1281 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1283     sp_export_dialog();
1284     return true;
1287 #endif
1289 /*######################
1290 ## E X P O R T  T O  O C A L
1291 ######################*/
1293 /**
1294  *  Display an Export dialog, export as the selected type if OK pressed
1295  */
1296 /*
1297 bool
1298 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1301    if (!SP_ACTIVE_DOCUMENT)
1302         return false;
1304     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1306     Glib::ustring export_path;
1307     Glib::ustring export_loc;
1308     Glib::ustring fileName;
1309     Inkscape::Extension::Extension *selectionType;
1311     bool success = false;
1313     static bool gotSuccess = false;
1315     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1316     (void)repr;
1318     if (!doc->uri && !doc->isModifiedSinceSave())
1319         return false;
1321     //  Get the default extension name
1322     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1323     char formatBuf[256];
1325     Glib::ustring filename_extension = ".svg";
1326     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1328     export_path = Glib::get_tmp_dir ();
1330     export_loc = export_path;
1331     export_loc.append(G_DIR_SEPARATOR_S);
1332     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1333     export_loc.append(formatBuf);
1335     // convert save_loc from utf-8 to locale
1336     // is this needed any more, now that everything is handled in
1337     // Inkscape::IO?
1338     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1339     if ( export_path_local.size() > 0)
1340         export_path = export_path_local;
1342     // Show the Export To OCAL dialog
1343     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1344         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1345                 parentWindow,
1346                 Inkscape::UI::Dialog::EXPORT_TYPES,
1347                 (char const *) _("Select file to export to")
1348                 );
1350     success = exportDialogInstance->show();
1351     if (!success) {
1352         delete exportDialogInstance;
1353         return success;
1354     }
1356     fileName = exportDialogInstance->getFilename();
1358     delete exportDialogInstance;
1359     exportDialogInstance = NULL;;
1361     fileName.append(filename_extension.c_str());
1362     if (fileName.size() > 0) {
1363         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1365         if ( newFileName.size()>0 )
1366             fileName = newFileName;
1367         else
1368             g_warning( "Error converting save filename to UTF-8." );
1369     }
1370     Glib::ustring filePath = export_path;
1371     filePath.append(G_DIR_SEPARATOR_S);
1372     filePath.append(Glib::path_get_basename(fileName));
1374     fileName = filePath;
1376     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1378     if (!success){
1379         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1380         sp_ui_error_dialog(text);
1382         return success;
1383     }
1385     // Start now the submition
1387     // Create the uri
1388     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1389     Glib::ustring uri = "dav://";
1390     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1391     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1392     if (username.empty() || password.empty())
1393     {
1394         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1395         if(!gotSuccess)
1396         {
1397             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1398                     parentWindow,
1399                     (char const *) _("Open Clip Art Login"));
1400             success = exportPasswordDialogInstance->show();
1401             if (!success) {
1402                 delete exportPasswordDialogInstance;
1403                 return success;
1404             }
1405         }
1406         username = exportPasswordDialogInstance->getUsername();
1407         password = exportPasswordDialogInstance->getPassword();
1409         delete exportPasswordDialogInstance;
1410         exportPasswordDialogInstance = NULL;
1411     }
1412     uri.append(username);
1413     uri.append(":");
1414     uri.append(password);
1415     uri.append("@");
1416     uri.append(prefs->getString("/options/ocalurl/str"));
1417     uri.append("/dav.php/");
1418     uri.append(Glib::path_get_basename(fileName));
1420     // Save as a remote file using the dav protocol.
1421     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1422     remove(fileName.c_str());
1423     if (!success)
1424     {
1425         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."));
1426         sp_ui_error_dialog(text);
1427     }
1428     else
1429         gotSuccess = true;
1431     return success;
1433 */
1434 /**
1435  * Export the current document to OCAL
1436  */
1437 /*
1438 void
1439 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1442     // Try to execute the new code and return;
1443     if (!SP_ACTIVE_DOCUMENT)
1444         return;
1445     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1446     if (success)
1447         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1449 */
1451 /*######################
1452 ## I M P O R T  F R O M  O C A L
1453 ######################*/
1455 /**
1456  * Display an ImportToOcal Dialog, and the selected document from OCAL
1457  */
1458 void
1459 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1461     static Glib::ustring import_path;
1463     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1464     if (!doc)
1465         return;
1467     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1469     if (!importDialogInstance) {
1470         importDialogInstance = new
1471              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1472                  parentWindow,
1473                  import_path,
1474                  Inkscape::UI::Dialog::IMPORT_TYPES,
1475                  (char const *)_("Import From Open Clip Art Library"));
1476     }
1478     bool success = importDialogInstance->show();
1479     if (!success) {
1480         delete importDialogInstance;
1481         return;
1482     }
1484     // Get file name and extension type
1485     Glib::ustring fileName = importDialogInstance->getFilename();
1486     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1488     delete importDialogInstance;
1489     importDialogInstance = NULL;
1491     if (fileName.size() > 0) {
1493         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1495         if ( newFileName.size() > 0)
1496             fileName = newFileName;
1497         else
1498             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1500         import_path = fileName;
1501         if (import_path.size()>0)
1502             import_path.append(G_DIR_SEPARATOR_S);
1504         file_import(doc, fileName, selection);
1505     }
1507     return;
1510 /*######################
1511 ## P R I N T
1512 ######################*/
1515 /**
1516  *  Print the current document, if any.
1517  */
1518 void
1519 sp_file_print(Gtk::Window& parentWindow)
1521     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1522     if (doc)
1523         sp_print_document(parentWindow, doc);
1526 /**
1527  * Display what the drawing would look like, if
1528  * printed.
1529  */
1530 void
1531 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1534     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1535     if (doc)
1536         sp_print_preview_document(doc);
1541 /*
1542   Local Variables:
1543   mode:c++
1544   c-file-style:"stroustrup"
1545   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1546   indent-tabs-mode:nil
1547   fill-column:99
1548   End:
1549 */
1550 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :