~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/store/KoTarStore.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2000-2002 David Faure <faure@kde.org>
 
3
   Copyright (C) 2010 C. Boemann <cbo@boemann.dk>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License as published by the Free Software Foundation; either
 
8
   version 2 of the License, or (at your option) any later version.
 
9
 
 
10
   This library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public License
 
16
   along with this library; see the file COPYING.LIB.  If not, write to
 
17
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "KoTarStore.h"
 
22
#include "KoStore_p.h"
 
23
 
 
24
#include <QBuffer>
 
25
 
 
26
#include <QByteArray>
 
27
 
 
28
#include <ktar.h>
 
29
#include <StoreDebug.h>
 
30
#include <QUrl>
 
31
 
 
32
#include <KoNetAccess.h>
 
33
 
 
34
KoTarStore::KoTarStore(const QString & _filename, Mode mode, const QByteArray & appIdentification,
 
35
                       bool writeMimetype)
 
36
 : KoStore(mode, writeMimetype)
 
37
{
 
38
    debugStore << "KoTarStore Constructor filename =" << _filename
 
39
    << " mode = " << int(mode) << endl;
 
40
    Q_D(KoStore);
 
41
 
 
42
    d->localFileName = _filename;
 
43
 
 
44
    m_pTar = new KTar(_filename, "application/x-gzip");
 
45
 
 
46
    init(appIdentification);   // open the targz file and init some vars
 
47
}
 
48
 
 
49
KoTarStore::KoTarStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification,
 
50
                       bool writeMimetype)
 
51
 : KoStore(mode, writeMimetype)
 
52
{
 
53
    m_pTar = new KTar(dev);
 
54
 
 
55
    init(appIdentification);
 
56
}
 
57
 
 
58
KoTarStore::KoTarStore(QWidget* window, const QUrl &_url, const QString & _filename, Mode mode,
 
59
                       const QByteArray & appIdentification, bool writeMimetype)
 
60
 : KoStore(mode, writeMimetype)
 
61
{
 
62
    debugStore << "KoTarStore Constructor url=" << _url.url(QUrl::PreferLocalFile)
 
63
                  << " filename = " << _filename
 
64
                  << " mode = " << int(mode) << endl;
 
65
    Q_D(KoStore);
 
66
 
 
67
    d->url = _url;
 
68
    d->window = window;
 
69
 
 
70
    if (mode == KoStore::Read) {
 
71
        d->fileMode = KoStorePrivate::RemoteRead;
 
72
        d->localFileName = _filename;
 
73
 
 
74
    } else {
 
75
        d->fileMode = KoStorePrivate::RemoteWrite;
 
76
        d->localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
 
77
    }
 
78
 
 
79
    m_pTar = new KTar(d->localFileName, "application/x-gzip");
 
80
 
 
81
    init(appIdentification);   // open the targz file and init some vars
 
82
}
 
83
 
 
84
KoTarStore::~KoTarStore()
 
85
{
 
86
    Q_D(KoStore);
 
87
    if (!d->finalized)
 
88
        finalize(); // ### no error checking when the app forgot to call finalize itself
 
89
    delete m_pTar;
 
90
 
 
91
    // Now we have still some job to do for remote files.
 
92
    if (d->fileMode == KoStorePrivate::RemoteRead) {
 
93
        KIO::NetAccess::removeTempFile(d->localFileName);
 
94
    } else if (d->fileMode == KoStorePrivate::RemoteWrite) {
 
95
        KIO::NetAccess::upload(d->localFileName, d->url, d->window);
 
96
        // ### FIXME: delete temp file
 
97
    }
 
98
}
 
99
 
 
100
QStringList KoTarStore::directoryList() const
 
101
{
 
102
    QStringList retval;
 
103
    const KArchiveDirectory *directory = m_pTar->directory();
 
104
    foreach(const QString &name, directory->entries()) {
 
105
        const KArchiveEntry* fileArchiveEntry = m_pTar->directory()->entry(name);
 
106
        if (fileArchiveEntry->isDirectory()) {
 
107
            retval << name;
 
108
        }
 
109
    }
 
110
    return retval;
 
111
}
 
112
 
 
113
QByteArray KoTarStore::completeMagic(const QByteArray& appMimetype)
 
114
{
 
115
    debugStore << "QCString KoTarStore::completeMagic( const QCString& appMimetype )********************";
 
116
    QByteArray res("Calligra ");
 
117
    res += appMimetype;
 
118
    res += '\004'; // Two magic bytes to make the identification
 
119
    res += '\006'; // more reliable (DF)
 
120
    debugStore << "sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
 
121
    debugStore << " return :!!!!!!!!!!!!!!! :" << res;
 
122
    return res;
 
123
}
 
