Code

A simple layout document as to what, why and how is cppification.
[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 = SPDocument::createNewDoc(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         doc->doUnref();
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* sp_file_new_default()
143     std::list<gchar *> sources;
144     sources.push_back( profile_path("templates") ); // first try user's local dir
145     sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
146     std::list<gchar const*> baseNames;
147     gchar const* localized = _("default.svg");
148     if (strcmp("default.svg", localized) != 0) {
149         baseNames.push_back(localized);
150     }
151     baseNames.push_back("default.svg");
152     gchar *foundTemplate = 0;
154     for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
155         for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
156             gchar *dirname = *it;
157             if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
159                 // TRANSLATORS: default.svg is localizable - this is the name of the default document
160                 //  template. This way you can localize the default pagesize, translate the name of
161                 //  the default layer, etc. If you wish to localize this file, please create a
162                 //  localized share/templates/default.xx.svg file, where xx is your language code.
163                 char *tmp = g_build_filename(dirname, *nameIt, NULL);
164                 if (Inkscape::IO::file_test(tmp, G_FILE_TEST_IS_REGULAR)) {
165                     foundTemplate = tmp;
166                 } else {
167                     g_free(tmp);
168                 }
169             }
170         }
171     }
173     for (std::list<gchar *>::iterator it = sources.begin(); it != sources.end(); ++it) {
174         g_free(*it);
175     }
177     SPDesktop* desk = sp_file_new(foundTemplate ? foundTemplate : "");
178     if (foundTemplate) {
179         g_free(foundTemplate);
180         foundTemplate = 0;
181     }
182     return desk;
186 /*######################
187 ## D E L E T E
188 ######################*/
190 /**
191  *  Perform document closures preceding an exit()
192  */
193 void
194 sp_file_exit()
196     sp_ui_close_all();
197     // no need to call inkscape_exit here; last document being closed will take care of that
201 /*######################
202 ## O P E N
203 ######################*/
205 /**
206  *  Open a file, add the document to the desktop
207  *
208  *  \param replace_empty if true, and the current desktop is empty, this document
209  *  will replace the empty one.
210  */
211 bool
212 sp_file_open(const Glib::ustring &uri,
213              Inkscape::Extension::Extension *key,
214              bool add_to_recent, bool replace_empty)
216     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
217     if (desktop)
218         desktop->setWaitingCursor();
220     SPDocument *doc = NULL;
221     try {
222         doc = Inkscape::Extension::open(key, uri.c_str());
223     } catch (Inkscape::Extension::Input::no_extension_found &e) {
224         doc = NULL;
225     } catch (Inkscape::Extension::Input::open_failed &e) {
226         doc = NULL;
227     }
229     if (desktop)
230         desktop->clearWaitingCursor();
232     if (doc) {
233         SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
235         if (existing && existing->virgin && replace_empty) {
236             // If the current desktop is empty, open the document there
237             doc->ensure_up_to_date ();
238             desktop->change_document(doc);
239             doc->resized_signal_emit (doc->getWidth(), doc->getHeight());
240         } else {
241             if (!Inkscape::NSApplication::Application::getNewGui()) {
242                 // create a whole new desktop and window
243                 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
244                 sp_create_window(dtw, TRUE);
245                 desktop = static_cast<SPDesktop*>(dtw->view);
246             } else {
247                 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
248             }
249         }
251         doc->virgin = FALSE;
252         // everyone who cares now has a reference, get rid of ours
253         doc->doUnref();
254         // resize the window to match the document properties
255         sp_namedview_window_from_document(desktop);
256         sp_namedview_update_layers_from_document(desktop);
258         if (add_to_recent) {
259             sp_file_add_recent(SP_DOCUMENT_URI(doc));
260         }
262         return TRUE;
263     } else {
264         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
265         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
266         sp_ui_error_dialog(text);
267         g_free(text);
268         g_free(safeUri);
269         return FALSE;
270     }
273 /**
274  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
275  */
276 void
277 sp_file_revert_dialog()
279     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
280     g_assert(desktop != NULL);
282     SPDocument *doc = sp_desktop_document(desktop);
283     g_assert(doc != NULL);
285     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
286     g_assert(repr != NULL);
288     gchar const *uri = doc->uri;
289     if (!uri) {
290         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
291         return;
292     }
294     bool do_revert = true;
295     if (doc->isModifiedSinceSave()) {
296         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
298         bool response = desktop->warnDialog (text);
299         g_free(text);
301         if (!response) {
302             do_revert = false;
303         }
304     }
306     bool reverted;
307     if (do_revert) {
308         // Allow overwriting of current document.
309         doc->virgin = TRUE;
311         // remember current zoom and view
312         double zoom = desktop->current_zoom();
313         Geom::Point c = desktop->get_display_area().midpoint();
315         reverted = sp_file_open(uri,NULL);
316         if (reverted) {
317             // restore zoom and view
318             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
319         }
320     } else {
321         reverted = false;
322     }
324     if (reverted) {
325         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
326     } else {
327         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
328     }
331 void dump_str(gchar const *str, gchar const *prefix)
333     Glib::ustring tmp;
334     tmp = prefix;
335     tmp += " [";
336     size_t const total = strlen(str);
337     for (unsigned i = 0; i < total; i++) {
338         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
339         tmp += tmp2;
340         g_free(tmp2);
341     }
343     tmp += "]";
344     g_message("%s", tmp.c_str());
347 void dump_ustr(Glib::ustring const &ustr)
349     char const *cstr = ustr.c_str();
350     char const *data = ustr.data();
351     Glib::ustring::size_type const byteLen = ustr.bytes();
352     Glib::ustring::size_type const dataLen = ustr.length();
353     Glib::ustring::size_type const cstrLen = strlen(cstr);
355     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
356               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
357     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
358     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
360     try {
361         Glib::ustring tmp;
362         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
363             tmp = "    ";
364             if (i < dataLen) {
365                 Glib::ustring::value_type val = ustr.at(i);
366                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
367                 tmp += tmp2;
368                 g_free( tmp2 );
369             } else {
370                 tmp += "    ";
371             }
373             if (i < byteLen) {
374                 int val = (0x0ff & data[i]);
375                 gchar *tmp2 = g_strdup_printf("    %02x", val);
376                 tmp += tmp2;
377                 g_free( tmp2 );
378                 if ( val > 32 && val < 127 ) {
379                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
380                     tmp += tmp2;
381                     g_free( tmp2 );
382                 } else {
383                     tmp += "    . ";
384                 }
385             } else {
386                 tmp += "       ";
387             }
389             if ( i < cstrLen ) {
390                 int val = (0x0ff & cstr[i]);
391                 gchar* tmp2 = g_strdup_printf("    %02x", val);
392                 tmp += tmp2;
393                 g_free(tmp2);
394                 if ( val > 32 && val < 127 ) {
395                     tmp2 = g_strdup_printf("   '%c'", (gchar) val);
396                     tmp += tmp2;
397                     g_free( tmp2 );
398                 } else {
399                     tmp += "    . ";
400                 }
401             } else {
402                 tmp += "            ";
403             }
405             g_message( "%s", tmp.c_str() );
406         }
407     } catch (...) {
408         g_message("XXXXXXXXXXXXXXXXXX Exception" );
409     }
410     g_message("---------------");
413 /**
414  *  Display an file Open selector.  Open a document if OK is pressed.
415  *  Can select single or multiple files for opening.
416  */
417 void
418 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
420     //# Get the current directory for finding files
421     static Glib::ustring open_path;
422     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
424     if(open_path.empty())
425     {
426         Glib::ustring attr = prefs->getString("/dialogs/open/path");
427         if (!attr.empty()) open_path = attr;
428     }
430     //# Test if the open_path directory exists
431     if (!Inkscape::IO::file_test(open_path.c_str(),
432               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
433         open_path = "";
435 #ifdef WIN32
436     //# If no open path, default to our win32 documents folder
437     if (open_path.empty())
438     {
439         // The path to the My Documents folder is read from the
440         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
441         HKEY key = NULL;
442         if(RegOpenKeyExA(HKEY_CURRENT_USER,
443             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
444             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
445         {
446             WCHAR utf16path[_MAX_PATH];
447             DWORD value_type;
448             DWORD data_size = sizeof(utf16path);
449             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
450                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
451             {
452                 g_assert(value_type == REG_SZ);
453                 gchar *utf8path = g_utf16_to_utf8(
454                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
455                 if(utf8path)
456                 {
457                     open_path = Glib::ustring(utf8path);
458                     g_free(utf8path);
459                 }
460             }
461         }
462     }
463 #endif
465     //# If no open path, default to our home directory
466     if (open_path.empty())
467     {
468         open_path = g_get_home_dir();
469         open_path.append(G_DIR_SEPARATOR_S);
470     }
472     //# Create a dialog
473     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
474               Inkscape::UI::Dialog::FileOpenDialog::create(
475                  parentWindow, open_path,
476                  Inkscape::UI::Dialog::SVG_TYPES,
477                  _("Select file to open"));
479     //# Show the dialog
480     bool const success = openDialogInstance->show();
482     //# Save the folder the user selected for later
483     open_path = openDialogInstance->getCurrentDirectory();
485     if (!success)
486     {
487         delete openDialogInstance;
488         return;
489     }
491     //# User selected something.  Get name and type
492     Glib::ustring fileName = openDialogInstance->getFilename();
494     Inkscape::Extension::Extension *selection =
495             openDialogInstance->getSelectionType();
497     //# Code to check & open if multiple files.
498     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
500     //# We no longer need the file dialog object - delete it
501     delete openDialogInstance;
502     openDialogInstance = NULL;
504     //# Iterate through filenames if more than 1
505     if (flist.size() > 1)
506     {
507         for (unsigned int i = 0; i < flist.size(); i++)
508         {
509             fileName = flist[i];
511             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
512             if ( newFileName.size() > 0 )
513                 fileName = newFileName;
514             else
515                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
517 #ifdef INK_DUMP_FILENAME_CONV
518             g_message("Opening File %s\n", fileName.c_str());
519 #endif
520             sp_file_open(fileName, selection);
521         }
523         return;
524     }
527     if (!fileName.empty())
528     {
529         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
531         if ( newFileName.size() > 0)
532             fileName = newFileName;
533         else
534             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
536         open_path = Glib::path_get_dirname (fileName);
537         open_path.append(G_DIR_SEPARATOR_S);
538         prefs->setString("/dialogs/open/path", open_path);
540         sp_file_open(fileName, selection);
541     }
543     return;
547 /*######################
548 ## V A C U U M
549 ######################*/
551 /**
552  * Remove unreferenced defs from the defs section of the document.
553  */
556 void
557 sp_file_vacuum()
559     SPDocument *doc = SP_ACTIVE_DOCUMENT;
561     unsigned int diff = doc->vacuum_document ();
563     SPDocumentUndo::done(doc, SP_VERB_FILE_VACUUM,
564                      _("Vacuum &lt;defs&gt;"));
566     SPDesktop *dt = SP_ACTIVE_DESKTOP;
567     if (diff > 0) {
568         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
569                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
570                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
571                          diff),
572                 diff);
573     } else {
574         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
575     }
580 /*######################
581 ## S A V E
582 ######################*/
584 /**
585  * This 'save' function called by the others below
586  *
587  * \param    official  whether to set :output_module and :modified in the
588  *                     document; is true for normal save, false for temporary saves
589  */
590 static bool
591 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
592           Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
593           Inkscape::Extension::FileSaveMethod save_method)
595     if (!doc || uri.size()<1) //Safety check
596         return false;
598     try {
599         Inkscape::Extension::save(key, doc, uri.c_str(),
600                                   false,
601                                   checkoverwrite, official,
602                                   save_method);
603     } catch (Inkscape::Extension::Output::no_extension_found &e) {
604         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
605         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
606         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
607         sp_ui_error_dialog(text);
608         g_free(text);
609         g_free(safeUri);
610         return FALSE;
611     } catch (Inkscape::Extension::Output::file_read_only &e) {
612         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
613         gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
614         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
615         sp_ui_error_dialog(text);
616         g_free(text);
617         g_free(safeUri);
618         return FALSE;
619     } catch (Inkscape::Extension::Output::save_failed &e) {
620         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
621         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
622         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
623         sp_ui_error_dialog(text);
624         g_free(text);
625         g_free(safeUri);
626         return FALSE;
627     } catch (Inkscape::Extension::Output::save_cancelled &e) {
628         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
629         return FALSE;
630     } catch (Inkscape::Extension::Output::no_overwrite &e) {
631         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
632     } catch (...) {
633         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
634         return FALSE;
635     }
637     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
638     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
639     return true;
642 /*
643  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
644  */
645 bool
646 file_save_remote(SPDocument */*doc*/,
647     #ifdef WITH_GNOME_VFS
648                  const Glib::ustring &uri,
649     #else
650                  const Glib::ustring &/*uri*/,
651     #endif
652                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
654 #ifdef WITH_GNOME_VFS
656 #define BUF_SIZE 8192
657     gnome_vfs_init();
659     GnomeVFSHandle    *from_handle = NULL;
660     GnomeVFSHandle    *to_handle = NULL;
661     GnomeVFSFileSize  bytes_read;
662     GnomeVFSFileSize  bytes_written;
663     GnomeVFSResult    result;
664     guint8 buffer[8192];
666     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
668     if ( uri_local == NULL ) {
669         g_warning( "Error converting filename to locale encoding.");
670     }
672     // Gets the temp file name.
673     Glib::ustring fileName = Glib::get_tmp_dir ();
674     fileName.append(G_DIR_SEPARATOR_S);
675     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
677     // Open the temp file to send.
678     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
680     if (result != GNOME_VFS_OK) {
681         g_warning("Could not find the temp saving.");
682         return false;
683     }
685     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
686     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
688     if (result != GNOME_VFS_OK) {
689         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
690         return false;
691     }
693     while (1) {
695         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
697         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
698             result = gnome_vfs_close (from_handle);
699             result = gnome_vfs_close (to_handle);
700             return true;
701         }
703         if (result != GNOME_VFS_OK) {
704             g_warning("%s", gnome_vfs_result_to_string(result));
705             return false;
706         }
707         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
708         if (result != GNOME_VFS_OK) {
709             g_warning("%s", gnome_vfs_result_to_string(result));
710             return false;
711         }
714         if (bytes_read != bytes_written){
715             return false;
716         }
718     }
719     return true;
720 #else
721     // in case we do not have GNOME_VFS
722     return false;
723 #endif
728 /**
729  *  Display a SaveAs dialog.  Save the document if OK pressed.
730  */
731 bool
732 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
734     Inkscape::Extension::Output *extension = 0;
735     bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
737     // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
738     //       filename_extension only uses ".svg"
739     Glib::ustring default_extension;
740     Glib::ustring filename_extension = ".svg";
742     default_extension= Inkscape::Extension::get_file_save_extension(save_method);
743     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
745     extension = dynamic_cast<Inkscape::Extension::Output *>
746         (Inkscape::Extension::db.get(default_extension.c_str()));
748     if (extension)
749         filename_extension = extension->get_extension();
751     Glib::ustring save_path;
752     Glib::ustring save_loc;
754     save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
756     if (!Inkscape::IO::file_test(save_path.c_str(),
757           (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
758         save_path = "";
760     if (save_path.size()<1)
761         save_path = g_get_home_dir();
763     save_loc = save_path;
764     save_loc.append(G_DIR_SEPARATOR_S);
766     char formatBuf[256];
767     int i = 1;
768     if (!doc->uri) {
769         // We are saving for the first time; create a unique default filename
770         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
771         save_loc.append(formatBuf);
773         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
774             save_loc = save_path;
775             save_loc.append(G_DIR_SEPARATOR_S);
776             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
777             save_loc.append(formatBuf);
778         }
779     } else {
780         snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
781         save_loc.append(formatBuf);
782     }
784     // convert save_loc from utf-8 to locale
785     // is this needed any more, now that everything is handled in
786     // Inkscape::IO?
787     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
789     if ( save_loc_local.size() > 0)
790         save_loc = save_loc_local;
792     //# Show the SaveAs dialog
793     char const * dialog_title;
794     if (is_copy) {
795         dialog_title = (char const *) _("Select file to save a copy to");
796     } else {
797         dialog_title = (char const *) _("Select file to save to");
798     }
799     gchar* doc_title = doc->root->title();
800     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
801         Inkscape::UI::Dialog::FileSaveDialog::create(
802             parentWindow,
803             save_loc,
804             Inkscape::UI::Dialog::SVG_TYPES,
805             dialog_title,
806             default_extension,
807             doc_title ? doc_title : "",
808             save_method
809             );
811     saveDialog->setSelectionType(extension);
813     bool success = saveDialog->show();
814     if (!success) {
815         delete saveDialog;
816         return success;
817     }
819     // set new title here (call RDF to ensure metadata and title element are updated)
820     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
821     // free up old string
822     if(doc_title) g_free(doc_title);
824     Glib::ustring fileName = saveDialog->getFilename();
825     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
827     delete saveDialog;
829     saveDialog = 0;
831     if (fileName.size() > 0) {
832         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
834         if ( newFileName.size()>0 )
835             fileName = newFileName;
836         else
837             g_warning( "Error converting save filename to UTF-8." );
839         // FIXME: does the argument !is_copy really convey the correct meaning here?
840         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
842         if (success && SP_DOCUMENT_URI(doc)) {
843             sp_file_add_recent(SP_DOCUMENT_URI(doc));
844         }
846         save_path = Glib::path_get_dirname(fileName);
847         Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
849         return success;
850     }
853     return false;
857 /**
858  * Save a document, displaying a SaveAs dialog if necessary.
859  */
860 bool
861 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
863     bool success = true;
865     if (doc->isModifiedSinceSave()) {
866         if ( doc->uri == NULL )
867         {
868             // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
869             // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
870             // benutzte "Save as ..."-Endung
871             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
872         } else {
873             Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
874             Glib::ustring fn = g_strdup(doc->uri);
875             // Try to determine the extension from the uri; this may not lead to a valid extension,
876             // but this case is caught in the file_save method below (or rather in Extension::save()
877             // further down the line).
878             Glib::ustring ext = "";
879             Glib::ustring::size_type pos = fn.rfind('.');
880             if (pos != Glib::ustring::npos) {
881                 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
882                 // but hopefully it's a reasonable workaround for now
883                 ext = fn.substr( pos );
884             }
885             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
886             if (success == false) {
887                 // give the user the chance to change filename or extension
888                 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
889             }
890         }
891     } else {
892         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
893         success = TRUE;
894     }
896     return success;
900 /**
901  * Save a document.
902  */
903 bool
904 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
906     if (!SP_ACTIVE_DOCUMENT)
907         return false;
909     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
911     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
912     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
916 /**
917  *  Save a document, always displaying the SaveAs dialog.
918  */
919 bool
920 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
922     if (!SP_ACTIVE_DOCUMENT)
923         return false;
924     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
925     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
930 /**
931  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
932  */
933 bool
934 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
936     if (!SP_ACTIVE_DOCUMENT)
937         return false;
938     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
939     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
943 /*######################
944 ## I M P O R T
945 ######################*/
947 /**
948  *  Import a resource.  Called by sp_file_import()
949  */
950 void
951 file_import(SPDocument *in_doc, const Glib::ustring &uri,
952                Inkscape::Extension::Extension *key)
954     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
956     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
957     SPDocument *doc;
958     try {
959         doc = Inkscape::Extension::open(key, uri.c_str());
960     } catch (Inkscape::Extension::Input::no_extension_found &e) {
961         doc = NULL;
962     } catch (Inkscape::Extension::Input::open_failed &e) {
963         doc = NULL;
964     }
966     if (doc != NULL) {
967         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
968         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
970         prevent_id_clashes(doc, in_doc);
972         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
973         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
975         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
977         // Count the number of top-level items in the imported document.
978         guint items_count = 0;
979         for (SPObject *child = SP_DOCUMENT_ROOT(doc)->first_child();
980              child != NULL; child = SP_OBJECT_NEXT(child))
981         {
982             if (SP_IS_ITEM(child)) items_count++;
983         }
985         // Create a new group if necessary.
986         Inkscape::XML::Node *newgroup = NULL;
987         if ((style && style->firstChild()) || items_count > 1) {
988             newgroup = xml_in_doc->createElement("svg:g");
989             sp_repr_css_set(newgroup, style, "style");
990         }
992         // Determine the place to insert the new object.
993         // This will be the current layer, if possible.
994         // FIXME: If there's no desktop (command line run?) we need
995         //        a document:: method to return the current layer.
996         //        For now, we just use the root in this case.
997         SPObject *place_to_insert;
998         if (desktop) {
999             place_to_insert = desktop->currentLayer();
1000         } else {
1001             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
1002         }
1004         // Construct a new object representing the imported image,
1005         // and insert it into the current document.
1006         SPObject *new_obj = NULL;
1007         for (SPObject *child = SP_DOCUMENT_ROOT(doc)->first_child();
1008              child != NULL; child = SP_OBJECT_NEXT(child) )
1009         {
1010             if (SP_IS_ITEM(child)) {
1011                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
1013                 // convert layers to groups, and make sure they are unlocked
1014                 // FIXME: add "preserve layers" mode where each layer from
1015                 //        import is copied to the same-named layer in host
1016                 newitem->setAttribute("inkscape:groupmode", NULL);
1017                 newitem->setAttribute("sodipodi:insensitive", NULL);
1019                 if (newgroup) newgroup->appendChild(newitem);
1020                 else new_obj = place_to_insert->appendChildRepr(newitem);
1021             }
1023             // don't lose top-level defs or style elements
1024             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1025                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1026                 if (!strcmp(tag, "svg:defs")) {
1027                     for (SPObject *x = child->first_child();
1028                          x != NULL; x = SP_OBJECT_NEXT(x))
1029                     {
1030                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1031                     }
1032                 }
1033                 else if (!strcmp(tag, "svg:style")) {
1034                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1035                 }
1036             }
1037         }
1038         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1040         // release some stuff
1041         if (newgroup) Inkscape::GC::release(newgroup);
1042         if (style) sp_repr_css_attr_unref(style);
1044         // select and move the imported item
1045         if (new_obj && SP_IS_ITEM(new_obj)) {
1046             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1047             selection->set(SP_ITEM(new_obj));
1049             // preserve parent and viewBox transformations
1050             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1051             doc->ensure_up_to_date();
1052             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * SP_ITEM(place_to_insert)->i2doc_affine().inverse();
1053             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1055             // move to mouse pointer
1056             {
1057                 sp_desktop_document(desktop)->ensure_up_to_date();
1058                 Geom::OptRect sel_bbox = selection->bounds();
1059                 if (sel_bbox) {
1060                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1061                     sp_selection_move_relative(selection, m, false);
1062                 }
1063             }
1064         }
1066         doc->doUnref();
1067         SPDocumentUndo::done(in_doc, SP_VERB_FILE_IMPORT,
1068                          _("Import"));
1070     } else {
1071         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1072         sp_ui_error_dialog(text);
1073         g_free(text);
1074     }
1076     return;
1080 /**
1081  *  Display an Open dialog, import a resource if OK pressed.
1082  */
1083 void
1084 sp_file_import(Gtk::Window &parentWindow)
1086     static Glib::ustring import_path;
1088     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1089     if (!doc)
1090         return;
1092     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1094     if(import_path.empty())
1095     {
1096         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1097         if (!attr.empty()) import_path = attr;
1098     }
1100     //# Test if the import_path directory exists
1101     if (!Inkscape::IO::file_test(import_path.c_str(),
1102               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1103         import_path = "";
1105     //# If no open path, default to our home directory
1106     if (import_path.empty())
1107     {
1108         import_path = g_get_home_dir();
1109         import_path.append(G_DIR_SEPARATOR_S);
1110     }
1112     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1113     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1114              Inkscape::UI::Dialog::FileOpenDialog::create(
1115                  parentWindow,
1116                  import_path,
1117                  Inkscape::UI::Dialog::IMPORT_TYPES,
1118                  (char const *)_("Select file to import"));
1120     bool success = importDialogInstance->show();
1121     if (!success) {
1122         delete importDialogInstance;
1123         return;
1124     }
1126     //# Get file name and extension type
1127     Glib::ustring fileName = importDialogInstance->getFilename();
1128     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1130     delete importDialogInstance;
1131     importDialogInstance = NULL;
1133     if (fileName.size() > 0) {
1135         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1137         if ( newFileName.size() > 0)
1138             fileName = newFileName;
1139         else
1140             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1142         import_path = Glib::path_get_dirname (fileName);
1143         import_path.append(G_DIR_SEPARATOR_S);
1144         prefs->setString("/dialogs/import/path", import_path);
1146         file_import(doc, fileName, selection);
1147     }
1149     return;
1154 /*######################
1155 ## E X P O R T
1156 ######################*/
1159 #ifdef NEW_EXPORT_DIALOG
1161 /**
1162  *  Display an Export dialog, export as the selected type if OK pressed
1163  */
1164 bool
1165 sp_file_export_dialog(Gtk::Window &parentWindow)
1167     //# temp hack for 'doc' until we can switch to this dialog
1168     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1170     Glib::ustring export_path;
1171     Glib::ustring export_loc;
1173     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1174     Inkscape::Extension::Output *extension;
1176     //# Get the default extension name
1177     Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1178     if(default_extension.empty()) {
1179         // FIXME: Is this a good default? Should there be a macro for the string?
1180         default_extension = "org.inkscape.output.png.cairo";
1181     }
1182     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1184     if (doc->uri == NULL)
1185         {
1186         char formatBuf[256];
1188         Glib::ustring filename_extension = ".svg";
1189         extension = dynamic_cast<Inkscape::Extension::Output *>
1190               (Inkscape::Extension::db.get(default_extension.c_str()));
1191         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1192         if (extension)
1193             filename_extension = extension->get_extension();
1195         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1196         if (!attr3.empty())
1197             export_path = attr3;
1199         if (!Inkscape::IO::file_test(export_path.c_str(),
1200               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1201             export_path = "";
1203         if (export_path.size()<1)
1204             export_path = g_get_home_dir();
1206         export_loc = export_path;
1207         export_loc.append(G_DIR_SEPARATOR_S);
1208         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1209         export_loc.append(formatBuf);
1211         }
1212     else
1213         {
1214         export_path = Glib::path_get_dirname(doc->uri);
1215         }
1217     // convert save_loc from utf-8 to locale
1218     // is this needed any more, now that everything is handled in
1219     // Inkscape::IO?
1220     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1221     if ( export_path_local.size() > 0)
1222         export_path = export_path_local;
1224     //# Show the Export dialog
1225     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1226         Inkscape::UI::Dialog::FileExportDialog::create(
1227             parentWindow,
1228             export_path,
1229             Inkscape::UI::Dialog::EXPORT_TYPES,
1230             (char const *) _("Select file to export to"),
1231             default_extension
1232         );
1234     bool success = exportDialogInstance->show();
1235     if (!success) {
1236         delete exportDialogInstance;
1237         return success;
1238     }
1240     Glib::ustring fileName = exportDialogInstance->getFilename();
1242     Inkscape::Extension::Extension *selectionType =
1243         exportDialogInstance->getSelectionType();
1245     delete exportDialogInstance;
1246     exportDialogInstance = NULL;
1248     if (fileName.size() > 0) {
1249         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1251         if ( newFileName.size()>0 )
1252             fileName = newFileName;
1253         else
1254             g_warning( "Error converting save filename to UTF-8." );
1256         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1258         if (success) {
1259             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1260             recent->add_item(SP_DOCUMENT_URI(doc));
1261         }
1263         export_path = fileName;
1264         prefs->setString("/dialogs/save_export/path", export_path);
1266         return success;
1267     }
1270     return false;
1273 #else
1275 /**
1276  *
1277  */
1278 bool
1279 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1281     sp_export_dialog();
1282     return true;
1285 #endif
1287 /*######################
1288 ## E X P O R T  T O  O C A L
1289 ######################*/
1291 /**
1292  *  Display an Export dialog, export as the selected type if OK pressed
1293  */
1294 /*
1295 bool
1296 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1299    if (!SP_ACTIVE_DOCUMENT)
1300         return false;
1302     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1304     Glib::ustring export_path;
1305     Glib::ustring export_loc;
1306     Glib::ustring fileName;
1307     Inkscape::Extension::Extension *selectionType;
1309     bool success = false;
1311     static bool gotSuccess = false;
1313     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1314     (void)repr;
1316     if (!doc->uri && !doc->isModifiedSinceSave())
1317         return false;
1319     //  Get the default extension name
1320     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1321     char formatBuf[256];
1323     Glib::ustring filename_extension = ".svg";
1324     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1326     export_path = Glib::get_tmp_dir ();
1328     export_loc = export_path;
1329     export_loc.append(G_DIR_SEPARATOR_S);
1330     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1331     export_loc.append(formatBuf);
1333     // convert save_loc from utf-8 to locale
1334     // is this needed any more, now that everything is handled in
1335     // Inkscape::IO?
1336     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1337     if ( export_path_local.size() > 0)
1338         export_path = export_path_local;
1340     // Show the Export To OCAL dialog
1341     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1342         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1343                 parentWindow,
1344                 Inkscape::UI::Dialog::EXPORT_TYPES,
1345                 (char const *) _("Select file to export to")
1346                 );
1348     success = exportDialogInstance->show();
1349     if (!success) {
1350         delete exportDialogInstance;
1351         return success;
1352     }
1354     fileName = exportDialogInstance->getFilename();
1356     delete exportDialogInstance;
1357     exportDialogInstance = NULL;;
1359     fileName.append(filename_extension.c_str());
1360     if (fileName.size() > 0) {
1361         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1363         if ( newFileName.size()>0 )
1364             fileName = newFileName;
1365         else
1366             g_warning( "Error converting save filename to UTF-8." );
1367     }
1368     Glib::ustring filePath = export_path;
1369     filePath.append(G_DIR_SEPARATOR_S);
1370     filePath.append(Glib::path_get_basename(fileName));
1372     fileName = filePath;
1374     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1376     if (!success){
1377         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1378         sp_ui_error_dialog(text);
1380         return success;
1381     }
1383     // Start now the submition
1385     // Create the uri
1386     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1387     Glib::ustring uri = "dav://";
1388     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1389     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1390     if (username.empty() || password.empty())
1391     {
1392         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1393         if(!gotSuccess)
1394         {
1395             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1396                     parentWindow,
1397                     (char const *) _("Open Clip Art Login"));
1398             success = exportPasswordDialogInstance->show();
1399             if (!success) {
1400                 delete exportPasswordDialogInstance;
1401                 return success;
1402             }
1403         }
1404         username = exportPasswordDialogInstance->getUsername();
1405         password = exportPasswordDialogInstance->getPassword();
1407         delete exportPasswordDialogInstance;
1408         exportPasswordDialogInstance = NULL;
1409     }
1410     uri.append(username);
1411     uri.append(":");
1412     uri.append(password);
1413     uri.append("@");
1414     uri.append(prefs->getString("/options/ocalurl/str"));
1415     uri.append("/dav.php/");
1416     uri.append(Glib::path_get_basename(fileName));
1418     // Save as a remote file using the dav protocol.
1419     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1420     remove(fileName.c_str());
1421     if (!success)
1422     {
1423         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."));
1424         sp_ui_error_dialog(text);
1425     }
1426     else
1427         gotSuccess = true;
1429     return success;
1431 */
1432 /**
1433  * Export the current document to OCAL
1434  */
1435 /*
1436 void
1437 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1440     // Try to execute the new code and return;
1441     if (!SP_ACTIVE_DOCUMENT)
1442         return;
1443     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1444     if (success)
1445         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1447 */
1449 /*######################
1450 ## I M P O R T  F R O M  O C A L
1451 ######################*/
1453 /**
1454  * Display an ImportToOcal Dialog, and the selected document from OCAL
1455  */
1456 void
1457 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1459     static Glib::ustring import_path;
1461     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1462     if (!doc)
1463         return;
1465     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1467     if (!importDialogInstance) {
1468         importDialogInstance = new
1469              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1470                  parentWindow,
1471                  import_path,
1472                  Inkscape::UI::Dialog::IMPORT_TYPES,
1473                  (char const *)_("Import From Open Clip Art Library"));
1474     }
1476     bool success = importDialogInstance->show();
1477     if (!success) {
1478         delete importDialogInstance;
1479         return;
1480     }
1482     // Get file name and extension type
1483     Glib::ustring fileName = importDialogInstance->getFilename();
1484     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1486     delete importDialogInstance;
1487     importDialogInstance = NULL;
1489     if (fileName.size() > 0) {
1491         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1493         if ( newFileName.size() > 0)
1494             fileName = newFileName;
1495         else
1496             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1498         import_path = fileName;
1499         if (import_path.size()>0)
1500             import_path.append(G_DIR_SEPARATOR_S);
1502         file_import(doc, fileName, selection);
1503     }
1505     return;
1508 /*######################
1509 ## P R I N T
1510 ######################*/
1513 /**
1514  *  Print the current document, if any.
1515  */
1516 void
1517 sp_file_print(Gtk::Window& parentWindow)
1519     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1520     if (doc)
1521         sp_print_document(parentWindow, doc);
1524 /**
1525  * Display what the drawing would look like, if
1526  * printed.
1527  */
1528 void
1529 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1532     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1533     if (doc)
1534         sp_print_preview_document(doc);
1539 /*
1540   Local Variables:
1541   mode:c++
1542   c-file-style:"stroustrup"
1543   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1544   indent-tabs-mode:nil
1545   fill-column:99
1546   End:
1547 */
1548 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :