mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-23 16:58:06 -06:00
Improve coding style
This commit is contained in:
@@ -42,7 +42,8 @@ SearchDownloadHandler::SearchDownloadHandler(const QString &siteUrl, const QStri
|
||||
m_downloadProcess->setEnvironment(QProcess::systemEnvironment());
|
||||
connect(m_downloadProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished)
|
||||
, this, &SearchDownloadHandler::downloadProcessFinished);
|
||||
const QStringList params {
|
||||
const QStringList params
|
||||
{
|
||||
Utils::Fs::toNativePath(m_manager->engineLocation() + "/nova2dl.py"),
|
||||
siteUrl,
|
||||
url
|
||||
@@ -55,7 +56,8 @@ void SearchDownloadHandler::downloadProcessFinished(int exitcode)
|
||||
{
|
||||
QString path;
|
||||
|
||||
if ((exitcode == 0) && (m_downloadProcess->exitStatus() == QProcess::NormalExit)) {
|
||||
if ((exitcode == 0) && (m_downloadProcess->exitStatus() == QProcess::NormalExit))
|
||||
{
|
||||
const QString line = QString::fromUtf8(m_downloadProcess->readAllStandardOutput()).trimmed();
|
||||
const QVector<QStringRef> parts = line.splitRef(' ');
|
||||
if (parts.size() == 2)
|
||||
|
||||
@@ -64,7 +64,8 @@ SearchHandler::SearchHandler(const QString &pattern, const QString &category, co
|
||||
// Load environment variables (proxy)
|
||||
m_searchProcess->setEnvironment(QProcess::systemEnvironment());
|
||||
|
||||
const QStringList params {
|
||||
const QStringList params
|
||||
{
|
||||
Utils::Fs::toNativePath(m_manager->engineLocation() + "/nova2.py"),
|
||||
m_usedPlugins.join(','),
|
||||
m_category
|
||||
@@ -137,13 +138,15 @@ void SearchHandler::readSearchOutput()
|
||||
QVector<SearchResult> searchResultList;
|
||||
searchResultList.reserve(lines.size());
|
||||
|
||||
for (const QByteArray &line : asConst(lines)) {
|
||||
for (const QByteArray &line : asConst(lines))
|
||||
{
|
||||
SearchResult searchResult;
|
||||
if (parseSearchResult(QString::fromUtf8(line), searchResult))
|
||||
searchResultList << searchResult;
|
||||
}
|
||||
|
||||
if (!searchResultList.isEmpty()) {
|
||||
if (!searchResultList.isEmpty())
|
||||
{
|
||||
for (const SearchResult &result : searchResultList)
|
||||
m_results.append(result);
|
||||
emit newSearchResults(searchResultList);
|
||||
|
||||
@@ -61,16 +61,19 @@ namespace
|
||||
while (iter.hasNext())
|
||||
dirs += iter.next();
|
||||
|
||||
for (const QString &dir : asConst(dirs)) {
|
||||
for (const QString &dir : asConst(dirs))
|
||||
{
|
||||
// python 3: remove "__pycache__" folders
|
||||
if (dir.endsWith("/__pycache__")) {
|
||||
if (dir.endsWith("/__pycache__"))
|
||||
{
|
||||
Utils::Fs::removeDirRecursive(dir);
|
||||
continue;
|
||||
}
|
||||
|
||||
// python 2: remove "*.pyc" files
|
||||
const QStringList files = QDir(dir).entryList(QDir::Files);
|
||||
for (const QString &file : files) {
|
||||
for (const QString &file : files)
|
||||
{
|
||||
if (file.endsWith(".pyc"))
|
||||
Utils::Fs::forceRemove(file);
|
||||
}
|
||||
@@ -115,7 +118,8 @@ QStringList SearchPluginManager::allPlugins() const
|
||||
QStringList SearchPluginManager::enabledPlugins() const
|
||||
{
|
||||
QStringList plugins;
|
||||
for (const PluginInfo *plugin : asConst(m_plugins)) {
|
||||
for (const PluginInfo *plugin : asConst(m_plugins))
|
||||
{
|
||||
if (plugin->enabled)
|
||||
plugins << plugin->name;
|
||||
}
|
||||
@@ -126,9 +130,12 @@ QStringList SearchPluginManager::enabledPlugins() const
|
||||
QStringList SearchPluginManager::supportedCategories() const
|
||||
{
|
||||
QStringList result;
|
||||
for (const PluginInfo *plugin : asConst(m_plugins)) {
|
||||
if (plugin->enabled) {
|
||||
for (const QString &cat : plugin->supportedCategories) {
|
||||
for (const PluginInfo *plugin : asConst(m_plugins))
|
||||
{
|
||||
if (plugin->enabled)
|
||||
{
|
||||
for (const QString &cat : plugin->supportedCategories)
|
||||
{
|
||||
if (!result.contains(cat))
|
||||
result << cat;
|
||||
}
|
||||
@@ -149,7 +156,8 @@ QStringList SearchPluginManager::getPluginCategories(const QString &pluginName)
|
||||
plugins << pluginName.trimmed();
|
||||
|
||||
QSet<QString> categories;
|
||||
for (const QString &name : asConst(plugins)) {
|
||||
for (const QString &name : asConst(plugins))
|
||||
{
|
||||
const PluginInfo *plugin = pluginInfo(name);
|
||||
if (!plugin) continue; // plugin wasn't found
|
||||
for (const QString &category : plugin->supportedCategories)
|
||||
@@ -167,7 +175,8 @@ PluginInfo *SearchPluginManager::pluginInfo(const QString &name) const
|
||||
void SearchPluginManager::enablePlugin(const QString &name, const bool enabled)
|
||||
{
|
||||
PluginInfo *plugin = m_plugins.value(name, nullptr);
|
||||
if (plugin) {
|
||||
if (plugin)
|
||||
{
|
||||
plugin->enabled = enabled;
|
||||
// Save to Hard disk
|
||||
Preferences *const pref = Preferences::instance();
|
||||
@@ -193,12 +202,14 @@ void SearchPluginManager::installPlugin(const QString &source)
|
||||
{
|
||||
clearPythonCache(engineLocation());
|
||||
|
||||
if (Net::DownloadManager::hasSupportedScheme(source)) {
|
||||
if (Net::DownloadManager::hasSupportedScheme(source))
|
||||
{
|
||||
using namespace Net;
|
||||
DownloadManager::instance()->download(DownloadRequest(source).saveToFile(true)
|
||||
, this, &SearchPluginManager::pluginDownloadFinished);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
QString path = source;
|
||||
if (path.startsWith("file:", Qt::CaseInsensitive))
|
||||
path = QUrl(path).toLocalFile();
|
||||
@@ -217,7 +228,8 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
|
||||
{
|
||||
const PluginVersion newVersion = getPluginVersion(path);
|
||||
const PluginInfo *plugin = pluginInfo(name);
|
||||
if (plugin && !(plugin->version < newVersion)) {
|
||||
if (plugin && !(plugin->version < newVersion))
|
||||
{
|
||||
LogMsg(tr("Plugin already at version %1, which is greater than %2").arg(plugin->version, newVersion), Log::INFO);
|
||||
emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed."));
|
||||
return;
|
||||
@@ -226,7 +238,8 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
|
||||
// Process with install
|
||||
const QString destPath = pluginPath(name);
|
||||
bool updated = false;
|
||||
if (QFile::exists(destPath)) {
|
||||
if (QFile::exists(destPath))
|
||||
{
|
||||
// Backup in case install fails
|
||||
QFile::copy(destPath, destPath + ".bak");
|
||||
Utils::Fs::forceRemove(destPath);
|
||||
@@ -237,11 +250,13 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
|
||||
// Update supported plugins
|
||||
update();
|
||||
// Check if this was correctly installed
|
||||
if (!m_plugins.contains(name)) {
|
||||
if (!m_plugins.contains(name))
|
||||
{
|
||||
// Remove broken file
|
||||
Utils::Fs::forceRemove(destPath);
|
||||
LogMsg(tr("Plugin %1 is not supported.").arg(name), Log::INFO);
|
||||
if (updated) {
|
||||
if (updated)
|
||||
{
|
||||
// restore backup
|
||||
QFile::copy(destPath + ".bak", destPath);
|
||||
Utils::Fs::forceRemove(destPath + ".bak");
|
||||
@@ -249,13 +264,16 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString
|
||||
update();
|
||||
emit pluginUpdateFailed(name, tr("Plugin is not supported."));
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
emit pluginInstallationFailed(name, tr("Plugin is not supported."));
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Install was successful, remove backup
|
||||
if (updated) {
|
||||
if (updated)
|
||||
{
|
||||
LogMsg(tr("Plugin %1 has been successfully updated.").arg(name), Log::INFO);
|
||||
Utils::Fs::forceRemove(destPath + ".bak");
|
||||
}
|
||||
@@ -284,10 +302,12 @@ void SearchPluginManager::updateIconPath(PluginInfo *const plugin)
|
||||
{
|
||||
if (!plugin) return;
|
||||
QString iconPath = QString::fromLatin1("%1/%2.png").arg(pluginsLocation(), plugin->name);
|
||||
if (QFile::exists(iconPath)) {
|
||||
if (QFile::exists(iconPath))
|
||||
{
|
||||
plugin->iconPath = iconPath;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
iconPath = QString::fromLatin1("%1/%2.ico").arg(pluginsLocation(), plugin->name);
|
||||
if (QFile::exists(iconPath))
|
||||
plugin->iconPath = iconPath;
|
||||
@@ -317,7 +337,8 @@ SearchHandler *SearchPluginManager::startSearch(const QString &pattern, const QS
|
||||
|
||||
QString SearchPluginManager::categoryFullName(const QString &categoryName)
|
||||
{
|
||||
const QHash<QString, QString> categoryTable {
|
||||
const QHash<QString, QString> categoryTable
|
||||
{
|
||||
{"all", tr("All categories")},
|
||||
{"movies", tr("Movies")},
|
||||
{"tv", tr("TV shows")},
|
||||
@@ -344,7 +365,8 @@ QString SearchPluginManager::pluginsLocation()
|
||||
QString SearchPluginManager::engineLocation()
|
||||
{
|
||||
static QString location;
|
||||
if (location.isEmpty()) {
|
||||
if (location.isEmpty())
|
||||
{
|
||||
location = Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Data) + "nova3");
|
||||
|
||||
const QDir locationDir(location);
|
||||
@@ -364,7 +386,8 @@ void SearchPluginManager::versionInfoDownloadFinished(const Net::DownloadResult
|
||||
|
||||
void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &result)
|
||||
{
|
||||
if (result.status == Net::DownloadStatus::Success) {
|
||||
if (result.status == Net::DownloadStatus::Success)
|
||||
{
|
||||
const QString filePath = Utils::Fs::toUniformPath(result.filePath);
|
||||
|
||||
QString pluginName = Utils::Fs::fileName(result.url);
|
||||
@@ -372,7 +395,8 @@ void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &resu
|
||||
installPlugin_impl(pluginName, filePath);
|
||||
Utils::Fs::forceRemove(filePath);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
const QString url = result.url;
|
||||
QString pluginName = url.mid(url.lastIndexOf('/') + 1);
|
||||
pluginName.replace(".py", "", Qt::CaseInsensitive);
|
||||
@@ -432,21 +456,25 @@ void SearchPluginManager::update()
|
||||
|
||||
const QString capabilities = nova.readAll();
|
||||
QDomDocument xmlDoc;
|
||||
if (!xmlDoc.setContent(capabilities)) {
|
||||
if (!xmlDoc.setContent(capabilities))
|
||||
{
|
||||
qWarning() << "Could not parse Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
|
||||
qWarning() << "Error: " << nova.readAllStandardError().constData();
|
||||
return;
|
||||
}
|
||||
|
||||
const QDomElement root = xmlDoc.documentElement();
|
||||
if (root.tagName() != "capabilities") {
|
||||
if (root.tagName() != "capabilities")
|
||||
{
|
||||
qWarning() << "Invalid XML file for Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
|
||||
return;
|
||||
}
|
||||
|
||||
for (QDomNode engineNode = root.firstChild(); !engineNode.isNull(); engineNode = engineNode.nextSibling()) {
|
||||
for (QDomNode engineNode = root.firstChild(); !engineNode.isNull(); engineNode = engineNode.nextSibling())
|
||||
{
|
||||
const QDomElement engineElem = engineNode.toElement();
|
||||
if (!engineElem.isNull()) {
|
||||
if (!engineElem.isNull())
|
||||
{
|
||||
const QString pluginName = engineElem.tagName();
|
||||
|
||||
auto plugin = std::make_unique<PluginInfo>();
|
||||
@@ -456,7 +484,8 @@ void SearchPluginManager::update()
|
||||
plugin->url = engineElem.elementsByTagName("url").at(0).toElement().text();
|
||||
|
||||
const QStringList categories = engineElem.elementsByTagName("categories").at(0).toElement().text().split(' ');
|
||||
for (QString cat : categories) {
|
||||
for (QString cat : categories)
|
||||
{
|
||||
cat = cat.trimmed();
|
||||
if (!cat.isEmpty())
|
||||
plugin->supportedCategories << cat;
|
||||
@@ -467,11 +496,13 @@ void SearchPluginManager::update()
|
||||
|
||||
updateIconPath(plugin.get());
|
||||
|
||||
if (!m_plugins.contains(pluginName)) {
|
||||
if (!m_plugins.contains(pluginName))
|
||||
{
|
||||
m_plugins[pluginName] = plugin.release();
|
||||
emit pluginInstalled(pluginName);
|
||||
}
|
||||
else if (m_plugins[pluginName]->version != plugin->version) {
|
||||
else if (m_plugins[pluginName]->version != plugin->version)
|
||||
{
|
||||
delete m_plugins.take(pluginName);
|
||||
m_plugins[pluginName] = plugin.release();
|
||||
emit pluginUpdated(pluginName);
|
||||
@@ -486,7 +517,8 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info)
|
||||
int numCorrectData = 0;
|
||||
|
||||
const QVector<QByteArray> lines = Utils::ByteArray::splitToViews(info, "\n", QString::SkipEmptyParts);
|
||||
for (QByteArray line : lines) {
|
||||
for (QByteArray line : lines)
|
||||
{
|
||||
line = line.trimmed();
|
||||
if (line.isEmpty()) continue;
|
||||
if (line.startsWith('#')) continue;
|
||||
@@ -500,17 +532,20 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info)
|
||||
if (!version.isValid()) continue;
|
||||
|
||||
++numCorrectData;
|
||||
if (isUpdateNeeded(pluginName, version)) {
|
||||
if (isUpdateNeeded(pluginName, version))
|
||||
{
|
||||
LogMsg(tr("Plugin \"%1\" is outdated, updating to version %2").arg(pluginName, version), Log::INFO);
|
||||
updateInfo[pluginName] = version;
|
||||
}
|
||||
}
|
||||
|
||||
if (numCorrectData < lines.size()) {
|
||||
if (numCorrectData < lines.size())
|
||||
{
|
||||
emit checkForUpdatesFailed(tr("Incorrect update info received for %1 out of %2 plugins.")
|
||||
.arg(QString::number(lines.size() - numCorrectData), QString::number(lines.size())));
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
emit checkForUpdatesFinished(updateInfo);
|
||||
}
|
||||
}
|
||||
@@ -535,7 +570,8 @@ PluginVersion SearchPluginManager::getPluginVersion(const QString &filePath)
|
||||
if (!pluginFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
return {};
|
||||
|
||||
while (!pluginFile.atEnd()) {
|
||||
while (!pluginFile.atEnd())
|
||||
{
|
||||
const QString line = QString(pluginFile.readLine()).remove(' ');
|
||||
if (!line.startsWith("#VERSION:", Qt::CaseInsensitive)) continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user