File indexing completed on 2024-12-22 04:13:04
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Anna Medonosova <anna.medonosova@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 0008 #include "KisManualUpdater.h" 0009 #include <KisMultiFeedRSSModel.h> 0010 #include <KritaVersionWrapper.h> 0011 0012 #include <QModelIndex> 0013 #include <QRegularExpression> 0014 #include <QRegularExpressionMatch> 0015 #include <QVersionNumber> 0016 0017 #include <kis_debug.h> 0018 0019 0020 KisManualUpdater::KisManualUpdater() 0021 : m_currentVersion(KritaVersionWrapper::versionString()) 0022 { 0023 m_rssModel.reset(new MultiFeedRssModel()); 0024 } 0025 0026 KisManualUpdater::KisManualUpdater(MultiFeedRssModel* rssModel, QString ¤tVersion) 0027 : m_currentVersion(currentVersion) 0028 { 0029 m_rssModel.reset(rssModel); 0030 } 0031 0032 void KisManualUpdater::checkForUpdate() 0033 { 0034 connect(m_rssModel.data(), SIGNAL(feedDataChanged()), this, SLOT(rssDataChanged())); 0035 m_rssModel->addFeed(QLatin1String("https://krita.org/en/feed/")); 0036 } 0037 0038 void KisManualUpdater::rssDataChanged() 0039 { 0040 // grab the latest release post and URL for reference later 0041 // if we need to update 0042 QString availableVersion; 0043 QString downloadLink; 0044 0045 for (int i = 0; i < m_rssModel->rowCount(); i++) { 0046 const QModelIndex &idx = m_rssModel->index(i); 0047 0048 if (idx.isValid()) { 0049 // only use official release announcements to get version number 0050 if ( idx.data(KisRssReader::RssRoles::CategoryRole).toString() != "Official Release") { 0051 continue; 0052 } 0053 0054 QString linkTitle = idx.data(KisRssReader::RssRoles::TitleRole).toString(); 0055 0056 // regex to capture version number 0057 QRegularExpression versionRegex("\\d\\.\\d\\.?\\d?\\.?\\d"); 0058 QRegularExpressionMatch matched = versionRegex.match(linkTitle); 0059 0060 // only take the top match for release version since that is the newest 0061 if (matched.hasMatch()) { 0062 availableVersion = matched.captured(0); 0063 downloadLink = idx.data(KisRssReader::RssRoles::LinkRole).toString(); 0064 break; 0065 } 0066 } 0067 } 0068 0069 UpdaterStatus::StatusID status; 0070 0071 if (availableVersionIsHigher(m_currentVersion, availableVersion)) { 0072 status = UpdaterStatus::StatusID::UPDATE_AVAILABLE; 0073 } else { 0074 status = UpdaterStatus::StatusID::UPTODATE; 0075 } 0076 0077 m_updaterStatus.setStatus(status); 0078 m_updaterStatus.setAvailableVersion(availableVersion); 0079 m_updaterStatus.setDownloadLink(downloadLink); 0080 0081 emit sigUpdateCheckStateChange(m_updaterStatus); 0082 } 0083 0084 bool KisManualUpdater::availableVersionIsHigher(QString currentVersion, QString availableVersion) 0085 { 0086 if (currentVersion.isEmpty() || availableVersion.isEmpty()) { 0087 return false; 0088 } 0089 0090 int currentSuffixIndex {5}; 0091 int availableSuffixIndex {5}; 0092 0093 QVersionNumber currentVersionNumber = QVersionNumber::fromString(currentVersion, ¤tSuffixIndex); 0094 QVersionNumber availableVersionNumber = QVersionNumber::fromString(availableVersion, &availableSuffixIndex); 0095 0096 QString currentSuffix = currentVersion.mid(currentSuffixIndex); 0097 QString availableSuffix = availableVersion.mid(availableSuffixIndex); 0098 0099 if (currentVersionNumber.normalized() == availableVersionNumber.normalized()) { 0100 if (!currentSuffix.isEmpty() && availableSuffix.isEmpty()) { 0101 return true; 0102 } else { 0103 return false; 0104 } 0105 } else { 0106 return (currentVersionNumber.normalized() < availableVersionNumber.normalized()); 0107 } 0108 }