Imgur album creation

This commit is contained in:
Jan Dalheimer 2014-02-24 11:30:27 +01:00
parent a8811a27f7
commit da33fa4090
13 changed files with 193 additions and 52 deletions

View File

@ -372,8 +372,10 @@ logic/net/HttpMetaCache.cpp
logic/net/PasteUpload.h
logic/net/PasteUpload.cpp
logic/net/URLConstants.h
logic/net/ScreenshotUploader.h
logic/net/ScreenshotUploader.cpp
logic/net/ImgurUpload.h
logic/net/ImgurUpload.cpp
logic/net/ImgurAlbumCreation.h
logic/net/ImgurAlbumCreation.cpp
# Yggdrasil login stuff
logic/auth/AuthSession.h

View File

@ -81,7 +81,6 @@
#include "logic/net/URLConstants.h"
#include "logic/net/NetJob.h"
#include "logic/net/ScreenshotUploader.h"
#include "logic/BaseInstance.h"
#include "logic/InstanceFactory.h"
@ -1519,13 +1518,7 @@ void MainWindow::on_actionScreenshots_triggered()
ScreenshotDialog dialog(list, this);
if (dialog.exec() == ScreenshotDialog::Accepted)
{
QStringList urls;
for (ScreenShot *shot : dialog.uploaded())
{
urls << QString("<a href=\"" + shot->url + "\">Image %1</a>")
.arg(shot->timestamp.toString());
}
CustomMessageBox::selectable(this, tr("Done uploading!"), urls.join("\n"),
CustomMessageBox::selectable(this, tr("Done uploading!"), dialog.message(),
QMessageBox::Information)->exec();
}
}

View File

@ -28,6 +28,7 @@ QMessageBox *selectable(QWidget *parent, const QString &title, const QString &te
messageBox->setDefaultButton(defaultButton);
messageBox->setTextInteractionFlags(Qt::TextSelectableByMouse);
messageBox->setIcon(icon);
messageBox->setTextInteractionFlags(Qt::TextBrowserInteraction);
return messageBox;
}

View File

