diff --git a/gui/ConsoleWindow.cpp b/gui/ConsoleWindow.cpp index ccc037f2..d11e8aff 100644 --- a/gui/ConsoleWindow.cpp +++ b/gui/ConsoleWindow.cpp @@ -42,6 +42,8 @@ ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent) connect(mcproc, SIGNAL(launch_failed(BaseInstance *)), this, SLOT(onLaunchFailed(BaseInstance *))); + connect(ui->btnScreenshots, &QPushButton::clicked, this, &ConsoleWindow::uploadScreenshots); + restoreState( QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray())); restoreGeometry( @@ -52,6 +54,7 @@ ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent) auto icon = MMC->icons()->getIcon(iconKey); setWindowIcon(icon); m_trayIcon = new QSystemTrayIcon(icon, this); + // TODO add screenshot upload as a menu item in the tray icon QString consoleTitle = tr("Console window for ") + name; m_trayIcon->setToolTip(consoleTitle); setWindowTitle(consoleTitle); diff --git a/gui/ConsoleWindow.h b/gui/ConsoleWindow.h index 7fe90c52..e21da33c 100644 --- a/gui/ConsoleWindow.h +++ b/gui/ConsoleWindow.h @@ -51,6 +51,7 @@ private: signals: void isClosing(); + void uploadScreenshots(); public slots: diff --git a/gui/ConsoleWindow.ui b/gui/ConsoleWindow.ui index c2307ecc..e50fb520 100644 --- a/gui/ConsoleWindow.ui +++ b/gui/ConsoleWindow.ui @@ -49,6 +49,13 @@ + + + + Upload Screenshots + + + diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 87c65601..f79b981a 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -1235,6 +1235,7 @@ void MainWindow::launchInstance(BaseInstance *instance, AuthSessionPtr session) console = new ConsoleWindow(proc); connect(console, SIGNAL(isClosing()), this, SLOT(instanceEnded())); + connect(console, &ConsoleWindow::uploadScreenshots, this, &MainWindow::on_actionScreenshots_triggered); proc->setLogin(session); proc->launch(); @@ -1516,34 +1517,15 @@ void MainWindow::on_actionScreenshots_triggered() return; } ScreenshotDialog dialog(list, this); - if (dialog.exec() == QDialog::Accepted) + if (dialog.exec() == ScreenshotDialog::Accepted) { - QList screenshots = dialog.selected(); - if (screenshots.size() == 0) - return; - NetJob *job = new NetJob("Screenshot Upload"); - for (ScreenShot *shot : screenshots) - job->addNetAction(ScreenShotUpload::make(shot)); - ProgressDialog prog2(this); - prog2.exec(job); - connect(job, &NetJob::failed, [this] + QStringList urls; + for (ScreenShot *shot : dialog.uploaded()) { - CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), - tr("Unknown error"), QMessageBox::Warning)->exec(); - }); - connect(job, &NetJob::succeeded, [this, screenshots] - { screenshotsUploaded(screenshots); }); + urls << QString("url + "\">Image %s") + .arg(QString::number(shot->imgurIndex)); + } + CustomMessageBox::selectable(this, tr("Done uploading!"), urls.join("\n"), + QMessageBox::Information)->exec(); } } - -void MainWindow::screenshotsUploaded(QList screenshots) -{ - QStringList urls; - for (ScreenShot *shot : screenshots) - { - urls << QString("url + "\">Image %s") - .arg(QString::number(shot->imgurIndex)); - } - CustomMessageBox::selectable(this, tr("Done uploading!"), urls.join("\n"), - QMessageBox::Information)->exec(); -} diff --git a/gui/MainWindow.h b/gui/MainWindow.h index 0a474ef1..5911d175 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -170,8 +170,6 @@ slots: void reloadStatus(); - void screenshotsUploaded(QList screenshots); - /*! * Runs the DownloadUpdateTask and installs updates. */ diff --git a/gui/dialogs/ScreenshotDialog.cpp b/gui/dialogs/ScreenshotDialog.cpp index 8900f15b..02764fa3 100644 --- a/gui/dialogs/ScreenshotDialog.cpp +++ b/gui/dialogs/ScreenshotDialog.cpp @@ -1,6 +1,13 @@ #include "ScreenshotDialog.h" #include "ui_ScreenshotDialog.h" -#include "QModelIndex" + +#include +#include + +#include "ProgressDialog.h" +#include "CustomMessageBox.h" +#include "logic/net/NetJob.h" +#include "logic/net/ScreenshotUploader.h" ScreenshotDialog::ScreenshotDialog(ScreenshotList *list, QWidget *parent) : QDialog(parent), @@ -16,7 +23,12 @@ ScreenshotDialog::~ScreenshotDialog() delete ui; } -QList ScreenshotDialog::selected() +QList ScreenshotDialog::uploaded() const +{ + return m_uploaded; +} + +QList ScreenshotDialog::selected() const { QList list; QList first = m_list->screenshots(); @@ -26,3 +38,29 @@ QList ScreenshotDialog::selected() } return list; } + +void ScreenshotDialog::on_buttonBox_accepted() +{ + QList screenshots = selected(); + if (screenshots.isEmpty()) + { + done(NothingDone); + return; + } + NetJob *job = new NetJob("Screenshot Upload"); + for (ScreenShot *shot : screenshots) + { + qDebug() << shot->file; + job->addNetAction(ScreenShotUpload::make(shot)); + } + ProgressDialog prog(this); + prog.exec(job); + connect(job, &NetJob::failed, [this] + { + CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), + tr("Unknown error"), QMessageBox::Warning)->exec(); + reject(); + }); + m_uploaded = screenshots; + connect(job, &NetJob::succeeded, this, &ScreenshotDialog::accept); +} diff --git a/gui/dialogs/ScreenshotDialog.h b/gui/dialogs/ScreenshotDialog.h index d3f629e7..1ca27bdd 100644 --- a/gui/dialogs/ScreenshotDialog.h +++ b/gui/dialogs/ScreenshotDialog.h @@ -18,12 +18,21 @@ public: explicit ScreenshotDialog(ScreenshotList *list, QWidget *parent = 0); ~ScreenshotDialog(); - QList selected(); + enum + { + NothingDone = 0x42 + }; + + QList uploaded() const; private slots: + void on_buttonBox_accepted(); private: Ui::ScreenshotDialog *ui; ScreenshotList *m_list; + QList m_uploaded; + + QList selected() const; }; diff --git a/gui/dialogs/ScreenshotDialog.ui b/gui/dialogs/ScreenshotDialog.ui index af73e5ec..7eeab859 100644 --- a/gui/dialogs/ScreenshotDialog.ui +++ b/gui/dialogs/ScreenshotDialog.ui @@ -66,22 +66,6 @@ - - buttonBox - accepted() - ScreenshotDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - buttonBox rejected() diff --git a/logic/lists/ScreenshotList.cpp b/logic/lists/ScreenshotList.cpp index e955f121..0565d0a4 100644 --- a/logic/lists/ScreenshotList.cpp +++ b/logic/lists/ScreenshotList.cpp @@ -1,6 +1,6 @@ #include "ScreenshotList.h" -#include "QDir" -#include "QIcon" +#include +#include ScreenshotList::ScreenshotList(BaseInstance *instance, QObject *parent) : QAbstractListModel(parent), m_instance(instance) @@ -39,7 +39,7 @@ QVariant ScreenshotList::headerData(int section, Qt::Orientation orientation, in Qt::ItemFlags ScreenshotList::flags(const QModelIndex &index) const { - return Qt::ItemIsSelectable; + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; } Task *ScreenshotList::load() diff --git a/logic/net/ScreenshotUploader.cpp b/logic/net/ScreenshotUploader.cpp index c1f62243..829a0815 100644 --- a/logic/net/ScreenshotUploader.cpp +++ b/logic/net/ScreenshotUploader.cpp @@ -1,5 +1,5 @@ #include "ScreenshotUploader.h" -#include "logic/lists/ScreenshotList.h" + #include #include #include @@ -7,11 +7,13 @@ #include #include #include + +#include "logic/lists/ScreenshotList.h" #include "URLConstants.h" #include "MultiMC.h" #include "logger/QsLog.h" -ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : m_shot(shot) +ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : NetAction(), m_shot(shot) { m_url = URLConstants::IMGUR_UPLOAD_URL; m_status = Job_NotStarted; @@ -26,9 +28,16 @@ void ScreenShotUpload::start() request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); request.setRawHeader("Accept", "application/json"); + QFile f(m_shot->file); + if (!f.open(QFile::ReadOnly)) + { + emit failed(m_index_within_job); + return; + } + QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart filePart; - filePart.setBody(QFile(m_shot->file).readAll().toBase64()); + filePart.setBody(f.readAll().toBase64()); filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png"); filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"image\""); multipart->append(filePart); @@ -45,12 +54,10 @@ void ScreenShotUpload::start() QNetworkReply *rep = worker->post(request, multipart); m_reply = std::shared_ptr(rep); - connect(rep, SIGNAL(downloadProgress(qint64, qint64)), - SLOT(downloadProgress(qint64, qint64))); - connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); + connect(rep, &QNetworkReply::uploadProgress, this, &ScreenShotUpload::downloadProgress); + connect(rep, &QNetworkReply::finished, this, &ScreenShotUpload::downloadFinished); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError))); - connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); } void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error) { @@ -86,6 +93,7 @@ void ScreenShotUpload::downloadFinished() } else { + QLOG_DEBUG() << m_reply->readAll(); m_reply.reset(); emit failed(m_index_within_job); return; @@ -97,7 +105,3 @@ void ScreenShotUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) m_progress = bytesReceived; emit progress(m_index_within_job, bytesReceived, bytesTotal); } -void ScreenShotUpload::downloadReadyRead() -{ - // noop -} diff --git a/logic/net/ScreenshotUploader.h b/logic/net/ScreenshotUploader.h index d5d1cef3..c1c9db6f 100644 --- a/logic/net/ScreenshotUploader.h +++ b/logic/net/ScreenshotUploader.h @@ -18,7 +18,9 @@ slots: virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); virtual void downloadError(QNetworkReply::NetworkError error); virtual void downloadFinished(); - virtual void downloadReadyRead(); + virtual void downloadReadyRead() + { + } public slots: