Browse Source

Change parseBool() to return optional bool value

adaptive-webui-19844
Vladimir Golovnev (Glassez) 4 years ago
parent
commit
d0cac421bb
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 11
      src/base/utils/string.cpp
  2. 4
      src/base/utils/string.h
  3. 8
      src/webui/api/logcontroller.cpp
  4. 2
      src/webui/api/rsscontroller.cpp
  5. 2
      src/webui/api/searchcontroller.cpp
  6. 16
      src/webui/api/torrentscontroller.cpp

11
src/base/utils/string.cpp

@ -190,11 +190,14 @@ QString Utils::String::wildcardToRegex(const QString &pattern)
return qt_regexp_toCanonical(pattern, QRegExp::Wildcard); return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
} }
bool Utils::String::parseBool(const QString &string, const bool defaultValue) std::optional<bool> Utils::String::parseBool(const QString &string)
{ {
if (defaultValue) if (string.compare("true", Qt::CaseInsensitive) == 0)
return (string.compare("false", Qt::CaseInsensitive) == 0) ? false : true; return true;
return (string.compare("true", Qt::CaseInsensitive) == 0) ? true : false; if (string.compare("false", Qt::CaseInsensitive) == 0)
return false;
return std::nullopt;
} }
QString Utils::String::join(const QVector<QStringRef> &strings, const QString &separator) QString Utils::String::join(const QVector<QStringRef> &strings, const QString &separator)

4
src/base/utils/string.h

@ -29,6 +29,8 @@
#pragma once #pragma once
#include <optional>
#include <QChar> #include <QChar>
#include <QMetaEnum> #include <QMetaEnum>
#include <QString> #include <QString>
@ -64,7 +66,7 @@ namespace Utils::String
return str; return str;
} }
bool parseBool(const QString &string, bool defaultValue); std::optional<bool> parseBool(const QString &string);
QString join(const QVector<QStringRef> &strings, const QString &separator); QString join(const QVector<QStringRef> &strings, const QString &separator);

8
src/webui/api/logcontroller.cpp

@ -61,10 +61,10 @@ void LogController::mainAction()
{ {
using Utils::String::parseBool; using Utils::String::parseBool;
const bool isNormal = parseBool(params()["normal"], true); const bool isNormal = parseBool(params()["normal"]).value_or(true);
const bool isInfo = parseBool(params()["info"], true); const bool isInfo = parseBool(params()["info"]).value_or(true);
const bool isWarning = parseBool(params()["warning"], true); const bool isWarning = parseBool(params()["warning"]).value_or(true);
const bool isCritical = parseBool(params()["critical"], true); const bool isCritical = parseBool(params()["critical"]).value_or(true);
bool ok = false; bool ok = false;
int lastKnownId = params()["last_known_id"].toInt(&ok); int lastKnownId = params()["last_known_id"].toInt(&ok);

2
src/webui/api/rsscontroller.cpp

@ -89,7 +89,7 @@ void RSSController::moveItemAction()
void RSSController::itemsAction() 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); const auto jsonVal = RSS::Session::instance()->rootFolder()->toJsonValue(withData);
setResult(jsonVal.toObject()); setResult(jsonVal.toObject());

2
src/webui/api/searchcontroller.cpp

@ -264,7 +264,7 @@ void SearchController::enablePluginAction()
requireParams({"names", "enable"}); requireParams({"names", "enable"});
const QStringList names = params()["names"].split('|'); 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) for (const QString &name : names)
SearchPluginManager::instance()->enablePlugin(name.trimmed(), enable); SearchPluginManager::instance()->enablePlugin(name.trimmed(), enable);

16
src/webui/api/torrentscontroller.cpp

@ -253,7 +253,7 @@ void TorrentsController::infoAction()
const QString filter {params()["filter"]}; const QString filter {params()["filter"]};
const QString category {params()["category"]}; const QString category {params()["category"]};
const QString sortedColumn {params()["sort"]}; 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 limit {params()["limit"].toInt()};
int offset {params()["offset"].toInt()}; int offset {params()["offset"].toInt()};
const QStringList hashes {params()["hashes"].split('|', QString::SkipEmptyParts)}; const QStringList hashes {params()["hashes"].split('|', QString::SkipEmptyParts)};
@ -602,9 +602,9 @@ void TorrentsController::addAction()
{ {
const QString urls = params()["urls"]; const QString urls = params()["urls"];
const bool skipChecking = parseBool(params()["skip_checking"], false); const bool skipChecking = parseBool(params()["skip_checking"]).value_or(false);
const bool seqDownload = parseBool(params()["sequentialDownload"], false); const bool seqDownload = parseBool(params()["sequentialDownload"]).value_or(false);
const bool firstLastPiece = parseBool(params()["firstLastPiecePrio"], false); const bool firstLastPiece = parseBool(params()["firstLastPiecePrio"]).value_or(false);
const auto addPaused = TriStateBool::fromString(params()["paused"]); const auto addPaused = TriStateBool::fromString(params()["paused"]);
const QString savepath = params()["savepath"].trimmed(); const QString savepath = params()["savepath"].trimmed();
const QString category = params()["category"]; const QString category = params()["category"];
@ -962,7 +962,7 @@ void TorrentsController::setSuperSeedingAction()
{ {
requireParams({"hashes", "value"}); 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('|')}; const QStringList hashes {params()["hashes"].split('|')};
applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent) { torrent->setSuperSeeding(value); }); applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent) { torrent->setSuperSeeding(value); });
} }
@ -971,7 +971,7 @@ void TorrentsController::setForceStartAction()
{ {
requireParams({"hashes", "value"}); 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('|')}; const QStringList hashes {params()["hashes"].split('|')};
applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent) applyToTorrents(hashes, [value](BitTorrent::TorrentHandle *const torrent)
{ {
@ -984,7 +984,7 @@ void TorrentsController::deleteAction()
requireParams({"hashes", "deleteFiles"}); requireParams({"hashes", "deleteFiles"});
const QStringList hashes {params()["hashes"].split('|')}; const QStringList hashes {params()["hashes"].split('|')};
const DeleteOption deleteOption = parseBool(params()["deleteFiles"], false) const DeleteOption deleteOption = parseBool(params()["deleteFiles"]).value_or(false)
? TorrentAndFiles : Torrent; ? TorrentAndFiles : Torrent;
applyToTorrents(hashes, [deleteOption](const BitTorrent::TorrentHandle *torrent) applyToTorrents(hashes, [deleteOption](const BitTorrent::TorrentHandle *torrent)
{ {
@ -1085,7 +1085,7 @@ void TorrentsController::setAutoManagementAction()
requireParams({"hashes", "enable"}); requireParams({"hashes", "enable"});
const QStringList hashes {params()["hashes"].split('|')}; 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) applyToTorrents(hashes, [isEnabled](BitTorrent::TorrentHandle *const torrent)
{ {

Loading…
Cancel
Save