124
 
 
125
void KoTarStore::init(const QByteArray &appIdentification)
 
126
{
 
127
    Q_D(KoStore);
 
128
    m_currentDir = 0;
 
129
    d->good = m_pTar->open(d->mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
 
130
 
 
131
    if (!d->good)
 
132
        return;
 
133
 
 
134
    if (d->mode == Write) {
 
135
        debugStore << "appIdentification :" << appIdentification;
 
136
        m_pTar->setOrigFileName(completeMagic(appIdentification));
 
137
    } else {
 
138
        d->good = m_pTar->directory() != 0;
 
139
    }
 
140
}
 
141
 
 
142
bool KoTarStore::doFinalize()
 
143
{
 
144
    return m_pTar->close();
 
145
}
 
146
 
 
147
// When reading, d->stream comes directly from KArchiveFile::device()
 
148
// When writing, d->stream buffers the data into m_byteArray
 
149
 
 
150
bool KoTarStore::openWrite(const QString& /*name*/)
 
151
{
 
152
    Q_D(KoStore);
 
153
    // Prepare memory buffer for writing
 
154
    m_byteArray.resize(0);
 
155
    d->stream = new QBuffer(&m_byteArray);
 
156
    d->stream->open(QIODevice::WriteOnly);
 
157
    return true;
 
158
}
 
159
 
 
160
bool KoTarStore::openRead(const QString& name)
 
161
{
 
162
    Q_D(KoStore);
 
163
    const KArchiveEntry * entry = m_pTar->directory()->entry(name);
 
164
    if (entry == 0) {
 
165
        return false;
 
166
    }
 
167
    if (entry->isDirectory()) {
 
168
        warnStore << name << " is a directory !";
 
169
        return false;
 
170
    }
 
171
    KArchiveFile * f = (KArchiveFile *) entry;
 
172
    m_byteArray.resize(0);
 
173
    delete d->stream;
 
174
    d->stream = f->createDevice();
 
175
    d->size = f->size();
 
176
    return true;
 
177
}
 
178
 
 
179
bool KoTarStore::closeWrite()
 
180
{
 
181
    Q_D(KoStore);
 
182
    // write the whole bytearray at once into the tar file
 
183
 
 
184
    debugStore << "Writing file" << d->fileName << " into TAR archive. size" << d->size;
 
185
    m_byteArray.resize(d->size); // TODO: check if really needed
 
186
    if (!m_pTar->writeFile(d->fileName, m_byteArray, 0100644, QLatin1String("user"), QLatin1String("group")))
 
187
        warnStore << "Failed to write " << d->fileName;
 
188
    m_byteArray.resize(0);   // save memory
 
189
    return true;
 
190
}
 
191
 
 
192
bool KoTarStore::enterRelativeDirectory(const QString& dirName)
 
193
{
 
194
    Q_D(KoStore);
 
195
    if (d->mode == Read) {
 
196
        if (!m_currentDir) {
 
197
            m_currentDir = m_pTar->directory(); // initialize
 
198
            Q_ASSERT(d->currentPath.isEmpty());
 
199
        }
 
200
        const KArchiveEntry *entry = m_currentDir->entry(dirName);
 
201
        if (entry && entry->isDirectory()) {
 
202
            m_currentDir = dynamic_cast<const KArchiveDirectory*>(entry);
 
203
            return m_currentDir != 0;
 
204
        }
 
205
        return false;
 
206
    } else // Write, no checking here
 
207
        return true;
 
208
}
 
209
 
 
210
bool KoTarStore::enterAbsoluteDirectory(const QString& path)
 
211
{
 
212
    Q_D(KoStore);
 
213
    if (path.isEmpty()) {
 
214
        m_currentDir = 0;
 
215
        return true;
 
216
    }
 
217
    if (d->mode == Read) {
 
218
        m_currentDir = dynamic_cast<const KArchiveDirectory*>(m_pTar->directory()->entry(path));
 
219
        Q_ASSERT(m_currentDir);
 
220
        return m_currentDir != 0;
 
221
    } else
 
222
        return true;
 
223
}
 
224
 
 
225
bool KoTarStore::fileExists(const QString& absPath) const
 
226
{
 
227
    return m_pTar->directory()->entry(absPath) != 0;
 
228
}