2017-01-08 09:28:05 +05:30
|
|
|
/* Copyright 2015-2017 MultiMC Contributors
|
2016-04-07 02:39:30 +05:30
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
#include "VersionList.h"
|
2016-04-07 02:39:30 +05:30
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
#include "Version.h"
|
2017-03-18 06:52:36 +05:30
|
|
|
#include "JsonFormat.h"
|
2017-03-11 06:09:45 +05:30
|
|
|
|
|
|
|
namespace Meta
|
|
|
|
{
|
|
|
|
VersionList::VersionList(const QString &uid, QObject *parent)
|
2016-04-07 02:39:30 +05:30
|
|
|
: BaseVersionList(parent), m_uid(uid)
|
|
|
|
{
|
2017-03-11 06:09:45 +05:30
|
|
|
setObjectName("Version list: " + uid);
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-20 04:28:54 +05:30
|
|
|
shared_qobject_ptr<Task> VersionList::getLoadTask()
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-20 04:28:54 +05:30
|
|
|
load();
|
|
|
|
return getCurrentTask();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
bool VersionList::isLoaded()
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-20 04:28:54 +05:30
|
|
|
return BaseEntity::isLoaded();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
const BaseVersionPtr VersionList::at(int i) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return m_versions.at(i);
|
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
int VersionList::count() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return m_versions.size();
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void VersionList::sortVersions()
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
beginResetModel();
|
2017-03-11 06:09:45 +05:30
|
|
|
std::sort(m_versions.begin(), m_versions.end(), [](const VersionPtr &a, const VersionPtr &b)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return *a.get() < *b.get();
|
|
|
|
});
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
QVariant VersionList::data(const QModelIndex &index, int role) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_versions.size() || index.parent().isValid())
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
VersionPtr version = m_versions.at(index.row());
|
2016-04-07 02:39:30 +05:30
|
|
|
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case VersionPointerRole: return QVariant::fromValue(std::dynamic_pointer_cast<BaseVersion>(version));
|
|
|
|
case VersionRole:
|
|
|
|
case VersionIdRole:
|
|
|
|
return version->version();
|
2017-03-27 07:04:39 +05:30
|
|
|
case ParentVersionRole:
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-24 06:56:06 +05:30
|
|
|
auto parentUid = this->parentUid();
|
|
|
|
if(parentUid.isEmpty())
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-24 06:56:06 +05:30
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
auto & reqs = version->requires();
|
|
|
|
auto iter = reqs.find(parentUid);
|
|
|
|
if (iter != reqs.end())
|
|
|
|
{
|
|
|
|
return iter.value();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
case TypeRole: return version->type();
|
|
|
|
|
|
|
|
case UidRole: return version->uid();
|
|
|
|
case TimeRole: return version->time();
|
|
|
|
case RequiresRole: return QVariant::fromValue(version->requires());
|
|
|
|
case SortRole: return version->rawTime();
|
2017-03-11 06:09:45 +05:30
|
|
|
case VersionPtrRole: return QVariant::fromValue(version);
|
2017-04-07 03:00:18 +05:30
|
|
|
case RecommendedRole: return version->isRecommended();
|
|
|
|
// FIXME: this should be determined in whatever view/proxy is used...
|
|
|
|
// case LatestRole: return version == getLatestStable();
|
2016-04-07 02:39:30 +05:30
|
|
|
default: return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
BaseVersionList::RoleList VersionList::providesRoles() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-27 07:04:39 +05:30
|
|
|
return {VersionPointerRole, VersionRole, VersionIdRole, ParentVersionRole,
|
2016-04-07 02:39:30 +05:30
|
|
|
TypeRole, UidRole, TimeRole, RequiresRole, SortRole,
|
2017-03-11 06:09:45 +05:30
|
|
|
RecommendedRole, LatestRole, VersionPtrRole};
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
QHash<int, QByteArray> VersionList::roleNames() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles = BaseVersionList::roleNames();
|
|
|
|
roles.insert(UidRole, "uid");
|
|
|
|
roles.insert(TimeRole, "time");
|
|
|
|
roles.insert(SortRole, "sort");
|
|
|
|
roles.insert(RequiresRole, "requires");
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
QString VersionList::localFilename() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-12 20:30:06 +05:30
|
|
|
return m_uid + "/index.json";
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
QString VersionList::humanReadable() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return m_name.isEmpty() ? m_uid : m_name;
|
|
|
|
}
|
|
|
|
|
2017-03-19 06:43:49 +05:30
|
|
|
VersionPtr VersionList::getVersion(const QString &version)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-19 06:43:49 +05:30
|
|
|
VersionPtr out = m_lookup.value(version, nullptr);
|
|
|
|
if(!out)
|
|
|
|
{
|
|
|
|
out = std::make_shared<Version>(m_uid, version);
|
|
|
|
m_lookup[version] = out;
|
|
|
|
}
|
|
|
|
return out;
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void VersionList::setName(const QString &name)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
emit nameChanged(name);
|
|
|
|
}
|
2017-03-18 06:52:36 +05:30
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void VersionList::setVersions(const QVector<VersionPtr> &versions)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_versions = versions;
|
2017-03-11 06:09:45 +05:30
|
|
|
std::sort(m_versions.begin(), m_versions.end(), [](const VersionPtr &a, const VersionPtr &b)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return a->rawTime() > b->rawTime();
|
|
|
|
});
|
|
|
|
for (int i = 0; i < m_versions.size(); ++i)
|
|
|
|
{
|
|
|
|
m_lookup.insert(m_versions.at(i)->version(), m_versions.at(i));
|
|
|
|
setupAddedVersion(i, m_versions.at(i));
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
auto recommendedIt = std::find_if(m_versions.constBegin(), m_versions.constEnd(), [](const VersionPtr &ptr) { return ptr->type() == "release"; });
|
2016-04-07 02:39:30 +05:30
|
|
|
m_recommended = recommendedIt == m_versions.constEnd() ? nullptr : *recommendedIt;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:54 +05:30
|
|
|
void VersionList::parse(const QJsonObject& obj)
|
|
|
|
{
|
|
|
|
parseVersionList(obj, this);
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void VersionList::merge(const BaseEntity::Ptr &other)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-03-11 06:09:45 +05:30
|
|
|
const VersionListPtr list = std::dynamic_pointer_cast<VersionList>(other);
|
2016-04-07 02:39:30 +05:30
|
|
|
if (m_name != list->m_name)
|
|
|
|
{
|
|
|
|
setName(list->m_name);
|
|
|
|
}
|
|
|
|
|
2017-03-24 06:56:06 +05:30
|
|
|
if(m_parentUid != list->m_parentUid)
|
|
|
|
{
|
|
|
|
setParentUid(list->m_parentUid);
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:00:51 +05:30
|
|
|
// TODO: do not reset the whole model. maybe?
|
|
|
|
beginResetModel();
|
|
|
|
m_versions.clear();
|
|
|
|
for (const VersionPtr &version : list->m_versions)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-04-24 05:00:51 +05:30
|
|
|
// we already have the version. merge the contents
|
|
|
|
if (m_lookup.contains(version->version()))
|
|
|
|
{
|
|
|
|
m_lookup.value(version->version())->merge(version);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_lookup.insert(version->uid(), version);
|
|
|
|
}
|
|
|
|
// connect it.
|
|
|
|
setupAddedVersion(m_versions.size(), version);
|
|
|
|
m_versions.append(version);
|
|
|
|
if (!m_recommended || (version->type() == "release" && version->rawTime() > m_recommended->rawTime()))
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-04-24 05:00:51 +05:30
|
|
|
m_recommended = version;
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
}
|
2017-04-24 05:00:51 +05:30
|
|
|
endResetModel();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void VersionList::setupAddedVersion(const int row, const VersionPtr &version)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2017-04-24 05:00:51 +05:30
|
|
|
// FIXME: do not disconnect from everythin, disconnect only the lambdas here
|
|
|
|
version->disconnect();
|
2017-03-11 06:09:45 +05:30
|
|
|
connect(version.get(), &Version::requiresChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << RequiresRole); });
|
|
|
|
connect(version.get(), &Version::timeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TimeRole << SortRole); });
|
|
|
|
connect(version.get(), &Version::typeChanged, this, [this, row]() { emit dataChanged(index(row), index(row), QVector<int>() << TypeRole); });
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
BaseVersionPtr VersionList::getRecommended() const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
return m_recommended;
|
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
}
|
|
|
|
|
2017-03-24 06:56:06 +05:30
|
|
|
void Meta::VersionList::setParentUid(const QString& parentUid)
|
|
|
|
{
|
|
|
|
m_parentUid = parentUid;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:54 +05:30
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
#include "VersionList.moc"
|