summaryrefslogtreecommitdiffstats
path: root/src/resource-manager.cpp
blob: a68b2c7ae4d0b7712cd25ab70c079d2500430e0e (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Inkscape::ResourceManager - tracks external resources such as image and css files.
 *
 * Copyright 2011  Jon A. Cruz  <jon@joncruz.org>
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <string>
#include <vector>
#include <glibmm/i18n.h>
#include <glibmm/convert.h>
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>
#include <glibmm/uriutils.h>

#include "resource-manager.h"

#include "document.h"
#include "sp-object.h"
#include "xml/node.h"
#include "document-undo.h"

namespace Inkscape {



class ResourceManagerImpl : public ResourceManager {
public:
    ResourceManagerImpl();
    virtual ~ResourceManagerImpl();

    virtual bool fixupBrokenLinks(SPDocument *doc);
    

    /**
     * Walk all links in a document and create a listing of unique broken links.
     *
     * @return a list of all broken links.
     */
    std::vector<Glib::ustring> findBrokenLinks(SPDocument *doc);

    /**
     * Resolve broken links as a whole and return a map for those that can be found.
     *
     * Note: this will allow for future enhancements including relinking to new locations
     * with the most broken files found, etc.
     *
     * @return a map of found links.
     */
    std::map<Glib::ustring, Glib::ustring> locateLinks(Glib::ustring const & docbase, std::vector<Glib::ustring> const & brokenLinks);

    bool extractFilepath( Glib::ustring const &href, std::string &uri );

protected:
};


ResourceManagerImpl::ResourceManagerImpl()
    : ResourceManager()
{
}

ResourceManagerImpl::~ResourceManagerImpl()
{
}

bool ResourceManagerImpl::extractFilepath( Glib::ustring const &href, std::string &uri )
{                    
    bool isFile = false;

    uri.clear();

    std::string scheme = Glib::uri_parse_scheme(href);
    if ( !scheme.empty() ) {
        // TODO debug g_message("Scheme is now [%s]", scheme.c_str());
        if ( scheme == "file" ) {
            // TODO debug g_message("--- is a file URI                 [%s]", href.c_str());

            // throws Glib::ConvertError:
            uri = Glib::filename_from_uri(href); // TODO see if we can get this to throw
            // TODO debug g_message("                                  [%s]", uri.c_str());
            isFile = true;
        }
    } else {
        // No scheme. Assuming it is a file path (absolute or relative).
        // throws Glib::ConvertError:
        uri = Glib::filename_from_utf8( href );
        isFile = true;
    }

    return isFile;
}


std::vector<Glib::ustring> ResourceManagerImpl::findBrokenLinks( SPDocument *doc )
{
    std::vector<Glib::ustring> result;
    std::set<Glib::ustring> uniques;

    if ( doc ) {
        GSList const *images = doc->getResourceList("image");
        for (GSList const *it = images; it; it = it->next) {
            Inkscape::XML::Node *ir = static_cast<SPObject *>(it->data)->getRepr();

            gchar const *href = ir->attribute("xlink:href");
            if ( href &&  ( uniques.find(href) == uniques.end() ) ) {
                std::string uri;
                if ( extractFilepath( href, uri ) ) {
                    if ( Glib::path_is_absolute(uri) ) {
                        if ( !Glib::file_test(uri, Glib::FILE_TEST_EXISTS) ) {
                            result.push_back(href);
                            uniques.insert(href);
                        }
                    } else {
                        std::string combined = Glib::build_filename(doc->getBase(), uri);
                        if ( !Glib::file_test(uri, Glib::FILE_TEST_EXISTS) ) {
                            result.push_back(href);
                            uniques.insert(href);
                        }
                    }
                }
            }
        }        
    }

    return result;
}


std::map<Glib::ustring, Glib::ustring> ResourceManagerImpl::locateLinks(Glib::ustring const & docbase, std::vector<Glib::ustring> const & brokenLinks)
{
    std::map<Glib::ustring, Glib::ustring> result;

    // At the moment we expect this list to contain file:// references, or simple relative or absolute paths.
    for ( std::vector<Glib::ustring>::const_iterator it = brokenLinks.begin(); it != brokenLinks.end(); ++it ) {
        // TODO debug g_message("========{%s}", it->c_str());

        std::string uri;
        if ( extractFilepath( *it, uri ) ) {
            // We were able to get some path. Check it

            if ( !Glib::path_is_absolute(uri) ) {
                uri = Glib::build_filename(docbase, uri);
                // TODO debug g_message("         not absolute. Fixing up as [%s]", uri.c_str());
            }

            if ( !Glib::file_test(uri, Glib::FILE_TEST_EXISTS) ) {
                // TODO debug g_message("                                  DOES NOT EXIST.");
                std::string tmp = uri;
                std::string prior;
                std::string remainder;
                bool exists = false;
                while ( (tmp != prior) && !exists) {
                    prior = tmp;
                    std::string basename = Glib::path_get_basename(tmp);
                    tmp = Glib::path_get_dirname(tmp);
                    if ( remainder.empty() ) {
                        remainder = basename;
                    } else {
                        remainder = Glib::build_filename(basename, remainder);
                    }

                    std::string rebuild = Glib::build_filename(docbase, remainder);
                    exists = Glib::file_test(rebuild, Glib::FILE_TEST_EXISTS);

                    // TODO debug g_message("                                    [%s]  [%s]%s", tmp.c_str(), remainder.c_str(), exists ? "   XXXX" : "");
                    if ( exists ) {
                        Glib::ustring replacement = Glib::filename_to_utf8( remainder );
                        result[*it] = replacement;
                    }
                }
            }
        }
    }

    return result;
}

bool ResourceManagerImpl::fixupBrokenLinks(SPDocument *doc)
{
    bool changed = false;
    if ( doc ) {
        // TODO debug g_message("FIXUP FIXUP FIXUP FIXUP FIXUP FIXUP FIXUP FIXUP FIXUP FIXUP");
        // TODO debug g_message("      base is [%s]", doc->getBase());

        std::vector<Glib::ustring> brokenHrefs = findBrokenLinks(doc);
        if ( !brokenHrefs.empty() ) {
            // TODO debug g_message("    FOUND SOME LINKS %d", brokenHrefs.size());
            for ( std::vector<Glib::ustring>::iterator it = brokenHrefs.begin(); it != brokenHrefs.end(); ++it ) {
                // TODO debug g_message("        [%s]", it->c_str());
            }
        }

        std::map<Glib::ustring, Glib::ustring> mapping = locateLinks(doc->getBase(), brokenHrefs);
        for ( std::map<Glib::ustring, Glib::ustring>::iterator it = mapping.begin(); it != mapping.end(); ++it )
        {
            // TODO debug g_message("     [%s] ==> {%s}", it->first.c_str(), it->second.c_str());
        }

        bool savedUndoState = DocumentUndo::getUndoSensitive(doc);
        DocumentUndo::setUndoSensitive(doc, true);

        GSList const *images = doc->getResourceList("image");
        for (GSList const *it = images; it; it = it->next) {
            Inkscape::XML::Node *ir = static_cast<SPObject *>(it->data)->getRepr();

            gchar const *href = ir->attribute("xlink:href");
            if ( href ) {
                // TODO debug g_message("                  consider [%s]", href);
                
                if ( mapping.find(href) != mapping.end() ) {
                    // TODO debug g_message("                     Found a replacement");

                    ir->setAttribute( "xlink:href", mapping[href].c_str() );
                    if ( ir->attribute( "sodipodi:absref" ) ) {
                        ir->setAttribute( "sodipodi:absref", 0 ); // Remove this attribute
                    }

                    SPObject *updated = doc->getObjectByRepr(ir);
                    if (updated) {
                        // force immediate update of dependant attributes
                        updated->updateRepr();
                    }

                    changed = true;
                }
            }
        }
        if ( changed ) {
            DocumentUndo::done( doc, SP_VERB_DIALOG_XML_EDITOR, _("Fixup broken links") );
        }
        DocumentUndo::setUndoSensitive(doc, savedUndoState);
    }

    return changed;
}




static ResourceManagerImpl* theInstance = 0;

ResourceManager::ResourceManager()
    : Glib::Object()
{
}

ResourceManager::~ResourceManager() {
}

ResourceManager& ResourceManager::getManager() {
    if ( !theInstance ) {
        theInstance = new ResourceManagerImpl();
    }

    return *theInstance;
}


} // namespace Inkscape

/*
  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 :