mirror-chatterino2/src/common/Channel.hpp

135 lines
3.2 KiB
C++
Raw Normal View History

#pragma once
2018-06-26 17:20:03 +02:00
#include "common/CompletionModel.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/Image.hpp"
#include "messages/LimitedQueue.hpp"
#include "messages/Message.hpp"
#include "util/ConcurrentMap.hpp"
2017-01-07 20:43:55 +01:00
2017-01-11 18:52:09 +01:00
#include <QString>
2018-03-30 13:50:43 +02:00
#include <QTimer>
#include <pajlada/signals/signal.hpp>
2017-01-11 01:08:20 +01:00
#include <memory>
2017-01-05 16:07:20 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2018-01-28 03:29:42 +01:00
struct Message;
class Channel : public std::enable_shared_from_this<Channel>
{
public:
2018-07-06 17:30:12 +02:00
enum class Type {
2018-04-18 09:12:29 +02:00
None,
2018-04-20 19:54:45 +02:00
Direct,
2018-04-18 09:12:29 +02:00
Twitch,
TwitchWhispers,
TwitchWatching,
TwitchMentions,
2018-05-25 13:53:55 +02:00
TwitchEnd,
Misc
2018-04-18 09:12:29 +02:00
};
explicit Channel(const QString &name, Type type);
2018-02-05 15:11:50 +01:00
virtual ~Channel();
2018-06-06 18:57:22 +02:00
pajlada::Signals::Signal<const QString &, const QString &, bool &> sendMessageSignal;
2017-01-07 20:43:55 +01:00
pajlada::Signals::Signal<MessagePtr &> messageRemovedFromStart;
pajlada::Signals::Signal<MessagePtr &> messageAppended;
pajlada::Signals::Signal<std::vector<MessagePtr> &> messagesAddedAtStart;
pajlada::Signals::Signal<size_t, MessagePtr &> messageReplaced;
2018-02-05 15:11:50 +01:00
pajlada::Signals::NoArgSignal destroyed;
2017-02-02 20:35:12 +01:00
2018-04-18 09:12:29 +02:00
Type getType() const;
2018-05-25 13:53:55 +02:00
bool isTwitchChannel() const;
virtual bool isEmpty() const;
LimitedQueueSnapshot<MessagePtr> getMessageSnapshot();
2017-01-05 20:49:33 +01:00
void addMessage(MessagePtr message);
2018-07-06 17:30:12 +02:00
void addMessagesAtStart(std::vector<MessagePtr> &messages_);
void addOrReplaceTimeout(MessagePtr message);
void disableAllMessages();
void replaceMessage(MessagePtr message, MessagePtr replacement);
virtual void addRecentChatter(const std::shared_ptr<Message> &message);
QString name;
QStringList modList;
virtual bool canSendMessage() const;
virtual void sendMessage(const QString &message);
2018-04-18 09:12:29 +02:00
virtual bool isMod() const;
virtual bool isBroadcaster() const;
virtual bool hasModRights() const;
2018-02-05 15:11:50 +01:00
static std::shared_ptr<Channel> getEmpty();
CompletionModel completionModel;
2018-07-14 14:24:18 +02:00
// pre c++17 polyfill
std::weak_ptr<Channel> weak_from_this();
2018-02-05 15:11:50 +01:00
protected:
virtual void onConnected();
private:
2018-07-06 17:30:12 +02:00
LimitedQueue<MessagePtr> messages_;
Type type_;
QTimer clearCompletionModelTimer_;
};
2018-03-30 12:37:00 +02:00
using ChannelPtr = std::shared_ptr<Channel>;
2018-04-20 19:54:45 +02:00
class IndirectChannel
{
struct Data {
ChannelPtr channel;
Channel::Type type;
pajlada::Signals::NoArgSignal changed;
Data() = delete;
Data(ChannelPtr _channel, Channel::Type _type)
: channel(_channel)
, type(_type)
{
}
};
public:
2018-07-06 17:30:12 +02:00
IndirectChannel(ChannelPtr channel, Channel::Type type = Channel::Type::Direct)
: data_(new Data(channel, type))
2018-04-20 19:54:45 +02:00
{
}
ChannelPtr get()
{
2018-07-06 17:30:12 +02:00
return data_->channel;
2018-04-20 19:54:45 +02:00
}
void update(ChannelPtr ptr)
{
2018-07-06 17:30:12 +02:00
assert(this->data_->type != Channel::Type::Direct);
2018-04-20 19:54:45 +02:00
2018-07-06 17:30:12 +02:00
this->data_->channel = ptr;
this->data_->changed.invoke();
2018-04-20 19:54:45 +02:00
}
pajlada::Signals::NoArgSignal &getChannelChanged()
{
2018-07-06 17:30:12 +02:00
return this->data_->changed;
2018-04-20 19:54:45 +02:00
}
Channel::Type getType()
{
2018-07-06 17:30:12 +02:00
if (this->data_->type == Channel::Type::Direct) {
2018-04-20 19:54:45 +02:00
return this->get()->getType();
} else {
2018-07-06 17:30:12 +02:00
return this->data_->type;
2018-04-20 19:54:45 +02:00
}
}
2018-07-06 17:30:12 +02:00
private:
std::shared_ptr<Data> data_;
2018-04-20 19:54:45 +02:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino