Implement simple ignored phrase system
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include "controllers/ignores/ignorecontroller.hpp"
|
||||
|
||||
#include "application.hpp"
|
||||
#include "controllers/ignores/ignoremodel.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace ignores {
|
||||
|
||||
void IgnoreController::initialize()
|
||||
{
|
||||
assert(!this->initialized);
|
||||
this->initialized = true;
|
||||
|
||||
for (const IgnorePhrase &phrase : this->ignoresSetting.getValue()) {
|
||||
this->phrases.appendItem(phrase);
|
||||
}
|
||||
|
||||
this->phrases.delayedItemsChanged.connect([this] { //
|
||||
this->ignoresSetting.setValue(this->phrases.getVector());
|
||||
});
|
||||
}
|
||||
|
||||
IgnoreModel *IgnoreController::createModel(QObject *parent)
|
||||
{
|
||||
IgnoreModel *model = new IgnoreModel(parent);
|
||||
model->init(&this->phrases);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
} // namespace ignores
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "controllers/ignores/ignorephrase.hpp"
|
||||
#include "singletons/settingsmanager.hpp"
|
||||
#include "util/signalvector2.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace ignores {
|
||||
|
||||
class IgnoreModel;
|
||||
|
||||
class IgnoreController
|
||||
{
|
||||
public:
|
||||
void initialize();
|
||||
|
||||
util::UnsortedSignalVector<IgnorePhrase> phrases;
|
||||
|
||||
IgnoreModel *createModel(QObject *parent);
|
||||
|
||||
private:
|
||||
bool initialized = false;
|
||||
|
||||
singletons::ChatterinoSetting<std::vector<ignores::IgnorePhrase>> ignoresSetting = {
|
||||
"/ignore/phrases"};
|
||||
};
|
||||
|
||||
} // namespace ignores
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "ignoremodel.hpp"
|
||||
|
||||
#include "application.hpp"
|
||||
#include "singletons/settingsmanager.hpp"
|
||||
#include "util/standarditemhelper.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace ignores {
|
||||
|
||||
// commandmodel
|
||||
IgnoreModel::IgnoreModel(QObject *parent)
|
||||
: util::SignalVectorModel<IgnorePhrase>(2, parent)
|
||||
{
|
||||
}
|
||||
|
||||
// turn a vector item into a model row
|
||||
IgnorePhrase IgnoreModel::getItemFromRow(std::vector<QStandardItem *> &row)
|
||||
{
|
||||
// key, regex
|
||||
|
||||
return IgnorePhrase{row[0]->data(Qt::DisplayRole).toString(),
|
||||
row[1]->data(Qt::CheckStateRole).toBool()};
|
||||
}
|
||||
|
||||
// turns a row in the model into a vector item
|
||||
void IgnoreModel::getRowFromItem(const IgnorePhrase &item, std::vector<QStandardItem *> &row)
|
||||
{
|
||||
util::setStringItem(row[0], item.getPattern());
|
||||
util::setBoolItem(row[1], item.isRegex());
|
||||
}
|
||||
|
||||
} // namespace ignores
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "controllers/ignores/ignorephrase.hpp"
|
||||
#include "util/signalvectormodel.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace ignores {
|
||||
|
||||
class IgnoreController;
|
||||
|
||||
class IgnoreModel : public util::SignalVectorModel<IgnorePhrase>
|
||||
{
|
||||
explicit IgnoreModel(QObject *parent);
|
||||
|
||||
protected:
|
||||
// turn a vector item into a model row
|
||||
virtual IgnorePhrase getItemFromRow(std::vector<QStandardItem *> &row) override;
|
||||
|
||||
// turns a row in the model into a vector item
|
||||
virtual void getRowFromItem(const IgnorePhrase &item,
|
||||
std::vector<QStandardItem *> &row) override;
|
||||
|
||||
friend class IgnoreController;
|
||||
};
|
||||
|
||||
} // namespace ignores
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include "util/rapidjson-helpers.hpp"
|
||||
#include "util/serialize-custom.hpp"
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QString>
|
||||
#include <pajlada/settings/serialize.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace ignores {
|
||||
|
||||
class IgnorePhrase
|
||||
{
|
||||
QString pattern;
|
||||
bool _isRegex;
|
||||
QRegularExpression regex;
|
||||
|
||||
public:
|
||||
bool operator==(const IgnorePhrase &other) const
|
||||
{
|
||||
return std::tie(this->pattern, this->_isRegex) == std::tie(other.pattern, other._isRegex);
|
||||
}
|
||||
|
||||
IgnorePhrase(const QString &_pattern, bool isRegex)
|
||||
: pattern(_pattern)
|
||||
, _isRegex(isRegex)
|
||||
, regex(_isRegex ? _pattern : "\\b" + QRegularExpression::escape(_pattern) + "\\b",
|
||||
QRegularExpression::CaseInsensitiveOption)
|
||||
{
|
||||
}
|
||||
|
||||
const QString &getPattern() const
|
||||
{
|
||||
return this->pattern;
|
||||
}
|
||||
bool isRegex() const
|
||||
{
|
||||
return this->_isRegex;
|
||||
}
|
||||
|
||||
bool isValid() const
|
||||
{
|
||||
return !this->pattern.isEmpty() && this->regex.isValid();
|
||||
}
|
||||
|
||||
bool isMatch(const QString &subject) const
|
||||
{
|
||||
return this->isValid() && this->regex.match(subject).hasMatch();
|
||||
}
|
||||
};
|
||||
} // namespace ignores
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
|
||||
namespace pajlada {
|
||||
namespace Settings {
|
||||
|
||||
template <>
|
||||
struct Serialize<chatterino::controllers::ignores::IgnorePhrase> {
|
||||
static rapidjson::Value get(const chatterino::controllers::ignores::IgnorePhrase &value,
|
||||
rapidjson::Document::AllocatorType &a)
|
||||
{
|
||||
rapidjson::Value ret(rapidjson::kObjectType);
|
||||
|
||||
AddMember(ret, "pattern", value.getPattern(), a);
|
||||
AddMember(ret, "regex", value.isRegex(), a);
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Deserialize<chatterino::controllers::ignores::IgnorePhrase> {
|
||||
static chatterino::controllers::ignores::IgnorePhrase get(const rapidjson::Value &value)
|
||||
{
|
||||
if (!value.IsObject()) {
|
||||
return chatterino::controllers::ignores::IgnorePhrase(QString(), false);
|
||||
}
|
||||
|
||||
QString _pattern;
|
||||
bool _isRegex = false;
|
||||
|
||||
chatterino::rj::getSafe(value, "pattern", _pattern);
|
||||
chatterino::rj::getSafe(value, "regex", _isRegex);
|
||||
|
||||
return chatterino::controllers::ignores::IgnorePhrase(_pattern, _isRegex);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Settings
|
||||
} // namespace pajlada
|
||||
Reference in New Issue
Block a user