diff options
author | robotbrain <robotbrainify@gmail.com> | 2014-02-23 16:14:24 -0500 |
---|---|---|
committer | robotbrain <robotbrainify@gmail.com> | 2014-02-23 16:14:24 -0500 |
commit | 4a77524b059c12165e20b38de6c0d4ed08e56419 (patch) | |
tree | 72212e305886d4cf37a387bb816b3397a0e56e31 /logic | |
parent | 5cf599673db88b39100ffca78e10bbe5e10200d7 (diff) | |
download | MultiMC-4a77524b059c12165e20b38de6c0d4ed08e56419.tar MultiMC-4a77524b059c12165e20b38de6c0d4ed08e56419.tar.gz MultiMC-4a77524b059c12165e20b38de6c0d4ed08e56419.tar.lz MultiMC-4a77524b059c12165e20b38de6c0d4ed08e56419.tar.xz MultiMC-4a77524b059c12165e20b38de6c0d4ed08e56419.zip |
Initial stuff. It doesnt work.
Diffstat (limited to 'logic')
-rw-r--r-- | logic/lists/ScreenshotList.cpp | 81 | ||||
-rw-r--r-- | logic/lists/ScreenshotList.h | 75 | ||||
-rw-r--r-- | logic/net/ScreenshotUploader.cpp | 144 | ||||
-rw-r--r-- | logic/net/ScreenshotUploader.h | 52 | ||||
-rw-r--r-- | logic/net/URLConstants.h | 2 |
5 files changed, 354 insertions, 0 deletions
diff --git a/logic/lists/ScreenshotList.cpp b/logic/lists/ScreenshotList.cpp new file mode 100644 index 00000000..ff37a092 --- /dev/null +++ b/logic/lists/ScreenshotList.cpp @@ -0,0 +1,81 @@ +#include "ScreenshotList.h" +#include "QDir" +#include "QPixmap" + +ScreenshotList::ScreenshotList(BaseInstance *instance, QObject *parent) + : QAbstractListModel(parent), m_instance(instance) +{ +} + +int ScreenshotList::rowCount(const QModelIndex &) const +{ + return m_screenshots.size(); +} + +QVariant ScreenshotList::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + switch (role) + { + case Qt::DecorationRole: + { + QPixmap map; + map.loadFromData(m_screenshots.at(index.row())->file); + return map; + } + case Qt::DisplayRole: + return m_screenshots.at(index.row())->timestamp; + case Qt::ToolTipRole: + return m_screenshots.at(index.row())->timestamp; + case Qt::TextAlignmentRole: + return (int)(Qt::AlignHCenter | Qt::AlignVCenter); + default: + return QVariant(); + } +} + +QVariant ScreenshotList::headerData(int section, Qt::Orientation orientation, int role) const +{ + return QVariant(); +} + +Qt::ItemFlags ScreenshotList::flags(const QModelIndex &index) const +{ + return Qt::NoItemFlags; +} + +Task *ScreenshotList::load() +{ + return new ScreenshotLoadTask(this); +} + +ScreenshotLoadTask::ScreenshotLoadTask(ScreenshotList *list) : m_list(list) +{ +} + +ScreenshotLoadTask::~ScreenshotLoadTask() +{ +} + +void ScreenshotLoadTask::executeTask() +{ + auto dir = QDir(m_list->instance()->minecraftRoot()); + if (!dir.cd("screenshots")) + { + emitFailed("Selected instance does not have any screenshots!"); + return; + } + dir.setNameFilters(QStringList() << "*.png"); + this->m_results = QList<ScreenShot *>(); + for (auto file : dir.entryList()) + { + ScreenShot *shot = new ScreenShot(); + shot->timestamp = file.left(file.length() - 4); + shot->file = QFile(file).readAll(); + m_results.append(shot); + } + m_list->loadShots(m_results); + emitSucceeded(); +} diff --git a/logic/lists/ScreenshotList.h b/logic/lists/ScreenshotList.h new file mode 100644 index 00000000..4011c1bd --- /dev/null +++ b/logic/lists/ScreenshotList.h @@ -0,0 +1,75 @@ +#pragma once + +#include <QAbstractListModel> +#include "logic/BaseInstance.h" +#include "logic/tasks/Task.h" + +class ScreenShot +{ +public: + QString timestamp; + QByteArray file; + int imgurIndex; + QString url; +}; + +class ScreenshotList : public QAbstractListModel +{ + Q_OBJECT +public: + ScreenshotList(BaseInstance *instance, QObject *parent = 0); + + QVariant data(const QModelIndex &index, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + + int rowCount(const QModelIndex &parent) const; + + Qt::ItemFlags flags(const QModelIndex &index) const; + + Task *load(); + + void loadShots(QList<ScreenShot *> shots) + { + m_screenshots = shots; + } + + QList<ScreenShot *> screenshots() const + { + return m_screenshots; + } + + BaseInstance *instance() const + { + return m_instance; + } + +signals: + +public +slots: + +private: + QList<ScreenShot *> m_screenshots; + BaseInstance *m_instance; +}; + +class ScreenshotLoadTask : public Task +{ + Q_OBJECT + +public: + explicit ScreenshotLoadTask(ScreenshotList *list); + ~ScreenshotLoadTask(); + + QList<ScreenShot *> screenShots() const + { + return m_results; + } + +protected: + virtual void executeTask(); + +private: + ScreenshotList *m_list; + QList<ScreenShot *> m_results; +}; diff --git a/logic/net/ScreenshotUploader.cpp b/logic/net/ScreenshotUploader.cpp new file mode 100644 index 00000000..03ea1cb4 --- /dev/null +++ b/logic/net/ScreenshotUploader.cpp @@ -0,0 +1,144 @@ +#include "ScreenshotUploader.h" +#include "logic/lists/ScreenshotList.h" +#include <QNetworkRequest> +#include <QJsonObject> +#include <QJsonDocument> +#include "URLConstants.h" +#include "MultiMC.h" + +ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : m_shot(shot) +{ + m_status = Job_NotStarted; +} + +void ScreenShotUpload::start() +{ + m_status = Job_InProgress; + QNetworkRequest request(URLConstants::IMGUR_UPLOAD_URL); + request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); + request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); + + 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); + + auto worker = MMC->qnam(); + QNetworkReply *rep = worker->post(request, doc.toJson()); + + 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 ScreenShotUpload::downloadError(QNetworkReply::NetworkError error) +{ + m_status = Job_Failed; +} +void ScreenShotUpload::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) + { + emit failed(m_index_within_job); + return; + } + auto object = doc.object(); + if (!object.value("success").toBool()) + { + emit failed(m_index_within_job); + return; + } + m_shot->imgurIndex = object.value("data").toVariant().toInt(); + m_status = Job_Finished; + emit succeeded(m_index_within_job); + return; + } + else + { + m_reply.reset(); + emit failed(m_index_within_job); + return; + } +} +void ScreenShotUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + m_total_progress = bytesTotal; + m_progress = bytesReceived; + emit progress(m_index_within_job, bytesReceived, bytesTotal); +} +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 new file mode 100644 index 00000000..09e58677 --- /dev/null +++ b/logic/net/ScreenshotUploader.h @@ -0,0 +1,52 @@ +#pragma once +#include "NetAction.h" + +class ScreenShot; +typedef std::shared_ptr<class ScreenShotUpload> ScreenShotUploadPtr; +typedef std::shared_ptr<class ScreenShotGet> ScreenShotGetPtr; +class ScreenShotUpload : public NetAction +{ +public: + explicit ScreenShotUpload(ScreenShot *shot); + static ScreenShotUploadPtr make(ScreenShot *shot) + { + return ScreenShotUploadPtr(new ScreenShotUpload(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; +}; +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 8cb1f3fd..3e21d21e 100644 --- a/logic/net/URLConstants.h +++ b/logic/net/URLConstants.h @@ -33,4 +33,6 @@ 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/"); } |