File: KoEmbeddedDocumentSaver.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (275 lines) | stat: -rw-r--r-- 9,223 bytes parent folder | download | duplicates (4)
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
273
274
275
/* This file is part of the KDE project
   Copyright (C) 2004-2006 David Faure <faure@kde.org>
   Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
   Copyright (C) 2010 Thomas Zander <zander@kde.org>
   Copyright (C) 2011 Inge Wallin <inge@lysator.liu.se>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoEmbeddedDocumentSaver.h"

#include <QList>

#include <OdfDebug.h>
#include <QUrl>

#include <KoStore.h>
#include <KoXmlWriter.h>
#include <KoOdfWriteStore.h>

#include "KoDocumentBase.h"
#include <KoOdfManifestEntry.h>


#define INTERNAL_PROTOCOL "intern"

struct FileEntry {
    QString path;
    QByteArray mimeType;       // QBA because this is what addManifestEntry wants
    QByteArray contents;
};


class Q_DECL_HIDDEN KoEmbeddedDocumentSaver::Private
{
public:
    Private() {}

    QHash<QString, int> prefixes; // Used in getFilename();

    // These will be saved when saveEmbeddedDocuments() is called.
    QList<KoDocumentBase*> documents; // Embedded documents
    QList<FileEntry*> files;    // Embedded files.
    QList<KoOdfManifestEntry*> manifestEntries;
};

KoEmbeddedDocumentSaver::KoEmbeddedDocumentSaver()
    : d(new Private())
{
}

KoEmbeddedDocumentSaver::~KoEmbeddedDocumentSaver()
{
    qDeleteAll(d->files);
    qDeleteAll(d->manifestEntries);
    delete d;
}


QString KoEmbeddedDocumentSaver::getFilename(const QString &prefix)
{
    int index = 1;
    if (d->prefixes.contains(prefix)) {
        index = d->prefixes.value(prefix);
    }

    // This inserts prefix into the map if it's not there.
    d->prefixes[prefix] = index + 1;

    //return prefix + QString("%1").arg(index, 4, 10, QChar('0'));
    return prefix + QString("%1").arg(index);
}

void KoEmbeddedDocumentSaver::embedDocument(KoXmlWriter &writer, KoDocumentBase * doc)
{
    Q_ASSERT(doc);
    d->documents.append(doc);

    QString ref;
    if (!doc->isStoredExtern()) {
        const QString name = getFilename("Object ");

        // set URL in document so that saveEmbeddedDocuments will save
        // the actual embedded object with the right name in the store.
        QUrl u;
        u.setScheme(INTERNAL_PROTOCOL);
        u.setPath(name);
        debugOdf << u;
        doc->setUrl(u);
        ref = "./" + name;
    } else {
        ref = doc->url().url();
    }

    debugOdf << "saving reference to embedded document as" << ref;
    writer.addAttribute("xlink:href", /*"#" + */ref);

    //<draw:object xlink:type="simple" xlink:show="embed"
    //    xlink:actuate="onLoad" xlink:href="#./Object 1"/>
    writer.addAttribute("xlink:type", "simple");
    writer.addAttribute("xlink:show", "embed");
    writer.addAttribute("xlink:actuate", "onLoad");

}

// Examples:
// Videos/Video1.mov  ← the number is autogenerated
// Videos/Video2.mov
// Object1/foo  ← the number is autogenerated
// Object1/bar

// Note: The contents QByteArray is implicitly shared.  It needs to be
//       copied since otherwise the actual array may disappear before
//       the real saving is done.
//
void KoEmbeddedDocumentSaver::embedFile(KoXmlWriter &writer, const char *element,
                                        const QString &path, const QByteArray &mimeType,
                                        const QByteArray &contents)
{
    // Put the file in the list of files to be written to the store later.
    FileEntry  *entry = new FileEntry;
    entry->mimeType = mimeType;
    entry->path = path;
    entry->contents = contents;
    d->files.append(entry);

    writer.startElement(element);
    // Write the attributes that refer to the file.

    //<draw:object xlink:href="#./Object 1" xlink:type="simple" xlink:show="embed"
    //             xlink:actuate="onLoad"/>
    writer.addAttribute("xlink:type", "simple");
    writer.addAttribute("xlink:show", "embed");
    writer.addAttribute("xlink:actuate", "onLoad");

    debugOdf << "saving reference to embedded file as" << path;
    writer.addAttribute("xlink:href", path);
    writer.endElement();
}

