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;
}
bool
isEmpty() const
{
return name.isEmpty();
}
const QMutex &
getMessageMutex() const
{

View file

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

View file

@ -8,20 +8,28 @@ namespace chatterino {
class Channels
{
public:
static Channel *
static std::shared_ptr<Channel>
getWhispers()
{
return &whispers;
return whispers;
}
static Channel *
static std::shared_ptr<Channel>
getMentions()
{
return &mentions;
return mentions;
}
static Channel *addChannel(const QString &channel);
static Channel *getChannel(const QString &channel);
static std::shared_ptr<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);
private:
@ -29,11 +37,12 @@ private:
{
}
static Channel whispers;
static Channel mentions;
static Channel empty;
static std::shared_ptr<Channel> whispers;
static std::shared_ptr<Channel> mentions;
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

View file

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

View file

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

View file

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

View file

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

View file

@ -52,7 +52,9 @@ ChatWidgetInput::ChatWidgetInput(ChatWidget *widget)
this->edit.keyPressed.connect([this](QKeyEvent *event) {
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) {
IrcManager::send("PRIVMSG #" + c->getName() + ": " +
this->edit.toPlainText());

View file

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