Skip to content
Permalink
Browse files

Close to finished. Need to fix the upload part. Viewing works (in gra…

…yscale)
  • Loading branch information...
robotbrain committed Feb 24, 2014
1 parent 4a77524 commit 5e33da258c3b5159dba854eb4792d0852a1ca363
@@ -1538,25 +1538,6 @@ void MainWindow::on_actionScreenshots_triggered()

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;
for (ScreenShot *shot : screenshots)
{
@@ -172,8 +172,6 @@ public

void screenshotsUploaded(QList<class ScreenShot*> screenshots);

void screenShotsGotten(QList<class ScreenShot*> screenshots);

/*!
* Runs the DownloadUpdateTask and installs updates.
*/
@@ -20,7 +20,7 @@ QList<ScreenShot*> ScreenshotDialog::selected()
{
QList<ScreenShot*> list;
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()));
}
@@ -11,7 +11,11 @@
</rect>
</property>
<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>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
@@ -36,12 +40,15 @@
</rect>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectItems</enum>
</property>
<property name="iconSize">
<size>
<width>480</width>
<height>360</height>
<width>120</width>
<height>90</height>
</size>
</property>
<property name="flow">
@@ -51,11 +58,13 @@
<bool>true</bool>
</property>
<property name="viewMode">
<enum>QListView::ListMode</enum>
<enum>QListView::IconMode</enum>
</property>
</widget>
</widget>
<resources/>
<resources>
<include location="../../resources/multimc/multimc.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
@@ -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);
@@ -8,9 +8,9 @@ class ScreenShot
{
public:
QString timestamp;
QByteArray file;
int imgurIndex;
QString file;
QString url;
int imgurIndex;
};

class ScreenshotList : public QAbstractListModel
@@ -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;
}
}
@@ -27,26 +27,3 @@ public
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;
};
@@ -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");
}

0 comments on commit 5e33da2

Please sign in to comment.
You can’t perform that action at this time.