void KoEmbeddedDocumentSaver::saveFile(const QString &path, const QByteArray &mimeType,
                                       const QByteArray &contents)
{
    // Put the file in the list of files to be written to the store later.
    FileEntry  *entry = new FileEntry;
    entry->mimeType = mimeType;
    entry->path     = path;
    entry->contents = contents;
    d->files.append(entry);

    debugOdf << "saving reference to embedded file as" << path;
}

/**
 *
 */
void KoEmbeddedDocumentSaver::saveManifestEntry(const QString &fullPath, const QString &mediaType,
                                                const QString &version)
{
    d->manifestEntries.append(new KoOdfManifestEntry(fullPath, mediaType, version));
}


bool KoEmbeddedDocumentSaver::saveEmbeddedDocuments(KoDocumentBase::SavingContext & documentContext)
{
    KoStore *store = documentContext.odfStore.store();

    // Write embedded documents.
    foreach(KoDocumentBase *doc, d->documents) {
        QString path;
        if (doc->isStoredExtern()) {
            debugOdf << " external (don't save) url:" << doc->url().url();
            path = doc->url().url();
        } else {
            // The name comes from addEmbeddedDocument (which was set while saving the document).
            Q_ASSERT(doc->url().scheme() == INTERNAL_PROTOCOL);
            const QString name = doc->url().path();
            debugOdf << "saving" << name;

            if (doc->nativeOasisMimeType().isEmpty()) {
                // Embedded object doesn't support OpenDocument, save in the old format.
                debugOdf << "Embedded object doesn't support OpenDocument, save in the old format.";

                if (!doc->saveToStore(store, name)) {
                    return false;
                }
            } else {
                // To make the children happy cd to the correct directory
                store->pushDirectory();
                store->enterDirectory(name);

                bool ok = doc->saveOdf(documentContext);

                // Now that we're done leave the directory again
                store->popDirectory();

                if (!ok) {
                    warnOdf << "KoEmbeddedDocumentSaver::saveEmbeddedDocuments failed";
                    return false;
                }
            }

            Q_ASSERT(doc->url().scheme() == INTERNAL_PROTOCOL);
            path = store->currentPath();
            if (!path.isEmpty()) {
                path += '/';
            }
            path += doc->url().path();
            if (path.startsWith(QLatin1Char('/'))) {
                path.remove(0, 1);   // remove leading '/', no wanted in manifest
            }
        }

        // OOo uses a trailing slash for the path to embedded objects (== directories)
        if (!path.endsWith('/')) {
            path += '/';
        }
        QByteArray mimetype = doc->nativeOasisMimeType();
        documentContext.odfStore.manifestWriter()->addManifestEntry(path, mimetype);
    }

    // Write the embedded files.
    foreach(FileEntry *entry, d->files) {
        QString path = entry->path;
        debugOdf << "saving" << path;

        // To make the children happy cd to the correct directory
        store->pushDirectory();

        int index = path.lastIndexOf('/');
        const QString dirPath = path.left(index);
        const QString fileName = path.right(path.size() - index - 1);
        store->enterDirectory(dirPath);

        if (!store->open(fileName)) {
            return false;
        }
        store->write(entry->contents);
        store->close();

        // Now that we're done leave the directory again
        store->popDirectory();

        // Create the manifest entry.
        if (path.startsWith(QLatin1String("./"))) {
            path.remove(0, 2);   // remove leading './', not wanted in manifest
        }
        documentContext.odfStore.manifestWriter()->addManifestEntry(path, entry->mimeType);
    }

    // Write the manifest entries.
    KoXmlWriter *manifestWriter = documentContext.odfStore.manifestWriter();
    foreach(KoOdfManifestEntry *entry, d->manifestEntries) {
        manifestWriter->startElement("manifest:file-entry");
        manifestWriter->addAttribute("manifest:version", entry->version());
        manifestWriter->addAttribute("manifest:media-type", entry->mediaType());
        manifestWriter->addAttribute("manifest:full-path", entry->fullPath());
        manifestWriter->endElement(); // manifest:file-entry
    }

    return true;
}