aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/dialogs/SkinUploadDialog.cpp
blob: f8715dca64dbc4c55241d3542ab5a6eefec2e75e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <QFileInfo>
#include <QFileDialog>
#include <QPainter>

#include <FileSystem.h>

#include <minecraft/services/SkinUpload.h>
#include <minecraft/services/CapeChange.h>
#include <tasks/SequentialTask.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()
{
    QString fileName;
    QString input = ui->skinPathTextBox->text();
    QRegularExpression urlPrefixMatcher(QRegularExpression::anchoredPattern("^([a-z]+)://.+$"));
    bool isLocalFile = false;
    // it has an URL prefix -> it is an URL
    if(urlPrefixMatcher.match(input).hasMatch())  // TODO: does this work?
    {
        QUrl fileURL = input;
        if(fileURL.isValid())
        {
            // local?
            if(fileURL.isLocalFile())
            {
                isLocalFile = true;
                fileName = fileURL.toLocalFile();
            }
            else
            {
                CustomMessageBox::selectable(
                    this,
                    tr("Skin Upload"),
                    tr("Using remote URLs for setting skins is not implemented yet."),
                    QMessageBox::Warning
                )->exec();
                close();
                return;
            }
        }
        else
        {
            CustomMessageBox::selectable(
                this,
                tr("Skin Upload"),
                tr("You cannot use an invalid URL for uploading skins."),
                QMessageBox::Warning
            )->exec();
            close();
            return;
        }
    }
    else
    {
        // just assume it's a path then
        isLocalFile = true;
        fileName = ui->skinPathTextBox->text();
    }
    if (isLocalFile && !QFile::exists(fileName))
    {
        CustomMessageBox::selectable(this, tr("Skin Upload"), tr("Skin file does not exist!"), 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;
    }
    ProgressDialog prog(this);
    SequentialTask skinUpload;
    skinUpload.addTask(shared_qobject_ptr<SkinUpload>(new SkinUpload(this, m_acct->accessToken(), FS::read(fileName), model)));
    auto selectedCape = ui->capeCombo->currentData().toString();
    if(selectedCape != m_acct->accountData()->minecraftProfile.currentCape) {
        skinUpload.addTask(shared_qobject_ptr<CapeChange>(new CapeChange(this, m_acct->accessToken(), selectedCape)));
    }
    if (prog.execWithTask(&skinUpload) != QDialog::Accepted)
    {
        CustomMessageBox::selectable(this, tr("Skin Upload"), tr("Failed to upload skin!"), QMessageBox::Warning)->exec();
        close();
        return;
    }
    CustomMessageBox::selectable(this, tr("Skin Upload"), tr("Success"), QMessageBox::Information)->exec();
    close();
}

void SkinUploadDialog::on_skinBrowseBtn_clicked()
{
    auto filter = QMimeDatabase().mimeTypeForName("image/png").filterString();
    QString raw_path = QFileDialog::getOpenFileName(this, tr("Select Skin Texture"), QString(), filter);
    if (raw_path.isEmpty() || !QFileInfo::exists(raw_path))
    {
        return;
    }
    QString cooked_path = FS::NormalizePath(raw_path);
    ui->skinPathTextBox->setText(cooked_path);
}

SkinUploadDialog::SkinUploadDialog(MinecraftAccountPtr acct, QWidget *parent)
    :QDialog(parent), m_acct(acct), ui(new Ui::SkinUploadDialog)
{
    ui->setupUi(this);

    // FIXME: add a model for this, download/refresh the capes on demand
    auto &data = *acct->accountData();
    int index = 0;
    ui->capeCombo->addItem(tr("No Cape"), QVariant());
    auto currentCape = data.minecraftProfile.currentCape;
    if(currentCape.isEmpty()) {
        ui->capeCombo->setCurrentIndex(index);
    }

    for(auto & cape: data.minecraftProfile.capes) {
        index++;
        if(cape.data.size()) {
            QPixmap capeImage;
            if(capeImage.loadFromData(cape.data, "PNG")) {
                QPixmap preview = QPixmap(10, 16);
                QPainter painter(&preview);
                painter.drawPixmap(0, 0, capeImage.copy(1, 1, 10, 16));
                ui->capeCombo->addItem(capeImage, cape.alias, cape.id);
                if(currentCape == cape.id) {
                    ui->capeCombo->setCurrentIndex(index);
                }
                continue;
            }
        }
        ui->capeCombo->addItem(cape.alias, cape.id);
        if(currentCape == cape.id) {
            ui->capeCombo->setCurrentIndex(index);
        }
    }
}