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("url + "\">Image %1") - .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 ScreenshotDialog::uploaded() const +QString ScreenshotDialog::message() const { - return m_uploaded; + return tr("Visit album
Delete hash: %2 (save " + "this if you want to be able to edit/delete the album)") + .arg(m_imgurAlbum->id(), m_imgurAlbum->deleteHash()); } -QList ScreenshotDialog::selected() const +QList ScreenshotDialog::selected() const { - QList list; - QList first = m_list->screenshots(); + QList list; + QList first = m_list->screenshots(); for (QModelIndex index : ui->listView->selectionModel()->selectedRows()) { list.append(first.at(index.row())); @@ -41,20 +43,24 @@ QList ScreenshotDialog::selected() const void ScreenshotDialog::on_uploadBtn_clicked() { - QList 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(job)); + task->addTask(std::shared_ptr(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 #include "logic/lists/ScreenshotList.h" -class BaseInstance; +class ImgurAlbumCreation; namespace Ui { @@ -23,7 +23,7 @@ public: NothingDone = 0x42 }; - QList uploaded() const; + QString message() const; private slots: @@ -33,6 +33,7 @@ private: Ui::ScreenshotDialog *ui; ScreenshotList *m_list; QList m_uploaded; + std::shared_ptr m_imgurAlbum; QList 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 +#include +#include +#include + +#include "logic/lists/ScreenshotList.h" +#include "URLConstants.h" +#include "MultiMC.h" +#include "logger/QsLog.h" + +ImgurAlbumCreation::ImgurAlbumCreation(QList 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(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 ImgurAlbumCreationPtr; +class ImgurAlbumCreation : public NetAction +{ +public: + explicit ImgurAlbumCreation(QList screenshots); + static ImgurAlbumCreationPtr make(QList 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 m_screenshots; + + QString m_deleteHash; + QString m_id; +}; diff --git a/logic/net/ScreenshotUploader.cpp b/logic/net/ImgurUpload.cpp similarity index 80% rename from logic/net/ScreenshotUploader.cpp rename to 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 #include @@ -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(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 similarity index 53% rename from logic/net/ScreenshotUploader.h rename to 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 ScreenShotUploadPtr; -typedef std::shared_ptr ScreenShotGetPtr; -class ScreenShotUpload : public NetAction +typedef std::shared_ptr 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) +void SequentialTask::addTask(std::shared_ptr task) { m_queue.append(task); } @@ -43,7 +43,7 @@ void SequentialTask::startNext() { if (m_currentIndex != -1) { - std::shared_ptr previous = m_queue[m_currentIndex]; + std::shared_ptr 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 next = m_queue[m_currentIndex]; + std::shared_ptr 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); + void addTask(std::shared_ptr task); protected: void executeTask(); @@ -27,6 +27,6 @@ slots: void subTaskProgress(); private: - QQueue > m_queue; + QQueue > m_queue; int m_currentIndex; };