summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Dalheimer <jan@dalheimer.de>2014-02-24 11:30:27 +0100
committerJan Dalheimer <jan@dalheimer.de>2014-02-24 11:30:27 +0100
commitda33fa4090b4510267d06b3fd3ec439ff563b80e (patch)
tree31e167a82193b607c57c1c94b05ff22d14f4b44d
parenta8811a27f78698f1ab2acad47e90f3b16274e221 (diff)
downloadMultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar
MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.gz
MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.lz
MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.xz
MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.zip
Imgur album creation
-rw-r--r--CMakeLists.txt6
-rw-r--r--gui/MainWindow.cpp9
-rw-r--r--gui/dialogs/CustomMessageBox.cpp1
-rw-r--r--gui/dialogs/ScreenshotDialog.cpp38
-rw-r--r--gui/dialogs/ScreenshotDialog.h5
-rw-r--r--logic/lists/ScreenshotList.h2
-rw-r--r--logic/net/ImgurAlbumCreation.cpp90
-rw-r--r--logic/net/ImgurAlbumCreation.h42
-rw-r--r--logic/net/ImgurUpload.cpp (renamed from logic/net/ScreenshotUploader.cpp)20
-rw-r--r--logic/net/ImgurUpload.h (renamed from logic/net/ScreenshotUploader.h)11
-rw-r--r--logic/net/URLConstants.h2
-rw-r--r--logic/tasks/SequentialTask.cpp15
-rw-r--r--logic/tasks/SequentialTask.h4
13 files changed, 193 insertions, 52 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 048a466a..12a673e3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp
index 93608a0e..559c2f48 100644
--- a/gui/MainWindow.cpp
+++ b/gui/MainWindow.cpp
@@ -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();
}
}
diff --git a/gui/dialogs/CustomMessageBox.cpp b/gui/dialogs/CustomMessageBox.cpp
index 1d2ab58a..4013db60 100644
--- a/gui/dialogs/CustomMessageBox.cpp
+++ b/gui/dialogs/CustomMessageBox.cpp
@@ -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;
}
diff --git a/gui/dialogs/ScreenshotDialog.cpp b/gui/dialogs/ScreenshotDialog.cpp
index 33e93162..3b4af5e5 100644
--- a/gui/dialogs/ScreenshotDialog.cpp
+++ b/gui/dialogs/ScreenshotDialog.cpp
@@ -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();
}
diff --git a/gui/dialogs/ScreenshotDialog.h b/gui/dialogs/ScreenshotDialog.h
index 104814f1..ac1494d6 100644
--- a/gui/dialogs/ScreenshotDialog.h
+++ b/gui/dialogs/ScreenshotDialog.h
@@ -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;
};
diff --git a/logic/lists/ScreenshotList.h b/logic/lists/ScreenshotList.h
index dee74807..1d3e73ca 100644
--- a/logic/lists/ScreenshotList.h
+++ b/logic/lists/ScreenshotList.h
@@ -10,7 +10,7 @@ public:
QDateTime timestamp;
QString file;
QString url;
- QString imgurIndex;
+ QString imgurId;
};
class ScreenshotList : public QAbstractListModel
diff --git a/logic/net/ImgurAlbumCreation.cpp b/logic/net/ImgurAlbumCreation.cpp
new file mode 100644
index 00000000..0d8c4dc1
--- /dev/null
+++ b/logic/net/ImgurAlbumCreation.cpp
@@ -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);
+}
diff --git a/logic/net/ImgurAlbumCreation.h b/logic/net/ImgurAlbumCreation.h
new file mode 100644
index 00000000..7be255df
--- /dev/null
+++ b/logic/net/ImgurAlbumCreation.h
@@ -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;
+};
diff --git a/logic/net/ScreenshotUploader.cpp b/logic/net/ImgurUpload.cpp
index 490c74b3..4992ee65 100644
--- a/logic/net/ScreenshotUploader.cpp
+++ b/logic/net/ImgurUpload.cpp
@@ -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;
diff --git a/logic/net/ScreenshotUploader.h b/logic/net/ImgurUpload.h
index c1c9db6f..e5e79587 100644
--- a/logic/net/ScreenshotUploader.h
+++ b/logic/net/ImgurUpload.h
@@ -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
diff --git a/logic/net/URLConstants.h b/logic/net/URLConstants.h
index f2a943bf..55c8d527 100644
--- a/logic/net/URLConstants.h
+++ b/logic/net/URLConstants.h
@@ -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/");
}
diff --git a/logic/tasks/SequentialTask.cpp b/logic/tasks/SequentialTask.cpp
index 63025eee..e0f8fcdd 100644
--- a/logic/tasks/SequentialTask.cpp
+++ b/logic/tasks/SequentialTask.cpp
@@ -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);
+ }
}
diff --git a/logic/tasks/SequentialTask.h b/logic/tasks/SequentialTask.h
index 7f046928..c405dca3 100644
--- a/logic/tasks/SequentialTask.h
+++ b/logic/tasks/SequentialTask.h
@@ -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;
};