summaryrefslogtreecommitdiffstats
path: root/installerbuilder/common/binaryformatengine.cpp
blob: e582879080c8caa0fd7d82e3853cf1be34d72ad7 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**************************************************************************
**
** This file is part of Installer Framework**
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).*
**
** Contact:  Nokia Corporation qt-info@nokia.com**
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception version
** 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you are unsure which license is appropriate for your use, please contact
** (qt-info@nokia.com).
**
**************************************************************************/
#include "binaryformatengine.h"

using namespace QInstallerCreator;

namespace {

class StringListIterator : public QAbstractFileEngineIterator
{
public:
    StringListIterator( const QStringList &list, QDir::Filters filters, const QStringList &nameFilters)
        : QAbstractFileEngineIterator(filters, nameFilters),
          list(list),
          index(-1)
    {
    }

    bool hasNext() const
    {
        return index < list.size() - 1;
    }

    QString next()
    {
        if(!hasNext())
            return QString();
        ++index;
        return currentFilePath();
    }

    QString currentFileName() const
    {
        return index < 0 ? QString() : list[index];
    }

private:
    const QStringList list;
    int index;
};

} // anon namespace

BinaryFormatEngine::BinaryFormatEngine(const ComponentIndex &index, const QString &fileName)
    : m_index(index)
    , m_hasComponent(false)
    , m_hasArchive(false)
    , m_archive(0)
{
    setArchive(fileName);
}

BinaryFormatEngine::~BinaryFormatEngine()
{
}

void BinaryFormatEngine::setArchive(const QString &file)
{
    m_fileNamePath = file;

    static const QChar sep = QLatin1Char('/');
    static const QString prefix = QLatin1String("installer://");
    Q_ASSERT(file.toLower().startsWith(prefix));

    // cut the prefix
    QString path = file.mid(prefix.length());
    while (path.endsWith(sep))
        path.chop(1);

    QString arch;
    const QString comp = path.section(sep, 0, 0);
    m_hasComponent = !comp.isEmpty();
    m_hasArchive = path.contains(sep);
    if (m_hasArchive)
        arch = path.section(sep, 1, 1);

    m_component = m_index.componentByName(comp.toUtf8());
    m_archive = m_component.archiveByName(arch.toUtf8());
}

/**
 * \reimp
 */
void BinaryFormatEngine::setFileName(const QString &file)
{
    setArchive(file);
}

/**
 * \reimp
 */
bool BinaryFormatEngine::close()
{
    if (m_archive == 0)
        return false;

    const bool result = m_archive->isOpen();
    m_archive->close();
    return result;
}

/**
 * \reimp
 */
bool BinaryFormatEngine::open(QIODevice::OpenMode mode)
{
    return m_archive == 0 ? false : m_archive->open(mode);
}

/**
 * \reimp
 */
qint64 BinaryFormatEngine::pos() const
{
    return m_archive == 0 ? 0 : m_archive->pos();
}

/**
 * \reimp
 */
qint64 BinaryFormatEngine::read(char *data, qint64 maxlen)
{
    return m_archive == 0 ? -1 : m_archive->read(data, maxlen);
}

/**
 * \reimp
 */
bool BinaryFormatEngine::seek(qint64 offset)
{
    return m_archive == 0 ? false : m_archive->seek(offset);
}

/**
 * \reimp
 */
QString BinaryFormatEngine::fileName(FileName file) const
{
    switch(file) {
    case BaseName:
        return m_fileNamePath.section(QChar::fromLatin1('/'), -1, -1, QString::SectionSkipEmpty);
    case PathName:
    case AbsolutePathName:
    case CanonicalPathName:
        return m_fileNamePath.section(QChar::fromLatin1('/'), 0, -2, QString::SectionSkipEmpty);
    case DefaultName:
    case AbsoluteName:
    case CanonicalName:
        return m_fileNamePath;
    default:
        return QString();
    }
}

/**
 * \reimp
 */
bool BinaryFormatEngine::copy(const QString &newName)
{
    if (QFile::exists(newName))
        return false;

    QFile target(newName);
    if (!target.open(QIODevice::WriteOnly))
        return false;

    qint64 bytesLeft = size();
    if (!open(QIODevice::ReadOnly))
        return false;

    char data[4096];
    while(bytesLeft > 0) {
        const qint64 len = qMin<qint64>(bytesLeft, 4096);
        const qint64 bytesRead = read(data, len);
        if (bytesRead != len) {
            close();
            return false;
        }
        const qint64 bytesWritten = target.write(data, len);
        if (bytesWritten != len) {
            close();
            return false;
        }
        bytesLeft -= len;
    }
    close();

    return true;
}

/**
 * \reimp
 */
QAbstractFileEngine::FileFlags BinaryFormatEngine::fileFlags(FileFlags type) const
{
    FileFlags result;
    if ((type & FileType) && m_archive != 0)
        result |= FileType;
    if ((type & DirectoryType) && !m_hasArchive)
        result |= DirectoryType;
    if ((type & ExistsFlag) && m_hasArchive && m_archive != 0)
        result |= ExistsFlag;
    if ((type & ExistsFlag) && !m_hasArchive && !m_component.name().isEmpty())
        result |= ExistsFlag;

    return result;
}

/**
 * \reimp
 */
QAbstractFileEngineIterator *BinaryFormatEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
{
    const QStringList entries = entryList(filters, filterNames);
    return new StringListIterator(entries, filters, filterNames);
}

/**
 * \reimp
 */
QStringList BinaryFormatEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
{
    if (m_hasArchive)
        return QStringList();
    
    QStringList result;

    if (m_hasComponent && (filters & QDir::Files)) {
        const QVector< QSharedPointer<Archive> > archives = m_component.archives();
        foreach (const QSharedPointer<Archive> &i, archives)
            result.push_back(QString::fromUtf8(i->name()));
    }
    else if (!m_hasComponent && (filters & QDir::Dirs)) {
        const QVector<Component> components = m_index.components();
        foreach (const Component &i, components)
            result.push_back(QString::fromUtf8(i.name()));
    }

    if (filterNames.isEmpty())
        return result;

    QList<QRegExp> regexps;
    foreach (const QString &i, filterNames)
        regexps.push_back(QRegExp(i, Qt::CaseInsensitive, QRegExp::Wildcard));

    QStringList entries;
    foreach (const QString &i, result) {
        bool matched = false;
        foreach (const QRegExp &reg, regexps) {
            matched = reg.exactMatch(i);
            if (matched)
                break;
        }
        if (matched)
            entries.push_back(i);
    }

    return entries;
}
 
/**
 * \reimp
 */
qint64 BinaryFormatEngine::size() const
{
    return m_archive == 0 ? 0 : m_archive->size();
}