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"
|
2018-08-11 22:23:06 +02:00
|
|
|
#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"
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-03-24 11:12:24 +01:00
|
|
|
#include <QtAlgorithms>
|
2018-03-30 12:06:02 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
namespace chatterino {
|
2018-03-24 12:02:07 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
void CompletionModel::refresh(const QString &prefix)
|
2017-12-31 00:50:07 +01:00
|
|
|
{
|
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
|
|
|
|
|
|
|
auto addString = [&](const QString &str, TaggedString::Type type) {
|
2018-08-14 17:34:45 +02:00
|
|
|
if (str.startsWith(prefix, Qt::CaseInsensitive))
|
|
|
|
this->items_.emplace(str + " ", type);
|
2018-08-13 13:54:39 +02:00
|
|
|
};
|
2017-12-31 00:50:07 +01: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;
|
|
|
|
|
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)))
|
|
|
|
{
|
2018-09-21 13:57:39 +02:00
|
|
|
addString("@" + name, TaggedString::Type::Username);
|
|
|
|
}
|
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)))
|
|
|
|
{
|
2018-09-21 13:57:39 +02:00
|
|
|
addString(name, TaggedString::Type::Username);
|
|
|
|
}
|
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
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto &command : getApp()->commands->items.getVector())
|
|
|
|
{
|
2018-08-13 13:54:39 +02:00
|
|
|
addString(command.name, TaggedString::Command);
|
|
|
|
}
|
2018-06-24 13:56:56 +02:00
|
|
|
|
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
|