~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/file.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Ted Gould, Kees Cook
  • Date: 2009-06-24 14:00:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624140043-07stp20mry48hqup
Tags: 0.47~pre0-0ubuntu1
* New upstream release

[ Ted Gould ]
* debian/control: Adding libgsl0 and removing version specifics on boost

[ Kees Cook ]
* debian/watch: updated to run uupdate and mangle pre-release versions.
* Dropped patches that have been taken upstream:
  - 01_mips
  - 02-poppler-0.8.3
  - 03-chinese-inkscape
  - 05_fix_latex_patch
  - 06_gcc-4.4
  - 07_cdr2svg
  - 08_skip-bad-utf-on-pdf-import
  - 09_gtk-clist
  - 10_belarussian
  - 11_libpng
  - 12_desktop
  - 13_slider
  - 100_svg_import_improvements
  - 102_sp_pattern_painter_free
  - 103_bitmap_type_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define __SP_FILE_C__
2
 
 
3
 
/*
4
 
 * File/Print operations
5
 
 *
6
 
 * Authors:
 
1
/** @file
 
2
 * @brief File/Print operations
 
3
 */
 
4
/* Authors:
7
5
 *   Lauris Kaplinski <lauris@kaplinski.com>
8
6
 *   Chema Celorio <chema@celorio.com>
9
7
 *   bulia byak <buliabyak@users.sf.net>
10
8
 *   Bruno Dilly <bruno.dilly@gmail.com>
 
9
 *   Stephen Silver <sasilver@users.sourceforge.net>
11
10
 *
12
11
 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
13
 
 * Copyright (C) 1999-2005 Authors
 
12
 * Copyright (C) 1999-2008 Authors
14
13
 * Copyright (C) 2004 David Turner
15
14
 * Copyright (C) 2001-2002 Ximian, Inc.
16
15
 *
17
16
 * Released under GNU GPL, read the file 'COPYING' for more information
18
17
 */
19
18
 
20
 
/**
21
 
 * Note: This file needs to be cleaned up extensively.
 
19
/** @file
 
20
 * @note This file needs to be cleaned up extensively.
22
21
 * What it probably needs is to have one .h file for
23
22
 * the API, and two or more .cpp files for the implementations.
24
23
 */
27
26
# include "config.h"
28
27
#endif
29
28
 
 
29
#include <gtk/gtk.h>
30
30
#include <glib/gmem.h>
 
31
#include <glibmm/i18n.h>
31
32
#include <libnr/nr-pixops.h>
32
33
 
 
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"
33
40
#include "document-private.h"
34
 
#include "selection-chemistry.h"
35
 
#include "ui/view/view-widget.h"
36
 
#include "dir-util.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"
37
46
#include "helper/png-write.h"
38
 
#include "dialogs/export.h"
39
 
#include <glibmm/i18n.h>
40
 
#include "inkscape.h"
41
 
#include "desktop.h"
42
 
#include "selection.h"
 
47
#include "id-clash.h"
 
48
#include "inkscape.h"
 
49
#include "inkscape.h"
43
50
#include "interface.h"
44
 
#include "style.h"
45
 
#include "print.h"
46
 
#include "file.h"
 
51
#include "io/sys.h"
47
52
#include "message.h"
48
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"
49
62
#include "ui/dialog/filedialog.h"
50
63
#include "ui/dialog/ocaldialogs.h"
51
 
#include "prefs-utils.h"
52
 
#include "path-prefix.h"
53
 
 
54
 
#include "sp-namedview.h"
55
 
#include "desktop-handles.h"
56
 
 
57
 
#include "extension/db.h"
58
 
#include "extension/input.h"
59
 
#include "extension/output.h"
60
 
/* #include "extension/menu.h"  */
61
 
#include "extension/system.h"
62
 
 
63
 
#include "io/sys.h"
64
 
#include "application/application.h"
65
 
#include "application/editor.h"
66
 
#include "inkscape.h"
 
64
#include "ui/view/view-widget.h"
67
65
#include "uri.h"
 
66
#include "xml/rebase-hrefs.h"
68
67
 
69
68
#ifdef WITH_GNOME_VFS
70
69
# include <libgnomevfs/gnome-vfs.h>
74
73
#include "jabber_whiteboard/session-manager.h"
75
74
#endif
76
75
 
 
76
#ifdef WIN32
 
77
#include <windows.h>
 
78
#endif
77
79
 
