blob: 8d89badbc21a5260e13b0100b951c75fb19f4c71 [file] [log] [blame]
Isa Nanic6e4a39a2018-12-04 14:26:02 -05001/***************************************************************************
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 <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "linkdevwidget.h"
20
21#include <QMovie>
Isa Nanic281d13a2018-12-11 18:55:02 -050022#include <QTimer>
Isa Nanic6e4a39a2018-12-04 14:26:02 -050023
24LinkDevWidget::LinkDevWidget(QWidget* parent)
25 : QWidget(parent),
26 ui(new Ui::LinkDevWidget)
27{
28 ui->setupUi(this);
29
30 if (LRCInstance::getCurrAccConfig().archiveHasPassword) {
31 ui->stackedWidget->setCurrentWidget(ui->start);
32 } else {
33 setGeneratingPage();
34 }
35
Isa Nanic281d13a2018-12-11 18:55:02 -050036 connect(ui->enterBtn, &QPushButton::clicked,
37 this, &LinkDevWidget::setGeneratingPage);
Isa Nanic6e4a39a2018-12-04 14:26:02 -050038 connect(&LRCInstance::accountModel(), &lrc::api::NewAccountModel::exportOnRingEnded,
Isa Nanic281d13a2018-12-11 18:55:02 -050039 this, &LinkDevWidget::setExportPage);
Isa Nanic6e4a39a2018-12-04 14:26:02 -050040}
41
42LinkDevWidget::~LinkDevWidget()
43{
44 delete ui;
45}
46
47void
48LinkDevWidget::setGeneratingPage()
49{
50 ui->stackedWidget->setCurrentWidget(ui->middle);
51
52 QMovie* movie = new QMovie(":/images/ajax-loader.gif");
53 ui->spinningLabel->setMovie(movie);
54 ui->spinningLabel->show();
55 movie->start();
56
Isa Nanic281d13a2018-12-11 18:55:02 -050057 timeout_ = new QTimer(this);
58 timeout_->setInterval(exportTimeout_);
59 timeout_->setSingleShot(true);
60 connect(timeout_, &QTimer::timeout, this,
61 [&]() {
62 setExportPage(std::string(),
63 lrc::api::account::ExportOnRingStatus::NETWORK_ERROR,
64 std::string());
65 });
66 timeout_->start();
67
Isa Nanic6e4a39a2018-12-04 14:26:02 -050068 LRCInstance::accountModel().exportOnRing(LRCInstance::getCurrAccId(), ui->passwordLineEdit->text().toStdString());
69}
70
71void
72LinkDevWidget::setExportPage(const std::string& accountId, lrc::api::account::ExportOnRingStatus status, const std::string& pin)
73{
74 Q_UNUSED(accountId);
Isa Nanic281d13a2018-12-11 18:55:02 -050075 timeout_->stop();
76
Isa Nanic6e4a39a2018-12-04 14:26:02 -050077 ui->stackedWidget->setCurrentWidget(ui->end);
78
79 switch (status) {
80 case lrc::api::account::ExportOnRingStatus::WRONG_PASSWORD:
81 ui->label_4->setText(tr("Your account password was incorrect"));
82 ui->label_5->hide();
83 ui->label_6->hide();
84 break;
85
86 case lrc::api::account::ExportOnRingStatus::SUCCESS:
87 ui->label_5->setText(QString::fromStdString(pin));
88 break;
89
90 default:
Isa Nanic281d13a2018-12-11 18:55:02 -050091 disconnect();
Isa Nanic6e4a39a2018-12-04 14:26:02 -050092 ui->label_4->setText(tr("Something went wrong.\nPlease try again later."));
93 ui->label_5->hide();
94 ui->label_6->hide();
95 }
96}