summaryrefslogtreecommitdiffstats
path: root/logic
diff options
context:
space:
mode:
authorrobotbrain <robotbrainify@gmail.com>2014-02-23 19:45:59 -0500
committerrobotbrain <robotbrainify@gmail.com>2014-02-23 19:48:00 -0500
commit5e33da258c3b5159dba854eb4792d0852a1ca363 (patch)
tree62f8819b0974c415e883d43eefb0ffb2fff73702 /logic
parent4a77524b059c12165e20b38de6c0d4ed08e56419 (diff)
downloadMultiMC-5e33da258c3b5159dba854eb4792d0852a1ca363.tar
MultiMC-5e33da258c3b5159dba854eb4792d0852a1ca363.tar.gz
MultiMC-5e33da258c3b5159dba854eb4792d0852a1ca363.tar.lz
MultiMC-5e33da258c3b5159dba854eb4792d0852a1ca363.tar.xz
MultiMC-5e33da258c3b5159dba854eb4792d0852a1ca363.zip
Close to finished. Need to fix the upload part. Viewing works (in grayscale)
Diffstat (limited to 'logic')
-rw-r--r--logic/lists/ScreenshotList.cpp12
-rw-r--r--logic/lists/ScreenshotList.h4
-rw-r--r--logic/net/ScreenshotUploader.cpp101
-rw-r--r--logic/net/ScreenshotUploader.h23
-rw-r--r--logic/net/URLConstants.h3
5 files changed, 37 insertions, 106 deletions
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 <QNetworkRequest>
-#include <QJsonObject>
+#include <QHttpMultiPart>
#include <QJsonDocument>
+#include <QJsonObject>
+#include <QHttpPart>
+#include <QFile>
+#include <QUrl>
#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<QNetworkReply>(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<QNetworkReply>(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");
}