78
80
//#define INK_DUMP_FILENAME_CONV 1
79
81
#undef INK_DUMP_FILENAME_CONV
84
86
void dump_str(gchar const *str, gchar const *prefix);
85
87
void dump_ustr(Glib::ustring const &ustr);
86
88
 
 
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
    }
 
106
}
 
107
 
87
108
 
88
109
/*######################
89
110
## N E W
176
197
             Inkscape::Extension::Extension *key,
177
198
             bool add_to_recent, bool replace_empty)
178
199
{
 
200
    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
201
    if (desktop)
 
202
        desktop->setWaitingCursor();
 
203
 
179
204
    SPDocument *doc = NULL;
180
205
    try {
181
206
        doc = Inkscape::Extension::open(key, uri.c_str());
185
210
        doc = NULL;
186
211
    }
187
212
 
 
213
    if (desktop)
 
214
        desktop->clearWaitingCursor();
 
215
 
188
216
    if (doc) {
189
 
        SPDesktop *desktop = SP_ACTIVE_DESKTOP;
190
217
        SPDocument *existing = desktop ? sp_desktop_document(desktop) : NULL;
191
218
 
192
219
        if (existing && existing->virgin && replace_empty) {
213
240
        sp_namedview_update_layers_from_document(desktop);
214
241
 
215
242
        if (add_to_recent) {
216
 
            prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
 
243
            sp_file_add_recent(SP_DOCUMENT_URI(doc));
217
244
        }
218
245
 
219
246
        return TRUE;
267
294
 
268
295
        // remember current zoom and view
269
296
        double zoom = desktop->current_zoom();
270
 
        NR::Point c = desktop->get_display_area().midpoint();
 
297
        Geom::Point c = desktop->get_display_area().midpoint();
271
298
 
272
299
        reverted = sp_file_open(uri,NULL);
273
300
        if (reverted) {
274
301
            // restore zoom and view
275
 
            desktop->zoom_absolute(c[NR::X], c[NR::Y], zoom);
 
302
            desktop->zoom_absolute(c[Geom::X], c[Geom::Y], zoom);
276
303
        }
277
304
    } else {
278
305
        reverted = false;
367
394
    g_message("---------------");
368
395
}
369
396
 
370
 
static Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance = NULL;
371
 
 
372
397
/**
373
398
 *  Display an file Open selector.  Open a document if OK is pressed.
374
399
 *  Can select single or multiple files for opening.
376
401
void
377
402
sp_file_open_dialog(Gtk::Window &parentWindow, gpointer /*object*/, gpointer /*data*/)
378
403
{
379
 
 
380
404
    //# Get the current directory for finding files
381
 
    Glib::ustring open_path;
382
 
    char *attr = (char *)prefs_get_string_attribute("dialogs.open", "path");
383
 
    if (attr)
384
 
        open_path = attr;
 
405
    static Glib::ustring open_path;
 
406
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
385
407
 
 
408
    if(open_path.empty())
 
409
    {
 
410
        Glib::ustring attr = prefs->getString("/dialogs/open/path");
 
411
        if (!attr.empty()) open_path = attr;
 
412
    }
386
413
 
387
414
    //# Test if the open_path directory exists
388
415
    if (!Inkscape::IO::file_test(open_path.c_str(),
389
416
              (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
390
417
        open_path = "";
391
418
 
 
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
 
448
 
392
449
    //# If no open path, default to our home directory
393
 
    if (open_path.size() < 1)
394
 
        {
 
450
    if (open_path.empty())
 
451
    {
395
452
        open_path = g_get_home_dir();
396
453
        open_path.append(G_DIR_SEPARATOR_S);
397
 
        }
 
454
    }
398
455
 
399
 
    //# Create a dialog if we don't already have one
400
 
    if (!openDialogInstance) {
401
 
        openDialogInstance =
 
456
    //# Create a dialog
 
457
    Inkscape::UI::Dialog::FileOpenDialog *openDialogInstance =
402
458
              Inkscape::UI::Dialog::FileOpenDialog::create(
403
 
                 parentWindow,
404
 
                 open_path,
 
459
                 parentWindow, open_path,
405
460
                 Inkscape::UI::Dialog::SVG_TYPES,
406
 
                 (char const *)_("Select file to open"));
407
 
    }
408
 
 
 
461
                 _("Select file to open"));
409
462
 
410
463
    //# Show the dialog
411
464
    bool const success = openDialogInstance->show();
 
465
 
 
466
    //# Save the folder the user selected for later
 
467
    open_path = openDialogInstance->getCurrentDirectory();
 
468
 
412
469
    if (!success)
 
470
    {
 
471
        delete openDialogInstance;
413
472
        return;
 
473
    }
414
474
 
415
475
    //# User selected something.  Get name and type
416
476
    Glib::ustring fileName = openDialogInstance->getFilename();
 
477
 
417
478
    Inkscape::Extension::Extension *selection =
418
479
            openDialogInstance->getSelectionType();
419
480
 
420
 
    //# Code to check & open iff multiple files.
421
 
    std::vector<Glib::ustring> flist=openDialogInstance->getFilenames();
 
481
    //# Code to check & open if multiple files.
 
482
    std::vector<Glib::ustring> flist = openDialogInstance->getFilenames();
 
483
 
 
484
    //# We no longer need the file dialog object - delete it
 
485
    delete openDialogInstance;
 
486
    openDialogInstance = NULL;
422
487
 
423
488
    //# Iterate through filenames if more than 1
424
489
    if (flist.size() > 1)
 
490
    {
 
491
        for (unsigned int i = 0; i < flist.size(); i++)
425
492
        {
426
 
        for (unsigned int i=1 ; i<flist.size() ; i++)
427
 
            {
428
 
            Glib::ustring fName = flist[i];
 
493
            fileName = flist[i];
429
494
 
430
 
            if (Glib::file_test(fileName, Glib::FILE_TEST_IS_DIR)) {
431
 
            Glib::ustring newFileName = Glib::filename_to_utf8(fName);
 
495
            Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
432
496
            if ( newFileName.size() > 0 )
433
 
                fName = newFileName;
 
497
                fileName = newFileName;
434
498
            else
435
499
                g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
436
500
 
437
501
#ifdef INK_DUMP_FILENAME_CONV
438
 
            g_message("Opening File %s\n",fileName);
 
502
            g_message("Opening File %s\n", fileName.c_str());
439
503
#endif
440
504
            sp_file_open(fileName, selection);
441
 
            }
442
505
        }
 
506
 
443
507
        return;
444
508
    }
445
509
 
446
510
 
447
 
    if (fileName.size() > 0) {
448
 
 
 
511
    if (!fileName.empty())
 
512
    {
449
513
        Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
450
514
 
451
515
        if ( newFileName.size() > 0)
453
517
        else
454
518
            g_warning( "ERROR CONVERTING OPEN FILENAME TO UTF-8" );
455
519
 
456
 
        open_path = fileName;
 
520
        open_path = Glib::path_get_dirname (fileName);
457
521
        open_path.append(G_DIR_SEPARATOR_S);
458
 
        prefs_set_string_attribute("dialogs.open", "path", open_path.c_str());
 
522
        prefs->setString("/dialogs/open/path", open_path);
459
523
 
460
524
        sp_file_open(fileName, selection);
461
525
    }
534
598
        g_free(text);
535
599
        g_free(safeUri);
536
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;
537
604
    } catch (Inkscape::Extension::Output::no_overwrite &e) {
538
605
        return sp_file_save_dialog(parentWindow, doc);
539
606
    }
622
689
    }
623
690
    return true;
624
691
#else
625
 
        // in case we do not have GNOME_VFS
626
 
        return false;
 
692
    // in case we do not have GNOME_VFS
 
693
    return false;
627
694
#endif
628
695
 
629
696
}
639
706
{
640
707
 
641
708
    Inkscape::XML::Node *repr = sp_document_repr_root(doc);
642
 
 
643
709
    Inkscape::Extension::Output *extension = 0;
 
710
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
644
711
 
645
712
    //# Get the default extension name
646
713
    Glib::ustring default_extension;
647
714
    char *attr = (char *)repr->attribute("inkscape:output_extension");
648
 
    if (!attr)
649
 
        attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
650
 
    if (attr)
 
715
    if (!attr) {
 
716
        Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
 
717
        if(!attr2.empty()) default_extension = attr2;
 
718
    } else {
651
719
        default_extension = attr;
 
720
    }
652
721
    //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
653
722
 
654
723
    Glib::ustring save_path;
665
734
        if (extension)
666
735
            filename_extension = extension->get_extension();
667
736
 
668
 
        attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
669
 
        if (attr)
670
 
            save_path = attr;
 
737
        Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
 
738
        if (!attr3.empty())
 
739
            save_path = attr3;
671
740
 
672
741
        if (!Inkscape::IO::file_test(save_path.c_str(),
673
742
              (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
707
776
    } else {
708
777
        dialog_title = (char const *) _("Select file to save to");
709
778
    }
 
779
    gchar* doc_title = doc->root->title();
710
780
    Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
711
781
        Inkscape::UI::Dialog::FileSaveDialog::create(
712
 
                parentWindow,
 
782
            parentWindow,
713
783
            save_loc,
714
784
            Inkscape::UI::Dialog::SVG_TYPES,
715
 
            (char const *) _("Select file to save to"),
716
 
            default_extension
 
785
            dialog_title,
 
786
            default_extension,
 
787
            doc_title ? doc_title : ""
717
788
            );
718
789
 
719
 
    saveDialog->change_title(dialog_title);
720
790
    saveDialog->setSelectionType(extension);
721
791
 
722
 
    // allow easy access to the user's own templates folder
723
 
    gchar *templates = profile_path ("templates");
724
 
    if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
725
 
        dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
726
 
    }
727
 
    g_free (templates);
728
 
 
729
792
    bool success = saveDialog->show();
730
793
    if (!success) {
731
794
        delete saveDialog;
732
795
        return success;
733
796
    }
734
797
 
 
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);
 
802
 
735
803
    Glib::ustring fileName = saveDialog->getFilename();
736
804
    Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
737
805
 
749
817
 
750
818
        success = file_save(parentWindow, doc, fileName, selectionType, TRUE, !is_copy);
751
819
 
752
 
        if (success)
753
 
            prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
 
820
        if (success && SP_DOCUMENT_URI(doc)) {
 
821
            sp_file_add_recent(SP_DOCUMENT_URI(doc));
 
822
        }
754
823
 
755
824
        save_path = Glib::path_get_dirname(fileName);
756
 
        prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
 
825
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
826
        prefs->setString("/dialogs/save_as/path", save_path);
757
827
 
758
828
        return success;
759
829
    }
859
929
    }
860
930
 
861
931
    if (doc != NULL) {
862
 
        // move imported defs to our document's defs
 
932
        Inkscape::XML::rebase_hrefs(doc, in_doc->base, true);
 
933
        Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc);
 
934
 
 
935
        prevent_id_clashes(doc, in_doc);
 
936
 
863
937
        SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
864
 
        SPObject *defs = SP_DOCUMENT_DEFS(doc);
865
 
 
866
 
        Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
867
 
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
868
 
 
869
938
        Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
870
 
        for (SPObject *child = sp_object_first_child(defs);
 
939
 
 
940
        SPCSSAttr *style = sp_css_attr_from_object(SP_DOCUMENT_ROOT(doc));
 
941
 
 
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));
871
945
             child != NULL; child = SP_OBJECT_NEXT(child))
