mirror of
https://github.com/fergalmoran/flameshot.git
synced 2025-12-22 09:51:06 +00:00
Added UploaderManager for further multiple Storage for images support (#2142)
Co-authored-by: Yuriy Puchkov <yuriy.puchkov@namecheap.com>
This commit is contained in:
@@ -536,9 +536,9 @@ void GeneralConf::initAntialiasingPinZoom()
|
||||
void GeneralConf::initUploadWithoutConfirmation()
|
||||
{
|
||||
m_uploadWithoutConfirmation =
|
||||
new QCheckBox(tr("Upload to Imgur without confirmation"), this);
|
||||
new QCheckBox(tr("Upload image without confirmation"), this);
|
||||
m_uploadWithoutConfirmation->setToolTip(
|
||||
tr("Upload to Imgur without confirmation"));
|
||||
tr("Upload image without confirmation"));
|
||||
m_scrollAreaLayout->addWidget(m_uploadWithoutConfirmation);
|
||||
connect(m_uploadWithoutConfirmation, &QCheckBox::clicked, [](bool checked) {
|
||||
ConfigHandler().setUploadWithoutConfirmation(checked);
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
#include "capturerequest.h"
|
||||
#include "confighandler.h"
|
||||
#include "controller.h"
|
||||
#include "imguruploader.h"
|
||||
#include "imgupload/imguploadermanager.h"
|
||||
#include "pinwidget.h"
|
||||
#include "src/utils/screenshotsaver.h"
|
||||
#include "src/widgets/imguruploaddialog.h"
|
||||
#include "src/widgets/imguploaddialog.h"
|
||||
#include "systemnotification.h"
|
||||
#include <QApplication>
|
||||
#include <QClipboard>
|
||||
@@ -166,18 +166,19 @@ void CaptureRequest::exportCapture(const QPixmap& capture)
|
||||
|
||||
if (m_tasks & UPLOAD) {
|
||||
if (!ConfigHandler().uploadWithoutConfirmation()) {
|
||||
ImgurUploadDialog* dialog = new ImgurUploadDialog();
|
||||
ImgUploadDialog* dialog = new ImgUploadDialog();
|
||||
if (dialog->exec() == QDialog::Rejected) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
ImgurUploader* widget = new ImgurUploader(capture);
|
||||
|
||||
ImgUploaderBase* widget = ImgUploaderManager().uploader(capture);
|
||||
widget->show();
|
||||
widget->activateWindow();
|
||||
// NOTE: lambda can't capture 'this' because it might be destroyed later
|
||||
ExportTask tasks = m_tasks;
|
||||
QObject::connect(
|
||||
widget, &ImgurUploader::uploadOk, [widget, tasks](const QUrl& url) {
|
||||
widget, &ImgUploaderBase::uploadOk, [widget, tasks](const QUrl& url) {
|
||||
if (ConfigHandler().copyAndCloseAfterUpload()) {
|
||||
if (!(tasks & COPY)) {
|
||||
SystemNotification().sendMessage(
|
||||
|
||||
@@ -8,10 +8,14 @@ target_sources(flameshot PRIVATE sizeincrease/sizeincreasetool.h sizeincrease/si
|
||||
target_sources(flameshot PRIVATE sizedecrease/sizedecreasetool.h sizedecrease/sizedecreasetool.cpp)
|
||||
target_sources(
|
||||
flameshot
|
||||
PRIVATE imgur/imguruploader.h
|
||||
imgur/imguruploadertool.h
|
||||
imgur/imguruploader.cpp
|
||||
imgur/imguruploadertool.cpp
|
||||
PRIVATE imgupload/storages/imgur/imguruploader.h
|
||||
imgupload/storages/imgur/imguruploader.cpp
|
||||
imgupload/storages/imguploaderbase.h
|
||||
imgupload/storages/imguploaderbase.cpp
|
||||
imgupload/imguploadertool.h
|
||||
imgupload/imguploadertool.cpp
|
||||
imgupload/imguploadermanager.h
|
||||
imgupload/imguploadermanager.cpp
|
||||
)
|
||||
target_sources(
|
||||
flameshot
|
||||
|
||||
70
src/tools/imgupload/imguploadermanager.cpp
Normal file
70
src/tools/imgupload/imguploadermanager.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: Yurii Puchkov & Contributors
|
||||
//
|
||||
|
||||
#include "imguploadermanager.h"
|
||||
#include <QPixmap>
|
||||
#include <QWidget>
|
||||
|
||||
// TODO - remove this hard-code and create plugin manager in the future, you may
|
||||
// include other storage headers here
|
||||
#include "storages/imgur/imguruploader.h"
|
||||
|
||||
ImgUploaderManager::ImgUploaderManager(QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_imgUploaderBase(nullptr)
|
||||
{
|
||||
// TODO - implement ImgUploader for other Storages and selection among them
|
||||
m_imgUploaderPlugin = IMG_UPLOADER_STORAGE_DEFAULT;
|
||||
init();
|
||||
}
|
||||
|
||||
void ImgUploaderManager::init()
|
||||
{
|
||||
// TODO - implement ImgUploader for other Storages and selection among them,
|
||||
// example:
|
||||
// if (uploaderPlugin().compare("s3") == 0) {
|
||||
// m_qstrUrl = ImgS3Settings().value("S3", "S3_URL").toString();
|
||||
//} else {
|
||||
// m_qstrUrl = "https://imgur.com/";
|
||||
// m_imgUploaderPlugin = "imgur";
|
||||
//}
|
||||
m_urlString = "https://imgur.com/";
|
||||
m_imgUploaderPlugin = "imgur";
|
||||
}
|
||||
|
||||
ImgUploaderBase* ImgUploaderManager::uploader(const QPixmap& capture,
|
||||
QWidget* parent)
|
||||
{
|
||||
// TODO - implement ImgUploader for other Storages and selection among them,
|
||||
// example:
|
||||
// if (uploaderPlugin().compare("s3") == 0) {
|
||||
// m_imgUploaderBase =
|
||||
// (ImgUploaderBase*)(new ImgS3Uploader(capture, parent));
|
||||
//} else {
|
||||
// m_imgUploaderBase =
|
||||
// (ImgUploaderBase*)(new ImgurUploader(capture, parent));
|
||||
//}
|
||||
m_imgUploaderBase = (ImgUploaderBase*)(new ImgurUploader(capture, parent));
|
||||
if (m_imgUploaderBase && !capture.isNull()) {
|
||||
m_imgUploaderBase->upload();
|
||||
}
|
||||
return m_imgUploaderBase;
|
||||
}
|
||||
|
||||
ImgUploaderBase* ImgUploaderManager::uploader(const QString& imgUploaderPlugin)
|
||||
{
|
||||
m_imgUploaderPlugin = imgUploaderPlugin;
|
||||
init();
|
||||
return uploader(QPixmap());
|
||||
}
|
||||
|
||||
const QString& ImgUploaderManager::uploaderPlugin()
|
||||
{
|
||||
return m_imgUploaderPlugin;
|
||||
}
|
||||
|
||||
const QString& ImgUploaderManager::url()
|
||||
{
|
||||
return m_urlString;
|
||||
}
|
||||
38
src/tools/imgupload/imguploadermanager.h
Normal file
38
src/tools/imgupload/imguploadermanager.h
Normal file
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: Yurii Puchkov & Contributors
|
||||
//
|
||||
|
||||
#ifndef FLAMESHOT_IMGUPLOADERMANAGER_H
|
||||
#define FLAMESHOT_IMGUPLOADERMANAGER_H
|
||||
|
||||
#include "src/tools/imgupload/storages/imguploaderbase.h"
|
||||
#include <QObject>
|
||||
|
||||
#define IMG_UPLOADER_STORAGE_DEFAULT "imgur"
|
||||
|
||||
class QPixmap;
|
||||
class QWidget;
|
||||
|
||||
class ImgUploaderManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImgUploaderManager(QObject* parent = nullptr);
|
||||
|
||||
ImgUploaderBase* uploader(const QPixmap& capture,
|
||||
QWidget* parent = nullptr);
|
||||
ImgUploaderBase* uploader(const QString& imgUploaderPlugin);
|
||||
|
||||
const QString& url();
|
||||
const QString& uploaderPlugin();
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
private:
|
||||
ImgUploaderBase* m_imgUploaderBase;
|
||||
QString m_urlString;
|
||||
QString m_imgUploaderPlugin;
|
||||
};
|
||||
|
||||
#endif // FLAMESHOT_IMGUPLOADERMANAGER_H
|
||||
46
src/tools/imgupload/imguploadertool.cpp
Normal file
46
src/tools/imgupload/imguploadertool.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#include "imguploadertool.h"
|
||||
|
||||
ImgUploaderTool::ImgUploaderTool(QObject* parent)
|
||||
: AbstractActionTool(parent)
|
||||
{}
|
||||
|
||||
bool ImgUploaderTool::closeOnButtonPressed() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QIcon ImgUploaderTool::icon(const QColor& background, bool inEditor) const
|
||||
{
|
||||
Q_UNUSED(inEditor);
|
||||
return QIcon(iconPath(background) + "cloud-upload.svg");
|
||||
}
|
||||
|
||||
QString ImgUploaderTool::name() const
|
||||
{
|
||||
return tr("Image Uploader");
|
||||
}
|
||||
|
||||
CaptureTool::Type ImgUploaderTool::type() const
|
||||
{
|
||||
return CaptureTool::TYPE_IMAGEUPLOADER;
|
||||
}
|
||||
|
||||
QString ImgUploaderTool::description() const
|
||||
{
|
||||
return tr("Upload the selection");
|
||||
}
|
||||
|
||||
CaptureTool* ImgUploaderTool::copy(QObject* parent)
|
||||
{
|
||||
return new ImgUploaderTool(parent);
|
||||
}
|
||||
|
||||
void ImgUploaderTool::pressed(CaptureContext& context)
|
||||
{
|
||||
emit requestAction(REQ_CAPTURE_DONE_OK);
|
||||
context.request()->addTask(CaptureRequest::UPLOAD);
|
||||
emit requestAction(REQ_CLOSE_GUI);
|
||||
}
|
||||
@@ -5,11 +5,11 @@
|
||||
|
||||
#include "src/tools/abstractactiontool.h"
|
||||
|
||||
class ImgurUploaderTool : public AbstractActionTool
|
||||
class ImgUploaderTool : public AbstractActionTool
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImgurUploaderTool(QObject* parent = nullptr);
|
||||
explicit ImgUploaderTool(QObject* parent = nullptr);
|
||||
|
||||
bool closeOnButtonPressed() const override;
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#include "imguruploader.h"
|
||||
#include "imguploaderbase.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/utils/filenamehandler.h"
|
||||
#include "src/utils/globalvalues.h"
|
||||
#include "src/utils/history.h"
|
||||
#include "src/utils/systemnotification.h"
|
||||
#include "src/widgets/imagelabel.h"
|
||||
#include "src/widgets/loadspinner.h"
|
||||
#include "src/widgets/notificationwidget.h"
|
||||
#include <QApplication>
|
||||
#include <QBuffer>
|
||||
#include <QClipboard>
|
||||
#include <QCursor>
|
||||
#include <QDesktopServices>
|
||||
#include <QDrag>
|
||||
#include <QGuiApplication>
|
||||
#include <QHBoxLayout>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QLabel>
|
||||
#include <QMimeData>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QPushButton>
|
||||
#include <QRect>
|
||||
#include <QScreen>
|
||||
@@ -33,11 +27,11 @@
|
||||
#include <QUrlQuery>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
ImgurUploader::ImgurUploader(const QPixmap& capture, QWidget* parent)
|
||||
ImgUploaderBase::ImgUploaderBase(const QPixmap& capture, QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_pixmap(capture)
|
||||
{
|
||||
setWindowTitle(tr("Upload to Imgur"));
|
||||
setWindowTitle(tr("Upload image"));
|
||||
setWindowIcon(QIcon(GlobalValues::iconPath()));
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
||||
@@ -60,52 +54,45 @@ ImgurUploader::ImgurUploader(const QPixmap& capture, QWidget* parent)
|
||||
m_vLayout->addWidget(m_spinner, 0, Qt::AlignHCenter);
|
||||
m_vLayout->addWidget(m_infoLabel);
|
||||
|
||||
m_NetworkAM = new QNetworkAccessManager(this);
|
||||
connect(m_NetworkAM,
|
||||
&QNetworkAccessManager::finished,
|
||||
this,
|
||||
&ImgurUploader::handleReply);
|
||||
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
upload();
|
||||
// QTimer::singleShot(2000, this, &ImgurUploader::showPostUploadDialog); //
|
||||
// testing
|
||||
}
|
||||
|
||||
void ImgurUploader::handleReply(QNetworkReply* reply)
|
||||
LoadSpinner* ImgUploaderBase::spinner()
|
||||
{
|
||||
m_spinner->deleteLater();
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
|
||||
QJsonObject json = response.object();
|
||||
QJsonObject data = json[QStringLiteral("data")].toObject();
|
||||
m_imageURL.setUrl(data[QStringLiteral("link")].toString());
|
||||
|
||||
auto deleteToken = data[QStringLiteral("deletehash")].toString();
|
||||
m_deleteImageURL.setUrl(
|
||||
QStringLiteral("https://imgur.com/delete/%1").arg(deleteToken));
|
||||
|
||||
// save history
|
||||
QString imageName = m_imageURL.toString();
|
||||
int lastSlash = imageName.lastIndexOf("/");
|
||||
if (lastSlash >= 0) {
|
||||
imageName = imageName.mid(lastSlash + 1);
|
||||
return m_spinner;
|
||||
}
|
||||
|
||||
// save image to history
|
||||
History history;
|
||||
imageName = history.packFileName("imgur", deleteToken, imageName);
|
||||
history.save(m_pixmap, imageName);
|
||||
|
||||
emit uploadOk(m_imageURL);
|
||||
} else {
|
||||
m_infoLabel->setText(reply->errorString());
|
||||
}
|
||||
new QShortcut(Qt::Key_Escape, this, SLOT(close()));
|
||||
const QUrl& ImgUploaderBase::imageURL()
|
||||
{
|
||||
return m_imageURL;
|
||||
}
|
||||
|
||||
void ImgurUploader::startDrag()
|
||||
void ImgUploaderBase::setImageURL(const QUrl& imageURL)
|
||||
{
|
||||
m_imageURL = imageURL;
|
||||
}
|
||||
|
||||
const QPixmap& ImgUploaderBase::pixmap()
|
||||
{
|
||||
return m_pixmap;
|
||||
}
|
||||
|
||||
void ImgUploaderBase::setPixmap(const QPixmap& pixmap)
|
||||
{
|
||||
m_pixmap = pixmap;
|
||||
}
|
||||
|
||||
NotificationWidget* ImgUploaderBase::notification()
|
||||
{
|
||||
return m_notification;
|
||||
}
|
||||
|
||||
void ImgUploaderBase::setInfoLabelText(const QString& text)
|
||||
{
|
||||
m_infoLabel->setText(text);
|
||||
}
|
||||
|
||||
void ImgUploaderBase::startDrag()
|
||||
{
|
||||
QMimeData* mimeData = new QMimeData;
|
||||
mimeData->setUrls(QList<QUrl>{ m_imageURL });
|
||||
@@ -118,30 +105,7 @@ void ImgurUploader::startDrag()
|
||||
dragHandler->exec();
|
||||
}
|
||||
|
||||
void ImgurUploader::upload()
|
||||
{
|
||||
QByteArray byteArray;
|
||||
QBuffer buffer(&byteArray);
|
||||
m_pixmap.save(&buffer, "PNG");
|
||||
|
||||
QUrlQuery urlQuery;
|
||||
urlQuery.addQueryItem(QStringLiteral("title"), QStringLiteral(""));
|
||||
QString description = FileNameHandler().parsedPattern();
|
||||
urlQuery.addQueryItem(QStringLiteral("description"), description);
|
||||
|
||||
QUrl url(QStringLiteral("https://api.imgur.com/3/image"));
|
||||
url.setQuery(urlQuery);
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
||||
"application/application/x-www-form-urlencoded");
|
||||
request.setRawHeader(
|
||||
"Authorization",
|
||||
QStringLiteral("Client-ID %1").arg(IMGUR_CLIENT_ID).toUtf8());
|
||||
|
||||
m_NetworkAM->post(request, byteArray);
|
||||
}
|
||||
|
||||
void ImgurUploader::showPostUploadDialog()
|
||||
void ImgUploaderBase::showPostUploadDialog()
|
||||
{
|
||||
m_infoLabel->deleteLater();
|
||||
|
||||
@@ -151,8 +115,10 @@ void ImgurUploader::showPostUploadDialog()
|
||||
ImageLabel* imageLabel = new ImageLabel();
|
||||
imageLabel->setScreenshot(m_pixmap);
|
||||
imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
connect(
|
||||
imageLabel, &ImageLabel::dragInitiated, this, &ImgurUploader::startDrag);
|
||||
connect(imageLabel,
|
||||
&ImageLabel::dragInitiated,
|
||||
this,
|
||||
&ImgUploaderBase::startDrag);
|
||||
m_vLayout->addWidget(imageLabel);
|
||||
|
||||
m_hLayout = new QHBoxLayout();
|
||||
@@ -168,20 +134,20 @@ void ImgurUploader::showPostUploadDialog()
|
||||
m_hLayout->addWidget(m_toClipboardButton);
|
||||
|
||||
connect(
|
||||
m_copyUrlButton, &QPushButton::clicked, this, &ImgurUploader::copyURL);
|
||||
m_copyUrlButton, &QPushButton::clicked, this, &ImgUploaderBase::copyURL);
|
||||
connect(
|
||||
m_openUrlButton, &QPushButton::clicked, this, &ImgurUploader::openURL);
|
||||
m_openUrlButton, &QPushButton::clicked, this, &ImgUploaderBase::openURL);
|
||||
connect(m_openDeleteUrlButton,
|
||||
&QPushButton::clicked,
|
||||
this,
|
||||
&ImgurUploader::openDeleteURL);
|
||||
&ImgUploaderBase::deleteCurrentImage);
|
||||
connect(m_toClipboardButton,
|
||||
&QPushButton::clicked,
|
||||
this,
|
||||
&ImgurUploader::copyImage);
|
||||
&ImgUploaderBase::copyImage);
|
||||
}
|
||||
|
||||
void ImgurUploader::openURL()
|
||||
void ImgUploaderBase::openURL()
|
||||
{
|
||||
bool successful = QDesktopServices::openUrl(m_imageURL);
|
||||
if (!successful) {
|
||||
@@ -189,22 +155,22 @@ void ImgurUploader::openURL()
|
||||
}
|
||||
}
|
||||
|
||||
void ImgurUploader::copyURL()
|
||||
void ImgUploaderBase::copyURL()
|
||||
{
|
||||
QApplication::clipboard()->setText(m_imageURL.toString());
|
||||
m_notification->showMessage(tr("URL copied to clipboard."));
|
||||
}
|
||||
|
||||
void ImgurUploader::openDeleteURL()
|
||||
{
|
||||
bool successful = QDesktopServices::openUrl(m_deleteImageURL);
|
||||
if (!successful) {
|
||||
m_notification->showMessage(tr("Unable to open the URL."));
|
||||
}
|
||||
}
|
||||
|
||||
void ImgurUploader::copyImage()
|
||||
void ImgUploaderBase::copyImage()
|
||||
{
|
||||
QApplication::clipboard()->setPixmap(m_pixmap);
|
||||
m_notification->showMessage(tr("Screenshot copied to clipboard."));
|
||||
}
|
||||
|
||||
void ImgUploaderBase::deleteCurrentImage()
|
||||
{
|
||||
History history;
|
||||
HISTORY_FILE_NAME unpackFileName =
|
||||
history.unpackFileName(m_currentImageName);
|
||||
deleteImage(unpackFileName.file, unpackFileName.token);
|
||||
}
|
||||
@@ -16,30 +16,41 @@ class QPushButton;
|
||||
class QUrl;
|
||||
class NotificationWidget;
|
||||
|
||||
class ImgurUploader : public QWidget
|
||||
class ImgUploaderBase : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImgurUploader(const QPixmap& capture, QWidget* parent = nullptr);
|
||||
explicit ImgUploaderBase(const QPixmap& capture, QWidget* parent = nullptr);
|
||||
|
||||
LoadSpinner* spinner();
|
||||
|
||||
const QUrl& imageURL();
|
||||
void setImageURL(const QUrl&);
|
||||
const QPixmap& pixmap();
|
||||
void setPixmap(const QPixmap&);
|
||||
void setInfoLabelText(const QString&);
|
||||
|
||||
NotificationWidget* notification();
|
||||
virtual void deleteImage(const QString& fileName,
|
||||
const QString& deleteToken) = 0;
|
||||
virtual void upload() = 0;
|
||||
|
||||
signals:
|
||||
void uploadOk(const QUrl& url);
|
||||
void deleteOk();
|
||||
|
||||
public slots:
|
||||
void showPostUploadDialog();
|
||||
|
||||
private slots:
|
||||
void handleReply(QNetworkReply* reply);
|
||||
void startDrag();
|
||||
|
||||
void openURL();
|
||||
void copyURL();
|
||||
void openDeleteURL();
|
||||
void copyImage();
|
||||
void deleteCurrentImage();
|
||||
|
||||
private:
|
||||
QPixmap m_pixmap;
|
||||
QNetworkAccessManager* m_NetworkAM;
|
||||
|
||||
QVBoxLayout* m_vLayout;
|
||||
QHBoxLayout* m_hLayout;
|
||||
@@ -52,8 +63,8 @@ private:
|
||||
QPushButton* m_copyUrlButton;
|
||||
QPushButton* m_toClipboardButton;
|
||||
QUrl m_imageURL;
|
||||
QUrl m_deleteImageURL;
|
||||
NotificationWidget* m_notification;
|
||||
|
||||
void upload();
|
||||
public:
|
||||
QString m_currentImageName;
|
||||
};
|
||||
96
src/tools/imgupload/storages/imgur/imguruploader.cpp
Normal file
96
src/tools/imgupload/storages/imgur/imguruploader.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#include "imguruploader.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/utils/filenamehandler.h"
|
||||
#include "src/utils/history.h"
|
||||
#include "src/widgets/loadspinner.h"
|
||||
#include "src/widgets/notificationwidget.h"
|
||||
#include <QBuffer>
|
||||
#include <QDesktopServices>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QShortcut>
|
||||
#include <QUrlQuery>
|
||||
|
||||
ImgurUploader::ImgurUploader(const QPixmap& capture, QWidget* parent)
|
||||
: ImgUploaderBase(capture, parent)
|
||||
{
|
||||
m_NetworkAM = new QNetworkAccessManager(this);
|
||||
connect(m_NetworkAM,
|
||||
&QNetworkAccessManager::finished,
|
||||
this,
|
||||
&ImgurUploader::handleReply);
|
||||
}
|
||||
|
||||
void ImgurUploader::handleReply(QNetworkReply* reply)
|
||||
{
|
||||
spinner()->deleteLater();
|
||||
m_currentImageName.clear();
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
|
||||
QJsonObject json = response.object();
|
||||
QJsonObject data = json[QStringLiteral("data")].toObject();
|
||||
setImageURL(data[QStringLiteral("link")].toString());
|
||||
|
||||
auto deleteToken = data[QStringLiteral("deletehash")].toString();
|
||||
|
||||
// save history
|
||||
m_currentImageName = imageURL().toString();
|
||||
int lastSlash = m_currentImageName.lastIndexOf("/");
|
||||
if (lastSlash >= 0) {
|
||||
m_currentImageName = m_currentImageName.mid(lastSlash + 1);
|
||||
}
|
||||
|
||||
// save image to history
|
||||
History history;
|
||||
m_currentImageName =
|
||||
history.packFileName("imgur", deleteToken, m_currentImageName);
|
||||
history.save(pixmap(), m_currentImageName);
|
||||
|
||||
emit uploadOk(imageURL());
|
||||
} else {
|
||||
setInfoLabelText(reply->errorString());
|
||||
}
|
||||
new QShortcut(Qt::Key_Escape, this, SLOT(close()));
|
||||
}
|
||||
|
||||
void ImgurUploader::upload()
|
||||
{
|
||||
QByteArray byteArray;
|
||||
QBuffer buffer(&byteArray);
|
||||
pixmap().save(&buffer, "PNG");
|
||||
|
||||
QUrlQuery urlQuery;
|
||||
urlQuery.addQueryItem(QStringLiteral("title"), QStringLiteral(""));
|
||||
QString description = FileNameHandler().parsedPattern();
|
||||
urlQuery.addQueryItem(QStringLiteral("description"), description);
|
||||
|
||||
QUrl url(QStringLiteral("https://api.imgur.com/3/image"));
|
||||
url.setQuery(urlQuery);
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
||||
"application/application/x-www-form-urlencoded");
|
||||
request.setRawHeader(
|
||||
"Authorization",
|
||||
QStringLiteral("Client-ID %1").arg(IMGUR_CLIENT_ID).toUtf8());
|
||||
|
||||
m_NetworkAM->post(request, byteArray);
|
||||
}
|
||||
|
||||
void ImgurUploader::deleteImage(const QString& fileName,
|
||||
const QString& deleteToken)
|
||||
{
|
||||
Q_UNUSED(fileName)
|
||||
bool successful = QDesktopServices::openUrl(
|
||||
QUrl(QStringLiteral("https://imgur.com/delete/%1").arg(deleteToken)));
|
||||
if (!successful) {
|
||||
notification()->showMessage(tr("Unable to open the URL."));
|
||||
}
|
||||
|
||||
emit deleteOk();
|
||||
}
|
||||
29
src/tools/imgupload/storages/imgur/imguruploader.h
Normal file
29
src/tools/imgupload/storages/imgur/imguruploader.h
Normal file
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "src/tools/imgupload/storages/imguploaderbase.h"
|
||||
#include <QUrl>
|
||||
#include <QWidget>
|
||||
|
||||
class QNetworkReply;
|
||||
class QNetworkAccessManager;
|
||||
class QUrl;
|
||||
|
||||
class ImgurUploader : public ImgUploaderBase
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImgurUploader(const QPixmap& capture, QWidget* parent = nullptr);
|
||||
void deleteImage(const QString& fileName, const QString& deleteToken);
|
||||
|
||||
private slots:
|
||||
void handleReply(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
void upload();
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* m_NetworkAM;
|
||||
};
|
||||
@@ -1,48 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#include "imguruploadertool.h"
|
||||
#include "imguruploader.h"
|
||||
#include <QPainter>
|
||||
|
||||
ImgurUploaderTool::ImgurUploaderTool(QObject* parent)
|
||||
: AbstractActionTool(parent)
|
||||
{}
|
||||
|
||||
bool ImgurUploaderTool::closeOnButtonPressed() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QIcon ImgurUploaderTool::icon(const QColor& background, bool inEditor) const
|
||||
{
|
||||
Q_UNUSED(inEditor);
|
||||
return QIcon(iconPath(background) + "cloud-upload.svg");
|
||||
}
|
||||
|
||||
QString ImgurUploaderTool::name() const
|
||||
{
|
||||
return tr("Image Uploader");
|
||||
}
|
||||
|
||||
CaptureTool::Type ImgurUploaderTool::type() const
|
||||
{
|
||||
return CaptureTool::TYPE_IMAGEUPLOADER;
|
||||
}
|
||||
|
||||
QString ImgurUploaderTool::description() const
|
||||
{
|
||||
return tr("Upload the selection to Imgur");
|
||||
}
|
||||
|
||||
CaptureTool* ImgurUploaderTool::copy(QObject* parent)
|
||||
{
|
||||
return new ImgurUploaderTool(parent);
|
||||
}
|
||||
|
||||
void ImgurUploaderTool::pressed(CaptureContext& context)
|
||||
{
|
||||
emit requestAction(REQ_CAPTURE_DONE_OK);
|
||||
context.request()->addTask(CaptureRequest::UPLOAD);
|
||||
emit requestAction(REQ_CLOSE_GUI);
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "circlecount/circlecounttool.h"
|
||||
#include "copy/copytool.h"
|
||||
#include "exit/exittool.h"
|
||||
#include "imgur/imguruploadertool.h"
|
||||
#include "imgupload/imguploadertool.h"
|
||||
#include "invert/inverttool.h"
|
||||
#include "launcher/applaunchertool.h"
|
||||
#include "line/linetool.h"
|
||||
@@ -24,7 +24,6 @@
|
||||
#include "sizedecrease/sizedecreasetool.h"
|
||||
#include "sizeincrease/sizeincreasetool.h"
|
||||
#include "sizeindicator/sizeindicatortool.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "text/texttool.h"
|
||||
#include "undo/undotool.h"
|
||||
|
||||
@@ -52,7 +51,7 @@ CaptureTool* ToolFactory::CreateTool(CaptureTool::Type t, QObject* parent)
|
||||
if_TYPE_return_TOOL(TYPE_COPY, CopyTool);
|
||||
if_TYPE_return_TOOL(TYPE_SAVE, SaveTool);
|
||||
if_TYPE_return_TOOL(TYPE_EXIT, ExitTool);
|
||||
if_TYPE_return_TOOL(TYPE_IMAGEUPLOADER, ImgurUploaderTool);
|
||||
if_TYPE_return_TOOL(TYPE_IMAGEUPLOADER, ImgUploaderTool);
|
||||
#if !defined(Q_OS_MACOS)
|
||||
if_TYPE_return_TOOL(TYPE_OPEN_APP, AppLauncher);
|
||||
#endif
|
||||
|
||||
@@ -13,7 +13,7 @@ target_sources(
|
||||
orientablepushbutton.h
|
||||
historywidget.h
|
||||
updatenotificationwidget.h
|
||||
imguruploaddialog.h
|
||||
imguploaddialog.h
|
||||
capture/capturetoolobjects.h
|
||||
)
|
||||
|
||||
@@ -28,6 +28,6 @@ target_sources(
|
||||
orientablepushbutton.cpp
|
||||
historywidget.cpp
|
||||
updatenotificationwidget.cpp
|
||||
imguruploaddialog.cpp
|
||||
imguploaddialog.cpp
|
||||
capture/capturetoolobjects.cpp
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "historywidget.h"
|
||||
#include "src/tools/imgupload/imguploadermanager.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/utils/globalvalues.h"
|
||||
#include "src/utils/history.h"
|
||||
@@ -18,6 +19,7 @@
|
||||
#include <QScrollArea>
|
||||
#include <QUrl>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
HistoryWidget::HistoryWidget(QWidget* parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
@@ -101,7 +103,7 @@ void HistoryWidget::addLine(const QString& path, const QString& fileName)
|
||||
History history;
|
||||
HISTORY_FILE_NAME unpackFileName = history.unpackFileName(fileName);
|
||||
|
||||
QString url = "https://imgur.com/" + unpackFileName.file;
|
||||
QString url = ImgUploaderManager(this).url() + unpackFileName.file;
|
||||
|
||||
// load pixmap
|
||||
QPixmap pixmap;
|
||||
@@ -170,9 +172,11 @@ void HistoryWidget::addLine(const QString& path, const QString& fileName)
|
||||
QMessageBox::Yes | QMessageBox::No)) {
|
||||
return;
|
||||
}
|
||||
QDesktopServices::openUrl(
|
||||
QUrl(QStringLiteral("https://imgur.com/delete/%1")
|
||||
.arg(unpackFileName.token)));
|
||||
|
||||
ImgUploaderBase* imgUploaderBase =
|
||||
ImgUploaderManager(this).uploader(unpackFileName.type);
|
||||
imgUploaderBase->deleteImage(unpackFileName.file, unpackFileName.token);
|
||||
|
||||
removeCacheFile(fullFileName);
|
||||
removeLayoutItem(phbl);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
|
||||
|
||||
#include "imguruploaddialog.h"
|
||||
#include "imguploaddialog.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/utils/globalvalues.h"
|
||||
#include <QCheckBox>
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <QLabel>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
ImgurUploadDialog::ImgurUploadDialog(QDialog* parent)
|
||||
ImgUploadDialog::ImgUploadDialog(QDialog* parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
@@ -19,8 +19,7 @@ ImgurUploadDialog::ImgurUploadDialog(QDialog* parent)
|
||||
|
||||
layout = new QVBoxLayout(this);
|
||||
|
||||
m_uploadLabel =
|
||||
new QLabel(tr("Do you want to upload this capture to Imgur?"), this);
|
||||
m_uploadLabel = new QLabel(tr("Do you want to upload this capture?"), this);
|
||||
|
||||
layout->addWidget(m_uploadLabel);
|
||||
|
||||
@@ -33,9 +32,8 @@ ImgurUploadDialog::ImgurUploadDialog(QDialog* parent)
|
||||
layout->addWidget(buttonBox);
|
||||
|
||||
m_uploadWithoutConfirmation =
|
||||
new QCheckBox(tr("Upload to Imgur without confirmation"), this);
|
||||
m_uploadWithoutConfirmation->setToolTip(
|
||||
tr("Upload to Imgur without confirmation"));
|
||||
new QCheckBox(tr("Upload without confirmation"), this);
|
||||
m_uploadWithoutConfirmation->setToolTip(tr("Upload without confirmation"));
|
||||
connect(m_uploadWithoutConfirmation, &QCheckBox::clicked, [](bool checked) {
|
||||
ConfigHandler().setUploadWithoutConfirmation(checked);
|
||||
});
|
||||
@@ -10,11 +10,11 @@ class QLabel;
|
||||
class QDialogButtonBox;
|
||||
class QVBoxLayout;
|
||||
|
||||
class ImgurUploadDialog : public QDialog
|
||||
class ImgUploadDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImgurUploadDialog(QDialog* parent = nullptr);
|
||||
explicit ImgUploadDialog(QDialog* parent = nullptr);
|
||||
|
||||
private:
|
||||
QCheckBox* m_uploadWithoutConfirmation;
|
||||
Reference in New Issue
Block a user