@ -7,12 +7,12 @@
#include "ProgressDialog.h"
#include "CustomMessageBox.h"
#include "logic/net/NetJob.h"
#include "logic/net/ScreenshotUploader.h"
#include "logic/net/ImgurUpload.h"
#include "logic/net/ImgurAlbumCreation.h"
#include "logic/tasks/SequentialTask.h"
ScreenshotDialog::ScreenshotDialog(ScreenshotList *list, QWidget *parent) :
QDialog(parent),
ui(new Ui::ScreenshotDialog),
m_list(list)
ScreenshotDialog::ScreenshotDialog(ScreenshotList *list, QWidget *parent)
: QDialog(parent), ui(new Ui::ScreenshotDialog), m_list(list)
{
ui->setupUi(this);
ui->listView->setModel(m_list);
@ -23,15 +23,17 @@ ScreenshotDialog::~ScreenshotDialog()
delete ui;
}
QList<ScreenShot *> ScreenshotDialog::uploaded() const
QString ScreenshotDialog::message() const
{
return m_uploaded;
return tr("<a href=\"https://imgur.com/a/%1\">Visit album</a><br/>Delete hash: %2 (save "
"this if you want to be able to edit/delete the album)")
.arg(m_imgurAlbum->id(), m_imgurAlbum->deleteHash());
}
QList<ScreenShot*> ScreenshotDialog::selected() const
QList<ScreenShot *> ScreenshotDialog::selected() const
{
QList<ScreenShot*> list;
QList<ScreenShot*> first = m_list->screenshots();
QList<ScreenShot *> list;
QList<ScreenShot *> first = m_list->screenshots();
for (QModelIndex index : ui->listView->selectionModel()->selectedRows())
{
list.append(first.at(index.row()));
@ -41,20 +43,24 @@ QList<ScreenShot*> ScreenshotDialog::selected() const
void ScreenshotDialog::on_uploadBtn_clicked()
{
QList<ScreenShot *> screenshots = selected();
if (screenshots.isEmpty())
m_uploaded = selected();
if (m_uploaded.isEmpty())
{
done(NothingDone);
return;
}
SequentialTask *task = new SequentialTask(this);
NetJob *job = new NetJob("Screenshot Upload");
for (ScreenShot *shot : screenshots)
for (ScreenShot *shot : m_uploaded)
{
job->addNetAction(ScreenShotUpload::make(shot));
job->addNetAction(ImgurUpload::make(shot));
}
m_uploaded = screenshots;
NetJob *albumTask = new NetJob("Imgur Album Creation");
albumTask->addNetAction(m_imgurAlbum = ImgurAlbumCreation::make(m_uploaded));
task->addTask(std::shared_ptr<NetJob>(job));
task->addTask(std::shared_ptr<NetJob>(albumTask));
ProgressDialog prog(this);
if (prog.exec(job) == QDialog::Accepted)
if (prog.exec(task) == QDialog::Accepted)
{
accept();
}

View File

@ -3,7 +3,7 @@
#include <QDialog>
#include "logic/lists/ScreenshotList.h"
class BaseInstance;
class ImgurAlbumCreation;
namespace Ui
{
@ -23,7 +23,7 @@ public:
NothingDone = 0x42
};
QList<ScreenShot *> uploaded() const;
QString message() const;
private
slots:
@ -33,6 +33,7 @@ private:
Ui::ScreenshotDialog *ui;
ScreenshotList *m_list;
QList<ScreenShot *> m_uploaded;
std::shared_ptr<ImgurAlbumCreation> m_imgurAlbum;
QList<ScreenShot *> selected() const;
};

View File

@ -10,7 +10,7 @@ public:
QDateTime timestamp;
QString file;
QString url;
QString imgurIndex;
QString imgurId;
};
class ScreenshotList : public QAbstractListModel

View File

@ -0,0 +1,90 @@
#include "ImgurAlbumCreation.h"
#include <QNetworkRequest>
#include <QJsonDocument>
#include <QJsonObject>
#include <QUrl>
#include "logic/lists/ScreenshotList.h"
#include "URLConstants.h"
#include "MultiMC.h"
#include "logger/QsLog.h"
ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot *> screenshots) : NetAction(), m_screenshots(screenshots)
{
m_url = URLConstants::IMGUR_BASE_URL + "album.json";
m_status = Job_NotStarted;
}
void ImgurAlbumCreation::start()
{
m_status = Job_InProgress;
QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3");
request.setRawHeader("Accept", "application/json");
QStringList ids;
for (auto shot : m_screenshots)
{
ids.append(shot->imgurId);
}
const QByteArray data = "ids=" + ids.join(',').toUtf8() + "&title=Minecraft%20Screenshots&privacy=hidden";
auto worker = MMC->qnam();
QNetworkReply *rep = worker->post(request, data);
m_reply = std::shared_ptr<QNetworkReply>(rep);
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurAlbumCreation::downloadProgress);
connect(rep, &QNetworkReply::finished, this, &ImgurAlbumCreation::downloadFinished);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
SLOT(downloadError(QNetworkReply::NetworkError)));
}
void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error)
{
QLOG_DEBUG() << m_reply->errorString();
m_status = Job_Failed;
}
void ImgurAlbumCreation::downloadFinished()
{
if (m_status != Job_Failed)
{
QByteArray data = m_reply->readAll();
m_reply.reset();
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
QLOG_DEBUG() << jsonError.errorString();
emit failed(m_index_within_job);
return;
}
auto object = doc.object();
if (!object.value("success").toBool())
{
QLOG_DEBUG() << doc.toJson();
emit failed(m_index_within_job);
return;
}
m_deleteHash = object.value("data").toObject().value("deletehash").toString();
m_id = object.value("data").toObject().value("id").toString();
m_status = Job_Finished;
emit succeeded(m_index_within_job);
return;
}
else
{
QLOG_DEBUG() << m_reply->readAll();
m_reply.reset();
emit failed(m_index_within_job);
return;
}
}
void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
m_total_progress = bytesTotal;
m_progress = bytesReceived;
emit progress(m_index_within_job, bytesReceived, bytesTotal);
}

View File

@ -0,0 +1,42 @@
#pragma once
#include "NetAction.h"
class ScreenShot;
typedef std::shared_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr;
class ImgurAlbumCreation : public NetAction
{
public:
explicit ImgurAlbumCreation(QList<ScreenShot *> screenshots);
static ImgurAlbumCreationPtr make(QList<ScreenShot *> screenshots)
{
return ImgurAlbumCreationPtr(new ImgurAlbumCreation(screenshots));
}
QString deleteHash() const
{
return m_deleteHash;
}
QString id() const
{
return m_id;
}
protected
slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
virtual void downloadError(QNetworkReply::NetworkError error);
virtual void downloadFinished();
virtual void downloadReadyRead()
{
}
public
slots:
virtual void start();
private:
QList<ScreenShot *> m_screenshots;
QString m_deleteHash;
QString m_id;
};

View File

