Close to finished. Need to fix the upload part. Viewing works (in grayscale)

This commit is contained in:
robotbrain 2014-02-23 19:45:59 -05:00
parent 4a77524b05
commit 5e33da258c
9 changed files with 53 additions and 134 deletions

View File

@ -1538,25 +1538,6 @@ void MainWindow::on_actionScreenshots_triggered()
void MainWindow::screenshotsUploaded(QList<ScreenShot *> screenshots) void MainWindow::screenshotsUploaded(QList<ScreenShot *> 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<ScreenShot *> screenshots)
{
QStringList urls; QStringList urls;
for (ScreenShot *shot : screenshots) for (ScreenShot *shot : screenshots)
{ {

View File

@ -172,8 +172,6 @@ slots:
void screenshotsUploaded(QList<class ScreenShot*> screenshots); void screenshotsUploaded(QList<class ScreenShot*> screenshots);
void screenShotsGotten(QList<class ScreenShot*> screenshots);
/*! /*!
* Runs the DownloadUpdateTask and installs updates. * Runs the DownloadUpdateTask and installs updates.
*/ */

View File

@ -20,7 +20,7 @@ QList<ScreenShot*> ScreenshotDialog::selected()
{ {
QList<ScreenShot*> list; QList<ScreenShot*> list;
QList<ScreenShot*> first = m_list->screenshots(); QList<ScreenShot*> first = m_list->screenshots();
for (QModelIndex index : ui->listView->selectionModel()->selectedIndexes()) for (QModelIndex index : ui->listView->selectionModel()->selectedRows())
{ {
list.append(first.at(index.row())); list.append(first.at(index.row()));
} }

View File

@ -11,7 +11,11 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Screenshots</string>
</property>
<property name="windowIcon">
<iconset resource="../../resources/multimc/multimc.qrc">
<normaloff>:/icons/multimc/scalable/apps/multimc.svg</normaloff>:/icons/multimc/scalable/apps/multimc.svg</iconset>
</property> </property>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry"> <property name="geometry">
@ -36,12 +40,15 @@
</rect> </rect>
</property> </property>
<property name="selectionMode"> <property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum> <enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectItems</enum>
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>480</width> <width>120</width>
<height>360</height> <height>90</height>
</size> </size>
</property> </property>
<property name="flow"> <property name="flow">
@ -51,11 +58,13 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="viewMode"> <property name="viewMode">
<enum>QListView::ListMode</enum> <enum>QListView::IconMode</enum>
</property> </property>
</widget> </widget>
</widget> </widget>
<resources/> <resources>
<include location="../../resources/multimc/multimc.qrc"/>
</resources>
<connections> <connections>
<connection> <connection>
<sender>buttonBox</sender> <sender>buttonBox</sender>

View File

@ -1,6 +1,6 @@
#include "ScreenshotList.h" #include "ScreenshotList.h"
#include "QDir" #include "QDir"
#include "QPixmap" #include "QIcon"
ScreenshotList::ScreenshotList(BaseInstance *instance, QObject *parent) ScreenshotList::ScreenshotList(BaseInstance *instance, QObject *parent)
: QAbstractListModel(parent), m_instance(instance) : QAbstractListModel(parent), m_instance(instance)
@ -20,11 +20,7 @@ QVariant ScreenshotList::data(const QModelIndex &index, int role) const
switch (role) switch (role)
{ {
case Qt::DecorationRole: case Qt::DecorationRole:
{ return QIcon(m_screenshots.at(index.row())->file);
QPixmap map;
map.loadFromData(m_screenshots.at(index.row())->file);
return map;
}
case Qt::DisplayRole: case Qt::DisplayRole:
return m_screenshots.at(index.row())->timestamp; return m_screenshots.at(index.row())->timestamp;
case Qt::ToolTipRole: case Qt::ToolTipRole:
@ -43,7 +39,7 @@ QVariant ScreenshotList::headerData(int section, Qt::Orientation orientation, in
Qt::ItemFlags ScreenshotList::flags(const QModelIndex &index) const Qt::ItemFlags ScreenshotList::flags(const QModelIndex &index) const
{ {
return Qt::NoItemFlags; return Qt::ItemIsSelectable;
} }
Task *ScreenshotList::load() Task *ScreenshotList::load()
@ -73,7 +69,7 @@ void ScreenshotLoadTask::executeTask()
{ {
ScreenShot *shot = new ScreenShot(); ScreenShot *shot = new ScreenShot();
shot->timestamp = file.left(file.length() - 4); shot->timestamp = file.left(file.length() - 4);
shot->file = QFile(file).readAll(); shot->file = dir.absoluteFilePath(file);
m_results.append(shot); m_results.append(shot);
} }
m_list->loadShots(m_results); m_list->loadShots(m_results);

View File

@ -8,9 +8,9 @@ class ScreenShot
{ {
public: public:
QString timestamp; QString timestamp;
QByteArray file; QString file;
int imgurIndex;
QString url; QString url;
int imgurIndex;
}; };
class ScreenshotList : public QAbstractListModel class ScreenshotList : public QAbstractListModel

View File

@ -1,32 +1,48 @@
#include "ScreenshotUploader.h" #include "ScreenshotUploader.h"
#include "logic/lists/ScreenshotList.h" #include "logic/lists/ScreenshotList.h"
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QJsonObject> #include <QHttpMultiPart>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject>
#include <QHttpPart>
#include <QFile>
#include <QUrl>
#include "URLConstants.h" #include "URLConstants.h"
#include "MultiMC.h" #include "MultiMC.h"
#include "logger/QsLog.h"
ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : m_shot(shot) ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : m_shot(shot)
{ {
m_url = URLConstants::IMGUR_UPLOAD_URL;
m_status = Job_NotStarted; m_status = Job_NotStarted;
} }
void ScreenShotUpload::start() void ScreenShotUpload::start()
{ {
m_status = Job_InProgress; 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::UserAgentHeader, "MultiMC/5.0 (Uncached)");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3");
request.setRawHeader("Accept", "application/json");
QJsonObject object; QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
object.insert("image", QJsonValue::fromVariant(m_shot->file.toBase64())); QHttpPart filePart;
object.insert("type", QJsonValue::fromVariant("base64")); filePart.setBody(QFile(m_shot->file).readAll().toBase64());
object.insert("name", QJsonValue::fromVariant(m_shot->timestamp)); filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png");
QJsonDocument doc; filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"image\"");
doc.setObject(object); 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(); auto worker = MMC->qnam();
QNetworkReply *rep = worker->post(request, doc.toJson()); QNetworkReply *rep = worker->post(request, multipart);
m_reply = std::shared_ptr<QNetworkReply>(rep); m_reply = std::shared_ptr<QNetworkReply>(rep);
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
@ -38,6 +54,7 @@ void ScreenShotUpload::start()
} }
void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error) void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error)
{ {
QLOG_DEBUG() << m_reply->errorString();
m_status = Job_Failed; m_status = Job_Failed;
} }
void ScreenShotUpload::downloadFinished() void ScreenShotUpload::downloadFinished()
@ -50,16 +67,19 @@ void ScreenShotUpload::downloadFinished()
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {
QLOG_DEBUG() << jsonError.errorString();
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
auto object = doc.object(); auto object = doc.object();
if (!object.value("success").toBool()) if (!object.value("success").toBool())
{ {
QLOG_DEBUG() << doc.toJson();
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; 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; m_status = Job_Finished;
emit succeeded(m_index_within_job); emit succeeded(m_index_within_job);
return; return;
@ -81,64 +101,3 @@ void ScreenShotUpload::downloadReadyRead()
{ {
// noop // 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;
}
}

View File

@ -27,26 +27,3 @@ slots:
private: private:
ScreenShot *m_shot; 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;
};

View File

@ -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 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_URL("http://status.mojang.com/check");
const QString MOJANG_STATUS_NEWS_URL("http://status.mojang.com/news"); 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_UPLOAD_URL("https://api.imgur.com/3/upload.json");
const QString IMGUR_GET_BASE("https://api.imgur.com/3/image/");
} }