accountmigrationdialog.cpp 6.62 KB
Newer Older
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/***************************************************************************
 * Copyright (C) 2015-2019 by Savoir-faire Linux                           *
 * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>              *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "accountmigrationdialog.h"
#include "ui_accountmigration.h"

#include <QTimer>
#include <QtConcurrent/QtConcurrent>

AccountMigrationDialog::AccountMigrationDialog(QWidget *parent, const std::string& accountId) :
    QDialog(parent),
    ui(new Ui::AccountMigrationDialog),
    accountId_(accountId)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    auto& avatarInfo = LRCInstance::accountModel().getAccountInfo(accountId_);

    auto managerUsername = QString::fromStdString(avatarInfo.confProperties.managerUsername);
    auto managerUri = QString::fromStdString(avatarInfo.confProperties.managerUri);
    auto username = QString::fromStdString(avatarInfo.confProperties.username);
    auto alias = QString::fromStdString(LRCInstance::accountModel().getAccountInfo(accountId_).profileInfo.alias);

    // manager uri
    if (!managerUri.isEmpty()) {
        ui->managerUriInputLabel->setText(managerUri);
    } else {
        ui->managerUriLabel->hide();
        ui->managerUriInputLabel->hide();
        ui->gridLayout->removeWidget(ui->managerUriLabel);
        ui->gridLayout->removeWidget(ui->managerUriInputLabel);
        ui->gridLayout->removeItem(ui->managerUriHorizontalSpacer);
    }
    // username
    if (!username.isEmpty()) {
        ui->usernameInputLabel->setText(username);
    } else {
        if (avatarInfo.confProperties.managerUsername.empty()) {
            ui->usernameLabel->hide();
            ui->usernameInputLabel->hide();
            ui->gridLayout->removeWidget(ui->usernameInputLabel);
            ui->gridLayout->removeWidget(ui->usernameLabel);
            ui->gridLayout->removeItem(ui->usernameHorizontalSpacer);
        } else {
            ui->usernameInputLabel->setText(managerUsername);
        }
    }

    // alias
    if (!alias.isEmpty()) {
        ui->aliasInputLabel->setText(alias);
    } else {
        ui->aliasLabel->hide();
        ui->aliasInputLabel->hide();
        ui->gridLayout->removeWidget(ui->aliasLabel);
        ui->gridLayout->removeWidget(ui->aliasInputLabel);
        ui->gridLayout->removeItem(ui->aliasHorizontalSpacer);
    }

    ui->avatarLabel->setPixmap(QPixmap::fromImage(Utils::accountPhoto(avatarInfo, ui->avatarLabel->size())));

    connect(ui->passwordInputLineEdit, &QLineEdit::editingFinished, this, &AccountMigrationDialog::slotPasswordEditingFinished);
    connect(ui->deleteAccountPushButton, &QAbstractButton::clicked, this, &AccountMigrationDialog::slotDeleteButtonClicked);
    connect(ui->migrationPushButton, &QAbstractButton::clicked, this, &AccountMigrationDialog::slotMigrationButtonClicked);
    connect(ui->passwordInputLineEdit, &QLineEdit::textChanged,
        [this](const QString& text) {
            if (text.isEmpty()) {
                ui->migrationPushButton->setDisabled(true);
            } else {
                ui->migrationPushButton->setDisabled(false);
            }
            password_ = text.toStdString();
        });

    ui->migrationPushButton->setDisabled(true);

    migrationSpinnerMovie_ = new QMovie(":/images/jami_eclipse_spinner.gif");
    ui->spinnerLabel->setMovie(migrationSpinnerMovie_);

    Utils::setStackWidget(ui->stackedWidget, ui->accountMigrationPage);
}

AccountMigrationDialog::~AccountMigrationDialog()
{
    delete ui;
}

void
AccountMigrationDialog::slotPasswordEditingFinished()
{
    password_ = ui->passwordInputLineEdit->text().toStdString();
}

void
AccountMigrationDialog::slotDeleteButtonClicked()
{
    LRCInstance::accountModel().removeAccount(accountId_);
    done(DialogCode::Accepted);
}

void
AccountMigrationDialog::slotMigrationButtonClicked()
{
    Utils::setStackWidget(ui->stackedWidget, ui->migrationWaitingPage);
    migrationSpinnerMovie_->start();

    connect(&LRCInstance::accountModel(), &lrc::api::NewAccountModel::migrationEnded,
        [this](const std::string& accountId, bool ok) {
            if (accountId_ != accountId) {
                return;
            }
            if (!ok) {
                auto spinnerGeometry = ui->spinnerLabel->frameGeometry();
                auto pixmap = Utils::generateTintedPixmap(":/images/icons/baseline-error_outline-24px.svg", RingTheme::urgentOrange_)
                    .scaled(spinnerGeometry.width(), spinnerGeometry.height());
                ui->spinnerLabel->setPixmap(pixmap);
                ui->progressLabel->setStyleSheet("color:red;");
                ui->progressLabel->setText(tr("Migration Failed"));
                QTimer::singleShot(1000, this,
                    [this] {
                        Utils::setStackWidget(ui->stackedWidget, ui->accountMigrationPage);
                        ui->passwordInputLineEdit->clear();
                        ui->spinnerLabel->setMovie(migrationSpinnerMovie_);
                        ui->progressLabel->setStyleSheet("color:black;");
                        ui->progressLabel->setText(tr("Migrating your Jami account..."));
                    });
            } else {
                done(DialogCode::Accepted);
            }
        });

    QtConcurrent::run(
        [this] {
            auto config = LRCInstance::accountModel().getAccountConfig(accountId_);
            config.archivePassword = password_;
            LRCInstance::accountModel().setAccountConfig(accountId_, config);
        });

}

void
AccountMigrationDialog::closeEvent(QCloseEvent *event)
{
    done(DialogCode::Rejected);
    QDialog::closeEvent(event);
}