872
946
        {
873
 
            // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
874
 
            SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
875
 
        }
876
 
 
877
 
        guint items_count = 0;
878
 
        for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc));
879
 
             child != NULL; child = SP_OBJECT_NEXT(child)) {
880
 
            if (SP_IS_ITEM(child))
881
 
                items_count ++;
882
 
        }
883
 
        SPCSSAttr *style = sp_css_attr_from_object (SP_DOCUMENT_ROOT (doc));
884
 
 
 
947
            if (SP_IS_ITEM(child)) items_count++;
 
948
        }
 
949
 
 
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
        }
 
956
 
 
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
        }
 
968
 
 
969
        // Construct a new object representing the imported image,
 
970
        // and insert it into the current document.
885
971
        SPObject *new_obj = NULL;
886
 
 
887
 
        if ((style && style->firstChild()) || items_count > 1) {
888
 
            // create group
889
 
            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
890
 
            Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
891
 
            sp_repr_css_set (newgroup, style, "style");
892
 
 
893
 
            for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
894
 
                if (SP_IS_ITEM(child)) {
895
 
                    Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
896
 
 
897
 
                    // convert layers to groups; FIXME: add "preserve layers" mode where each layer
898
 
                    // from impot is copied to the same-named layer in host
899
 
                    newchild->setAttribute("inkscape:groupmode", NULL);
900
 
 
901
 
                    newgroup->appendChild(newchild);
902
 
                }
903
 
            }
