Code

Store last used paths separately for the 'Save as ...' and 'Save a copy ...' dialogs...
[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/filedialog.h"
63 #include "ui/dialog/ocaldialogs.h"
64 #include "ui/view/view-widget.h"
65 #include "uri.h"
66 #include "xml/rebase-hrefs.h"
68 #ifdef WITH_GNOME_VFS
69 # include <libgnomevfs/gnome-vfs.h>
70 #endif
72 #ifdef WITH_INKBOARD
73 #include "jabber_whiteboard/session-manager.h"
74 #endif
76 #ifdef WIN32
77 #include <windows.h>
78 #endif
80 //#define INK_DUMP_FILENAME_CONV 1
81 #undef INK_DUMP_FILENAME_CONV
83 //#define INK_DUMP_FOPEN 1
84 #undef INK_DUMP_FOPEN
86 void dump_str(gchar const *str, gchar const *prefix);
87 void dump_ustr(Glib::ustring const &ustr);
89 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
90 static void sp_file_add_recent(gchar const *uri)
91 {
92     if(uri == NULL) {
93         g_warning("sp_file_add_recent: uri == NULL");
94         return;
95     }
96     GtkRecentManager *recent = gtk_recent_manager_get_default();
97     gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
98     if (fn) {
99         gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
100         if (uri_to_add) {
101             gtk_recent_manager_add_item(recent, uri_to_add);
102             g_free(uri_to_add);
103         }
104         g_free(fn);
105     }
109 /*######################
110 ## N E W
111 ######################*/
113 /**
114  * Create a blank document and add it to the desktop
115  */
116 SPDesktop*
117 sp_file_new(const Glib::ustring &templ)
119     char *templName = NULL;
120     if (templ.size()>0)
121         templName = (char *)templ.c_str();
122     SPDocument *doc = sp_document_new(templName, TRUE, true);
123     g_return_val_if_fail(doc != NULL, NULL);
125     SPDesktop *dt;
126     if (Inkscape::NSApplication::Application::getNewGui())
127     {
128         dt = Inkscape::NSApplication::Editor::createDesktop (doc);
129     } else {
130         SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
131         g_return_val_if_fail(dtw != NULL, NULL);
132         sp_document_unref(doc);
134         sp_create_window(dtw, TRUE);
135         dt = static_cast<SPDesktop*>(dtw->view);
136         sp_namedview_window_from_document(dt);
137         sp_namedview_update_layers_from_document(dt);
138     }
139     return dt;
142 SPDesktop*
143 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
149     while (!sources.empty()) {
150         gchar *dirname = sources.front();
151         if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
153             // TRANSLATORS: default.svg is localizable - this is the name of the default document
154             //  template. This way you can localize the default pagesize, translate the name of
155             //  the default layer, etc. If you wish to localize this file, please create a
156             //  localized share/templates/default.xx.svg file, where xx is your language code.
157             char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
158             if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
159                 return sp_file_new(default_template);
160             }
161         }
162         g_free(dirname);
163         sources.pop_front();
164     }
166     return sp_file_new("");
170 /*######################
171 ## D E L E T E
172 ######################*/
174 /**
175  *  Perform document closures preceding an exit()
176  */
177 void
178 sp_file_exit()
180     sp_ui_close_all();
181     // no need to call inkscape_exit here; last document being closed will take care of that
185 /*######################
186 ## O P E N
187 ######################*/
189 /**
190  *  Open a file, add the document to the desktop
191  *
192  *  \param replace_empty if true, and the current desktop is empty, this document
193  *  will replace the empty one.
194  */
195 bool
196 sp_file_open(const Glib::ustring &uri,
197              Inkscape::Extension::Extension *key,
198              bool add_to_recent, bool replace_empty)
200     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
201     if (desktop)
202         desktop->setWaitingCursor();
204     SPDocument *doc = NULL;
205     try {
206         doc = Inkscape::Extension::open(key, uri.c_str());
207     } catch (Inkscape::Extension::Input::no_extension_found &e) {
208         doc = NULL;
209     } catch (Inkscape::Extension::Input::open_failed &e) {
210         doc = NULL;
211     }
213     if (desktop)
214         desktop->clearWaitingCursor();
216     if (doc) {
217         SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
219         if (existing && existing->virgin && replace_empty) {
220             // If the current desktop is empty, open the document there
221             sp_document_ensure_up_to_date (doc);
222             desktop->change_document(doc);
223             sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
224         } else {
225             if (!Inkscape::NSApplication::Application::getNewGui()) {
226                 // create a whole new desktop and window
227                 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
228                 sp_create_window(dtw, TRUE);
229                 desktop = static_cast<SPDesktop*>(dtw->view);
230             } else {
231                 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
232             }
233         }
235         doc->virgin = FALSE;
236         // everyone who cares now has a reference, get rid of ours
237         sp_document_unref(doc);
238         // resize the window to match the document properties
239         sp_namedview_window_from_document(desktop);
240         sp_namedview_update_layers_from_document(desktop);
242         if (add_to_recent) {
243             sp_file_add_recent(SP_DOCUMENT_URI(doc));
244         }
246         return TRUE;
247     } else {
248         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
249         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
250         sp_ui_error_dialog(text);
251         g_free(text);
252         g_free(safeUri);
253         return FALSE;
254     }
257 /**
258  *  Handle prompting user for "do you want to revert"?  Revert on "OK"
259  */
260 void
261 sp_file_revert_dialog()
263     SPDesktop  *desktop = SP_ACTIVE_DESKTOP;
264     g_assert(desktop != NULL);
266     SPDocument *doc = sp_desktop_document(desktop);
267     g_assert(doc != NULL);
269     Inkscape::XML::Node     *repr = sp_document_repr_root(doc);
270     g_assert(repr != NULL);
272     gchar const *uri = doc->uri;
273     if (!uri) {
274         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet.  Cannot revert."));
275         return;
276     }
278     bool do_revert = true;
279     if (doc->isModifiedSinceSave()) {
280         gchar *text = g_strdup_printf(_("Changes will be lost!  Are you sure you want to reload document %s?"), uri);
282         bool response = desktop->warnDialog (text);
283         g_free(text);
285         if (!response) {
286             do_revert = false;
287         }
288     }
290     bool reverted;
291     if (do_revert) {
292         // Allow overwriting of current document.
293         doc->virgin = TRUE;
295         // remember current zoom and view
296         double zoom = desktop->current_zoom();
297         Geom::Point c = desktop->get_display_area().midpoint();
299         reverted = sp_file_open(uri,NULL);
300         if (reverted) {
301             // restore zoom and view
302             desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
303         }
304     } else {
305         reverted = false;
306     }
308     if (reverted) {
309         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
310     } else {
311         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
312     }
315 void dump_str(gchar const *str, gchar const *prefix)
317     Glib::ustring tmp;
318     tmp = prefix;
319     tmp += " [";
320     size_t const total = strlen(str);
321     for (unsigned i = 0; i < total; i++) {
322         gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
323         tmp += tmp2;
324         g_free(tmp2);
325     }
327     tmp += "]";
328     g_message("%s", tmp.c_str());
331 void dump_ustr(Glib::ustring const &ustr)
333     char const *cstr = ustr.c_str();
334     char const *data = ustr.data();
335     Glib::ustring::size_type const byteLen = ustr.bytes();
336     Glib::ustring::size_type const dataLen = ustr.length();
337     Glib::ustring::size_type const cstrLen = strlen(cstr);
339     g_message("   size: %lu\n   length: %lu\n   bytes: %lu\n    clen: %lu",
340               gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
341     g_message( "  ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
342     g_message( "  UTF-8? %s", (ustr.validate() ? "yes":"no") );
344     try {
345         Glib::ustring tmp;
346         for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
347             tmp = "    ";
348             if (i < dataLen) {
349                 Glib::ustring::value_type val = ustr.at(i);
350                 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? "  %02x" : "%04x"), val );
351                 tmp += tmp2;
352                 g_free( tmp2 );
353             } else {
354                 tmp += "    ";
355             }
357             if (i < byteLen) {
358                 int val = (0x0ff & data[i]);
359                 gchar *tmp2 = g_strdup_printf("    %02x", val);
360                 tmp += tmp2;
361                 g_free( tmp2 );
362                 if ( val > 32 && val < 127 ) {
363                     tmp2 = g_strdup_printf( "   '%c'", (gchar)val );
364                     tmp += tmp2;
365                     g_free( tmp2 );
366                 } else {
367                     tmp += "    . ";
368                 }
369             } else {
370                 tmp += "       ";
371             }
373             if ( i < cstrLen ) {
374                 int val = (0x0ff & cstr[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             g_message( "%s", tmp.c_str() );
390         }
391     } catch (...) {
392         g_message("XXXXXXXXXXXXXXXXXX Exception" );
393     }
394     g_message("---------------");
397 /**
398  *  Display an file Open selector.  Open a document if OK is pressed.
399  *  Can select single or multiple files for opening.
400  */
401 void
402 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
404     //# Get the current directory for finding files
405     static Glib::ustring open_path;
406     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
408     if(open_path.empty())
409     {
410         Glib::ustring attr = prefs->getString("/dialogs/open/path");
411         if (!attr.empty()) open_path = attr;
412     }
414     //# Test if the open_path directory exists
415     if (!Inkscape::IO::file_test(open_path.c_str(),
416               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
417         open_path = "";
419 #ifdef WIN32
420     //# If no open path, default to our win32 documents folder
421     if (open_path.empty())
422     {
423         // The path to the My Documents folder is read from the
424         // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
425         HKEY key = NULL;
426         if(RegOpenKeyExA(HKEY_CURRENT_USER,
427             "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
428             0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
429         {
430             WCHAR utf16path[_MAX_PATH];
431             DWORD value_type;
432             DWORD data_size = sizeof(utf16path);
433             if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
434                 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
435             {
436                 g_assert(value_type == REG_SZ);
437                 gchar *utf8path = g_utf16_to_utf8(
438                     (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
439                 if(utf8path)
440                 {
441                     open_path = Glib::ustring(utf8path);
442                     g_free(utf8path);
443                 }
444             }
445         }
446     }
447 #endif
449     //# If no open path, default to our home directory
450     if (open_path.empty())
451     {
452         open_path = g_get_home_dir();
453         open_path.append(G_DIR_SEPARATOR_S);
454     }
456     //# Create a dialog
457     Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
458               Inkscape::UI::Dialog::FileOpenDialog::create(
459                  parentWindow, open_path,
460                  Inkscape::UI::Dialog::SVG_TYPES,
461                  _("Select file to open"));
463     //# Show the dialog
464     bool const success = openDialogInstance->show();
466     //# Save the folder the user selected for later
467     open_path = openDialogInstance->getCurrentDirectory();
469     if (!success)
470     {
471         delete openDialogInstance;
472         return;
473     }
475     //# User selected something.  Get name and type
476     Glib::ustring fileName = openDialogInstance->getFilename();
478     Inkscape::Extension::Extension *selection =
479             openDialogInstance->getSelectionType();
481     //# Code to check & open if multiple files.
482     std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
484     //# We no longer need the file dialog object - delete it
485     delete openDialogInstance;
486     openDialogInstance = NULL;
488     //# Iterate through filenames if more than 1
489     if (flist.size() > 1)
490     {
491         for (unsigned int i = 0; i < flist.size(); i++)
492         {
493             fileName = flist[i];
495             Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
496             if ( newFileName.size() > 0 )
497                 fileName = newFileName;
498             else
499                 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
501 #ifdef INK_DUMP_FILENAME_CONV
502             g_message("Opening File %s\n", fileName.c_str());
503 #endif
504             sp_file_open(fileName, selection);
505         }
507         return;
508     }
511     if (!fileName.empty())
512     {
513         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
515         if ( newFileName.size() > 0)
516             fileName = newFileName;
517         else
518             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
520         open_path = Glib::path_get_dirname (fileName);
521         open_path.append(G_DIR_SEPARATOR_S);
522         prefs->setString("/dialogs/open/path", open_path);
524         sp_file_open(fileName, selection);
525     }
527     return;
531 /*######################
532 ## V A C U U M
533 ######################*/
535 /**
536  * Remove unreferenced defs from the defs section of the document.
537  */
540 void
541 sp_file_vacuum()
543     SPDocument *doc = SP_ACTIVE_DOCUMENT;
545     unsigned int diff = vacuum_document (doc);
547     sp_document_done(doc, SP_VERB_FILE_VACUUM,
548                      _("Vacuum &lt;defs&gt;"));
550     SPDesktop *dt = SP_ACTIVE_DESKTOP;
551     if (diff > 0) {
552         dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
553                 ngettext("Removed <b>%i</b> unused definition in &lt;defs&gt;.",
554                          "Removed <b>%i</b> unused definitions in &lt;defs&gt;.",
555                          diff),
556                 diff);
557     } else {
558         dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE,  _("No unused definitions in &lt;defs&gt;."));
559     }
564 /*######################
565 ## S A V E
566 ######################*/
568 /**
569  * This 'save' function called by the others below
570  *
571  * \param    official  whether to set :output_module and :modified in the
572  *                     document; is true for normal save, false for temporary saves
573  */
574 static bool
575 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
576           Inkscape::Extension::Extension *key, bool saveas, bool official)
578     if (!doc || uri.size()<1) //Safety check
579         return false;
581     try {
582         Inkscape::Extension::save(key, doc, uri.c_str(),
583                  false,
584                  saveas, official);
585     } catch (Inkscape::Extension::Output::no_extension_found &e) {
586         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
587         gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s).  This may have been caused by an unknown filename extension."), safeUri);
588         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
589         sp_ui_error_dialog(text);
590         g_free(text);
591         g_free(safeUri);
592         return FALSE;
593     } catch (Inkscape::Extension::Output::save_failed &e) {
594         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
595         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
596         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
597         sp_ui_error_dialog(text);
598         g_free(text);
599         g_free(safeUri);
600         return FALSE;
601     } catch (Inkscape::Extension::Output::save_cancelled &e) {
602         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
603         return FALSE;
604     } catch (Inkscape::Extension::Output::no_overwrite &e) {
605         return sp_file_save_dialog(parentWindow, doc);
606     }
608     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
609     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
610     return true;
613 /*
614  * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
615  */
616 bool
617 file_save_remote(SPDocument */*doc*/,
618     #ifdef WITH_GNOME_VFS
619                  const Glib::ustring &uri,
620     #else
621                  const Glib::ustring &/*uri*/,
622     #endif
623                  Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
625 #ifdef WITH_GNOME_VFS
627 #define BUF_SIZE 8192
628     gnome_vfs_init();
630     GnomeVFSHandle    *from_handle = NULL;
631     GnomeVFSHandle    *to_handle = NULL;
632     GnomeVFSFileSize  bytes_read;
633     GnomeVFSFileSize  bytes_written;
634     GnomeVFSResult    result;
635     guint8 buffer[8192];
637     gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
639     if ( uri_local == NULL ) {
640         g_warning( "Error converting filename to locale encoding.");
641     }
643     // Gets the temp file name.
644     Glib::ustring fileName = Glib::get_tmp_dir ();
645     fileName.append(G_DIR_SEPARATOR_S);
646     fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
648     // Open the temp file to send.
649     result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
651     if (result != GNOME_VFS_OK) {
652         g_warning("Could not find the temp saving.");
653         return false;
654     }
656     result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
657     result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
659     if (result != GNOME_VFS_OK) {
660         g_warning("file creating: %s", gnome_vfs_result_to_string(result));
661         return false;
662     }
664     while (1) {
666         result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
668         if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
669             result = gnome_vfs_close (from_handle);
670             result = gnome_vfs_close (to_handle);
671             return true;
672         }
674         if (result != GNOME_VFS_OK) {
675             g_warning("%s", gnome_vfs_result_to_string(result));
676             return false;
677         }
678         result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
679         if (result != GNOME_VFS_OK) {
680             g_warning("%s", gnome_vfs_result_to_string(result));
681             return false;
682         }
685         if (bytes_read != bytes_written){
686             return false;
687         }
689     }
690     return true;
691 #else
692     // in case we do not have GNOME_VFS
693     return false;
694 #endif
699 /**
700  *  Display a SaveAs dialog.  Save the document if OK pressed.
701  *
702  * \param    ascopy  (optional) wether to set the documents->uri to the new filename or not
703  */
704 bool
705 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, bool is_copy)
708     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
709     Inkscape::Extension::Output *extension = 0;
710     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
712     //# Get the default extension name
713     Glib::ustring default_extension;
714     char *attr = (char *)repr->attribute(is_copy ? "inkscape:output_extension_copy" : "inkscape:output_extension");
715     if (!attr) {
716         Glib::ustring attr2 = prefs->getString(is_copy ? "/dialogs/save_copy/default" : "/dialogs/save_as/default");
717         if(!attr2.empty()) default_extension = attr2;
718     } else {
719         default_extension = attr;
720     }
721     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
723     Glib::ustring save_path;
724     Glib::ustring save_loc;
726     if (!default_extension.empty()) {
727         extension = dynamic_cast<Inkscape::Extension::Output *>
728               (Inkscape::Extension::db.get(default_extension.c_str()));
729     } else {
730         g_warning ("No default extension!!!! What to do?\n");
731     }
733     if (doc->uri && !is_copy) {
734         // Saving as a regular file: recover the filename from the existing doc->uri
735         save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
736                                         Glib::path_get_basename(doc->uri));
737     } else {
738         Glib::ustring filename_extension = ".svg";
739         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
740         if (extension)
741             filename_extension = extension->get_extension();
743         Glib::ustring attr3 = prefs->getString(is_copy ? "/dialogs/save_copy/path" : "/dialogs/save_as/path");
744         if (!attr3.empty())
745             save_path = attr3;
747         if (!Inkscape::IO::file_test(save_path.c_str(),
748               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
749             save_path = "";
751         if (save_path.size()<1)
752             save_path = g_get_home_dir();
754         save_loc = save_path;
755         save_loc.append(G_DIR_SEPARATOR_S);
757         char formatBuf[256];
758         int i = 1;
759         if (!doc->uri) {
760             // We are saving for the first time; create a unique default filename
761             snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
762             save_loc.append(formatBuf);
764             while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
765                 save_loc = save_path;
766                 save_loc.append(G_DIR_SEPARATOR_S);
767                 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
768                 save_loc.append(formatBuf);
769             }
770         } else {
771             if (is_copy) {
772                 // Use the document uri's base name as the filename but
773                 // store in the directory last used for "Save a copy ..."
774                 snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
775                 save_loc.append(formatBuf);
776             } else {
777                 g_assert_not_reached();
778             }
779         }
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             is_copy
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         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
839         if (success && SP_DOCUMENT_URI(doc)) {
840             sp_file_add_recent(SP_DOCUMENT_URI(doc));
841         }
843         save_path = Glib::path_get_dirname(fileName);
844         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
845         prefs->setString(is_copy ? "/dialogs/save_copy/path" : "/dialogs/save_as/path", save_path);
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         Inkscape::XML::Node *repr = sp_document_repr_root(doc);
865         if ( doc->uri == NULL
866             || repr->attribute("inkscape:output_extension") == NULL )
867         {
868             return sp_file_save_dialog(parentWindow, doc, FALSE);
869         } else {
870             gchar const *fn = g_strdup(doc->uri);
871             gchar const *ext = repr->attribute("inkscape:output_extension");
872             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
873             g_free((void *) fn);
874         }
875     } else {
876         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
877         success = TRUE;
878     }
880     return success;
884 /**
885  * Save a document.
886  */
887 bool
888 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
890     if (!SP_ACTIVE_DOCUMENT)
891         return false;
893     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
895     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
896     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
900 /**
901  *  Save a document, always displaying the SaveAs dialog.
902  */
903 bool
904 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
906     if (!SP_ACTIVE_DOCUMENT)
907         return false;
908     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
909     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
914 /**
915  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
916  */
917 bool
918 sp_file_save_a_copy(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, TRUE);
927 /*######################
928 ## I M P O R T
929 ######################*/
931 /**
932  *  Import a resource.  Called by sp_file_import()
933  */
934 void
935 file_import(SPDocument *in_doc, const Glib::ustring &uri,
936                Inkscape::Extension::Extension *key)
938     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
940     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
941     SPDocument *doc;
942     try {
943         doc = Inkscape::Extension::open(key, uri.c_str());
944     } catch (Inkscape::Extension::Input::no_extension_found &e) {
945         doc = NULL;
946     } catch (Inkscape::Extension::Input::open_failed &e) {
947         doc = NULL;
948     }
950     if (doc != NULL) {
951         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
952         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
954         prevent_id_clashes(doc, in_doc);
956         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
957         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
959         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
961         // Count the number of top-level items in the imported document.
962         guint items_count = 0;
963         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
964              child != NULL; child = SP_OBJECT_NEXT(child))
965         {
966             if (SP_IS_ITEM(child)) items_count++;
967         }
969         // Create a new group if necessary.
970         Inkscape::XML::Node *newgroup = NULL;
971         if ((style && style->firstChild()) || items_count > 1) {
972             newgroup = xml_in_doc->createElement("svg:g");
973             sp_repr_css_set(newgroup, style, "style");
974         }
976         // Determine the place to insert the new object.
977         // This will be the current layer, if possible.
978         // FIXME: If there's no desktop (command line run?) we need
979         //        a document:: method to return the current layer.
980         //        For now, we just use the root in this case.
981         SPObject *place_to_insert;
982         if (desktop) {
983             place_to_insert = desktop->currentLayer();
984         } else {
985             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
986         }
988         // Construct a new object representing the imported image,
989         // and insert it into the current document.
990         SPObject *new_obj = NULL;
991         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
992              child != NULL; child = SP_OBJECT_NEXT(child) )
993         {
994             if (SP_IS_ITEM(child)) {
995                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
997                 // convert layers to groups, and make sure they are unlocked
998                 // FIXME: add "preserve layers" mode where each layer from
999                 //        import is copied to the same-named layer in host
1000                 newitem->setAttribute("inkscape:groupmode", NULL);
1001                 newitem->setAttribute("sodipodi:insensitive", NULL);
1003                 if (newgroup) newgroup->appendChild(newitem);
1004                 else new_obj = place_to_insert->appendChildRepr(newitem);
1005             }
1007             // don't lose top-level defs or style elements
1008             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1009                 const gchar *tag = SP_OBJECT_REPR(child)->name();
1010                 if (!strcmp(tag, "svg:defs")) {
1011                     for (SPObject *x = sp_object_first_child(child);
1012                          x != NULL; x = SP_OBJECT_NEXT(x))
1013                     {
1014                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1015                     }
1016                 }
1017                 else if (!strcmp(tag, "svg:style")) {
1018                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1019                 }
1020             }
1021         }
1022         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1024         // release some stuff
1025         if (newgroup) Inkscape::GC::release(newgroup);
1026         if (style) sp_repr_css_attr_unref(style);
1028         // select and move the imported item
1029         if (new_obj && SP_IS_ITEM(new_obj)) {
1030             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1031             selection->set(SP_ITEM(new_obj));
1033             // preserve parent and viewBox transformations
1034             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1035             sp_document_ensure_up_to_date(doc);
1036             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1037             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1039             // move to mouse pointer
1040             {
1041                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1042                 Geom::OptRect sel_bbox = selection->bounds();
1043                 if (sel_bbox) {
1044                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1045                     sp_selection_move_relative(selection, m, false);
1046                 }
1047             }
1048         }
1050         sp_document_unref(doc);
1051         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1052                          _("Import"));
1054     } else {
1055         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1056         sp_ui_error_dialog(text);
1057         g_free(text);
1058     }
1060     return;
1064 /**
1065  *  Display an Open dialog, import a resource if OK pressed.
1066  */
1067 void
1068 sp_file_import(Gtk::Window &parentWindow)
1070     static Glib::ustring import_path;
1072     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1073     if (!doc)
1074         return;
1076     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1078     if(import_path.empty())
1079     {
1080         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1081         if (!attr.empty()) import_path = attr;
1082     }
1084     //# Test if the import_path directory exists
1085     if (!Inkscape::IO::file_test(import_path.c_str(),
1086               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1087         import_path = "";
1089     //# If no open path, default to our home directory
1090     if (import_path.empty())
1091     {
1092         import_path = g_get_home_dir();
1093         import_path.append(G_DIR_SEPARATOR_S);
1094     }
1096     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1097     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1098              Inkscape::UI::Dialog::FileOpenDialog::create(
1099                  parentWindow,
1100                  import_path,
1101                  Inkscape::UI::Dialog::IMPORT_TYPES,
1102                  (char const *)_("Select file to import"));
1104     bool success = importDialogInstance->show();
1105     if (!success) {
1106         delete importDialogInstance;
1107         return;
1108     }
1110     //# Get file name and extension type
1111     Glib::ustring fileName = importDialogInstance->getFilename();
1112     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1114     delete importDialogInstance;
1115     importDialogInstance = NULL;
1117     if (fileName.size() > 0) {
1119         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1121         if ( newFileName.size() > 0)
1122             fileName = newFileName;
1123         else
1124             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1126         import_path = Glib::path_get_dirname (fileName);
1127         import_path.append(G_DIR_SEPARATOR_S);
1128         prefs->setString("/dialogs/import/path", import_path);
1130         file_import(doc, fileName, selection);
1131     }
1133     return;
1138 /*######################
1139 ## E X P O R T
1140 ######################*/
1142 //#define NEW_EXPORT_DIALOG
1146 #ifdef NEW_EXPORT_DIALOG
1148 /**
1149  *  Display an Export dialog, export as the selected type if OK pressed
1150  */
1151 bool
1152 sp_file_export_dialog(void *widget)
1154     //# temp hack for 'doc' until we can switch to this dialog
1155     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1157     Glib::ustring export_path;
1158     Glib::ustring export_loc;
1160     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1161     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1162     Inkscape::Extension::Output *extension;
1164     //# Get the default extension name
1165     Glib::ustring default_extension;
1166     char *attr = (char *)repr->attribute("inkscape:output_extension_export");
1167     if (!attr) {
1168         Glib::ustring attr2 = prefs->getString("/dialogs/save_export/default");
1169         if(!attr2.empty()) default_extension = attr2;
1170     } else {
1171         default_extension = attr;
1172     }
1173     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1175     if (doc->uri == NULL)
1176         {
1177         char formatBuf[256];
1179         Glib::ustring filename_extension = ".svg";
1180         extension = dynamic_cast<Inkscape::Extension::Output *>
1181               (Inkscape::Extension::db.get(default_extension.c_str()));
1182         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1183         if (extension)
1184             filename_extension = extension->get_extension();
1186         Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1187         if (!attr3.empty())
1188             export_path = attr3;
1190         if (!Inkscape::IO::file_test(export_path.c_str(),
1191               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1192             export_path = "";
1194         if (export_path.size()<1)
1195             export_path = g_get_home_dir();
1197         export_loc = export_path;
1198         export_loc.append(G_DIR_SEPARATOR_S);
1199         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1200         export_loc.append(formatBuf);
1202         }
1203     else
1204         {
1205         export_path = Glib::path_get_dirname(doc->uri);
1206         }
1208     // convert save_loc from utf-8 to locale
1209     // is this needed any more, now that everything is handled in
1210     // Inkscape::IO?
1211     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1212     if ( export_path_local.size() > 0)
1213         export_path = export_path_local;
1215     //# Show the SaveAs dialog
1216     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1217         Inkscape::UI::Dialog::FileExportDialog::create(
1218             export_path,
1219             Inkscape::UI::Dialog::EXPORT_TYPES,
1220             (char const *) _("Select file to export to"),
1221             default_extension
1222         );
1224     bool success = exportDialogInstance->show();
1225     if (!success) {
1226         delete exportDialogInstance;
1227         return success;
1228     }
1230     Glib::ustring fileName = exportDialogInstance->getFilename();
1232     Inkscape::Extension::Extension *selectionType =
1233         exportDialogInstance->getSelectionType();
1235     delete exportDialogInstance;
1236     exportDialogInstance = NULL;
1238     if (fileName.size() > 0) {
1239         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1241         if ( newFileName.size()>0 )
1242             fileName = newFileName;
1243         else
1244             g_warning( "Error converting save filename to UTF-8." );
1246         success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1248         if (success) {
1249             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1250             recent->add_item(SP_DOCUMENT_URI(doc));
1251         }
1253         export_path = fileName;
1254         prefs->setString("/dialogs/save_export/path", export_path);
1256         return success;
1257     }
1260     return false;
1263 #else
1265 /**
1266  *
1267  */
1268 bool
1269 sp_file_export_dialog(void */*widget*/)
1271     sp_export_dialog();
1272     return true;
1275 #endif
1277 /*######################
1278 ## E X P O R T  T O  O C A L
1279 ######################*/
1281 /**
1282  *  Display an Export dialog, export as the selected type if OK pressed
1283  */
1284 bool
1285 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1288    if (!SP_ACTIVE_DOCUMENT)
1289         return false;
1291     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1293     Glib::ustring export_path;
1294     Glib::ustring export_loc;
1295     Glib::ustring fileName;
1296     Inkscape::Extension::Extension *selectionType;
1298     bool success = false;
1300     static bool gotSuccess = false;
1302     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1303     (void)repr;
1305     if (!doc->uri && !doc->isModifiedSinceSave())
1306         return false;
1308     //  Get the default extension name
1309     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1310     char formatBuf[256];
1312     Glib::ustring filename_extension = ".svg";
1313     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1315     export_path = Glib::get_tmp_dir ();
1317     export_loc = export_path;
1318     export_loc.append(G_DIR_SEPARATOR_S);
1319     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1320     export_loc.append(formatBuf);
1322     // convert save_loc from utf-8 to locale
1323     // is this needed any more, now that everything is handled in
1324     // Inkscape::IO?
1325     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1326     if ( export_path_local.size() > 0)
1327         export_path = export_path_local;
1329     // Show the Export To OCAL dialog
1330     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1331         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1332                 parentWindow,
1333                 Inkscape::UI::Dialog::EXPORT_TYPES,
1334                 (char const *) _("Select file to export to")
1335                 );
1337     success = exportDialogInstance->show();
1338     if (!success) {
1339         delete exportDialogInstance;
1340         return success;
1341     }
1343     fileName = exportDialogInstance->getFilename();
1345     delete exportDialogInstance;
1346     exportDialogInstance = NULL;;
1348     fileName.append(filename_extension.c_str());
1349     if (fileName.size() > 0) {
1350         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1352         if ( newFileName.size()>0 )
1353             fileName = newFileName;
1354         else
1355             g_warning( "Error converting save filename to UTF-8." );
1356     }
1357     Glib::ustring filePath = export_path;
1358     filePath.append(G_DIR_SEPARATOR_S);
1359     filePath.append(Glib::path_get_basename(fileName));
1361     fileName = filePath;
1363     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1365     if (!success){
1366         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1367         sp_ui_error_dialog(text);
1369         return success;
1370     }
1372     // Start now the submition
1374     // Create the uri
1375     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1376     Glib::ustring uri = "dav://";
1377     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1378     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1379     if (username.empty() || password.empty())
1380     {
1381         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1382         if(!gotSuccess)
1383         {
1384             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1385                     parentWindow,
1386                     (char const *) _("Open Clip Art Login"));
1387             success = exportPasswordDialogInstance->show();
1388             if (!success) {
1389                 delete exportPasswordDialogInstance;
1390                 return success;
1391             }
1392         }
1393         username = exportPasswordDialogInstance->getUsername();
1394         password = exportPasswordDialogInstance->getPassword();
1396         delete exportPasswordDialogInstance;
1397         exportPasswordDialogInstance = NULL;
1398     }
1399     uri.append(username);
1400     uri.append(":");
1401     uri.append(password);
1402     uri.append("@");
1403     uri.append(prefs->getString("/options/ocalurl/str"));
1404     uri.append("/dav.php/");
1405     uri.append(Glib::path_get_basename(fileName));
1407     // Save as a remote file using the dav protocol.
1408     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1409     remove(fileName.c_str());
1410     if (!success)
1411     {
1412         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."));
1413         sp_ui_error_dialog(text);
1414     }
1415     else
1416         gotSuccess = true;
1418     return success;
1421 /**
1422  * Export the current document to OCAL
1423  */
1424 void
1425 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1428     // Try to execute the new code and return;
1429     if (!SP_ACTIVE_DOCUMENT)
1430         return;
1431     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1432     if (success)
1433         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1437 /*######################
1438 ## I M P O R T  F R O M  O C A L
1439 ######################*/
1441 /**
1442  * Display an ImportToOcal Dialog, and the selected document from OCAL
1443  */
1444 void
1445 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1447     static Glib::ustring import_path;
1449     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1450     if (!doc)
1451         return;
1453     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1455     if (!importDialogInstance) {
1456         importDialogInstance = new
1457              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1458                  parentWindow,
1459                  import_path,
1460                  Inkscape::UI::Dialog::IMPORT_TYPES,
1461                  (char const *)_("Import From Open Clip Art Library"));
1462     }
1464     bool success = importDialogInstance->show();
1465     if (!success) {
1466         delete importDialogInstance;
1467         return;
1468     }
1470     // Get file name and extension type
1471     Glib::ustring fileName = importDialogInstance->getFilename();
1472     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1474     delete importDialogInstance;
1475     importDialogInstance = NULL;
1477     if (fileName.size() > 0) {
1479         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1481         if ( newFileName.size() > 0)
1482             fileName = newFileName;
1483         else
1484             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1486         import_path = fileName;
1487         if (import_path.size()>0)
1488             import_path.append(G_DIR_SEPARATOR_S);
1490         file_import(doc, fileName, selection);
1491     }
1493     return;
1496 /*######################
1497 ## P R I N T
1498 ######################*/
1501 /**
1502  *  Print the current document, if any.
1503  */
1504 void
1505 sp_file_print(Gtk::Window& parentWindow)
1507     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1508     if (doc)
1509         sp_print_document(parentWindow, doc);
1512 /**
1513  * Display what the drawing would look like, if
1514  * printed.
1515  */
1516 void
1517 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1520     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1521     if (doc)
1522         sp_print_preview_document(doc);
1527 /*
1528   Local Variables:
1529   mode:c++
1530   c-file-style:"stroustrup"
1531   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1532   indent-tabs-mode:nil
1533   fill-column:99
1534   End:
1535 */
1536 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :