Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * Copyright (C) 2018 by Savoir-faire Linux * |
| 3 | * Author: Isa Nanic <isa.nanic@savoirfairelinux.com> * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "regnamedialog.h" |
| 20 | #include "ui_regnamedialog.h" |
| 21 | |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 22 | #include <QTimer> |
| 23 | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 24 | RegNameDialog::RegNameDialog(const QString& newRegName, QWidget* parent) |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 25 | :QDialog(parent), |
| 26 | ui(new Ui::RegNameDialog), |
| 27 | registeredName_(newRegName), |
| 28 | gif(new QMovie(":/images/ajax-loader.gif")) |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 29 | { |
| 30 | ui->setupUi(this); |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 31 | |
| 32 | ui->stackedWidget->setCurrentWidget(ui->startPage); |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 33 | ui->registeredName->setText(newRegName); |
| 34 | |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 35 | connect(ui->startPageConfirmBtn, &QPushButton::clicked, this, &RegNameDialog::startNameRegistration); |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 36 | |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 37 | connect(ui->startPageCancelBtn, &QPushButton::clicked, [this]() { |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 38 | reject(); |
| 39 | } |
| 40 | ); |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 41 | |
| 42 | // get name registration result |
| 43 | connect(LRCInstance::editableAccountModel(), &lrc::api::NewAccountModel::nameRegistrationEnded, |
| 44 | this, &RegNameDialog::nameRegistrationResultSlot); |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | RegNameDialog::~RegNameDialog() |
| 48 | { |
| 49 | disconnect(this); |
| 50 | delete ui; |
| 51 | } |
| 52 | |
Isa Nanic | 26c8661 | 2018-12-14 12:21:56 -0500 | [diff] [blame] | 53 | void |
| 54 | RegNameDialog::startNameRegistration() |
| 55 | { |
| 56 | LRCInstance::editableAccountModel()->registerName(LRCInstance::getCurrAccId(), |
| 57 | "", registeredName_.toStdString()); |
| 58 | startSpinner(); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | RegNameDialog::nameRegistrationResultSlot(const std::string& accountId, |
| 63 | lrc::api::account::RegisterNameStatus status, const std::string& registerdName) |
| 64 | { |
| 65 | gif->stop(); |
| 66 | |
| 67 | if(status == lrc::api::account::RegisterNameStatus::SUCCESS) { |
| 68 | ui->stackedWidget->setCurrentWidget(ui->nameRegisteredPage); |
| 69 | QTimer::singleShot(1000, this, &RegNameDialog::accept); |
| 70 | } else { |
| 71 | ui->stackedWidget->setCurrentWidget(ui->nameNotRegisteredPage); |
| 72 | QTimer::singleShot(1000, this, &RegNameDialog::reject); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | RegNameDialog::startSpinner() |
| 78 | { |
| 79 | ui->stackedWidget->setCurrentWidget(ui->loadingPage); |
| 80 | |
| 81 | ui->spinnerLabel->setMovie(gif); |
| 82 | gif->start(); |
| 83 | } |