mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-23 13:04:23 +00:00
Merge pull request #5264 from evsh/plugin-version-class
Use explicit class for search plugin versions
This commit is contained in:
commit
ead1f6e1f6
@ -48,6 +48,7 @@ utils/misc.h
|
|||||||
utils/net.h
|
utils/net.h
|
||||||
utils/random.h
|
utils/random.h
|
||||||
utils/string.h
|
utils/string.h
|
||||||
|
utils/version.h
|
||||||
filesystemwatcher.h
|
filesystemwatcher.h
|
||||||
iconprovider.h
|
iconprovider.h
|
||||||
indexrange.h
|
indexrange.h
|
||||||
|
@ -54,6 +54,7 @@ HEADERS += \
|
|||||||
$$PWD/utils/net.h \
|
$$PWD/utils/net.h \
|
||||||
$$PWD/utils/random.h \
|
$$PWD/utils/random.h \
|
||||||
$$PWD/utils/string.h \
|
$$PWD/utils/string.h \
|
||||||
|
$$PWD/utils/version.h \
|
||||||
$$PWD/profile.h \
|
$$PWD/profile.h \
|
||||||
$$PWD/private/profile_p.h \
|
$$PWD/private/profile_p.h \
|
||||||
$$PWD/unicodestrings.h \
|
$$PWD/unicodestrings.h \
|
||||||
|
@ -177,11 +177,11 @@ void SearchEngine::installPlugin(const QString &source)
|
|||||||
|
|
||||||
void SearchEngine::installPlugin_impl(const QString &name, const QString &path)
|
void SearchEngine::installPlugin_impl(const QString &name, const QString &path)
|
||||||
{
|
{
|
||||||
qreal newVersion = getPluginVersion(path);
|
PluginVersion newVersion = getPluginVersion(path);
|
||||||
qDebug("Version to be installed: %.2f", newVersion);
|
qDebug() << "Version to be installed:" << newVersion;
|
||||||
|
|
||||||
PluginInfo *plugin = pluginInfo(name);
|
PluginInfo *plugin = pluginInfo(name);
|
||||||
if (plugin && (plugin->version >= newVersion)) {
|
if (plugin && !(plugin->version < newVersion)) {
|
||||||
qDebug("Apparently update is not needed, we have a more recent version");
|
qDebug("Apparently update is not needed, we have a more recent version");
|
||||||
emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed."));
|
emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed."));
|
||||||
return;
|
return;
|
||||||
@ -608,7 +608,7 @@ void SearchEngine::parseVersionInfo(const QByteArray &info)
|
|||||||
{
|
{
|
||||||
qDebug("Checking if update is needed");
|
qDebug("Checking if update is needed");
|
||||||
|
|
||||||
QHash<QString, qreal> updateInfo;
|
QHash<QString, PluginVersion> updateInfo;
|
||||||
bool dataCorrect = false;
|
bool dataCorrect = false;
|
||||||
QList<QByteArray> lines = info.split('\n');
|
QList<QByteArray> lines = info.split('\n');
|
||||||
foreach (QByteArray line, lines) {
|
foreach (QByteArray line, lines) {
|
||||||
@ -624,14 +624,17 @@ void SearchEngine::parseVersionInfo(const QByteArray &info)
|
|||||||
|
|
||||||
pluginName.chop(1); // remove trailing ':'
|
pluginName.chop(1); // remove trailing ':'
|
||||||
bool ok;
|
bool ok;
|
||||||
qreal version = list.last().toFloat(&ok);
|
qreal versionParseTest = list.last().toFloat(&ok);
|
||||||
qDebug("read line %s: %.2f", qPrintable(pluginName), version);
|
qDebug("read line %s: %.2f", qPrintable(pluginName), versionParseTest);
|
||||||
if (!ok) continue;
|
if (!ok) continue;
|
||||||
|
|
||||||
dataCorrect = true;
|
PluginVersion version = PluginVersion::tryParse(list.last(), {});
|
||||||
if (isUpdateNeeded(pluginName, version)) {
|
if (version != PluginVersion()) {
|
||||||
qDebug("Plugin: %s is outdated", qPrintable(pluginName));
|
dataCorrect = true;
|
||||||
updateInfo[pluginName] = version;
|
if (isUpdateNeeded(pluginName, version)) {
|
||||||
|
qDebug("Plugin: %s is outdated", qPrintable(pluginName));
|
||||||
|
updateInfo[pluginName] = version;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -641,13 +644,13 @@ void SearchEngine::parseVersionInfo(const QByteArray &info)
|
|||||||
emit checkForUpdatesFinished(updateInfo);
|
emit checkForUpdatesFinished(updateInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SearchEngine::isUpdateNeeded(QString pluginName, qreal newVersion) const
|
bool SearchEngine::isUpdateNeeded(QString pluginName, PluginVersion newVersion) const
|
||||||
{
|
{
|
||||||
PluginInfo *plugin = pluginInfo(pluginName);
|
PluginInfo *plugin = pluginInfo(pluginName);
|
||||||
if (!plugin) return true;
|
if (!plugin) return true;
|
||||||
|
|
||||||
qreal oldVersion = plugin->version;
|
PluginVersion oldVersion = plugin->version;
|
||||||
qDebug("IsUpdate needed? to be installed: %.2f, already installed: %.2f", newVersion, oldVersion);
|
qDebug() << "IsUpdate needed? to be installed:" << newVersion << ", already installed:" << oldVersion;
|
||||||
return (newVersion > oldVersion);
|
return (newVersion > oldVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -673,27 +676,26 @@ QHash<QString, QString> SearchEngine::initializeCategoryNames()
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
qreal SearchEngine::getPluginVersion(QString filePath)
|
PluginVersion SearchEngine::getPluginVersion(QString filePath)
|
||||||
{
|
{
|
||||||
QFile plugin(filePath);
|
QFile plugin(filePath);
|
||||||
if (!plugin.exists()) {
|
if (!plugin.exists()) {
|
||||||
qDebug("%s plugin does not exist, returning 0.0", qPrintable(filePath));
|
qDebug("%s plugin does not exist, returning 0.0", qPrintable(filePath));
|
||||||
return 0.0;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!plugin.open(QIODevice::ReadOnly | QIODevice::Text))
|
if (!plugin.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||||
return 0.0;
|
return {};
|
||||||
|
|
||||||
qreal version = 0.0;
|
PluginVersion version;
|
||||||
while (!plugin.atEnd()) {
|
while (!plugin.atEnd()) {
|
||||||
QByteArray line = plugin.readLine();
|
QByteArray line = plugin.readLine();
|
||||||
if (line.startsWith("#VERSION: ")) {
|
if (line.startsWith("#VERSION: ")) {
|
||||||
line = line.split(' ').last().trimmed();
|
line = line.split(' ').last().trimmed();
|
||||||
version = line.toFloat();
|
version = PluginVersion::tryParse(line, {});
|
||||||
qDebug("plugin %s version: %.2f", qPrintable(filePath), version);
|
qDebug() << "plugin" << filePath << "version: " << version;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
@ -30,18 +30,23 @@
|
|||||||
#ifndef SEARCHENGINE_H
|
#ifndef SEARCHENGINE_H
|
||||||
#define SEARCHENGINE_H
|
#define SEARCHENGINE_H
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QStringList>
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
#include <QMetaType>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
#include "base/utils/version.h"
|
||||||
|
|
||||||
class QProcess;
|
class QProcess;
|
||||||
class QTimer;
|
class QTimer;
|
||||||
|
|
||||||
|
using PluginVersion = Utils::Version<unsigned short, 2>;
|
||||||
|
Q_DECLARE_METATYPE(PluginVersion)
|
||||||
|
|
||||||
struct PluginInfo
|
struct PluginInfo
|
||||||
{
|
{
|
||||||
QString name;
|
QString name;
|
||||||
qreal version;
|
PluginVersion version;
|
||||||
QString fullName;
|
QString fullName;
|
||||||
QString url;
|
QString url;
|
||||||
QStringList supportedCategories;
|
QStringList supportedCategories;
|
||||||
@ -86,7 +91,7 @@ public:
|
|||||||
|
|
||||||
void downloadTorrent(const QString &siteUrl, const QString &url);
|
void downloadTorrent(const QString &siteUrl, const QString &url);
|
||||||
|
|
||||||
static qreal getPluginVersion(QString filePath);
|
static PluginVersion getPluginVersion(QString filePath);
|
||||||
static QString categoryFullName(const QString &categoryName);
|
static QString categoryFullName(const QString &categoryName);
|
||||||
QString pluginFullName(const QString &pluginName);
|
QString pluginFullName(const QString &pluginName);
|
||||||
static QString pluginsLocation();
|
static QString pluginsLocation();
|
||||||
@ -102,7 +107,7 @@ signals:
|
|||||||
void pluginUpdated(const QString &name);
|
void pluginUpdated(const QString &name);
|
||||||
void pluginUpdateFailed(const QString &name, const QString &reason);
|
void pluginUpdateFailed(const QString &name, const QString &reason);
|
||||||
|
|
||||||
void checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo);
|
void checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo);
|
||||||
void checkForUpdatesFailed(const QString &reason);
|
void checkForUpdatesFailed(const QString &reason);
|
||||||
|
|
||||||
void torrentFileDownloaded(const QString &path);
|
void torrentFileDownloaded(const QString &path);
|
||||||
@ -123,7 +128,7 @@ private:
|
|||||||
bool parseSearchResult(const QString &line, SearchResult &searchResult);
|
bool parseSearchResult(const QString &line, SearchResult &searchResult);
|
||||||
void parseVersionInfo(const QByteArray &info);
|
void parseVersionInfo(const QByteArray &info);
|
||||||
void installPlugin_impl(const QString &name, const QString &path);
|
void installPlugin_impl(const QString &name, const QString &path);
|
||||||
bool isUpdateNeeded(QString pluginName, qreal newVersion) const;
|
bool isUpdateNeeded(QString pluginName, PluginVersion newVersion) const;
|
||||||
|
|
||||||
static QString engineLocation();
|
static QString engineLocation();
|
||||||
static QString pluginPath(const QString &name);
|
static QString pluginPath(const QString &name);
|
||||||
|
195
src/base/utils/version.h
Normal file
195
src/base/utils/version.h
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
|
* Copyright (C) 2016 Eugene Shalygin
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give permission to
|
||||||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||||||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||||||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
* exception statement from your version.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef QBITTORRENT_UTILS_VERSION_H
|
||||||
|
#define QBITTORRENT_UTILS_VERSION_H
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QString>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
namespace Utils
|
||||||
|
{
|
||||||
|
template <typename T, std::size_t N, std::size_t Mandatory = N>
|
||||||
|
class Version
|
||||||
|
{
|
||||||
|
static_assert(N > 0, "The number of version components may not be smaller than 1");
|
||||||
|
static_assert(N >= Mandatory,
|
||||||
|
"The number of mandatory components may not be larger than the total number of components");
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef T ComponentType;
|
||||||
|
typedef Version<T, N, Mandatory> ThisType;
|
||||||
|
|
||||||
|
constexpr Version()
|
||||||
|
: m_components {}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Version(const ThisType &other) = default;
|
||||||
|
|
||||||
|
template <typename ... Other>
|
||||||
|
constexpr Version(Other ... components)
|
||||||
|
: m_components {{components ...}}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates version from string in format "x.y.z"
|
||||||
|
*
|
||||||
|
* @param version Version string in format "x.y.z"
|
||||||
|
* @throws std::runtime_error if parsing fails
|
||||||
|
*/
|
||||||
|
Version(const QString &version)
|
||||||
|
: Version {version.split(QLatin1Char('.'))}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates version from byte array in format "x.y.z"
|
||||||
|
*
|
||||||
|
* @param version Version string in format "x.y.z"
|
||||||
|
* @throws std::runtime_error if parsing fails
|
||||||
|
*/
|
||||||
|
Version(const QByteArray &version)
|
||||||
|
: Version {version.split('.')}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentType majorNumber() const
|
||||||
|
{
|
||||||
|
static_assert(N >= 1, "The number of version components is too small");
|
||||||
|
return (*this)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentType minorNumber() const
|
||||||
|
{
|
||||||
|
static_assert(N >= 2, "The number of version components is too small");
|
||||||
|
return (*this)[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentType revisionNumber() const
|
||||||
|
{
|
||||||
|
static_assert(N >= 3, "The number of version components is too small");
|
||||||
|
return (*this)[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentType patchNumber() const
|
||||||
|
{
|
||||||
|
static_assert(N >= 4, "The number of version components is too small");
|
||||||
|
return (*this)[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentType operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
return m_components.at(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
operator QString() const
|
||||||
|
{
|
||||||
|
// find the last one non-zero component
|
||||||
|
std::size_t lastSignificantIndex = N - 1;
|
||||||
|
while (lastSignificantIndex >= 0 && (*this)[lastSignificantIndex] == 0)
|
||||||
|
--lastSignificantIndex;
|
||||||
|
|
||||||
|
if (lastSignificantIndex + 1 < Mandatory) // lastSignificantIndex >= 0
|
||||||
|
lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1
|
||||||
|
|
||||||
|
QString res = QString::number((*this)[0]);
|
||||||
|
for (std::size_t i = 1; i <= lastSignificantIndex; ++i)
|
||||||
|
res += QLatin1Char('.') + QString::number((*this)[i]);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const ThisType &other) const
|
||||||
|
{
|
||||||
|
return m_components == other.m_components;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const ThisType &other) const
|
||||||
|
{
|
||||||
|
return m_components < other.m_components;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>(const ThisType &other) const
|
||||||
|
{
|
||||||
|
return m_components > other.m_components;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename StringClassWithSplitMethod>
|
||||||
|
static Version tryParse(const StringClassWithSplitMethod &s, const Version &defaultVersion)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return Version(s);
|
||||||
|
}
|
||||||
|
catch (std::runtime_error &er) {
|
||||||
|
qDebug() << "Error parsing version:" << er.what();
|
||||||
|
return defaultVersion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using ComponentsArray = std::array<T, N>;
|
||||||
|
|
||||||
|
template <typename StringsList>
|
||||||
|
static ComponentsArray parseList(const StringsList &versionParts)
|
||||||
|
{
|
||||||
|
if ((static_cast<std::size_t>(versionParts.size()) > N)
|
||||||
|
|| (static_cast<std::size_t>(versionParts.size()) < Mandatory))
|
||||||
|
throw std::runtime_error ("Incorrect number of version components");
|
||||||
|
|
||||||
|
bool ok = false;
|
||||||
|
ComponentsArray res;
|
||||||
|
for (std::size_t i = 0; i < static_cast<std::size_t>(versionParts.size()); ++i) {
|
||||||
|
res[i] = static_cast<T>(versionParts[i].toInt(&ok));
|
||||||
|
if (!ok)
|
||||||
|
throw std::runtime_error("Can not parse version component");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename StringsList>
|
||||||
|
Version(const StringsList &versionParts)
|
||||||
|
: m_components (parseList(versionParts)) // GCC 4.8.4 has problems with the uniform initializer here
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentsArray m_components;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, std::size_t N, std::size_t Mandatory>
|
||||||
|
inline bool operator!=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // QBITTORRENT_UTILS_VERSION_H
|
@ -298,7 +298,7 @@ void PluginSelectDlg::addNewPlugin(QString pluginName)
|
|||||||
connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(iconDownloaded(QString, QString)));
|
connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(iconDownloaded(QString, QString)));
|
||||||
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(iconDownloadFailed(QString, QString)));
|
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(iconDownloadFailed(QString, QString)));
|
||||||
}
|
}
|
||||||
item->setText(PLUGIN_VERSION, QString::number(plugin->version, 'f', 2));
|
item->setText(PLUGIN_VERSION, plugin->version);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PluginSelectDlg::startAsyncOp()
|
void PluginSelectDlg::startAsyncOp()
|
||||||
@ -424,9 +424,9 @@ void PluginSelectDlg::pluginInstallationFailed(const QString &name, const QStrin
|
|||||||
void PluginSelectDlg::pluginUpdated(const QString &name)
|
void PluginSelectDlg::pluginUpdated(const QString &name)
|
||||||
{
|
{
|
||||||
finishAsyncOp();
|
finishAsyncOp();
|
||||||
qreal version = m_pluginManager->pluginInfo(name)->version;
|
PluginVersion version = m_pluginManager->pluginInfo(name)->version;
|
||||||
QTreeWidgetItem *item = findItemWithID(name);
|
QTreeWidgetItem *item = findItemWithID(name);
|
||||||
item->setText(PLUGIN_VERSION, QString::number(version, 'f', 2));
|
item->setText(PLUGIN_VERSION, version);
|
||||||
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully updated.", "%1 is the name of the search engine").arg(name));
|
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully updated.", "%1 is the name of the search engine").arg(name));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user