904
 
 
905
 
            if (desktop) {
906
 
                // Add it to the current layer
907
 
                new_obj = desktop->currentLayer()->appendChildRepr(newgroup);
908
 
            } else {
909
 
                // There's no desktop (command line run?)
910
 
                // FIXME: For such cases we need a document:: method to return the current layer
911
 
                new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newgroup);
912
 
            }
913
 
 
914
 
            Inkscape::GC::release(newgroup);
915
 
        } else {
916
 
            // just add one item
917
 
            for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
918
 
                if (SP_IS_ITEM(child)) {
919
 
                    Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
920
 
                    newitem->setAttribute("inkscape:groupmode", NULL);
921
 
 
922
 
                    if (desktop) {
923
 
                        // Add it to the current layer
924
 
                        new_obj = desktop->currentLayer()->appendChildRepr(newitem);
925
 
                    } else {
926
 
                        // There's no desktop (command line run?)
927
 
                        // FIXME: For such cases we need a document:: method to return the current layer
928
 
                        new_obj = SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(newitem);
 
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);
 
977
 
 
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);
 
983
 
 
984
                if (newgroup) newgroup->appendChild(newitem);
 
985
                else new_obj = place_to_insert->appendChildRepr(newitem);
 
986
            }
 
987
 
 
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);
929
996
                    }
