diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2014-02-24 11:30:27 +0100 |
---|---|---|
committer | Jan Dalheimer <jan@dalheimer.de> | 2014-02-24 11:30:27 +0100 |
commit | da33fa4090b4510267d06b3fd3ec439ff563b80e (patch) | |
tree | 31e167a82193b607c57c1c94b05ff22d14f4b44d /logic | |
parent | a8811a27f78698f1ab2acad47e90f3b16274e221 (diff) | |
download | MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.gz MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.lz MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.tar.xz MultiMC-da33fa4090b4510267d06b3fd3ec439ff563b80e.zip |
Imgur album creation
Diffstat (limited to 'logic')
-rw-r--r-- | logic/lists/ScreenshotList.h | 2 | ||||
-rw-r--r-- | logic/net/ImgurAlbumCreation.cpp | 90 | ||||
-rw-r--r-- | logic/net/ImgurAlbumCreation.h | 42 | ||||
-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.h | 2 | ||||
-rw-r--r-- | logic/tasks/SequentialTask.cpp | 15 | ||||
-rw-r--r-- | logic/tasks/SequentialTask.h | 4 |
8 files changed, 162 insertions, 24 deletions
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 ¤t, 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 ¤t, 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; }; |