mirror-chatterino2/src/util/CompletionModel.hpp

139 lines
3.2 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#pragma once
2018-06-26 14:09:39 +02:00
#include "Common.hpp"
2017-12-31 00:50:07 +01:00
#include <QAbstractListModel>
2018-03-30 13:50:43 +02:00
#include <chrono>
#include <mutex>
#include <set>
2017-12-31 00:50:07 +01:00
namespace chatterino {
2017-12-31 00:50:07 +01:00
class CompletionModel : public QAbstractListModel
{
struct TaggedString {
enum Type {
Username,
2017-12-31 00:50:07 +01:00
// Emotes
FFZGlobalEmote = 20,
FFZChannelEmote,
BTTVGlobalEmote,
BTTVChannelEmote,
TwitchGlobalEmote,
TwitchSubscriberEmote,
Emoji,
2018-06-24 11:24:21 +02:00
Command,
2018-03-30 13:50:43 +02:00
};
TaggedString(const QString &_str, Type _type)
: str(_str)
, type(_type)
, timeAdded(std::chrono::steady_clock::now())
{
}
QString str;
// Type will help decide the lifetime of the tagged strings
Type type;
mutable std::chrono::steady_clock::time_point timeAdded;
2018-03-30 13:50:43 +02:00
bool HasExpired(const std::chrono::steady_clock::time_point &now) const
{
switch (this->type) {
case Type::Username: {
static std::chrono::minutes expirationTimer(10);
return (this->timeAdded + expirationTimer < now);
} break;
default: {
return false;
} break;
}
return false;
}
2017-12-31 00:50:07 +01:00
bool IsEmote() const
{
return this->type >= 20;
}
bool operator<(const TaggedString &that) const
{
if (this->IsEmote()) {
if (that.IsEmote()) {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
}
2018-03-30 13:50:43 +02:00
return k < 0;
}
2018-03-30 13:50:43 +02:00
return true;
}
2018-03-30 13:50:43 +02:00
if (that.IsEmote()) {
return false;
}
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return false;
}
return k < 0;
}
};
public:
CompletionModel(const QString &_channelName);
2018-03-30 13:50:43 +02:00
int columnCount(const QModelIndex &) const override
{
return 1;
}
2018-03-30 13:50:43 +02:00
QVariant data(const QModelIndex &index, int) const override
{
2018-03-30 13:50:43 +02:00
std::lock_guard<std::mutex> lock(this->emotesMutex);
// TODO: Implement more safely
auto it = this->emotes.begin();
std::advance(it, index.row());
return QVariant(it->str);
}
2018-03-30 13:50:43 +02:00
int rowCount(const QModelIndex &) const override
{
2018-03-30 13:50:43 +02:00
std::lock_guard<std::mutex> lock(this->emotesMutex);
return this->emotes.size();
}
void refresh();
void addString(const QString &str, TaggedString::Type type);
void addUser(const QString &str);
2018-03-30 13:50:43 +02:00
void ClearExpiredStrings();
private:
TaggedString createUser(const QString &str)
{
return TaggedString{str, TaggedString::Type::Username};
}
2018-03-30 13:50:43 +02:00
mutable std::mutex emotesMutex;
std::set<TaggedString> emotes;
2017-12-31 00:50:07 +01:00
QString channelName;
};
} // namespace chatterino