4
* File/Print operations
7
* Lauris Kaplinski <lauris@kaplinski.com>
8
* Chema Celorio <chema@celorio.com>
9
* bulia byak <buliabyak@users.sf.net>
11
* Copyright (C) 1999-2005 Authors
12
* Copyright (C) 2001-2002 Ximian, Inc.
13
* Copyright (C) 2004 David Turner
15
* Released under GNU GPL, read the file 'COPYING' for more information
19
* Note: This file needs to be cleaned up extensively.
20
* What it probably needs is to have one .h file for
21
* the API, and two or more .cpp files for the implementations.
28
#include <libnr/nr-pixops.h>
30
#include "document-private.h"
31
#include "selection-chemistry.h"
32
#include "ui/view/view-widget.h"
34
#include "helper/png-write.h"
35
#include "dialogs/export.h"
36
#include <glibmm/i18n.h>
39
#include "selection.h"
40
#include "interface.h"
44
#include "message-stack.h"
45
#include "dialogs/filedialog.h"
46
#include "prefs-utils.h"
47
#include "path-prefix.h"
49
#include "sp-namedview.h"
50
#include "desktop-handles.h"
52
#include "extension/db.h"
53
#include "extension/input.h"
54
#include "extension/output.h"
55
/* #include "extension/menu.h" */
56
#include "extension/system.h"
59
#include "application/application.h"
60
#include "application/editor.h"
65
#include "jabber_whiteboard/session-manager.h"
69
* 'Current' paths. Used to remember which directory
70
* had the last file accessed.
71
* Static globals are evil. This will be gone soon
72
* as C++ification continues
74
static gchar *import_path = NULL;
76
//#define INK_DUMP_FILENAME_CONV 1
77
#undef INK_DUMP_FILENAME_CONV
79
//#define INK_DUMP_FOPEN 1
82
void dump_str(gchar const *str, gchar const *prefix);
83
void dump_ustr(Glib::ustring const &ustr);
86
/*######################
88
######################*/
91
* Create a blank document and add it to the desktop
94
sp_file_new(gchar const *templ)
96
SPDocument *doc = sp_document_new(templ, TRUE, true);
97
g_return_val_if_fail(doc != NULL, NULL);
100
if (Inkscape::NSApplication::Application::getNewGui())
102
dt = Inkscape::NSApplication::Editor::createDesktop (doc);
104
SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
105
g_return_val_if_fail(dtw != NULL, NULL);
106
sp_document_unref(doc);
108
sp_create_window(dtw, TRUE);
109
dt = static_cast<SPDesktop*>(dtw->view);
110
sp_namedview_window_from_document(dt);
116
sp_file_new_default()
118
std::list<gchar *> sources;
119
sources.push_back( profile_path("templates") ); // first try user's local dir
120
sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
122
while (!sources.empty()) {
123
gchar *dirname = sources.front();
124
if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
126
// TRANSLATORS: default.svg is localizable - this is the name of the default document
127
// template. This way you can localize the default pagesize, translate the name of
128
// the default layer, etc. If you wish to localize this file, please create a
129
// localized share/templates/default.xx.svg file, where xx is your language code.
130
char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
131
if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
132
return sp_file_new(default_template);
139
return sp_file_new(NULL);
143
/*######################
145
######################*/
148
* Perform document closures preceding an exit()
154
// no need to call inkscape_exit here; last document being closed will take care of that
158
/*######################
160
######################*/
163
* Open a file, add the document to the desktop
165
* \param replace_empty if true, and the current desktop is empty, this document
166
* will replace the empty one.
169
sp_file_open(gchar const *uri, Inkscape::Extension::Extension *key, bool add_to_recent, bool replace_empty)
173
doc = Inkscape::Extension::open(key, uri);
174
} catch (Inkscape::Extension::Input::no_extension_found &e) {
176
} catch (Inkscape::Extension::Input::open_failed &e) {
181
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
182
SPDocument *existing = desktop ? SP_DT_DOCUMENT(desktop) : NULL;
184
if (existing && existing->virgin && replace_empty) {
185
// If the current desktop is empty, open the document there
186
desktop->change_document(doc);
188
if (!Inkscape::NSApplication::Application::getNewGui()) {
189
// create a whole new desktop and window
190
SPViewWidget *dtw = sp_desktop_widget_new(sp_document_namedview(doc, NULL));
191
sp_create_window(dtw, TRUE);
192
desktop = static_cast<SPDesktop*>(dtw->view);
194
desktop = Inkscape::NSApplication::Editor::createDesktop (doc);
199
// everyone who cares now has a reference, get rid of ours
200
sp_document_unref(doc);
201
// resize the window to match the document properties
202
// (this may be redundant for new windows... if so, move to the "virgin"
205
desktop->whiteboard_session_manager()->setDesktop(desktop);
207
sp_namedview_window_from_document(desktop);
210
prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
215
gchar *safeUri = Inkscape::IO::sanitizeString(uri);
216
gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), safeUri);
217
sp_ui_error_dialog(text);
225
* Handle prompting user for "do you want to revert"? Revert on "OK"
228
sp_file_revert_dialog()
230
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
231
g_assert(desktop != NULL);
233
SPDocument *doc = SP_DT_DOCUMENT(desktop);
234
g_assert(doc != NULL);
236
Inkscape::XML::Node *repr = sp_document_repr_root(doc);
237
g_assert(repr != NULL);
239
gchar const *uri = doc->uri;
241
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved yet. Cannot revert."));
245
bool do_revert = true;
246
if (repr->attribute("sodipodi:modified") != NULL) {
247
gchar *text = g_strdup_printf(_("Changes will be lost! Are you sure you want to reload document %s?"), uri);
249
bool response = desktop->warnDialog (text);
259
// Allow overwriting of current document.
261
reverted = sp_file_open(uri,NULL);
267
desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document reverted."));
269
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not reverted."));
273
void dump_str(gchar const *str, gchar const *prefix)
278
size_t const total = strlen(str);
279
for (unsigned i = 0; i < total; i++) {
280
gchar *const tmp2 = g_strdup_printf(" %02x", (0x0ff & str[i]));
286
g_message(tmp.c_str());
289
void dump_ustr(Glib::ustring const &ustr)
291
char const *cstr = ustr.c_str();
292
char const *data = ustr.data();
293
Glib::ustring::size_type const byteLen = ustr.bytes();
294
Glib::ustring::size_type const dataLen = ustr.length();
295
Glib::ustring::size_type const cstrLen = strlen(cstr);
297
g_message(" size: %lu\n length: %lu\n bytes: %lu\n clen: %lu",
298
gulong(ustr.size()), gulong(dataLen), gulong(byteLen), gulong(cstrLen) );
299
g_message( " ASCII? %s", (ustr.is_ascii() ? "yes":"no") );
300
g_message( " UTF-8? %s", (ustr.validate() ? "yes":"no") );
304
for (Glib::ustring::size_type i = 0; i < ustr.bytes(); i++) {
307
Glib::ustring::value_type val = ustr.at(i);
308
gchar* tmp2 = g_strdup_printf( (((val & 0xff00) == 0) ? " %02x" : "%04x"), val );
316
int val = (0x0ff & data[i]);
317
gchar *tmp2 = g_strdup_printf(" %02x", val);
320
if ( val > 32 && val < 127 ) {
321
tmp2 = g_strdup_printf( " '%c'", (gchar)val );
332
int val = (0x0ff & cstr[i]);
333
gchar* tmp2 = g_strdup_printf(" %02x", val);
336
if ( val > 32 && val < 127 ) {
337
tmp2 = g_strdup_printf(" '%c'", (gchar) val);
347
g_message( tmp.c_str() );
350
g_message("XXXXXXXXXXXXXXXXXX Exception" );
352
g_message("---------------");
355
static Inkscape::UI::Dialogs::FileOpenDialog *openDialogInstance = NULL;
358
* Display an file Open selector. Open a document if OK is pressed.
359
* Can select single or multiple files for opening.
362
sp_file_open_dialog(gpointer object, gpointer data)
364
gchar *open_path2 = NULL;
366
gchar *open_path = g_strdup(prefs_get_string_attribute("dialogs.open", "path"));
367
if (open_path != NULL && open_path[0] == '\0') {
371
if (open_path && !Inkscape::IO::file_test(open_path, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
375
if (open_path == NULL)
376
open_path = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, NULL);
378
if (!openDialogInstance) {
380
Inkscape::UI::Dialogs::FileOpenDialog::create(
381
(char const *)open_path,
382
Inkscape::UI::Dialogs::SVG_TYPES,
383
(char const *)_("Select file to open"));
385
bool const success = openDialogInstance->show();
386
gchar *fileName = ( success
387
? g_strdup(openDialogInstance->getFilename())
389
Inkscape::Extension::Extension *selection =
390
openDialogInstance->getSelectionType();
393
if (!success) return;
395
// Code to check & open iff multiple files.
396
Glib::SListHandle<Glib::ustring> flist=openDialogInstance->getFilenames();
397
GSList *list=flist.data();
399
if(g_slist_length(list)>1)
401
gchar *fileName=NULL;
406
#ifdef INK_DUMP_FILENAME_CONV
407
g_message(" FileName: %s",(const char *)list->data);
410
fileName=(gchar *)g_strdup((gchar *)list->data);
412
if (fileName && !g_file_test(fileName,G_FILE_TEST_IS_DIR)) {
414
gsize bytesWritten = 0;
415
GError *error = NULL;
416
#ifdef INK_DUMP_FILENAME_CONV
417
dump_str( fileName, "A file pre is " );
419
gchar *newFileName = g_filename_to_utf8(fileName,
424
if ( newFileName != NULL ) {
426
fileName = newFileName;
427
#ifdef INK_DUMP_FILENAME_CONV
428
dump_str( fileName, "A file post is " );
431
// TODO: bulia, please look over
432
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
435
#ifdef INK_DUMP_FILENAME_CONV
436
g_message("Opening File %s\n",fileName);
439
sp_file_open(fileName, selection);
444
g_message("Cannot Open Directory %s\n",fileName);
456
gsize bytesWritten = 0;
457
GError *error = NULL;
458
#ifdef INK_DUMP_FILENAME_CONV
459
dump_str( fileName, "A file pre is " );
461
gchar *newFileName = g_filename_to_utf8(fileName,
466
if ( newFileName != NULL ) {
468
fileName = newFileName;
469
#ifdef INK_DUMP_FILENAME_CONV
470
dump_str( fileName, "A file post is " );
473
// TODO: bulia, please look over
474
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
478
if ( !g_utf8_validate(fileName, -1, NULL) ) {
479
// TODO: bulia, please look over
480
g_warning( "INPUT FILENAME IS NOT UTF-8" );
484
open_path = g_dirname(fileName);
485
open_path2 = g_strconcat(open_path, G_DIR_SEPARATOR_S, NULL);
486
prefs_set_string_attribute("dialogs.open", "path", open_path2);
490
sp_file_open(fileName, selection);
498
/*######################
500
######################*/
503
* Remove unreferenced defs from the defs section of the document.
510
SPDocument *doc = SP_ACTIVE_DOCUMENT;
512
unsigned int diff = vacuum_document (doc);
514
sp_document_done(doc);
516
SPDesktop *dt = SP_ACTIVE_DESKTOP;
518
dt->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
519
ngettext("Removed <b>%i</b> unused definition in <defs>.",
520
"Removed <b>%i</b> unused definitions in <defs>.",
524
dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("No unused definitions in <defs>."));
530
/*######################
532
######################*/
535
* This 'save' function called by the others below
538
file_save(SPDocument *doc, gchar const *uri, Inkscape::Extension::Extension *key, bool saveas)
540
if (!doc || !uri) //Safety check
544
Inkscape::Extension::save(key, doc, uri,
545
saveas && prefs_get_int_attribute("dialogs.save_as", "append_extension", 1),
546
saveas, TRUE); // save officially, with inkscape: attributes set
547
} catch (Inkscape::Extension::Output::no_extension_found &e) {
548
gchar *safeUri = Inkscape::IO::sanitizeString(uri);
549
gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri);
550
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
551
sp_ui_error_dialog(text);
555
} catch (Inkscape::Extension::Output::save_failed &e) {
556
gchar *safeUri = Inkscape::IO::sanitizeString(uri);
557
gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
558
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
559
sp_ui_error_dialog(text);
563
} catch (Inkscape::Extension::Output::no_overwrite &e) {
564
return sp_file_save_dialog(doc);
567
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Document saved."));
571
static Inkscape::UI::Dialogs::FileSaveDialog *saveDialogInstance = NULL;
574
* Display a SaveAs dialog. Save the document if OK pressed.
577
sp_file_save_dialog(SPDocument *doc)
579
Inkscape::XML::Node *repr = sp_document_repr_root(doc);
580
gchar const *default_extension = NULL;
582
Inkscape::Extension::Output *extension;
583
gchar *save_path = NULL;
585
default_extension = repr->attribute("inkscape:output_extension");
586
if (default_extension == NULL) {
587
default_extension = prefs_get_string_attribute("dialogs.save_as", "default");
589
//g_warning("%s: extension name: '%s'", __FUNCTION__, default_extension);
591
if (doc->uri == NULL) {
593
char const *filename_extension;
596
extension = dynamic_cast<Inkscape::Extension::Output *>(Inkscape::Extension::db.get(default_extension));
597
//g_warning("%s: extension ptr: 0x%x", __FUNCTION__, (unsigned int)extension);
598
if (extension == NULL) {
599
filename_extension = ".svg";
601
filename_extension = extension->get_extension();
604
save_path = g_strdup(prefs_get_string_attribute("dialogs.save_as", "path"));
605
if (save_path != NULL && save_path[0] == '\0') {
609
if (save_path && !Inkscape::IO::file_test(save_path, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
613
if (save_path == NULL)
614
save_path = g_strdup(g_get_home_dir());
615
temp_filename = g_strdup_printf(_("drawing%s"), filename_extension);
616
save_loc = g_build_filename(save_path, temp_filename, NULL);
617
g_free(temp_filename);
619
while (Inkscape::IO::file_test(save_loc, G_FILE_TEST_EXISTS)) {
621
temp_filename = g_strdup_printf(_("drawing-%d%s"), i++, filename_extension);
622
save_loc = g_build_filename(save_path, temp_filename, NULL);
623
g_free(temp_filename);
626
save_loc = g_path_get_dirname(doc->uri); /* \todo should use a getter */
629
{ // convert save_loc from utf-8 to locale
630
// is this needed any more, now that everything is handled in
633
gsize bytesWritten = 0;
634
GError* error = NULL;
635
#ifdef INK_DUMP_FILENAME_CONV
636
dump_str( save_loc, "B file pre is " );
638
gchar* save_loc_local = g_filename_from_utf8( save_loc, -1, &bytesRead, &bytesWritten, &error);
640
if ( save_loc_local != NULL ) {
642
save_loc = save_loc_local;
643
#ifdef INK_DUMP_FILENAME_CONV
644
dump_str( save_loc, "B file post is " );
647
//g_warning( "Error converting save filename stored in the file to locale encoding.");
651
if (!saveDialogInstance) {
653
Inkscape::UI::Dialogs::FileSaveDialog::create(
654
(char const *) save_loc,
655
Inkscape::UI::Dialogs::SVG_TYPES,
656
(char const *) _("Select file to save to"),
659
} // FIXME: else (i.e. if reshowing an already shown dialog) save_loc is not used, it thus always displays the previously opened dir
660
bool success = saveDialogInstance->show();
661
char *fileName = ( success
662
? g_strdup(saveDialogInstance->getFilename())
664
Inkscape::Extension::Extension *selectionType =
665
saveDialogInstance->getSelectionType();
672
if (fileName && *fileName) {
674
gsize bytesWritten = 0;
675
GError *error = NULL;
676
#ifdef INK_DUMP_FILENAME_CONV
677
dump_str( fileName, "C file pre is " );
679
gchar *newFileName = g_filename_to_utf8(fileName,
684
if ( newFileName != NULL ) {
686
fileName = newFileName;
687
#ifdef INK_DUMP_FILENAME_CONV
688
dump_str( fileName, "C file post is " );
691
g_warning( "Error converting save filename to UTF-8." );
694
if (!g_utf8_validate(fileName, -1, NULL)) {
695
// TODO: bulia, please look over
696
g_warning( "The filename is not UTF-8." );
699
success = file_save(doc, fileName, selectionType, TRUE);
702
prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
705
save_path = g_dirname(fileName);
706
prefs_set_string_attribute("dialogs.save_as", "path", save_path);
718
* Save a document, displaying a SaveAs dialog if necessary.
721
sp_file_save_document(SPDocument *doc)
723
gboolean success = TRUE;
725
Inkscape::XML::Node *repr = sp_document_repr_root(doc);
727
gchar const *fn = repr->attribute("sodipodi:modified");
730
|| repr->attribute("inkscape:output_extension") == NULL)
732
return sp_file_save_dialog(doc);
734
fn = g_strdup(doc->uri);
735
gchar const *ext = repr->attribute("inkscape:output_extension");
736
success = file_save(doc, fn, Inkscape::Extension::db.get(ext), FALSE);
740
SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
752
sp_file_save(gpointer object, gpointer data)
754
if (!SP_ACTIVE_DOCUMENT)
756
sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
757
return sp_file_save_document(SP_ACTIVE_DOCUMENT);
762
* Save a document, always displaying the SaveAs dialog.
765
sp_file_save_as(gpointer object, gpointer data)
767
if (!SP_ACTIVE_DOCUMENT)
769
sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
770
return sp_file_save_dialog(SP_ACTIVE_DOCUMENT);
776
/*######################
778
######################*/
781
* Import a resource. Called by sp_file_import()
784
file_import(SPDocument *in_doc, gchar const *uri, Inkscape::Extension::Extension *key)
786
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
788
//DEBUG_MESSAGE( fileImport, "file_import( in_doc:%p uri:[%s], key:%p", in_doc, uri, key );
791
doc = Inkscape::Extension::open(key, uri);
792
} catch (Inkscape::Extension::Input::no_extension_found &e) {
794
} catch (Inkscape::Extension::Input::open_failed &e) {
799
// the import extension has passed us a document, now we need to embed it into our document
801
// const gchar *docbase = (sp_repr_document_root( sp_repr_document( repr ))->attribute("sodipodi:docbase" );
802
g_message(" settings uri [%s]", doc->uri );
803
g_message(" base [%s]", doc->base );
804
g_message(" name [%s]", doc->name );
805
Inkscape::IO::fixupHrefs( doc, doc->base, TRUE );
806
g_message(" mid-fixup");
807
Inkscape::IO::fixupHrefs( doc, in_doc->base, TRUE );
810
// move imported defs to our document's defs
811
SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
812
SPObject *defs = SP_DOCUMENT_DEFS(doc);
813
Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
814
for (SPObject *child = sp_object_first_child(defs);
815
child != NULL; child = SP_OBJECT_NEXT(child))
817
// FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
818
SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(), last_def);
821
guint items_count = 0;
822
for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
823
child != NULL; child = SP_OBJECT_NEXT(child)) {
824
if (SP_IS_ITEM(child))
827
SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
829
SPObject *new_obj = NULL;
831
if ((style && style->firstChild()) || items_count > 1) {
833
Inkscape::XML::Node *newgroup = sp_repr_new("svg:g");
834
sp_repr_css_set (newgroup, style, "style");
836
for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
837
if (SP_IS_ITEM(child)) {
838
Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate();
840
// convert layers to groups; FIXME: add "preserve layers" mode where each layer
841
// from impot is copied to the same-named layer in host
842
newchild->setAttribute("inkscape:groupmode", NULL);
844
newgroup->appendChild(newchild);
849
// Add it to the current layer
850
new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
852
// There's no desktop (command line run?)
853
// FIXME: For such cases we need a document:: method to return the current layer
854
new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
857
Inkscape::GC::release(newgroup);
860
for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
861
if (SP_IS_ITEM(child)) {
862
Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate();
863
newitem->setAttribute("inkscape:groupmode", NULL);
866
// Add it to the current layer
867
new_obj = desktop->currentLayer()->appendChildRepr(newitem);
869
// There's no desktop (command line run?)
870
// FIXME: For such cases we need a document:: method to return the current layer
871
new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
878
if (style) sp_repr_css_attr_unref (style);
880
// select and move the imported item
881
if (new_obj && SP_IS_ITEM(new_obj)) {
882
Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
883
selection->set(SP_ITEM(new_obj));
885
// To move the imported object, we must temporarily set the "transform pattern with
888
int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
889
prefs_set_int_attribute("options.transform", "pattern", 1);
890
sp_document_ensure_up_to_date(SP_DT_DOCUMENT(desktop));
891
NR::Point m( desktop->point() - selection->bounds().midpoint() );
892
sp_selection_move_relative(selection, m);
893
prefs_set_int_attribute("options.transform", "pattern", saved_pref);
897
sp_document_unref(doc);
898
sp_document_done(in_doc);
901
gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri);
902
sp_ui_error_dialog(text);
910
static Inkscape::UI::Dialogs::FileOpenDialog *importDialogInstance = NULL;
913
* Display an Open dialog, import a resource if OK pressed.
916
sp_file_import(GtkWidget *widget)
918
SPDocument *doc = SP_ACTIVE_DOCUMENT;
922
if (!importDialogInstance) {
923
importDialogInstance =
924
Inkscape::UI::Dialogs::FileOpenDialog::create(
925
(char const *)import_path,
926
Inkscape::UI::Dialogs::IMPORT_TYPES,
927
(char const *)_("Select file to import"));
929
bool success = importDialogInstance->show();
930
char *fileName = ( success
931
? g_strdup(importDialogInstance->getFilename())
933
Inkscape::Extension::Extension *selection =
934
importDialogInstance->getSelectionType();
936
if (!success) return;
939
gsize bytesWritten = 0;
940
GError *error = NULL;
941
#ifdef INK_DUMP_FILENAME_CONV
942
dump_str( fileName, "D file pre is " );
944
gchar *newFileName = g_filename_to_utf8( fileName,
949
if ( newFileName != NULL ) {
951
fileName = newFileName;
952
#ifdef INK_DUMP_FILENAME_CONV
953
dump_str( fileName, "D file post is " );
956
// TODO: bulia, please look over
957
g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
961
if (!g_utf8_validate(fileName, -1, NULL)) {
962
// TODO: bulia, please look over
963
g_warning( "INPUT FILENAME IS NOT UTF-8" );
967
import_path = g_dirname(fileName);
968
if (import_path) import_path = g_strconcat(import_path, G_DIR_SEPARATOR_S, NULL);
970
file_import(doc, fileName, selection);
979
/*######################
981
######################*/
987
sp_file_export_dialog(void *widget)
992
#include <display/nr-arena-item.h>
993
#include <display/nr-arena.h>
996
int width, height, sheight;
998
NRArenaItem *root; // the root arena item to show; it is assumed that all unneeded items are hidden
1000
unsigned (*status)(float, void *);
1009
sp_export_get_rows(guchar const **rows, int row, int num_rows, void *data)
1011
struct SPEBP *ebp = (struct SPEBP *) data;
1014
if (!ebp->status((float) row / ebp->height, ebp->data)) return 0;
1017
num_rows = MIN(num_rows, ebp->sheight);
1018
num_rows = MIN(num_rows, ebp->height - row);
1020
/* Set area of interest */
1024
bbox.x1 = ebp->width;
1025
bbox.y1 = row + num_rows;
1026
/* Update to renderable state */
1028
nr_matrix_set_identity(&gc.transform);
1030
nr_arena_item_invoke_update(ebp->root, &bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
1033
nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
1034
bbox.x0, bbox.y0, bbox.x1, bbox.y1,
1035
ebp->px, 4 * ebp->width, FALSE, FALSE);
1037
for (int r = 0; r < num_rows; r++) {
1038
guchar *p = NR_PIXBLOCK_PX(&pb) + r * pb.rs;
1039
for (int c = 0; c < ebp->width; c++) {
1048
nr_arena_item_invoke_render(ebp->root, &bbox, &pb, 0);
1050
for (int r = 0; r < num_rows; r++) {
1051
rows[r] = NR_PIXBLOCK_PX(&pb) + r * pb.rs;
1054
nr_pixblock_release(&pb);
1060
Hide all items which are not listed in list, recursively, skipping groups and defs
1063
hide_other_items_recursively(SPObject *o, GSList *list, unsigned dkey)
1069
&& !g_slist_find(list, o))
1071
sp_item_invoke_hide(SP_ITEM(o), dkey);
1075
if (!g_slist_find(list, o)) {
1076
for (SPObject *child = sp_object_first_child(o) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
1077
hide_other_items_recursively(child, list, dkey);
1084
* Render the SVG drawing onto a PNG raster image, then save to
1085
* a file. Returns TRUE if succeeded in writing the file,
1089
sp_export_png_file(SPDocument *doc, gchar const *filename,
1090
double x0, double y0, double x1, double y1,
1091
unsigned width, unsigned height,
1092
unsigned long bgcolor,
1093
unsigned (*status)(float, void *),
1094
void *data, bool force_overwrite,
1097
int write_status = TRUE;
1098
g_return_val_if_fail(doc != NULL, FALSE);
1099
g_return_val_if_fail(filename != NULL, FALSE);
1100
g_return_val_if_fail(width >= 1, FALSE);
1101
g_return_val_if_fail(height >= 1, FALSE);
1103
if (!force_overwrite && !sp_ui_overwrite_file(filename)) {
1107
sp_document_ensure_up_to_date(doc);
1109
/* Go to document coordinates */
1111
y0 = sp_document_height(doc) - y1;
1112
y1 = sp_document_height(doc) - t;
1115
* 1) a[0] * x0 + a[2] * y1 + a[4] = 0.0
1116
* 2) a[1] * x0 + a[3] * y1 + a[5] = 0.0
1117
* 3) a[0] * x1 + a[2] * y1 + a[4] = width
1118
* 4) a[1] * x0 + a[3] * y0 + a[5] = height
1122
* (1,3) a[0] * x1 - a[0] * x0 = width
1123
* a[0] = width / (x1 - x0)
1124
* (2,4) a[3] * y0 - a[3] * y1 = height
1125
* a[3] = height / (y0 - y1)
1126
* (1) a[4] = -a[0] * x0
1127
* (2) a[5] = -a[3] * y1
1131
affine.c[0] = width / (x1 - x0);
1134
affine.c[3] = height / (y1 - y0);
1135
affine.c[4] = -affine.c[0] * x0;
1136
affine.c[5] = -affine.c[3] * y0;
1138
//SP_PRINT_MATRIX("SVG2PNG", &affine);
1142
ebp.height = height;
1143
ebp.r = NR_RGBA32_R(bgcolor);
1144
ebp.g = NR_RGBA32_G(bgcolor);
1145
ebp.b = NR_RGBA32_B(bgcolor);
1146
ebp.a = NR_RGBA32_A(bgcolor);
1148
/* Create new arena */
1149
NRArena *arena = NRArena::create();
1150
unsigned dkey = sp_item_display_key_new(1);
1152
/* Create ArenaItems and set transform */
1153
ebp.root = sp_item_invoke_show(SP_ITEM(sp_document_root(doc)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
1154
nr_arena_item_set_transform(NR_ARENA_ITEM(ebp.root), NR::Matrix(&affine));
1156
// We show all and then hide all items we don't want, instead of showing only requested items,
1157
// because that would not work if the shown item references something in defs
1159
hide_other_items_recursively(sp_document_root(doc), items_only, dkey);
1162
ebp.status = status;
1165
if ((width < 256) || ((width * height) < 32768)) {
1166
ebp.px = nr_pixelstore_64K_new(FALSE, 0);
1167
ebp.sheight = 65536 / (4 * width);
1168
write_status = sp_png_write_rgba_striped(filename, width, height, sp_export_get_rows, &ebp);
1169
nr_pixelstore_64K_free(ebp.px);
1171
ebp.px = nr_new(guchar, 4 * 64 * width);
1173
write_status = sp_png_write_rgba_striped(filename, width, height, sp_export_get_rows, &ebp);
1178
sp_item_invoke_hide(SP_ITEM(sp_document_root(doc)), dkey);
1180
/* Free Arena and ArenaItem */
1181
nr_arena_item_unref(ebp.root);
1182
nr_object_unref((NRObject *) arena);
1183
return write_status;
1187
/*######################
1189
######################*/
1193
* Print the current document, if any.
1198
SPDocument *doc = SP_ACTIVE_DOCUMENT;
1200
sp_print_document(doc, FALSE);
1205
* Print the current document, if any. Do not use
1206
* the machine's print drivers.
1209
sp_file_print_direct()
1211
SPDocument *doc = SP_ACTIVE_DOCUMENT;
1213
sp_print_document(doc, TRUE);
1218
* Display what the drawing would look like, if
1222
sp_file_print_preview(gpointer object, gpointer data)
1225
SPDocument *doc = SP_ACTIVE_DOCUMENT;
1227
sp_print_preview_document(doc);
1231
void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1233
//g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1236
gchar const* things[] = {
1238
"http://www.google.com/image.png",
1239
"ftp://ssd.com/doo",
1242
"file:/foo/dee/bar.svg",
1243
"file:///foo/dee/bar.svg",
1245
"/foo/bar\xe1\x84\x92.svg",
1246
"file:///foo/bar\xe1\x84\x92.svg",
1247
"file:///foo/bar%e1%84%92.svg",
1248
"/foo/bar%e1%84%92.svg",
1249
"bar\xe1\x84\x92.svg",
1253
g_message("+------");
1254
for ( int i = 0; things[i]; i++ )
1259
gboolean isAbs = g_path_is_absolute( things[i] );
1260
gchar *str = uri.toString();
1261
g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1262
(int)uri.isRelative(),
1270
catch ( MalformedURIException err )
1272
dump_str( things[i], "MalformedURIException" );
1273
xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1274
g_message(" gone from [%s] to [%s]", things[i], redo );
1277
URI again = URI::fromUtf8( things[i] );
1278
gboolean isAbs = g_path_is_absolute( things[i] );
1279
gchar *str = again.toString();
1280
g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs,
1281
(int)again.isRelative(),
1292
g_message("+------");
1295
GSList const *images = sp_document_get_resource_list(doc, "image");
1296
for (GSList const *l = images; l != NULL; l = l->next) {
1297
Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1299
const gchar *href = ir->attribute("xlink:href");
1301
// First try to figure out an absolute path to the asset
1302
//g_message("image href [%s]", href );
1303
if (spns && !g_path_is_absolute(href)) {
1304
const gchar *absref = ir->attribute("sodipodi:absref");
1305
const gchar *base_href = g_build_filename(base, href, NULL);
1306
//g_message(" absr [%s]", absref );
1308
if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1310
// only switch over if the absref is valid while href is not
1312
//g_message(" copied absref to href");
1316
// Once we have an absolute path, convert it relative to the new location
1317
if (href && g_path_is_absolute(href)) {
1318
const gchar *relname = sp_relative_path_from_path(href, base);
1319
//g_message(" setting to [%s]", relname );
1320
ir->setAttribute("xlink:href", relname);
1322
// TODO next refinement is to make the first choice keeping the relative path as-is if
1323
// based on the new location it gives us a valid file.
1331
c-file-style:"stroustrup"
1332
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1333
indent-tabs-mode:nil
1337
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :