Add imgur auth. upload and uploader settings
This commit is contained in:
parent
4ba500e378
commit
6ab5452fb6
@ -63,7 +63,8 @@ SOURCES += main.cpp\
|
|||||||
cropeditor/drawing/rectitem.cpp \
|
cropeditor/drawing/rectitem.cpp \
|
||||||
cropeditor/drawing/ellipseitem.cpp \
|
cropeditor/drawing/ellipseitem.cpp \
|
||||||
hotkeyinputdialog.cpp \
|
hotkeyinputdialog.cpp \
|
||||||
cropeditor/drawing/arrowitem.cpp
|
cropeditor/drawing/arrowitem.cpp \
|
||||||
|
uploaders/default/imgursettingsdialog.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.hpp \
|
HEADERS += mainwindow.hpp \
|
||||||
cropeditor/cropeditor.hpp \
|
cropeditor/cropeditor.hpp \
|
||||||
@ -105,7 +106,8 @@ HEADERS += mainwindow.hpp \
|
|||||||
cropeditor/drawing/rectitem.hpp \
|
cropeditor/drawing/rectitem.hpp \
|
||||||
cropeditor/drawing/ellipseitem.hpp \
|
cropeditor/drawing/ellipseitem.hpp \
|
||||||
hotkeyinputdialog.hpp \
|
hotkeyinputdialog.hpp \
|
||||||
cropeditor/drawing/arrowitem.hpp
|
cropeditor/drawing/arrowitem.hpp \
|
||||||
|
uploaders/default/imgursettingsdialog.hpp
|
||||||
|
|
||||||
LIBS += -lavcodec -lavformat -lavutil -lswscale -lavutil
|
LIBS += -lavcodec -lavformat -lavutil -lswscale -lavutil
|
||||||
|
|
||||||
@ -133,7 +135,8 @@ FORMS += mainwindow.ui \
|
|||||||
recording/encoders/encodersettingsdialog.ui \
|
recording/encoders/encodersettingsdialog.ui \
|
||||||
settingsdialog.ui \
|
settingsdialog.ui \
|
||||||
aboutbox.ui \
|
aboutbox.ui \
|
||||||
hotkeyinputdialog.ui
|
hotkeyinputdialog.ui \
|
||||||
|
uploaders/default/imgursettingsdialog.ui
|
||||||
|
|
||||||
DISTFILES += \
|
DISTFILES += \
|
||||||
README.md \
|
README.md \
|
||||||
|
@ -53,7 +53,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) {
|
|||||||
delete enc;
|
delete enc;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (std::runtime_error e) {
|
} catch (std::runtime_error &e) {
|
||||||
// notifications::notify("KShare Video Encoder Error", e.what(),
|
// notifications::notify("KShare Video Encoder Error", e.what(),
|
||||||
// QSystemTrayIcon::Critical);
|
// QSystemTrayIcon::Critical);
|
||||||
qCritical() << "Encoder error: " << e.what();
|
qCritical() << "Encoder error: " << e.what();
|
||||||
@ -68,7 +68,7 @@ RecordingFormats::RecordingFormats(formats::Recording f) {
|
|||||||
if (!interrupt) try {
|
if (!interrupt) try {
|
||||||
frameAdded = true;
|
frameAdded = true;
|
||||||
enc->addFrame(img);
|
enc->addFrame(img);
|
||||||
} catch (std::runtime_error e) {
|
} catch (std::runtime_error &e) {
|
||||||
// notifications::notify("KShare Video Encoder Error", e.what(),
|
// notifications::notify("KShare Video Encoder Error", e.what(),
|
||||||
// QSystemTrayIcon::Critical);
|
// QSystemTrayIcon::Critical);
|
||||||
qCritical() << "Encoder error: " << e.what();
|
qCritical() << "Encoder error: " << e.what();
|
||||||
|
@ -35,12 +35,13 @@ SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::Se
|
|||||||
ui->uploaderList->setSelectionMode(QAbstractItemView::SingleSelection);
|
ui->uploaderList->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
|
|
||||||
// Add items to uploader selection
|
// Add items to uploader selection
|
||||||
|
connect(&UploaderSingleton::inst(), &UploaderSingleton::newUploader, this, &SettingsDialog::newUploader);
|
||||||
|
connect(&UploaderSingleton::inst(), &UploaderSingleton::uploaderChanged, this, &SettingsDialog::uploaderChanged);
|
||||||
for (Uploader *u : UploaderSingleton::inst().uploaderList()) newUploader(u);
|
for (Uploader *u : UploaderSingleton::inst().uploaderList()) newUploader(u);
|
||||||
|
|
||||||
// Set filename scheme
|
// Set filename scheme
|
||||||
setScheme(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH:mm:ss)date.ext").toString());
|
setScheme(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH:mm:ss)date.ext").toString());
|
||||||
|
|
||||||
|
|
||||||
// Set delay
|
// Set delay
|
||||||
if ((settings::settings().contains("delay")))
|
if ((settings::settings().contains("delay")))
|
||||||
ui->delay->setValue(settings::settings().value("delay").toDouble());
|
ui->delay->setValue(settings::settings().value("delay").toDouble());
|
||||||
@ -93,11 +94,17 @@ void SettingsDialog::newUploader(Uploader *u) {
|
|||||||
if (UploaderSingleton::inst().currentUploader() == u->name()) ui->uploaderList->setCurrentItem(uploader);
|
if (UploaderSingleton::inst().currentUploader() == u->name()) ui->uploaderList->setCurrentItem(uploader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SettingsDialog::uploaderChanged(QString newName) {
|
||||||
|
for (auto item : ui->uploaderList->findItems(newName, Qt::MatchCaseSensitive))
|
||||||
|
ui->uploaderList->setCurrentItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
void SettingsDialog::on_uploaderList_clicked(const QModelIndex &) {
|
void SettingsDialog::on_uploaderList_clicked(const QModelIndex &) {
|
||||||
QList<QListWidgetItem *> index = ui->uploaderList->selectedItems();
|
UploaderSingleton::inst().set(ui->uploaderList->currentItem()->text());
|
||||||
if (index.size() == 1) {
|
}
|
||||||
UploaderSingleton::inst().set(index.at(0)->text());
|
|
||||||
}
|
void SettingsDialog::on_uploaderList_doubleClicked(const QModelIndex &) {
|
||||||
|
UploaderSingleton::inst().showSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::on_nameScheme_textChanged(const QString &arg1) {
|
void SettingsDialog::on_nameScheme_textChanged(const QString &arg1) {
|
||||||
@ -109,14 +116,12 @@ void SettingsDialog::on_delay_valueChanged(double arg1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::on_hotkeys_doubleClicked(const QModelIndex &) {
|
void SettingsDialog::on_hotkeys_doubleClicked(const QModelIndex &) {
|
||||||
if (ui->hotkeys->selectedItems().length() == 1) {
|
QListWidgetItem *i = ui->hotkeys->currentItem();
|
||||||
QListWidgetItem *i = ui->hotkeys->selectedItems().at(0);
|
|
||||||
QString str = i->data(Qt::UserRole + 1).toString();
|
QString str = i->data(Qt::UserRole + 1).toString();
|
||||||
HotkeyInputDialog *hotkey = new HotkeyInputDialog(str, hotkeying::sequence(str), this);
|
HotkeyInputDialog *hotkey = new HotkeyInputDialog(str, hotkeying::sequence(str), this);
|
||||||
connect(hotkey, &HotkeyInputDialog::sequenceSelected,
|
connect(hotkey, &HotkeyInputDialog::sequenceSelected,
|
||||||
[&](QKeySequence seq, QString name) { hotkeying::hotkey(name, seq, fncs.value(name)); });
|
[&](QKeySequence seq, QString name) { hotkeying::hotkey(name, seq, fncs.value(name)); });
|
||||||
hotkey->show();
|
hotkey->show();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::on_settingsButton_clicked() {
|
void SettingsDialog::on_settingsButton_clicked() {
|
||||||
|
@ -16,12 +16,12 @@ public:
|
|||||||
explicit SettingsDialog(QWidget *parent = 0);
|
explicit SettingsDialog(QWidget *parent = 0);
|
||||||
~SettingsDialog();
|
~SettingsDialog();
|
||||||
void setScheme(QString scheme);
|
void setScheme(QString scheme);
|
||||||
void newUploader(Uploader *u);
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void on_uploaderList_clicked(const QModelIndex &);
|
void on_uploaderList_clicked(const QModelIndex &);
|
||||||
|
void on_uploaderList_doubleClicked(const QModelIndex &);
|
||||||
void on_delay_valueChanged(double arg1);
|
void on_delay_valueChanged(double arg1);
|
||||||
void on_hotkeys_doubleClicked(const QModelIndex &index);
|
void on_hotkeys_doubleClicked(const QModelIndex &i);
|
||||||
void on_settingsButton_clicked();
|
void on_settingsButton_clicked();
|
||||||
void on_quickMode_clicked(bool checked);
|
void on_quickMode_clicked(bool checked);
|
||||||
void on_hideToTray_clicked(bool checked);
|
void on_hideToTray_clicked(bool checked);
|
||||||
@ -30,6 +30,8 @@ public slots:
|
|||||||
void on_imageFormatBox_currentIndexChanged(int index);
|
void on_imageFormatBox_currentIndexChanged(int index);
|
||||||
void on_pushButton_clicked();
|
void on_pushButton_clicked();
|
||||||
void on_nameScheme_textChanged(const QString &arg1);
|
void on_nameScheme_textChanged(const QString &arg1);
|
||||||
|
void newUploader(Uploader *u);
|
||||||
|
void uploaderChanged(QString newName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::SettingsDialog *ui;
|
Ui::SettingsDialog *ui;
|
||||||
|
73
uploaders/default/imgursettingsdialog.cpp
Normal file
73
uploaders/default/imgursettingsdialog.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include "imgursettingsdialog.hpp"
|
||||||
|
#include "ui_imgursettingsdialog.h"
|
||||||
|
|
||||||
|
#include <QDesktopServices>
|
||||||
|
#include <QDialogButtonBox>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QUrl>
|
||||||
|
#include <io/ioutils.hpp>
|
||||||
|
#include <settings.hpp>
|
||||||
|
|
||||||
|
ImgurSettingsDialog::ImgurSettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ImgurSettingsDialog) {
|
||||||
|
ui->setupUi(this);
|
||||||
|
connect(this, &ImgurSettingsDialog::accepted, this, &ImgurSettingsDialog::deleteLater);
|
||||||
|
ui->clientId->setText(settings::settings().value("imgur/cid").toString());
|
||||||
|
ui->clientSecret->setText(settings::settings().value("imgur/csecret").toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
ImgurSettingsDialog::~ImgurSettingsDialog() {
|
||||||
|
delete ui;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImgurSettingsDialog::on_addApp_clicked() {
|
||||||
|
QDesktopServices::openUrl(QUrl("https://api.imgur.com/oauth2/addclient"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImgurSettingsDialog::on_getPin_clicked() {
|
||||||
|
QDesktopServices::openUrl(
|
||||||
|
QUrl(QString("https://api.imgur.com/oauth2/authorize?client_id=%1&response_type=pin").arg(ui->clientId->text())));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImgurSettingsDialog::on_authorize_clicked() {
|
||||||
|
if (ui->pin->text().isEmpty() || ui->clientId->text().isEmpty() || ui->clientSecret->text().isEmpty()) return;
|
||||||
|
ui->buttonBox->setEnabled(false);
|
||||||
|
QJsonObject object;
|
||||||
|
object.insert("client_id", ui->clientId->text());
|
||||||
|
object.insert("client_secret", ui->clientSecret->text());
|
||||||
|
object.insert("grant_type", "pin");
|
||||||
|
object.insert("pin", ui->pin->text());
|
||||||
|
settings::settings().setValue("imgur/cid", ui->clientId->text());
|
||||||
|
settings::settings().setValue("imgur/csecret", ui->clientSecret->text());
|
||||||
|
|
||||||
|
ioutils::postJson(QUrl("https://api.imgur.com/oauth2/token"),
|
||||||
|
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
|
||||||
|
QJsonDocument::fromVariant(object.toVariantMap()).toJson(),
|
||||||
|
[&](QJsonDocument response, QByteArray, QNetworkReply *r) {
|
||||||
|
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
|
||||||
|
ui->buttonBox->setEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QJsonObject res = response.object();
|
||||||
|
if (res.value("success").toBool()) {
|
||||||
|
ui->buttonBox->setEnabled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
settings::settings().setValue("imgur/expire",
|
||||||
|
QDateTime::currentDateTimeUtc().addSecs(res["expires_in"].toInt()));
|
||||||
|
settings::settings().setValue("imgur/refresh", res["refresh_token"].toString());
|
||||||
|
settings::settings().setValue("imgur/access", res["refresh_token"].toString());
|
||||||
|
ui->status->setText("It works!");
|
||||||
|
ui->status->setStyleSheet("* { color: green; }");
|
||||||
|
|
||||||
|
ui->authorize->setEnabled(false);
|
||||||
|
ui->addApp->setEnabled(false);
|
||||||
|
ui->clientSecret->setEnabled(false);
|
||||||
|
ui->clientId->setEnabled(false);
|
||||||
|
|
||||||
|
ui->buttonBox->setEnabled(true);
|
||||||
|
});
|
||||||
|
}
|
26
uploaders/default/imgursettingsdialog.hpp
Normal file
26
uploaders/default/imgursettingsdialog.hpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef IMGURSETTINGSDIALOG_HPP
|
||||||
|
#define IMGURSETTINGSDIALOG_HPP
|
||||||
|
|
||||||
|
#include <QDialog>
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class ImgurSettingsDialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ImgurSettingsDialog : public QDialog {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ImgurSettingsDialog(QWidget *parent = 0);
|
||||||
|
~ImgurSettingsDialog();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void on_addApp_clicked();
|
||||||
|
void on_getPin_clicked();
|
||||||
|
void on_authorize_clicked();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::ImgurSettingsDialog *ui;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // IMGURSETTINGSDIALOG_HPP
|
150
uploaders/default/imgursettingsdialog.ui
Normal file
150
uploaders/default/imgursettingsdialog.ui
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>ImgurSettingsDialog</class>
|
||||||
|
<widget class="QDialog" name="ImgurSettingsDialog">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>287</width>
|
||||||
|
<height>367</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>Imgur auth</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout_2">
|
||||||
|
<item row="1" column="1">
|
||||||
|
<widget class="QDialogButtonBox" name="buttonBox">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="standardButtons">
|
||||||
|
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="0" colspan="2">
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="title">
|
||||||
|
<string>OAuth2</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
|
<item row="0" column="0" colspan="2">
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="text">
|
||||||
|
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||||
|
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||||
|
p, li { white-space: pre-wrap; }
|
||||||
|
</style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;">
|
||||||
|
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Create a new application:</p></body></html></string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" colspan="2">
|
||||||
|
<widget class="QPushButton" name="addApp">
|
||||||
|
<property name="text">
|
||||||
|
<string>Open imgur</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0" colspan="2">
|
||||||
|
<widget class="QLabel" name="label_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Insert Client ID and secret:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="3" column="0">
|
||||||
|
<widget class="QLineEdit" name="clientId">
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>Client ID</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="3" column="1">
|
||||||
|
<widget class="QLineEdit" name="clientSecret">
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>Client Secret</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="4" column="0" colspan="2">
|
||||||
|
<widget class="QPushButton" name="getPin">
|
||||||
|
<property name="text">
|
||||||
|
<string>Get the pin</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="5" column="0" colspan="2">
|
||||||
|
<widget class="QLabel" name="label_3">
|
||||||
|
<property name="text">
|
||||||
|
<string>Insert the pin below:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="6" column="0" colspan="2">
|
||||||
|
<widget class="QLineEdit" name="pin">
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>PIN</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="7" column="0" colspan="2">
|
||||||
|
<widget class="QPushButton" name="authorize">
|
||||||
|
<property name="text">
|
||||||
|
<string>Authorize</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="8" column="0" colspan="2">
|
||||||
|
<widget class="QLabel" name="status">
|
||||||
|
<property name="styleSheet">
|
||||||
|
<string notr="true">* { color: red; }</string>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Not working</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections>
|
||||||
|
<connection>
|
||||||
|
<sender>buttonBox</sender>
|
||||||
|
<signal>accepted()</signal>
|
||||||
|
<receiver>ImgurSettingsDialog</receiver>
|
||||||
|
<slot>accept()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>248</x>
|
||||||
|
<y>254</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>157</x>
|
||||||
|
<y>274</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>buttonBox</sender>
|
||||||
|
<signal>rejected()</signal>
|
||||||
|
<receiver>ImgurSettingsDialog</receiver>
|
||||||
|
<slot>reject()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>316</x>
|
||||||
|
<y>260</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>286</x>
|
||||||
|
<y>274</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
</connections>
|
||||||
|
</ui>
|
@ -1,12 +1,56 @@
|
|||||||
#include "imguruploader.hpp"
|
#include "imguruploader.hpp"
|
||||||
|
#include "imgursettingsdialog.hpp"
|
||||||
|
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
#include <QNetworkReply>
|
||||||
#include <formats.hpp>
|
#include <formats.hpp>
|
||||||
#include <io/ioutils.hpp>
|
#include <io/ioutils.hpp>
|
||||||
#include <notifications.hpp>
|
#include <notifications.hpp>
|
||||||
|
#include <printf.h>
|
||||||
#include <screenshotutil.hpp>
|
#include <screenshotutil.hpp>
|
||||||
|
#include <settings.hpp>
|
||||||
|
|
||||||
|
struct SegfaultWorkaround {
|
||||||
|
SegfaultWorkaround(QByteArray a, ImgurUploader *u, QString m) : byteArray(), dis(u), mime(m) {
|
||||||
|
a.swap(byteArray);
|
||||||
|
QJsonObject object;
|
||||||
|
object.insert("client_id", settings::settings().value("imgur/cid").toString());
|
||||||
|
object.insert("client_secret", settings::settings().value("imgur/csecret").toString());
|
||||||
|
object.insert("grant_type", "refresh_token");
|
||||||
|
object.insert("refresh_token", settings::settings().value("imgur/refresh").toString());
|
||||||
|
|
||||||
|
ioutils::postJson(
|
||||||
|
QUrl("https://api.imgur.com/oauth2/token"),
|
||||||
|
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
|
||||||
|
QJsonDocument::fromVariant(object.toVariantMap()).toJson(), [&](QJsonDocument response, QByteArray, QNetworkReply *r) {
|
||||||
|
qDebug() << response;
|
||||||
|
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
|
||||||
|
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QJsonObject res = response.object();
|
||||||
|
if (res.value("success").toBool()) {
|
||||||
|
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString token = res["access_token"].toString();
|
||||||
|
settings::settings().setValue("imgur/expire", QDateTime::currentDateTimeUtc().addSecs(res["expires_in"].toInt()));
|
||||||
|
settings::settings().setValue("imgur/refresh", res["refresh_token"].toString());
|
||||||
|
settings::settings().setValue("imgur/access", token);
|
||||||
|
|
||||||
|
dis->handleSend(token.prepend("Bearer "), mime, byteArray);
|
||||||
|
QScopedPointer<SegfaultWorkaround>(this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QByteArray byteArray;
|
||||||
|
ImgurUploader *dis;
|
||||||
|
QString mime;
|
||||||
|
};
|
||||||
|
|
||||||
void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
|
void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
|
||||||
if (byteArray.size() > 1e+7) {
|
if (byteArray.size() > 1e+7) {
|
||||||
@ -18,10 +62,26 @@ void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
|
|||||||
mime = formats::normalFormatMIME(formats::normalFormatFromName(format));
|
mime = formats::normalFormatMIME(formats::normalFormatFromName(format));
|
||||||
else
|
else
|
||||||
mime = formats::recordingFormatMIME(formats::recordingFormatFromName(format));
|
mime = formats::recordingFormatMIME(formats::recordingFormatFromName(format));
|
||||||
|
if (settings::settings().contains("imgur/expire") //
|
||||||
|
&& settings::settings().contains("imgur/refresh") //
|
||||||
|
&& settings::settings().contains("imgur/access")) {
|
||||||
|
QDateTime loltheseguysIminacallwithareretarded = settings::settings().value("imgur/expire").toDateTime();
|
||||||
|
if (loltheseguysIminacallwithareretarded.msecsTo(QDateTime::currentDateTimeUtc()) < 0) {
|
||||||
|
new SegfaultWorkaround(byteArray, this, mime);
|
||||||
|
} else
|
||||||
|
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray);
|
||||||
|
} else
|
||||||
|
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImgurUploader::showSettings() {
|
||||||
|
(new ImgurSettingsDialog())->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
|
||||||
ioutils::postJson(QUrl("https://api.imgur.com/3/image"),
|
ioutils::postJson(QUrl("https://api.imgur.com/3/image"),
|
||||||
QList<QPair<QString, QString>>()
|
QList<QPair<QString, QString>>() << QPair<QString, QString>("Content-Type", mime.toUtf8())
|
||||||
<< QPair<QString, QString>("Content-Type", mime.toLatin1())
|
<< QPair<QString, QString>("Authorization", auth),
|
||||||
<< QPair<QString, QString>("Authorization", "Client-ID 8a98f183fc895da"),
|
|
||||||
byteArray, [](QJsonDocument res, QByteArray, QNetworkReply *) {
|
byteArray, [](QJsonDocument res, QByteArray, QNetworkReply *) {
|
||||||
QString result = res.object()["data"].toObject()["link"].toString();
|
QString result = res.object()["data"].toObject()["link"].toString();
|
||||||
if (!result.isEmpty()) {
|
if (!result.isEmpty()) {
|
||||||
|
@ -4,14 +4,20 @@
|
|||||||
#include "../uploader.hpp"
|
#include "../uploader.hpp"
|
||||||
|
|
||||||
class ImgurUploader : public Uploader {
|
class ImgurUploader : public Uploader {
|
||||||
|
friend struct SegfaultWorkaround;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QString name() {
|
QString name() override {
|
||||||
return "imgur";
|
return "imgur";
|
||||||
}
|
}
|
||||||
QString description() {
|
QString description() override {
|
||||||
return "imgur.com uploader";
|
return "imgur.com uploader";
|
||||||
}
|
}
|
||||||
void doUpload(QByteArray byteArray, QString);
|
void doUpload(QByteArray byteArray, QString) override;
|
||||||
|
void showSettings() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void handleSend(QString auth, QString mime, QByteArray byteArray);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // IMGURUPLOADER_HPP
|
#endif // IMGURUPLOADER_HPP
|
||||||
|
@ -9,6 +9,11 @@ public:
|
|||||||
virtual void doUpload(QByteArray imgData, QString format) = 0;
|
virtual void doUpload(QByteArray imgData, QString format) = 0;
|
||||||
virtual QString name() = 0;
|
virtual QString name() = 0;
|
||||||
virtual QString description() = 0;
|
virtual QString description() = 0;
|
||||||
|
virtual void showSettings() {
|
||||||
|
}
|
||||||
|
virtual bool validate() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // UPLOADER_HPP
|
#endif // UPLOADER_HPP
|
||||||
|
@ -29,7 +29,7 @@ UploaderSingleton::UploaderSingleton()
|
|||||||
for (QString file : configDir.entryList()) {
|
for (QString file : configDir.entryList()) {
|
||||||
try {
|
try {
|
||||||
registerUploader(new CustomUploader(configDir.absoluteFilePath(file)));
|
registerUploader(new CustomUploader(configDir.absoluteFilePath(file)));
|
||||||
} catch (std::runtime_error e) {
|
} catch (std::runtime_error &e) {
|
||||||
qWarning() << e.what();
|
qWarning() << e.what();
|
||||||
errs << e;
|
errs << e;
|
||||||
}
|
}
|
||||||
@ -45,21 +45,25 @@ UploaderSingleton::UploaderSingleton()
|
|||||||
else
|
else
|
||||||
settings::settings().setValue("uploader", uploader);
|
settings::settings().setValue("uploader", uploader);
|
||||||
if (!uploaders.contains(uploader)) {
|
if (!uploaders.contains(uploader)) {
|
||||||
settings::settings().setValue("uploader", uploader);
|
|
||||||
uploader = "imgur";
|
uploader = "imgur";
|
||||||
|
settings::settings().setValue("uploader", uploader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UploaderSingleton::registerUploader(Uploader *uploader) {
|
void UploaderSingleton::registerUploader(Uploader *uploader) {
|
||||||
if (uploaders.contains(uploader->name())) {
|
if (uploaders.contains(uploader->name()))
|
||||||
throw std::runtime_error(("Ambigious uploader " + uploader->name()).toStdString());
|
throw std::runtime_error(("Ambigious uploader " + uploader->name()).toStdString());
|
||||||
}
|
|
||||||
uploaders.insert(uploader->name(), uploader);
|
uploaders.insert(uploader->name(), uploader);
|
||||||
emit newUploader(uploader);
|
emit newUploader(uploader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UploaderSingleton::upload(QPixmap *pixmap) {
|
void UploaderSingleton::upload(QPixmap *pixmap) {
|
||||||
auto u = uploaders.value(uploader);
|
auto u = uploaders.value(uploader);
|
||||||
|
if (!u->validate()) {
|
||||||
|
u = uploaders.value("imgur");
|
||||||
|
set("imgur");
|
||||||
|
qWarning() << "Currently selected uploader is not set up properly! Falling back to imgur";
|
||||||
|
}
|
||||||
QString format = settings::settings().value("captureformat", "PNG").toString();
|
QString format = settings::settings().value("captureformat", "PNG").toString();
|
||||||
QFile file(saveDir.absoluteFilePath(
|
QFile file(saveDir.absoluteFilePath(
|
||||||
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH:mm:ss)date.%ext").toString(),
|
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH:mm:ss)date.%ext").toString(),
|
||||||
@ -91,6 +95,14 @@ void UploaderSingleton::upload(QFile img, QString format) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UploaderSingleton::showSettings() {
|
||||||
|
uploaders.value(uploader)->showSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UploaderSingleton::validate() {
|
||||||
|
return uploaders.value(uploader)->validate();
|
||||||
|
}
|
||||||
|
|
||||||
QList<Uploader *> UploaderSingleton::uploaderList() {
|
QList<Uploader *> UploaderSingleton::uploaderList() {
|
||||||
return uploaders.values();
|
return uploaders.values();
|
||||||
}
|
}
|
||||||
@ -99,6 +111,7 @@ void UploaderSingleton::set(QString uploader) {
|
|||||||
if (uploaders.contains(uploader)) {
|
if (uploaders.contains(uploader)) {
|
||||||
this->uploader = uploader;
|
this->uploader = uploader;
|
||||||
settings::settings().setValue("uploader", uploader);
|
settings::settings().setValue("uploader", uploader);
|
||||||
|
emit uploaderChanged(uploader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,14 +16,17 @@ public:
|
|||||||
void upload(QPixmap *pixmap);
|
void upload(QPixmap *pixmap);
|
||||||
void upload(QByteArray img, QString format);
|
void upload(QByteArray img, QString format);
|
||||||
void upload(QFile img, QString format);
|
void upload(QFile img, QString format);
|
||||||
|
void showSettings();
|
||||||
QList<Uploader *> uploaderList();
|
QList<Uploader *> uploaderList();
|
||||||
void set(QString uploader);
|
void set(QString uploader);
|
||||||
QString selectedUploader();
|
QString selectedUploader();
|
||||||
QList<std::runtime_error> errors();
|
QList<std::runtime_error> errors();
|
||||||
QString currentUploader();
|
QString currentUploader();
|
||||||
|
bool validate();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newUploader(Uploader *u);
|
void newUploader(Uploader *u);
|
||||||
|
void uploaderChanged(QString newName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QDir saveDir;
|
QDir saveDir;
|
||||||
|
Loading…
Reference in New Issue
Block a user