mirror-chatterino2/src/common/CompletionModel.cpp

196 lines
5.6 KiB
C++
Raw Normal View History

2018-06-26 15:33:51 +02:00
#include "common/CompletionModel.hpp"
2017-12-31 00:50:07 +01:00
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-08-13 13:54:39 +02:00
#include "common/UsernameSet.hpp"
2018-08-02 14:23:27 +02:00
#include "controllers/accounts/AccountController.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/commands/CommandController.hpp"
2018-08-13 13:54:39 +02:00
#include "debug/Benchmark.hpp"
2018-06-26 14:09:39 +02:00
#include "debug/Log.hpp"
#include "providers/twitch/TwitchChannel.hpp"
2018-08-02 14:23:27 +02:00
#include "providers/twitch/TwitchServer.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
2017-12-31 00:50:07 +01:00
#include <QtAlgorithms>
#include <utility>
2017-12-31 00:50:07 +01:00
namespace chatterino {
2018-08-13 14:38:03 +02:00
//
// TaggedString
//
2018-07-06 17:42:00 +02:00
2018-08-13 14:38:03 +02:00
CompletionModel::TaggedString::TaggedString(const QString &_string, Type _type)
: string(_string)
2018-07-06 17:42:00 +02:00
, type(_type)
{
}
bool CompletionModel::TaggedString::isEmote() const
{
return this->type > Type::EmoteStart && this->type < Type::EmoteEnd;
}
bool CompletionModel::TaggedString::operator<(const TaggedString &that) const
{
2018-10-21 13:43:02 +02:00
if (this->isEmote() != that.isEmote())
{
2018-08-13 14:38:03 +02:00
return this->isEmote();
2018-07-06 17:42:00 +02:00
}
2018-08-13 14:38:03 +02:00
// try comparing insensitively, if they are the same then senstively
// (fixes order of LuL and LUL)
int k = QString::compare(this->string, that.string, Qt::CaseInsensitive);
2018-10-21 13:43:02 +02:00
if (k == 0)
return this->string > that.string;
2018-07-06 17:42:00 +02:00
return k < 0;
}
2018-08-13 14:38:03 +02:00
//
// CompletionModel
//
CompletionModel::CompletionModel(Channel &channel)
: channel_(channel)
2017-12-31 00:50:07 +01:00
{
}
2018-07-06 17:42:00 +02:00
int CompletionModel::columnCount(const QModelIndex &) const
{
return 1;
}
QVariant CompletionModel::data(const QModelIndex &index, int) const
{
2018-08-13 14:38:03 +02:00
std::lock_guard<std::mutex> lock(this->itemsMutex_);
2018-07-06 17:42:00 +02:00
2018-08-13 14:38:03 +02:00
auto it = this->items_.begin();
2018-07-06 17:42:00 +02:00
std::advance(it, index.row());
2018-08-13 14:38:03 +02:00
return QVariant(it->string);
2018-07-06 17:42:00 +02:00
}
int CompletionModel::rowCount(const QModelIndex &) const
{
2018-08-13 14:38:03 +02:00
std::lock_guard<std::mutex> lock(this->itemsMutex_);
2018-07-06 17:42:00 +02:00
2018-08-13 14:38:03 +02:00
return this->items_.size();
2018-07-06 17:42:00 +02:00
}
2019-08-21 01:08:15 +02:00
void CompletionModel::refresh(const QString &prefix, bool isFirstWord)
2017-12-31 00:50:07 +01:00
{
std::function<void(const QString &, TaggedString::Type)> addString;
if (getSettings()->prefixOnlyEmoteCompletion)
{
addString = [&](const QString &str, TaggedString::Type type) {
if (str.startsWith(prefix, Qt::CaseInsensitive))
this->items_.emplace(str + " ", type);
};
}
else
{
addString = [&](const QString &str, TaggedString::Type type) {
if (str.contains(prefix, Qt::CaseInsensitive))
this->items_.emplace(str + " ", type);
};
}
2018-08-13 14:38:03 +02:00
std::lock_guard<std::mutex> guard(this->itemsMutex_);
this->items_.clear();
2018-08-13 13:54:39 +02:00
2018-10-21 13:43:02 +02:00
if (prefix.length() < 2)
return;
2018-08-13 13:54:39 +02:00
2018-10-21 13:43:02 +02:00
if (auto channel = dynamic_cast<TwitchChannel *>(&this->channel_))
{
2018-08-13 13:54:39 +02:00
// account emotes
2018-10-21 13:43:02 +02:00
if (auto account = getApp()->accounts->twitch.getCurrent())
{
for (const auto &emote : account->accessEmotes()->allEmoteNames)
{
2018-08-13 13:54:39 +02:00
// XXX: No way to discern between a twitch global emote and sub
// emote right now
addString(emote.string, TaggedString::Type::TwitchGlobalEmote);
}
2017-12-31 00:50:07 +01:00
}
2018-08-13 13:54:39 +02:00
// Usernames
2018-10-21 13:43:02 +02:00
if (prefix.length() >= UsernameSet::PrefixLength)
{
2018-08-13 13:54:39 +02:00
auto usernames = channel->accessChatters();
2018-09-21 13:57:39 +02:00
QString usernamePrefix = prefix;
2019-08-21 01:08:15 +02:00
QString usernamePostfix =
isFirstWord && getSettings()->mentionUsersWithComma ? ","
: QString();
2018-09-21 13:57:39 +02:00
2018-10-21 13:43:02 +02:00
if (usernamePrefix.startsWith("@"))
{
2018-09-21 13:57:39 +02:00
usernamePrefix.remove(0, 1);
for (const auto &name :
2018-10-21 13:43:02 +02:00
usernames->subrange(Prefix(usernamePrefix)))
{
2019-08-21 01:08:15 +02:00
addString("@" + name + usernamePostfix,
TaggedString::Type::Username);
2018-09-21 13:57:39 +02:00
}
2018-10-21 13:43:02 +02:00
}
else
{
2018-09-21 13:57:39 +02:00
for (const auto &name :
2018-10-21 13:43:02 +02:00
usernames->subrange(Prefix(usernamePrefix)))
{
2019-08-21 01:08:15 +02:00
addString(name + usernamePostfix,
TaggedString::Type::Username);
2018-09-21 13:57:39 +02:00
}
2018-08-13 13:54:39 +02:00
}
2018-08-02 14:23:27 +02:00
}
2017-12-31 00:50:07 +01:00
2018-08-13 13:54:39 +02:00
// Bttv Global
2018-10-21 13:43:02 +02:00
for (auto &emote : *channel->globalBttv().emotes())
{
2018-08-13 13:54:39 +02:00
addString(emote.first.string, TaggedString::Type::BTTVChannelEmote);
}
2017-12-31 00:50:07 +01:00
2018-08-13 13:54:39 +02:00
// Ffz Global
2018-10-21 13:43:02 +02:00
for (auto &emote : *channel->globalFfz().emotes())
{
2018-08-13 13:54:39 +02:00
addString(emote.first.string, TaggedString::Type::FFZChannelEmote);
}
2018-06-24 11:24:21 +02:00
2018-08-13 13:54:39 +02:00
// Bttv Channel
2018-10-21 13:43:02 +02:00
for (auto &emote : *channel->bttvEmotes())
{
2018-08-13 13:54:39 +02:00
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
}
// Ffz Channel
2018-10-21 13:43:02 +02:00
for (auto &emote : *channel->ffzEmotes())
{
2018-08-13 13:54:39 +02:00
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
}
// Emojis
2018-10-21 13:43:02 +02:00
if (prefix.startsWith(":"))
{
2018-08-13 13:54:39 +02:00
const auto &emojiShortCodes = getApp()->emotes->emojis.shortCodes;
2018-10-21 13:43:02 +02:00
for (auto &m : emojiShortCodes)
{
2018-08-13 13:54:39 +02:00
addString(":" + m + ":", TaggedString::Type::Emoji);
}
}
// Commands
for (auto &command : getApp()->commands->items_)
2018-10-21 13:43:02 +02:00
{
2018-08-13 13:54:39 +02:00
addString(command.name, TaggedString::Command);
}
2018-10-21 13:43:02 +02:00
for (auto &command : getApp()->commands->getDefaultTwitchCommandList())
{
2018-08-13 13:54:39 +02:00
addString(command, TaggedString::Command);
}
}
2017-12-31 00:50:07 +01:00
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino