2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/accountpopup.hpp"
|
|
|
|
#include "channel.hpp"
|
2017-04-19 15:25:05 +02:00
|
|
|
#include "ui_accountpopupform.h"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
2017-07-31 22:14:54 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QPixmap>
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-11 11:36:42 +02:00
|
|
|
AccountPopupWidget::AccountPopupWidget(std::shared_ptr<Channel> &channel)
|
2017-04-12 17:46:44 +02:00
|
|
|
: QWidget(nullptr)
|
2017-04-19 15:24:19 +02:00
|
|
|
, _ui(new Ui::AccountPopup)
|
2017-05-27 17:45:40 +02:00
|
|
|
, _channel(channel)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
_ui->setupUi(this);
|
|
|
|
|
|
|
|
resize(0, 0);
|
|
|
|
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
|
|
|
|
// Close button
|
|
|
|
connect(_ui->btnClose, &QPushButton::clicked, [=]() {
|
|
|
|
hide(); //
|
|
|
|
});
|
2017-07-31 22:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-31 22:14:54 +02:00
|
|
|
QNetworkRequest req(nameUrl);
|
2017-08-12 12:09:26 +02:00
|
|
|
req.setRawHeader(QByteArray("Accept"), QByteArray("application/vnd.twitchtv.v5+json"));
|
|
|
|
req.setRawHeader(QByteArray("Client-ID"), QByteArray("7ue61iz46fz11y3cugd0l3tawb4taal"));
|
2017-05-27 17:45:40 +02:00
|
|
|
|
2017-07-31 22:14:54 +02:00
|
|
|
static auto manager = new QNetworkAccessManager();
|
|
|
|
auto *reply = manager->get(req);
|
|
|
|
|
2017-08-12 12:09:26 +02:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [=] {
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
2017-07-31 22:14:54 +02:00
|
|
|
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();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-31 22:14:54 +02:00
|
|
|
void AccountPopupWidget::getUserData()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-31 22:14:54 +02:00
|
|
|
QUrl idUrl("https://api.twitch.tv/kraken/channels/" + userID);
|
|
|
|
|
|
|
|
QNetworkRequest req(idUrl);
|
2017-08-12 12:09:26 +02:00
|
|
|
req.setRawHeader(QByteArray("Accept"), QByteArray("application/vnd.twitchtv.v5+json"));
|
|
|
|
req.setRawHeader(QByteArray("Client-ID"), QByteArray("7ue61iz46fz11y3cugd0l3tawb4taal"));
|
2017-07-31 22:14:54 +02:00
|
|
|
|
|
|
|
static auto manager = new QNetworkAccessManager();
|
|
|
|
auto *reply = manager->get(req);
|
|
|
|
|
2017-08-12 12:09:26 +02:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [=] {
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
2017-07-31 22:14:54 +02:00
|
|
|
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()));
|
2017-08-12 12:09:26 +02:00
|
|
|
_ui->lblAccountAge->setText(obj.value("created_at").toString().section("T", 0, 0));
|
2017-07-31 22:14:54 +02:00
|
|
|
|
|
|
|
loadAvatar(QUrl(obj.value("logo").toString()));
|
2017-08-12 12:09:26 +02:00
|
|
|
} else {
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblFollowers->setText("ERROR");
|
|
|
|
_ui->lblViews->setText("ERROR");
|
|
|
|
_ui->lblAccountAge->setText("ERROR");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccountPopupWidget::loadAvatar(const QUrl &avatarUrl)
|
|
|
|
{
|
2017-08-12 12:09:26 +02:00
|
|
|
if (!avatarMap.tryGet(userID, this->avatar)) {
|
|
|
|
if (!avatarUrl.isEmpty()) {
|
2017-07-31 22:14:54 +02:00
|
|
|
QNetworkRequest req(avatarUrl);
|
|
|
|
static auto manager = new QNetworkAccessManager();
|
|
|
|
auto *reply = manager->get(req);
|
|
|
|
|
2017-08-12 12:09:26 +02:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [=] {
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
2017-07-31 22:14:54 +02:00
|
|
|
const auto data = reply->readAll();
|
|
|
|
this->avatar.loadFromData(data);
|
2017-08-12 12:09:26 +02:00
|
|
|
this->avatarMap.insert(userID, avatar);
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblAvatar->setPixmap(avatar);
|
2017-08-12 12:09:26 +02:00
|
|
|
} else {
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblAvatar->setText("ERROR");
|
|
|
|
}
|
|
|
|
});
|
2017-08-12 12:09:26 +02:00
|
|
|
} else {
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblAvatar->setText("No Avatar");
|
|
|
|
}
|
2017-08-12 12:09:26 +02:00
|
|
|
} else {
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblAvatar->setPixmap(this->avatar);
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 22:14:54 +02:00
|
|
|
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...");
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|