improving code quality
This commit is contained in:
+42
-5
@@ -18,6 +18,9 @@
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
//
|
||||
// Channel
|
||||
//
|
||||
Channel::Channel(const QString &name, Type type)
|
||||
: completionModel(name)
|
||||
, name_(name)
|
||||
@@ -67,8 +70,7 @@ void Channel::addMessage(MessagePtr message)
|
||||
|
||||
const QString &username = message->loginName;
|
||||
if (!username.isEmpty()) {
|
||||
// TODO: Add recent chatters display name. This should maybe be a
|
||||
// setting
|
||||
// TODO: Add recent chatters display name
|
||||
this->addRecentChatter(message);
|
||||
}
|
||||
|
||||
@@ -194,7 +196,6 @@ void Channel::replaceMessage(MessagePtr message, MessagePtr replacement)
|
||||
|
||||
void Channel::addRecentChatter(const MessagePtr &message)
|
||||
{
|
||||
// Do nothing by default
|
||||
}
|
||||
|
||||
bool Channel::canSendMessage() const
|
||||
@@ -232,9 +233,45 @@ void Channel::onConnected()
|
||||
{
|
||||
}
|
||||
|
||||
std::weak_ptr<Channel> Channel::weak_from_this()
|
||||
//
|
||||
// Indirect channel
|
||||
//
|
||||
IndirectChannel::Data::Data(ChannelPtr _channel, Channel::Type _type)
|
||||
: channel(_channel)
|
||||
, type(_type)
|
||||
{
|
||||
return std::weak_ptr<Channel>(this->shared_from_this());
|
||||
}
|
||||
|
||||
IndirectChannel::IndirectChannel(ChannelPtr channel, Channel::Type type)
|
||||
: data_(std::make_unique<Data>(channel, type))
|
||||
{
|
||||
}
|
||||
|
||||
ChannelPtr IndirectChannel::get()
|
||||
{
|
||||
return data_->channel;
|
||||
}
|
||||
|
||||
void IndirectChannel::reset(ChannelPtr channel)
|
||||
{
|
||||
assert(this->data_->type != Channel::Type::Direct);
|
||||
|
||||
this->data_->channel = channel;
|
||||
this->data_->changed.invoke();
|
||||
}
|
||||
|
||||
pajlada::Signals::NoArgSignal &IndirectChannel::getChannelChanged()
|
||||
{
|
||||
return this->data_->changed;
|
||||
}
|
||||
|
||||
Channel::Type IndirectChannel::getType()
|
||||
{
|
||||
if (this->data_->type == Channel::Type::Direct) {
|
||||
return this->get()->getType();
|
||||
} else {
|
||||
return this->data_->type;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
+6
-40
@@ -4,7 +4,6 @@
|
||||
#include "messages/Image.hpp"
|
||||
#include "messages/LimitedQueue.hpp"
|
||||
#include "messages/Message.hpp"
|
||||
#include "util/ConcurrentMap.hpp"
|
||||
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
@@ -66,9 +65,6 @@ public:
|
||||
|
||||
CompletionModel completionModel;
|
||||
|
||||
// pre c++17 polyfill
|
||||
std::weak_ptr<Channel> weak_from_this();
|
||||
|
||||
protected:
|
||||
virtual void onConnected();
|
||||
|
||||
@@ -88,47 +84,17 @@ class IndirectChannel
|
||||
Channel::Type type;
|
||||
pajlada::Signals::NoArgSignal changed;
|
||||
|
||||
Data() = delete;
|
||||
Data(ChannelPtr _channel, Channel::Type _type)
|
||||
: channel(_channel)
|
||||
, type(_type)
|
||||
{
|
||||
}
|
||||
Data(ChannelPtr channel, Channel::Type type);
|
||||
};
|
||||
|
||||
public:
|
||||
IndirectChannel(ChannelPtr channel,
|
||||
Channel::Type type = Channel::Type::Direct)
|
||||
: data_(new Data(channel, type))
|
||||
{
|
||||
}
|
||||
Channel::Type type = Channel::Type::Direct);
|
||||
|
||||
ChannelPtr get()
|
||||
{
|
||||
return data_->channel;
|
||||
}
|
||||
|
||||
void update(ChannelPtr ptr)
|
||||
{
|
||||
assert(this->data_->type != Channel::Type::Direct);
|
||||
|
||||
this->data_->channel = ptr;
|
||||
this->data_->changed.invoke();
|
||||
}
|
||||
|
||||
pajlada::Signals::NoArgSignal &getChannelChanged()
|
||||
{
|
||||
return this->data_->changed;
|
||||
}
|
||||
|
||||
Channel::Type getType()
|
||||
{
|
||||
if (this->data_->type == Channel::Type::Direct) {
|
||||
return this->get()->getType();
|
||||
} else {
|
||||
return this->data_->type;
|
||||
}
|
||||
}
|
||||
ChannelPtr get();
|
||||
void reset(ChannelPtr channel);
|
||||
pajlada::Signals::NoArgSignal &getChannelChanged();
|
||||
Channel::Type getType();
|
||||
|
||||
private:
|
||||
std::shared_ptr<Data> data_;
|
||||
|
||||
+45
-51
@@ -5,69 +5,63 @@
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
|
||||
// ip 0.0.0.0 - 224.0.0.0
|
||||
#define IP \
|
||||
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" \
|
||||
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" \
|
||||
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
|
||||
#define PORT "(?::\\d{2,5})"
|
||||
#define WEB_CHAR1 "[_a-z\\x{00a1}-\\x{ffff}0-9]"
|
||||
#define WEB_CHAR2 "[a-z\\x{00a1}-\\x{ffff}0-9]"
|
||||
|
||||
#define SPOTIFY_1 "(?:artist|album|track|user:[^:]+:playlist):[a-zA-Z0-9]+"
|
||||
#define SPOTIFY_2 "user:[^:]+"
|
||||
#define SPOTIFY_3 "search:(?:[-\\w$\\.+!*'(),]+|%[a-fA-F0-9]{2})+"
|
||||
#define SPOTIFY_PARAMS "(?:" SPOTIFY_1 "|" SPOTIFY_2 "|" SPOTIFY_3 ")"
|
||||
#define SPOTIFY_LINK "(?x-mi:(spotify:" SPOTIFY_PARAMS "))"
|
||||
|
||||
#define WEB_PROTOCOL "(?:(?:https?|ftps?)://)?"
|
||||
#define WEB_USER "(?:\\S+(?::\\S*)?@)?"
|
||||
#define WEB_HOST "(?:(?:" WEB_CHAR1 "-*)*" WEB_CHAR2 "+)"
|
||||
#define WEB_DOMAIN "(?:\\.(?:" WEB_CHAR2 "-*)*" WEB_CHAR2 "+)*"
|
||||
#define WEB_TLD "(?:" + tldData + ")"
|
||||
#define WEB_RESOURCE_PATH "(?:[/?#]\\S*)"
|
||||
#define WEB_LINK \
|
||||
WEB_PROTOCOL WEB_USER "(?:" IP "|" WEB_HOST WEB_DOMAIN "\\." WEB_TLD PORT \
|
||||
"?" WEB_RESOURCE_PATH "?)"
|
||||
|
||||
#define LINK "^(?:" SPOTIFY_LINK "|" WEB_LINK ")$"
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
LinkParser::LinkParser(const QString &unparsedString)
|
||||
{
|
||||
static QRegularExpression linkRegex = [] {
|
||||
static QRegularExpression newLineRegex("\r?\n");
|
||||
QFile tldFile(":/tlds.txt");
|
||||
tldFile.open(QFile::ReadOnly);
|
||||
QFile file(":/tlds.txt");
|
||||
file.open(QFile::ReadOnly);
|
||||
QTextStream tlds(&file);
|
||||
tlds.setCodec("UTF-8");
|
||||
|
||||
QTextStream t1(&tldFile);
|
||||
t1.setCodec("UTF-8");
|
||||
// tldData gets injected into the LINK macro
|
||||
auto tldData = tlds.readAll().replace(newLineRegex, "|");
|
||||
(void)tldData;
|
||||
|
||||
// Read the TLDs in and replace the newlines with pipes
|
||||
QString tldData = t1.readAll().replace(newLineRegex, "|");
|
||||
|
||||
const QString hyperlinkRegExp =
|
||||
"^"
|
||||
// Identifier for spotify
|
||||
"(?x-mi:(spotify:(?:"
|
||||
"(?:artist|album|track|user:[^:]+:playlist):"
|
||||
"[a-zA-Z0-9]+|user:[^:]+|search:"
|
||||
"(?:[-\\w$\\.+!*'(),]+|%[a-fA-F0-9]{2})+)))"
|
||||
// If nothing matches then just go on
|
||||
"|"
|
||||
"^"
|
||||
// Identifier for http and ftp
|
||||
"(?:(?:https?|ftps?)://)?"
|
||||
// user:pass authentication
|
||||
"(?:\\S+(?::\\S*)?@)?"
|
||||
"(?:"
|
||||
// IP address dotted notation octets
|
||||
// excludes loopback network 0.0.0.0
|
||||
// excludes reserved space >= 224.0.0.0
|
||||
// excludes network & broacast addresses
|
||||
// (first & last IP address of each class)
|
||||
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
|
||||
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
|
||||
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
|
||||
"|"
|
||||
// host name
|
||||
"(?:(?:[_a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+"
|
||||
")"
|
||||
// domain name
|
||||
"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-"
|
||||
"9]+)*"
|
||||
// TLD identifier
|
||||
//"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,}))"
|
||||
"(?:[\\.](?:" +
|
||||
tldData +
|
||||
"))"
|
||||
"\\.?"
|
||||
")"
|
||||
// port number
|
||||
"(?::\\d{2,5})?"
|
||||
// resource path
|
||||
"(?:[/?#]\\S*)?"
|
||||
"$";
|
||||
|
||||
return QRegularExpression(hyperlinkRegExp,
|
||||
return QRegularExpression(LINK,
|
||||
QRegularExpression::CaseInsensitiveOption);
|
||||
}();
|
||||
|
||||
this->match_ = linkRegex.match(unparsedString);
|
||||
}
|
||||
|
||||
bool LinkParser::hasMatch() const
|
||||
{
|
||||
return this->match_.hasMatch();
|
||||
}
|
||||
|
||||
QString LinkParser::getCaptured() const
|
||||
{
|
||||
return this->match_.captured();
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -10,15 +10,8 @@ class LinkParser
|
||||
public:
|
||||
explicit LinkParser(const QString &unparsedString);
|
||||
|
||||
bool hasMatch() const
|
||||
{
|
||||
return this->match_.hasMatch();
|
||||
}
|
||||
|
||||
QString getCaptured() const
|
||||
{
|
||||
return this->match_.captured();
|
||||
}
|
||||
bool hasMatch() const;
|
||||
QString getCaptured() const;
|
||||
|
||||
private:
|
||||
QRegularExpressionMatch match_;
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
template <typename Type>
|
||||
class LockedObject
|
||||
{
|
||||
public:
|
||||
LockedObject &operator=(const LockedObject<Type> &other)
|
||||
{
|
||||
this->mutex_.lock();
|
||||
|
||||
this->data = other.getValue();
|
||||
|
||||
this->mutex_.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
LockedObject &operator=(const Type &other)
|
||||
{
|
||||
this->mutex_.lock();
|
||||
|
||||
this->data = other;
|
||||
|
||||
this->mutex_.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
Type value_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -5,11 +5,11 @@
|
||||
namespace chatterino {
|
||||
|
||||
QThread NetworkManager::workerThread;
|
||||
QNetworkAccessManager NetworkManager::NaM;
|
||||
QNetworkAccessManager NetworkManager::accessManager;
|
||||
|
||||
void NetworkManager::init()
|
||||
{
|
||||
NetworkManager::NaM.moveToThread(&NetworkManager::workerThread);
|
||||
NetworkManager::accessManager.moveToThread(&NetworkManager::workerThread);
|
||||
NetworkManager::workerThread.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class NetworkManager : public QObject
|
||||
|
||||
public:
|
||||
static QThread workerThread;
|
||||
static QNetworkAccessManager NaM;
|
||||
static QNetworkAccessManager accessManager;
|
||||
|
||||
static void init();
|
||||
static void deinit();
|
||||
|
||||
@@ -186,14 +186,14 @@ void NetworkRequest::doRequest()
|
||||
auto reply = [&]() -> QNetworkReply * {
|
||||
switch (data->requestType_) {
|
||||
case NetworkRequestType::Get:
|
||||
return NetworkManager::NaM.get(data->request_);
|
||||
return NetworkManager::accessManager.get(data->request_);
|
||||
|
||||
case NetworkRequestType::Put:
|
||||
return NetworkManager::NaM.put(data->request_,
|
||||
return NetworkManager::accessManager.put(data->request_,
|
||||
data->payload_);
|
||||
|
||||
case NetworkRequestType::Delete:
|
||||
return NetworkManager::NaM.deleteResource(data->request_);
|
||||
return NetworkManager::accessManager.deleteResource(data->request_);
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
|
||||
@@ -58,8 +58,6 @@ public:
|
||||
void execute();
|
||||
|
||||
private:
|
||||
// Returns true if the file was successfully loaded from cache
|
||||
// Returns false if the cache file either didn't exist, or it contained
|
||||
// "invalid" data "invalid" is specified by the onSuccess callback
|
||||
Outcome tryLoadCachedFile();
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "boost/noncopyable.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
template <typename T>
|
||||
class Property final : boost::noncopyable
|
||||
{
|
||||
public:
|
||||
Property()
|
||||
{
|
||||
}
|
||||
|
||||
Property(const T &value)
|
||||
: value_(value)
|
||||
{
|
||||
}
|
||||
|
||||
T &operator=(const T &f)
|
||||
{
|
||||
return value_ = f;
|
||||
}
|
||||
|
||||
operator T const &() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
protected:
|
||||
T value_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -1,43 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <pajlada/settings/serialize.hpp>
|
||||
|
||||
namespace pajlada {
|
||||
namespace Settings {
|
||||
|
||||
template <>
|
||||
struct Serialize<QString> {
|
||||
static rapidjson::Value get(const QString &value,
|
||||
rapidjson::Document::AllocatorType &a)
|
||||
{
|
||||
return rapidjson::Value(value.toUtf8(), a);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Deserialize<QString> {
|
||||
static QString get(const rapidjson::Value &value, bool *error = nullptr)
|
||||
{
|
||||
if (!value.IsString()) {
|
||||
PAJLADA_REPORT_ERROR(error)
|
||||
PAJLADA_THROW_EXCEPTION(
|
||||
"Deserialized rapidjson::Value is not a string");
|
||||
return QString{};
|
||||
}
|
||||
|
||||
try {
|
||||
return QString::fromUtf8(value.GetString(),
|
||||
value.GetStringLength());
|
||||
} catch (const std::exception &) {
|
||||
// int x = 5;
|
||||
} catch (...) {
|
||||
// int y = 5;
|
||||
}
|
||||
|
||||
return QString{};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Settings
|
||||
} // namespace pajlada
|
||||
Reference in New Issue
Block a user