930
 
 
 
997
                }
 
998
                else if (!strcmp(tag, "svg:style")) {
 
999
                    SP_DOCUMENT_ROOT(in_doc)->appendChildRepr(SP_OBJECT_REPR(child)->duplicate(xml_in_doc));
931
1000
                }
932
1001
            }
933
1002
        }
 
1003
        if (newgroup) new_obj = place_to_insert->appendChildRepr(newgroup);
934
1004
 
935
 
        if (style) sp_repr_css_attr_unref (style);
 
1005
        // release some stuff
 
1006
        if (newgroup) Inkscape::GC::release(newgroup);
 
1007
        if (style) sp_repr_css_attr_unref(style);
936
1008
 
937
1009
        // select and move the imported item
938
1010
        if (new_obj && SP_IS_ITEM(new_obj)) {
939
1011
            Inkscape::Selection *selection = sp_desktop_selection(desktop);
940
1012
            selection->set(SP_ITEM(new_obj));
941
1013
 
942
 
            // To move the imported object, we must temporarily set the "transform pattern with
943
 
            // object" option.
 
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);
 
1019
 
 
1020
            // move to mouse pointer
944
1021
            {
945
 
                int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
946
 
                prefs_set_int_attribute("options.transform", "pattern", 1);
947
1022
                sp_document_ensure_up_to_date(sp_desktop_document(desktop));
948
 
                NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
 
1023
                Geom::OptRect sel_bbox = selection->bounds();
949
1024
                if (sel_bbox) {
950
 
                    NR::Point m( desktop->point() - sel_bbox->midpoint() );
951
 
                    sp_selection_move_relative(selection, m);
 
1025
                    Geom::Point m( desktop->point() - sel_bbox->midpoint() );
 
1026
                    sp_selection_move_relative(selection, m, false);
952
1027
                }
953
 
                prefs_set_int_attribute("options.transform", "pattern", saved_pref);
954
1028
            }
955
1029
        }
956
1030
 
968
1042
}
969
1043
 
970
1044
 
971
 
static Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance = NULL;
972
 
 
973
1045
/**
974
1046
 *  Display an Open dialog, import a resource if OK pressed.
975
1047
 */
982
1054
    if (!doc)
983
1055
        return;
984
1056
 
985
 
    if (!importDialogInstance) {
986
 
        importDialogInstance =
 
1057
    // Create new dialog (don't use an old one, because parentWindow has probably changed)
 
1058
    Inkscape::UI::Dialog::FileOpenDialog *importDialogInstance =
987
1059
             Inkscape::UI::Dialog::FileOpenDialog::create(
988
1060
                 parentWindow,
989
 
                 import_path,
 
1061
                 import_path,
990
1062
                 Inkscape::UI::Dialog::IMPORT_TYPES,
991
1063
                 (char const *)_("Select file to import"));
992
 
    }
993
1064
 
994
1065
    bool success = importDialogInstance->show();
995
 
    if (!success)
 
1066
    if (!success) {
 
1067
        delete importDialogInstance;
996
1068
        return;
 
1069
    }
997
1070
 
998
1071
    //# Get file name and extension type
999
1072
    Glib::ustring fileName = importDialogInstance->getFilename();
1000
 
    Inkscape::Extension::Extension *selection =
1001
 
        importDialogInstance->getSelectionType();
 
1073
    Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
1002
1074
 
 
1075
    delete importDialogInstance;
 
1076
    importDialogInstance = NULL;
1003
1077
 
