refactored Channel

This commit is contained in:
fourtf 2018-07-06 17:30:12 +02:00
parent b639604a47
commit aa3df2e6d8
14 changed files with 55 additions and 59 deletions

View file

@ -20,31 +20,27 @@ namespace chatterino {
Channel::Channel(const QString &_name, Type _type) Channel::Channel(const QString &_name, Type _type)
: name(_name) : name(_name)
, completionModel(this->name) , completionModel(this->name)
, type(_type) , type_(_type)
{ {
this->clearCompletionModelTimer = new QTimer; QObject::connect(&this->clearCompletionModelTimer, &QTimer::timeout, [this]() {
QObject::connect(this->clearCompletionModelTimer, &QTimer::timeout, [this]() {
this->completionModel.ClearExpiredStrings(); // this->completionModel.ClearExpiredStrings(); //
}); });
this->clearCompletionModelTimer->start(60 * 1000); this->clearCompletionModelTimer.start(60 * 1000);
} }
Channel::~Channel() Channel::~Channel()
{ {
this->destroyed.invoke(); this->destroyed.invoke();
this->clearCompletionModelTimer->stop();
this->clearCompletionModelTimer->deleteLater();
} }
Channel::Type Channel::getType() const Channel::Type Channel::getType() const
{ {
return this->type; return this->type_;
} }
bool Channel::isTwitchChannel() const bool Channel::isTwitchChannel() const
{ {
return this->type >= Twitch && this->type < TwitchEnd; return this->type_ >= Type::Twitch && this->type_ < Type::TwitchEnd;
} }
bool Channel::isEmpty() const bool Channel::isEmpty() const
@ -54,7 +50,7 @@ bool Channel::isEmpty() const
LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot() LimitedQueueSnapshot<MessagePtr> Channel::getMessageSnapshot()
{ {
return this->messages.getSnapshot(); return this->messages_.getSnapshot();
} }
void Channel::addMessage(MessagePtr message) void Channel::addMessage(MessagePtr message)
@ -73,7 +69,7 @@ void Channel::addMessage(MessagePtr message)
app->logging->addMessage(this->name, message); app->logging->addMessage(this->name, message);
} }
if (this->messages.pushBack(message, deleted)) { if (this->messages_.pushBack(message, deleted)) {
this->messageRemovedFromStart.invoke(deleted); this->messageRemovedFromStart.invoke(deleted);
} }
@ -163,7 +159,7 @@ void Channel::disableAllMessages()
void Channel::addMessagesAtStart(std::vector<MessagePtr> &_messages) void Channel::addMessagesAtStart(std::vector<MessagePtr> &_messages)
{ {
std::vector<MessagePtr> addedMessages = this->messages.pushFront(_messages); std::vector<MessagePtr> addedMessages = this->messages_.pushFront(_messages);
if (addedMessages.size() != 0) { if (addedMessages.size() != 0) {
this->messagesAddedAtStart.invoke(addedMessages); this->messagesAddedAtStart.invoke(addedMessages);
@ -172,7 +168,7 @@ void Channel::addMessagesAtStart(std::vector<MessagePtr> &_messages)
void Channel::replaceMessage(MessagePtr message, MessagePtr replacement) void Channel::replaceMessage(MessagePtr message, MessagePtr replacement)
{ {
int index = this->messages.replaceItem(message, replacement); int index = this->messages_.replaceItem(message, replacement);
if (index >= 0) { if (index >= 0) {
this->messageReplaced.invoke((size_t)index, replacement); this->messageReplaced.invoke((size_t)index, replacement);
@ -211,7 +207,7 @@ bool Channel::hasModRights() const
std::shared_ptr<Channel> Channel::getEmpty() std::shared_ptr<Channel> Channel::getEmpty()
{ {
static std::shared_ptr<Channel> channel(new Channel("", None)); static std::shared_ptr<Channel> channel(new Channel("", Type::None));
return channel; return channel;
} }

View file

@ -17,10 +17,8 @@ struct Message;
class Channel : public std::enable_shared_from_this<Channel> class Channel : public std::enable_shared_from_this<Channel>
{ {
QTimer *clearCompletionModelTimer;
public: public:
enum Type { enum class Type {
None, None,
Direct, Direct,
Twitch, Twitch,
@ -31,7 +29,7 @@ public:
Misc Misc
}; };
explicit Channel(const QString &_name, Type type); explicit Channel(const QString &_name, Type type_);
virtual ~Channel(); virtual ~Channel();
pajlada::Signals::Signal<const QString &, const QString &, bool &> sendMessageSignal; pajlada::Signals::Signal<const QString &, const QString &, bool &> sendMessageSignal;
@ -48,7 +46,7 @@ public:
LimitedQueueSnapshot<MessagePtr> getMessageSnapshot(); LimitedQueueSnapshot<MessagePtr> getMessageSnapshot();
void addMessage(MessagePtr message); void addMessage(MessagePtr message);
void addMessagesAtStart(std::vector<MessagePtr> &messages); void addMessagesAtStart(std::vector<MessagePtr> &messages_);
void addOrReplaceTimeout(MessagePtr message); void addOrReplaceTimeout(MessagePtr message);
void disableAllMessages(); void disableAllMessages();
void replaceMessage(MessagePtr message, MessagePtr replacement); void replaceMessage(MessagePtr message, MessagePtr replacement);
@ -71,8 +69,9 @@ protected:
virtual void onConnected(); virtual void onConnected();
private: private:
LimitedQueue<MessagePtr> messages; LimitedQueue<MessagePtr> messages_;
Type type; Type type_;
QTimer clearCompletionModelTimer_;
}; };
using ChannelPtr = std::shared_ptr<Channel>; using ChannelPtr = std::shared_ptr<Channel>;
@ -92,40 +91,41 @@ class IndirectChannel
} }
}; };
std::shared_ptr<Data> data;
public: public:
IndirectChannel(ChannelPtr channel, Channel::Type type = Channel::Direct) IndirectChannel(ChannelPtr channel, Channel::Type type = Channel::Type::Direct)
: data(new Data(channel, type)) : data_(new Data(channel, type))
{ {
} }
ChannelPtr get() ChannelPtr get()
{ {
return data->channel; return data_->channel;
} }
void update(ChannelPtr ptr) void update(ChannelPtr ptr)
{ {
assert(this->data->type != Channel::Direct); assert(this->data_->type != Channel::Type::Direct);
this->data->channel = ptr; this->data_->channel = ptr;
this->data->changed.invoke(); this->data_->changed.invoke();
} }
pajlada::Signals::NoArgSignal &getChannelChanged() pajlada::Signals::NoArgSignal &getChannelChanged()
{ {
return this->data->changed; return this->data_->changed;
} }
Channel::Type getType() Channel::Type getType()
{ {
if (this->data->type == Channel::Direct) { if (this->data_->type == Channel::Type::Direct) {
return this->get()->getType(); return this->get()->getType();
} else { } else {
return this->data->type; return this->data_->type;
} }
} }
private:
std::shared_ptr<Data> data_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -17,7 +17,7 @@
namespace chatterino { namespace chatterino {
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection) TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection)
: Channel(channelName, Channel::Twitch) : Channel(channelName, Channel::Type::Twitch)
, bttvChannelEmotes(new EmoteMap) , bttvChannelEmotes(new EmoteMap)
, ffzChannelEmotes(new EmoteMap) , ffzChannelEmotes(new EmoteMap)
, subscriptionURL("https://www.twitch.tv/subs/" + name) , subscriptionURL("https://www.twitch.tv/subs/" + name)

View file

@ -19,9 +19,9 @@ using namespace std::chrono_literals;
namespace chatterino { namespace chatterino {
TwitchServer::TwitchServer() TwitchServer::TwitchServer()
: whispersChannel(new Channel("/whispers", Channel::TwitchWhispers)) : whispersChannel(new Channel("/whispers", Channel::Type::TwitchWhispers))
, mentionsChannel(new Channel("/mentions", Channel::TwitchMentions)) , mentionsChannel(new Channel("/mentions", Channel::Type::TwitchMentions))
, watchingChannel(Channel::getEmpty(), Channel::TwitchWatching) , watchingChannel(Channel::getEmpty(), Channel::Type::TwitchWatching)
{ {
qDebug() << "init TwitchServer"; qDebug() << "init TwitchServer";
} }

View file

@ -417,17 +417,17 @@ void WindowManager::encodeChannel(IndirectChannel channel, QJsonObject &obj)
assertInGuiThread(); assertInGuiThread();
switch (channel.getType()) { switch (channel.getType()) {
case Channel::Twitch: { case Channel::Type::Twitch: {
obj.insert("type", "twitch"); obj.insert("type", "twitch");
obj.insert("name", channel.get()->name); obj.insert("name", channel.get()->name);
} break; } break;
case Channel::TwitchMentions: { case Channel::Type::TwitchMentions: {
obj.insert("type", "mentions"); obj.insert("type", "mentions");
} break; } break;
case Channel::TwitchWatching: { case Channel::Type::TwitchWatching: {
obj.insert("type", "watching"); obj.insert("type", "watching");
} break; } break;
case Channel::TwitchWhispers: { case Channel::Type::TwitchWhispers: {
obj.insert("type", "whispers"); obj.insert("type", "whispers");
} break; } break;
} }

View file

@ -84,7 +84,7 @@ AttachedWindow *AttachedWindow::get(void *target, const GetArgs &args)
} }
if (show) { if (show) {
window->updateWindowRect_(window->target_); window->updateWindowRect(window->target_);
window->show(); window->show();
} }
@ -107,10 +107,10 @@ void AttachedWindow::setChannel(ChannelPtr channel)
void AttachedWindow::showEvent(QShowEvent *) void AttachedWindow::showEvent(QShowEvent *)
{ {
this->attachToHwnd_(this->target_); this->attachToHwnd(this->target_);
} }
void AttachedWindow::attachToHwnd_(void *_attachedPtr) void AttachedWindow::attachToHwnd(void *_attachedPtr)
{ {
#ifdef USEWINSDK #ifdef USEWINSDK
if (this->attached_) { if (this->attached_) {
@ -145,13 +145,13 @@ void AttachedWindow::attachToHwnd_(void *_attachedPtr)
this->validProcessName_ = true; this->validProcessName_ = true;
} }
this->updateWindowRect_(attached); this->updateWindowRect(attached);
}); });
this->timer_.start(); this->timer_.start();
#endif #endif
} }
void AttachedWindow::updateWindowRect_(void *_attachedPtr) void AttachedWindow::updateWindowRect(void *_attachedPtr)
{ {
#ifdef USEWINSDK #ifdef USEWINSDK
auto hwnd = HWND(this->winId()); auto hwnd = HWND(this->winId());

View file

@ -47,8 +47,8 @@ private:
Split *split; Split *split;
} ui_; } ui_;
void attachToHwnd_(void *attached); void attachToHwnd(void *attached);
void updateWindowRect_(void *attached); void updateWindowRect(void *attached);
struct Item { struct Item {
void *hwnd; void *hwnd;

View file

@ -54,7 +54,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
return; return;
} }
ChannelPtr emoteChannel(new Channel("", Channel::None)); ChannelPtr emoteChannel(new Channel("", Channel::Type::None));
auto addEmotes = [&](EmoteMap &map, const QString &title, const QString &emoteDesc) { auto addEmotes = [&](EmoteMap &map, const QString &title, const QString &emoteDesc) {
// TITLE // TITLE
@ -134,7 +134,7 @@ void EmotePopup::loadEmojis()
{ {
auto &emojis = getApp()->emotes->emojis.emojis; auto &emojis = getApp()->emotes->emojis.emojis;
ChannelPtr emojiChannel(new Channel("", Channel::None)); ChannelPtr emojiChannel(new Channel("", Channel::Type::None));
// title // title
MessageBuilder builder1; MessageBuilder builder1;

View file

@ -39,7 +39,7 @@ void LogsPopup::setInfo(ChannelPtr channel, QString userName)
void LogsPopup::setMessages(std::vector<MessagePtr> &messages) void LogsPopup::setMessages(std::vector<MessagePtr> &messages)
{ {
ChannelPtr logsChannel(new Channel("logs", Channel::Misc)); ChannelPtr logsChannel(new Channel("logs", Channel::Type::Misc));
logsChannel->addMessagesAtStart(messages); logsChannel->addMessagesAtStart(messages);
this->channelView_->setChannel(logsChannel); this->channelView_->setChannel(logsChannel);
@ -68,7 +68,7 @@ void LogsPopup::getLogviewerLogs()
req.getJSON([this, channelName](QJsonObject &data) { req.getJSON([this, channelName](QJsonObject &data) {
std::vector<MessagePtr> messages; std::vector<MessagePtr> messages;
ChannelPtr logsChannel(new Channel("logs", Channel::None)); ChannelPtr logsChannel(new Channel("logs", Channel::Type::None));
QJsonValue before = data.value("before"); QJsonValue before = data.value("before");

View file

@ -10,7 +10,7 @@ namespace chatterino {
NotificationPopup::NotificationPopup() NotificationPopup::NotificationPopup()
: BaseWindow((QWidget *)nullptr, BaseWindow::Frameless) : BaseWindow((QWidget *)nullptr, BaseWindow::Frameless)
, channel(std::make_shared<Channel>("notifications", Channel::None)) , channel(std::make_shared<Channel>("notifications", Channel::Type::None))
{ {
this->channelView = new ChannelView(this); this->channelView = new ChannelView(this);

View file

@ -151,20 +151,20 @@ void SelectChannelDialog::setSelectedChannel(IndirectChannel _channel)
this->selectedChannel = channel; this->selectedChannel = channel;
switch (_channel.getType()) { switch (_channel.getType()) {
case Channel::Twitch: { case Channel::Type::Twitch: {
this->ui_.notebook->selectIndex(TAB_TWITCH); this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.channel->setFocus(); this->ui_.twitch.channel->setFocus();
this->ui_.twitch.channelName->setText(channel->name); this->ui_.twitch.channelName->setText(channel->name);
} break; } break;
case Channel::TwitchWatching: { case Channel::Type::TwitchWatching: {
this->ui_.notebook->selectIndex(TAB_TWITCH); this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.watching->setFocus(); this->ui_.twitch.watching->setFocus();
} break; } break;
case Channel::TwitchMentions: { case Channel::Type::TwitchMentions: {
this->ui_.notebook->selectIndex(TAB_TWITCH); this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.mentions->setFocus(); this->ui_.twitch.mentions->setFocus();
} break; } break;
case Channel::TwitchWhispers: { case Channel::Type::TwitchWhispers: {
this->ui_.notebook->selectIndex(TAB_TWITCH); this->ui_.notebook->selectIndex(TAB_TWITCH);
this->ui_.twitch.whispers->setFocus(); this->ui_.twitch.whispers->setFocus();
} break; } break;

View file

@ -71,7 +71,7 @@ void SearchPopup::performSearch()
{ {
QString text = searchInput->text(); QString text = searchInput->text();
ChannelPtr channel(new Channel("search", Channel::None)); ChannelPtr channel(new Channel("search", Channel::Type::None));
for (size_t i = 0; i < this->snapshot.getLength(); i++) { for (size_t i = 0; i < this->snapshot.getLength(); i++) {
MessagePtr message = this->snapshot[i]; MessagePtr message = this->snapshot[i];

View file

@ -272,7 +272,7 @@ void LookPage::addLastReadMessageIndicatorPatternSelector(LayoutCreator<QVBoxLay
ChannelPtr LookPage::createPreviewChannel() ChannelPtr LookPage::createPreviewChannel()
{ {
auto channel = ChannelPtr(new Channel("preview", Channel::Misc)); auto channel = ChannelPtr(new Channel("preview", Channel::Type::Misc));
{ {
auto message = MessagePtr(new Message()); auto message = MessagePtr(new Message());

View file

@ -311,7 +311,7 @@ void SplitHeader::updateChannelText()
QString title = channel->name; QString title = channel->name;
if (indirectChannel.getType() == Channel::TwitchWatching) { if (indirectChannel.getType() == Channel::Type::TwitchWatching) {
title = "watching: " + (title.isEmpty() ? "none" : title); title = "watching: " + (title.isEmpty() ? "none" : title);
} }