2017-01-08 09:28:05 +05:30
|
|
|
/* Copyright 2013-2017 MultiMC Contributors
|
2013-05-17 22:23:22 +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
|
2013-10-06 04:43:40 +05:30
|
|
|
*
|
2013-05-17 22:23:22 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-29 04:29:35 +05:30
|
|
|
#include "LwjglVersionList.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "Env.h"
|
2013-05-17 22:23:22 +05:30
|
|
|
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QtXml>
|
|
|
|
#include <QRegExp>
|
|
|
|
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
#define RSS_URL "https://sourceforge.net/projects/java-game-lib/rss"
|
2013-05-17 22:23:22 +05:30
|
|
|
|
2015-02-02 05:39:28 +05:30
|
|
|
LWJGLVersionList::LWJGLVersionList(QObject *parent) : BaseVersionList(parent)
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LWJGLVersionList::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
if (index.row() > count())
|
|
|
|
return QVariant();
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2015-02-02 05:39:28 +05:30
|
|
|
const PtrLWJGLVersion version = m_vlist.at(index.row());
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2013-06-23 03:04:33 +05:30
|
|
|
return version->name();
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
case Qt::ToolTipRole:
|
2013-08-07 05:08:18 +05:30
|
|
|
return version->url();
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LWJGLVersionList::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2014-11-02 23:54:28 +05:30
|
|
|
return tr("Version");
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
case Qt::ToolTipRole:
|
2014-11-02 23:54:28 +05:30
|
|
|
return tr("LWJGL version name.");
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2013-05-17 22:23:22 +05:30
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LWJGLVersionList::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::loadList()
|
|
|
|
{
|
2016-11-27 06:13:20 +05:30
|
|
|
if(m_loading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_loading = true;
|
|
|
|
|
|
|
|
qDebug() << "Downloading LWJGL RSS...";
|
|
|
|
m_rssDLJob.reset(new NetJob("LWJGL RSS"));
|
|
|
|
m_rssDL = Net::Download::makeByteArray(QUrl(RSS_URL), &m_rssData);
|
|
|
|
m_rssDLJob->addNetAction(m_rssDL);
|
|
|
|
connect(m_rssDLJob.get(), &NetJob::failed, this, &LWJGLVersionList::rssFailed);
|
|
|
|
connect(m_rssDLJob.get(), &NetJob::succeeded, this, &LWJGLVersionList::rssSucceeded);
|
|
|
|
m_rssDLJob->start();
|
2013-05-17 22:23:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
|
|
|
|
{
|
|
|
|
QDomNodeList elementList = parent.elementsByTagName(tagname);
|
|
|
|
if (elementList.count())
|
|
|
|
return elementList.at(0).toElement();
|
|
|
|
else
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
void LWJGLVersionList::rssFailed(const QString& reason)
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
2017-03-20 04:28:54 +05:30
|
|
|
m_rssDLJob.reset();
|
2016-11-27 06:13:20 +05:30
|
|
|
m_loading = false;
|
|
|
|
qWarning() << "Failed to load LWJGL list. Network error: " + reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::rssSucceeded()
|
|
|
|
{
|
|
|
|
QRegExp lwjglRegex("lwjgl-(([0-9]\\.?)+)\\.zip");
|
|
|
|
Q_ASSERT_X(lwjglRegex.isValid(), "load LWJGL list", "LWJGL regex is invalid");
|
|
|
|
|
|
|
|
QDomDocument doc;
|
|
|
|
|
|
|
|
QString xmlErrorMsg;
|
|
|
|
int errorLine;
|
|
|
|
|
|
|
|
if (!doc.setContent(m_rssData, false, &xmlErrorMsg, &errorLine))
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
2016-11-27 06:13:20 +05:30
|
|
|
qWarning() << "Failed to load LWJGL list. XML error: " + xmlErrorMsg + " at line " + QString::number(errorLine);
|
2017-03-20 04:28:54 +05:30
|
|
|
m_rssDLJob.reset();
|
2016-11-27 06:13:20 +05:30
|
|
|
m_rssData.clear();
|
2017-03-20 04:28:54 +05:30
|
|
|
m_loading = false;
|
2016-11-27 06:13:20 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_rssData.clear();
|
|
|
|
|
|
|
|
QDomNodeList items = doc.elementsByTagName("item");
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
QList<PtrLWJGLVersion> tempList;
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
for (int i = 0; i < items.length(); i++)
|
|
|
|
{
|
|
|
|
Q_ASSERT_X(items.at(i).isElement(), "load LWJGL list", "XML element isn't an element... wat?");
|
|
|
|
|
|
|
|
QDomElement linkElement = getDomElementByTagName(items.at(i).toElement(), "link");
|
|
|
|
if (linkElement.isNull())
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
2016-11-27 06:13:20 +05:30
|
|
|
qDebug() << "Link element" << i << "in RSS feed doesn't exist! Skipping.";
|
|
|
|
continue;
|
2013-05-17 22:23:22 +05:30
|
|
|
}
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
QString link = linkElement.text();
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
// Make sure it's a download link.
|
|
|
|
if (link.endsWith("/download") && link.contains(lwjglRegex))
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
2016-11-27 06:13:20 +05:30
|
|
|
QString name = link.mid(lwjglRegex.indexIn(link) + 6);
|
|
|
|
// Subtract 4 here to remove the .zip file extension.
|
|
|
|
name = name.left(lwjglRegex.matchedLength() - 10);
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
QUrl url(link);
|
|
|
|
if (!url.isValid())
|
2013-05-17 22:23:22 +05:30
|
|
|
{
|
2016-11-27 06:13:20 +05:30
|
|
|
qWarning() << "LWJGL version URL isn't valid:" << link << "Skipping.";
|
2013-05-17 22:23:22 +05:30
|
|
|
continue;
|
|
|
|
}
|
2016-11-27 06:13:20 +05:30
|
|
|
qDebug() << "Discovered LWGL version" << name << "at" << link;
|
|
|
|
tempList.append(std::make_shared<LWJGLVersion>(name, link));
|
2013-05-17 22:23:22 +05:30
|
|
|
}
|
|
|
|
}
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
beginResetModel();
|
|
|
|
m_vlist.swap(tempList);
|
|
|
|
endResetModel();
|
2013-05-17 22:23:22 +05:30
|
|
|
|
2016-11-27 06:13:20 +05:30
|
|
|
qDebug() << "Loaded LWJGL list.";
|
2017-03-20 04:28:54 +05:30
|
|
|
m_rssDLJob.reset();
|
2016-11-27 06:13:20 +05:30
|
|
|
m_loading = false;
|
2013-05-17 22:23:22 +05:30
|
|
|
}
|