1004
1078
    if (fileName.size() > 0) {
1005
1079
 
1033
1107
 
1034
1108
#ifdef NEW_EXPORT_DIALOG
1035
1109
 
1036
 
static Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance = NULL;
1037
 
 
1038
1110
/**
1039
1111
 *  Display an Export dialog, export as the selected type if OK pressed
1040
1112
 */
1048
1120
    Glib::ustring export_loc;
1049
1121
 
1050
1122
    Inkscape::XML::Node *repr = sp_document_repr_root(doc);
1051
 
 
 
1123
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1052
1124
    Inkscape::Extension::Output *extension;
1053
1125
 
1054
1126
    //# Get the default extension name
1055
1127
    Glib::ustring default_extension;
1056
1128
    char *attr = (char *)repr->attribute("inkscape:output_extension");
1057
 
    if (!attr)
1058
 
        attr = (char *)prefs_get_string_attribute("dialogs.save_as", "default");
1059
 
    if (attr)
 
1129
    if (!attr) {
 
1130
        Glib::ustring attr2 = prefs->getString("/dialogs/save_as/default");
 
1131
        if(!attr2.empty()) default_extension = attr2;
 
1132
    } else {
1060
1133
        default_extension = attr;
 
1134
    }
1061
1135
    //g_message("%s: extension name: '%s'", __FUNCTION__, default_extension);
1062
1136
 
1063
1137
    if (doc->uri == NULL)
1071
1145
        if (extension)
1072
1146
            filename_extension = extension->get_extension();
1073
1147
 
1074
 
        attr = (char *)prefs_get_string_attribute("dialogs.save_as", "path");
1075
 
        if (attr)
1076
 
            export_path = attr;
 
1148
        Glib::ustring attr3 = prefs->getString("/dialogs/save_as/path");
 
1149
        if (!attr3.empty())
 
1150
            export_path = attr3;
1077
1151
 
1078
1152
        if (!Inkscape::IO::file_test(export_path.c_str(),
1079
1153
              (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
1101
1175
        export_path = export_path_local;
1102
1176
 
1103
1177
    //# Show the SaveAs dialog
1104
 
    if (!exportDialogInstance)
1105
 
        exportDialogInstance =
1106
 
             Inkscape::UI::Dialog::FileExportDialog::create(
1107
 
                 export_path,
1108
 
                 Inkscape::UI::Dialog::EXPORT_TYPES,
1109
 
                 (char const *) _("Select file to export to"),
1110
 
                 default_extension
1111
 
            );
 
1178
    Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
 
1179
        Inkscape::UI::Dialog::FileExportDialog::create(
 
1180
            export_path,
 
1181
            Inkscape::UI::Dialog::EXPORT_TYPES,
 
1182
            (char const *) _("Select file to export to"),
 
1183
            default_extension
 
1184
        );
1112
1185
 
1113
1186
    bool success = exportDialogInstance->show();
1114
 
    if (!success)
 
1187
    if (!success) {
 
1188
        delete exportDialogInstance;
1115
1189
        return success;
 
1190
    }
1116
1191
 
1117
1192
    Glib::ustring fileName = exportDialogInstance->getFilename();
1118
1193
 
1119
1194
    Inkscape::Extension::Extension *selectionType =
1120
1195
        exportDialogInstance->getSelectionType();
1121
1196
 
 
1197
    delete exportDialogInstance;
 
1198
    exportDialogInstance = NULL;
1122
1199
 
1123
1200
    if (fileName.size() > 0) {
1124
1201
        Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1130
1207
 
1131
1208
        success = file_save(doc, fileName, selectionType, TRUE, FALSE);
1132
1209
 
1133
 
        if (success)
1134
 
            prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
 
1210
        if (success) {
 
1211
            Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
 
1212
            recent->add_item(SP_DOCUMENT_URI(doc));
 
1213
        }
1135
1214
 
1136
1215
        export_path = fileName;
1137
 
        prefs_set_string_attribute("dialogs.save_as", "path", export_path.c_str());
 
1216
        prefs->setString("/dialogs/save_as/path", export_path);
1138
1217
 
1139
1218
        return success;
1140
1219
    }
1180
1259
 
1181
1260
    bool success = false;
1182
1261
 
1183
 
    static Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance = NULL;
1184
 
    static Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1185
1262
    static bool gotSuccess = false;
1186
1263
 
1187
1264
    Inkscape::XML::Node *repr = sp_document_repr_root(doc);
 
1265
    (void)repr;
1188
1266
 
1189
1267
    if (!doc->uri && !doc->isModifiedSinceSave())
1190
1268
        return false;
1211
1289
        export_path = export_path_local;
1212
1290
 
1213
1291
    // Show the Export To OCAL dialog
1214
 
    if (!exportDialogInstance)
1215
 
        exportDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALDialog(
 
1292
    Inkscape::UI::Dialog::FileExportToOCALDialog *exportDialogInstance =
 
1293
        new Inkscape::UI::Dialog::FileExportToOCALDialog(
1216
1294
                parentWindow,
1217
1295
                Inkscape::UI::Dialog::EXPORT_TYPES,
1218
1296
                (char const *) _("Select file to export to")
1219
1297
                );
1220
1298
 
1221
1299
    success = exportDialogInstance->show();
1222
 
    if (!success)
 
1300
    if (!success) {
 
1301
        delete exportDialogInstance;
1223
1302
        return success;
 
1303
    }
1224
1304
 
1225
1305
    fileName = exportDialogInstance->getFilename();
1226
1306
 
 
1307
    delete exportDialogInstance;
 
1308
    exportDialogInstance = NULL;;
 
1309
 
1227
1310
    fileName.append(filename_extension.c_str());
1228
1311
    if (fileName.size() > 0) {
1229
1312
        Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
1251
1334
    // Start now the submition
1252
1335
 
1253
1336
    // Create the uri
 
1337
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1254
1338
    Glib::ustring uri = "dav://";
1255
 
    char *username = (char *)prefs_get_string_attribute("options.ocalusername", "str");
1256
 
    char *password = (char *)prefs_get_string_attribute("options.ocalpassword", "str");
1257
 
    if ((username == NULL) || (!strcmp(username, "")) || (password == NULL) || (!strcmp(password, "")))
 
1339
    Glib::ustring username = prefs->getString("/options/ocalusername/str");
 
1340
    Glib::ustring password = prefs->getString("/options/ocalpassword/str");
 
1341
    if (username.empty() || password.empty())
1258
1342
    {
 
1343
        Inkscape::UI::Dialog::FileExportToOCALPasswordDialog *exportPasswordDialogInstance = NULL;
1259
1344
        if(!gotSuccess)
1260
1345
        {
1261
 
            if (!exportPasswordDialogInstance)
1262
 
                exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
 
1346
            exportPasswordDialogInstance = new Inkscape::UI::Dialog::FileExportToOCALPasswordDialog(
1263
1347
                    parentWindow,
1264
1348
                    (char const *) _("Open Clip Art Login"));
1265
1349
            success = exportPasswordDialogInstance->show();
1266
 
            if (!success)
 
1350
            if (!success) {
 
1351
                delete exportPasswordDialogInstance;
1267
1352
                return success;
 
1353
            }
1268
1354
        }
1269
 
        username = (char *)exportPasswordDialogInstance->getUsername().c_str();
1270
 
        password = (char *)exportPasswordDialogInstance->getPassword().c_str();
 
1355
        username = exportPasswordDialogInstance->getUsername();
 
1356
        password = exportPasswordDialogInstance->getPassword();
 
1357
 
 
1358
        delete exportPasswordDialogInstance;
 
1359
        exportPasswordDialogInstance = NULL;
1271
1360
    }
1272
1361
    uri.append(username);
1273
1362
    uri.append(":");
1274
1363
    uri.append(password);
1275
1364
    uri.append("@");
1276
 
    uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
 
1365
    uri.append(prefs->getString("/options/ocalurl/str"));
1277
1366
    uri.append("/dav.php/");
1278
1367
    uri.append(Glib::path_get_basename(fileName));
1279
1368
 
1282
1371
    remove(fileName.c_str());
1283
1372
    if (!success)
1284
1373
    {
1285
 
        gchar *text = g_strdup_printf(_("Error exporting the document. Verify if the server name, username and password are correct. If the server have support for webdav and verify if you didn't forget to choose a license too."));
 
1374
        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."));
1286
1375
        sp_ui_error_dialog(text);
1287
1376
    }
1288
1377
    else
1323
1412
    if (!doc)
1324
1413
        return;
1325
1414
 
1326
 
    static Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
 
1415
    Inkscape::UI::Dialog::FileImportFromOCALDialog *importDialogInstance = NULL;
1327
1416
 
1328
1417
    if (!importDialogInstance) {
1329
1418
        importDialogInstance = new
1335
1424
    }
1336
1425
 
1337
1426
    bool success = importDialogInstance->show();
1338
 
    if (!success)
 
1427
    if (!success) {
 
1428
        delete importDialogInstance;
1339
1429
        return;
 
1430
    }
1340
1431
 
1341
1432
    // Get file name and extension type
1342
1433
    Glib::ustring fileName = importDialogInstance->getFilename();
1343
 
    Inkscape::Extension::Extension *selection =
1344
 
        importDialogInstance->getSelectionType();
 
1434
    Inkscape::Extension::Extension *selection = importDialogInstance->getSelectionType();
 
1435
 
 
1436
    delete importDialogInstance;
 
1437
    importDialogInstance = NULL;
1345
1438
 
1346
1439
    if (fileName.size() > 0) {
1347
1440
 
1392
1485
 
1393
1486
}
1394
1487
 
1395
 
void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
1396
 
{
1397
 
    //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
1398
 
 
1399
 
    if ( 0 ) {
1400
 
        gchar const* things[] = {
1401
 
            "data:foo,bar",
1402
 
            "http://www.google.com/image.png",
1403
 
            "ftp://ssd.com/doo",
1404
 
            "/foo/dee/bar.svg",
1405
 
            "foo.svg",
1406
 
            "file:/foo/dee/bar.svg",
1407
 
            "file:///foo/dee/bar.svg",
1408
 
            "file:foo.svg",
1409
 
            "/foo/bar\xe1\x84\x92.svg",
1410
 
            "file:///foo/bar\xe1\x84\x92.svg",
1411
 
            "file:///foo/bar%e1%84%92.svg",
1412
 
            "/foo/bar%e1%84%92.svg",
1413
 
            "bar\xe1\x84\x92.svg",
1414
 
            "bar%e1%84%92.svg",
1415
 
            NULL
1416
 
        };
1417
 
        g_message("+------");
1418
 
        for ( int i = 0; things[i]; i++ )
1419
 
        {
1420
 
            try
1421
 
            {
1422
 
                URI uri(things[i]);
1423
 
                gboolean isAbs = g_path_is_absolute( things[i] );
1424
 
                gchar *str = uri.toString();
1425
 
                g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
1426
 
                           (int)uri.isRelative(),
1427
 
                           uri.getScheme(),
1428
 
                           uri.getPath(),
1429
 
                           uri.getOpaque(),
1430
 
                           things[i],
1431
 
                           str );
1432
 
                g_free(str);
1433
 
            }
1434
 
            catch ( MalformedURIException err )
1435
 
            {
1436
 
                dump_str( things[i], "MalformedURIException" );
1437
 
                xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]);
1438
 
                g_message("    gone from [%s] to [%s]", things[i], redo );
1439
 
                if ( redo == NULL )
1440
 
                {
1441
 
                    URI again = URI::fromUtf8( things[i] );
1442
 
                    gboolean isAbs = g_path_is_absolute( things[i] );
1443
 
                    gchar *str = again.toString();
1444
 
                    g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
1445
 
                               (int)again.isRelative(),
1446
 
                               again.getScheme(),
1447
 
                               again.getPath(),
1448
 
                               again.getOpaque(),
1449
 
                               things[i],
1450
 
                               str );
1451
 
                    g_free(str);
1452
 
                    g_message("    ----");
1453
 
                }
1454
 
            }
1455
 
        }
1456
 
        g_message("+------");
1457
 
    }
1458
 
 
1459
 
    GSList const *images = sp_document_get_resource_list(doc, "image");
1460
 
    for (GSList const *l = images; l != NULL; l = l->next) {
1461
 
        Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data);
1462
 
 
1463
 
        const gchar *href = ir->attribute("xlink:href");
1464
 
 
1465
 
        // First try to figure out an absolute path to the asset
1466
 
        //g_message("image href [%s]", href );
1467
 
        if (spns && !g_path_is_absolute(href)) {
1468
 
            const gchar *absref = ir->attribute("sodipodi:absref");
1469
 
            const gchar *base_href = g_build_filename(base, href, NULL);
1470
 
            //g_message("      absr [%s]", absref );
1471
 
 
1472
 
            if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS))
1473
 
            {
1474
 
                // only switch over if the absref is valid while href is not
1475
 
                href = absref;
1476
 
                //g_message("     copied absref to href");
1477
 
            }
1478
 
        }
1479
 
 
1480
 
        // Once we have an absolute path, convert it relative to the new location
1481
 
        if (href && g_path_is_absolute(href)) {
1482
 
            const gchar *relname = sp_relative_path_from_path(href, base);
1483
 
            //g_message("     setting to [%s]", relname );
1484
 
            ir->setAttribute("xlink:href", relname);
1485
 
        }
1486
 
// TODO next refinement is to make the first choice keeping the relative path as-is if
1487
 
//      based on the new location it gives us a valid file.
1488
 
    }
1489
 
}
1490
 
 
1491
1488
 
1492
1489
/*
1493
1490
  Local Variables: