-
-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathEmoteSource.cpp
More file actions
168 lines (146 loc) · 5.22 KB
/
EmoteSource.cpp
File metadata and controls
168 lines (146 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "controllers/completion/sources/EmoteSource.hpp"
#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/completion/sources/Helpers.hpp"
#include "controllers/emotes/ChannelEmotes.hpp"
#include "controllers/emotes/EmoteController.hpp"
#include "controllers/emotes/EmoteProvider.hpp"
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/emoji/Emojis.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "widgets/splits/InputCompletionItem.hpp"
#include <QStringBuilder>
namespace chatterino::completion {
namespace {
void addEmotes(std::vector<EmoteItem> &out, const EmoteMap &map,
const QString &providerName)
{
for (auto &&emote : map)
{
out.push_back({.emote = emote.second,
.searchName = emote.first.string,
.tabCompletionName = emote.first.string,
.displayName = emote.second->name.string,
.providerName = providerName,
.isEmoji = false});
}
}
void addEmojis(std::vector<EmoteItem> &out, const std::vector<EmojiPtr> &map)
{
for (const auto &emoji : map)
{
for (auto &&shortCode : emoji->shortCodes)
{
out.push_back(
{.emote = emoji->emote,
.searchName = shortCode,
.tabCompletionName = QStringLiteral(":%1:").arg(shortCode),
.displayName = shortCode,
.providerName = "Emoji",
.isEmoji = true});
}
};
}
} // namespace
EmoteSource::EmoteSource(const Channel *channel,
std::unique_ptr<EmoteStrategy> strategy,
ActionCallback callback)
: strategy_(std::move(strategy))
, callback_(std::move(callback))
{
this->initializeFromChannel(channel);
}
void EmoteSource::update(const QString &query)
{
this->output_.clear();
if (this->strategy_)
{
this->strategy_->apply(this->items_, this->output_, query);
}
}
void EmoteSource::addToListModel(GenericListModel &model, size_t maxCount) const
{
addVecToListModel(this->output_, model, maxCount,
[this](const EmoteItem &e) {
return std::make_unique<InputCompletionItem>(
e.emote, e.displayName + " - " + e.providerName,
this->callback_);
});
}
void EmoteSource::addToStringList(QStringList &list, size_t maxCount,
bool /* isFirstWord */) const
{
addVecToStringList(this->output_, list, maxCount, [](const EmoteItem &e) {
return e.tabCompletionName + " ";
});
}
void EmoteSource::initializeFromChannel(const Channel *channel)
{
auto *app = getApp();
std::vector<EmoteItem> emotes;
const auto *tc = dynamic_cast<const TwitchChannel *>(channel);
// returns true also for special Twitch channels (/live, /mentions, /whispers, etc.)
if (channel->isTwitchChannel())
{
if (tc)
{
if (auto twitch = tc->localTwitchEmotes())
{
addEmotes(emotes, *twitch, "Local Twitch Emotes");
}
auto user = getApp()->getAccounts()->twitch.getCurrent();
addEmotes(emotes, **user->accessEmotes(), "Twitch Emote");
const auto &channelEmotes = tc->channelEmotes();
for (const auto &data : channelEmotes.providerData())
{
auto provider = data.provider.lock();
if (!provider)
{
continue;
}
addEmotes(emotes, *data.emotes, u"Channel " % provider->name());
}
// TODO extract "Channel {BetterTTV,7TV,FrankerFaceZ}" text into a #define.
if (auto bttv = tc->bttvEmotes())
{
addEmotes(emotes, *bttv, "Channel BetterTTV");
}
if (auto ffz = tc->ffzEmotes())
{
addEmotes(emotes, *ffz, "Channel FrankerFaceZ");
}
if (auto seventv = tc->seventvEmotes())
{
addEmotes(emotes, *seventv, "Channel 7TV");
}
}
for (const auto &provider : app->getEmotes()->getProviders())
{
addEmotes(emotes, *provider->globalEmotes(),
u"Global " % provider->name());
}
if (auto bttvG = app->getBttvEmotes()->emotes())
{
addEmotes(emotes, *bttvG, "Global BetterTTV");
}
if (auto ffzG = app->getFfzEmotes()->emotes())
{
addEmotes(emotes, *ffzG, "Global FrankerFaceZ");
}
if (auto seventvG = app->getSeventvEmotes()->globalEmotes())
{
addEmotes(emotes, *seventvG, "Global 7TV");
}
}
addEmojis(emotes, app->getEmotes()->getEmojis()->getEmojis());
this->items_ = std::move(emotes);
}
const std::vector<EmoteItem> &EmoteSource::output() const
{
return this->output_;
}
} // namespace chatterino::completion