2011-02-21 16:30:31 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
2013-02-05 10:31:52 +01:00
|
|
|
** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies).
|
2012-12-21 10:31:17 +01:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
2012-12-21 10:31:17 +01:00
|
|
|
** This file is part of the Qt Installer Framework.
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
2012-12-21 10:31:17 +01:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-12-21 10:31:17 +01:00
|
|
|
** Alternatively, 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.
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
2012-12-21 10:31:17 +01:00
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2012-02-06 09:52:58 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2012-12-21 10:31:17 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
2012-02-06 09:52:58 +01:00
|
|
|
**
|
2012-12-21 10:31:17 +01:00
|
|
|
** $QT_END_LICENSE$
|
2011-02-21 16:30:31 +01:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
2012-02-06 09:52:58 +01:00
|
|
|
|
2011-02-21 16:30:31 +01:00
|
|
|
#include "binaryformatengine.h"
|
|
|
|
|
|
|
|
using namespace QInstallerCreator;
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
namespace {
|
|
|
|
|
2011-02-21 16:30:31 +01:00
|
|
|
class StringListIterator : public QAbstractFileEngineIterator
|
|
|
|
{
|
|
|
|
public:
|
2011-11-17 22:12:18 +01:00
|
|
|
StringListIterator( const QStringList &list, QDir::Filters filters, const QStringList &nameFilters)
|
2013-04-23 19:20:31 +02:00
|
|
|
: QAbstractFileEngineIterator(filters, nameFilters)
|
|
|
|
, list(list)
|
|
|
|
, index(-1)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasNext() const
|
|
|
|
{
|
|
|
|
return index < list.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString next()
|
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
if(!hasNext())
|
2011-02-21 16:30:31 +01:00
|
|
|
return QString();
|
|
|
|
++index;
|
|
|
|
return currentFilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString currentFileName() const
|
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
return index < 0 ? QString() : list[index];
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QStringList list;
|
|
|
|
int index;
|
|
|
|
};
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
BinaryFormatEngine::BinaryFormatEngine(const ComponentIndex &index, const QString &fileName)
|
|
|
|
: m_index(index)
|
|
|
|
, m_hasComponent(false)
|
|
|
|
, m_hasArchive(false)
|
|
|
|
, m_archive(0)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
setArchive(fileName);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryFormatEngine::~BinaryFormatEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
void BinaryFormatEngine::setArchive(const QString &file)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
|
|
|
m_fileNamePath = file;
|
|
|
|
|
2011-11-19 13:01:27 +01:00
|
|
|
static const QChar sep = QLatin1Char('/');
|
|
|
|
static const QString prefix = QLatin1String("installer://");
|
2011-11-17 22:12:18 +01:00
|
|
|
Q_ASSERT(file.toLower().startsWith(prefix));
|
2011-02-21 16:30:31 +01:00
|
|
|
|
|
|
|
// cut the prefix
|
2011-11-17 22:12:18 +01:00
|
|
|
QString path = file.mid(prefix.length());
|
|
|
|
while (path.endsWith(sep))
|
|
|
|
path.chop(1);
|
2011-02-21 16:30:31 +01:00
|
|
|
|
|
|
|
QString arch;
|
2011-11-17 22:12:18 +01:00
|
|
|
const QString comp = path.section(sep, 0, 0);
|
2011-02-21 16:30:31 +01:00
|
|
|
m_hasComponent = !comp.isEmpty();
|
2011-11-17 22:12:18 +01:00
|
|
|
m_hasArchive = path.contains(sep);
|
|
|
|
if (m_hasArchive)
|
|
|
|
arch = path.section(sep, 1, 1);
|
2011-02-21 16:30:31 +01:00
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
m_component = m_index.componentByName(comp.toUtf8());
|
|
|
|
m_archive = m_component.archiveByName(arch.toUtf8());
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
void BinaryFormatEngine::setFileName(const QString &file)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
setArchive(file);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
|
|
|
bool BinaryFormatEngine::close()
|
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
if (m_archive == 0)
|
2011-02-21 16:30:31 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const bool result = m_archive->isOpen();
|
|
|
|
m_archive->close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
bool BinaryFormatEngine::open(QIODevice::OpenMode mode)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
return m_archive == 0 ? false : m_archive->open(mode);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
|
|
|
qint64 BinaryFormatEngine::pos() const
|
|
|
|
{
|
|
|
|
return m_archive == 0 ? 0 : m_archive->pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
qint64 BinaryFormatEngine::read(char *data, qint64 maxlen)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
return m_archive == 0 ? -1 : m_archive->read(data, maxlen);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
bool BinaryFormatEngine::seek(qint64 offset)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
return m_archive == 0 ? false : m_archive->seek(offset);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
QString BinaryFormatEngine::fileName(FileName file) const
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
switch(file) {
|
2011-02-21 16:30:31 +01:00
|
|
|
case BaseName:
|
2011-11-17 22:12:18 +01:00
|
|
|
return m_fileNamePath.section(QChar::fromLatin1('/'), -1, -1, QString::SectionSkipEmpty);
|
2011-02-21 16:30:31 +01:00
|
|
|
case PathName:
|
|
|
|
case AbsolutePathName:
|
|
|
|
case CanonicalPathName:
|
2011-11-17 22:12:18 +01:00
|
|
|
return m_fileNamePath.section(QChar::fromLatin1('/'), 0, -2, QString::SectionSkipEmpty);
|
2011-02-21 16:30:31 +01:00
|
|
|
case DefaultName:
|
|
|
|
case AbsoluteName:
|
|
|
|
case CanonicalName:
|
|
|
|
return m_fileNamePath;
|
|
|
|
default:
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
bool BinaryFormatEngine::copy(const QString &newName)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
if (QFile::exists(newName))
|
2011-02-21 16:30:31 +01:00
|
|
|
return false;
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
QFile target(newName);
|
|
|
|
if (!target.open(QIODevice::WriteOnly))
|
2011-02-21 16:30:31 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
qint64 bytesLeft = size();
|
2011-11-17 22:12:18 +01:00
|
|
|
if (!open(QIODevice::ReadOnly))
|
2011-02-21 16:30:31 +01:00
|
|
|
return false;
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
char data[4096];
|
|
|
|
while(bytesLeft > 0) {
|
|
|
|
const qint64 len = qMin<qint64>(bytesLeft, 4096);
|
|
|
|
const qint64 bytesRead = read(data, len);
|
|
|
|
if (bytesRead != len) {
|
2011-02-21 16:30:31 +01:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-17 22:12:18 +01:00
|
|
|
const qint64 bytesWritten = target.write(data, len);
|
|
|
|
if (bytesWritten != len) {
|
2011-02-21 16:30:31 +01:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bytesLeft -= len;
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
QAbstractFileEngine::FileFlags BinaryFormatEngine::fileFlags(FileFlags type) const
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
|
|
|
FileFlags result;
|
2011-11-17 22:12:18 +01:00
|
|
|
if ((type & FileType) && m_archive != 0)
|
2011-02-21 16:30:31 +01:00
|
|
|
result |= FileType;
|
2011-11-17 22:12:18 +01:00
|
|
|
if ((type & DirectoryType) && !m_hasArchive)
|
2011-02-21 16:30:31 +01:00
|
|
|
result |= DirectoryType;
|
2011-11-17 22:12:18 +01:00
|
|
|
if ((type & ExistsFlag) && m_hasArchive && m_archive != 0)
|
2011-02-21 16:30:31 +01:00
|
|
|
result |= ExistsFlag;
|
2011-11-17 22:12:18 +01:00
|
|
|
if ((type & ExistsFlag) && !m_hasArchive && !m_component.name().isEmpty())
|
2011-02-21 16:30:31 +01:00
|
|
|
result |= ExistsFlag;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
QAbstractFileEngineIterator *BinaryFormatEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
const QStringList entries = entryList(filters, filterNames);
|
|
|
|
return new StringListIterator(entries, filters, filterNames);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
2011-11-17 22:12:18 +01:00
|
|
|
QStringList BinaryFormatEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
|
2011-02-21 16:30:31 +01:00
|
|
|
{
|
2011-11-17 22:12:18 +01:00
|
|
|
if (m_hasArchive)
|
2011-02-21 16:30:31 +01:00
|
|
|
return QStringList();
|
|
|
|
|
|
|
|
QStringList result;
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
if (m_hasComponent && (filters & QDir::Files)) {
|
2011-02-21 16:30:31 +01:00
|
|
|
const QVector< QSharedPointer<Archive> > archives = m_component.archives();
|
2011-11-17 22:12:18 +01:00
|
|
|
foreach (const QSharedPointer<Archive> &i, archives)
|
|
|
|
result.push_back(QString::fromUtf8(i->name()));
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
2011-11-17 22:12:18 +01:00
|
|
|
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()));
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
if (filterNames.isEmpty())
|
2011-02-21 16:30:31 +01:00
|
|
|
return result;
|
|
|
|
|
2011-11-17 22:12:18 +01:00
|
|
|
QList<QRegExp> regexps;
|
|
|
|
foreach (const QString &i, filterNames)
|
|
|
|
regexps.push_back(QRegExp(i, Qt::CaseInsensitive, QRegExp::Wildcard));
|
2011-02-21 16:30:31 +01:00
|
|
|
|
|
|
|
QStringList entries;
|
2011-11-17 22:12:18 +01:00
|
|
|
foreach (const QString &i, result) {
|
2011-02-21 16:30:31 +01:00
|
|
|
bool matched = false;
|
2011-11-17 22:12:18 +01:00
|
|
|
foreach (const QRegExp ®, regexps) {
|
|
|
|
matched = reg.exactMatch(i);
|
|
|
|
if (matched)
|
2011-02-21 16:30:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-11-17 22:12:18 +01:00
|
|
|
if (matched)
|
|
|
|
entries.push_back(i);
|
2011-02-21 16:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \reimp
|
|
|
|
*/
|
|
|
|
qint64 BinaryFormatEngine::size() const
|
|
|
|
{
|
|
|
|
return m_archive == 0 ? 0 : m_archive->size();
|
|
|
|
}
|