2017-06-11 09:31:45 +02:00
|
|
|
#include "widgets/accountpopup.hpp"
|
2017-09-23 19:23:10 +02:00
|
|
|
#include "accountmanager.hpp"
|
2017-06-11 09:31:45 +02:00
|
|
|
#include "channel.hpp"
|
2017-08-19 15:29:36 +02:00
|
|
|
#include "credentials.hpp"
|
2017-09-23 19:23:10 +02:00
|
|
|
#include "settingsmanager.hpp"
|
2017-04-19 15:25:05 +02:00
|
|
|
#include "ui_accountpopupform.h"
|
2017-12-16 02:22:35 +01:00
|
|
|
#include "util/urlfetch.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
#include <QClipboard>
|
|
|
|
#include <QDesktopServices>
|
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-09-12 22:10:30 +02:00
|
|
|
AccountPopupWidget::AccountPopupWidget(std::shared_ptr<Channel> channel)
|
2017-12-17 16:45:15 +01:00
|
|
|
: BaseWidget()
|
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);
|
|
|
|
|
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
2017-12-17 16:45:15 +01:00
|
|
|
this->initAsWindow();
|
|
|
|
|
|
|
|
resize(0, 0);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
SettingsManager &settings = SettingsManager::getInstance();
|
|
|
|
|
|
|
|
permission = permissions::User;
|
2017-12-16 02:22:35 +01:00
|
|
|
for (auto button : this->_ui->profileLayout->findChildren<QPushButton *>()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
button->setFocusProxy(this);
|
|
|
|
}
|
2017-12-16 02:22:35 +01:00
|
|
|
for (auto button : this->_ui->userLayout->findChildren<QPushButton *>()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
button->setFocusProxy(this);
|
|
|
|
}
|
2017-12-16 02:22:35 +01:00
|
|
|
for (auto button : this->_ui->modLayout->findChildren<QPushButton *>()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
button->setFocusProxy(this);
|
|
|
|
}
|
2017-12-16 02:22:35 +01:00
|
|
|
for (auto button : this->_ui->ownerLayout->findChildren<QPushButton *>()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
button->setFocusProxy(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout(this->_ui->purge, 1);
|
|
|
|
timeout(this->_ui->min1, 60);
|
|
|
|
timeout(this->_ui->min10, 600);
|
|
|
|
timeout(this->_ui->hour1, 3600);
|
|
|
|
timeout(this->_ui->hour24, 86400);
|
|
|
|
|
|
|
|
sendCommand(this->_ui->ban, "/ban ");
|
|
|
|
sendCommand(this->_ui->unBan, "/unban ");
|
|
|
|
sendCommand(this->_ui->mod, "/mod ");
|
|
|
|
sendCommand(this->_ui->unMod, "/unmod ");
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
auto &accountManager = AccountManager::getInstance();
|
|
|
|
QString userId;
|
|
|
|
QString userNickname;
|
|
|
|
auto currentTwitchUser = accountManager.Twitch.getCurrent();
|
|
|
|
if (currentTwitchUser) {
|
|
|
|
userId = currentTwitchUser->getUserId();
|
|
|
|
userNickname = currentTwitchUser->getNickName();
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->profile, &QPushButton::clicked, this, [=]() {
|
|
|
|
QDesktopServices::openUrl(QUrl("https://twitch.tv/" + this->_ui->lblUsername->text()));
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->sendMessage, &QPushButton::clicked, this, [=]() {
|
|
|
|
QDesktopServices::openUrl(
|
|
|
|
QUrl("https://www.twitch.tv/message/compose?to=" + this->_ui->lblUsername->text()));
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->copy, &QPushButton::clicked, this,
|
|
|
|
[=]() { QApplication::clipboard()->setText(this->_ui->lblUsername->text()); });
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->follow, &QPushButton::clicked, this, [=]() {
|
|
|
|
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + userId + "/follows/channels/" +
|
|
|
|
this->userID);
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
util::twitch::put(requestUrl,
|
|
|
|
[](QJsonObject obj) { qDebug() << "follows channel: " << obj; });
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->ignore, &QPushButton::clicked, this, [=]() {
|
|
|
|
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + userId + "/blocks/" + this->userID);
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
util::twitch::put(requestUrl, [](QJsonObject obj) { qDebug() << "blocks user: " << obj; });
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->disableHighlights, &QPushButton::clicked, this, [=, &settings]() {
|
2017-09-23 19:23:10 +02:00
|
|
|
QString str = settings.highlightUserBlacklist.getnonConst();
|
|
|
|
str.append(this->_ui->lblUsername->text() + "\n");
|
|
|
|
settings.highlightUserBlacklist.set(str);
|
|
|
|
this->_ui->disableHighlights->hide();
|
|
|
|
this->_ui->enableHighlights->show();
|
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(this->_ui->enableHighlights, &QPushButton::clicked, this, [=, &settings]() {
|
2017-09-23 19:23:10 +02:00
|
|
|
QString str = settings.highlightUserBlacklist.getnonConst();
|
|
|
|
QStringList list = str.split("\n");
|
|
|
|
list.removeAll(this->_ui->lblUsername->text());
|
|
|
|
settings.highlightUserBlacklist.set(list.join("\n"));
|
|
|
|
this->_ui->enableHighlights->hide();
|
|
|
|
this->_ui->disableHighlights->show();
|
|
|
|
});
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
updateButtons(this->_ui->userLayout, false);
|
|
|
|
updateButtons(this->_ui->modLayout, false);
|
|
|
|
updateButtons(this->_ui->ownerLayout, false);
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// Close button
|
|
|
|
connect(_ui->btnClose, &QPushButton::clicked, [=]() {
|
|
|
|
hide(); //
|
|
|
|
});
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
util::twitch::getUserID(userNickname, this,
|
2017-12-16 02:22:35 +01:00
|
|
|
[=](const QString &id) { currentTwitchUser->setUserId(id); });
|
2017-07-31 22:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccountPopupWidget::setName(const QString &name)
|
|
|
|
{
|
|
|
|
_ui->lblUsername->setText(name);
|
|
|
|
getUserId();
|
|
|
|
}
|
|
|
|
|
2017-09-12 22:10:30 +02:00
|
|
|
void AccountPopupWidget::setChannel(std::shared_ptr<Channel> channel)
|
|
|
|
{
|
|
|
|
this->_channel = channel;
|
|
|
|
}
|
|
|
|
|
2017-07-31 22:14:54 +02:00
|
|
|
void AccountPopupWidget::getUserId()
|
|
|
|
{
|
2017-12-16 02:22:35 +01:00
|
|
|
util::twitch::getUserID(this->_ui->lblUsername->text(), this, [=](const QString &id) {
|
2017-09-23 19:23:10 +02:00
|
|
|
userID = id;
|
|
|
|
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-12-16 02:22:35 +01:00
|
|
|
util::twitch::get(
|
|
|
|
"https://api.twitch.tv/kraken/channels/" + userID, this, [=](const QJsonObject &obj) {
|
|
|
|
_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));
|
2017-07-31 22:14:54 +02:00
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
loadAvatar(QUrl(obj.value("logo").toString()));
|
|
|
|
});
|
2017-07-31 22:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-23 19:23:10 +02:00
|
|
|
void AccountPopupWidget::updatePermissions()
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
AccountManager &accountManager = AccountManager::getInstance();
|
|
|
|
auto currentTwitchUser = accountManager.Twitch.getCurrent();
|
|
|
|
if (!currentTwitchUser) {
|
|
|
|
// No twitch user set (should never happen)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
if (this->_channel.get()->name == currentTwitchUser->getNickName()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
permission = permissions::Owner;
|
2017-12-16 02:22:35 +01:00
|
|
|
} else if (this->_channel->modList.contains(currentTwitchUser->getNickName())) {
|
2017-12-16 02:21:06 +01:00
|
|
|
// XXX(pajlada): This might always trigger if user is anonymous (if nickName is empty?)
|
2017-09-23 19:23:10 +02:00
|
|
|
permission = permissions::Mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
void AccountPopupWidget::updateButtons(QWidget *layout, bool state)
|
2017-09-23 19:23:10 +02:00
|
|
|
{
|
2017-12-16 02:22:35 +01:00
|
|
|
for (auto button : layout->findChildren<QPushButton *>()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
button->setVisible(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccountPopupWidget::timeout(QPushButton *button, int time)
|
|
|
|
{
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(button, &QPushButton::clicked, this, [=]() {
|
|
|
|
this->_channel->sendMessage("/timeout " + this->_ui->lblUsername->text() + " " +
|
|
|
|
QString::number(time));
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccountPopupWidget::sendCommand(QPushButton *button, QString command)
|
2017-07-31 22:14:54 +02:00
|
|
|
{
|
2017-12-16 02:22:35 +01:00
|
|
|
QObject::connect(button, &QPushButton::clicked, this, [=]() {
|
|
|
|
this->_channel->sendMessage(command + this->_ui->lblUsername->text());
|
2017-09-23 19:23:10 +02:00
|
|
|
});
|
|
|
|
}
|
2017-07-31 22:14:54 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
void AccountPopupWidget::focusOutEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
this->hide();
|
2017-07-31 22:14:54 +02:00
|
|
|
_ui->lblFollowers->setText("Loading...");
|
|
|
|
_ui->lblViews->setText("Loading...");
|
|
|
|
_ui->lblAccountAge->setText("Loading...");
|
|
|
|
_ui->lblUsername->setText("Loading...");
|
|
|
|
_ui->lblAvatar->setText("Loading...");
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
void AccountPopupWidget::showEvent(QShowEvent *event)
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
AccountManager &accountManager = AccountManager::getInstance();
|
|
|
|
auto currentTwitchUser = accountManager.Twitch.getCurrent();
|
|
|
|
if (!currentTwitchUser) {
|
|
|
|
// No twitch user set (should never happen)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:22:35 +01:00
|
|
|
if (this->_ui->lblUsername->text() != currentTwitchUser->getNickName()) {
|
2017-09-23 19:23:10 +02:00
|
|
|
updateButtons(this->_ui->userLayout, true);
|
2017-12-16 02:22:35 +01:00
|
|
|
if (permission != permissions::User) {
|
|
|
|
if (!this->_channel->modList.contains(this->_ui->lblUsername->text())) {
|
2017-09-23 19:23:10 +02:00
|
|
|
updateButtons(this->_ui->modLayout, true);
|
|
|
|
}
|
2017-12-16 02:22:35 +01:00
|
|
|
if (permission == permissions::Owner) {
|
2017-09-23 19:23:10 +02:00
|
|
|
updateButtons(this->_ui->ownerLayout, true);
|
|
|
|
updateButtons(this->_ui->modLayout, true);
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 02:22:35 +01:00
|
|
|
} else {
|
2017-09-23 19:23:10 +02:00
|
|
|
updateButtons(this->_ui->modLayout, false);
|
|
|
|
updateButtons(this->_ui->userLayout, false);
|
|
|
|
updateButtons(this->_ui->ownerLayout, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString blacklisted = SettingsManager::getInstance().highlightUserBlacklist.getnonConst();
|
2017-12-16 02:22:35 +01:00
|
|
|
QStringList list = blacklisted.split("\n", QString::SkipEmptyParts);
|
|
|
|
if (list.contains(this->_ui->lblUsername->text(), Qt::CaseInsensitive)) {
|
2017-09-23 19:23:10 +02:00
|
|
|
this->_ui->disableHighlights->hide();
|
|
|
|
this->_ui->enableHighlights->show();
|
2017-12-16 02:22:35 +01:00
|
|
|
} else {
|
2017-09-23 19:23:10 +02:00
|
|
|
this->_ui->disableHighlights->show();
|
|
|
|
this->_ui->enableHighlights->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|