From d0cac421bb55abb0fd8c90bea96e8598da1afd37 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 3 Jan 2021 15:33:32 +0300 Subject: [PATCH] Change parseBool() to return optional bool value --- src/base/utils/string.cpp | 11 +++++++---- src/base/utils/string.h | 4 +++- src/webui/api/logcontroller.cpp | 8 ++++---- src/webui/api/rsscontroller.cpp | 2 +- src/webui/api/searchcontroller.cpp | 2 +- src/webui/api/torrentscontroller.cpp | 16 ++++++++-------- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index bc524e6cf..9c0ba7ea9 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -190,11 +190,14 @@ QString Utils::String::wildcardToRegex(const QString &pattern) return qt_regexp_toCanonical(pattern, QRegExp::Wildcard); } -bool Utils::String::parseBool(const QString &string, const bool defaultValue) +std::optional Utils::String::parseBool(const QString &string) { - if (defaultValue) - return (string.compare("false", Qt::CaseInsensitive) == 0) ? false : true; - return (string.compare("true", Qt::CaseInsensitive) == 0) ? true : false; + if (string.compare("true", Qt::CaseInsensitive) == 0) + return true; + if (string.compare("false", Qt::CaseInsensitive) == 0) + return false; + + return std::nullopt; } QString Utils::String::join(const QVector &strings, const QString &separator) diff --git a/src/base/utils/string.h b/src/base/utils/string.h index 3d3c3aa72..836fc770c 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -29,6 +29,8 @@ #pragma once +#include + #include #include #include @@ -64,7 +66,7 @@ namespace Utils::String return str; } - bool parseBool(const QString &string, bool defaultValue); + std::optional parseBool(const QString &string); QString join(const QVector &strings, const QString &separator); diff --git a/src/webui/api/logcontroller.cpp b/src/webui/api/logcontroller.cpp index 52dd1776f..90e752c7f 100644 --- a/src/webui/api/logcontroller.cpp +++ b/src/webui/api/logcontroller.cpp @@ -61,10 +61,10 @@ void LogController::mainAction() { using Utils::String::parseBool; - const bool isNormal = parseBool(params()["normal"], true); - const bool isInfo = parseBool(params()["info"], true); - const bool isWarning = parseBool(params()["warning"], true); - const bool isCritical = parseBool(params()["critical"], true); + const bool isNormal = parseBool(params()["normal"]).value_or(true); + const bool isInfo = parseBool(params()["info"]).value_or(true); + const bool isWarning = parseBool(params()["warning"]).value_or(true); + const bool isCritical = parseBool(params()["critical"]).value_or(true); bool ok = false; int lastKnownId = params()["last_known_id"].toInt(&ok); diff --git a/src/webui/api/rsscontroller.cpp b/src/webui/api/rsscontroller.cpp index dac9e58b1..f4903eb51 100644 --- a/src/webui/api/rsscontroller.cpp +++ b/src/webui/api/rsscontroller.cpp @@ -89,7 +89,7 @@ void RSSController::moveItemAction() void RSSController::itemsAction() { - const bool withData {parseBool(params()["withData"], false)}; + const bool withData {parseBool(params()["withData"]).value_or(false)}; const auto jsonVal = RSS::Session::instance()->rootFolder()->toJsonValue(withData); setResult(jsonVal.toObject()); diff --git a/src/webui/api/searchcontroller.cpp b/src/webui/api/searchcontroller.cpp index 319e51473..7ef495da6 100644 --- a/src/webui/api/searchcontroller.cpp +++ b/src/webui/api/searchcontroller.cpp @@ -264,7 +264,7 @@ void SearchController::enablePluginAction() requireParams({"names", "enable"}); const QStringList names = params()["names"].split('|'); - const bool enable = Utils::String::parseBool(params()["enable"].trimmed(), false); + const bool enable = Utils::String::parseBool(params()["enable"].trimmed()).value_or(false); for (const QString &name : names) SearchPluginManager::instance()->enablePlugin(name.trimmed(), enable); diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 7b736f244..fc2af2bcb 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -253,7 +253,7 @@ void TorrentsController::infoAction() const QString filter {params()["filter"]}; const QString category {params()["category"]}; const QString sortedColumn {params()["sort"]}; - const bool reverse {parseBool(params()["reverse"], false)}; + const bool reverse {parseBool(params()["reverse"]).value_or(false)}; int limit {params()["limit"].toInt()}; int offset {params()["offset"].toInt()}; const QStringList hashes {params()["hashes"].split('|', QString::SkipEmptyParts)}; @@ -602,9 +602,9 @@ void TorrentsController::addAction() { const QString urls = params()["urls"]; - const bool skipChecking = parseBool(params()["skip_checking"], false); - const bool seqDownload = parseBool(params()["sequentialDownload"], false); - const bool firstLastPiece = parseBool(params()["firstLastPiecePrio"], false); + const bool skipChecking = parseBool(params()["skip_checking"]).value_or(false); + const bool seqDownload = parseBool(params()["sequentialDownload"]).value_or(false); + const bool firstLastPiece = parseBool(params()["firstLastPiecePrio"]).value_or(false); const auto addPaused = TriStateBool::fromString(params()["paused"]); const QString savepath = params()["savepath"].trimmed(); const QString category = params()["category"]; @@ -962,7 +962,7 @@ void TorrentsController::setSuperSeedingAction() { requireParams({"hashes", "value"}); - const bool value {parseBool(params()["value"], false)}; + const bool value {parseBool(params()["value"]).value_or(false)}; const QStringList hashes {params()["hashes"].split('|')}; applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent) { torrent->setSuperSeeding(value); }); } @@ -971,7 +971,7 @@ void TorrentsController::setForceStartAction() { requireParams({"hashes", "value"}); - const bool value {parseBool(params()["value"], false)}; + const bool value {parseBool(params()["value"]).value_or(false)}; const QStringList hashes {params()["hashes"].split('|')}; applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent) { @@ -984,7 +984,7 @@ void TorrentsController::deleteAction() requireParams({"hashes", "deleteFiles"}); const QStringList hashes {params()["hashes"].split('|')}; - const DeleteOption deleteOption = parseBool(params()["deleteFiles"], false) + const DeleteOption deleteOption = parseBool(params()["deleteFiles"]).value_or(false) ? TorrentAndFiles : Torrent; applyToTorrents(hashes, [deleteOption](const BitTorrent::TorrentHandle *torrent) { @@ -1085,7 +1085,7 @@ void TorrentsController::setAutoManagementAction() requireParams({"hashes", "enable"}); const QStringList hashes {params()["hashes"].split('|')}; - const bool isEnabled {parseBool(params()["enable"], false)}; + const bool isEnabled {parseBool(params()["enable"]).value_or(false)}; applyToTorrents(hashes, [isEnabled](BitTorrent::TorrentHandle *const torrent) {