From 5e33da258c3b5159dba854eb4792d0852a1ca363 Mon Sep 17 00:00:00 2001 From: robotbrain Date: Sun, 23 Feb 2014 19:45:59 -0500 Subject: [PATCH] Close to finished. Need to fix the upload part. Viewing works (in grayscale) --- gui/MainWindow.cpp | 19 ------ gui/MainWindow.h | 2 - gui/dialogs/ScreenshotDialog.cpp | 2 +- gui/dialogs/ScreenshotDialog.ui | 21 +++++-- logic/lists/ScreenshotList.cpp | 12 ++-- logic/lists/ScreenshotList.h | 4 +- logic/net/ScreenshotUploader.cpp | 101 +++++++++---------------------- logic/net/ScreenshotUploader.h | 23 ------- logic/net/URLConstants.h | 3 +- 9 files changed, 53 insertions(+), 134 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 5a22f678..87c65601 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -1538,25 +1538,6 @@ void MainWindow::on_actionScreenshots_triggered() void MainWindow::screenshotsUploaded(QList screenshots) { - NetJob *job2 = new NetJob("Screenshot Data"); - for (ScreenShot *shot : screenshots) - { - job2->addNetAction(ScreenShotGet::make(shot)); - } - ProgressDialog prog3(this); - prog3.exec(job2); - connect(job2, &NetJob::failed, [this] - { - CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), - tr("Unknown error"), QMessageBox::Warning)->exec(); - }); - connect(job2, &NetJob::succeeded, [this, screenshots] - { screenShotsGotten(screenshots); }); -} - -void MainWindow::screenShotsGotten(QList screenshots) -{ - QStringList urls; for (ScreenShot *shot : screenshots) { diff --git a/gui/MainWindow.h b/gui/MainWindow.h index b57afcf5..0a474ef1 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -172,8 +172,6 @@ slots: void screenshotsUploaded(QList screenshots); - void screenShotsGotten(QList screenshots); - /*! * Runs the DownloadUpdateTask and installs updates. */ diff --git a/gui/dialogs/ScreenshotDialog.cpp b/gui/dialogs/ScreenshotDialog.cpp index 662c9e84..8900f15b 100644 --- a/gui/dialogs/ScreenshotDialog.cpp +++ b/gui/dialogs/ScreenshotDialog.cpp @@ -20,7 +20,7 @@ QList ScreenshotDialog::selected() { QList list; QList first = m_list->screenshots(); - for (QModelIndex index : ui->listView->selectionModel()->selectedIndexes()) + for (QModelIndex index : ui->listView->selectionModel()->selectedRows()) { list.append(first.at(index.row())); } diff --git a/gui/dialogs/ScreenshotDialog.ui b/gui/dialogs/ScreenshotDialog.ui index c912dffe..af73e5ec 100644 --- a/gui/dialogs/ScreenshotDialog.ui +++ b/gui/dialogs/ScreenshotDialog.ui @@ -11,7 +11,11 @@ - Dialog + Screenshots + + + + :/icons/multimc/scalable/apps/multimc.svg:/icons/multimc/scalable/apps/multimc.svg @@ -36,12 +40,15 @@ - QAbstractItemView::MultiSelection + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::SelectItems - 480 - 360 + 120 + 90 @@ -51,11 +58,13 @@ true - QListView::ListMode + QListView::IconMode - + + + buttonBox diff --git a/logic/lists/ScreenshotList.cpp b/logic/lists/ScreenshotList.cpp index ff37a092..e955f121 100644 --- a/logic/lists/ScreenshotList.cpp +++ b/logic/lists/ScreenshotList.cpp @@ -1,6 +1,6 @@ #include "ScreenshotList.h" #include "QDir" -#include "QPixmap" +#include "QIcon" ScreenshotList::ScreenshotList(BaseInstance *instance, QObject *parent) : QAbstractListModel(parent), m_instance(instance) @@ -20,11 +20,7 @@ QVariant ScreenshotList::data(const QModelIndex &index, int role) const switch (role) { case Qt::DecorationRole: - { - QPixmap map; - map.loadFromData(m_screenshots.at(index.row())->file); - return map; - } + return QIcon(m_screenshots.at(index.row())->file); case Qt::DisplayRole: return m_screenshots.at(index.row())->timestamp; case Qt::ToolTipRole: @@ -43,7 +39,7 @@ QVariant ScreenshotList::headerData(int section, Qt::Orientation orientation, in Qt::ItemFlags ScreenshotList::flags(const QModelIndex &index) const { - return Qt::NoItemFlags; + return Qt::ItemIsSelectable; } Task *ScreenshotList::load() @@ -73,7 +69,7 @@ void ScreenshotLoadTask::executeTask() { ScreenShot *shot = new ScreenShot(); shot->timestamp = file.left(file.length() - 4); - shot->file = QFile(file).readAll(); + shot->file = dir.absoluteFilePath(file); m_results.append(shot); } m_list->loadShots(m_results); diff --git a/logic/lists/ScreenshotList.h b/logic/lists/ScreenshotList.h index 4011c1bd..08c968f1 100644 --- a/logic/lists/ScreenshotList.h +++ b/logic/lists/ScreenshotList.h @@ -8,9 +8,9 @@ class ScreenShot { public: QString timestamp; - QByteArray file; - int imgurIndex; + QString file; QString url; + int imgurIndex; }; class ScreenshotList : public QAbstractListModel diff --git a/logic/net/ScreenshotUploader.cpp b/logic/net/ScreenshotUploader.cpp index 03ea1cb4..c1f62243 100644 --- a/logic/net/ScreenshotUploader.cpp +++ b/logic/net/ScreenshotUploader.cpp @@ -1,32 +1,48 @@ #include "ScreenshotUploader.h" #include "logic/lists/ScreenshotList.h" #include -#include +#include #include +#include +#include +#include +#include #include "URLConstants.h" #include "MultiMC.h" +#include "logger/QsLog.h" ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : m_shot(shot) { + m_url = URLConstants::IMGUR_UPLOAD_URL; m_status = Job_NotStarted; } void ScreenShotUpload::start() { m_status = Job_InProgress; - QNetworkRequest request(URLConstants::IMGUR_UPLOAD_URL); + QNetworkRequest request(m_url); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); + request.setRawHeader("Accept", "application/json"); - QJsonObject object; - object.insert("image", QJsonValue::fromVariant(m_shot->file.toBase64())); - object.insert("type", QJsonValue::fromVariant("base64")); - object.insert("name", QJsonValue::fromVariant(m_shot->timestamp)); - QJsonDocument doc; - doc.setObject(object); + QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType); + QHttpPart filePart; + filePart.setBody(QFile(m_shot->file).readAll().toBase64()); + filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png"); + filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"image\""); + multipart->append(filePart); + QHttpPart typePart; + typePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"type\""); + typePart.setBody("base64"); + multipart->append(typePart); + QHttpPart namePart; + namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\""); + namePart.setBody(m_shot->timestamp.toUtf8()); + multipart->append(namePart); auto worker = MMC->qnam(); - QNetworkReply *rep = worker->post(request, doc.toJson()); + QNetworkReply *rep = worker->post(request, multipart); m_reply = std::shared_ptr(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), @@ -38,6 +54,7 @@ void ScreenShotUpload::start() } void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error) { + QLOG_DEBUG() << m_reply->errorString(); m_status = Job_Failed; } void ScreenShotUpload::downloadFinished() @@ -50,16 +67,19 @@ void ScreenShotUpload::downloadFinished() 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_shot->imgurIndex = object.value("data").toVariant().toInt(); + m_shot->imgurIndex = object.value("data").toObject().value("id").toVariant().toInt(); + m_shot->url = "https://imgur.com/gallery/" + QString::number(m_shot->imgurIndex); m_status = Job_Finished; emit succeeded(m_index_within_job); return; @@ -81,64 +101,3 @@ void ScreenShotUpload::downloadReadyRead() { // noop } -ScreenShotGet::ScreenShotGet(ScreenShot *shot) : m_shot(shot) -{ - m_status = Job_NotStarted; -} -void ScreenShotGet::start() -{ - m_status = Job_InProgress; - QNetworkRequest request(URLConstants::IMGUR_GET_BASE + m_shot->imgurIndex + ".json"); - request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); - request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); - - auto worker = MMC->qnam(); - QNetworkReply *rep = worker->get(request); - - m_reply = std::shared_ptr(rep); - connect(rep, SIGNAL(downloadProgress(qint64, qint64)), - SLOT(downloadProgress(qint64, qint64))); - connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); - connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), - SLOT(downloadError(QNetworkReply::NetworkError))); - connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); -} -void ScreenShotGet::downloadError(QNetworkReply::NetworkError error) -{ - m_status = Job_Failed; -} -void ScreenShotGet::downloadFinished() -{ -} -void ScreenShotGet::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) -{ - m_total_progress = bytesTotal; - m_progress = bytesReceived; - emit progress(m_index_within_job, bytesReceived, bytesTotal); -} -void ScreenShotGet::downloadReadyRead() -{ - 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) - { - emit failed(m_index_within_job); - return; - } - auto object = doc.object(); - m_shot->url = object.value("link").toString(); - m_status = Job_Finished; - emit succeeded(m_index_within_job); - return; - } - else - { - m_reply.reset(); - emit failed(m_index_within_job); - return; - } -} diff --git a/logic/net/ScreenshotUploader.h b/logic/net/ScreenshotUploader.h index 09e58677..d5d1cef3 100644 --- a/logic/net/ScreenshotUploader.h +++ b/logic/net/ScreenshotUploader.h @@ -27,26 +27,3 @@ slots: private: ScreenShot *m_shot; }; -class ScreenShotGet : public NetAction -{ -public: - explicit ScreenShotGet(ScreenShot *shot); - static ScreenShotGetPtr make(ScreenShot *shot) - { - return ScreenShotGetPtr(new ScreenShotGet(shot)); - } - -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: - ScreenShot *m_shot; -}; diff --git a/logic/net/URLConstants.h b/logic/net/URLConstants.h index 3e21d21e..f2a943bf 100644 --- a/logic/net/URLConstants.h +++ b/logic/net/URLConstants.h @@ -33,6 +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/image.json"); -const QString IMGUR_GET_BASE("https://api.imgur.com/3/image/"); +const QString IMGUR_UPLOAD_URL("https://api.imgur.com/3/upload.json"); }