Code

049c1acb405313bce8ad55075074075720e742cc
[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("inkscape:output_extension");
715     if (!attr) {
716         Glib::ustring attr2 = prefs->getString("/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 (doc->uri == NULL) {
727         char formatBuf[256];
728         int i = 1;
730         Glib::ustring filename_extension = ".svg";
731         extension = dynamic_cast<Inkscape::Extension::Output *>
732               (Inkscape::Extension::db.get(default_extension.c_str()));
733         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
734         if (extension)
735             filename_extension = extension->get_extension();
737         Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
738         if (!attr3.empty())
739             save_path = attr3;
741         if (!Inkscape::IO::file_test(save_path.c_str(),
742               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
743             save_path = "";
745         if (save_path.size()<1)
746             save_path = g_get_home_dir();
748         save_loc = save_path;
749         save_loc.append(G_DIR_SEPARATOR_S);
750         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
751         save_loc.append(formatBuf);
753         while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
754             save_loc = save_path;
755             save_loc.append(G_DIR_SEPARATOR_S);
756             snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
757             save_loc.append(formatBuf);
758         }
759     } else {
760         save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
761                                         Glib::path_get_basename(doc->uri));
762     }
764     // convert save_loc from utf-8 to locale
765     // is this needed any more, now that everything is handled in
766     // Inkscape::IO?
767     Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
769     if ( save_loc_local.size() > 0)
770         save_loc = save_loc_local;
772     //# Show the SaveAs dialog
773     char const * dialog_title;
774     if (is_copy) {
775         dialog_title = (char const *) _("Select file to save a copy to");
776     } else {
777         dialog_title = (char const *) _("Select file to save to");
778     }
779     gchar* doc_title = doc->root->title();
780     Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
781         Inkscape::UI::Dialog::FileSaveDialog::create(
782             parentWindow,
783             save_loc,
784             Inkscape::UI::Dialog::SVG_TYPES,
785             dialog_title,
786             default_extension,
787             doc_title ? doc_title : ""
788             );
790     saveDialog->setSelectionType(extension);
792     bool success = saveDialog->show();
793     if (!success) {
794         delete saveDialog;
795         return success;
796     }
798     // set new title here (call RDF to ensure metadata and title element are updated)
799     rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
800     // free up old string
801     if(doc_title) g_free(doc_title);
803     Glib::ustring fileName = saveDialog->getFilename();
804     Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
806     delete saveDialog;
808     saveDialog = 0;
810     if (fileName.size() > 0) {
811         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
813         if ( newFileName.size()>0 )
814             fileName = newFileName;
815         else
816             g_warning( "Error converting save filename to UTF-8." );
818         success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
820         if (success && SP_DOCUMENT_URI(doc)) {
821             sp_file_add_recent(SP_DOCUMENT_URI(doc));
822         }
824         save_path = Glib::path_get_dirname(fileName);
825         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
826         prefs->setString("/dialogs/save_as/path", save_path);
828         return success;
829     }
832     return false;
836 /**
837  * Save a document, displaying a SaveAs dialog if necessary.
838  */
839 bool
840 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
842     bool success = true;
844     if (doc->isModifiedSinceSave()) {
845         Inkscape::XML::Node *repr = sp_document_repr_root(doc);
846         if ( doc->uri == NULL
847             || repr->attribute("inkscape:output_extension") == NULL )
848         {
849             return sp_file_save_dialog(parentWindow, doc, FALSE);
850         } else {
851             gchar const *fn = g_strdup(doc->uri);
852             gchar const *ext = repr->attribute("inkscape:output_extension");
853             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE);
854             g_free((void *) fn);
855         }
856     } else {
857         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
858         success = TRUE;
859     }
861     return success;
865 /**
866  * Save a document.
867  */
868 bool
869 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
871     if (!SP_ACTIVE_DOCUMENT)
872         return false;
874     SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
876     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
877     return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
881 /**
882  *  Save a document, always displaying the SaveAs dialog.
883  */
884 bool
885 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
887     if (!SP_ACTIVE_DOCUMENT)
888         return false;
889     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
890     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, FALSE);
895 /**
896  *  Save a copy of a document, always displaying a sort of SaveAs dialog.
897  */
898 bool
899 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
901     if (!SP_ACTIVE_DOCUMENT)
902         return false;
903     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
904     return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, TRUE);
908 /*######################
909 ## I M P O R T
910 ######################*/
912 /**
913  *  Import a resource.  Called by sp_file_import()
914  */
915 void
916 file_import(SPDocument *in_doc, const Glib::ustring &uri,
917                Inkscape::Extension::Extension *key)
919     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
921     //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
922     SPDocument *doc;
923     try {
924         doc = Inkscape::Extension::open(key, uri.c_str());
925     } catch (Inkscape::Extension::Input::no_extension_found &e) {
926         doc = NULL;
927     } catch (Inkscape::Extension::Input::open_failed &e) {
928         doc = NULL;
929     }
931     if (doc != NULL) {
932         Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
933         Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
935         prevent_id_clashes(doc, in_doc);
937         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
938         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
940         SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
942         // Count the number of top-level items in the imported document.
943         guint items_count = 0;
944         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
945              child != NULL; child = SP_OBJECT_NEXT(child))
946         {
947             if (SP_IS_ITEM(child)) items_count++;
948         }
950         // Create a new group if necessary.
951         Inkscape::XML::Node *newgroup = NULL;
952         if ((style && style->firstChild()) || items_count > 1) {
953             newgroup = xml_in_doc->createElement("svg:g");
954             sp_repr_css_set(newgroup, style, "style");
955         }
957         // Determine the place to insert the new object.
958         // This will be the current layer, if possible.
959         // FIXME: If there's no desktop (command line run?) we need
960         //        a document:: method to return the current layer.
961         //        For now, we just use the root in this case.
962         SPObject *place_to_insert;
963         if (desktop) {
964             place_to_insert = desktop->currentLayer();
965         } else {
966             place_to_insert = SP_DOCUMENT_ROOT(in_doc);
967         }
969         // Construct a new object representing the imported image,
970         // and insert it into the current document.
971         SPObject *new_obj = NULL;
972         for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
973              child != NULL; child = SP_OBJECT_NEXT(child) )
974         {
975             if (SP_IS_ITEM(child)) {
976                 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
978                 // convert layers to groups, and make sure they are unlocked
979                 // FIXME: add "preserve layers" mode where each layer from
980                 //        import is copied to the same-named layer in host
981                 newitem->setAttribute("inkscape:groupmode", NULL);
982                 newitem->setAttribute("sodipodi:insensitive", NULL);
984                 if (newgroup) newgroup->appendChild(newitem);
985                 else new_obj = place_to_insert->appendChildRepr(newitem);
986             }
988             // don't lose top-level defs or style elements
989             else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
990                 const gchar *tag = SP_OBJECT_REPR(child)->name();
991                 if (!strcmp(tag, "svg:defs")) {
992                     for (SPObject *x = sp_object_first_child(child);
993                          x != NULL; x = SP_OBJECT_NEXT(x))
994                     {
995                         SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
996                     }
997                 }
998                 else if (!strcmp(tag, "svg:style")) {
999                     SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1000                 }
1001             }
1002         }
1003         if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1005         // release some stuff
1006         if (newgroup) Inkscape::GC::release(newgroup);
1007         if (style) sp_repr_css_attr_unref(style);
1009         // select and move the imported item
1010         if (new_obj && SP_IS_ITEM(new_obj)) {
1011             Inkscape::Selection *selection = sp_desktop_selection(desktop);
1012             selection->set(SP_ITEM(new_obj));
1014             // preserve parent and viewBox transformations
1015             // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1016             sp_document_ensure_up_to_date(doc);
1017             Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1018             sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1020             // move to mouse pointer
1021             {
1022                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1023                 Geom::OptRect sel_bbox = selection->bounds();
1024                 if (sel_bbox) {
1025                     Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1026                     sp_selection_move_relative(selection, m, false);
1027                 }
1028             }
1029         }
1031         sp_document_unref(doc);
1032         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1033                          _("Import"));
1035     } else {
1036         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1037         sp_ui_error_dialog(text);
1038         g_free(text);
1039     }
1041     return;
1045 /**
1046  *  Display an Open dialog, import a resource if OK pressed.
1047  */
1048 void
1049 sp_file_import(Gtk::Window &parentWindow)
1051     static Glib::ustring import_path;
1053     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1054     if (!doc)
1055         return;
1057     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1059     if(import_path.empty())
1060     {
1061         Glib::ustring attr = prefs->getString("/dialogs/import/path");
1062         if (!attr.empty()) import_path = attr;
1063     }
1065     //# Test if the import_path directory exists
1066     if (!Inkscape::IO::file_test(import_path.c_str(),
1067               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1068         import_path = "";
1070     //# If no open path, default to our home directory
1071     if (import_path.empty())
1072     {
1073         import_path = g_get_home_dir();
1074         import_path.append(G_DIR_SEPARATOR_S);
1075     }
1077     // Create new dialog (don't use an old one, because parentWindow has probably changed)
1078     Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1079              Inkscape::UI::Dialog::FileOpenDialog::create(
1080                  parentWindow,
1081                  import_path,
1082                  Inkscape::UI::Dialog::IMPORT_TYPES,
1083                  (char const *)_("Select file to import"));
1085     bool success = importDialogInstance->show();
1086     if (!success) {
1087         delete importDialogInstance;
1088         return;
1089     }
1091     //# Get file name and extension type
1092     Glib::ustring fileName = importDialogInstance->getFilename();
1093     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1095     delete importDialogInstance;
1096     importDialogInstance = NULL;
1098     if (fileName.size() > 0) {
1100         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1102         if ( newFileName.size() > 0)
1103             fileName = newFileName;
1104         else
1105             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1107         import_path = Glib::path_get_dirname (fileName);
1108         import_path.append(G_DIR_SEPARATOR_S);
1109         prefs->setString("/dialogs/import/path", import_path);
1111         file_import(doc, fileName, selection);
1112     }
1114     return;
1119 /*######################
1120 ## E X P O R T
1121 ######################*/
1123 //#define NEW_EXPORT_DIALOG
1127 #ifdef NEW_EXPORT_DIALOG
1129 /**
1130  *  Display an Export dialog, export as the selected type if OK pressed
1131  */
1132 bool
1133 sp_file_export_dialog(void *widget)
1135     //# temp hack for 'doc' until we can switch to this dialog
1136     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1138     Glib::ustring export_path;
1139     Glib::ustring export_loc;
1141     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1142     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1143     Inkscape::Extension::Output *extension;
1145     //# Get the default extension name
1146     Glib::ustring default_extension;
1147     char *attr = (char *)repr->attribute("inkscape:output_extension");
1148     if (!attr) {
1149         Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
1150         if(!attr2.empty()) default_extension = attr2;
1151     } else {
1152         default_extension = attr;
1153     }
1154     //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1156     if (doc->uri == NULL)
1157         {
1158         char formatBuf[256];
1160         Glib::ustring filename_extension = ".svg";
1161         extension = dynamic_cast<Inkscape::Extension::Output *>
1162               (Inkscape::Extension::db.get(default_extension.c_str()));
1163         //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1164         if (extension)
1165             filename_extension = extension->get_extension();
1167         Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
1168         if (!attr3.empty())
1169             export_path = attr3;
1171         if (!Inkscape::IO::file_test(export_path.c_str(),
1172               (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1173             export_path = "";
1175         if (export_path.size()<1)
1176             export_path = g_get_home_dir();
1178         export_loc = export_path;
1179         export_loc.append(G_DIR_SEPARATOR_S);
1180         snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1181         export_loc.append(formatBuf);
1183         }
1184     else
1185         {
1186         export_path = Glib::path_get_dirname(doc->uri);
1187         }
1189     // convert save_loc from utf-8 to locale
1190     // is this needed any more, now that everything is handled in
1191     // Inkscape::IO?
1192     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1193     if ( export_path_local.size() > 0)
1194         export_path = export_path_local;
1196     //# Show the SaveAs dialog
1197     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1198         Inkscape::UI::Dialog::FileExportDialog::create(
1199             export_path,
1200             Inkscape::UI::Dialog::EXPORT_TYPES,
1201             (char const *) _("Select file to export to"),
1202             default_extension
1203         );
1205     bool success = exportDialogInstance->show();
1206     if (!success) {
1207         delete exportDialogInstance;
1208         return success;
1209     }
1211     Glib::ustring fileName = exportDialogInstance->getFilename();
1213     Inkscape::Extension::Extension *selectionType =
1214         exportDialogInstance->getSelectionType();
1216     delete exportDialogInstance;
1217     exportDialogInstance = NULL;
1219     if (fileName.size() > 0) {
1220         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1222         if ( newFileName.size()>0 )
1223             fileName = newFileName;
1224         else
1225             g_warning( "Error converting save filename to UTF-8." );
1227         success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1229         if (success) {
1230             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1231             recent->add_item(SP_DOCUMENT_URI(doc));
1232         }
1234         export_path = fileName;
1235         prefs->setString("/dialogs/save_as/path", export_path);
1237         return success;
1238     }
1241     return false;
1244 #else
1246 /**
1247  *
1248  */
1249 bool
1250 sp_file_export_dialog(void */*widget*/)
1252     sp_export_dialog();
1253     return true;
1256 #endif
1258 /*######################
1259 ## E X P O R T  T O  O C A L
1260 ######################*/
1262 /**
1263  *  Display an Export dialog, export as the selected type if OK pressed
1264  */
1265 bool
1266 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1269    if (!SP_ACTIVE_DOCUMENT)
1270         return false;
1272     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1274     Glib::ustring export_path;
1275     Glib::ustring export_loc;
1276     Glib::ustring fileName;
1277     Inkscape::Extension::Extension *selectionType;
1279     bool success = false;
1281     static bool gotSuccess = false;
1283     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1284     (void)repr;
1286     if (!doc->uri && !doc->isModifiedSinceSave())
1287         return false;
1289     //  Get the default extension name
1290     Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1291     char formatBuf[256];
1293     Glib::ustring filename_extension = ".svg";
1294     selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1296     export_path = Glib::get_tmp_dir ();
1298     export_loc = export_path;
1299     export_loc.append(G_DIR_SEPARATOR_S);
1300     snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1301     export_loc.append(formatBuf);
1303     // convert save_loc from utf-8 to locale
1304     // is this needed any more, now that everything is handled in
1305     // Inkscape::IO?
1306     Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1307     if ( export_path_local.size() > 0)
1308         export_path = export_path_local;
1310     // Show the Export To OCAL dialog
1311     Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1312         new Inkscape::UI::Dialog::FileExportToOCALDialog(
1313                 parentWindow,
1314                 Inkscape::UI::Dialog::EXPORT_TYPES,
1315                 (char const *) _("Select file to export to")
1316                 );
1318     success = exportDialogInstance->show();
1319     if (!success) {
1320         delete exportDialogInstance;
1321         return success;
1322     }
1324     fileName = exportDialogInstance->getFilename();
1326     delete exportDialogInstance;
1327     exportDialogInstance = NULL;;
1329     fileName.append(filename_extension.c_str());
1330     if (fileName.size() > 0) {
1331         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1333         if ( newFileName.size()>0 )
1334             fileName = newFileName;
1335         else
1336             g_warning( "Error converting save filename to UTF-8." );
1337     }
1338     Glib::ustring filePath = export_path;
1339     filePath.append(G_DIR_SEPARATOR_S);
1340     filePath.append(Glib::path_get_basename(fileName));
1342     fileName = filePath;
1344     success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE);
1346     if (!success){
1347         gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1348         sp_ui_error_dialog(text);
1350         return success;
1351     }
1353     // Start now the submition
1355     // Create the uri
1356     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1357     Glib::ustring uri = "dav://";
1358     Glib::ustring username = prefs->getString("/options/ocalusername/str");
1359     Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1360     if (username.empty() || password.empty())
1361     {
1362         Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1363         if(!gotSuccess)
1364         {
1365             exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1366                     parentWindow,
1367                     (char const *) _("Open Clip Art Login"));
1368             success = exportPasswordDialogInstance->show();
1369             if (!success) {
1370                 delete exportPasswordDialogInstance;
1371                 return success;
1372             }
1373         }
1374         username = exportPasswordDialogInstance->getUsername();
1375         password = exportPasswordDialogInstance->getPassword();
1377         delete exportPasswordDialogInstance;
1378         exportPasswordDialogInstance = NULL;
1379     }
1380     uri.append(username);
1381     uri.append(":");
1382     uri.append(password);
1383     uri.append("@");
1384     uri.append(prefs->getString("/options/ocalurl/str"));
1385     uri.append("/dav.php/");
1386     uri.append(Glib::path_get_basename(fileName));
1388     // Save as a remote file using the dav protocol.
1389     success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1390     remove(fileName.c_str());
1391     if (!success)
1392     {
1393         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."));
1394         sp_ui_error_dialog(text);
1395     }
1396     else
1397         gotSuccess = true;
1399     return success;
1402 /**
1403  * Export the current document to OCAL
1404  */
1405 void
1406 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1409     // Try to execute the new code and return;
1410     if (!SP_ACTIVE_DOCUMENT)
1411         return;
1412     bool success = sp_file_export_to_ocal_dialog(parentWindow);
1413     if (success)
1414         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1418 /*######################
1419 ## I M P O R T  F R O M  O C A L
1420 ######################*/
1422 /**
1423  * Display an ImportToOcal Dialog, and the selected document from OCAL
1424  */
1425 void
1426 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1428     static Glib::ustring import_path;
1430     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1431     if (!doc)
1432         return;
1434     Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1436     if (!importDialogInstance) {
1437         importDialogInstance = new
1438              Inkscape::UI::Dialog::FileImportFromOCALDialog(
1439                  parentWindow,
1440                  import_path,
1441                  Inkscape::UI::Dialog::IMPORT_TYPES,
1442                  (char const *)_("Import From Open Clip Art Library"));
1443     }
1445     bool success = importDialogInstance->show();
1446     if (!success) {
1447         delete importDialogInstance;
1448         return;
1449     }
1451     // Get file name and extension type
1452     Glib::ustring fileName = importDialogInstance->getFilename();
1453     Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1455     delete importDialogInstance;
1456     importDialogInstance = NULL;
1458     if (fileName.size() > 0) {
1460         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1462         if ( newFileName.size() > 0)
1463             fileName = newFileName;
1464         else
1465             g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1467         import_path = fileName;
1468         if (import_path.size()>0)
1469             import_path.append(G_DIR_SEPARATOR_S);
1471         file_import(doc, fileName, selection);
1472     }
1474     return;
1477 /*######################
1478 ## P R I N T
1479 ######################*/
1482 /**
1483  *  Print the current document, if any.
1484  */
1485 void
1486 sp_file_print(Gtk::Window& parentWindow)
1488     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1489     if (doc)
1490         sp_print_document(parentWindow, doc);
1493 /**
1494  * Display what the drawing would look like, if
1495  * printed.
1496  */
1497 void
1498 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1501     SPDocument *doc = SP_ACTIVE_DOCUMENT;
1502     if (doc)
1503         sp_print_preview_document(doc);
1508 /*
1509   Local Variables:
1510   mode:c++
1511   c-file-style:"stroustrup"
1512   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1513   indent-tabs-mode:nil
1514   fill-column:99
1515   End:
1516 */
1517 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :