diff options
author | Petr Mrázek <peterix@gmail.com> | 2013-12-08 17:39:32 +0100 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2013-12-08 17:39:32 +0100 |
commit | d68f49ffc0e4b39ed842032a1e365870d2597adf (patch) | |
tree | 7befe15e9cd0ae9fede0f2d883a1f4f789ee3959 | |
parent | 0cb8ff40b26401a707781c2c4171d3ec6c114077 (diff) | |
parent | c6f0d9ce937349aaab0ed7ee3968c73bf721665c (diff) | |
download | MultiMC-d68f49ffc0e4b39ed842032a1e365870d2597adf.tar MultiMC-d68f49ffc0e4b39ed842032a1e365870d2597adf.tar.gz MultiMC-d68f49ffc0e4b39ed842032a1e365870d2597adf.tar.lz MultiMC-d68f49ffc0e4b39ed842032a1e365870d2597adf.tar.xz MultiMC-d68f49ffc0e4b39ed842032a1e365870d2597adf.zip |
Merge branch 'feature_pasting' of https://github.com/robotbrain/MultiMC5 into develop
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | gui/ConsoleWindow.cpp | 15 | ||||
-rw-r--r-- | gui/ConsoleWindow.h | 2 | ||||
-rw-r--r-- | gui/ConsoleWindow.ui | 7 | ||||
-rw-r--r-- | logic/net/PasteUpload.cpp | 84 | ||||
-rw-r--r-- | logic/net/PasteUpload.h | 26 |
6 files changed, 136 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index beaa9bb1..11b64c3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,8 @@ logic/net/HttpMetaCache.h logic/net/HttpMetaCache.cpp logic/net/S3ListBucket.h logic/net/S3ListBucket.cpp +logic/net/PasteUpload.h +logic/net/PasteUpload.cpp # Yggdrasil login stuff logic/auth/MojangAccount.h diff --git a/gui/ConsoleWindow.cpp b/gui/ConsoleWindow.cpp index d0210df6..5db4442e 100644 --- a/gui/ConsoleWindow.cpp +++ b/gui/ConsoleWindow.cpp @@ -22,6 +22,9 @@ #include <gui/Platform.h> #include <gui/dialogs/CustomMessageBox.h> +#include <gui/dialogs/ProgressDialog.h> + +#include "logic/net/PasteUpload.h" ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent) : QMainWindow(parent), ui(new Ui::ConsoleWindow), proc(mcproc) @@ -179,3 +182,15 @@ void ConsoleWindow::onLaunchFailed(BaseInstance *instance) if(!isVisible()) show(); } + +void ConsoleWindow::on_btnPaste_clicked() +{ + auto text = ui->text->toPlainText(); + ProgressDialog dialog(this); + PasteUpload* paste=new PasteUpload(this, text); + dialog.exec(paste); + if(!paste->successful()) + { + CustomMessageBox::selectable(this, "Upload failed", paste->failReason(), QMessageBox::Critical)->exec(); + } +} diff --git a/gui/ConsoleWindow.h b/gui/ConsoleWindow.h index 2d948484..731c616c 100644 --- a/gui/ConsoleWindow.h +++ b/gui/ConsoleWindow.h @@ -76,6 +76,8 @@ slots: // FIXME: add handlers for the other MinecraftProcess signals (pre/post launch command // failures) + void on_btnPaste_clicked(); + protected: void closeEvent(QCloseEvent *); diff --git a/gui/ConsoleWindow.ui b/gui/ConsoleWindow.ui index ed1b627b..6cb5e93b 100644 --- a/gui/ConsoleWindow.ui +++ b/gui/ConsoleWindow.ui @@ -74,6 +74,13 @@ </property> </widget> </item> + <item> + <widget class="QPushButton" name="btnPaste"> + <property name="text"> + <string>Upload Log</string> + </property> + </widget> + </item> </layout> </item> </layout> diff --git a/logic/net/PasteUpload.cpp b/logic/net/PasteUpload.cpp new file mode 100644 index 00000000..acf09291 --- /dev/null +++ b/logic/net/PasteUpload.cpp @@ -0,0 +1,84 @@ +#include "PasteUpload.h" +#include "MultiMC.h" +#include "logger/QsLog.h" +#include <QJsonObject> +#include <QJsonDocument> +#include "gui/dialogs/CustomMessageBox.h" +#include <QDesktopServices> + +PasteUpload::PasteUpload(QWidget *window, QString text) : m_text(text), m_window(window) +{ +} + +void PasteUpload::executeTask() +{ + QNetworkRequest request(QUrl("http://paste.ee/api")); + request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); + QByteArray content( + "key=public&description=MultiMC5+Log+File&language=plain&format=json&paste=" + + m_text.toUtf8()); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setRawHeader("Content-Length", QByteArray::number(content.size())); + + auto worker = MMC->qnam(); + QNetworkReply *rep = worker->post(request, content); + + m_reply = std::shared_ptr<QNetworkReply>(rep); + connect(rep, &QNetworkReply::downloadProgress, [&](qint64 value, qint64 max) + { setProgress(value / max * 100); }); + connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, + SLOT(downloadError(QNetworkReply::NetworkError))); + connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished())); +} + +void PasteUpload::downloadError(QNetworkReply::NetworkError error) +{ + // error happened during download. + QLOG_ERROR() << "Network error: " << error; + emitFailed(m_reply->errorString()); +} + +void PasteUpload::downloadFinished() +{ + // if the download succeeded + if (m_reply->error() == QNetworkReply::NetworkError::NoError) + { + QByteArray data = m_reply->readAll(); + m_reply.reset(); + QJsonParseError jsonError; + QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError) + { + emitFailed(jsonError.errorString()); + return; + } + QString error; + if (!parseResult(doc, &error)) + { + emitFailed(error); + return; + } + } + // else the download failed + else + { + emitFailed(QString("Network error: %1").arg(m_reply->errorString())); + m_reply.reset(); + return; + } + emitSucceeded(); +} + +bool PasteUpload::parseResult(QJsonDocument doc, QString *parseError) +{ + auto object = doc.object(); + auto status = object.value("status").toString("error"); + if (status == "error") + { + parseError = new QString(object.value("error").toString()); + return false; + } + QString pasteUrl = object.value("paste").toObject().value("link").toString(); + QDesktopServices::openUrl(pasteUrl); + return true; +} diff --git a/logic/net/PasteUpload.h b/logic/net/PasteUpload.h new file mode 100644 index 00000000..917a0016 --- /dev/null +++ b/logic/net/PasteUpload.h @@ -0,0 +1,26 @@ +#pragma once +#include "logic/tasks/Task.h" +#include <QMessageBox> +#include <QNetworkReply> +#include <memory> + +class PasteUpload : public Task +{ + Q_OBJECT +public: + PasteUpload(QWidget *window, QString text); + +protected: + virtual void executeTask(); + +private: + bool parseResult(QJsonDocument doc, QString *parseError); + QString m_text; + QString m_error; + QWidget *m_window; + std::shared_ptr<QNetworkReply> m_reply; +public +slots: + void downloadError(QNetworkReply::NetworkError); + void downloadFinished(); +}; |