Code

Patch by Diederik for 168384. Tested by myself and others and works well, additionall...
[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_INKBOARD
72 #include "jabber_whiteboard/session-manager.h"
73 #endif
75 #ifdef WIN32
76 #include <windows.h>
77 #endif
79 //#define INK_DUMP_FILENAME_CONV 1
80 #undef INK_DUMP_FILENAME_CONV
82 //#define INK_DUMP_FOPEN 1
83 #undef INK_DUMP_FOPEN
85 void dump_str(gchar const *str, gchar const *prefix);
86 void dump_ustr(Glib::ustring const &ustr);
88 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
89 static void sp_file_add_recent(gchar const *uri)
90 {
91     if(uri == NULL) {
92         g_warning("sp_file_add_recent: uri == NULL");
93         return;
94     }
95     GtkRecentManager *recent = gtk_recent_manager_get_default();
96     gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
97     if (fn) {
98         gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
99         if (uri_to_add) {
100             gtk_recent_manager_add_item(recent, uri_to_add);
101             g_free(uri_to_add);
102         }
103         g_free(fn);
104     }
108 /*######################
109 ## N E W
110 ######################*/
112 /**
113  * Create a blank document and add it to the desktop
114  */
115 SPDesktop*
116 sp_file_new(const Glib::ustring &templ)
118     char *templName = NULL;
119     if (templ.size()>0)
120         templName = (char *)templ.c_str();
121     SPDocument *doc = sp_document_new(templName, TRUE, true);
122     g_return_val_if_fail(doc != NULL, NULL);
124     SPDesktop *dt;
125     if (Inkscape::NSApplication::Application::getNewGui())
126     {
127         dt = Inkscape::NSApplication::Editor::createDesktop (doc);
128     } else {
129         SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
130         g_return_val_if_fail(dtw != NULL, NULL);
131         sp_document_unref(doc);
133         sp_create_window(dtw, TRUE);
134         dt = static_cast<SPDesktop*>(dtw->view);
135         sp_namedview_window_from_document(dt);
136         sp_namedview_update_layers_from_document(dt);
137     }
138     return dt;
141 SPDesktop*
142 sp_file_new_default()
144     std::list<gchar *> sources;
145     sources.push_back( profile_path("templates") ); // first try user's local dir
146     sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
148     while (!sources.empty()) {
149         gchar *dirname = sources.front();
150         if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
152             // TRANSLATORS: default.svg is localizable - this is the name of the default document
153             //  template. This way you can localize the default pagesize, translate the name of
154             //  the default layer, etc. If you wish to localize this file, please create a
155             //  localized share/templates/default.xx.svg file, where xx is your language code.
156             char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
157             if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
158                 return sp_file_new(default_template);
159             }
160         }
161         g_free(dirname);
162         sources.pop_front();
163     }
165     return sp_file_new("");
169 /*######################
170 ## D E L E T E
171 ######################*/
173 /**
174  *  Perform document closures preceding an exit()
175  */
176 void
177 sp_file_exit()
179     sp_ui_close_all();
180     // no need to call inkscape_exit here; last document being closed will take care of that
184 /*######################
185 ## O P E N
186 ######################*/
188 /**
189  *  Open a file, add the document to the desktop
190  *
191  *  \param replace_empty if true, and the current desktop is empty, this document
192  *  will replace the empty one.
193  */
194 bool
195 sp_file_open(const Glib::ustring &uri,
196              Inkscape::Extension::Extension *key,
197              bool add_to_recent, bool replace_empty)
199     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
200     if (desktop)
201         desktop->setWaitingCursor();
203     SPDocument *doc = NULL;
204     try {
205         doc = Inkscape::Extension::open(key, uri.c_str());
206     } catch (Inkscape::Extension::Input::no_extension_found &e) {
207         doc = NULL;
208     } catch (Inkscape::Extension::Input::open_failed &e) {
209         doc = NULL;
210     }
212     if (desktop)
213         desktop->clearWaitingCursor();
215     if (doc) {
216         SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
218         if (existing && existing->virgin && replace_empty) {
219             // If the current desktop is empty, open the document there
220             sp_document_ensure_up_to_date (doc);
221             desktop->change_document(doc);
222             sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
223         } else {
224             if (!Inkscape::NSApplication::Application::getNewGui()) {
225                 // create a whole new desktop and window
226                 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
227                 sp_create_window(dtw, TRUE);
228                 desktop = static_cast<SPDesktop*>(dtw->view);
229             } else {
230                 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
231             }
232         }
234         doc->virgin = FALSE;
235         // everyone who cares now has a reference, get rid of ours
236         sp_document_unref(doc);
237         // resize the window to match the document properties
238         sp_namedview_window_from_document(desktop);
239         sp_namedview_update_layers_from_document(desktop);
241         if (add_to_recent) {
242             sp_file_add_recent(SP_DOCUMENT_URI(doc));
243         }
245         return TRUE;
246     } else {
247         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
248         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
249         sp_ui_error_dialog(text);
250         g_free(text);
251         g_free(safeUri);
252         return FALSE;
253     }
256 /**
257  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
258  */
259 void
260 sp_file_revert_dialog()
262     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
263     g_assert(desktop != NULL);
265     SPDocument *doc = sp_desktop_document(desktop);
266     g_assert(doc != NULL);
268     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
269     g_assert(repr != NULL);
271     gchar const *uri = doc->uri;
272     if (!uri) {
273         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
274         return;
275     }
277     bool do_revert = true;
278     if (doc->isModifiedSinceSave()) {
279         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
281         bool response = desktop->warnDialog (text);
282         g_free(text);
284         if (!response) {
285             do_revert = false;
286         }
287     }
289     bool reverted;
290     if (do_revert) {
291         // Allow overwriting of current document.
292         doc->virgin = TRUE;
294         // remember current zoom and view
295         double zoom = desktop->current_zoom();
296         Geom::Point c = desktop->get_display_area().midpoint();
298         reverted = sp_file_open(uri,NULL);
299         if (reverted) {
300             // restore zoom and view
301             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
302         }
303     } else {
304         reverted = false;
305     }
307     if (reverted) {
308         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
309     } else {
310         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
311     }
314 void dump_str(gchar const *str, gchar const *prefix)
316     Glib::ustring tmp;
317     tmp = prefix;
318     tmp += " [";
319     size_t const total = strlen(str);
320     for (unsigned i = 0; i < total; i++) {
321         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
322         tmp += tmp2;
323         g_free(tmp2);
324     }
326     tmp += "]";
327     g_message("%s", tmp.c_str());
330 void dump_ustr(Glib::ustring const &ustr)
332     char const *cstr = ustr.c_str();
333     char const *data = ustr.data();
334     Glib::ustring::size_type const byteLen = ustr.bytes();
335     Glib::ustring::size_type const dataLen = ustr.length();
336     Glib::ustring::size_type const cstrLen = strlen(cstr);
338     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
339               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
340     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
341     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
343     try {
344         Glib::ustring tmp;
345         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
346             tmp = "    ";
347             if (i < dataLen) {
348                 Glib::ustring::value_type val = ustr.at(i);
349                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
350                 tmp += tmp2;
351                 g_free( tmp2 );
352             } else {
353                 tmp += "    ";
354             }
356             if (i < byteLen) {
357                 int val = (0x0ff & data[i]);
358                 gchar *tmp2 = g_strdup_printf("    %02x", val);
359                 tmp += tmp2;
360                 g_free( tmp2 );
361                 if ( val > 32 && val < 127 ) {
362                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
363                     tmp += tmp2;
364                     g_free( tmp2 );
365                 } else {
366                     tmp += "    . ";
367                 }
368             } else {
369                 tmp += "       ";
370             }
372             if ( i < cstrLen ) {
373                 int val = (0x0ff & cstr[i]);
374                 gchar* tmp2 = g_strdup_printf("    %02x", val);
375                 tmp += tmp2;
376                 g_free(tmp2);
377                 if ( val > 32 && val < 127 ) {
378                     tmp2 = g_strdup_printf("   '%c'", (gchar) val);
379                     tmp += tmp2;
380                     g_free( tmp2 );
381                 } else {
382                     tmp += "    . ";
383                 }
384             } else {
385                 tmp += "            ";
386             }
388             g_message( "%s", tmp.c_str() );
389         }
390     } catch (...) {
391         g_message("XXXXXXXXXXXXXXXXXX Exception" );
392     }
393     g_message("---------------");
396 /**
397  *  Display an file Open selector.  Open a document if OK is pressed.
398  *  Can select single or multiple files for opening.
399  */
400 void
401 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
403     //# Get the current directory for finding files
404     static Glib::ustring open_path;
405     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
407     if(open_path.empty())
408     {
409         Glib::ustring attr = prefs->getString("/dialogs/open/path");
410         if (!attr.empty()) open_path = attr;
411     }
413     //# Test if the open_path directory exists
414     if (!Inkscape::IO::file_test(open_path.c_str(),
415               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
416         open_path = "";
418 #ifdef WIN32
419     //# If no open path, default to our win32 documents folder
420     if (open_path.empty())
421     {
422         // The path to the My Documents folder is read from the
423         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
424         HKEY key = NULL;
425         if(RegOpenKeyExA(HKEY_CURRENT_USER,
426             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
427             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
428         {
429             WCHAR utf16path[_MAX_PATH];
430             DWORD value_type;
431             DWORD data_size = sizeof(utf16path);
432             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
433                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
434             {
435                 g_assert(value_type == REG_SZ);
436                 gchar *utf8path = g_utf16_to_utf8(
437                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
438                 if(utf8path)
439                 {
440                     open_path = Glib::ustring(utf8path);
441                     g_free(utf8path);
442                 }
443             }
444         }
445     }
446 #endif
448     //# If no open path, default to our home directory
449     if (open_path.empty())
450     {
451         open_path = g_get_home_dir();
452         open_path.append(G_DIR_SEPARATOR_S);
453     }
455     //# Create a dialog
456     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
457               Inkscape::UI::Dialog::FileOpenDialog::create(
458                  parentWindow, open_path,
459                  Inkscape::UI::Dialog::SVG_TYPES,
460                  _("Select file to open"));
462     //# Show the dialog
463     bool const success = openDialogInstance->show();
465     //# Save the folder the user selected for later
466     open_path = openDialogInstance->getCurrentDirectory();
468     if (!success)
469     {
470         delete openDialogInstance;
471         return;
472     }
474     //# User selected something.  Get name and type
475     Glib::ustring fileName = openDialogInstance->getFilename();
477     Inkscape::Extension::Extension *selection =
478             openDialogInstance->getSelectionType();
480     //# Code to check & open if multiple files.
481     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
483     //# We no longer need the file dialog object - delete it
484     delete openDialogInstance;
485     openDialogInstance = NULL;
487     //# Iterate through filenames if more than 1
488     if (flist.size() > 1)
489     {
490         for (unsigned int i = 0; i < flist.size(); i++)
491         {
492             fileName = flist[i];
494             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
495             if ( newFileName.size() > 0 )
496                 fileName = newFileName;
497             else
498                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
500 #ifdef INK_DUMP_FILENAME_CONV
501             g_message("Opening File %s\n", fileName.c_str());
502 #endif
503             sp_file_open(fileName, selection);
504         }
506         return;
507     }
510     if (!fileName.empty())
511     {
512         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         open_path = Glib::path_get_dirname (fileName);
520         open_path.append(G_DIR_SEPARATOR_S);
521         prefs->setString("/dialogs/open/path", open_path);
523         sp_file_open(fileName, selection);
524     }
526     return;
530 /*######################
531 ## V A C U U M
532 ######################*/
534 /**
535  * Remove unreferenced defs from the defs section of the document.
536  */
539 void
540 sp_file_vacuum()
542     SPDocument *doc = SP_ACTIVE_DOCUMENT;
544     unsigned int diff = vacuum_document (doc);
546     sp_document_done(doc, SP_VERB_FILE_VACUUM,
547                      _("Vacuum &lt;defs&gt;"));
549     SPDesktop *dt = SP_ACTIVE_DESKTOP;
550     if (diff > 0) {
551         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
552                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
553                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
554                          diff),
555                 diff);
556     } else {
557         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
558     }
563 /*######################
564 ## S A V E
565 ######################*/
567 /**
568  * This 'save' function called by the others below
569  *
570  * \param    official  whether to set :output_module and :modified in the
571  *                     document; is true for normal save, false for temporary saves
572  */
573 static bool
574 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
575           Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
576           Inkscape::Extension::FileSaveMethod save_method)
578     if (!doc || uri.size()<1) //Safety check
579         return false;
581     try {
582         Inkscape::Extension::save(key, doc, uri.c_str(),
583                                   false,
584                                   checkoverwrite, official,
585                                   save_method);
586     } catch (Inkscape::Extension::Output::no_extension_found &e) {
587         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
588         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
589         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
590         sp_ui_error_dialog(text);
591         g_free(text);
592         g_free(safeUri);
593         return FALSE;
594     } catch (Inkscape::Extension::Output::file_read_only &e) {
595         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
596         gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
597         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
598         sp_ui_error_dialog(text);
599         g_free(text);
600         g_free(safeUri);
601         return FALSE;
602     } catch (Inkscape::Extension::Output::save_failed &e) {
603         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
604         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
605         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
606         sp_ui_error_dialog(text);
607         g_free(text);
608         g_free(safeUri);
609         return FALSE;
610     } catch (Inkscape::Extension::Output::save_cancelled &e) {
611         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
612         return FALSE;
613     } catch (Inkscape::Extension::Output::no_overwrite &e) {
614         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
615     } catch (...) {
616         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
617         return FALSE;
618     }
620     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
621     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
622     return true;
625 /*
626  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
627  */
628 bool
629 file_save_remote(SPDocument */*doc*/,
630     #ifdef WITH_GNOME_VFS
631                  const Glib::ustring &uri,
632     #else
633                  const Glib::ustring &/*uri*/,
634     #endif
635                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
637 #ifdef WITH_GNOME_VFS
639 #define BUF_SIZE 8192
640     gnome_vfs_init();
642     GnomeVFSHandle    *from_handle = NULL;
643     GnomeVFSHandle    *to_handle = NULL;
644     GnomeVFSFileSize  bytes_read;
645     GnomeVFSFileSize  bytes_written;
646     GnomeVFSResult    result;
647     guint8 buffer[8192];
649     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
651     if ( uri_local == NULL ) {
652         g_warning( "Error converting filename to locale encoding.");
653     }
655     // Gets the temp file name.
656     Glib::ustring fileName = Glib::get_tmp_dir ();
657     fileName.append(G_DIR_SEPARATOR_S);
658     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
660     // Open the temp file to send.
661     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
663     if (result != GNOME_VFS_OK) {
664         g_warning("Could not find the temp saving.");
665         return false;
666     }
668     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
669     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
671     if (result != GNOME_VFS_OK) {
672         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
673         return false;
674     }
676     while (1) {
678         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
680         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
681             result = gnome_vfs_close (from_handle);
682             result = gnome_vfs_close (to_handle);
683             return true;
684         }
686         if (result != GNOME_VFS_OK) {
687             g_warning("%s", gnome_vfs_result_to_string(result));
688             return false;
689         }
690         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
691         if (result != GNOME_VFS_OK) {
692             g_warning("%s", gnome_vfs_result_to_string(result));
693             return false;
694         }
697         if (bytes_read != bytes_written){
698             return false;
699         }
701     }
702     return true;
703 #else
704     // in case we do not have GNOME_VFS
705     return false;
706 #endif
711 /**
712  *  Display a SaveAs dialog.  Save the document if OK pressed.
713  */
714 bool
715 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
717     Inkscape::Extension::Output *extension = 0;
718     bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
720     // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
721     //       filename_extension only uses ".svg"
722     Glib::ustring default_extension;
723     Glib::ustring filename_extension = ".svg";
725     default_extension= Inkscape::Extension::get_file_save_extension(save_method);
726     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
728     extension = dynamic_cast<Inkscape::Extension::Output *>
729         (Inkscape::Extension::db.get(default_extension.c_str()));
731     if (extension)
732         filename_extension = extension->get_extension();
734     Glib::ustring save_path;
735     Glib::ustring save_loc;
737     save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
739     if (!Inkscape::IO::file_test(save_path.c_str(),
740           (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
741         save_path = "";
743     if (save_path.size()<1)
744         save_path = g_get_home_dir();
746     save_loc = save_path;
747     save_loc.append(G_DIR_SEPARATOR_S);
749     char formatBuf[256];
750     int i = 1;
751     if (!doc->uri) {
752         // We are saving for the first time; create a unique default filename
753         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
754         save_loc.append(formatBuf);
756         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
757             save_loc = save_path;
758             save_loc.append(G_DIR_SEPARATOR_S);
759             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
760             save_loc.append(formatBuf);
761         }
762     } else {
763         snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
764         save_loc.append(formatBuf);
765     }
767     // convert save_loc from utf-8 to locale
768     // is this needed any more, now that everything is handled in
769     // Inkscape::IO?
770     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
772     if ( save_loc_local.size() > 0)
773         save_loc = save_loc_local;
775     //# Show the SaveAs dialog
776     char const * dialog_title;
777     if (is_copy) {
778         dialog_title = (char const *) _("Select file to save a copy to");
779     } else {
780         dialog_title = (char const *) _("Select file to save to");
781     }
782     gchar* doc_title = doc->root->title();
783     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
784         Inkscape::UI::Dialog::FileSaveDialog::create(
785             parentWindow,
786             save_loc,
787             Inkscape::UI::Dialog::SVG_TYPES,
788             dialog_title,
789             default_extension,
790             doc_title ? doc_title : "",
791             save_method
792             );
794     saveDialog->setSelectionType(extension);
796     bool success = saveDialog->show();
797     if (!success) {
798         delete saveDialog;
799         return success;
800     }
802     // set new title here (call RDF to ensure metadata and title element are updated)
803     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
804     // free up old string
805     if(doc_title) g_free(doc_title);
807     Glib::ustring fileName = saveDialog->getFilename();
808     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
810     delete saveDialog;
812     saveDialog = 0;
814     if (fileName.size() > 0) {
815         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
817         if ( newFileName.size()>0 )
818             fileName = newFileName;
819         else
820             g_warning( "Error converting save filename to UTF-8." );
822         // FIXME: does the argument !is_copy really convey the correct meaning here?
823         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
825         if (success && SP_DOCUMENT_URI(doc)) {
826             sp_file_add_recent(SP_DOCUMENT_URI(doc));
827         }
829         save_path = Glib::path_get_dirname(fileName);
830         Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
832         return success;
833     }
836     return false;
840 /**
841  * Save a document, displaying a SaveAs dialog if necessary.
842  */
843 bool
844 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
846     bool success = true;
848     if (doc->isModifiedSinceSave()) {
849         if ( doc->uri == NULL )
850         {
851             // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
852             // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
853             // benutzte "Save as ..."-Endung
854             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
855         } else {
856             Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
857             Glib::ustring fn = g_strdup(doc->uri);
858             // Try to determine the extension from the uri; this may not lead to a valid extension,
859             // but this case is caught in the file_save method below (or rather in Extension::save()
860             // further down the line).
861             Glib::ustring ext = "";
862             Glib::ustring::size_type pos = fn.rfind('.');
863             if (pos != Glib::ustring::npos) {
864                 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
865                 // but hopefully it's a reasonable workaround for now
866                 ext = fn.substr( pos );
867             }
868             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
869         }
870     } else {
871         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
872         success = TRUE;
873     }
875     return success;
879 /**
880  * Save a document.
881  */
882 bool
883 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
885     if (!SP_ACTIVE_DOCUMENT)
886         return false;
888     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
890     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
891     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
895 /**
896  *  Save a document, always displaying the SaveAs dialog.
897  */
898 bool
899 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
901     if (!SP_ACTIVE_DOCUMENT)
902         return false;
903     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
904     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
909 /**
910  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
911  */
912 bool
913 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
915     if (!SP_ACTIVE_DOCUMENT)
916         return false;
917     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
918     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
922 /*######################
923 ## I M P O R T
924 ######################*/
926 /**
927  *  Import a resource.  Called by sp_file_import()
928  */
929 void
930 file_import(SPDocument *in_doc, const Glib::ustring &uri,
931                Inkscape::Extension::Extension *key)
933     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
935     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
936     SPDocument *doc;
937     try {
938         doc = Inkscape::Extension::open(key, uri.c_str());
939     } catch (Inkscape::Extension::Input::no_extension_found &e) {
940         doc = NULL;
941     } catch (Inkscape::Extension::Input::open_failed &e) {
942         doc = NULL;
943     }
945     if (doc != NULL) {
946         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
947         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
949         prevent_id_clashes(doc, in_doc);
951         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
952         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
954         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
956         // Count the number of top-level items in the imported document.
957         guint items_count = 0;
958         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
959              child != NULL; child = SP_OBJECT_NEXT(child))
960         {
961             if (SP_IS_ITEM(child)) items_count++;
962         }
964         // Create a new group if necessary.
965         Inkscape::XML::Node *newgroup = NULL;
966         if ((style && style->firstChild()) || items_count > 1) {
967             newgroup = xml_in_doc->createElement("svg:g");
968             sp_repr_css_set(newgroup, style, "style");
969         }
971         // Determine the place to insert the new object.
972         // This will be the current layer, if possible.
973         // FIXME: If there's no desktop (command line run?) we need
974         //        a document:: method to return the current layer.
975         //        For now, we just use the root in this case.
976         SPObject *place_to_insert;
977         if (desktop) {
978             place_to_insert = desktop->currentLayer();
979         } else {
980             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
981         }
983         // Construct a new object representing the imported image,
984         // and insert it into the current document.
985         SPObject *new_obj = NULL;
986         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
987              child != NULL; child = SP_OBJECT_NEXT(child) )
988         {
989             if (SP_IS_ITEM(child)) {
990                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
992                 // convert layers to groups, and make sure they are unlocked
993                 // FIXME: add "preserve layers" mode where each layer from
994                 //        import is copied to the same-named layer in host
995                 newitem->setAttribute("inkscape:groupmode", NULL);
996                 newitem->setAttribute("sodipodi:insensitive", NULL);
998                 if (newgroup) newgroup->appendChild(newitem);
999                 else new_obj = place_to_insert->appendChildRepr(newitem);
1000             }
1002             // don't lose top-level defs or style elements
1003             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1004                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1005                 if (!strcmp(tag, "svg:defs")) {
1006                     for (SPObject *x = sp_object_first_child(child);
1007                          x != NULL; x = SP_OBJECT_NEXT(x))
1008                     {
1009                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1010                     }
1011                 }
1012                 else if (!strcmp(tag, "svg:style")) {
1013                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1014                 }
1015             }
1016         }
1017         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1019         // release some stuff
1020         if (newgroup) Inkscape::GC::release(newgroup);
1021         if (style) sp_repr_css_attr_unref(style);
1023         // select and move the imported item
1024         if (new_obj && SP_IS_ITEM(new_obj)) {
1025             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1026             selection->set(SP_ITEM(new_obj));
1028             // preserve parent and viewBox transformations
1029             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1030             sp_document_ensure_up_to_date(doc);
1031             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1032             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1034             // move to mouse pointer
1035             {
1036                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1037                 Geom::OptRect sel_bbox = selection->bounds();
1038                 if (sel_bbox) {
1039                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1040                     sp_selection_move_relative(selection, m, false);
1041                 }
1042             }
1043         }
1045         sp_document_unref(doc);
1046         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1047                          _("Import"));
1049     } else {
1050         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1051         sp_ui_error_dialog(text);
1052         g_free(text);
1053     }
1055     return;
1059 /**
1060  *  Display an Open dialog, import a resource if OK pressed.
1061  */
1062 void
1063 sp_file_import(Gtk::Window &parentWindow)
1065     static Glib::ustring import_path;
1067     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1068     if (!doc)
1069         return;
1071     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1073     if(import_path.empty())
1074     {
1075         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1076         if (!attr.empty()) import_path = attr;
1077     }
1079     //# Test if the import_path directory exists
1080     if (!Inkscape::IO::file_test(import_path.c_str(),
1081               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1082         import_path = "";
1084     //# If no open path, default to our home directory
1085     if (import_path.empty())
1086     {
1087         import_path = g_get_home_dir();
1088         import_path.append(G_DIR_SEPARATOR_S);
1089     }
1091     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1092     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1093              Inkscape::UI::Dialog::FileOpenDialog::create(
1094                  parentWindow,
1095                  import_path,
1096                  Inkscape::UI::Dialog::IMPORT_TYPES,
1097                  (char const *)_("Select file to import"));
1099     bool success = importDialogInstance->show();
1100     if (!success) {
1101         delete importDialogInstance;
1102         return;
1103     }
1105     //# Get file name and extension type
1106     Glib::ustring fileName = importDialogInstance->getFilename();
1107     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1109     delete importDialogInstance;
1110     importDialogInstance = NULL;
1112     if (fileName.size() > 0) {
1114         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1116         if ( newFileName.size() > 0)
1117             fileName = newFileName;
1118         else
1119             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1121         import_path = Glib::path_get_dirname (fileName);
1122         import_path.append(G_DIR_SEPARATOR_S);
1123         prefs->setString("/dialogs/import/path", import_path);
1125         file_import(doc, fileName, selection);
1126     }
1128     return;
1133 /*######################
1134 ## E X P O R T
1135 ######################*/
1138 #ifdef NEW_EXPORT_DIALOG
1140 /**
1141  *  Display an Export dialog, export as the selected type if OK pressed
1142  */
1143 bool
1144 sp_file_export_dialog(Gtk::Window &parentWindow)
1146     //# temp hack for 'doc' until we can switch to this dialog
1147     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1149     Glib::ustring export_path;
1150     Glib::ustring export_loc;
1152     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1153     Inkscape::Extension::Output *extension;
1155     //# Get the default extension name
1156     Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1157     if(default_extension.empty()) {
1158         // FIXME: Is this a good default? Should there be a macro for the string?
1159         default_extension = "org.inkscape.output.png.cairo";
1160     }
1161     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1163     if (doc->uri == NULL)
1164         {
1165         char formatBuf[256];
1167         Glib::ustring filename_extension = ".svg";
1168         extension = dynamic_cast<Inkscape::Extension::Output *>
1169               (Inkscape::Extension::db.get(default_extension.c_str()));
1170         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1171         if (extension)
1172             filename_extension = extension->get_extension();
1174         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1175         if (!attr3.empty())
1176             export_path = attr3;
1178         if (!Inkscape::IO::file_test(export_path.c_str(),
1179               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1180             export_path = "";
1182         if (export_path.size()<1)
1183             export_path = g_get_home_dir();
1185         export_loc = export_path;
1186         export_loc.append(G_DIR_SEPARATOR_S);
1187         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1188         export_loc.append(formatBuf);
1190         }
1191     else
1192         {
1193         export_path = Glib::path_get_dirname(doc->uri);
1194         }
1196     // convert save_loc from utf-8 to locale
1197     // is this needed any more, now that everything is handled in
1198     // Inkscape::IO?
1199     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1200     if ( export_path_local.size() > 0)
1201         export_path = export_path_local;
1203     //# Show the Export dialog
1204     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1205         Inkscape::UI::Dialog::FileExportDialog::create(
1206             parentWindow,
1207             export_path,
1208             Inkscape::UI::Dialog::EXPORT_TYPES,
1209             (char const *) _("Select file to export to"),
1210             default_extension
1211         );
1213     bool success = exportDialogInstance->show();
1214     if (!success) {
1215         delete exportDialogInstance;
1216         return success;
1217     }
1219     Glib::ustring fileName = exportDialogInstance->getFilename();
1221     Inkscape::Extension::Extension *selectionType =
1222         exportDialogInstance->getSelectionType();
1224     delete exportDialogInstance;
1225     exportDialogInstance = NULL;
1227     if (fileName.size() > 0) {
1228         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1230         if ( newFileName.size()>0 )
1231             fileName = newFileName;
1232         else
1233             g_warning( "Error converting save filename to UTF-8." );
1235         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1237         if (success) {
1238             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1239             recent->add_item(SP_DOCUMENT_URI(doc));
1240         }
1242         export_path = fileName;
1243         prefs->setString("/dialogs/save_export/path", export_path);
1245         return success;
1246     }
1249     return false;
1252 #else
1254 /**
1255  *
1256  */
1257 bool
1258 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1260     sp_export_dialog();
1261     return true;
1264 #endif
1266 /*######################
1267 ## E X P O R T  T O  O C A L
1268 ######################*/
1270 /**
1271  *  Display an Export dialog, export as the selected type if OK pressed
1272  */
1273 bool
1274 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1277    if (!SP_ACTIVE_DOCUMENT)
1278         return false;
1280     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1282     Glib::ustring export_path;
1283     Glib::ustring export_loc;
1284     Glib::ustring fileName;
1285     Inkscape::Extension::Extension *selectionType;
1287     bool success = false;
1289     static bool gotSuccess = false;
1291     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1292     (void)repr;
1294     if (!doc->uri && !doc->isModifiedSinceSave())
1295         return false;
1297     //  Get the default extension name
1298     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1299     char formatBuf[256];
1301     Glib::ustring filename_extension = ".svg";
1302     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1304     export_path = Glib::get_tmp_dir ();
1306     export_loc = export_path;
1307     export_loc.append(G_DIR_SEPARATOR_S);
1308     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1309     export_loc.append(formatBuf);
1311     // convert save_loc from utf-8 to locale
1312     // is this needed any more, now that everything is handled in
1313     // Inkscape::IO?
1314     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1315     if ( export_path_local.size() > 0)
1316         export_path = export_path_local;
1318     // Show the Export To OCAL dialog
1319     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1320         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1321                 parentWindow,
1322                 Inkscape::UI::Dialog::EXPORT_TYPES,
1323                 (char const *) _("Select file to export to")
1324                 );
1326     success = exportDialogInstance->show();
1327     if (!success) {
1328         delete exportDialogInstance;
1329         return success;
1330     }
1332     fileName = exportDialogInstance->getFilename();
1334     delete exportDialogInstance;
1335     exportDialogInstance = NULL;;
1337     fileName.append(filename_extension.c_str());
1338     if (fileName.size() > 0) {
1339         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1341         if ( newFileName.size()>0 )
1342             fileName = newFileName;
1343         else
1344             g_warning( "Error converting save filename to UTF-8." );
1345     }
1346     Glib::ustring filePath = export_path;
1347     filePath.append(G_DIR_SEPARATOR_S);
1348     filePath.append(Glib::path_get_basename(fileName));
1350     fileName = filePath;
1352     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1354     if (!success){
1355         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1356         sp_ui_error_dialog(text);
1358         return success;
1359     }
1361     // Start now the submition
1363     // Create the uri
1364     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1365     Glib::ustring uri = "dav://";
1366     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1367     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1368     if (username.empty() || password.empty())
1369     {
1370         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1371         if(!gotSuccess)
1372         {
1373             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1374                     parentWindow,
1375                     (char const *) _("Open Clip Art Login"));
1376             success = exportPasswordDialogInstance->show();
1377             if (!success) {
1378                 delete exportPasswordDialogInstance;
1379                 return success;
1380             }
1381         }
1382         username = exportPasswordDialogInstance->getUsername();
1383         password = exportPasswordDialogInstance->getPassword();
1385         delete exportPasswordDialogInstance;
1386         exportPasswordDialogInstance = NULL;
1387     }
1388     uri.append(username);
1389     uri.append(":");
1390     uri.append(password);
1391     uri.append("@");
1392     uri.append(prefs->getString("/options/ocalurl/str"));
1393     uri.append("/dav.php/");
1394     uri.append(Glib::path_get_basename(fileName));
1396     // Save as a remote file using the dav protocol.
1397     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1398     remove(fileName.c_str());
1399     if (!success)
1400     {
1401         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."));
1402         sp_ui_error_dialog(text);
1403     }
1404     else
1405         gotSuccess = true;
1407     return success;
1410 /**
1411  * Export the current document to OCAL
1412  */
1413 void
1414 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1417     // Try to execute the new code and return;
1418     if (!SP_ACTIVE_DOCUMENT)
1419         return;
1420     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1421     if (success)
1422         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1426 /*######################
1427 ## I M P O R T  F R O M  O C A L
1428 ######################*/
1430 /**
1431  * Display an ImportToOcal Dialog, and the selected document from OCAL
1432  */
1433 void
1434 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1436     static Glib::ustring import_path;
1438     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1439     if (!doc)
1440         return;
1442     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1444     if (!importDialogInstance) {
1445         importDialogInstance = new
1446              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1447                  parentWindow,
1448                  import_path,
1449                  Inkscape::UI::Dialog::IMPORT_TYPES,
1450                  (char const *)_("Import From Open Clip Art Library"));
1451     }
1453     bool success = importDialogInstance->show();
1454     if (!success) {
1455         delete importDialogInstance;
1456         return;
1457     }
1459     // Get file name and extension type
1460     Glib::ustring fileName = importDialogInstance->getFilename();
1461     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1463     delete importDialogInstance;
1464     importDialogInstance = NULL;
1466     if (fileName.size() > 0) {
1468         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1470         if ( newFileName.size() > 0)
1471             fileName = newFileName;
1472         else
1473             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1475         import_path = fileName;
1476         if (import_path.size()>0)
1477             import_path.append(G_DIR_SEPARATOR_S);
1479         file_import(doc, fileName, selection);
1480     }
1482     return;
1485 /*######################
1486 ## P R I N T
1487 ######################*/
1490 /**
1491  *  Print the current document, if any.
1492  */
1493 void
1494 sp_file_print(Gtk::Window& parentWindow)
1496     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1497     if (doc)
1498         sp_print_document(parentWindow, doc);
1501 /**
1502  * Display what the drawing would look like, if
1503  * printed.
1504  */
1505 void
1506 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1509     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1510     if (doc)
1511         sp_print_preview_document(doc);
1516 /*
1517   Local Variables:
1518   mode:c++
1519   c-file-style:"stroustrup"
1520   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1521   indent-tabs-mode:nil
1522   fill-column:99
1523   End:
1524 */
1525 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :