Implemented the accountpopup. Also fixed regex again. (#89)

This commit is contained in:
Cranken 2017-07-31 22:14:54 +02:00 committed by pajlada
parent a975e15573
commit 5feaf4c8eb
5 changed files with 156 additions and 33 deletions

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>131</height>
<width>317</width>
<height>145</height>
</rect>
</property>
<property name="sizePolicy">
@ -19,60 +19,72 @@
<property name="windowTitle">
<string>AccountPopup</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0">
<widget class="QPushButton" name="btnPurge">
<property name="text">
<string>Purge</string>
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
</item>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1" rowspan="2" colspan="2">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Views</string>
<string>Views:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="lblViews">
<property name="text">
<string>420</string>
<string>Loading...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Followers</string>
<string>Followers:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="lblFollowers">
<property name="text">
<string>69</string>
<string>Loading...</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Account Age</string>
<string>Created at:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="lblAccountAge">
<property name="text">
<string>6 years</string>
<string>Loading...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0" rowspan="3">
<widget class="QLabel" name="lblAvatar">
<property name="maximumSize">
<size>
<width>100</width>
<height>100</height>
</size>
</property>
<property name="text">
<string>Loading...</string>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
@ -91,7 +103,7 @@
</font>
</property>
<property name="text">
<string>USERNAME</string>
<string>Loading...</string>
</property>
</widget>
</item>
@ -110,13 +122,6 @@
</item>
</layout>
</item>
<item row="0" column="0" rowspan="3">
<widget class="QLabel" name="lblAvatar">
<property name="text">
<string>AVATAR</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View file

@ -10,7 +10,7 @@ MessageBuilder::MessageBuilder()
: _words()
{
_parseTime = std::chrono::system_clock::now();
regex.setPattern("[[:ascii:]]*\\.[A-Z]+");
regex.setPattern("[[:ascii:]]*\\.[A-Z]+\/[[:ascii:]]+");
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
}

View file

@ -3,6 +3,12 @@
#include "ui_accountpopupform.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPixmap>
namespace chatterino {
namespace widgets {
@ -22,20 +28,119 @@ AccountPopupWidget::AccountPopupWidget(std::shared_ptr<Channel> &channel)
connect(_ui->btnClose, &QPushButton::clicked, [=]() {
hide(); //
});
connect(_ui->btnPurge, &QPushButton::clicked, [=]() {
qDebug() << "xD: " << _channel->name;
printf("Channel pointer in dialog: %p\n", _channel.get());
//_channel->sendMessage(QString(".timeout %1 0").arg(_ui->lblUsername->text()));
_channel->sendMessage("xD");
});
}
void AccountPopupWidget::setName(const QString &name)
{
_ui->lblUsername->setText(name);
getUserId();
}
void AccountPopupWidget::getUserId()
{
QUrl nameUrl("https://api.twitch.tv/kraken/users?login=" + _ui->lblUsername->text());
QNetworkRequest req(nameUrl);
req.setRawHeader(QByteArray("Accept"),QByteArray("application/vnd.twitchtv.v5+json"));
req.setRawHeader(QByteArray("Client-ID"),QByteArray("7ue61iz46fz11y3cugd0l3tawb4taal"));
static auto manager = new QNetworkAccessManager();
auto *reply = manager->get(req);
QObject::connect(reply,&QNetworkReply::finished,this,[=]
{
if(reply->error() == QNetworkReply::NoError){
auto doc = QJsonDocument::fromJson(reply->readAll());
auto obj = doc.object();
auto array = obj.value("users").toArray();
userID = array.at(0).toObject().value("_id").toString();
getUserData();
}
});
}
void AccountPopupWidget::getUserData()
{
QUrl idUrl("https://api.twitch.tv/kraken/channels/" + userID);
QNetworkRequest req(idUrl);
req.setRawHeader(QByteArray("Accept"),QByteArray("application/vnd.twitchtv.v5+json"));
req.setRawHeader(QByteArray("Client-ID"),QByteArray("7ue61iz46fz11y3cugd0l3tawb4taal"));
static auto manager = new QNetworkAccessManager();
auto *reply = manager->get(req);
QObject::connect(reply,&QNetworkReply::finished,this,[=]
{
if(reply->error() == QNetworkReply::NoError){
auto doc = QJsonDocument::fromJson(reply->readAll());
auto obj = doc.object();
_ui->lblFollowers->setText(QString::number(obj.value("followers").toInt()));
_ui->lblViews->setText(QString::number(obj.value("views").toInt()));
_ui->lblAccountAge->setText(obj.value("created_at").toString().section("T",0,0));
loadAvatar(QUrl(obj.value("logo").toString()));
}
else
{
_ui->lblFollowers->setText("ERROR");
_ui->lblViews->setText("ERROR");
_ui->lblAccountAge->setText("ERROR");
}
});
}
void AccountPopupWidget::loadAvatar(const QUrl &avatarUrl)
{
if(!avatarMap.tryGet(userID,this->avatar))
{
if(!avatarUrl.isEmpty())
{
QNetworkRequest req(avatarUrl);
static auto manager = new QNetworkAccessManager();
auto *reply = manager->get(req);
QObject::connect(reply,&QNetworkReply::finished,this,[=]
{
if(reply->error() == QNetworkReply::NoError)
{
const auto data = reply->readAll();
this->avatar.loadFromData(data);
this->avatarMap.insert(userID,avatar);
_ui->lblAvatar->setPixmap(avatar);
}
else
{
_ui->lblAvatar->setText("ERROR");
}
});
}
else
{
_ui->lblAvatar->setText("No Avatar");
}
}
else
{
_ui->lblAvatar->setPixmap(this->avatar);
}
}
void AccountPopupWidget::focusOutEvent(QFocusEvent *event)
{
hide();
_ui->lblFollowers->setText("Loading...");
_ui->lblViews->setText("Loading...");
_ui->lblAccountAge->setText("Loading...");
_ui->lblUsername->setText("Loading...");
_ui->lblAvatar->setText("Loading...");
}
} // namespace widgets
} // namespace chatterino

View file

@ -1,4 +1,5 @@
#pragma once
#include "concurrentmap.hpp"
#include <QWidget>
@ -25,7 +26,19 @@ public:
private:
Ui::AccountPopup *_ui;
void getUserId();
void getUserData();
void loadAvatar(const QUrl &avatarUrl);
std::shared_ptr<Channel> &_channel;
QString userID;
QPixmap avatar;
ConcurrentMap<QString,QPixmap> avatarMap;
protected:
virtual void focusOutEvent(QFocusEvent *event) override;
};
} // namespace widgets

View file

@ -383,7 +383,7 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
switch (link.getType()) {
case messages::Link::UserInfo: {
auto user = message->getMessage()->getUserName();
auto user = link.getValue();
this->userPopupWidget.setName(user);
this->userPopupWidget.move(event->screenPos().toPoint());
this->userPopupWidget.show();