2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-05-17 17:53:22 +01:00
|
|
|
*
|
|
|
|
* 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 00:13:40 +01:00
|
|
|
*
|
2013-05-17 17:53:22 +01:00
|
|
|
* 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-28 23:59:35 +01:00
|
|
|
#include "LwjglVersionList.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "Env.h"
|
2013-05-17 17:53:22 +01:00
|
|
|
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QtXml>
|
|
|
|
#include <QRegExp>
|
|
|
|
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2014-07-20 11:59:44 +01:00
|
|
|
#define RSS_URL "http://sourceforge.net/projects/java-game-lib/rss"
|
2013-05-17 17:53:22 +01:00
|
|
|
|
2015-02-02 00:09:28 +00:00
|
|
|
LWJGLVersionList::LWJGLVersionList(QObject *parent) : BaseVersionList(parent)
|
2013-05-17 17:53:22 +01:00
|
|
|
{
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LWJGLVersionList::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
if (index.row() > count())
|
|
|
|
return QVariant();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2015-02-02 00:09:28 +00:00
|
|
|
const PtrLWJGLVersion version = m_vlist.at(index.row());
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2013-06-22 22:34:33 +01:00
|
|
|
return version->name();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
case Qt::ToolTipRole:
|
2013-08-07 00:38:18 +01:00
|
|
|
return version->url();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LWJGLVersionList::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2014-11-02 18:24:28 +00:00
|
|
|
return tr("Version");
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
case Qt::ToolTipRole:
|
2014-11-02 18:24:28 +00:00
|
|
|
return tr("LWJGL version name.");
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int LWJGLVersionList::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LWJGLVersionList::isLoading() const
|
|
|
|
{
|
|
|
|
return m_loading;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::loadList()
|
|
|
|
{
|
|
|
|
Q_ASSERT_X(!m_loading, "loadList", "list is already loading (m_loading is true)");
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
setLoading(true);
|
2013-11-06 19:59:44 +00:00
|
|
|
QNetworkRequest req(QUrl(RSS_URL));
|
2014-07-20 11:59:44 +01:00
|
|
|
req.setRawHeader("Accept", "application/rss+xml, text/xml, */*");
|
2013-11-06 19:59:44 +00:00
|
|
|
req.setRawHeader("User-Agent", "MultiMC/5.0 (Uncached)");
|
2016-11-06 20:58:54 +00:00
|
|
|
reply = ENV.qnam().get(req);
|
2013-05-17 17:53:22 +01:00
|
|
|
connect(reply, SIGNAL(finished()), SLOT(netRequestComplete()));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
|
|
|
|
{
|
|
|
|
QDomNodeList elementList = parent.elementsByTagName(tagname);
|
|
|
|
if (elementList.count())
|
|
|
|
return elementList.at(0).toElement();
|
|
|
|
else
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::netRequestComplete()
|
|
|
|
{
|
|
|
|
if (reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
QRegExp lwjglRegex("lwjgl-(([0-9]\\.?)+)\\.zip");
|
2013-10-06 00:13:40 +01:00
|
|
|
Q_ASSERT_X(lwjglRegex.isValid(), "load LWJGL list", "LWJGL regex is invalid");
|
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QDomDocument doc;
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QString xmlErrorMsg;
|
|
|
|
int errorLine;
|
2014-07-20 11:59:44 +01:00
|
|
|
auto rawData = reply->readAll();
|
|
|
|
if (!doc.setContent(rawData, false, &xmlErrorMsg, &errorLine))
|
2013-05-17 17:53:22 +01:00
|
|
|
{
|
2013-10-06 00:13:40 +01:00
|
|
|
failed("Failed to load LWJGL list. XML error: " + xmlErrorMsg + " at line " +
|
|
|
|
QString::number(errorLine));
|
2013-05-17 17:53:22 +01:00
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QDomNodeList items = doc.elementsByTagName("item");
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-06-22 22:34:33 +01:00
|
|
|
QList<PtrLWJGLVersion> tempList;
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
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?");
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QDomElement linkElement = getDomElementByTagName(items.at(i).toElement(), "link");
|
|
|
|
if (linkElement.isNull())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Link element" << i << "in RSS feed doesn't exist! Skipping.";
|
2013-05-17 17:53:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QString link = linkElement.text();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
// Make sure it's a download link.
|
|
|
|
if (link.endsWith("/download") && link.contains(lwjglRegex))
|
|
|
|
{
|
2013-08-07 00:38:18 +01:00
|
|
|
QString name = link.mid(lwjglRegex.indexIn(link) + 6);
|
2013-05-17 17:53:22 +01:00
|
|
|
// Subtract 4 here to remove the .zip file extension.
|
2013-08-07 00:38:18 +01:00
|
|
|
name = name.left(lwjglRegex.matchedLength() - 10);
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
QUrl url(link);
|
|
|
|
if (!url.isValid())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << "LWJGL version URL isn't valid:" << link << "Skipping.";
|
2013-05-17 17:53:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Discovered LWGL version" << name << "at" << link;
|
2015-02-02 00:09:28 +00:00
|
|
|
tempList.append(std::make_shared<LWJGLVersion>(name, link));
|
2013-05-17 17:53:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
beginResetModel();
|
|
|
|
m_vlist.swap(tempList);
|
|
|
|
endResetModel();
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Loaded LWJGL list.";
|
2013-05-17 17:53:22 +01:00
|
|
|
finished();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failed("Failed to load LWJGL list. Network error: " + reply->errorString());
|
|
|
|
}
|
2013-10-06 00:13:40 +01:00
|
|
|
|
2013-05-17 17:53:22 +01:00
|
|
|
setLoading(false);
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::failed(QString msg)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << msg;
|
2013-05-17 17:53:22 +01:00
|
|
|
emit loadListFailed(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::finished()
|
|
|
|
{
|
|
|
|
emit loadListFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LWJGLVersionList::setLoading(bool loading)
|
|
|
|
{
|
|
|
|
m_loading = loading;
|
|
|
|
emit loadingStateUpdated(m_loading);
|
|
|
|
}
|