@ -1,4 +1,4 @@
#include "ScreenshotUploader.h"
#include "ImgurUpload.h"
#include <QNetworkRequest>
#include <QHttpMultiPart>
@ -13,13 +13,13 @@
#include "MultiMC.h"
#include "logger/QsLog.h"
ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : NetAction(), m_shot(shot)
ImgurUpload::ImgurUpload(ScreenShot *shot) : NetAction(), m_shot(shot)
{
m_url = URLConstants::IMGUR_UPLOAD_URL;
m_url = URLConstants::IMGUR_BASE_URL + "upload.json";
m_status = Job_NotStarted;
}
void ScreenShotUpload::start()
void ImgurUpload::start()
{
m_status = Job_InProgress;
QNetworkRequest request(m_url);
@ -53,17 +53,17 @@ void ScreenShotUpload::start()
QNetworkReply *rep = worker->post(request, multipart);
m_reply = std::shared_ptr<QNetworkReply>(rep);
connect(rep, &QNetworkReply::uploadProgress, this, &ScreenShotUpload::downloadProgress);
connect(rep, &QNetworkReply::finished, this, &ScreenShotUpload::downloadFinished);
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurUpload::downloadProgress);
connect(rep, &QNetworkReply::finished, this, &ImgurUpload::downloadFinished);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
SLOT(downloadError(QNetworkReply::NetworkError)));
}
void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error)
void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
{
QLOG_DEBUG() << m_reply->errorString();
m_status = Job_Failed;
}
void ScreenShotUpload::downloadFinished()
void ImgurUpload::downloadFinished()
{
if (m_status != Job_Failed)
{
@ -84,7 +84,7 @@ void ScreenShotUpload::downloadFinished()
emit failed(m_index_within_job);
return;
}
m_shot->imgurIndex = object.value("data").toObject().value("id").toString();
m_shot->imgurId = object.value("data").toObject().value("id").toString();
m_shot->url = object.value("data").toObject().value("link").toString();
m_status = Job_Finished;
emit succeeded(m_index_within_job);
@ -98,7 +98,7 @@ void ScreenShotUpload::downloadFinished()
return;
}
}
void ScreenShotUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
m_total_progress = bytesTotal;
m_progress = bytesReceived;

View File

@ -2,15 +2,14 @@
#include "NetAction.h"
class ScreenShot;
typedef std::shared_ptr<class ScreenShotUpload> ScreenShotUploadPtr;
typedef std::shared_ptr<class ScreenShotGet> ScreenShotGetPtr;
class ScreenShotUpload : public NetAction
typedef std::shared_ptr<class ImgurUpload> ImgurUploadPtr;
class ImgurUpload : public NetAction
{
public:
explicit ScreenShotUpload(ScreenShot *shot);
static ScreenShotUploadPtr make(ScreenShot *shot)
explicit ImgurUpload(ScreenShot *shot);
static ImgurUploadPtr make(ScreenShot *shot)
{
return ScreenShotUploadPtr(new ScreenShotUpload(shot));
return ImgurUploadPtr(new ImgurUpload(shot));
}
protected

View File

@ -33,5 +33,5 @@ const QString FORGE_LEGACY_URL("http://files.minecraftforge.net/minecraftforge/j
const QString FORGE_GRADLE_URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/json");
const QString MOJANG_STATUS_URL("http://status.mojang.com/check");
const QString MOJANG_STATUS_NEWS_URL("http://status.mojang.com/news");
const QString IMGUR_UPLOAD_URL("https://api.imgur.com/3/upload.json");
const QString IMGUR_BASE_URL("https://api.imgur.com/3/");
}

View File

@ -28,7 +28,7 @@ void SequentialTask::getProgress(qint64 &current, qint64 &total)
}
}
void SequentialTask::addTask(std::shared_ptr<Task> task)
void SequentialTask::addTask(std::shared_ptr<ProgressProvider> task)
{
m_queue.append(task);
}
@ -43,7 +43,7 @@ void SequentialTask::startNext()
{
if (m_currentIndex != -1)
{
std::shared_ptr<Task> previous = m_queue[m_currentIndex];
std::shared_ptr<ProgressProvider> previous = m_queue[m_currentIndex];
disconnect(previous.get(), 0, this, 0);
}
m_currentIndex++;
@ -52,7 +52,7 @@ void SequentialTask::startNext()
emitSucceeded();
return;
}
std::shared_ptr<Task> next = m_queue[m_currentIndex];
std::shared_ptr<ProgressProvider> next = m_queue[m_currentIndex];
connect(next.get(), SIGNAL(failed(QString)), this, SLOT(subTaskFailed(QString)));
connect(next.get(), SIGNAL(status(QString)), this, SLOT(subTaskStatus(QString)));
connect(next.get(), SIGNAL(progress(qint64,qint64)), this, SLOT(subTaskProgress()));
@ -73,5 +73,12 @@ void SequentialTask::subTaskProgress()
{
qint64 current, total;
getProgress(current, total);
setProgress(100 * current / total);
if (total == 0)
{
setProgress(0);
}
else
{
setProgress(100 * current / total);
}
}

View File

@ -14,7 +14,7 @@ public:
virtual QString getStatus() const;
virtual void getProgress(qint64 &current, qint64 &total);
void addTask(std::shared_ptr<Task> task);
void addTask(std::shared_ptr<ProgressProvider> task);
protected:
void executeTask();
@ -27,6 +27,6 @@ slots:
void subTaskProgress();
private:
QQueue<std::shared_ptr<Task> > m_queue;
QQueue<std::shared_ptr<ProgressProvider> > m_queue;
int m_currentIndex;
};