made channels shared_pointers

This commit is contained in:
fourtf 2017-01-30 19:14:25 +01:00
parent 23c2bf03d6
commit 1d1c98ecdb
9 changed files with 90 additions and 42 deletions

View file

@ -33,6 +33,12 @@ public:
return ffzChannelEmotes; return ffzChannelEmotes;
} }
bool
isEmpty() const
{
return name.isEmpty();
}
const QMutex & const QMutex &
getMessageMutex() const getMessageMutex() const
{ {

View file

@ -3,54 +3,75 @@
namespace chatterino { namespace chatterino {
Channel Channels::whispers(QString("/whispers")); std::shared_ptr<Channel> Channels::whispers(new Channel(QString("/whispers")));
Channel Channels::mentions(QString("/mentions")); std::shared_ptr<Channel> Channels::mentions(new Channel(QString("/mentions")));
Channel Channels::empty(QString("")); std::shared_ptr<Channel> Channels::empty(new Channel(QString("")));
QMap<QString, std::tuple<Channel *, int>> Channels::channels; QMap<QString, std::tuple<std::shared_ptr<Channel>, int>> Channels::channels;
QMutex Channels::channelsMutex;
Channel * const std::vector<std::shared_ptr<Channel>>
Channels::addChannel(const QString &channel) Channels::getItems()
{ {
QString c = channel.toLower(); QMutexLocker locker(&Channels::channelsMutex);
auto a = Channels::channels.find(c); int size = Channels::channels.size();
if (a == Channels::channels.end()) { std::vector<std::shared_ptr<Channel>> items(size);
auto _c = new Channel(c);
Channels::channels.insert(c, std::tuple<Channel *, int>(_c, 1));
IrcManager::joinChannel(c); for (auto &item : Channels::channels.values()) {
items.push_back(std::get<0>(item));
return _c;
} else {
std::get<1>(a.value())++;
} }
return std::get<0>(a.value()); return items;
} }
Channel * std::shared_ptr<Channel>
Channels::addChannel(const QString &channel)
{
QMutexLocker locker(&Channels::channelsMutex);
QString channelName = channel.toLower();
auto it = Channels::channels.find(channelName);
if (it == Channels::channels.end()) {
auto channel = std::shared_ptr<Channel>(new Channel(channelName));
Channels::channels.insert(channelName, std::make_tuple(channel, 1));
IrcManager::sendJoin(channelName);
return channel;
}
std::get<1>(it.value())++;
return std::get<0>(it.value());
}
std::shared_ptr<Channel>
Channels::getChannel(const QString &channel) Channels::getChannel(const QString &channel)
{ {
QMutexLocker locker(&Channels::channelsMutex);
QString c = channel.toLower(); QString c = channel.toLower();
if (channel.length() > 1 && channel.at(0) == '/') { if (channel.length() > 1 && channel.at(0) == '/') {
if (c == "/whispers") { if (c == "/whispers") {
return &Channels::whispers; return Channels::whispers;
} }
if (c == "/mentions") { if (c == "/mentions") {
return &Channels::mentions; return Channels::mentions;
} }
return &Channels::empty; return Channels::empty;
} }
auto a = Channels::channels.find(c); auto a = Channels::channels.find(c);
if (a == Channels::channels.end()) { if (a == Channels::channels.end()) {
return NULL; return Channels::empty;
} }
return std::get<0>(a.value()); return std::get<0>(a.value());
@ -59,6 +80,8 @@ Channels::getChannel(const QString &channel)
void void
Channels::removeChannel(const QString &channel) Channels::removeChannel(const QString &channel)
{ {
QMutexLocker locker(&Channels::channelsMutex);
if (channel.length() > 1 && channel.at(0) == '/') { if (channel.length() > 1 && channel.at(0) == '/') {
return; return;
} }
@ -76,7 +99,6 @@ Channels::removeChannel(const QString &channel)
if (std::get<1>(a.value()) == 0) { if (std::get<1>(a.value()) == 0) {
IrcManager::partChannel(c); IrcManager::partChannel(c);
Channels::channels.remove(c); Channels::channels.remove(c);
delete std::get<0>(a.value());
} }
} }
} }

View file

@ -8,20 +8,28 @@ namespace chatterino {
class Channels class Channels
{ {
public: public:
static Channel * static std::shared_ptr<Channel>
getWhispers() getWhispers()
{ {
return &whispers; return whispers;
} }
static Channel * static std::shared_ptr<Channel>
getMentions() getMentions()
{ {
return &mentions; return mentions;
} }
static Channel *addChannel(const QString &channel); static std::shared_ptr<Channel>
static Channel *getChannel(const QString &channel); getEmpty()
{
return empty;
}
const static std::vector<std::shared_ptr<Channel>> getItems();
static std::shared_ptr<Channel> addChannel(const QString &channel);
static std::shared_ptr<Channel> getChannel(const QString &channel);
static void removeChannel(const QString &channel); static void removeChannel(const QString &channel);
private: private:
@ -29,11 +37,12 @@ private:
{ {
} }
static Channel whispers; static std::shared_ptr<Channel> whispers;
static Channel mentions; static std::shared_ptr<Channel> mentions;
static Channel empty; static std::shared_ptr<Channel> empty;
static QMap<QString, std::tuple<Channel *, int>> channels; static QMap<QString, std::tuple<std::shared_ptr<Channel>, int>> channels;
static QMutex channelsMutex;
}; };
} }
#endif // CHANNELS_H #endif // CHANNELS_H

View file

@ -60,8 +60,9 @@ IrcManager::beginConnecting()
username + "/blocks?limit=" + 100 + username + "/blocks?limit=" + 100 +
"&client_id=" + oauthClient; "&client_id=" + oauthClient;
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken)); QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
QNetworkReply *reply = IrcManager::accessManager.get(req); QNetworkReply *reply = manager->get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] { QObject::connect(reply, &QNetworkReply::finished, [=] {
IrcManager::twitchBlockedUsersMutex.lock(); IrcManager::twitchBlockedUsersMutex.lock();
@ -86,6 +87,8 @@ IrcManager::beginConnecting()
user.value("name").toString().toLower(), true); user.value("name").toString().toLower(), true);
} }
IrcManager::twitchBlockedUsersMutex.unlock(); IrcManager::twitchBlockedUsersMutex.unlock();
manager->deleteLater();
}); });
} }
@ -136,6 +139,12 @@ IrcManager::beginConnecting()
delete IrcManager::connection; delete IrcManager::connection;
c->moveToThread(QCoreApplication::instance()->thread()); c->moveToThread(QCoreApplication::instance()->thread());
IrcManager::connection = c; IrcManager::connection = c;
auto channels = Channels::getItems();
for (auto &channel : channels) {
IrcManager::sendJoin(channel.get()->getName());
}
} else { } else {
delete c; delete c;
} }
@ -164,7 +173,7 @@ IrcManager::send(QString raw)
} }
void void
IrcManager::joinChannel(const QString &channel) IrcManager::sendJoin(const QString &channel)
{ {
IrcManager::connectionMutex.lock(); IrcManager::connectionMutex.lock();
if (IrcManager::connection != NULL) { if (IrcManager::connection != NULL) {

View file

@ -38,7 +38,7 @@ public:
return accessManager; return accessManager;
} }
static void joinChannel(const QString &channel); static void sendJoin(const QString &channel);
static void partChannel(const QString &channel); static void partChannel(const QString &channel);

View file

@ -14,7 +14,7 @@ namespace widgets {
ChatWidget::ChatWidget(QWidget *parent) ChatWidget::ChatWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, channel(NULL) , channel(Channels::getEmpty())
, channelName(QString()) , channelName(QString())
, vbox(this) , vbox(this)
, header(this) , header(this)

View file

@ -29,7 +29,7 @@ public:
return view; return view;
} }
Channel * std::shared_ptr<Channel>
getChannel() const getChannel() const
{ {
return channel; return channel;
@ -49,7 +49,7 @@ protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
private: private:
Channel *channel; std::shared_ptr<Channel> channel;
QString channelName; QString channelName;
QFont font; QFont font;

View file

@ -52,7 +52,9 @@ ChatWidgetInput::ChatWidgetInput(ChatWidget *widget)
this->edit.keyPressed.connect([this](QKeyEvent *event) { this->edit.keyPressed.connect([this](QKeyEvent *event) {
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
Channel *c = this->chatWidget->getChannel(); auto ptr = this->chatWidget->getChannel();
Channel *c = ptr.get();
if (c != nullptr) { if (c != nullptr) {
IrcManager::send("PRIVMSG #" + c->getName() + ": " + IrcManager::send("PRIVMSG #" + c->getName() + ": " +
this->edit.toPlainText()); this->edit.toPlainText());

View file

@ -42,7 +42,7 @@ MainWindow::layoutVisibleChatWidgets(Channel *channel)
for (auto it = widgets.begin(); it != widgets.end(); ++it) { for (auto it = widgets.begin(); it != widgets.end(); ++it) {
ChatWidget *widget = *it; ChatWidget *widget = *it;
if (channel == NULL || channel == widget->getChannel()) { if (channel == NULL || channel == widget->getChannel().get()) {
if (widget->getView().layoutMessages()) { if (widget->getView().layoutMessages()) {
widget->update(); widget->update();
} }
@ -64,7 +64,7 @@ MainWindow::repaintVisibleChatWidgets(Channel *channel)
for (auto it = widgets.begin(); it != widgets.end(); ++it) { for (auto it = widgets.begin(); it != widgets.end(); ++it) {
ChatWidget *widget = *it; ChatWidget *widget = *it;
if (channel == NULL || channel == widget->getChannel()) { if (channel == NULL || channel == widget->getChannel().get()) {
widget->getView().layoutMessages(); widget->getView().layoutMessages();
widget->update(); widget->update();
} }