NOISSUE implement deleting skins

This commit is contained in:
Petr Mrázek 2021-02-11 02:22:43 +01:00
parent 280903e52b
commit 1edcd9b86e
9 changed files with 118 additions and 19 deletions

View File

@ -303,9 +303,11 @@ set(MINECRAFT_SOURCES
minecraft/AssetsUtils.h minecraft/AssetsUtils.h
minecraft/AssetsUtils.cpp minecraft/AssetsUtils.cpp
# Skin upload utilities # Minecraft services
minecraft/SkinUpload.cpp minecraft/services/SkinUpload.cpp
minecraft/SkinUpload.h minecraft/services/SkinUpload.h
minecraft/services/SkinDelete.cpp
minecraft/services/SkinDelete.h
mojang/PackageManifest.h mojang/PackageManifest.h
mojang/PackageManifest.cpp mojang/PackageManifest.cpp

View File

@ -0,0 +1,42 @@
#include "SkinDelete.h"
#include <QNetworkRequest>
#include <QHttpMultiPart>
#include <Env.h>
SkinDelete::SkinDelete(QObject *parent, AuthSessionPtr session)
: Task(parent), m_session(session)
{
}
void SkinDelete::executeTask()
{
QNetworkRequest request(QUrl("https://api.minecraftservices.com/minecraft/profile/skins/active"));
request.setRawHeader("Authorization", QString("Bearer %1").arg(m_session->access_token).toLocal8Bit());
QNetworkReply *rep = ENV.qnam().deleteResource(request);
m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Deleting 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 SkinDelete::downloadError(QNetworkReply::NetworkError error)
{
// error happened during download.
qCritical() << "Network error: " << error;
emitFailed(m_reply->errorString());
}
void SkinDelete::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();
}

View File

@ -0,0 +1,30 @@
#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 SkinDelete> SkinDeletePtr;
class MULTIMC_LOGIC_EXPORT SkinDelete : public Task
{
Q_OBJECT
public:
SkinDelete(QObject *parent, AuthSessionPtr session);
virtual ~SkinDelete() = default;
private:
AuthSessionPtr m_session;
std::shared_ptr<QNetworkReply> m_reply;
protected:
virtual void executeTask();
public slots:
void downloadError(QNetworkReply::NetworkError);
void downloadFinished();
};

View File

@ -1,7 +1,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QFileDialog> #include <QFileDialog>
#include <FileSystem.h> #include <FileSystem.h>
#include <minecraft/SkinUpload.h> #include <minecraft/services/SkinUpload.h>
#include "SkinUploadDialog.h" #include "SkinUploadDialog.h"
#include "ui_SkinUploadDialog.h" #include "ui_SkinUploadDialog.h"
#include "ProgressDialog.h" #include "ProgressDialog.h"

View File

@ -30,6 +30,7 @@
#include "dialogs/SkinUploadDialog.h" #include "dialogs/SkinUploadDialog.h"
#include "tasks/Task.h" #include "tasks/Task.h"
#include "minecraft/auth/YggdrasilTask.h" #include "minecraft/auth/YggdrasilTask.h"
#include "minecraft/services/SkinDelete.h"
#include "MultiMC.h" #include "MultiMC.h"
@ -142,6 +143,7 @@ void AccountListPage::updateButtonStates()
ui->actionRemove->setEnabled(selection.size() > 0); ui->actionRemove->setEnabled(selection.size() > 0);
ui->actionSetDefault->setEnabled(selection.size() > 0); ui->actionSetDefault->setEnabled(selection.size() > 0);
ui->actionUploadSkin->setEnabled(selection.size() > 0); ui->actionUploadSkin->setEnabled(selection.size() > 0);
ui->actionDeleteSkin->setEnabled(selection.size() > 0);
if(m_accounts->activeAccount().get() == nullptr) { if(m_accounts->activeAccount().get() == nullptr) {
ui->actionNoDefault->setEnabled(false); ui->actionNoDefault->setEnabled(false);
@ -191,3 +193,25 @@ void AccountListPage::on_actionUploadSkin_triggered()
dialog.exec(); dialog.exec();
} }
} }
void AccountListPage::on_actionDeleteSkin_triggered()
{
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() <= 0)
return;
QModelIndex selected = selection.first();
AuthSessionPtr session = std::make_shared<AuthSession>();
MojangAccountPtr account = selected.data(MojangAccountList::PointerRole).value<MojangAccountPtr>();
auto login = account->login(session);
ProgressDialog prog(this);
if (prog.execWithTask((Task*)login.get()) != QDialog::Accepted) {
CustomMessageBox::selectable(this, tr("Skin Delete"), tr("Failed to login!"), QMessageBox::Warning)->exec();
return;
}
auto deleteSkinTask = std::make_shared<SkinDelete>(this, session);
if (prog.execWithTask((Task*)deleteSkinTask.get()) != QDialog::Accepted) {
CustomMessageBox::selectable(this, tr("Skin Delete"), tr("Failed to delete current skin!"), QMessageBox::Warning)->exec();
return;
}
}

View File

@ -59,35 +59,26 @@ public:
return "Getting-Started#adding-an-account"; return "Getting-Started#adding-an-account";
} }
private: public slots:
void changeEvent(QEvent * event) override;
QMenu * createPopupMenu() override;
public
slots:
void on_actionAdd_triggered(); void on_actionAdd_triggered();
void on_actionRemove_triggered(); void on_actionRemove_triggered();
void on_actionSetDefault_triggered(); void on_actionSetDefault_triggered();
void on_actionNoDefault_triggered(); void on_actionNoDefault_triggered();
void on_actionUploadSkin_triggered(); void on_actionUploadSkin_triggered();
void on_actionDeleteSkin_triggered();
void listChanged(); void listChanged();
//! Updates the states of the dialog's buttons. //! Updates the states of the dialog's buttons.
void updateButtonStates(); void updateButtonStates();
protected: protected slots:
std::shared_ptr<MojangAccountList> m_accounts;
protected
slots:
void ShowContextMenu(const QPoint &pos); void ShowContextMenu(const QPoint &pos);
void addAccount(const QString& errMsg=""); void addAccount(const QString& errMsg="");
private: private:
void changeEvent(QEvent * event) override;
QMenu * createPopupMenu() override;
std::shared_ptr<MojangAccountList> m_accounts;
Ui::AccountListPage *ui; Ui::AccountListPage *ui;
}; };

View File

@ -40,7 +40,9 @@
<addaction name="actionRemove"/> <addaction name="actionRemove"/>
<addaction name="actionSetDefault"/> <addaction name="actionSetDefault"/>
<addaction name="actionNoDefault"/> <addaction name="actionNoDefault"/>
<addaction name="separator"/>
<addaction name="actionUploadSkin"/> <addaction name="actionUploadSkin"/>
<addaction name="actionDeleteSkin"/>
</widget> </widget>
<action name="actionAdd"> <action name="actionAdd">
<property name="text"> <property name="text">
@ -70,6 +72,14 @@
<string>Upload Skin</string> <string>Upload Skin</string>
</property> </property>
</action> </action>
<action name="actionDeleteSkin">
<property name="text">
<string>Delete Skin</string>
</property>
<property name="toolTip">
<string>Delete the currently active skin and go back to the default one</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>