File indexing completed on 2024-05-05 05:40:34
0001 /************************************************************************* 0002 * Copyright (C) 2011 by Renaud Guezennec * 0003 * Copyright (C) 2011 by Joseph Boudou * 0004 * * 0005 * https://rolisteam.org/ * 0006 * * 0007 * Rolisteam is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published * 0009 * by the Free Software Foundation; either version 2 of the License, * 0010 * or (at your option) any later version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 *************************************************************************/ 0022 0023 #include "services/updatechecker.h" 0024 0025 #include <QDebug> 0026 #include <QNetworkReply> 0027 #include <QNetworkRequest> 0028 #include <QRegularExpression> 0029 #include <QStringList> 0030 #include <QUrl> 0031 /***************** 0032 * UpdateChecker * 0033 *****************/ 0034 0035 UpdateChecker::UpdateChecker(const QString& version, QObject* obj) 0036 : QObject(obj), m_localVersion(version), m_manager(nullptr) 0037 { 0038 } 0039 0040 bool UpdateChecker::needUpdate() 0041 { 0042 return m_needUpdate; 0043 } 0044 0045 void UpdateChecker::startChecking() 0046 { 0047 #ifdef HAVE_QT_NETWORK 0048 m_manager.reset(new QNetworkAccessManager); 0049 connect(m_manager.get(), &QNetworkAccessManager::finished, this, &UpdateChecker::readXML); 0050 m_manager->get(QNetworkRequest(QUrl("https://rolisteam.org/version.xml"))); 0051 #endif 0052 } 0053 0054 QString UpdateChecker::getLatestVersion() 0055 { 0056 return m_remoteVersion; 0057 } 0058 0059 QString UpdateChecker::getLatestVersionDate() 0060 { 0061 return m_dateRemoteVersion; 0062 } 0063 void UpdateChecker::readXML(QNetworkReply* p) 0064 { 0065 if(p->error() != QNetworkReply::NoError) 0066 { 0067 m_noErrror= false; 0068 emit checkFinished(); 0069 return; 0070 } 0071 0072 QByteArray a= p->readAll(); 0073 QString string(a); 0074 auto func= [](const QString& string, const QRegularExpression& reg) -> QString { 0075 auto match= reg.match(string); 0076 if(match.hasMatch()) 0077 return match.captured(0); 0078 return {}; 0079 }; 0080 0081 int major= func(string, QRegularExpression("<version_major>(.*)</version_major>")).toInt(); 0082 int middle= func(string, QRegularExpression("<version_middle>(.*)</version_middle>")).toInt(); 0083 int minor= func(string, QRegularExpression("<version_minor>(.*)</version_minor>")).toInt(); 0084 m_dateRemoteVersion= func(string, QRegularExpression("<date>(.*)</date>")); 0085 m_changeLog= func(string, QRegularExpression("<changelog>(.*)</changelog>")); 0086 0087 m_remoteVersion= QString("%1.%2.%3").arg(major).arg(middle).arg(minor); 0088 0089 setNeedUpdate(m_remoteVersion != m_localVersion); 0090 emit checkFinished(); 0091 } 0092 0093 void UpdateChecker::setNeedUpdate(bool b) 0094 { 0095 if(m_needUpdate == b) 0096 return; 0097 m_needUpdate= b; 0098 emit needUpdateChanged(); 0099 }