2015-03-10 00:13:55 +00:00
|
|
|
// AircraftModel.cxx - part of GUI launcher using Qt5
|
|
|
|
//
|
|
|
|
// Written by James Turner, started March 2015.
|
|
|
|
//
|
2015-03-13 16:29:20 +00:00
|
|
|
// Copyright (C) 2015 James Turner <zakalawe@mac.com>
|
2015-03-10 00:13:55 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#include "AircraftModel.hxx"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QDebug>
|
2015-08-03 20:53:56 +00:00
|
|
|
#include <QSharedPointer>
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
// Simgear
|
|
|
|
#include <simgear/props/props_io.hxx>
|
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2015-03-13 16:29:20 +00:00
|
|
|
#include <simgear/package/Package.hxx>
|
|
|
|
#include <simgear/package/Catalog.hxx>
|
|
|
|
#include <simgear/package/Install.hxx>
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
// FlightGear
|
|
|
|
#include <Main/globals.hxx>
|
2017-06-22 09:45:47 +00:00
|
|
|
#include <Include/version.h>
|
2015-03-10 00:13:55 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
const int STANDARD_THUMBNAIL_HEIGHT = 128;
|
2015-11-10 23:28:24 +00:00
|
|
|
const int STANDARD_THUMBNAIL_WIDTH = 172;
|
2017-06-22 09:45:47 +00:00
|
|
|
static quint32 CACHE_VERSION = 8;
|
2015-08-03 20:53:56 +00:00
|
|
|
|
2015-03-13 16:29:20 +00:00
|
|
|
using namespace simgear::pkg;
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
AircraftItem::AircraftItem()
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
AircraftItem::AircraftItem(QDir dir, QString filePath)
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
|
|
|
SGPropertyNode root;
|
|
|
|
readProperties(filePath.toStdString(), &root);
|
|
|
|
|
|
|
|
if (!root.hasChild("sim")) {
|
|
|
|
throw sg_io_exception(std::string("Malformed -set.xml file"), filePath.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
SGPropertyNode_ptr sim = root.getNode("sim");
|
|
|
|
|
|
|
|
path = filePath;
|
|
|
|
pathModTime = QFileInfo(path).lastModified();
|
2015-03-16 16:34:22 +00:00
|
|
|
if (sim->getBoolValue("exclude-from-gui", false)) {
|
|
|
|
excluded = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
description = sim->getStringValue("description");
|
|
|
|
authors = sim->getStringValue("author");
|
|
|
|
|
|
|
|
if (sim->hasChild("rating")) {
|
|
|
|
SGPropertyNode_ptr ratingsNode = sim->getNode("rating");
|
|
|
|
ratings[0] = ratingsNode->getIntValue("FDM");
|
|
|
|
ratings[1] = ratingsNode->getIntValue("systems");
|
|
|
|
ratings[2] = ratingsNode->getIntValue("cockpit");
|
|
|
|
ratings[3] = ratingsNode->getIntValue("model");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-15 16:21:49 +00:00
|
|
|
if (sim->hasChild("long-description")) {
|
|
|
|
// clean up any XML whitspace in the text.
|
|
|
|
longDescription = QString(sim->getStringValue("long-description")).simplified();
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
if (sim->hasChild("variant-of")) {
|
|
|
|
variantOf = sim->getStringValue("variant-of");
|
2017-03-05 20:01:54 +00:00
|
|
|
} else {
|
|
|
|
isPrimary = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sim->hasChild("primary-set")) {
|
|
|
|
isPrimary = sim->getBoolValue("primary-set");
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
2015-11-17 07:36:54 +00:00
|
|
|
|
|
|
|
if (sim->hasChild("tags")) {
|
|
|
|
SGPropertyNode_ptr tagsNode = sim->getChild("tags");
|
|
|
|
int nChildren = tagsNode->nChildren();
|
|
|
|
for (int i = 0; i < nChildren; i++) {
|
|
|
|
const SGPropertyNode* c = tagsNode->getChild(i);
|
|
|
|
if (strcmp(c->getName(), "tag") == 0) {
|
|
|
|
const char* tagName = c->getStringValue();
|
|
|
|
usesHeliports |= (strcmp(tagName, "helicopter") == 0);
|
|
|
|
// could also consider vtol tag?
|
|
|
|
usesSeaports |= (strcmp(tagName, "seaplane") == 0);
|
|
|
|
usesSeaports |= (strcmp(tagName, "floats") == 0);
|
2017-06-22 09:45:47 +00:00
|
|
|
|
|
|
|
needsMaintenance |= (strcmp(tagName, "needs-maintenance") == 0);
|
2015-11-17 07:36:54 +00:00
|
|
|
}
|
|
|
|
} // of tags iteration
|
|
|
|
} // of set-xml has tags
|
2017-02-06 14:09:07 +00:00
|
|
|
|
|
|
|
if (sim->hasChild("previews")) {
|
|
|
|
SGPropertyNode_ptr previewsNode = sim->getChild("previews");
|
|
|
|
for (auto previewNode : previewsNode->getChildren("preview")) {
|
|
|
|
// add file path as url
|
|
|
|
QString pathInXml = QString::fromStdString(previewNode->getStringValue("path"));
|
|
|
|
QString previewPath = dir.absoluteFilePath(pathInXml);
|
|
|
|
previews.append(QUrl::fromLocalFile(previewPath));
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 20:01:54 +00:00
|
|
|
|
|
|
|
if (sim->hasChild("thumbnail")) {
|
|
|
|
thumbnailPath = sim->getStringValue("thumbnail");
|
|
|
|
} else {
|
|
|
|
thumbnailPath = "thumbnail.jpg";
|
|
|
|
}
|
2017-06-22 09:45:47 +00:00
|
|
|
|
|
|
|
if (sim->hasChild("minimum-fg-version")) {
|
|
|
|
minFGVersion = sim->getStringValue("minimum-fg-version");
|
|
|
|
}
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString AircraftItem::baseName() const
|
|
|
|
{
|
|
|
|
QString fn = QFileInfo(path).fileName();
|
|
|
|
fn.truncate(fn.count() - 8);
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItem::fromDataStream(QDataStream& ds)
|
|
|
|
{
|
2015-03-16 16:34:22 +00:00
|
|
|
ds >> path >> pathModTime >> excluded;
|
|
|
|
if (excluded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:54 +00:00
|
|
|
ds >> description >> longDescription >> authors >> variantOf >> isPrimary;
|
2015-03-10 00:13:55 +00:00
|
|
|
for (int i=0; i<4; ++i) ds >> ratings[i];
|
2017-02-06 14:09:07 +00:00
|
|
|
ds >> previews;
|
2017-03-05 20:01:54 +00:00
|
|
|
ds >> thumbnailPath;
|
2017-06-22 09:45:47 +00:00
|
|
|
ds >> minFGVersion;
|
|
|
|
ds >> needsMaintenance >> usesHeliports >> usesSeaports;
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItem::toDataStream(QDataStream& ds) const
|
|
|
|
{
|
2015-03-16 16:34:22 +00:00
|
|
|
ds << path << pathModTime << excluded;
|
|
|
|
if (excluded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:54 +00:00
|
|
|
ds << description << longDescription << authors << variantOf << isPrimary;
|
2015-03-10 00:13:55 +00:00
|
|
|
for (int i=0; i<4; ++i) ds << ratings[i];
|
2017-02-06 14:09:07 +00:00
|
|
|
ds << previews;
|
2017-03-05 20:01:54 +00:00
|
|
|
ds << thumbnailPath;
|
2017-06-22 09:45:47 +00:00
|
|
|
ds << minFGVersion;
|
|
|
|
ds << needsMaintenance << usesHeliports << usesSeaports;
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 21:06:46 +00:00
|
|
|
QPixmap AircraftItem::thumbnail(bool loadIfRequired) const
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
2016-11-17 21:06:46 +00:00
|
|
|
if (m_thumbnail.isNull() && loadIfRequired) {
|
2015-03-10 00:13:55 +00:00
|
|
|
QFileInfo info(path);
|
|
|
|
QDir dir = info.dir();
|
2017-03-05 20:01:54 +00:00
|
|
|
if (dir.exists(thumbnailPath)) {
|
|
|
|
m_thumbnail.load(dir.filePath(thumbnailPath));
|
2015-03-10 00:13:55 +00:00
|
|
|
// resize to the standard size
|
2015-08-03 20:53:56 +00:00
|
|
|
if (m_thumbnail.height() > STANDARD_THUMBNAIL_HEIGHT) {
|
2017-01-17 22:18:35 +00:00
|
|
|
m_thumbnail = m_thumbnail.scaledToHeight(STANDARD_THUMBNAIL_HEIGHT, Qt::SmoothTransformation);
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_thumbnail;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AircraftScanThread : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
AircraftScanThread(QStringList dirsToScan) :
|
|
|
|
m_dirs(dirsToScan),
|
|
|
|
m_done(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~AircraftScanThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** thread-safe access to items already scanned */
|
2015-08-03 20:53:56 +00:00
|
|
|
QVector<AircraftItemPtr> items()
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
2015-08-03 20:53:56 +00:00
|
|
|
QVector<AircraftItemPtr> result;
|
2015-03-10 00:13:55 +00:00
|
|
|
QMutexLocker g(&m_lock);
|
|
|
|
result.swap(m_items);
|
|
|
|
g.unlock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDone()
|
|
|
|
{
|
|
|
|
m_done = true;
|
|
|
|
}
|
|
|
|
Q_SIGNALS:
|
|
|
|
void addedItems();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void run()
|
|
|
|
{
|
|
|
|
readCache();
|
|
|
|
|
|
|
|
Q_FOREACH(QString d, m_dirs) {
|
|
|
|
scanAircraftDir(QDir(d));
|
|
|
|
if (m_done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void readCache()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QByteArray cacheData = settings.value("aircraft-cache").toByteArray();
|
|
|
|
if (!cacheData.isEmpty()) {
|
|
|
|
QDataStream ds(cacheData);
|
|
|
|
quint32 count, cacheVersion;
|
|
|
|
ds >> cacheVersion >> count;
|
|
|
|
|
|
|
|
if (cacheVersion != CACHE_VERSION) {
|
|
|
|
return; // mis-matched cache, version, drop
|
|
|
|
}
|
|
|
|
|
2015-11-14 16:45:04 +00:00
|
|
|
for (quint32 i=0; i<count; ++i) {
|
2015-08-03 20:53:56 +00:00
|
|
|
AircraftItemPtr item(new AircraftItem);
|
2015-03-10 00:13:55 +00:00
|
|
|
item->fromDataStream(ds);
|
|
|
|
|
|
|
|
QFileInfo finfo(item->path);
|
2015-08-03 20:53:56 +00:00
|
|
|
if (finfo.exists() && (finfo.lastModified() == item->pathModTime)) {
|
2015-03-10 00:13:55 +00:00
|
|
|
// corresponding -set.xml file still exists and is
|
|
|
|
// unmodified
|
|
|
|
m_cachedItems[item->path] = item;
|
|
|
|
}
|
|
|
|
} // of cached item iteration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeCache()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QByteArray cacheData;
|
|
|
|
{
|
|
|
|
QDataStream ds(&cacheData, QIODevice::WriteOnly);
|
|
|
|
quint32 count = m_nextCache.count();
|
|
|
|
ds << CACHE_VERSION << count;
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
Q_FOREACH(AircraftItemPtr item, m_nextCache.values()) {
|
2015-03-10 00:13:55 +00:00
|
|
|
item->toDataStream(ds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.setValue("aircraft-cache", cacheData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanAircraftDir(QDir path)
|
|
|
|
{
|
|
|
|
QTime t;
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
QStringList filters;
|
|
|
|
filters << "*-set.xml";
|
|
|
|
Q_FOREACH(QFileInfo child, path.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
|
|
|
|
QDir childDir(child.absoluteFilePath());
|
2015-08-03 20:53:56 +00:00
|
|
|
QMap<QString, AircraftItemPtr> baseAircraft;
|
|
|
|
QList<AircraftItemPtr> variants;
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
Q_FOREACH(QFileInfo xmlChild, childDir.entryInfoList(filters, QDir::Files)) {
|
|
|
|
try {
|
|
|
|
QString absolutePath = xmlChild.absoluteFilePath();
|
2015-08-03 20:53:56 +00:00
|
|
|
AircraftItemPtr item;
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
if (m_cachedItems.contains(absolutePath)) {
|
|
|
|
item = m_cachedItems.value(absolutePath);
|
|
|
|
} else {
|
2015-08-03 20:53:56 +00:00
|
|
|
item = AircraftItemPtr(new AircraftItem(childDir, absolutePath));
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_nextCache[absolutePath] = item;
|
|
|
|
|
2015-03-16 16:34:22 +00:00
|
|
|
if (item->excluded) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:01:54 +00:00
|
|
|
if (item->isPrimary) {
|
2015-03-10 00:13:55 +00:00
|
|
|
baseAircraft.insert(item->baseName(), item);
|
|
|
|
} else {
|
|
|
|
variants.append(item);
|
|
|
|
}
|
|
|
|
} catch (sg_exception& e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // of set.xml iteration
|
|
|
|
|
|
|
|
// bind variants to their principals
|
2015-08-03 20:53:56 +00:00
|
|
|
Q_FOREACH(AircraftItemPtr item, variants) {
|
2015-03-10 00:13:55 +00:00
|
|
|
if (!baseAircraft.contains(item->variantOf)) {
|
|
|
|
qWarning() << "can't find principal aircraft " << item->variantOf << " for variant:" << item->path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
baseAircraft.value(item->variantOf)->variants.append(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock mutex while we modify the items array
|
|
|
|
{
|
|
|
|
QMutexLocker g(&m_lock);
|
2015-09-21 21:12:24 +00:00
|
|
|
m_items+=(baseAircraft.values().toVector());
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit addedItems();
|
|
|
|
} // of subdir iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutex m_lock;
|
|
|
|
QStringList m_dirs;
|
2015-08-03 20:53:56 +00:00
|
|
|
QVector<AircraftItemPtr> m_items;
|
2015-03-10 00:13:55 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
QMap<QString, AircraftItemPtr > m_cachedItems;
|
|
|
|
QMap<QString, AircraftItemPtr > m_nextCache;
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
bool m_done;
|
|
|
|
};
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
class PackageDelegate : public simgear::pkg::Delegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PackageDelegate(AircraftItemModel* model) :
|
|
|
|
m_model(model)
|
|
|
|
{
|
|
|
|
m_model->m_packageRoot->addDelegate(this);
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
~PackageDelegate()
|
|
|
|
{
|
|
|
|
m_model->m_packageRoot->removeDelegate(this);
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
protected:
|
|
|
|
virtual void catalogRefreshed(CatalogRef aCatalog, StatusCode aReason)
|
|
|
|
{
|
|
|
|
if (aReason == STATUS_IN_PROGRESS) {
|
2016-11-16 21:27:00 +00:00
|
|
|
// nothing to do
|
2015-08-03 20:53:56 +00:00
|
|
|
} else if ((aReason == STATUS_REFRESHED) || (aReason == STATUS_SUCCESS)) {
|
|
|
|
m_model->refreshPackages();
|
|
|
|
} else {
|
|
|
|
qWarning() << "failed refresh of "
|
|
|
|
<< QString::fromStdString(aCatalog->url()) << ":" << aReason << endl;
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
void startInstall(InstallRef aInstall) override
|
2015-08-03 20:53:56 +00:00
|
|
|
{
|
|
|
|
QModelIndex mi(indexForPackage(aInstall->package()));
|
|
|
|
m_model->dataChanged(mi, mi);
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
void installProgress(InstallRef aInstall, unsigned int bytes, unsigned int total) override
|
2015-08-03 20:53:56 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(bytes);
|
|
|
|
Q_UNUSED(total);
|
|
|
|
QModelIndex mi(indexForPackage(aInstall->package()));
|
|
|
|
m_model->dataChanged(mi, mi);
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
void finishInstall(InstallRef aInstall, StatusCode aReason) override
|
2015-08-03 20:53:56 +00:00
|
|
|
{
|
|
|
|
QModelIndex mi(indexForPackage(aInstall->package()));
|
|
|
|
m_model->dataChanged(mi, mi);
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
if ((aReason != USER_CANCELLED) && (aReason != STATUS_SUCCESS)) {
|
|
|
|
m_model->installFailed(mi, aReason);
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
if (aReason == STATUS_SUCCESS) {
|
|
|
|
m_model->installSucceeded(mi);
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
void availablePackagesChanged() override
|
2015-11-23 17:59:16 +00:00
|
|
|
{
|
|
|
|
m_model->refreshPackages();
|
|
|
|
}
|
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
void installStatusChanged(InstallRef aInstall, StatusCode aReason) override
|
|
|
|
{
|
|
|
|
Q_UNUSED(aReason);
|
|
|
|
QModelIndex mi(indexForPackage(aInstall->package()));
|
|
|
|
m_model->dataChanged(mi, mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finishUninstall(const PackageRef& pkg) override
|
|
|
|
{
|
|
|
|
QModelIndex mi(indexForPackage(pkg));
|
|
|
|
m_model->dataChanged(mi, mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
virtual void dataForThumbnail(const std::string& aThumbnailUrl,
|
|
|
|
size_t length, const uint8_t* bytes)
|
|
|
|
{
|
|
|
|
QImage img = QImage::fromData(QByteArray::fromRawData(reinterpret_cast<const char*>(bytes), length));
|
|
|
|
if (img.isNull()) {
|
|
|
|
qWarning() << "failed to load image data for URL:" <<
|
|
|
|
QString::fromStdString(aThumbnailUrl);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-11-10 23:28:24 +00:00
|
|
|
QPixmap pix = QPixmap::fromImage(img);
|
|
|
|
if (pix.height() > STANDARD_THUMBNAIL_HEIGHT) {
|
2017-01-17 22:18:35 +00:00
|
|
|
pix = pix.scaledToHeight(STANDARD_THUMBNAIL_HEIGHT, Qt::SmoothTransformation);
|
2015-11-10 23:28:24 +00:00
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
QString url = QString::fromStdString(aThumbnailUrl);
|
|
|
|
m_model->m_downloadedPixmapCache.insert(url, pix);
|
2017-02-06 14:09:07 +00:00
|
|
|
|
|
|
|
// notify any affected items. Linear scan here avoids another map/dict structure.
|
2016-11-17 21:06:46 +00:00
|
|
|
for (auto pkg : m_model->m_packages) {
|
2017-04-08 04:08:46 +00:00
|
|
|
const size_t variantCount = pkg->variants().size();
|
2017-02-06 14:09:07 +00:00
|
|
|
bool notifyChanged = false;
|
|
|
|
|
2017-04-08 04:08:46 +00:00
|
|
|
for (size_t v=0; v < variantCount; ++v) {
|
2017-02-06 14:09:07 +00:00
|
|
|
const Package::Thumbnail& thumb(pkg->thumbnailForVariant(v));
|
|
|
|
if (thumb.url == aThumbnailUrl) {
|
|
|
|
notifyChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notifyChanged) {
|
2016-11-17 21:06:46 +00:00
|
|
|
QModelIndex mi = indexForPackage(pkg);
|
2015-08-03 20:53:56 +00:00
|
|
|
m_model->dataChanged(mi, mi);
|
|
|
|
}
|
|
|
|
} // of packages iteration
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
private:
|
|
|
|
QModelIndex indexForPackage(const PackageRef& ref) const
|
|
|
|
{
|
2016-11-16 21:27:00 +00:00
|
|
|
auto it = std::find(m_model->m_packages.begin(),
|
|
|
|
m_model->m_packages.end(), ref);
|
2015-08-03 20:53:56 +00:00
|
|
|
if (it == m_model->m_packages.end()) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-04-08 04:08:46 +00:00
|
|
|
int offset = std::distance(m_model->m_packages.begin(), it);
|
2015-08-03 20:53:56 +00:00
|
|
|
return m_model->index(offset + m_model->m_items.size());
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
AircraftItemModel* m_model;
|
|
|
|
};
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
AircraftItemModel::AircraftItemModel(QObject* pr) :
|
2016-11-07 19:49:33 +00:00
|
|
|
QAbstractListModel(pr)
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
2015-03-11 23:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AircraftItemModel::~AircraftItemModel()
|
|
|
|
{
|
|
|
|
abandonCurrentScan();
|
2015-09-28 00:44:29 +00:00
|
|
|
delete m_delegate;
|
2015-03-11 23:20:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:10:06 +00:00
|
|
|
void AircraftItemModel::setPackageRoot(const simgear::pkg::RootRef& root)
|
|
|
|
{
|
|
|
|
if (m_packageRoot) {
|
|
|
|
delete m_delegate;
|
|
|
|
m_delegate = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_packageRoot = root;
|
|
|
|
|
|
|
|
if (m_packageRoot) {
|
|
|
|
m_delegate = new PackageDelegate(this);
|
|
|
|
// packages may already be refreshed, so pull now
|
|
|
|
refreshPackages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:20:18 +00:00
|
|
|
void AircraftItemModel::setPaths(QStringList paths)
|
|
|
|
{
|
|
|
|
m_paths = paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItemModel::scanDirs()
|
|
|
|
{
|
2016-06-29 15:25:42 +00:00
|
|
|
abandonCurrentScan();
|
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
const int numToRemove = m_items.size();
|
2016-06-29 15:25:42 +00:00
|
|
|
if (numToRemove > 0) {
|
2017-06-22 09:45:47 +00:00
|
|
|
int lastRow = numToRemove - 1;
|
|
|
|
beginRemoveRows(QModelIndex(), 0, lastRow);
|
|
|
|
m_items.remove(0, numToRemove);
|
|
|
|
m_delegateStates.remove(0, numToRemove);
|
2016-06-29 15:25:42 +00:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
2015-03-11 23:20:18 +00:00
|
|
|
|
|
|
|
QStringList dirs = m_paths;
|
|
|
|
|
2016-06-21 11:29:04 +00:00
|
|
|
Q_FOREACH(SGPath ap, globals->get_aircraft_paths()) {
|
|
|
|
dirs << QString::fromStdString(ap.utf8Str());
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SGPath rootAircraft(globals->get_fg_root());
|
|
|
|
rootAircraft.append("Aircraft");
|
2016-06-21 11:28:35 +00:00
|
|
|
dirs << QString::fromStdString(rootAircraft.utf8Str());
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
m_scanThread = new AircraftScanThread(dirs);
|
|
|
|
connect(m_scanThread, &AircraftScanThread::finished, this,
|
|
|
|
&AircraftItemModel::onScanFinished);
|
|
|
|
connect(m_scanThread, &AircraftScanThread::addedItems,
|
|
|
|
this, &AircraftItemModel::onScanResults);
|
|
|
|
m_scanThread->start();
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:20:18 +00:00
|
|
|
void AircraftItemModel::abandonCurrentScan()
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
|
|
|
if (m_scanThread) {
|
|
|
|
m_scanThread->setDone();
|
|
|
|
m_scanThread->wait(1000);
|
|
|
|
delete m_scanThread;
|
2015-03-11 23:20:18 +00:00
|
|
|
m_scanThread = NULL;
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
void AircraftItemModel::refreshPackages()
|
|
|
|
{
|
2016-04-15 16:06:53 +00:00
|
|
|
simgear::pkg::PackageList newPkgs = m_packageRoot->allPackages();
|
2016-11-16 21:27:00 +00:00
|
|
|
const int firstRow = m_items.size();
|
|
|
|
const int newSize = newPkgs.size();
|
|
|
|
const int newTotalSize = firstRow + newSize;
|
2016-04-15 16:06:53 +00:00
|
|
|
|
|
|
|
if (m_packages.size() != newPkgs.size()) {
|
|
|
|
int oldSize = m_packages.size();
|
|
|
|
if (newSize > oldSize) {
|
|
|
|
// growing
|
|
|
|
int firstNewRow = firstRow + oldSize;
|
|
|
|
int lastNewRow = firstRow + newSize - 1;
|
|
|
|
beginInsertRows(QModelIndex(), firstNewRow, lastNewRow);
|
|
|
|
m_packages = newPkgs;
|
2016-11-16 21:27:00 +00:00
|
|
|
m_delegateStates.resize(newTotalSize);
|
2016-04-15 16:06:53 +00:00
|
|
|
endInsertRows();
|
|
|
|
} else {
|
|
|
|
// shrinking
|
|
|
|
int firstOldRow = firstRow + newSize;
|
|
|
|
int lastOldRow = firstRow + oldSize - 1;
|
|
|
|
beginRemoveRows(QModelIndex(), firstOldRow, lastOldRow);
|
|
|
|
m_packages = newPkgs;
|
2016-11-16 21:27:00 +00:00
|
|
|
m_delegateStates.resize(newTotalSize);
|
2016-04-15 16:06:53 +00:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_packages = newPkgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit dataChanged(index(firstRow), index(firstRow + newSize - 1));
|
2017-06-22 09:45:47 +00:00
|
|
|
m_cachedUpdateCount = m_packageRoot->packagesNeedingUpdate().size();
|
|
|
|
emit aircraftNeedingUpdatedChanged();
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AircraftItemModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
return m_items.size() + m_packages.size();
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
QVariant AircraftItemModel::data(const QModelIndex& index, int role) const
|
2015-03-13 16:29:20 +00:00
|
|
|
{
|
2016-04-15 16:06:53 +00:00
|
|
|
int row = index.row();
|
2016-11-16 21:27:00 +00:00
|
|
|
if (role == AircraftVariantRole) {
|
|
|
|
return m_delegateStates.at(row).variant;
|
|
|
|
}
|
2015-08-03 20:53:56 +00:00
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
if (row >= m_items.size()) {
|
|
|
|
quint32 packageIndex = row - m_items.size();
|
2015-08-03 20:53:56 +00:00
|
|
|
const PackageRef& pkg(m_packages[packageIndex]);
|
|
|
|
InstallRef ex = pkg->existingInstall();
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
if (role == AircraftInstallPercentRole) {
|
|
|
|
return ex.valid() ? ex->downloadedPercent() : 0;
|
|
|
|
} else if (role == AircraftInstallDownloadedSizeRole) {
|
|
|
|
return static_cast<quint64>(ex.valid() ? ex->downloadedBytes() : 0);
|
2016-11-30 21:33:16 +00:00
|
|
|
} else if (role == AircraftPackageRefRole ) {
|
|
|
|
return QVariant::fromValue(pkg);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
return dataFromPackage(pkg, m_delegateStates.at(row), role);
|
2015-08-03 20:53:56 +00:00
|
|
|
} else {
|
2016-04-15 16:06:53 +00:00
|
|
|
const AircraftItemPtr item(m_items.at(row));
|
2016-11-16 21:27:00 +00:00
|
|
|
return dataFromItem(item, m_delegateStates.at(row), role);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-03-13 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
QVariant AircraftItemModel::dataFromItem(AircraftItemPtr item, const DelegateState& state, int role) const
|
2015-03-13 16:29:20 +00:00
|
|
|
{
|
|
|
|
if (role == AircraftVariantCountRole) {
|
|
|
|
return item->variants.count();
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
if (role >= AircraftVariantDescriptionRole) {
|
2015-03-13 16:29:20 +00:00
|
|
|
int variantIndex = role - AircraftVariantDescriptionRole;
|
|
|
|
return item->variants.at(variantIndex)->description;
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
if (state.variant) {
|
|
|
|
if (state.variant <= static_cast<quint32>(item->variants.count())) {
|
2015-03-13 16:29:20 +00:00
|
|
|
// show the selected variant
|
2016-11-16 21:27:00 +00:00
|
|
|
item = item->variants.at(state.variant - 1);
|
2015-03-13 16:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
if (role == AircraftThumbnailSizeRole) {
|
|
|
|
QPixmap pm = item->thumbnail(false);
|
|
|
|
if (pm.isNull()) {
|
|
|
|
return QSize(STANDARD_THUMBNAIL_WIDTH, STANDARD_THUMBNAIL_HEIGHT);
|
|
|
|
}
|
|
|
|
return pm.size();
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:29:20 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
2015-09-26 17:41:32 +00:00
|
|
|
if (item->description.isEmpty()) {
|
|
|
|
return tr("Missing description for: %1").arg(item->baseName());
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:29:20 +00:00
|
|
|
return item->description;
|
|
|
|
} else if (role == Qt::DecorationRole) {
|
|
|
|
return item->thumbnail();
|
|
|
|
} else if (role == AircraftPathRole) {
|
|
|
|
return item->path;
|
|
|
|
} else if (role == AircraftAuthorsRole) {
|
|
|
|
return item->authors;
|
|
|
|
} else if ((role >= AircraftRatingRole) && (role < AircraftVariantDescriptionRole)) {
|
|
|
|
return item->ratings[role - AircraftRatingRole];
|
2017-02-06 14:09:07 +00:00
|
|
|
} else if (role == AircraftPreviewsRole) {
|
|
|
|
QVariantList result;
|
|
|
|
Q_FOREACH(QUrl u, item->previews) {
|
|
|
|
result.append(u);
|
|
|
|
}
|
|
|
|
return result;
|
2017-06-22 09:45:47 +00:00
|
|
|
} else if (role == AircraftHasPreviewsRole) {
|
|
|
|
return !item->previews.empty();
|
2017-02-06 14:09:07 +00:00
|
|
|
} else if (role == AircraftThumbnailRole) {
|
2015-03-13 16:29:20 +00:00
|
|
|
return item->thumbnail();
|
|
|
|
} else if (role == AircraftPackageIdRole) {
|
|
|
|
// can we fake an ID? otherwise fall through to a null variant
|
|
|
|
} else if (role == AircraftPackageStatusRole) {
|
|
|
|
return PackageInstalled; // always the case
|
|
|
|
} else if (role == Qt::ToolTipRole) {
|
|
|
|
return item->path;
|
2015-08-03 20:53:56 +00:00
|
|
|
} else if (role == AircraftURIRole) {
|
|
|
|
return QUrl::fromLocalFile(item->path);
|
2015-03-16 16:34:22 +00:00
|
|
|
} else if (role == AircraftHasRatingsRole) {
|
|
|
|
bool have = false;
|
|
|
|
for (int i=0; i<4; ++i) {
|
|
|
|
have |= (item->ratings[i] > 0);
|
|
|
|
}
|
|
|
|
return have;
|
2015-03-16 15:07:44 +00:00
|
|
|
} else if (role == AircraftLongDescriptionRole) {
|
2016-04-15 16:21:49 +00:00
|
|
|
return item->longDescription;
|
2015-11-17 07:36:54 +00:00
|
|
|
} else if (role == AircraftIsHelicopterRole) {
|
|
|
|
return item->usesHeliports;
|
|
|
|
} else if (role == AircraftIsSeaplaneRole) {
|
|
|
|
return item->usesSeaports;
|
2017-06-22 09:45:47 +00:00
|
|
|
} else if ((role == AircraftInstallDownloadedSizeRole) ||
|
|
|
|
(role == AircraftPackageSizeRole))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
} else if (role == AircraftStatusRole) {
|
|
|
|
return itemAircraftStatus(item, state);
|
|
|
|
} else if (role == AircraftMinVersionRole) {
|
|
|
|
return item->minFGVersion;
|
2015-03-13 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
QVariant AircraftItemModel::dataFromPackage(const PackageRef& item, const DelegateState& state, int role) const
|
2015-03-13 16:29:20 +00:00
|
|
|
{
|
2015-08-03 20:53:56 +00:00
|
|
|
if (role == Qt::DecorationRole) {
|
2017-02-06 14:09:07 +00:00
|
|
|
role = AircraftThumbnailRole;
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
if (role >= AircraftVariantDescriptionRole) {
|
2016-01-04 02:58:03 +00:00
|
|
|
int variantIndex = role - AircraftVariantDescriptionRole;
|
2016-01-11 05:46:44 +00:00
|
|
|
QString desc = QString::fromStdString(item->nameForVariant(variantIndex));
|
|
|
|
if (desc.isEmpty()) {
|
|
|
|
desc = tr("Missing description for: %1").arg(QString::fromStdString(item->id()));
|
|
|
|
}
|
|
|
|
return desc;
|
2016-01-04 02:58:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:29:20 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
2016-11-16 21:27:00 +00:00
|
|
|
QString desc = QString::fromStdString(item->nameForVariant(state.variant));
|
2016-01-11 05:46:44 +00:00
|
|
|
if (desc.isEmpty()) {
|
|
|
|
desc = tr("Missing description for: %1").arg(QString::fromStdString(item->id()));
|
|
|
|
}
|
|
|
|
return desc;
|
2015-03-13 16:29:20 +00:00
|
|
|
} else if (role == AircraftPathRole) {
|
2015-08-03 20:53:56 +00:00
|
|
|
InstallRef i = item->existingInstall();
|
|
|
|
if (i.valid()) {
|
2016-06-21 11:28:35 +00:00
|
|
|
return QString::fromStdString(i->primarySetPath().utf8Str());
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-03-13 16:29:20 +00:00
|
|
|
} else if (role == AircraftPackageIdRole) {
|
2016-11-16 21:27:00 +00:00
|
|
|
return QString::fromStdString(item->variants()[state.variant]);
|
2015-03-13 16:29:20 +00:00
|
|
|
} else if (role == AircraftPackageStatusRole) {
|
2015-08-03 20:53:56 +00:00
|
|
|
InstallRef i = item->existingInstall();
|
|
|
|
if (i.valid()) {
|
2015-03-13 16:29:20 +00:00
|
|
|
if (i->isDownloading()) {
|
|
|
|
return PackageDownloading;
|
|
|
|
}
|
2015-08-03 20:53:56 +00:00
|
|
|
if (i->isQueued()) {
|
|
|
|
return PackageQueued;
|
|
|
|
}
|
2015-03-13 16:29:20 +00:00
|
|
|
if (i->hasUpdate()) {
|
|
|
|
return PackageUpdateAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PackageInstalled;
|
|
|
|
} else {
|
|
|
|
return PackageNotInstalled;
|
|
|
|
}
|
2016-01-04 02:58:03 +00:00
|
|
|
} else if (role == AircraftVariantCountRole) {
|
|
|
|
// this value wants the number of aditional variants, i.e not
|
|
|
|
// including the primary. Hence the -1 term.
|
|
|
|
return static_cast<quint32>(item->variants().size() - 1);
|
2015-11-10 23:28:24 +00:00
|
|
|
} else if (role == AircraftThumbnailSizeRole) {
|
2016-11-16 21:27:00 +00:00
|
|
|
QPixmap pm = packageThumbnail(item, state, false).value<QPixmap>();
|
2015-11-10 23:28:24 +00:00
|
|
|
if (pm.isNull())
|
|
|
|
return QSize(STANDARD_THUMBNAIL_WIDTH, STANDARD_THUMBNAIL_HEIGHT);
|
|
|
|
return pm.size();
|
2017-02-06 14:09:07 +00:00
|
|
|
} else if (role == AircraftThumbnailRole) {
|
|
|
|
return packageThumbnail(item, state);
|
|
|
|
} else if (role == AircraftPreviewsRole) {
|
|
|
|
return packagePreviews(item, state);
|
2017-06-22 09:45:47 +00:00
|
|
|
} else if (role == AircraftHasPreviewsRole) {
|
|
|
|
return !item->previewsForVariant(state.variant).empty();
|
2015-08-03 20:53:56 +00:00
|
|
|
} else if (role == AircraftAuthorsRole) {
|
2016-11-25 22:45:48 +00:00
|
|
|
std::string authors = item->getLocalisedProp("author", state.variant);
|
|
|
|
if (!authors.empty()) {
|
|
|
|
return QString::fromStdString(authors);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-03-16 15:07:44 +00:00
|
|
|
} else if (role == AircraftLongDescriptionRole) {
|
2016-11-25 22:45:48 +00:00
|
|
|
std::string longDesc = item->getLocalisedProp("description", state.variant);
|
|
|
|
return QString::fromStdString(longDesc).simplified();
|
2015-08-03 20:53:56 +00:00
|
|
|
} else if (role == AircraftPackageSizeRole) {
|
|
|
|
return static_cast<int>(item->fileSizeBytes());
|
|
|
|
} else if (role == AircraftURIRole) {
|
2016-11-16 21:27:00 +00:00
|
|
|
return QUrl("package:" + QString::fromStdString(item->qualifiedVariantId(state.variant)));
|
2015-08-03 20:53:56 +00:00
|
|
|
} else if (role == AircraftHasRatingsRole) {
|
|
|
|
return item->properties()->hasChild("rating");
|
|
|
|
} else if ((role >= AircraftRatingRole) && (role < AircraftVariantDescriptionRole)) {
|
|
|
|
int ratingIndex = role - AircraftRatingRole;
|
|
|
|
SGPropertyNode* ratings = item->properties()->getChild("rating");
|
|
|
|
if (!ratings) {
|
2017-06-22 09:45:47 +00:00
|
|
|
return 0;
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
|
|
|
return ratings->getChild(ratingIndex)->getIntValue();
|
2017-06-22 09:45:47 +00:00
|
|
|
} else if (role == AircraftStatusRole) {
|
|
|
|
return packageAircraftStatus(item, state);
|
|
|
|
} else if (role == AircraftMinVersionRole) {
|
|
|
|
const std::string v = item->properties()->getStringValue("minimum-fg-version");
|
|
|
|
if (!v.empty()) {
|
|
|
|
return QString::fromStdString(v);
|
|
|
|
}
|
2015-03-13 16:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
2015-03-10 00:13:55 +00:00
|
|
|
|
2016-11-16 21:27:00 +00:00
|
|
|
QVariant AircraftItemModel::packageThumbnail(PackageRef p, const DelegateState& ds, bool download) const
|
2015-08-03 20:53:56 +00:00
|
|
|
{
|
2017-02-06 14:09:07 +00:00
|
|
|
const Package::Thumbnail& thumb(p->thumbnailForVariant(ds.variant));
|
|
|
|
if (thumb.url.empty()) {
|
2015-08-03 20:53:56 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
QString urlQString(QString::fromStdString(thumb.url));
|
|
|
|
if (m_downloadedPixmapCache.contains(urlQString)) {
|
2015-08-03 20:53:56 +00:00
|
|
|
// cache hit, easy
|
2017-02-06 14:09:07 +00:00
|
|
|
return m_downloadedPixmapCache.value(urlQString);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
// check the on-disk store.
|
2015-08-03 20:53:56 +00:00
|
|
|
InstallRef ex = p->existingInstall();
|
|
|
|
if (ex.valid()) {
|
2017-02-06 14:09:07 +00:00
|
|
|
SGPath thumbPath = ex->path() / thumb.path;
|
|
|
|
if (thumbPath.exists()) {
|
|
|
|
QPixmap pix;
|
|
|
|
pix.load(QString::fromStdString(thumbPath.utf8Str()));
|
|
|
|
// resize to the standard size
|
|
|
|
if (pix.height() > STANDARD_THUMBNAIL_HEIGHT) {
|
|
|
|
pix = pix.scaledToHeight(STANDARD_THUMBNAIL_HEIGHT);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
2017-02-06 14:09:07 +00:00
|
|
|
m_downloadedPixmapCache[urlQString] = pix;
|
|
|
|
return pix;
|
|
|
|
}
|
2015-08-03 20:53:56 +00:00
|
|
|
} // of have existing install
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-11-10 23:28:24 +00:00
|
|
|
if (download) {
|
2017-02-06 14:09:07 +00:00
|
|
|
m_packageRoot->requestThumbnailData(thumb.url);
|
2015-11-10 23:28:24 +00:00
|
|
|
}
|
2016-11-16 21:27:00 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:09:07 +00:00
|
|
|
QVariant AircraftItemModel::packagePreviews(PackageRef p, const DelegateState& ds) const
|
|
|
|
{
|
|
|
|
const Package::PreviewVec& previews = p->previewsForVariant(ds.variant);
|
|
|
|
if (previews.empty()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList result;
|
|
|
|
// if we have an install, return file URLs, not remote (http) ones
|
|
|
|
InstallRef ex = p->existingInstall();
|
|
|
|
if (ex.valid()) {
|
|
|
|
for (auto p : previews) {
|
|
|
|
SGPath localPreviewPath = ex->path() / p.path;
|
|
|
|
if (!localPreviewPath.exists()) {
|
|
|
|
qWarning() << "missing local preview" << QString::fromStdString(localPreviewPath.utf8Str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.append(QUrl::fromLocalFile(QString::fromStdString(localPreviewPath.utf8Str())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return remote urls
|
|
|
|
for (auto p : previews) {
|
|
|
|
result.append(QUrl(QString::fromStdString(p.url)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
|
|
|
|
QVariant AircraftItemModel::itemAircraftStatus(AircraftItemPtr item, const DelegateState& ds) const
|
|
|
|
{
|
|
|
|
if (item->needsMaintenance) {
|
|
|
|
return AircraftUnmaintained;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->minFGVersion.isEmpty()) {
|
|
|
|
return AircraftOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int c = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION,
|
|
|
|
item->minFGVersion.toStdString(), 2);
|
|
|
|
return (c < 0) ? AircraftNeedsNewerSimulator : AircraftOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AircraftItemModel::packageAircraftStatus(PackageRef p, const DelegateState& ds) const
|
|
|
|
{
|
|
|
|
if (p->hasTag("needs-maintenance")) {
|
|
|
|
return AircraftUnmaintained;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->properties()->hasChild("minimum-fg-version")) {
|
|
|
|
return AircraftOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string minFGVersion = p->properties()->getStringValue("minimum-fg-version");
|
|
|
|
const int c = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, minFGVersion, 2);
|
|
|
|
return (c < 0) ? AircraftNeedsNewerSimulator : AircraftOk;
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
bool AircraftItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
2016-01-04 02:58:03 +00:00
|
|
|
int row = index.row();
|
2016-11-16 21:27:00 +00:00
|
|
|
int newValue = value.toInt();
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
if (role == AircraftVariantRole) {
|
2016-11-16 21:27:00 +00:00
|
|
|
if (m_delegateStates[row].variant == newValue) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_delegateStates[row].variant = newValue;
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
return false;
|
2017-06-22 09:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> AircraftItemModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> result = QAbstractListModel::roleNames();
|
|
|
|
|
|
|
|
result[Qt::DisplayRole] = "title";
|
|
|
|
result[AircraftURIRole] = "uri";
|
|
|
|
result[AircraftPackageIdRole] = "package";
|
|
|
|
result[AircraftAuthorsRole] = "authors";
|
|
|
|
result[AircraftVariantCountRole] = "variantCount";
|
|
|
|
result[AircraftLongDescriptionRole] = "description";
|
|
|
|
result[AircraftThumbnailRole] = "thumbnail";
|
|
|
|
result[AircraftPackageSizeRole] = "packageSizeBytes";
|
|
|
|
result[AircraftPackageStatusRole] = "packageStatus";
|
|
|
|
|
|
|
|
result[AircraftHasPreviewsRole] = "hasPreviews";
|
|
|
|
result[AircraftInstallDownloadedSizeRole] = "downloadedBytes";
|
|
|
|
result[AircraftVariantRole] = "activeVariant";
|
|
|
|
|
|
|
|
result[AircraftStatusRole] = "aircraftStatus";
|
|
|
|
result[AircraftMinVersionRole] = "requiredFGVersion";
|
|
|
|
|
|
|
|
result[AircraftHasRatingsRole] = "hasRatings";
|
|
|
|
result[AircraftRatingRole] = "ratingFDM";
|
|
|
|
result[AircraftRatingRole + 1] = "ratingSystems";
|
|
|
|
result[AircraftRatingRole + 2] = "ratingCockpit";
|
|
|
|
result[AircraftRatingRole + 3] = "ratingExterior";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-03-10 00:13:55 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
QModelIndex AircraftItemModel::indexOfAircraftURI(QUrl uri) const
|
2015-03-10 00:13:55 +00:00
|
|
|
{
|
2016-04-15 16:06:53 +00:00
|
|
|
if (uri.isEmpty()) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
if (uri.isLocalFile()) {
|
|
|
|
QString path = uri.toLocalFile();
|
|
|
|
for (int row=0; row <m_items.size(); ++row) {
|
|
|
|
const AircraftItemPtr item(m_items.at(row));
|
|
|
|
if (item->path == path) {
|
|
|
|
return index(row);
|
|
|
|
}
|
2016-03-05 09:34:16 +00:00
|
|
|
|
|
|
|
// check variants too
|
|
|
|
for (int vr=0; vr < item->variants.size(); ++vr) {
|
|
|
|
if (item->variants.at(vr)->path == path) {
|
|
|
|
return index(row);
|
|
|
|
}
|
|
|
|
}
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
|
|
|
} else if (uri.scheme() == "package") {
|
|
|
|
QString ident = uri.path();
|
2016-04-15 16:06:53 +00:00
|
|
|
int rowOffset = m_items.size();
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
PackageRef pkg = m_packageRoot->getPackageById(ident.toStdString());
|
|
|
|
if (pkg) {
|
2015-11-14 16:45:04 +00:00
|
|
|
for (size_t i=0; i < m_packages.size(); ++i) {
|
2015-08-03 20:53:56 +00:00
|
|
|
if (m_packages[i] == pkg) {
|
2016-04-15 16:06:53 +00:00
|
|
|
return index(rowOffset + i);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
|
|
|
} // of linear package scan
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
} else if (uri.scheme() == "") {
|
|
|
|
// Empty URI scheme (no selection), nothing to do
|
2015-08-03 20:53:56 +00:00
|
|
|
} else {
|
|
|
|
qWarning() << "Unknown aircraft URI scheme" << uri << uri.scheme();
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:10:02 +00:00
|
|
|
void AircraftItemModel::selectVariantForAircraftURI(QUrl uri)
|
|
|
|
{
|
|
|
|
if (uri.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int variantIndex = 0;
|
|
|
|
QModelIndex modelIndex;
|
|
|
|
|
|
|
|
if (uri.isLocalFile()) {
|
|
|
|
QString path = uri.toLocalFile();
|
|
|
|
for (int row=0; row <m_items.size(); ++row) {
|
|
|
|
const AircraftItemPtr item(m_items.at(row));
|
|
|
|
if (item->path == path) {
|
|
|
|
modelIndex = index(row);
|
|
|
|
variantIndex = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check variants too
|
|
|
|
for (int vr=0; vr < item->variants.size(); ++vr) {
|
|
|
|
if (item->variants.at(vr)->path == path) {
|
|
|
|
modelIndex = index(row);
|
|
|
|
variantIndex = vr + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (uri.scheme() == "package") {
|
|
|
|
QString ident = uri.path();
|
|
|
|
int rowOffset = m_items.size();
|
|
|
|
|
|
|
|
PackageRef pkg = m_packageRoot->getPackageById(ident.toStdString());
|
|
|
|
if (pkg) {
|
|
|
|
for (size_t i=0; i < m_packages.size(); ++i) {
|
|
|
|
if (m_packages[i] == pkg) {
|
|
|
|
modelIndex = index(rowOffset + i);
|
|
|
|
variantIndex = pkg->indexOfVariant(ident.toStdString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // of linear package scan
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "Unknown aircraft URI scheme" << uri << uri.scheme();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modelIndex.isValid()) {
|
|
|
|
setData(modelIndex, variantIndex, AircraftVariantRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 21:25:05 +00:00
|
|
|
QString AircraftItemModel::nameForAircraftURI(QUrl uri) const
|
|
|
|
{
|
|
|
|
if (uri.isLocalFile()) {
|
|
|
|
QString path = uri.toLocalFile();
|
|
|
|
for (int row=0; row <m_items.size(); ++row) {
|
|
|
|
const AircraftItemPtr item(m_items.at(row));
|
|
|
|
if (item->path == path) {
|
|
|
|
return item->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check variants too
|
|
|
|
for (int vr=0; vr < item->variants.size(); ++vr) {
|
|
|
|
if (item->variants.at(vr)->path == path) {
|
|
|
|
return item->description;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (uri.scheme() == "package") {
|
|
|
|
QString ident = uri.path();
|
|
|
|
PackageRef pkg = m_packageRoot->getPackageById(ident.toStdString());
|
|
|
|
if (pkg) {
|
|
|
|
int variantIndex = pkg->indexOfVariant(ident.toStdString());
|
|
|
|
return QString::fromStdString(pkg->nameForVariant(variantIndex));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "Unknown aircraft URI scheme" << uri << uri.scheme();
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
void AircraftItemModel::onScanResults()
|
|
|
|
{
|
2015-08-03 20:53:56 +00:00
|
|
|
QVector<AircraftItemPtr> newItems = m_scanThread->items();
|
2015-03-10 00:13:55 +00:00
|
|
|
if (newItems.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int firstRow = m_items.count();
|
|
|
|
int lastRow = firstRow + newItems.count() - 1;
|
|
|
|
beginInsertRows(QModelIndex(), firstRow, lastRow);
|
2015-09-21 21:12:24 +00:00
|
|
|
m_items+=newItems;
|
2015-03-10 00:13:55 +00:00
|
|
|
|
|
|
|
// default variants in all cases
|
|
|
|
for (int i=0; i< newItems.count(); ++i) {
|
2016-11-16 21:27:00 +00:00
|
|
|
m_delegateStates.insert(firstRow + i, DelegateState());
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
2016-11-16 21:27:00 +00:00
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItemModel::onScanFinished()
|
|
|
|
{
|
|
|
|
delete m_scanThread;
|
|
|
|
m_scanThread = NULL;
|
2015-11-12 00:11:12 +00:00
|
|
|
emit scanCompleted();
|
2015-03-10 00:13:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
void AircraftItemModel::installFailed(QModelIndex index, simgear::pkg::Delegate::StatusCode reason)
|
|
|
|
{
|
|
|
|
QString msg;
|
|
|
|
switch (reason) {
|
|
|
|
case Delegate::FAIL_CHECKSUM:
|
|
|
|
msg = tr("Invalid package checksum"); break;
|
|
|
|
case Delegate::FAIL_DOWNLOAD:
|
|
|
|
msg = tr("Download failed"); break;
|
|
|
|
case Delegate::FAIL_EXTRACT:
|
|
|
|
msg = tr("Package could not be extracted"); break;
|
|
|
|
case Delegate::FAIL_FILESYSTEM:
|
|
|
|
msg = tr("A local file-system error occurred"); break;
|
2015-09-28 00:44:29 +00:00
|
|
|
case Delegate::FAIL_NOT_FOUND:
|
|
|
|
msg = tr("Package file missing from download server"); break;
|
2015-08-03 20:53:56 +00:00
|
|
|
case Delegate::FAIL_UNKNOWN:
|
|
|
|
default:
|
|
|
|
msg = tr("Unknown reason");
|
|
|
|
}
|
2015-09-28 00:44:29 +00:00
|
|
|
|
|
|
|
emit aircraftInstallFailed(index, msg);
|
2015-08-03 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItemModel::installSucceeded(QModelIndex index)
|
|
|
|
{
|
|
|
|
emit aircraftInstallCompleted(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AircraftItemModel::isIndexRunnable(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
if (index.row() < m_items.size()) {
|
|
|
|
return true; // local file, always runnable
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
quint32 packageIndex = index.row() - m_items.size();
|
|
|
|
const PackageRef& pkg(m_packages[packageIndex]);
|
|
|
|
InstallRef ex = pkg->existingInstall();
|
|
|
|
if (!ex.valid()) {
|
|
|
|
return false; // not installed
|
|
|
|
}
|
2015-11-14 16:45:04 +00:00
|
|
|
|
2015-08-03 20:53:56 +00:00
|
|
|
return !ex->isDownloading();
|
|
|
|
}
|
|
|
|
|
2016-03-05 09:35:02 +00:00
|
|
|
bool AircraftItemModel::isCandidateAircraftPath(QString path)
|
|
|
|
{
|
|
|
|
QStringList filters;
|
|
|
|
filters << "*-set.xml";
|
|
|
|
int dirCount = 0,
|
|
|
|
setXmlCount = 0;
|
|
|
|
|
|
|
|
QDir d(path);
|
|
|
|
Q_FOREACH(QFileInfo child, d.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
|
|
|
|
QDir childDir(child.absoluteFilePath());
|
|
|
|
++dirCount;
|
|
|
|
Q_FOREACH(QFileInfo xmlChild, childDir.entryInfoList(filters, QDir::Files)) {
|
|
|
|
++setXmlCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((setXmlCount > 0) || (dirCount > 10)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (setXmlCount > 0);
|
|
|
|
}
|
|
|
|
|
2017-06-22 09:45:47 +00:00
|
|
|
int AircraftItemModel::aircraftNeedingUpdated() const
|
|
|
|
{
|
|
|
|
return m_cachedUpdateCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AircraftItemModel::showUpdateAll() const
|
|
|
|
{
|
|
|
|
return (m_cachedUpdateCount > 0) && m_showUpdateAll;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AircraftItemModel::setShowUpdateAll(bool showUpdateAll)
|
|
|
|
{
|
|
|
|
if (m_showUpdateAll == showUpdateAll)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_showUpdateAll = showUpdateAll;
|
|
|
|
emit aircraftNeedingUpdatedChanged();
|
|
|
|
}
|
2016-03-05 09:35:02 +00:00
|
|
|
|
2015-03-10 00:13:55 +00:00
|
|
|
#include "AircraftModel.moc"
|