Code

Pot and Dutch translation update
[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 "desktop.h"
35 #include "desktop-handles.h"
36 #include "dialogs/export.h"
37 #include "dir-util.h"
38 #include "document-private.h"
39 #include "extension/db.h"
40 #include "extension/input.h"
41 #include "extension/output.h"
42 #include "extension/system.h"
43 #include "file.h"
44 #include "helper/png-write.h"
45 #include "id-clash.h"
46 #include "inkscape.h"
47 #include "inkscape.h"
48 #include "interface.h"
49 #include "io/sys.h"
50 #include "message.h"
51 #include "message-stack.h"
52 #include "path-prefix.h"
53 #include "preferences.h"
54 #include "print.h"
55 #include "rdf.h"
56 #include "selection-chemistry.h"
57 #include "selection.h"
58 #include "sp-namedview.h"
59 #include "style.h"
60 #include "ui/dialog/ocaldialogs.h"
61 #include "ui/view/view-widget.h"
62 #include "uri.h"
63 #include "xml/rebase-hrefs.h"
65 #ifdef WITH_GNOME_VFS
66 # include <libgnomevfs/gnome-vfs.h>
67 #endif
69 #ifdef WITH_DBUS
70 #include "extension/dbus/dbus-init.h"
71 #endif
73 //#ifdef WITH_INKBOARD
74 //#include "jabber_whiteboard/session-manager.h"
75 //#endif
77 #ifdef WIN32
78 #include <windows.h>
79 #endif
81 //#define INK_DUMP_FILENAME_CONV 1
82 #undef INK_DUMP_FILENAME_CONV
84 //#define INK_DUMP_FOPEN 1
85 #undef INK_DUMP_FOPEN
87 void dump_str(gchar const *str, gchar const *prefix);
88 void dump_ustr(Glib::ustring const &ustr);
90 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
91 static void sp_file_add_recent(gchar const *uri)
92 {
93     if(uri == NULL) {
94         g_warning("sp_file_add_recent: uri == NULL");
95         return;
96     }
97     GtkRecentManager *recent = gtk_recent_manager_get_default();
98     gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
99     if (fn) {
100         gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
101         if (uri_to_add) {
102             gtk_recent_manager_add_item(recent, uri_to_add);
103             g_free(uri_to_add);
104         }
105         g_free(fn);
106     }
110 /*######################
111 ## N E W
112 ######################*/
114 /**
115  * Create a blank document and add it to the desktop
116  */
117 SPDesktop*
118 sp_file_new(const Glib::ustring &templ)
120     char *templName = NULL;
121     if (templ.size()>0)
122         templName = (char *)templ.c_str();
123     SPDocument *doc = sp_document_new(templName, TRUE, true);
124     g_return_val_if_fail(doc != NULL, NULL);
126     SPDesktop *dt;
127     SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
128     g_return_val_if_fail(dtw != NULL, NULL);
129     sp_document_unref(doc);
131     sp_create_window(dtw, TRUE);
132     dt = static_cast<SPDesktop*>(dtw->view);
133     sp_namedview_window_from_document(dt);
134     sp_namedview_update_layers_from_document(dt);
136 #ifdef WITH_DBUS
137     Inkscape::Extension::Dbus::dbus_init_desktop_interface(dt);
138 #endif
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             // create a whole new desktop and window
244             SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
245             sp_create_window(dtw, TRUE);
246             desktop = static_cast<SPDesktop*>(dtw->view);
247         }
249         doc->virgin = FALSE;
250         // everyone who cares now has a reference, get rid of ours
251         sp_document_unref(doc);
252         // resize the window to match the document properties
253         sp_namedview_window_from_document(desktop);
254         sp_namedview_update_layers_from_document(desktop);
256         if (add_to_recent) {
257             sp_file_add_recent(SP_DOCUMENT_URI(doc));
258         }
260         return TRUE;
261     } else {
262         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
263         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
264         sp_ui_error_dialog(text);
265         g_free(text);
266         g_free(safeUri);
267         return FALSE;
268     }
271 /**
272  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
273  */
274 void
275 sp_file_revert_dialog()
277     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
278     g_assert(desktop != NULL);
280     SPDocument *doc = sp_desktop_document(desktop);
281     g_assert(doc != NULL);
283     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
284     g_assert(repr != NULL);
286     gchar const *uri = doc->uri;
287     if (!uri) {
288         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
289         return;
290     }
292     bool do_revert = true;
293     if (doc->isModifiedSinceSave()) {
294         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
296         bool response = desktop->warnDialog (text);
297         g_free(text);
299         if (!response) {
300             do_revert = false;
301         }
302     }
304     bool reverted;
305     if (do_revert) {
306         // Allow overwriting of current document.
307         doc->virgin = TRUE;
309         // remember current zoom and view
310         double zoom = desktop->current_zoom();
311         Geom::Point c = desktop->get_display_area().midpoint();
313         reverted = sp_file_open(uri,NULL);
314         if (reverted) {
315             // restore zoom and view
316             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
317         }
318     } else {
319         reverted = false;
320     }
322     if (reverted) {
323         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
324     } else {
325         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
326     }
329 void dump_str(gchar const *str, gchar const *prefix)
331     Glib::ustring tmp;
332     tmp = prefix;
333     tmp += " [";
334     size_t const total = strlen(str);
335     for (unsigned i = 0; i < total; i++) {
336         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
337         tmp += tmp2;
338         g_free(tmp2);
339     }
341     tmp += "]";
342     g_message("%s", tmp.c_str());
345 void dump_ustr(Glib::ustring const &ustr)
347     char const *cstr = ustr.c_str();
348     char const *data = ustr.data();
349     Glib::ustring::size_type const byteLen = ustr.bytes();
350     Glib::ustring::size_type const dataLen = ustr.length();
351     Glib::ustring::size_type const cstrLen = strlen(cstr);
353     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
354               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
355     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
356     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
358     try {
359         Glib::ustring tmp;
360         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
361             tmp = "    ";
362             if (i < dataLen) {
363                 Glib::ustring::value_type val = ustr.at(i);
364                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
365                 tmp += tmp2;
366                 g_free( tmp2 );
367             } else {
368                 tmp += "    ";
369             }
371             if (i < byteLen) {
372                 int val = (0x0ff & data[i]);
373                 gchar *tmp2 = g_strdup_printf("    %02x", val);
374                 tmp += tmp2;
375                 g_free( tmp2 );
376                 if ( val > 32 && val < 127 ) {
377                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
378                     tmp += tmp2;
379                     g_free( tmp2 );
380                 } else {
381                     tmp += "    . ";
382                 }
383             } else {
384                 tmp += "       ";
385             }
387             if ( i < cstrLen ) {
388                 int val = (0x0ff & cstr[i]);
389                 gchar* tmp2 = g_strdup_printf("    %02x", val);
390                 tmp += tmp2;
391                 g_free(tmp2);
392                 if ( val > 32 && val < 127 ) {
393                     tmp2 = g_strdup_printf("   '%c'", (gchar) val);
394                     tmp += tmp2;
395                     g_free( tmp2 );
396                 } else {
397                     tmp += "    . ";
398                 }
399             } else {
400                 tmp += "            ";
401             }
403             g_message( "%s", tmp.c_str() );
404         }
405     } catch (...) {
406         g_message("XXXXXXXXXXXXXXXXXX Exception" );
407     }
408     g_message("---------------");
411 /**
412  *  Display an file Open selector.  Open a document if OK is pressed.
413  *  Can select single or multiple files for opening.
414  */
415 void
416 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
418     //# Get the current directory for finding files
419     static Glib::ustring open_path;
420     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
422     if(open_path.empty())
423     {
424         Glib::ustring attr = prefs->getString("/dialogs/open/path");
425         if (!attr.empty()) open_path = attr;
426     }
428     //# Test if the open_path directory exists
429     if (!Inkscape::IO::file_test(open_path.c_str(),
430               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
431         open_path = "";
433 #ifdef WIN32
434     //# If no open path, default to our win32 documents folder
435     if (open_path.empty())
436     {
437         // The path to the My Documents folder is read from the
438         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
439         HKEY key = NULL;
440         if(RegOpenKeyExA(HKEY_CURRENT_USER,
441             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
442             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
443         {
444             WCHAR utf16path[_MAX_PATH];
445             DWORD value_type;
446             DWORD data_size = sizeof(utf16path);
447             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
448                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
449             {
450                 g_assert(value_type == REG_SZ);
451                 gchar *utf8path = g_utf16_to_utf8(
452                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
453                 if(utf8path)
454                 {
455                     open_path = Glib::ustring(utf8path);
456                     g_free(utf8path);
457                 }
458             }
459         }
460     }
461 #endif
463     //# If no open path, default to our home directory
464     if (open_path.empty())
465     {
466         open_path = g_get_home_dir();
467         open_path.append(G_DIR_SEPARATOR_S);
468     }
470     //# Create a dialog
471     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
472               Inkscape::UI::Dialog::FileOpenDialog::create(
473                  parentWindow, open_path,
474                  Inkscape::UI::Dialog::SVG_TYPES,
475                  _("Select file to open"));
477     //# Show the dialog
478     bool const success = openDialogInstance->show();
480     //# Save the folder the user selected for later
481     open_path = openDialogInstance->getCurrentDirectory();
483     if (!success)
484     {
485         delete openDialogInstance;
486         return;
487     }
489     //# User selected something.  Get name and type
490     Glib::ustring fileName = openDialogInstance->getFilename();
492     Inkscape::Extension::Extension *selection =
493             openDialogInstance->getSelectionType();
495     //# Code to check & open if multiple files.
496     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
498     //# We no longer need the file dialog object - delete it
499     delete openDialogInstance;
500     openDialogInstance = NULL;
502     //# Iterate through filenames if more than 1
503     if (flist.size() > 1)
504     {
505         for (unsigned int i = 0; i < flist.size(); i++)
506         {
507             fileName = flist[i];
509             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
510             if ( newFileName.size() > 0 )
511                 fileName = newFileName;
512             else
513                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
515 #ifdef INK_DUMP_FILENAME_CONV
516             g_message("Opening File %s\n", fileName.c_str());
517 #endif
518             sp_file_open(fileName, selection);
519         }
521         return;
522     }
525     if (!fileName.empty())
526     {
527         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
529         if ( newFileName.size() > 0)
530             fileName = newFileName;
531         else
532             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
534         open_path = Glib::path_get_dirname (fileName);
535         open_path.append(G_DIR_SEPARATOR_S);
536         prefs->setString("/dialogs/open/path", open_path);
538         sp_file_open(fileName, selection);
539     }
541     return;
545 /*######################
546 ## V A C U U M
547 ######################*/
549 /**
550  * Remove unreferenced defs from the defs section of the document.
551  */
554 void
555 sp_file_vacuum()
557     SPDocument *doc = SP_ACTIVE_DOCUMENT;
559     unsigned int diff = vacuum_document (doc);
561     sp_document_done(doc, SP_VERB_FILE_VACUUM,
562                      _("Vacuum &lt;defs&gt;"));
564     SPDesktop *dt = SP_ACTIVE_DESKTOP;
565     if (diff > 0) {
566         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
567                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
568                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
569                          diff),
570                 diff);
571     } else {
572         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
573     }
578 /*######################
579 ## S A V E
580 ######################*/
582 /**
583  * This 'save' function called by the others below
584  *
585  * \param    official  whether to set :output_module and :modified in the
586  *                     document; is true for normal save, false for temporary saves
587  */
588 static bool
589 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
590           Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
591           Inkscape::Extension::FileSaveMethod save_method)
593     if (!doc || uri.size()<1) //Safety check
594         return false;
596     try {
597         Inkscape::Extension::save(key, doc, uri.c_str(),
598                                   false,
599                                   checkoverwrite, official,
600                                   save_method);
601     } catch (Inkscape::Extension::Output::no_extension_found &e) {
602         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
603         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
604         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
605         sp_ui_error_dialog(text);
606         g_free(text);
607         g_free(safeUri);
608         return FALSE;
609     } catch (Inkscape::Extension::Output::file_read_only &e) {
610         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
611         gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
612         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
613         sp_ui_error_dialog(text);
614         g_free(text);
615         g_free(safeUri);
616         return FALSE;
617     } catch (Inkscape::Extension::Output::save_failed &e) {
618         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
619         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
620         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
621         sp_ui_error_dialog(text);
622         g_free(text);
623         g_free(safeUri);
624         return FALSE;
625     } catch (Inkscape::Extension::Output::save_cancelled &e) {
626         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
627         return FALSE;
628     } catch (Inkscape::Extension::Output::no_overwrite &e) {
629         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
630     } catch (...) {
631         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
632         return FALSE;
633     }
635     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
636     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
637     return true;
640 /*
641  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
642  */
643 bool
644 file_save_remote(SPDocument */*doc*/,
645     #ifdef WITH_GNOME_VFS
646                  const Glib::ustring &uri,
647     #else
648                  const Glib::ustring &/*uri*/,
649     #endif
650                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
652 #ifdef WITH_GNOME_VFS
654 #define BUF_SIZE 8192
655     gnome_vfs_init();
657     GnomeVFSHandle    *from_handle = NULL;
658     GnomeVFSHandle    *to_handle = NULL;
659     GnomeVFSFileSize  bytes_read;
660     GnomeVFSFileSize  bytes_written;
661     GnomeVFSResult    result;
662     guint8 buffer[8192];
664     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
666     if ( uri_local == NULL ) {
667         g_warning( "Error converting filename to locale encoding.");
668     }
670     // Gets the temp file name.
671     Glib::ustring fileName = Glib::get_tmp_dir ();
672     fileName.append(G_DIR_SEPARATOR_S);
673     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
675     // Open the temp file to send.
676     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
678     if (result != GNOME_VFS_OK) {
679         g_warning("Could not find the temp saving.");
680         return false;
681     }
683     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
684     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
686     if (result != GNOME_VFS_OK) {
687         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
688         return false;
689     }
691     while (1) {
693         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
695         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
696             result = gnome_vfs_close (from_handle);
697             result = gnome_vfs_close (to_handle);
698             return true;
699         }
701         if (result != GNOME_VFS_OK) {
702             g_warning("%s", gnome_vfs_result_to_string(result));
703             return false;
704         }
705         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
706         if (result != GNOME_VFS_OK) {
707             g_warning("%s", gnome_vfs_result_to_string(result));
708             return false;
709         }
712         if (bytes_read != bytes_written){
713             return false;
714         }
716     }
717     return true;
718 #else
719     // in case we do not have GNOME_VFS
720     return false;
721 #endif
726 /**
727  *  Display a SaveAs dialog.  Save the document if OK pressed.
728  */
729 bool
730 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
732     Inkscape::Extension::Output *extension = 0;
733     bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
735     // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
736     //       filename_extension only uses ".svg"
737     Glib::ustring default_extension;
738     Glib::ustring filename_extension = ".svg";
740     default_extension= Inkscape::Extension::get_file_save_extension(save_method);
741     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
743     extension = dynamic_cast<Inkscape::Extension::Output *>
744         (Inkscape::Extension::db.get(default_extension.c_str()));
746     if (extension)
747         filename_extension = extension->get_extension();
749     Glib::ustring save_path;
750     Glib::ustring save_loc;
752     save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
754     if (!Inkscape::IO::file_test(save_path.c_str(),
755           (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
756         save_path = "";
758     if (save_path.size()<1)
759         save_path = g_get_home_dir();
761     save_loc = save_path;
762     save_loc.append(G_DIR_SEPARATOR_S);
764     char formatBuf[256];
765     int i = 1;
766     if (!doc->uri) {
767         // We are saving for the first time; create a unique default filename
768         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
769         save_loc.append(formatBuf);
771         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
772             save_loc = save_path;
773             save_loc.append(G_DIR_SEPARATOR_S);
774             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
775             save_loc.append(formatBuf);
776         }
777     } else {
778         snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
779         save_loc.append(formatBuf);
780     }
782     // convert save_loc from utf-8 to locale
783     // is this needed any more, now that everything is handled in
784     // Inkscape::IO?
785     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
787     if ( save_loc_local.size() > 0)
788         save_loc = save_loc_local;
790     //# Show the SaveAs dialog
791     char const * dialog_title;
792     if (is_copy) {
793         dialog_title = (char const *) _("Select file to save a copy to");
794     } else {
795         dialog_title = (char const *) _("Select file to save to");
796     }
797     gchar* doc_title = doc->root->title();
798     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
799         Inkscape::UI::Dialog::FileSaveDialog::create(
800             parentWindow,
801             save_loc,
802             Inkscape::UI::Dialog::SVG_TYPES,
803             dialog_title,
804             default_extension,
805             doc_title ? doc_title : "",
806             save_method
807             );
809     saveDialog->setSelectionType(extension);
811     bool success = saveDialog->show();
812     if (!success) {
813         delete saveDialog;
814         return success;
815     }
817     // set new title here (call RDF to ensure metadata and title element are updated)
818     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
819     // free up old string
820     if(doc_title) g_free(doc_title);
822     Glib::ustring fileName = saveDialog->getFilename();
823     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
825     delete saveDialog;
827     saveDialog = 0;
829     if (fileName.size() > 0) {
830         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
832         if ( newFileName.size()>0 )
833             fileName = newFileName;
834         else
835             g_warning( "Error converting save filename to UTF-8." );
837         // FIXME: does the argument !is_copy really convey the correct meaning here?
838         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
840         if (success && SP_DOCUMENT_URI(doc)) {
841             sp_file_add_recent(SP_DOCUMENT_URI(doc));
842         }
844         save_path = Glib::path_get_dirname(fileName);
845         Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
847         return success;
848     }
851     return false;
855 /**
856  * Save a document, displaying a SaveAs dialog if necessary.
857  */
858 bool
859 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
861     bool success = true;
863     if (doc->isModifiedSinceSave()) {
864         if ( doc->uri == NULL )
865         {
866             // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
867             // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
868             // benutzte "Save as ..."-Endung
869             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
870         } else {
871             Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
872             Glib::ustring fn = g_strdup(doc->uri);
873             // Try to determine the extension from the uri; this may not lead to a valid extension,
874             // but this case is caught in the file_save method below (or rather in Extension::save()
875             // further down the line).
876             Glib::ustring ext = "";
877             Glib::ustring::size_type pos = fn.rfind('.');
878             if (pos != Glib::ustring::npos) {
879                 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
880                 // but hopefully it's a reasonable workaround for now
881                 ext = fn.substr( pos );
882             }
883             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
884             if (success == false) {
885                 // give the user the chance to change filename or extension
886                 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
887             }
888         }
889     } else {
890         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
891         success = TRUE;
892     }
894     return success;
898 /**
899  * Save a document.
900  */
901 bool
902 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
904     if (!SP_ACTIVE_DOCUMENT)
905         return false;
907     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
909     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
910     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
914 /**
915  *  Save a document, always displaying the SaveAs dialog.
916  */
917 bool
918 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
920     if (!SP_ACTIVE_DOCUMENT)
921         return false;
922     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
923     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
928 /**
929  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
930  */
931 bool
932 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
934     if (!SP_ACTIVE_DOCUMENT)
935         return false;
936     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
937     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
941 /*######################
942 ## I M P O R T
943 ######################*/
945 /**
946  *  Import a resource.  Called by sp_file_import()
947  */
948 void
949 file_import(SPDocument *in_doc, const Glib::ustring &uri,
950                Inkscape::Extension::Extension *key)
952     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
954     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
955     SPDocument *doc;
956     try {
957         doc = Inkscape::Extension::open(key, uri.c_str());
958     } catch (Inkscape::Extension::Input::no_extension_found &e) {
959         doc = NULL;
960     } catch (Inkscape::Extension::Input::open_failed &e) {
961         doc = NULL;
962     }
964     if (doc != NULL) {
965         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
966         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
968         prevent_id_clashes(doc, in_doc);
970         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
971         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
973         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
975         // Count the number of top-level items in the imported document.
976         guint items_count = 0;
977         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
978              child != NULL; child = SP_OBJECT_NEXT(child))
979         {
980             if (SP_IS_ITEM(child)) items_count++;
981         }
983         // Create a new group if necessary.
984         Inkscape::XML::Node *newgroup = NULL;
985         if ((style && style->firstChild()) || items_count > 1) {
986             newgroup = xml_in_doc->createElement("svg:g");
987             sp_repr_css_set(newgroup, style, "style");
988         }
990         // Determine the place to insert the new object.
991         // This will be the current layer, if possible.
992         // FIXME: If there's no desktop (command line run?) we need
993         //        a document:: method to return the current layer.
994         //        For now, we just use the root in this case.
995         SPObject *place_to_insert;
996         if (desktop) {
997             place_to_insert = desktop->currentLayer();
998         } else {
999             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
1000         }
1002         // Construct a new object representing the imported image,
1003         // and insert it into the current document.
1004         SPObject *new_obj = NULL;
1005         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
1006              child != NULL; child = SP_OBJECT_NEXT(child) )
1007         {
1008             if (SP_IS_ITEM(child)) {
1009                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
1011                 // convert layers to groups, and make sure they are unlocked
1012                 // FIXME: add "preserve layers" mode where each layer from
1013                 //        import is copied to the same-named layer in host
1014                 newitem->setAttribute("inkscape:groupmode", NULL);
1015                 newitem->setAttribute("sodipodi:insensitive", NULL);
1017                 if (newgroup) newgroup->appendChild(newitem);
1018                 else new_obj = place_to_insert->appendChildRepr(newitem);
1019             }
1021             // don't lose top-level defs or style elements
1022             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1023                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1024                 if (!strcmp(tag, "svg:defs")) {
1025                     for (SPObject *x = sp_object_first_child(child);
1026                          x != NULL; x = SP_OBJECT_NEXT(x))
1027                     {
1028                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1029                     }
1030                 }
1031                 else if (!strcmp(tag, "svg:style")) {
1032                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1033                 }
1034             }
1035         }
1036         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1038         // release some stuff
1039         if (newgroup) Inkscape::GC::release(newgroup);
1040         if (style) sp_repr_css_attr_unref(style);
1042         // select and move the imported item
1043         if (new_obj && SP_IS_ITEM(new_obj)) {
1044             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1045             selection->set(SP_ITEM(new_obj));
1047             // preserve parent and viewBox transformations
1048             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1049             sp_document_ensure_up_to_date(doc);
1050             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1051             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1053             // move to mouse pointer
1054             {
1055                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1056                 Geom::OptRect sel_bbox = selection->bounds();
1057                 if (sel_bbox) {
1058                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1059                     sp_selection_move_relative(selection, m, false);
1060                 }
1061             }
1062         }
1064         sp_document_unref(doc);
1065         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1066                          _("Import"));
1068     } else {
1069         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1070         sp_ui_error_dialog(text);
1071         g_free(text);
1072     }
1074     return;
1078 /**
1079  *  Display an Open dialog, import a resource if OK pressed.
1080  */
1081 void
1082 sp_file_import(Gtk::Window &parentWindow)
1084     static Glib::ustring import_path;
1086     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1087     if (!doc)
1088         return;
1090     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1092     if(import_path.empty())
1093     {
1094         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1095         if (!attr.empty()) import_path = attr;
1096     }
1098     //# Test if the import_path directory exists
1099     if (!Inkscape::IO::file_test(import_path.c_str(),
1100               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1101         import_path = "";
1103     //# If no open path, default to our home directory
1104     if (import_path.empty())
1105     {
1106         import_path = g_get_home_dir();
1107         import_path.append(G_DIR_SEPARATOR_S);
1108     }
1110     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1111     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1112              Inkscape::UI::Dialog::FileOpenDialog::create(
1113                  parentWindow,
1114                  import_path,
1115                  Inkscape::UI::Dialog::IMPORT_TYPES,
1116                  (char const *)_("Select file to import"));
1118     bool success = importDialogInstance->show();
1119     if (!success) {
1120         delete importDialogInstance;
1121         return;
1122     }
1124     //# Get file name and extension type
1125     Glib::ustring fileName = importDialogInstance->getFilename();
1126     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1128     delete importDialogInstance;
1129     importDialogInstance = NULL;
1131     if (fileName.size() > 0) {
1133         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1135         if ( newFileName.size() > 0)
1136             fileName = newFileName;
1137         else
1138             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1140         import_path = Glib::path_get_dirname (fileName);
1141         import_path.append(G_DIR_SEPARATOR_S);
1142         prefs->setString("/dialogs/import/path", import_path);
1144         file_import(doc, fileName, selection);
1145     }
1147     return;
1152 /*######################
1153 ## E X P O R T
1154 ######################*/
1157 #ifdef NEW_EXPORT_DIALOG
1159 /**
1160  *  Display an Export dialog, export as the selected type if OK pressed
1161  */
1162 bool
1163 sp_file_export_dialog(Gtk::Window &parentWindow)
1165     //# temp hack for 'doc' until we can switch to this dialog
1166     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1168     Glib::ustring export_path;
1169     Glib::ustring export_loc;
1171     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1172     Inkscape::Extension::Output *extension;
1174     //# Get the default extension name
1175     Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1176     if(default_extension.empty()) {
1177         // FIXME: Is this a good default? Should there be a macro for the string?
1178         default_extension = "org.inkscape.output.png.cairo";
1179     }
1180     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1182     if (doc->uri == NULL)
1183         {
1184         char formatBuf[256];
1186         Glib::ustring filename_extension = ".svg";
1187         extension = dynamic_cast<Inkscape::Extension::Output *>
1188               (Inkscape::Extension::db.get(default_extension.c_str()));
1189         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1190         if (extension)
1191             filename_extension = extension->get_extension();
1193         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1194         if (!attr3.empty())
1195             export_path = attr3;
1197         if (!Inkscape::IO::file_test(export_path.c_str(),
1198               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1199             export_path = "";
1201         if (export_path.size()<1)
1202             export_path = g_get_home_dir();
1204         export_loc = export_path;
1205         export_loc.append(G_DIR_SEPARATOR_S);
1206         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1207         export_loc.append(formatBuf);
1209         }
1210     else
1211         {
1212         export_path = Glib::path_get_dirname(doc->uri);
1213         }
1215     // convert save_loc from utf-8 to locale
1216     // is this needed any more, now that everything is handled in
1217     // Inkscape::IO?
1218     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1219     if ( export_path_local.size() > 0)
1220         export_path = export_path_local;
1222     //# Show the Export dialog
1223     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1224         Inkscape::UI::Dialog::FileExportDialog::create(
1225             parentWindow,
1226             export_path,
1227             Inkscape::UI::Dialog::EXPORT_TYPES,
1228             (char const *) _("Select file to export to"),
1229             default_extension
1230         );
1232     bool success = exportDialogInstance->show();
1233     if (!success) {
1234         delete exportDialogInstance;
1235         return success;
1236     }
1238     Glib::ustring fileName = exportDialogInstance->getFilename();
1240     Inkscape::Extension::Extension *selectionType =
1241         exportDialogInstance->getSelectionType();
1243     delete exportDialogInstance;
1244     exportDialogInstance = NULL;
1246     if (fileName.size() > 0) {
1247         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1249         if ( newFileName.size()>0 )
1250             fileName = newFileName;
1251         else
1252             g_warning( "Error converting save filename to UTF-8." );
1254         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1256         if (success) {
1257             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1258             recent->add_item(SP_DOCUMENT_URI(doc));
1259         }
1261         export_path = fileName;
1262         prefs->setString("/dialogs/save_export/path", export_path);
1264         return success;
1265     }
1268     return false;
1271 #else
1273 /**
1274  *
1275  */
1276 bool
1277 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1279     sp_export_dialog();
1280     return true;
1283 #endif
1285 /*######################
1286 ## E X P O R T  T O  O C A L
1287 ######################*/
1289 /**
1290  *  Display an Export dialog, export as the selected type if OK pressed
1291  */
1292 /*
1293 bool
1294 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1297    if (!SP_ACTIVE_DOCUMENT)
1298         return false;
1300     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1302     Glib::ustring export_path;
1303     Glib::ustring export_loc;
1304     Glib::ustring fileName;
1305     Inkscape::Extension::Extension *selectionType;
1307     bool success = false;
1309     static bool gotSuccess = false;
1311     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1312     (void)repr;
1314     if (!doc->uri && !doc->isModifiedSinceSave())
1315         return false;
1317     //  Get the default extension name
1318     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1319     char formatBuf[256];
1321     Glib::ustring filename_extension = ".svg";
1322     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1324     export_path = Glib::get_tmp_dir ();
1326     export_loc = export_path;
1327     export_loc.append(G_DIR_SEPARATOR_S);
1328     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1329     export_loc.append(formatBuf);
1331     // convert save_loc from utf-8 to locale
1332     // is this needed any more, now that everything is handled in
1333     // Inkscape::IO?
1334     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1335     if ( export_path_local.size() > 0)
1336         export_path = export_path_local;
1338     // Show the Export To OCAL dialog
1339     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1340         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1341                 parentWindow,
1342                 Inkscape::UI::Dialog::EXPORT_TYPES,
1343                 (char const *) _("Select file to export to")
1344                 );
1346     success = exportDialogInstance->show();
1347     if (!success) {
1348         delete exportDialogInstance;
1349         return success;
1350     }
1352     fileName = exportDialogInstance->getFilename();
1354     delete exportDialogInstance;
1355     exportDialogInstance = NULL;;
1357     fileName.append(filename_extension.c_str());
1358     if (fileName.size() > 0) {
1359         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1361         if ( newFileName.size()>0 )
1362             fileName = newFileName;
1363         else
1364             g_warning( "Error converting save filename to UTF-8." );
1365     }
1366     Glib::ustring filePath = export_path;
1367     filePath.append(G_DIR_SEPARATOR_S);
1368     filePath.append(Glib::path_get_basename(fileName));
1370     fileName = filePath;
1372     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1374     if (!success){
1375         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1376         sp_ui_error_dialog(text);
1378         return success;
1379     }
1381     // Start now the submition
1383     // Create the uri
1384     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1385     Glib::ustring uri = "dav://";
1386     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1387     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1388     if (username.empty() || password.empty())
1389     {
1390         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1391         if(!gotSuccess)
1392         {
1393             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1394                     parentWindow,
1395                     (char const *) _("Open Clip Art Login"));
1396             success = exportPasswordDialogInstance->show();
1397             if (!success) {
1398                 delete exportPasswordDialogInstance;
1399                 return success;
1400             }
1401         }
1402         username = exportPasswordDialogInstance->getUsername();
1403         password = exportPasswordDialogInstance->getPassword();
1405         delete exportPasswordDialogInstance;
1406         exportPasswordDialogInstance = NULL;
1407     }
1408     uri.append(username);
1409     uri.append(":");
1410     uri.append(password);
1411     uri.append("@");
1412     uri.append(prefs->getString("/options/ocalurl/str"));
1413     uri.append("/dav.php/");
1414     uri.append(Glib::path_get_basename(fileName));
1416     // Save as a remote file using the dav protocol.
1417     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1418     remove(fileName.c_str());
1419     if (!success)
1420     {
1421         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."));
1422         sp_ui_error_dialog(text);
1423     }
1424     else
1425         gotSuccess = true;
1427     return success;
1429 */
1430 /**
1431  * Export the current document to OCAL
1432  */
1433 /*
1434 void
1435 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1438     // Try to execute the new code and return;
1439     if (!SP_ACTIVE_DOCUMENT)
1440         return;
1441     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1442     if (success)
1443         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1445 */
1447 /*######################
1448 ## I M P O R T  F R O M  O C A L
1449 ######################*/
1451 /**
1452  * Display an ImportToOcal Dialog, and the selected document from OCAL
1453  */
1454 void
1455 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1457     static Glib::ustring import_path;
1459     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1460     if (!doc)
1461         return;
1463     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1465     if (!importDialogInstance) {
1466         importDialogInstance = new
1467              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1468                  parentWindow,
1469                  import_path,
1470                  Inkscape::UI::Dialog::IMPORT_TYPES,
1471                  (char const *)_("Import From Open Clip Art Library"));
1472     }
1474     bool success = importDialogInstance->show();
1475     if (!success) {
1476         delete importDialogInstance;
1477         return;
1478     }
1480     // Get file name and extension type
1481     Glib::ustring fileName = importDialogInstance->getFilename();
1482     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1484     delete importDialogInstance;
1485     importDialogInstance = NULL;
1487     if (fileName.size() > 0) {
1489         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1491         if ( newFileName.size() > 0)
1492             fileName = newFileName;
1493         else
1494             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1496         import_path = fileName;
1497         if (import_path.size()>0)
1498             import_path.append(G_DIR_SEPARATOR_S);
1500         file_import(doc, fileName, selection);
1501     }
1503     return;
1506 /*######################
1507 ## P R I N T
1508 ######################*/
1511 /**
1512  *  Print the current document, if any.
1513  */
1514 void
1515 sp_file_print(Gtk::Window& parentWindow)
1517     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1518     if (doc)
1519         sp_print_document(parentWindow, doc);
1522 /**
1523  * Display what the drawing would look like, if
1524  * printed.
1525  */
1526 void
1527 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1530     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1531     if (doc)
1532         sp_print_preview_document(doc);
1537 /*
1538   Local Variables:
1539   mode:c++
1540   c-file-style:"stroustrup"
1541   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1542   indent-tabs-mode:nil
1543   fill-column:99
1544   End:
1545 */
1546 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :