installer-framework/src/libs/installer/binaryformatengine.cpp

292 lines
7.6 KiB
C++
Raw Normal View History

2011-02-21 16:30:31 +01:00
/**************************************************************************
**
** Copyright (C) 2012-2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2011-02-21 16:30:31 +01:00
**
** This file is part of the Qt Installer Framework.
2011-02-21 16:30:31 +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
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2011-02-21 16:30:31 +01:00
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
**
** $QT_END_LICENSE$
2011-02-21 16:30:31 +01:00
**
**************************************************************************/
2011-02-21 16:30:31 +01:00
#include "binaryformatengine.h"
namespace {
2011-02-21 16:30:31 +01:00
class StringListIterator : public QAbstractFileEngineIterator
{
public:
StringListIterator( const QStringList &list, QDir::Filters filters, const QStringList &nameFilters)
: 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()
{
if(!hasNext())
2011-02-21 16:30:31 +01:00
return QString();
++index;
return currentFilePath();
}
QString currentFileName() const
{
return index < 0 ? QString() : list[index];
2011-02-21 16:30:31 +01:00
}
private:
const QStringList list;
int index;
};
} // anon namespace
namespace QInstaller {
/*!
\class QInstaller::BinaryFormatEngine
\inmodule QtInstallerFramework
\brief The BinaryFormatEngine class is the default file engine for accessing resource
collections and resource files.
*/
BinaryFormatEngine::BinaryFormatEngine(const QHash<QByteArray, ResourceCollection> &collections,
const QString &fileName)
: m_resource(0)
, m_collections(collections)
2011-02-21 16:30:31 +01:00
{
setFileName(fileName);
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
void BinaryFormatEngine::setFileName(const QString &file)
2011-02-21 16:30:31 +01:00
{
m_fileNamePath = file;
static const QChar sep = QLatin1Char('/');
static const QString prefix = QLatin1String("installer://");
Q_ASSERT(file.toLower().startsWith(prefix));
2011-02-21 16:30:31 +01:00
// cut the prefix
QString path = file.mid(prefix.length());
while (path.endsWith(sep))
path.chop(1);
2011-02-21 16:30:31 +01:00
m_collection = m_collections.value(path.section(sep, 0, 0).toUtf8());
m_collection.setName(path.section(sep, 0, 0).toUtf8());
m_resource = m_collection.resourceByName(path.section(sep, 1, 1).toUtf8());
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
2011-02-21 16:30:31 +01:00
bool BinaryFormatEngine::close()
{
if (m_resource.isNull())
2011-02-21 16:30:31 +01:00
return false;
const bool result = m_resource->isOpen();
m_resource->close();
2011-02-21 16:30:31 +01:00
return result;
}
/*!
\internal
*/
bool BinaryFormatEngine::open(QIODevice::OpenMode mode)
2011-02-21 16:30:31 +01:00
{
Q_UNUSED(mode)
return m_resource.isNull() ? false : m_resource->open();
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
2011-02-21 16:30:31 +01:00
qint64 BinaryFormatEngine::pos() const
{
return m_resource.isNull() ? 0 : m_resource->pos();
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
qint64 BinaryFormatEngine::read(char *data, qint64 maxlen)
2011-02-21 16:30:31 +01:00
{
return m_resource.isNull() ? -1 : m_resource->read(data, maxlen);
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
bool BinaryFormatEngine::seek(qint64 offset)
2011-02-21 16:30:31 +01:00
{
return m_resource.isNull() ? false : m_resource->seek(offset);
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
QString BinaryFormatEngine::fileName(FileName file) const
2011-02-21 16:30:31 +01:00
{
static const QChar sep = QLatin1Char('/');
switch(file) {
case BaseName:
return m_fileNamePath.section(sep, -1, -1, QString::SectionSkipEmpty);
case PathName:
case AbsolutePathName:
case CanonicalPathName:
return m_fileNamePath.section(sep, 0, -2, QString::SectionSkipEmpty);
case DefaultName:
case AbsoluteName:
case CanonicalName:
return m_fileNamePath;
default:
return QString();
2011-02-21 16:30:31 +01:00
}
}
/*!
\internal
*/
bool BinaryFormatEngine::copy(const QString &newName)
2011-02-21 16:30:31 +01:00
{
if (QFile::exists(newName))
2011-02-21 16:30:31 +01:00
return false;
QFile target(newName);
if (!target.open(QIODevice::WriteOnly))
2011-02-21 16:30:31 +01:00
return false;
qint64 bytesLeft = size();
if (!open(QIODevice::ReadOnly))
2011-02-21 16:30:31 +01:00
return false;
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;
}
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;
}
/*!
\internal
*/
QAbstractFileEngine::FileFlags BinaryFormatEngine::fileFlags(FileFlags type) const
2011-02-21 16:30:31 +01:00
{
FileFlags result;
if ((type & FileType) && (!m_resource.isNull()))
2011-02-21 16:30:31 +01:00
result |= FileType;
if ((type & DirectoryType) && m_resource.isNull())
2011-02-21 16:30:31 +01:00
result |= DirectoryType;
if ((type & ExistsFlag) && (!m_resource.isNull()))
2011-02-21 16:30:31 +01:00
result |= ExistsFlag;
if ((type & ExistsFlag) && m_resource.isNull() && (!m_collection.name().isEmpty()))
2011-02-21 16:30:31 +01:00
result |= ExistsFlag;
return result;
}
/*!
\internal
*/
QAbstractFileEngineIterator *BinaryFormatEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
2011-02-21 16:30:31 +01:00
{
const QStringList entries = entryList(filters, filterNames);
return new StringListIterator(entries, filters, filterNames);
2011-02-21 16:30:31 +01:00
}
/*!
\internal
*/
QStringList BinaryFormatEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
2011-02-21 16:30:31 +01:00
{
if (!m_resource.isNull())
2011-02-21 16:30:31 +01:00
return QStringList();
QStringList result;
if ((!m_collection.name().isEmpty()) && (filters & QDir::Files)) {
foreach (const QSharedPointer<Resource> &resource, m_collection.resources())
result.append(QString::fromUtf8(resource->name()));
} else if (m_collection.name().isEmpty() && (filters & QDir::Dirs)) {
foreach (const ResourceCollection &collection, m_collections)
result.append(QString::fromUtf8(collection.name()));
2011-02-21 16:30:31 +01:00
}
result.removeAll(QString()); // Remove empty names, will crash while using directory iterator.
2011-02-21 16:30:31 +01:00
if (filterNames.isEmpty())
2011-02-21 16:30:31 +01:00
return result;
QList<QRegExp> regexps;
foreach (const QString &i, filterNames)
regexps.append(QRegExp(i, Qt::CaseInsensitive, QRegExp::Wildcard));
2011-02-21 16:30:31 +01:00
QStringList entries;
foreach (const QString &i, result) {
2011-02-21 16:30:31 +01:00
bool matched = false;
foreach (const QRegExp &reg, regexps) {
matched = reg.exactMatch(i);
if (matched)
2011-02-21 16:30:31 +01:00
break;
}
if (matched)
entries.append(i);
2011-02-21 16:30:31 +01:00
}
return entries;
}
/*!
\internal
*/
2011-02-21 16:30:31 +01:00
qint64 BinaryFormatEngine::size() const
{
return m_resource.isNull() ? 0 : m_resource->size();
2011-02-21 16:30:31 +01:00
}
} // namespace QInstaller