From ad5c88be3d31665f6eba9954443e0b4a4aec3df1 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 28 Jun 2018 15:50:50 +0800 Subject: [PATCH] Simplify function --- src/base/search/searchpluginmanager.cpp | 31 ++++++++++--------------- src/base/search/searchpluginmanager.h | 2 +- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index 892b3186a..c00bc80f6 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -540,32 +540,25 @@ QString SearchPluginManager::pluginPath(const QString &name) return QString("%1/%2.py").arg(pluginsLocation(), name); } -PluginVersion SearchPluginManager::getPluginVersion(QString filePath) +PluginVersion SearchPluginManager::getPluginVersion(const QString &filePath) { - QFile plugin(filePath); - if (!plugin.exists()) { - qDebug("%s plugin does not exist, returning 0.0", qUtf8Printable(filePath)); - return {}; - } - - if (!plugin.open(QIODevice::ReadOnly | QIODevice::Text)) + QFile pluginFile(filePath); + if (!pluginFile.open(QIODevice::ReadOnly | QIODevice::Text)) return {}; - const PluginVersion invalidVersion; - - PluginVersion version; - while (!plugin.atEnd()) { - const QString line = QString(plugin.readLine()).remove(' '); + while (!pluginFile.atEnd()) { + const QString line = QString(pluginFile.readLine()).remove(' '); if (!line.startsWith("#VERSION:", Qt::CaseInsensitive)) continue; const QString versionStr = line.mid(9); - version = PluginVersion::tryParse(versionStr, invalidVersion); - if (version == invalidVersion) { - LogMsg(tr("Search plugin '%1' contains invalid version string ('%2')") - .arg(Utils::Fs::fileName(filePath), line), Log::MsgType::WARNING); - } + const PluginVersion version = PluginVersion::tryParse(versionStr, {}); + if (version.isValid()) + return version; + LogMsg(tr("Search plugin '%1' contains invalid version string ('%2')") + .arg(Utils::Fs::fileName(filePath), versionStr), Log::MsgType::WARNING); break; } - return version; + + return {}; } diff --git a/src/base/search/searchpluginmanager.h b/src/base/search/searchpluginmanager.h index 7f2b93ab3..bf12a745a 100644 --- a/src/base/search/searchpluginmanager.h +++ b/src/base/search/searchpluginmanager.h @@ -79,7 +79,7 @@ public: SearchHandler *startSearch(const QString &pattern, const QString &category, const QStringList &usedPlugins); SearchDownloadHandler *downloadTorrent(const QString &siteUrl, const QString &url); - static PluginVersion getPluginVersion(QString filePath); + static PluginVersion getPluginVersion(const QString &filePath); static QString categoryFullName(const QString &categoryName); QString pluginFullName(const QString &pluginName); static QString pluginsLocation();