1 /** @file
2 * @brief File/Print operations
3 */
4 /* Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Chema Celorio <chema@celorio.com>
7 * bulia byak <buliabyak@users.sf.net>
8 * Bruno Dilly <bruno.dilly@gmail.com>
9 * Stephen Silver <sasilver@users.sourceforge.net>
10 *
11 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12 * Copyright (C) 1999-2008 Authors
13 * Copyright (C) 2004 David Turner
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 /** @file
20 * @note This file needs to be cleaned up extensively.
21 * What it probably needs is to have one .h file for
22 * the API, and two or more .cpp files for the implementations.
23 */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <gtk/gtk.h>
30 #include <glib/gmem.h>
31 #include <glibmm/i18n.h>
32 #include <libnr/nr-pixops.h>
34 #include "application/application.h"
35 #include "application/editor.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
38 #include "dialogs/export.h"
39 #include "dir-util.h"
40 #include "document-private.h"
41 #include "extension/db.h"
42 #include "extension/input.h"
43 #include "extension/output.h"
44 #include "extension/system.h"
45 #include "file.h"
46 #include "helper/png-write.h"
47 #include "id-clash.h"
48 #include "inkscape.h"
49 #include "inkscape.h"
50 #include "interface.h"
51 #include "io/sys.h"
52 #include "message.h"
53 #include "message-stack.h"
54 #include "path-prefix.h"
55 #include "preferences.h"
56 #include "print.h"
57 #include "rdf.h"
58 #include "selection-chemistry.h"
59 #include "selection.h"
60 #include "sp-namedview.h"
61 #include "style.h"
62 #include "ui/dialog/ocaldialogs.h"
63 #include "ui/view/view-widget.h"
64 #include "uri.h"
65 #include "xml/rebase-hrefs.h"
67 #ifdef WITH_GNOME_VFS
68 # include <libgnomevfs/gnome-vfs.h>
69 #endif
71 //#ifdef WITH_INKBOARD
72 //#include "jabber_whiteboard/session-manager.h"
73 //#endif
75 #ifdef WIN32
76 #include <windows.h>
77 #endif
79 //#define INK_DUMP_FILENAME_CONV 1
80 #undef INK_DUMP_FILENAME_CONV
82 //#define INK_DUMP_FOPEN 1
83 #undef INK_DUMP_FOPEN
85 void dump_str(gchar const *str, gchar const *prefix);
86 void dump_ustr(Glib::ustring const &ustr);
88 // what gets passed here is not actually an URI... it is an UTF-8 encoded filename (!)
89 static void sp_file_add_recent(gchar const *uri)
90 {
91 if(uri == NULL) {
92 g_warning("sp_file_add_recent: uri == NULL");
93 return;
94 }
95 GtkRecentManager *recent = gtk_recent_manager_get_default();
96 gchar *fn = g_filename_from_utf8(uri, -1, NULL, NULL, NULL);
97 if (fn) {
98 gchar *uri_to_add = g_filename_to_uri(fn, NULL, NULL);
99 if (uri_to_add) {
100 gtk_recent_manager_add_item(recent, uri_to_add);
101 g_free(uri_to_add);
102 }
103 g_free(fn);
104 }
105 }
108 /*######################
109 ## N E W
110 ######################*/
112 /**
113 * Create a blank document and add it to the desktop
114 */
115 SPDesktop*
116 sp_file_new(const Glib::ustring &templ)
117 {
118 char *templName = NULL;
119 if (templ.size()>0)
120 templName = (char *)templ.c_str();
121 SPDocument *doc = sp_document_new(templName, TRUE, true);
122 g_return_val_if_fail(doc != NULL, NULL);
124 SPDesktop *dt;
125 if (Inkscape::NSApplication::Application::getNewGui())
126 {
127 dt = Inkscape::NSApplication::Editor::createDesktop (doc);
128 } else {
129 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
130 g_return_val_if_fail(dtw != NULL, NULL);
131 sp_document_unref(doc);
133 sp_create_window(dtw, TRUE);
134 dt = static_cast<SPDesktop*>(dtw->view);
135 sp_namedview_window_from_document(dt);
136 sp_namedview_update_layers_from_document(dt);
137 }
138 return dt;
139 }
141 SPDesktop* sp_file_new_default()
142 {
143 std::list<gchar *> sources;
144 sources.push_back( profile_path("templates") ); // first try user's local dir
145 sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
146 std::list<gchar const*> baseNames;
147 gchar const* localized = _("default.svg");
148 if (strcmp("default.svg", localized) != 0) {
149 baseNames.push_back(localized);
150 }
151 baseNames.push_back("default.svg");
152 gchar *foundTemplate = 0;
154 for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
155 for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
156 gchar *dirname = *it;
157 if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
159 // TRANSLATORS: default.svg is localizable - this is the name of the default document
160 // template. This way you can localize the default pagesize, translate the name of
161 // the default layer, etc. If you wish to localize this file, please create a
162 // localized share/templates/default.xx.svg file, where xx is your language code.
163 char *tmp = g_build_filename(dirname, *nameIt, NULL);
164 if (Inkscape::IO::file_test(tmp, G_FILE_TEST_IS_REGULAR)) {
165 foundTemplate = tmp;
166 } else {
167 g_free(tmp);
168 }
169 }
170 }
171 }
173 for (std::list<gchar *>::iterator it = sources.begin(); it != sources.end(); ++it) {
174 g_free(*it);
175 }
177 SPDesktop* desk = sp_file_new(foundTemplate ? foundTemplate : "");
178 if (foundTemplate) {
179 g_free(foundTemplate);
180 foundTemplate = 0;
181 }
182 return desk;
183 }
186 /*######################
187 ## D E L E T E
188 ######################*/
190 /**
191 * Perform document closures preceding an exit()
192 */
193 void
194 sp_file_exit()
195 {
196 sp_ui_close_all();
197 // no need to call inkscape_exit here; last document being closed will take care of that
198 }
201 /*######################
202 ## O P E N
203 ######################*/
205 /**
206 * Open a file, add the document to the desktop
207 *
208 * \param replace_empty if true, and the current desktop is empty, this document
209 * will replace the empty one.
210 */
211 bool
212 sp_file_open(const Glib::ustring &uri,
213 Inkscape::Extension::Extension *key,
214 bool add_to_recent, bool replace_empty)
215 {
216 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
217 if (desktop)
218 desktop->setWaitingCursor();
220 SPDocument *doc = NULL;
221 try {
222 doc = Inkscape::Extension::open(key, uri.c_str());
223 } catch (Inkscape::Extension::Input::no_extension_found &e) {
224 doc = NULL;
225 } catch (Inkscape::Extension::Input::open_failed &e) {
226 doc = NULL;
227 }
229 if (desktop)
230 desktop->clearWaitingCursor();
232 if (doc) {
233 SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
235 if (existing && existing->virgin && replace_empty) {
236 // If the current desktop is empty, open the document there
237 sp_document_ensure_up_to_date (doc);
238 desktop->change_document(doc);
239 sp_document_resized_signal_emit (doc, sp_document_width(doc), sp_document_height(doc));
240 } else {
241 if (!Inkscape::NSApplication::Application::getNewGui()) {
242 // create a whole new desktop and window
243 SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
244 sp_create_window(dtw, TRUE);
245 desktop = static_cast<SPDesktop*>(dtw->view);
246 } else {
247 desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
248 }
249 }
251 doc->virgin = FALSE;
252 // everyone who cares now has a reference, get rid of ours
253 sp_document_unref(doc);
254 // resize the window to match the document properties
255 sp_namedview_window_from_document(desktop);
256 sp_namedview_update_layers_from_document(desktop);
258 if (add_to_recent) {
259 sp_file_add_recent(SP_DOCUMENT_URI(doc));
260 }
262 return TRUE;
263 } else {
264 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
265 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
266 sp_ui_error_dialog(text);
267 g_free(text);
268 g_free(safeUri);
269 return FALSE;
270 }
271 }
273 /**
274 * Handle prompting user for "do you want to revert"? Revert on "OK"
275 */
276 void
277 sp_file_revert_dialog()
278 {
279 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
280 g_assert(desktop != NULL);
282 SPDocument *doc = sp_desktop_document(desktop);
283 g_assert(doc != NULL);
285 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
286 g_assert(repr != NULL);
288 gchar const *uri = doc->uri;
289 if (!uri) {
290 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
291 return;
292 }
294 bool do_revert = true;
295 if (doc->isModifiedSinceSave()) {
296 gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
298 bool response = desktop->warnDialog (text);
299 g_free(text);
301 if (!response) {
302 do_revert = false;
303 }
304 }
306 bool reverted;
307 if (do_revert) {
308 // Allow overwriting of current document.
309 doc->virgin = TRUE;
311 // remember current zoom and view
312 double zoom = desktop->current_zoom();
313 Geom::Point c = desktop->get_display_area().midpoint();
315 reverted = sp_file_open(uri,NULL);
316 if (reverted) {
317 // restore zoom and view
318 desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
319 }
320 } else {
321 reverted = false;
322 }
324 if (reverted) {
325 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
326 } else {
327 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
328 }
329 }
331 void dump_str(gchar const *str, gchar const *prefix)
332 {
333 Glib::ustring tmp;
334 tmp = prefix;
335 tmp += " [";
336 size_t const total = strlen(str);
337 for (unsigned i = 0; i < total; i++) {
338 gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
339 tmp += tmp2;
340 g_free(tmp2);
341 }
343 tmp += "]";
344 g_message("%s", tmp.c_str());
345 }
347 void dump_ustr(Glib::ustring const &ustr)
348 {
349 char const *cstr = ustr.c_str();
350 char const *data = ustr.data();
351 Glib::ustring::size_type const byteLen = ustr.bytes();
352 Glib::ustring::size_type const dataLen = ustr.length();
353 Glib::ustring::size_type const cstrLen = strlen(cstr);
355 g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
356 gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
357 g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
358 g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
360 try {
361 Glib::ustring tmp;
362 for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
363 tmp = " ";
364 if (i < dataLen) {
365 Glib::ustring::value_type val = ustr.at(i);
366 gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
367 tmp += tmp2;
368 g_free( tmp2 );
369 } else {
370 tmp += " ";
371 }
373 if (i < byteLen) {
374 int val = (0x0ff & data[i]);
375 gchar *tmp2 = g_strdup_printf(" %02x", val);
376 tmp += tmp2;
377 g_free( tmp2 );
378 if ( val > 32 && val < 127 ) {
379 tmp2 = g_strdup_printf( " '%c'", (gchar)val );
380 tmp += tmp2;
381 g_free( tmp2 );
382 } else {
383 tmp += " . ";
384 }
385 } else {
386 tmp += " ";
387 }
389 if ( i < cstrLen ) {
390 int val = (0x0ff & cstr[i]);
391 gchar* tmp2 = g_strdup_printf(" %02x", val);
392 tmp += tmp2;
393 g_free(tmp2);
394 if ( val > 32 && val < 127 ) {
395 tmp2 = g_strdup_printf(" '%c'", (gchar) val);
396 tmp += tmp2;
397 g_free( tmp2 );
398 } else {
399 tmp += " . ";
400 }
401 } else {
402 tmp += " ";
403 }
405 g_message( "%s", tmp.c_str() );
406 }
407 } catch (...) {
408 g_message("XXXXXXXXXXXXXXXXXX Exception" );
409 }
410 g_message("---------------");
411 }
413 /**
414 * Display an file Open selector. Open a document if OK is pressed.
415 * Can select single or multiple files for opening.
416 */
417 void
418 sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
419 {
420 //# Get the current directory for finding files
421 static Glib::ustring open_path;
422 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
424 if(open_path.empty())
425 {
426 Glib::ustring attr = prefs->getString("/dialogs/open/path");
427 if (!attr.empty()) open_path = attr;
428 }
430 //# Test if the open_path directory exists
431 if (!Inkscape::IO::file_test(open_path.c_str(),
432 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
433 open_path = "";
435 #ifdef WIN32
436 //# If no open path, default to our win32 documents folder
437 if (open_path.empty())
438 {
439 // The path to the My Documents folder is read from the
440 // value "HKEY_CURRENT_USER\Software\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
441 HKEY key = NULL;
442 if(RegOpenKeyExA(HKEY_CURRENT_USER,
443 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
444 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
445 {
446 WCHAR utf16path[_MAX_PATH];
447 DWORD value_type;
448 DWORD data_size = sizeof(utf16path);
449 if(RegQueryValueExW(key, L"Personal", NULL, &value_type,
450 (BYTE*)utf16path, &data_size) == ERROR_SUCCESS)
451 {
452 g_assert(value_type == REG_SZ);
453 gchar *utf8path = g_utf16_to_utf8(
454 (const gunichar2*)utf16path, -1, NULL, NULL, NULL);
455 if(utf8path)
456 {
457 open_path = Glib::ustring(utf8path);
458 g_free(utf8path);
459 }
460 }
461 }
462 }
463 #endif
465 //# If no open path, default to our home directory
466 if (open_path.empty())
467 {
468 open_path = g_get_home_dir();
469 open_path.append(G_DIR_SEPARATOR_S);
470 }
472 //# Create a dialog
473 Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
474 Inkscape::UI::Dialog::FileOpenDialog::create(
475 parentWindow, open_path,
476 Inkscape::UI::Dialog::SVG_TYPES,
477 _("Select file to open"));
479 //# Show the dialog
480 bool const success = openDialogInstance->show();
482 //# Save the folder the user selected for later
483 open_path = openDialogInstance->getCurrentDirectory();
485 if (!success)
486 {
487 delete openDialogInstance;
488 return;
489 }
491 //# User selected something. Get name and type
492 Glib::ustring fileName = openDialogInstance->getFilename();
494 Inkscape::Extension::Extension *selection =
495 openDialogInstance->getSelectionType();
497 //# Code to check & open if multiple files.
498 std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
500 //# We no longer need the file dialog object - delete it
501 delete openDialogInstance;
502 openDialogInstance = NULL;
504 //# Iterate through filenames if more than 1
505 if (flist.size() > 1)
506 {
507 for (unsigned int i = 0; i < flist.size(); i++)
508 {
509 fileName = flist[i];
511 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
512 if ( newFileName.size() > 0 )
513 fileName = newFileName;
514 else
515 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
517 #ifdef INK_DUMP_FILENAME_CONV
518 g_message("Opening File %s\n", fileName.c_str());
519 #endif
520 sp_file_open(fileName, selection);
521 }
523 return;
524 }
527 if (!fileName.empty())
528 {
529 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
531 if ( newFileName.size() > 0)
532 fileName = newFileName;
533 else
534 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
536 open_path = Glib::path_get_dirname (fileName);
537 open_path.append(G_DIR_SEPARATOR_S);
538 prefs->setString("/dialogs/open/path", open_path);
540 sp_file_open(fileName, selection);
541 }
543 return;
544 }
547 /*######################
548 ## V A C U U M
549 ######################*/
551 /**
552 * Remove unreferenced defs from the defs section of the document.
553 */
556 void
557 sp_file_vacuum()
558 {
559 SPDocument *doc = SP_ACTIVE_DOCUMENT;
561 unsigned int diff = vacuum_document (doc);
563 sp_document_done(doc, SP_VERB_FILE_VACUUM,
564 _("Vacuum <defs>"));
566 SPDesktop *dt = SP_ACTIVE_DESKTOP;
567 if (diff > 0) {
568 dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
569 ngettext("Removed <b>%i</b> unused definition in <defs>.",
570 "Removed <b>%i</b> unused definitions in <defs>.",
571 diff),
572 diff);
573 } else {
574 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
575 }
576 }
580 /*######################
581 ## S A V E
582 ######################*/
584 /**
585 * This 'save' function called by the others below
586 *
587 * \param official whether to set :output_module and :modified in the
588 * document; is true for normal save, false for temporary saves
589 */
590 static bool
591 file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
592 Inkscape::Extension::Extension *key, bool checkoverwrite, bool official,
593 Inkscape::Extension::FileSaveMethod save_method)
594 {
595 if (!doc || uri.size()<1) //Safety check
596 return false;
598 try {
599 Inkscape::Extension::save(key, doc, uri.c_str(),
600 false,
601 checkoverwrite, official,
602 save_method);
603 } catch (Inkscape::Extension::Output::no_extension_found &e) {
604 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
605 gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
606 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
607 sp_ui_error_dialog(text);
608 g_free(text);
609 g_free(safeUri);
610 return FALSE;
611 } catch (Inkscape::Extension::Output::file_read_only &e) {
612 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
613 gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
614 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
615 sp_ui_error_dialog(text);
616 g_free(text);
617 g_free(safeUri);
618 return FALSE;
619 } catch (Inkscape::Extension::Output::save_failed &e) {
620 gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
621 gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
622 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
623 sp_ui_error_dialog(text);
624 g_free(text);
625 g_free(safeUri);
626 return FALSE;
627 } catch (Inkscape::Extension::Output::save_cancelled &e) {
628 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
629 return FALSE;
630 } catch (Inkscape::Extension::Output::no_overwrite &e) {
631 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
632 } catch (...) {
633 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
634 return FALSE;
635 }
637 SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
638 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
639 return true;
640 }
642 /*
643 * Used only for remote saving using VFS and a specific uri. Gets the file at the /tmp.
644 */
645 bool
646 file_save_remote(SPDocument */*doc*/,
647 #ifdef WITH_GNOME_VFS
648 const Glib::ustring &uri,
649 #else
650 const Glib::ustring &/*uri*/,
651 #endif
652 Inkscape::Extension::Extension */*key*/, bool /*saveas*/, bool /*official*/)
653 {
654 #ifdef WITH_GNOME_VFS
656 #define BUF_SIZE 8192
657 gnome_vfs_init();
659 GnomeVFSHandle *from_handle = NULL;
660 GnomeVFSHandle *to_handle = NULL;
661 GnomeVFSFileSize bytes_read;
662 GnomeVFSFileSize bytes_written;
663 GnomeVFSResult result;
664 guint8 buffer[8192];
666 gchar* uri_local = g_filename_from_utf8( uri.c_str(), -1, NULL, NULL, NULL);
668 if ( uri_local == NULL ) {
669 g_warning( "Error converting filename to locale encoding.");
670 }
672 // Gets the temp file name.
673 Glib::ustring fileName = Glib::get_tmp_dir ();
674 fileName.append(G_DIR_SEPARATOR_S);
675 fileName.append((gnome_vfs_uri_extract_short_name(gnome_vfs_uri_new(uri_local))));
677 // Open the temp file to send.
678 result = gnome_vfs_open (&from_handle, fileName.c_str(), GNOME_VFS_OPEN_READ);
680 if (result != GNOME_VFS_OK) {
681 g_warning("Could not find the temp saving.");
682 return false;
683 }
685 result = gnome_vfs_create (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
686 result = gnome_vfs_open (&to_handle, uri_local, GNOME_VFS_OPEN_WRITE);
688 if (result != GNOME_VFS_OK) {
689 g_warning("file creating: %s", gnome_vfs_result_to_string(result));
690 return false;
691 }
693 while (1) {
695 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
697 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
698 result = gnome_vfs_close (from_handle);
699 result = gnome_vfs_close (to_handle);
700 return true;
701 }
703 if (result != GNOME_VFS_OK) {
704 g_warning("%s", gnome_vfs_result_to_string(result));
705 return false;
706 }
707 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
708 if (result != GNOME_VFS_OK) {
709 g_warning("%s", gnome_vfs_result_to_string(result));
710 return false;
711 }
714 if (bytes_read != bytes_written){
715 return false;
716 }
718 }
719 return true;
720 #else
721 // in case we do not have GNOME_VFS
722 return false;
723 #endif
725 }
728 /**
729 * Display a SaveAs dialog. Save the document if OK pressed.
730 */
731 bool
732 sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
733 {
734 Inkscape::Extension::Output *extension = 0;
735 bool is_copy = (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
737 // Note: default_extension has the format "org.inkscape.output.svg.inkscape", whereas
738 // filename_extension only uses ".svg"
739 Glib::ustring default_extension;
740 Glib::ustring filename_extension = ".svg";
742 default_extension= Inkscape::Extension::get_file_save_extension(save_method);
743 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
745 extension = dynamic_cast<Inkscape::Extension::Output *>
746 (Inkscape::Extension::db.get(default_extension.c_str()));
748 if (extension)
749 filename_extension = extension->get_extension();
751 Glib::ustring save_path;
752 Glib::ustring save_loc;
754 save_path = Inkscape::Extension::get_file_save_path(doc, save_method);
756 if (!Inkscape::IO::file_test(save_path.c_str(),
757 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
758 save_path = "";
760 if (save_path.size()<1)
761 save_path = g_get_home_dir();
763 save_loc = save_path;
764 save_loc.append(G_DIR_SEPARATOR_S);
766 char formatBuf[256];
767 int i = 1;
768 if (!doc->uri) {
769 // We are saving for the first time; create a unique default filename
770 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
771 save_loc.append(formatBuf);
773 while (Inkscape::IO::file_test(save_loc.c_str(), G_FILE_TEST_EXISTS)) {
774 save_loc = save_path;
775 save_loc.append(G_DIR_SEPARATOR_S);
776 snprintf(formatBuf, 255, _("drawing-%d%s"), i++, filename_extension.c_str());
777 save_loc.append(formatBuf);
778 }
779 } else {
780 snprintf(formatBuf, 255, _("%s"), Glib::path_get_basename(doc->uri).c_str());
781 save_loc.append(formatBuf);
782 }
784 // convert save_loc from utf-8 to locale
785 // is this needed any more, now that everything is handled in
786 // Inkscape::IO?
787 Glib::ustring save_loc_local = Glib::filename_from_utf8(save_loc);
789 if ( save_loc_local.size() > 0)
790 save_loc = save_loc_local;
792 //# Show the SaveAs dialog
793 char const * dialog_title;
794 if (is_copy) {
795 dialog_title = (char const *) _("Select file to save a copy to");
796 } else {
797 dialog_title = (char const *) _("Select file to save to");
798 }
799 gchar* doc_title = doc->root->title();
800 Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
801 Inkscape::UI::Dialog::FileSaveDialog::create(
802 parentWindow,
803 save_loc,
804 Inkscape::UI::Dialog::SVG_TYPES,
805 dialog_title,
806 default_extension,
807 doc_title ? doc_title : "",
808 save_method
809 );
811 saveDialog->setSelectionType(extension);
813 bool success = saveDialog->show();
814 if (!success) {
815 delete saveDialog;
816 return success;
817 }
819 // set new title here (call RDF to ensure metadata and title element are updated)
820 rdf_set_work_entity(doc, rdf_find_entity("title"), saveDialog->getDocTitle().c_str());
821 // free up old string
822 if(doc_title) g_free(doc_title);
824 Glib::ustring fileName = saveDialog->getFilename();
825 Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
827 delete saveDialog;
829 saveDialog = 0;
831 if (fileName.size() > 0) {
832 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
834 if ( newFileName.size()>0 )
835 fileName = newFileName;
836 else
837 g_warning( "Error converting save filename to UTF-8." );
839 // FIXME: does the argument !is_copy really convey the correct meaning here?
840 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy, save_method);
842 if (success && SP_DOCUMENT_URI(doc)) {
843 sp_file_add_recent(SP_DOCUMENT_URI(doc));
844 }
846 save_path = Glib::path_get_dirname(fileName);
847 Inkscape::Extension::store_save_path_in_prefs(save_path, save_method);
849 return success;
850 }
853 return false;
854 }
857 /**
858 * Save a document, displaying a SaveAs dialog if necessary.
859 */
860 bool
861 sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
862 {
863 bool success = true;
865 if (doc->isModifiedSinceSave()) {
866 if ( doc->uri == NULL )
867 {
868 // Hier sollte in Argument mitgegeben werden, das anzeigt, da� das Dokument das erste
869 // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
870 // benutzte "Save as ..."-Endung
871 return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
872 } else {
873 Glib::ustring extension = Inkscape::Extension::get_file_save_extension(Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
874 Glib::ustring fn = g_strdup(doc->uri);
875 // Try to determine the extension from the uri; this may not lead to a valid extension,
876 // but this case is caught in the file_save method below (or rather in Extension::save()
877 // further down the line).
878 Glib::ustring ext = "";
879 Glib::ustring::size_type pos = fn.rfind('.');
880 if (pos != Glib::ustring::npos) {
881 // FIXME: this could/should be more sophisticated (see FileSaveDialog::appendExtension()),
882 // but hopefully it's a reasonable workaround for now
883 ext = fn.substr( pos );
884 }
885 success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
886 }
887 } else {
888 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
889 success = TRUE;
890 }
892 return success;
893 }
896 /**
897 * Save a document.
898 */
899 bool
900 sp_file_save(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
901 {
902 if (!SP_ACTIVE_DOCUMENT)
903 return false;
905 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
907 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
908 return sp_file_save_document(parentWindow, SP_ACTIVE_DOCUMENT);
909 }
912 /**
913 * Save a document, always displaying the SaveAs dialog.
914 */
915 bool
916 sp_file_save_as(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
917 {
918 if (!SP_ACTIVE_DOCUMENT)
919 return false;
920 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
921 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
922 }
926 /**
927 * Save a copy of a document, always displaying a sort of SaveAs dialog.
928 */
929 bool
930 sp_file_save_a_copy(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
931 {
932 if (!SP_ACTIVE_DOCUMENT)
933 return false;
934 sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
935 return sp_file_save_dialog(parentWindow, SP_ACTIVE_DOCUMENT, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY);
936 }
939 /*######################
940 ## I M P O R T
941 ######################*/
943 /**
944 * Import a resource. Called by sp_file_import()
945 */
946 void
947 file_import(SPDocument *in_doc, const Glib::ustring &uri,
948 Inkscape::Extension::Extension *key)
949 {
950 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
952 //DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
953 SPDocument *doc;
954 try {
955 doc = Inkscape::Extension::open(key, uri.c_str());
956 } catch (Inkscape::Extension::Input::no_extension_found &e) {
957 doc = NULL;
958 } catch (Inkscape::Extension::Input::open_failed &e) {
959 doc = NULL;
960 }
962 if (doc != NULL) {
963 Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
964 Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
966 prevent_id_clashes(doc, in_doc);
968 SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
969 Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
971 SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
973 // Count the number of top-level items in the imported document.
974 guint items_count = 0;
975 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
976 child != NULL; child = SP_OBJECT_NEXT(child))
977 {
978 if (SP_IS_ITEM(child)) items_count++;
979 }
981 // Create a new group if necessary.
982 Inkscape::XML::Node *newgroup = NULL;
983 if ((style && style->firstChild()) || items_count > 1) {
984 newgroup = xml_in_doc->createElement("svg:g");
985 sp_repr_css_set(newgroup, style, "style");
986 }
988 // Determine the place to insert the new object.
989 // This will be the current layer, if possible.
990 // FIXME: If there's no desktop (command line run?) we need
991 // a document:: method to return the current layer.
992 // For now, we just use the root in this case.
993 SPObject *place_to_insert;
994 if (desktop) {
995 place_to_insert = desktop->currentLayer();
996 } else {
997 place_to_insert = SP_DOCUMENT_ROOT(in_doc);
998 }
1000 // Construct a new object representing the imported image,
1001 // and insert it into the current document.
1002 SPObject *new_obj = NULL;
1003 for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
1004 child != NULL; child = SP_OBJECT_NEXT(child) )
1005 {
1006 if (SP_IS_ITEM(child)) {
1007 Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_in_doc);
1009 // convert layers to groups, and make sure they are unlocked
1010 // FIXME: add "preserve layers" mode where each layer from
1011 // import is copied to the same-named layer in host
1012 newitem->setAttribute("inkscape:groupmode", NULL);
1013 newitem->setAttribute("sodipodi:insensitive", NULL);
1015 if (newgroup) newgroup->appendChild(newitem);
1016 else new_obj = place_to_insert->appendChildRepr(newitem);
1017 }
1019 // don't lose top-level defs or style elements
1020 else if (SP_OBJECT_REPR(child)->type() == Inkscape::XML::ELEMENT_NODE) {
1021 const gchar *tag = SP_OBJECT_REPR(child)->name();
1022 if (!strcmp(tag, "svg:defs")) {
1023 for (SPObject *x = sp_object_first_child(child);
1024 x != NULL; x = SP_OBJECT_NEXT(x))
1025 {
1026 SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(x)->duplicate(xml_in_doc), last_def);
1027 }
1028 }
1029 else if (!strcmp(tag, "svg:style")) {
1030 SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
1031 }
1032 }
1033 }
1034 if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
1036 // release some stuff
1037 if (newgroup) Inkscape::GC::release(newgroup);
1038 if (style) sp_repr_css_attr_unref(style);
1040 // select and move the imported item
1041 if (new_obj && SP_IS_ITEM(new_obj)) {
1042 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1043 selection->set(SP_ITEM(new_obj));
1045 // preserve parent and viewBox transformations
1046 // c2p is identity matrix at this point unless sp_document_ensure_up_to_date is called
1047 sp_document_ensure_up_to_date(doc);
1048 Geom::Matrix affine = SP_ROOT(SP_DOCUMENT_ROOT(doc))->c2p * sp_item_i2doc_affine(SP_ITEM(place_to_insert)).inverse();
1049 sp_selection_apply_affine(selection, desktop->dt2doc() * affine * desktop->doc2dt(), true, false);
1051 // move to mouse pointer
1052 {
1053 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
1054 Geom::OptRect sel_bbox = selection->bounds();
1055 if (sel_bbox) {
1056 Geom::Point m( desktop->point() - sel_bbox->midpoint() );
1057 sp_selection_move_relative(selection, m, false);
1058 }
1059 }
1060 }
1062 sp_document_unref(doc);
1063 sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
1064 _("Import"));
1066 } else {
1067 gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
1068 sp_ui_error_dialog(text);
1069 g_free(text);
1070 }
1072 return;
1073 }
1076 /**
1077 * Display an Open dialog, import a resource if OK pressed.
1078 */
1079 void
1080 sp_file_import(Gtk::Window &parentWindow)
1081 {
1082 static Glib::ustring import_path;
1084 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1085 if (!doc)
1086 return;
1088 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1090 if(import_path.empty())
1091 {
1092 Glib::ustring attr = prefs->getString("/dialogs/import/path");
1093 if (!attr.empty()) import_path = attr;
1094 }
1096 //# Test if the import_path directory exists
1097 if (!Inkscape::IO::file_test(import_path.c_str(),
1098 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1099 import_path = "";
1101 //# If no open path, default to our home directory
1102 if (import_path.empty())
1103 {
1104 import_path = g_get_home_dir();
1105 import_path.append(G_DIR_SEPARATOR_S);
1106 }
1108 // Create new dialog (don't use an old one, because parentWindow has probably changed)
1109 Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
1110 Inkscape::UI::Dialog::FileOpenDialog::create(
1111 parentWindow,
1112 import_path,
1113 Inkscape::UI::Dialog::IMPORT_TYPES,
1114 (char const *)_("Select file to import"));
1116 bool success = importDialogInstance->show();
1117 if (!success) {
1118 delete importDialogInstance;
1119 return;
1120 }
1122 //# Get file name and extension type
1123 Glib::ustring fileName = importDialogInstance->getFilename();
1124 Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1126 delete importDialogInstance;
1127 importDialogInstance = NULL;
1129 if (fileName.size() > 0) {
1131 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1133 if ( newFileName.size() > 0)
1134 fileName = newFileName;
1135 else
1136 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1138 import_path = Glib::path_get_dirname (fileName);
1139 import_path.append(G_DIR_SEPARATOR_S);
1140 prefs->setString("/dialogs/import/path", import_path);
1142 file_import(doc, fileName, selection);
1143 }
1145 return;
1146 }
1150 /*######################
1151 ## E X P O R T
1152 ######################*/
1155 #ifdef NEW_EXPORT_DIALOG
1157 /**
1158 * Display an Export dialog, export as the selected type if OK pressed
1159 */
1160 bool
1161 sp_file_export_dialog(Gtk::Window &parentWindow)
1162 {
1163 //# temp hack for 'doc' until we can switch to this dialog
1164 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1166 Glib::ustring export_path;
1167 Glib::ustring export_loc;
1169 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1170 Inkscape::Extension::Output *extension;
1172 //# Get the default extension name
1173 Glib::ustring default_extension = prefs->getString("/dialogs/save_export/default");
1174 if(default_extension.empty()) {
1175 // FIXME: Is this a good default? Should there be a macro for the string?
1176 default_extension = "org.inkscape.output.png.cairo";
1177 }
1178 //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1180 if (doc->uri == NULL)
1181 {
1182 char formatBuf[256];
1184 Glib::ustring filename_extension = ".svg";
1185 extension = dynamic_cast<Inkscape::Extension::Output *>
1186 (Inkscape::Extension::db.get(default_extension.c_str()));
1187 //g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
1188 if (extension)
1189 filename_extension = extension->get_extension();
1191 Glib::ustring attr3 = prefs->getString("/dialogs/save_export/path");
1192 if (!attr3.empty())
1193 export_path = attr3;
1195 if (!Inkscape::IO::file_test(export_path.c_str(),
1196 (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1197 export_path = "";
1199 if (export_path.size()<1)
1200 export_path = g_get_home_dir();
1202 export_loc = export_path;
1203 export_loc.append(G_DIR_SEPARATOR_S);
1204 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1205 export_loc.append(formatBuf);
1207 }
1208 else
1209 {
1210 export_path = Glib::path_get_dirname(doc->uri);
1211 }
1213 // convert save_loc from utf-8 to locale
1214 // is this needed any more, now that everything is handled in
1215 // Inkscape::IO?
1216 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1217 if ( export_path_local.size() > 0)
1218 export_path = export_path_local;
1220 //# Show the Export dialog
1221 Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
1222 Inkscape::UI::Dialog::FileExportDialog::create(
1223 parentWindow,
1224 export_path,
1225 Inkscape::UI::Dialog::EXPORT_TYPES,
1226 (char const *) _("Select file to export to"),
1227 default_extension
1228 );
1230 bool success = exportDialogInstance->show();
1231 if (!success) {
1232 delete exportDialogInstance;
1233 return success;
1234 }
1236 Glib::ustring fileName = exportDialogInstance->getFilename();
1238 Inkscape::Extension::Extension *selectionType =
1239 exportDialogInstance->getSelectionType();
1241 delete exportDialogInstance;
1242 exportDialogInstance = NULL;
1244 if (fileName.size() > 0) {
1245 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1247 if ( newFileName.size()>0 )
1248 fileName = newFileName;
1249 else
1250 g_warning( "Error converting save filename to UTF-8." );
1252 success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1254 if (success) {
1255 Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
1256 recent->add_item(SP_DOCUMENT_URI(doc));
1257 }
1259 export_path = fileName;
1260 prefs->setString("/dialogs/save_export/path", export_path);
1262 return success;
1263 }
1266 return false;
1267 }
1269 #else
1271 /**
1272 *
1273 */
1274 bool
1275 sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
1276 {
1277 sp_export_dialog();
1278 return true;
1279 }
1281 #endif
1283 /*######################
1284 ## E X P O R T T O O C A L
1285 ######################*/
1287 /**
1288 * Display an Export dialog, export as the selected type if OK pressed
1289 */
1290 /*
1291 bool
1292 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
1293 {
1295 if (!SP_ACTIVE_DOCUMENT)
1296 return false;
1298 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1300 Glib::ustring export_path;
1301 Glib::ustring export_loc;
1302 Glib::ustring fileName;
1303 Inkscape::Extension::Extension *selectionType;
1305 bool success = false;
1307 static bool gotSuccess = false;
1309 Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1310 (void)repr;
1312 if (!doc->uri && !doc->isModifiedSinceSave())
1313 return false;
1315 // Get the default extension name
1316 Glib::ustring default_extension = "org.inkscape.output.svg.inkscape";
1317 char formatBuf[256];
1319 Glib::ustring filename_extension = ".svg";
1320 selectionType = Inkscape::Extension::db.get(default_extension.c_str());
1322 export_path = Glib::get_tmp_dir ();
1324 export_loc = export_path;
1325 export_loc.append(G_DIR_SEPARATOR_S);
1326 snprintf(formatBuf, 255, _("drawing%s"), filename_extension.c_str());
1327 export_loc.append(formatBuf);
1329 // convert save_loc from utf-8 to locale
1330 // is this needed any more, now that everything is handled in
1331 // Inkscape::IO?
1332 Glib::ustring export_path_local = Glib::filename_from_utf8(export_path);
1333 if ( export_path_local.size() > 0)
1334 export_path = export_path_local;
1336 // Show the Export To OCAL dialog
1337 Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
1338 new Inkscape::UI::Dialog::FileExportToOCALDialog(
1339 parentWindow,
1340 Inkscape::UI::Dialog::EXPORT_TYPES,
1341 (char const *) _("Select file to export to")
1342 );
1344 success = exportDialogInstance->show();
1345 if (!success) {
1346 delete exportDialogInstance;
1347 return success;
1348 }
1350 fileName = exportDialogInstance->getFilename();
1352 delete exportDialogInstance;
1353 exportDialogInstance = NULL;;
1355 fileName.append(filename_extension.c_str());
1356 if (fileName.size() > 0) {
1357 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1359 if ( newFileName.size()>0 )
1360 fileName = newFileName;
1361 else
1362 g_warning( "Error converting save filename to UTF-8." );
1363 }
1364 Glib::ustring filePath = export_path;
1365 filePath.append(G_DIR_SEPARATOR_S);
1366 filePath.append(Glib::path_get_basename(fileName));
1368 fileName = filePath;
1370 success = file_save(parentWindow, doc, filePath, selectionType, FALSE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
1372 if (!success){
1373 gchar *text = g_strdup_printf(_("Error saving a temporary copy"));
1374 sp_ui_error_dialog(text);
1376 return success;
1377 }
1379 // Start now the submition
1381 // Create the uri
1382 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1383 Glib::ustring uri = "dav://";
1384 Glib::ustring username = prefs->getString("/options/ocalusername/str");
1385 Glib::ustring password = prefs->getString("/options/ocalpassword/str");
1386 if (username.empty() || password.empty())
1387 {
1388 Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1389 if(!gotSuccess)
1390 {
1391 exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1392 parentWindow,
1393 (char const *) _("Open Clip Art Login"));
1394 success = exportPasswordDialogInstance->show();
1395 if (!success) {
1396 delete exportPasswordDialogInstance;
1397 return success;
1398 }
1399 }
1400 username = exportPasswordDialogInstance->getUsername();
1401 password = exportPasswordDialogInstance->getPassword();
1403 delete exportPasswordDialogInstance;
1404 exportPasswordDialogInstance = NULL;
1405 }
1406 uri.append(username);
1407 uri.append(":");
1408 uri.append(password);
1409 uri.append("@");
1410 uri.append(prefs->getString("/options/ocalurl/str"));
1411 uri.append("/dav.php/");
1412 uri.append(Glib::path_get_basename(fileName));
1414 // Save as a remote file using the dav protocol.
1415 success = file_save_remote(doc, uri, selectionType, FALSE, FALSE);
1416 remove(fileName.c_str());
1417 if (!success)
1418 {
1419 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."));
1420 sp_ui_error_dialog(text);
1421 }
1422 else
1423 gotSuccess = true;
1425 return success;
1426 }
1427 */
1428 /**
1429 * Export the current document to OCAL
1430 */
1431 /*
1432 void
1433 sp_file_export_to_ocal(Gtk::Window &parentWindow)
1434 {
1436 // Try to execute the new code and return;
1437 if (!SP_ACTIVE_DOCUMENT)
1438 return;
1439 bool success = sp_file_export_to_ocal_dialog(parentWindow);
1440 if (success)
1441 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
1442 }
1443 */
1445 /*######################
1446 ## I M P O R T F R O M O C A L
1447 ######################*/
1449 /**
1450 * Display an ImportToOcal Dialog, and the selected document from OCAL
1451 */
1452 void
1453 sp_file_import_from_ocal(Gtk::Window &parentWindow)
1454 {
1455 static Glib::ustring import_path;
1457 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1458 if (!doc)
1459 return;
1461 Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1463 if (!importDialogInstance) {
1464 importDialogInstance = new
1465 Inkscape::UI::Dialog::FileImportFromOCALDialog(
1466 parentWindow,
1467 import_path,
1468 Inkscape::UI::Dialog::IMPORT_TYPES,
1469 (char const *)_("Import From Open Clip Art Library"));
1470 }
1472 bool success = importDialogInstance->show();
1473 if (!success) {
1474 delete importDialogInstance;
1475 return;
1476 }
1478 // Get file name and extension type
1479 Glib::ustring fileName = importDialogInstance->getFilename();
1480 Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1482 delete importDialogInstance;
1483 importDialogInstance = NULL;
1485 if (fileName.size() > 0) {
1487 Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1489 if ( newFileName.size() > 0)
1490 fileName = newFileName;
1491 else
1492 g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
1494 import_path = fileName;
1495 if (import_path.size()>0)
1496 import_path.append(G_DIR_SEPARATOR_S);
1498 file_import(doc, fileName, selection);
1499 }
1501 return;
1502 }
1504 /*######################
1505 ## P R I N T
1506 ######################*/
1509 /**
1510 * Print the current document, if any.
1511 */
1512 void
1513 sp_file_print(Gtk::Window& parentWindow)
1514 {
1515 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1516 if (doc)
1517 sp_print_document(parentWindow, doc);
1518 }
1520 /**
1521 * Display what the drawing would look like, if
1522 * printed.
1523 */
1524 void
1525 sp_file_print_preview(gpointer /*object*/, gpointer /*data*/)
1526 {
1528 SPDocument *doc = SP_ACTIVE_DOCUMENT;
1529 if (doc)
1530 sp_print_preview_document(doc);
1532 }
1535 /*
1536 Local Variables:
1537 mode:c++
1538 c-file-style:"stroustrup"
1539 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1540 indent-tabs-mode:nil
1541 fill-column:99
1542 End:
1543 */
1544 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :