Use std::optional<bool> instead of custom TriStateBool

This commit is contained in:
Vladimir Golovnev (Glassez)
2021-01-02 16:55:17 +03:00
parent d0cac421bb
commit 531ae501ad
17 changed files with 75 additions and 235 deletions

View File

@@ -73,7 +73,6 @@ add_library(qbt_base STATIC
settingsstorage.h
torrentfileguard.h
torrentfilter.h
tristatebool.h
types.h
unicodestrings.h
utils/bytearray.h
@@ -150,7 +149,6 @@ add_library(qbt_base STATIC
settingsstorage.cpp
torrentfileguard.cpp
torrentfilter.cpp
tristatebool.cpp
utils/bytearray.cpp
utils/foreignapps.cpp
utils/fs.cpp

View File

@@ -73,7 +73,6 @@ HEADERS += \
$$PWD/settingvalue.h \
$$PWD/torrentfileguard.h \
$$PWD/torrentfilter.h \
$$PWD/tristatebool.h \
$$PWD/types.h \
$$PWD/unicodestrings.h \
$$PWD/utils/bytearray.h \
@@ -150,7 +149,6 @@ SOURCES += \
$$PWD/settingsstorage.cpp \
$$PWD/torrentfileguard.cpp \
$$PWD/torrentfilter.cpp \
$$PWD/tristatebool.cpp \
$$PWD/utils/bytearray.cpp \
$$PWD/utils/foreignapps.cpp \
$$PWD/utils/fs.cpp \

View File

@@ -34,7 +34,6 @@
#include <QString>
#include <QVector>
#include "base/tristatebool.h"
#include "torrenthandle.h"
#include "torrentcontentlayout.h"
@@ -51,12 +50,12 @@ namespace BitTorrent
bool disableTempPath = false; // e.g. for imported torrents
bool sequential = false;
bool firstLastPiecePriority = false;
TriStateBool addForced;
TriStateBool addPaused;
bool addForced = false;
std::optional<bool> addPaused;
QVector<DownloadPriority> filePriorities; // used if TorrentInfo is set
bool skipChecking = false;
std::optional<BitTorrent::TorrentContentLayout> contentLayout;
TriStateBool useAutoTMM;
std::optional<bool> useAutoTMM;
int uploadLimit = -1;
int downloadLimit = -1;
int seedingTimeLimit = TorrentHandle::USE_GLOBAL_SEEDING_TIME;

View File

@@ -78,7 +78,6 @@
#include "base/profile.h"
#include "base/torrentfileguard.h"
#include "base/torrentfilter.h"
#include "base/tristatebool.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "base/utils/fs.h"
@@ -2058,16 +2057,12 @@ LoadTorrentParams Session::initLoadTorrentParams(const AddTorrentParams &addTorr
loadTorrentParams.firstLastPiecePriority = addTorrentParams.firstLastPiecePriority;
loadTorrentParams.hasSeedStatus = addTorrentParams.skipChecking; // do not react on 'torrent_finished_alert' when skipping
loadTorrentParams.contentLayout = addTorrentParams.contentLayout.value_or(torrentContentLayout());
loadTorrentParams.forced = (addTorrentParams.addForced == TriStateBool::True);
loadTorrentParams.paused = ((addTorrentParams.addPaused == TriStateBool::Undefined)
? isAddTorrentPaused()
: (addTorrentParams.addPaused == TriStateBool::True));
loadTorrentParams.forced = addTorrentParams.addForced;
loadTorrentParams.paused = addTorrentParams.addPaused.value_or(isAddTorrentPaused());
loadTorrentParams.ratioLimit = addTorrentParams.ratioLimit;
loadTorrentParams.seedingTimeLimit = addTorrentParams.seedingTimeLimit;
const bool useAutoTMM = ((addTorrentParams.useAutoTMM == TriStateBool::Undefined)
? !isAutoTMMDisabledByDefault()
: (addTorrentParams.useAutoTMM == TriStateBool::True));
const bool useAutoTMM = addTorrentParams.useAutoTMM.value_or(!isAutoTMMDisabledByDefault());
if (useAutoTMM)
loadTorrentParams.savePath = "";
else if (addTorrentParams.savePath.trimmed().isEmpty())

View File

@@ -47,7 +47,6 @@
#include "../logger.h"
#include "../profile.h"
#include "../settingsstorage.h"
#include "../tristatebool.h"
#include "../utils/fs.h"
#include "rss_article.h"
#include "rss_autodownloadrule.h"
@@ -398,7 +397,7 @@ void AutoDownloader::processJob(const QSharedPointer<ProcessingJob> &job)
params.addPaused = rule.addPaused();
params.contentLayout = rule.torrentContentLayout();
if (!rule.savePath().isEmpty())
params.useAutoTMM = TriStateBool::False;
params.useAutoTMM = false;
const auto torrentURL = job->articleData.value(Article::KeyTorrentURL).toString();
BitTorrent::Session::instance()->addTorrent(torrentURL, params);

View File

@@ -42,7 +42,6 @@
#include "base/global.h"
#include "base/preferences.h"
#include "base/tristatebool.h"
#include "base/utils/fs.h"
#include "base/utils/string.h"
#include "rss_article.h"
@@ -51,45 +50,38 @@
namespace
{
TriStateBool jsonValueToTriStateBool(const QJsonValue &jsonVal)
std::optional<bool> toOptionalBool(const QJsonValue &jsonVal)
{
if (jsonVal.isBool())
return TriStateBool(jsonVal.toBool());
return jsonVal.toBool();
if (!jsonVal.isNull())
qDebug() << Q_FUNC_INFO << "Incorrect value" << jsonVal.toVariant();
return TriStateBool::Undefined;
return std::nullopt;
}
QJsonValue triStateBoolToJsonValue(const TriStateBool triStateBool)
QJsonValue toJsonValue(const std::optional<bool> boolValue)
{
switch (static_cast<signed char>(triStateBool))
{
case 0: return false;
case 1: return true;
default: return {};
}
return boolValue.has_value() ? *boolValue : QJsonValue {};
}
TriStateBool addPausedLegacyToTriStateBool(const int val)
std::optional<bool> addPausedLegacyToOptionalBool(const int val)
{
switch (val)
{
case 1: return TriStateBool::True; // always
case 2: return TriStateBool::False; // never
default: return TriStateBool::Undefined; // default
case 1:
return true; // always
case 2:
return false; // never
default:
return std::nullopt; // default
}
}
int triStateBoolToAddPausedLegacy(const TriStateBool triStateBool)
int toAddPausedLegacy(const std::optional<bool> boolValue)
{
switch (static_cast<signed char>(triStateBool))
{
case 0: return 2; // never
case 1: return 1; // always
default: return 0; // default
}
if (!boolValue.has_value())
return 0; // default
return (*boolValue ? 1 /* always */ : 2 /* never */);
}
std::optional<BitTorrent::TorrentContentLayout> jsonValueToContentLayout(const QJsonValue &jsonVal)
@@ -142,7 +134,7 @@ namespace RSS
QString savePath;
QString category;
TriStateBool addPaused = TriStateBool::Undefined;
std::optional<bool> addPaused;
std::optional<BitTorrent::TorrentContentLayout> contentLayout;
bool smartFilter = false;
@@ -477,7 +469,7 @@ QJsonObject AutoDownloadRule::toJsonObject() const
, {Str_AssignedCategory, assignedCategory()}
, {Str_LastMatch, lastMatch().toString(Qt::RFC2822Date)}
, {Str_IgnoreDays, ignoreDays()}
, {Str_AddPaused, triStateBoolToJsonValue(addPaused())}
, {Str_AddPaused, toJsonValue(addPaused())}
, {Str_ContentLayout, contentLayoutToJsonValue(torrentContentLayout())}
, {Str_SmartFilter, useSmartFilter()}
, {Str_PreviouslyMatched, QJsonArray::fromStringList(previouslyMatchedEpisodes())}};
@@ -494,7 +486,7 @@ AutoDownloadRule AutoDownloadRule::fromJsonObject(const QJsonObject &jsonObj, co
rule.setEnabled(jsonObj.value(Str_Enabled).toBool(true));
rule.setSavePath(jsonObj.value(Str_SavePath).toString());
rule.setCategory(jsonObj.value(Str_AssignedCategory).toString());
rule.setAddPaused(jsonValueToTriStateBool(jsonObj.value(Str_AddPaused)));
rule.setAddPaused(toOptionalBool(jsonObj.value(Str_AddPaused)));
// TODO: The following code is deprecated. Replace with the commented one after several releases in 4.4.x.
// === BEGIN DEPRECATED CODE === //
@@ -504,12 +496,14 @@ AutoDownloadRule AutoDownloadRule::fromJsonObject(const QJsonObject &jsonObj, co
}
else
{
const TriStateBool createSubfolder = jsonValueToTriStateBool(jsonObj.value(Str_CreateSubfolder));
const std::optional<bool> createSubfolder = toOptionalBool(jsonObj.value(Str_CreateSubfolder));
std::optional<BitTorrent::TorrentContentLayout> contentLayout;
if (createSubfolder == TriStateBool::True)
contentLayout = BitTorrent::TorrentContentLayout::Original;
else if (createSubfolder == TriStateBool::False)
contentLayout = BitTorrent::TorrentContentLayout::NoSubfolder;
if (createSubfolder.has_value())
{
contentLayout = (*createSubfolder
? BitTorrent::TorrentContentLayout::Original
: BitTorrent::TorrentContentLayout::NoSubfolder);
}
rule.setTorrentContentLayout(contentLayout);
}
@@ -556,7 +550,7 @@ QVariantHash AutoDownloadRule::toLegacyDict() const
{"enabled", isEnabled()},
{"category_assigned", assignedCategory()},
{"use_regex", useRegex()},
{"add_paused", triStateBoolToAddPausedLegacy(addPaused())},
{"add_paused", toAddPausedLegacy(addPaused())},
{"episode_filter", episodeFilter()},
{"last_match", lastMatch()},
{"ignore_days", ignoreDays()}};
@@ -574,7 +568,7 @@ AutoDownloadRule AutoDownloadRule::fromLegacyDict(const QVariantHash &dict)
rule.setEnabled(dict.value("enabled", false).toBool());
rule.setSavePath(dict.value("save_path").toString());
rule.setCategory(dict.value("category_assigned").toString());
rule.setAddPaused(addPausedLegacyToTriStateBool(dict.value("add_paused").toInt()));
rule.setAddPaused(addPausedLegacyToOptionalBool(dict.value("add_paused").toInt()));
rule.setLastMatch(dict.value("last_match").toDateTime());
rule.setIgnoreDays(dict.value("ignore_days").toInt());
@@ -639,12 +633,12 @@ void AutoDownloadRule::setSavePath(const QString &savePath)
m_dataPtr->savePath = Utils::Fs::toUniformPath(savePath);
}
TriStateBool AutoDownloadRule::addPaused() const
std::optional<bool> AutoDownloadRule::addPaused() const
{
return m_dataPtr->addPaused;
}
void AutoDownloadRule::setAddPaused(const TriStateBool addPaused)
void AutoDownloadRule::setAddPaused(const std::optional<bool> addPaused)
{
m_dataPtr->addPaused = addPaused;
}

View File

@@ -40,8 +40,6 @@ class QDateTime;
class QJsonObject;
class QRegularExpression;
class TriStateBool;
namespace RSS
{
struct AutoDownloadRuleData;
@@ -81,8 +79,8 @@ namespace RSS
QString savePath() const;
void setSavePath(const QString &savePath);
TriStateBool addPaused() const;
void setAddPaused(TriStateBool addPaused);
std::optional<bool> addPaused() const;
void setAddPaused(std::optional<bool> addPaused);
std::optional<BitTorrent::TorrentContentLayout> torrentContentLayout() const;
void setTorrentContentLayout(std::optional<BitTorrent::TorrentContentLayout> contentLayout);
QString assignedCategory() const;

View File

@@ -366,12 +366,12 @@ void ScanFoldersModel::addTorrentsToSession(const QStringList &pathList)
if (downloadInWatchFolder(file))
{
params.savePath = QFileInfo(file).dir().path();
params.useAutoTMM = TriStateBool::False;
params.useAutoTMM = false;
}
else if (!downloadInDefaultFolder(file))
{
params.savePath = downloadPathTorrentFolder(file);
params.useAutoTMM = TriStateBool::False;
params.useAutoTMM = false;
}
if (file.endsWith(".magnet", Qt::CaseInsensitive))

View File

@@ -1,44 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
*
* 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.
*/
#include "tristatebool.h"
#include <QString>
const TriStateBool TriStateBool::Undefined(-1);
const TriStateBool TriStateBool::False(0);
const TriStateBool TriStateBool::True(1);
TriStateBool TriStateBool::fromString(const QString &string)
{
if (string.compare("true", Qt::CaseInsensitive) == 0)
return True;
if (string.compare("false", Qt::CaseInsensitive) == 0)
return False;
return Undefined;
}

View File

@@ -1,73 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
*
* 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.
*/
#pragma once
class QString;
class TriStateBool
{
public:
static const TriStateBool Undefined;
static const TriStateBool False;
static const TriStateBool True;
constexpr TriStateBool() = default;
constexpr TriStateBool(const TriStateBool &other) = default;
explicit constexpr TriStateBool(const bool boolean)
{
*this = boolean ? True : False;
}
constexpr TriStateBool &operator=(const TriStateBool &other) = default;
explicit constexpr operator signed char() const
{
return m_value;
}
constexpr friend bool operator==(const TriStateBool &left, const TriStateBool &right)
{
return (left.m_value == right.m_value);
}
static TriStateBool fromString(const QString &string);
private:
explicit constexpr TriStateBool(const int value)
: m_value((value < 0) ? -1 : ((value > 0) ? 1 : 0))
{
}
signed char m_value = -1; // Undefined by default
};
constexpr bool operator!=(const TriStateBool &left, const TriStateBool &right)
{
return !(left == right);
}