summaryrefslogtreecommitdiffstats
path: root/src/sp-tag-use-reference.cpp
blob: 50c01181231bd0198144486dc231355bf9ee4730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * The reference corresponding to href of <inkscape:tagref> element.
 *
 * Copyright (C) Theodore Janeczko 2012-2014 <flutterguy317@gmail.com>
 *
 * Released under GNU GPL, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <string>
#include <string.h>

#include "enums.h"
#include "sp-tag-use-reference.h"

#include "display/curve.h"
#include "livarot/Path.h"
#include "preferences.h"
#include "sp-shape.h"
#include "sp-text.h"
#include "uri.h"

#if 0
namespace {
    SPObject* createTagUseReference() {
        return new SPTag();
    }
    bool tagUseReferencesRegistered = SPFactory::instance().registerObject("inkscape:tag", createTag);
}
// this SPObject doesn't need to be registered
#endif


bool SPTagUseReference::_acceptObject(SPObject * const obj) const
{
    if (SP_IS_ITEM(obj)) {
        SPObject * const owner = getOwner();
        // Refuse references to us or to an ancestor.
        for ( SPObject *iter = owner ; iter ; iter = iter->parent ) {
            if ( iter == obj ) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}


static void sp_usepath_href_changed(SPObject *old_ref, SPObject *ref, SPTagUsePath *offset);
static void sp_usepath_delete_self(SPObject *deleted, SPTagUsePath *offset);

SPTagUsePath::SPTagUsePath(SPObject* i_owner):SPTagUseReference(i_owner)
{
    owner=i_owner;
    originalPath = NULL;
    sourceDirty=false;
    sourceHref = NULL;
    sourceRepr = NULL;
    sourceObject = NULL;
    _changed_connection = changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_usepath_href_changed), this)); // listening to myself, this should be virtual instead

    user_unlink = NULL;
}

SPTagUsePath::~SPTagUsePath(void)
{
    delete originalPath;
    originalPath = NULL;

    _changed_connection.disconnect(); // to do before unlinking

    quit_listening();
    unlink();
}

void
SPTagUsePath::link(char *to)
{
    if ( to == NULL ) {
        quit_listening();
        unlink();
    } else {
        if ( !sourceHref || ( strcmp(to, sourceHref) != 0 ) ) {
            g_free(sourceHref);
            sourceHref = g_strdup(to);
            try {
                attach(Inkscape::URI(to));
            } catch (Inkscape::BadURIException &e) {
                /* TODO: Proper error handling as per
                 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
                 */
                g_warning("%s", e.what());
                detach();
            }
        }
    }
}

void
SPTagUsePath::unlink(void)
{
    g_free(sourceHref);
    sourceHref = NULL;
    detach();
}

void
SPTagUsePath::start_listening(SPObject* to)
{
    if ( to == NULL ) {
        return;
    }
    sourceObject = to;
    sourceRepr = to->getRepr();
    _delete_connection = to->connectDelete(sigc::bind(sigc::ptr_fun(&sp_usepath_delete_self), this));
}

void
SPTagUsePath::quit_listening(void)
{
    if ( sourceObject == NULL ) {
        return;
    }
    _delete_connection.disconnect();
    sourceRepr = NULL;
    sourceObject = NULL;
}

static void
sp_usepath_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPTagUsePath *offset)
{
    offset->quit_listening();
    SPItem *refobj = offset->getObject();
    if ( refobj ) {
        offset->start_listening(refobj);
    }
}

static void
sp_usepath_delete_self(SPObject */*deleted*/, SPTagUsePath *offset)
{
    offset->owner->deleteObject();
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :