refactor: common/Credentials (#4979)

Use full path in includes
Sort includes
Move anon namespace out of chatterino namespace
Use auto * where possible
Disable convert-member-function-to-static check for all member functions
Don't use else after return
Removed empty constructor
Replace use of `boost::variant` with `std::variant`

Co-authored-by: nerix <nerixdev@outlook.de>
This commit is contained in:
pajlada 2023-11-26 19:38:31 +01:00 committed by GitHub
parent 5b741a8eb6
commit 1f09035bfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 124 deletions

View file

@ -63,6 +63,7 @@
- Dev: Removed direct dependency on Qt 5 compatibility module. (#4906)
- Dev: Refactor `Emoji`'s EmojiMap into a vector. (#4980)
- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Refactor `common/Credentials`. (#4979)
- Dev: Changed lifetime of context menus. (#4924)
- Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926)
- Dev: Refactor `IrcMessageHandler`, removing a bunch of clang-tidy warnings & changing its public API. (#4927)

View file

@ -1,13 +1,17 @@
#include "Credentials.hpp"
#include "common/Credentials.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "util/CombinePath.hpp"
#include "util/Overloaded.hpp"
#include "util/Variant.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QSaveFile>
#include <variant>
#ifndef NO_QTKEYCHAIN
# ifdef CMAKE_BUILD
@ -20,8 +24,6 @@
# include "keychain.h"
# endif
#endif
#include <boost/variant.hpp>
#include <QSaveFile>
#define FORMAT_NAME \
([&] { \
@ -29,9 +31,10 @@
return QString("chatterino:%1:%2").arg(provider).arg(name_); \
})()
namespace chatterino {
namespace {
using namespace chatterino;
bool useKeyring()
{
#ifdef NO_QTKEYCHAIN
@ -41,15 +44,13 @@ namespace {
{
return false;
}
else
{
#ifdef Q_OS_LINUX
return getSettings()->useKeyring;
#else
return true;
#endif
}
}
// Insecure storage:
QString insecurePath()
@ -103,15 +104,15 @@ namespace {
QString name;
};
using Job = boost::variant<SetJob, EraseJob>;
using Job = std::variant<SetJob, EraseJob>;
static std::queue<Job> &jobQueue()
std::queue<Job> &jobQueue()
{
static std::queue<Job> jobs;
return jobs;
}
static void runNextJob()
void runNextJob()
{
#ifndef NO_QTKEYCHAIN
auto &&queue = jobQueue();
@ -122,10 +123,10 @@ namespace {
auto &&item = queue.front();
if (item.which() == 0) // set job
{
auto set = boost::get<SetJob>(item);
auto job = new QKeychain::WritePasswordJob("chatterino");
std::visit(
variant::Overloaded{
[](const SetJob &set) {
auto *job = new QKeychain::WritePasswordJob("chatterino");
job->setAutoDelete(true);
job->setKey(set.name);
job->setTextData(set.credential);
@ -134,11 +135,9 @@ namespace {
runNextJob();
});
job->start();
}
else // erase job
{
auto erase = boost::get<EraseJob>(item);
auto job = new QKeychain::DeletePasswordJob("chatterino");
},
[](const EraseJob &erase) {
auto *job = new QKeychain::DeletePasswordJob("chatterino");
job->setAutoDelete(true);
job->setKey(erase.name);
QObject::connect(job, &QKeychain::Job::finished, qApp,
@ -146,14 +145,16 @@ namespace {
runNextJob();
});
job->start();
}
},
},
item);
queue.pop();
}
#endif
}
static void queueJob(Job &&job)
void queueJob(Job &&job)
{
auto &&queue = jobQueue();
@ -163,18 +164,18 @@ namespace {
runNextJob();
}
}
} // namespace
namespace chatterino {
Credentials &Credentials::instance()
{
static Credentials creds;
return creds;
}
Credentials::Credentials()
{
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void Credentials::get(const QString &provider, const QString &name_,
QObject *receiver,
std::function<void(const QString &)> &&onLoaded)
@ -187,7 +188,7 @@ void Credentials::get(const QString &provider, const QString &name_,
{
#ifndef NO_QTKEYCHAIN
// if NO_QTKEYCHAIN is set, then this code is never used either way
auto job = new QKeychain::ReadPasswordJob("chatterino");
auto *job = new QKeychain::ReadPasswordJob("chatterino");
job->setAutoDelete(true);
job->setKey(name);
QObject::connect(
@ -207,6 +208,7 @@ void Credentials::get(const QString &provider, const QString &name_,
}
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void Credentials::set(const QString &provider, const QString &name_,
const QString &credential)
{
@ -233,6 +235,7 @@ void Credentials::set(const QString &provider, const QString &name_,
}
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void Credentials::erase(const QString &provider, const QString &name_)
{
assertInGuiThread();

View file

@ -19,7 +19,7 @@ public:
void erase(const QString &provider, const QString &name);
private:
Credentials();
Credentials() = default;
};
} // namespace chatterino