summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexia <alexia@robotbrain.info>2016-05-12 16:51:25 -0400
committerPetr Mrázek <peterix@gmail.com>2016-05-15 16:01:05 +0200
commit377316999ea9ef6adbc75c52a858eb8e526616e7 (patch)
tree1ad7ba47c89c69d0e25f7e6e0f592782e75b81d1
parentf9791a5cc80b8e99eebd68176c33fdc1c377af86 (diff)
downloadMultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar
MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.gz
MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.lz
MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.xz
MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.zip
GH-767 Basic skin upload
-rw-r--r--api/logic/CMakeLists.txt4
-rw-r--r--api/logic/minecraft/SkinUpload.cpp69
-rw-r--r--api/logic/minecraft/SkinUpload.h40
-rw-r--r--application/CMakeLists.txt6
-rw-r--r--application/dialogs/SkinUploadDialog.cpp69
-rw-r--r--application/dialogs/SkinUploadDialog.h29
-rw-r--r--application/dialogs/SkinUploadDialog.ui85
-rw-r--r--application/pages/global/AccountListPage.cpp13
-rw-r--r--application/pages/global/AccountListPage.h2
-rw-r--r--application/pages/global/AccountListPage.ui10
10 files changed, 324 insertions, 3 deletions
diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt
index 32f5fd12..da465992 100644
--- a/api/logic/CMakeLists.txt
+++ b/api/logic/CMakeLists.txt
@@ -276,7 +276,9 @@ set(MINECRAFT_SOURCES
minecraft/liteloader/LiteLoaderInstaller.cpp
minecraft/liteloader/LiteLoaderVersionList.h
minecraft/liteloader/LiteLoaderVersionList.cpp
-)
+ minecraft/SkinUpload.cpp
+ minecraft/SkinUpload.h
+ )
add_unit_test(GradleSpecifier
SOURCES minecraft/GradleSpecifier_test.cpp
diff --git a/api/logic/minecraft/SkinUpload.cpp b/api/logic/minecraft/SkinUpload.cpp
new file mode 100644
index 00000000..2c67c7aa
--- /dev/null
+++ b/api/logic/minecraft/SkinUpload.cpp
@@ -0,0 +1,69 @@
+#include "SkinUpload.h"
+#include <QNetworkRequest>
+#include <QHttpMultiPart>
+#include <Env.h>
+
+QByteArray getModelString(SkinUpload::Model model) {
+ switch (model) {
+ case SkinUpload::STEVE:
+ return "steve";
+ case SkinUpload::ALEX:
+ return "alex";
+ default:
+ qDebug() << "Unknown skin type!";
+ return "";
+ }
+}
+
+SkinUpload::SkinUpload(QObject *parent, AuthSessionPtr session, QByteArray skin, SkinUpload::Model model)
+ : Task(parent), m_model(model), m_skin(skin), m_session(session)
+{
+}
+
+void SkinUpload::executeTask()
+{
+ QNetworkRequest request(QUrl(QString("https://api.mojang.com/user/profile/%1/skin").arg(m_session->uuid)));
+ request.setRawHeader("Authorization", QString("Bearer: %1").arg(m_session->access_token).toLocal8Bit());
+
+ QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
+
+ QHttpPart model;
+ model.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"model\""));
+ model.setBody(getModelString(m_model));
+
+ QHttpPart skin;
+ skin.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
+ skin.setHeader(QNetworkRequest::ContentDispositionHeader,
+ QVariant("form-data; name=\"file\"; filename=\"skin.png\""));
+ skin.setBody(m_skin);
+
+ multiPart->append(model);
+ multiPart->append(skin);
+
+ QNetworkReply *rep = ENV.qnam()->put(request, multiPart);
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+
+ setStatus(tr("Uploading skin"));
+ connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
+ connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
+}
+
+void SkinUpload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ qCritical() << "Network error: " << error;
+ emitFailed(m_reply->errorString());
+}
+
+void SkinUpload::downloadFinished()
+{
+ // if the download failed
+ if (m_reply->error() != QNetworkReply::NetworkError::NoError)
+ {
+ emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
+ m_reply.reset();
+ return;
+ }
+ emitSucceeded();
+}
diff --git a/api/logic/minecraft/SkinUpload.h b/api/logic/minecraft/SkinUpload.h
new file mode 100644
index 00000000..86944b82
--- /dev/null
+++ b/api/logic/minecraft/SkinUpload.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <QFile>
+#include <QtNetwork/QtNetwork>
+#include <memory>
+#include <minecraft/auth/AuthSession.h>
+#include "tasks/Task.h"
+#include "multimc_logic_export.h"
+
+typedef std::shared_ptr<class SkinUpload> SkinUploadPtr;
+
+class MULTIMC_LOGIC_EXPORT SkinUpload : public Task\
+{
+Q_OBJECT
+public:
+ enum Model
+ {
+ STEVE,
+ ALEX
+ };
+
+ // Note this class takes ownership of the file.
+ SkinUpload(QObject *parent, AuthSessionPtr session, QByteArray skin, Model model = STEVE);
+
+ virtual ~SkinUpload() {}
+
+private:
+ Model m_model;
+ QByteArray m_skin;
+ AuthSessionPtr m_session;
+ std::shared_ptr<QNetworkReply> m_reply;
+protected:
+ virtual void executeTask();
+
+public slots:
+
+ void downloadError(QNetworkReply::NetworkError);
+
+ void downloadFinished();
+};
diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt
index 15934e3e..9d71f977 100644
--- a/application/CMakeLists.txt
+++ b/application/CMakeLists.txt
@@ -197,7 +197,8 @@ SET(MULTIMC_SOURCES
dialogs/UpdateDialog.h
dialogs/VersionSelectDialog.cpp
dialogs/VersionSelectDialog.h
-
+ dialogs/SkinUploadDialog.cpp
+ dialogs/SkinUploadDialog.h
# GUI - widgets
widgets/Common.cpp
@@ -234,7 +235,7 @@ SET(MULTIMC_SOURCES
groupview/InstanceDelegate.h
groupview/VisualGroup.cpp
groupview/VisualGroup.h
-)
+ )
######## UIs ########
SET(MULTIMC_UIS
@@ -273,6 +274,7 @@ SET(MULTIMC_UIS
dialogs/LoginDialog.ui
dialogs/UpdateDialog.ui
dialogs/NotificationDialog.ui
+ dialogs/SkinUploadDialog.ui
# Widgets/other
widgets/MCModInfoFrame.ui
diff --git a/application/dialogs/SkinUploadDialog.cpp b/application/dialogs/SkinUploadDialog.cpp
new file mode 100644
index 00000000..ebbab785
--- /dev/null
+++ b/application/dialogs/SkinUploadDialog.cpp
@@ -0,0 +1,69 @@
+#include <QFileInfo>
+#include <QFileDialog>
+#include <FileSystem.h>
+#include <minecraft/SkinUpload.h>
+#include "SkinUploadDialog.h"
+#include "ui_SkinUploadDialog.h"
+#include "ProgressDialog.h"
+#include "CustomMessageBox.h"
+
+void SkinUploadDialog::on_buttonBox_rejected()
+{
+ close();
+}
+
+void SkinUploadDialog::on_buttonBox_accepted()
+{
+ AuthSessionPtr session = std::make_shared<AuthSession>();
+ auto login = m_acct->login(session);
+ ProgressDialog prog(this);
+ if (prog.execWithTask((Task*)login.get()) != QDialog::Accepted)
+ {
+ //FIXME: recover with password prompt
+ CustomMessageBox::selectable(this, tr("Failed to login!"), tr("Unknown error"), QMessageBox::Warning)->exec();
+ close();
+ return;
+ }
+ QString fileName = ui->skinPathTextBox->text();
+ if (!QFile::exists(fileName))
+ {
+ CustomMessageBox::selectable(this, tr("Skin file does not exist!"), tr("Unknown error"), QMessageBox::Warning)->exec();
+ close();
+ return;
+ }
+ SkinUpload::Model model = SkinUpload::STEVE;
+ if (ui->steveBtn->isChecked())
+ {
+ model = SkinUpload::STEVE;
+ }
+ else if (ui->alexBtn->isChecked())
+ {
+ model = SkinUpload::ALEX;
+ }
+ SkinUploadPtr upload = std::make_shared<SkinUpload>(this, session, FS::read(fileName), model);
+ if (prog.execWithTask((Task*)upload.get()) != QDialog::Accepted)
+ {
+ CustomMessageBox::selectable(this, tr("Failed to upload skin!"), tr("Unknown error"), QMessageBox::Warning)->exec();
+ close();
+ return;
+ }
+ CustomMessageBox::selectable(this, tr("Skin uploaded!"), tr("Success"), QMessageBox::Information)->exec();
+ close();
+}
+
+void SkinUploadDialog::on_skinBrowseBtn_clicked()
+{
+ QString raw_path = QFileDialog::getOpenFileName(this, tr("Select Skin Texture"), QString(), "*.png");
+ QString cooked_path = FS::NormalizePath(raw_path);
+ if (cooked_path.isEmpty() || !QFileInfo::exists(cooked_path))
+ {
+ return;
+ }
+ ui->skinPathTextBox->setText(cooked_path);
+}
+
+SkinUploadDialog::SkinUploadDialog(MojangAccountPtr acct, QWidget *parent)
+ :QDialog(parent), m_acct(acct), ui(new Ui::SkinUploadDialog)
+{
+ ui->setupUi(this);
+}
diff --git a/application/dialogs/SkinUploadDialog.h b/application/dialogs/SkinUploadDialog.h
new file mode 100644
index 00000000..514eabc8
--- /dev/null
+++ b/application/dialogs/SkinUploadDialog.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <QDialog>
+#include <minecraft/auth/MojangAccount.h>
+
+namespace Ui
+{
+ class SkinUploadDialog;
+}
+
+class SkinUploadDialog : public QDialog {
+ Q_OBJECT
+public:
+ explicit SkinUploadDialog(MojangAccountPtr acct, QWidget *parent = 0);
+ virtual ~SkinUploadDialog() {};
+
+public slots:
+ void on_buttonBox_accepted();
+
+ void on_buttonBox_rejected();
+
+ void on_skinBrowseBtn_clicked();
+
+protected:
+ MojangAccountPtr m_acct;
+
+private:
+ Ui::SkinUploadDialog *ui;
+};
diff --git a/application/dialogs/SkinUploadDialog.ui b/application/dialogs/SkinUploadDialog.ui
new file mode 100644
index 00000000..6f5307e3
--- /dev/null
+++ b/application/dialogs/SkinUploadDialog.ui
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>SkinUploadDialog</class>
+ <widget class="QDialog" name="SkinUploadDialog">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>413</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Skin Upload</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QGroupBox" name="fileBox">
+ <property name="title">
+ <string>Skin File</string>
+ </property>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QLineEdit" name="skinPathTextBox"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="skinBrowseBtn">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="maximumSize">
+ <size>
+ <width>28</width>
+ <height>16777215</height>
+ </size>
+ </property>
+ <property name="text">
+ <string notr="true">...</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <widget class="QGroupBox" name="modelBox">
+ <property name="title">
+ <string>Player Model</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout_1">
+ <item>
+ <widget class="QRadioButton" name="steveBtn">
+ <property name="text">
+ <string>Steve Model</string>
+ </property>
+ <property name="checked">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QRadioButton" name="alexBtn">
+ <property name="text">
+ <string>Alex Model</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <widget class="QDialogButtonBox" name="buttonBox">
+ <property name="standardButtons">
+ <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/application/pages/global/AccountListPage.cpp b/application/pages/global/AccountListPage.cpp
index 89b853c5..eb3ddff9 100644
--- a/application/pages/global/AccountListPage.cpp
+++ b/application/pages/global/AccountListPage.cpp
@@ -28,6 +28,7 @@
#include "dialogs/AccountSelectDialog.h"
#include "dialogs/LoginDialog.h"
#include "dialogs/CustomMessageBox.h"
+#include "dialogs/SkinUploadDialog.h"
#include "tasks/Task.h"
#include "minecraft/auth/YggdrasilTask.h"
@@ -139,3 +140,15 @@ void AccountListPage::addAccount(const QString &errMsg)
job->start();
}
}
+
+void AccountListPage::on_uploadSkinBtn_clicked()
+{
+ QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
+ if (selection.size() > 0)
+ {
+ QModelIndex selected = selection.first();
+ MojangAccountPtr account = selected.data(MojangAccountList::PointerRole).value<MojangAccountPtr>();
+ SkinUploadDialog dialog(account, this);
+ dialog.exec();
+ }
+}
diff --git a/application/pages/global/AccountListPage.h b/application/pages/global/AccountListPage.h
index 5701dfcb..ed518e92 100644
--- a/application/pages/global/AccountListPage.h
+++ b/application/pages/global/AccountListPage.h
@@ -69,6 +69,8 @@ slots:
void on_noDefaultBtn_clicked();
+ void on_uploadSkinBtn_clicked();
+
void listChanged();
//! Updates the states of the dialog's buttons.
diff --git a/application/pages/global/AccountListPage.ui b/application/pages/global/AccountListPage.ui
index fa2e5bf0..270d5b56 100644
--- a/application/pages/global/AccountListPage.ui
+++ b/application/pages/global/AccountListPage.ui
@@ -97,6 +97,16 @@
</property>
</widget>
</item>
+ <item>
+ <widget class="QPushButton" name="uploadSkinBtn">
+ <property name="toolTip">
+ <string>Opens a dialog to select and upload a skin image to the selected account.</string>
+ </property>
+ <property name="text">
+ <string>&amp;Upload</string>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
</layout>