blob: 8ce4e04067425e6fedb4655dbed9dde4c58b8640 [file] [log] [blame]
Edric Milaret627500d2015-03-27 16:41:40 -04001/***************************************************************************
Anthony Léonard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 by Savoir-faire Linux *
Edric Milaret627500d2015-03-27 16:41:40 -04003 * Author: Edric Ladent Milaret <edric.ladent-milaret@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 "accountdetails.h"
20#include "ui_accountdetails.h"
21
22#include <QSortFilterProxyModel>
Edric Milaret3e6aefe2015-06-05 16:07:26 -040023#include <QFileDialog>
24#include <QPushButton>
Anthony Léonardaa90e1a2016-10-12 11:24:17 -040025#include <QMessageBox>
Edric Milaret627500d2015-03-27 16:41:40 -040026
Edric Milarete82782e2016-03-21 12:14:17 -040027#include "codecmodel.h"
Edric Milaret3e6aefe2015-06-05 16:07:26 -040028#include "protocolmodel.h"
29#include "certificate.h"
30#include "ciphermodel.h"
Edric Milaretb1b00ce2016-02-03 14:10:05 -050031#include "ringtonemodel.h"
Edric Milaret627500d2015-03-27 16:41:40 -040032
33AccountDetails::AccountDetails(QWidget *parent) :
34 QWidget(parent),
35 ui(new Ui::AccountDetails),
36 codecModel_(nullptr),
Edric Milaret2d03da42015-07-15 15:36:43 -040037 currentAccount_(nullptr)
Edric Milaret627500d2015-03-27 16:41:40 -040038{
39 ui->setupUi(this);
40
Edric Milaretc9d3e412015-08-11 15:43:04 -040041 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
42
Nicolas Jager584a80e2016-03-18 16:10:00 -040043 connect(ui->lrcfg_tlsCaListCertificate, &RingButton::clicked, this, &AccountDetails::onCertButtonClicked);
44 connect(ui->lrcfg_tlsCertificate, &RingButton::clicked, this, &AccountDetails::onCertButtonClicked);
45 connect(ui->lrcfg_tlsPrivateKeyCertificate, &RingButton::clicked, this, &AccountDetails::onCertButtonClicked);
Edric Milaretdda49b62016-02-05 14:27:38 -050046
47 connect(&RingtoneModel::instance(),
48 &RingtoneModel::dataChanged,
49 [=](const QModelIndex& topLeft, const QModelIndex& bottomRight) {
50 Q_UNUSED(topLeft)
51 Q_UNUSED(bottomRight)
52 if (not currentAccount_)
53 return;
54 if (RingtoneModel::instance().isPlaying())
55 ui->playButton->setText(tr("Pause"));
56 else
57 ui->playButton->setText(tr("Play"));
58
59 });
Nicolas Jagere6384052016-03-10 13:13:24 -050060
Nicolas Jagere6384052016-03-10 13:13:24 -050061 connect(ui->defaultCipherCheckBox, &QCheckBox::stateChanged, [=] (int state) {
62 if (state == Qt::Checked) {
63 ui->cipherListView->setVisible(false);
64 currentAccount_->cipherModel()->setUseDefault(true);
65 } else {
66 ui->cipherListView->setVisible(true);
67 currentAccount_->cipherModel()->setUseDefault(false);
68 }
69 });
Edric Milaret13438312016-03-18 13:27:46 -040070
71 connect(ui->lrcfg_alias, &QLineEdit::textEdited, [=](const QString& newAlias) {
72 if (currentAccount_ && currentAccount_->protocol() == Account::Protocol::RING)
73 currentAccount_->setDisplayName(newAlias);
74 });
Anthony Léonard5107a692016-11-04 13:20:37 -040075
76 connect(ui->lrcfg_nameServiceURL, &QLineEdit::textEdited, [=](const QString& newNSURL) {
77 if (currentAccount_ && currentAccount_->protocol() == Account::Protocol::RING)
78 currentAccount_->setNameServiceURL(newNSURL);
79 });
Edric Milaret627500d2015-03-27 16:41:40 -040080}
81
82AccountDetails::~AccountDetails()
83{
84 delete ui;
85}
86
87void
Edric Milaret627500d2015-03-27 16:41:40 -040088AccountDetails::setAccount(Account* currentAccount) {
89
Edric Milaret3e6aefe2015-06-05 16:07:26 -040090 if (currentAccount_) {
Edric Milaretdda49b62016-02-05 14:27:38 -050091 stopRingtone();
92 save();
Edric Milaret3e6aefe2015-06-05 16:07:26 -040093 }
94
Edric Milaret627500d2015-03-27 16:41:40 -040095 currentAccount_ = currentAccount;
96
Edric Milaretce0ea472016-04-12 10:16:56 -040097 if (currentAccount_ == nullptr)
98 return;
99
Anthony Léonardaa90e1a2016-10-12 11:24:17 -0400100 if (currentAccount_->protocol() == Account::Protocol::RING) {
Anthony Léonard44338762016-11-15 11:22:27 -0500101 ui->usernameLabel->setText(tr("RingID"));
102 ui->lrcfg_username->setReadOnly(true);
Anthony Léonardaa90e1a2016-10-12 11:24:17 -0400103 if (currentAccount_->registeredName().isEmpty() ){ // If our user isn't registered on the blockhain
104 ui->lrcfg_registeredName->clear();
105 ui->lrcfg_registeredName->setReadOnly(false);
106 ui->registerButton->setText(tr("Register on blockchain"));
107 ui->registerButton->show();
108 ui->registerButton->setEnabled(true);
109 } else {
110 ui->lrcfg_registeredName->setText(currentAccount_->registeredName());
111 ui->lrcfg_registeredName->setReadOnly(true);
112 ui->registerButton->hide();
113 }
Anthony Léonard44338762016-11-15 11:22:27 -0500114 } else { // If currentAccount_ is of type SIP
115 ui->usernameLabel->setText(tr("Username"));
116 ui->lrcfg_username->setReadOnly(false);
Anthony Léonardaa90e1a2016-10-12 11:24:17 -0400117 }
Edric Milareted0b2802015-10-01 15:10:02 -0400118
Edric Milaret627500d2015-03-27 16:41:40 -0400119 codecModel_ = currentAccount->codecModel();
Edric Milarete5313852015-11-09 13:06:00 -0500120 ui->audioCodecView->setModel(codecModel_->audioCodecs());
121 ui->videoCodecView->setModel(codecModel_->videoCodecs());
Edric Milaret25236d92016-03-28 09:40:58 -0400122
Edric Milarete5313852015-11-09 13:06:00 -0500123 connect(ui->audioCodecView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
124 this, SLOT(audioCodecSelectionChanged(QItemSelection,QItemSelection)));
125 connect(ui->videoCodecView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
126 this, SLOT(videoCodecSelectionChanged(QItemSelection,QItemSelection)));
Edric Milaret627500d2015-03-27 16:41:40 -0400127
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400128 ui->typeValueLabel->setText(currentAccount_->protocolModel()->
129 selectionModel()->currentIndex().data().value<QString>());
Edric Milaret627500d2015-03-27 16:41:40 -0400130
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400131 ui->publishGroup->disconnect();
Edric Milaret627500d2015-03-27 16:41:40 -0400132
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400133 if (currentAccount_->isPublishedSameAsLocal())
134 ui->puslishedSameAsLocalRadio->setChecked(true);
135 else
136 ui->customPublishedRadio->setChecked(true);
137
138 ui->publishGroup->setId(ui->puslishedSameAsLocalRadio, 1);
139 ui->publishGroup->setId(ui->customPublishedRadio, 0);
140 ui->lrcfg_publishedAddress->setEnabled(!currentAccount_->isPublishedSameAsLocal());
141 ui->lrcfg_publishedPort->setEnabled(!currentAccount_->isPublishedSameAsLocal());
142
Nicolas Jagere6384052016-03-10 13:13:24 -0500143 connect(ui->publishGroup,
144 static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
145 [=](int id) {
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400146 currentAccount_->setPublishedSameAsLocal(static_cast<bool>(id));
147 });
148
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400149 if (currentAccount_->tlsCaListCertificate())
Edric Milaret4f0b02c2015-08-14 11:40:58 -0400150 ui->lrcfg_tlsCaListCertificate->setText(currentAccount_->tlsCaListCertificate()->path());
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400151 if (currentAccount_->tlsCertificate())
Edric Milaret4f0b02c2015-08-14 11:40:58 -0400152 ui->lrcfg_tlsCertificate->setText(currentAccount_->tlsCertificate()->path());
153 if (not currentAccount_->tlsPrivateKey().isEmpty())
154 ui->lrcfg_tlsPrivateKeyCertificate->setText(currentAccount_->tlsPrivateKey());
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400155
Edric Milaret36587362016-02-04 12:30:52 -0500156#ifdef Q_OS_WIN
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400157 certMap_[ui->lrcfg_tlsCaListCertificate->objectName()] = &currentAccount_->setTlsCaListCertificate;
158 certMap_[ui->lrcfg_tlsCertificate->objectName()] = &currentAccount_->setTlsCertificate;
Edric Milaret4f0b02c2015-08-14 11:40:58 -0400159 certMap_[ui->lrcfg_tlsPrivateKeyCertificate->objectName()] = &currentAccount_->setTlsPrivateKey;
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400160#endif
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400161
Olivier SOLDANOee351812017-07-07 08:34:41 -0400162 ui->srtpEnabledChkBox->disconnect();
163 connect(ui->srtpEnabledChkBox, &QCheckBox::toggled, [=](bool checked) {
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400164 currentAccount_->setSrtpEnabled(checked);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400165 });
166
Olivier SOLDANOee351812017-07-07 08:34:41 -0400167 ui->srtpEnabledChkBox->setChecked(currentAccount_->isSrtpEnabled());
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400168
169 if (currentAccount_->cipherModel()->useDefault())
Nicolas Jagere6384052016-03-10 13:13:24 -0500170 ui->defaultCipherCheckBox->setChecked(true);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400171 else
Nicolas Jagere6384052016-03-10 13:13:24 -0500172 ui->defaultCipherCheckBox->setChecked(false);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400173
174 ui->cipherListView->setModel(currentAccount_->cipherModel());
Edric Milaretb1b00ce2016-02-03 14:10:05 -0500175
176 disconnect(ui->ringtonesBox);
177 ui->ringtonesBox->setModel(&RingtoneModel::instance());
178 ui->ringtonesBox->setCurrentIndex(RingtoneModel::instance().selectionModel(currentAccount_)->currentIndex().row());
179 connect(ui->ringtonesBox, SIGNAL(currentIndexChanged(int)), this, SLOT(ringtonesBoxCurrentIndexChanged(int)));
Nicolas Jagere6384052016-03-10 13:13:24 -0500180
181 auto accountProtocol = currentAccount_->protocol();
182 if (accountProtocol == Account::Protocol::RING) {
Olivier SOLDANOee351812017-07-07 08:34:41 -0400183 ui->srtpEnabledChkBox->hide();
Anthony Léonard5107a692016-11-04 13:20:37 -0400184 ui->nameServiceURLLabel->show();
185 ui->lrcfg_nameServiceURL->show();
186 ui->lrcfg_nameServiceURL->setText(currentAccount_->nameServiceURL());
Nicolas Jagere6384052016-03-10 13:13:24 -0500187 } else if (accountProtocol == Account::Protocol::SIP) {
Olivier SOLDANOee351812017-07-07 08:34:41 -0400188 ui->srtpEnabledChkBox->show();
Anthony Léonard5107a692016-11-04 13:20:37 -0400189 ui->nameServiceURLLabel->hide();
190 ui->lrcfg_nameServiceURL->hide();
Nicolas Jagere6384052016-03-10 13:13:24 -0500191 }
192
Nicolas Jagere6384052016-03-10 13:13:24 -0500193 if (ui->defaultCipherCheckBox->checkState() == Qt::Checked)
194 ui->cipherListView->setVisible(false);
195 else
196 ui->cipherListView->setVisible(true);
Edric Milaret57467842016-08-30 13:06:11 -0400197
198 ui->tableView->setModel((QAbstractItemModel*)currentAccount_->ringDeviceModel());
Olivier SOLDANO5d4a1ff2017-05-08 13:12:47 -0400199 ui->bannedContactsWidget->setAccount(currentAccount_);
Edric Milaret627500d2015-03-27 16:41:40 -0400200}
201
202void
Edric Milarete5313852015-11-09 13:06:00 -0500203AccountDetails::on_upAudioButton_clicked() {
Edric Milaret627500d2015-03-27 16:41:40 -0400204 codecModel_->moveUp();
Edric Milaret627500d2015-03-27 16:41:40 -0400205}
206
207void
Edric Milarete5313852015-11-09 13:06:00 -0500208AccountDetails::on_downAudioButton_clicked() {
Edric Milaret627500d2015-03-27 16:41:40 -0400209 codecModel_->moveDown();
Edric Milaret627500d2015-03-27 16:41:40 -0400210}
211
212void
Edric Milarete5313852015-11-09 13:06:00 -0500213AccountDetails::on_upVideoButton_clicked() {
Edric Milaret627500d2015-03-27 16:41:40 -0400214 codecModel_->moveUp();
Edric Milaret627500d2015-03-27 16:41:40 -0400215}
216
217void
Edric Milarete5313852015-11-09 13:06:00 -0500218AccountDetails::on_downVideoButton_clicked() {
Edric Milaret627500d2015-03-27 16:41:40 -0400219 codecModel_->moveDown();
Edric Milaret627500d2015-03-27 16:41:40 -0400220}
221
222void
223AccountDetails::save() {
Edric Milaret2d03da42015-07-15 15:36:43 -0400224 codecModel_->performAction(CodecModel::EditAction::SAVE);
Edric Milaret627500d2015-03-27 16:41:40 -0400225}
226
227void
Edric Milarete5313852015-11-09 13:06:00 -0500228AccountDetails::onCertButtonClicked() {
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400229 QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"),
Nicolas Jagere6384052016-03-10 13:13:24 -0500230 "",
231 tr("Files (*)"));
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400232
233 auto sender = QObject::sender();
234
235 (currentAccount_->*certMap_[sender->objectName()])(fileName);
Edric Milaret9f6b5192016-02-02 15:14:27 -0500236 if (not fileName.isEmpty())
237 static_cast<QPushButton*>(sender)->setText(fileName);
Edric Milarete6538792015-05-08 11:51:01 -0400238}
Edric Milarete5313852015-11-09 13:06:00 -0500239
240void
241AccountDetails::audioCodecSelectionChanged(const QItemSelection& selected,
242 const QItemSelection& deselected) {
243 Q_UNUSED(deselected)
244 if (not codecModel_ || selected.empty())
245 return;
246 auto idx = codecModel_->audioCodecs()->mapToSource(selected.indexes().at(0));
247 codecModel_->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
248}
249
250void
251AccountDetails::videoCodecSelectionChanged(const QItemSelection& selected,
252 const QItemSelection& deselected) {
253 Q_UNUSED(deselected)
254 if (not codecModel_ || selected.empty())
255 return;
256 auto idx = codecModel_->videoCodecs()->mapToSource(selected.indexes().at(0));
257 codecModel_->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
258}
Edric Milaretb1b00ce2016-02-03 14:10:05 -0500259
260void
261AccountDetails::ringtonesBoxCurrentIndexChanged(int index)
262{
263 RingtoneModel::instance().selectionModel(currentAccount_)->setCurrentIndex(
264 RingtoneModel::instance().index(index, 0), QItemSelectionModel::ClearAndSelect);
265}
266
267void
268AccountDetails::on_playButton_clicked()
269{
270 RingtoneModel::instance().play(RingtoneModel::instance().index(
271 ui->ringtonesBox->currentIndex(), 0));
272}
Edric Milaretdda49b62016-02-05 14:27:38 -0500273
274void
275AccountDetails::stopRingtone() {
276 if (not currentAccount_)
277 return;
278 auto idx = RingtoneModel::instance().selectionModel(currentAccount_)->currentIndex();
279 if (RingtoneModel::instance().isPlaying())
280 RingtoneModel::instance().play(idx);
281}
Nicolas Jager74fe46f2016-02-29 14:55:09 -0500282
Edric Milaret57467842016-08-30 13:06:11 -0400283void
284AccountDetails::on_addDeviceButton_clicked()
285{
286 ui->devicesStackedWidget->setCurrentIndex(1);
287}
288
289void
290AccountDetails::on_cancelButton_clicked()
291{
292 ui->devicesStackedWidget->setCurrentIndex(0);
293}
294
295void
296AccountDetails::on_exportOnRingButton_clicked()
297{
298 if (ui->passwordArchiveEdit->text().isEmpty())
299 return;
300
301 connect(currentAccount_, SIGNAL(exportOnRingEnded(Account::ExportOnRingStatus,QString)), this, SLOT(exportOnRingEnded(Account::ExportOnRingStatus,QString)));
302 currentAccount_->exportOnRing(ui->passwordArchiveEdit->text());
303 ui->devicesStackedWidget->setCurrentIndex(2);
304 ui->pinLabel->setText(tr("Please wait while your PIN is generated."));
305}
306
307void
308AccountDetails::exportOnRingEnded(Account::ExportOnRingStatus state, const QString& pin) {
309
310 ui->devicesStackedWidget->setCurrentIndex(2);
311
312 ui->pinLabel->clear();
313
314 switch (state) {
315 case Account::ExportOnRingStatus::SUCCESS:
316 {
317 ui->pinLabel->setText(pin);
Anthony Léonard39374992016-10-21 13:39:55 -0400318 ui->pinLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
Edric Milaret57467842016-08-30 13:06:11 -0400319 break;
320 }
321 case Account::ExportOnRingStatus::NETWORK_ERROR:
322 {
323 ui->pinLabel->setText(tr("Network Error. Please try again later."));
324 break;
325 }
326 case Account::ExportOnRingStatus::WRONG_PASSWORD:
327 ui->pinLabel->setText(tr("Wrong password."));
328 break;
329 }
330}
331
332void
333AccountDetails::on_exportEndedOkButton_clicked()
334{
335 ui->devicesStackedWidget->setCurrentIndex(0);
336}
337
338void
339AccountDetails::on_cancelAddButton_clicked()
340{
341 ui->devicesStackedWidget->setCurrentIndex(0);
342}
Anthony Léonard59db8ce2016-10-19 10:54:11 -0400343
344void AccountDetails::on_devicesStackedWidget_currentChanged(int pageNum)
345{
346 // We clear the password textEdit each time we leave its page
347 if (pageNum != ui->devicesStackedWidget->indexOf(ui->passwordAskingPage))
348 ui->passwordArchiveEdit->clear();
349}
Anthony Léonardaa90e1a2016-10-12 11:24:17 -0400350
351void AccountDetails::on_registerButton_clicked()
352{
353 ui->registerButton->setEnabled(false);
354 ui->registerButton->setText(tr("Registering... It may take some time"));
355 bool regSuccess = currentAccount_->registerName(ui->lrcfg_password->text(), ui->lrcfg_registeredName->text());
356 if (!regSuccess) {
357 QMessageBox::warning(this, "Username not registered", "Username registration failed, try again later.");
358 ui->registerButton->setEnabled(true);
359 ui->registerButton->setText(tr("Register on blockchain"));
360 return;
361 }
362
363 connect(currentAccount_, SIGNAL(nameRegistrationEnded(NameDirectory::RegisterNameStatus,QString)),
364 this, SLOT(handle_nameRegistrationEnded(NameDirectory::RegisterNameStatus,QString)));
365}
366
367
368void AccountDetails::handle_nameRegistrationEnded(NameDirectory::RegisterNameStatus status, const QString& name)
369{
370 disconnect(currentAccount_, SIGNAL(nameRegistrationEnded(NameDirectory::RegisterNameStatus,QString)),
371 this, SLOT(handle_nameRegistrationEnded(NameDirectory::RegisterNameStatus,QString)));
372 switch(status) {
373 case NameDirectory::RegisterNameStatus::ALREADY_TAKEN:
374 QMessageBox::warning(this, "Username not registered", "This username is already taken, try another one.");
375 ui->registerButton->setEnabled(true);
376 ui->registerButton->setText(tr("Register on blockchain"));
377 break;
378 case NameDirectory::RegisterNameStatus::INVALID_NAME:
379 QMessageBox::warning(this, "Username not registered", "This username is invalid, try another one.");
380 ui->registerButton->setEnabled(true);
381 ui->registerButton->setText(tr("Register on blockchain"));
382 break;
383 case NameDirectory::RegisterNameStatus::WRONG_PASSWORD:
384 QMessageBox::warning(this, "Username not registered", "Wrong password, try again.");
385 ui->registerButton->setEnabled(true);
386 ui->registerButton->setText(tr("Register on blockchain"));
387 break;
388 case NameDirectory::RegisterNameStatus::NETWORK_ERROR:
389 QMessageBox::warning(this, "Username not registered", "Network error. Try again later.");
390 ui->registerButton->setEnabled(true);
391 ui->registerButton->setText(tr("Register on blockchain"));
392 break;
393 case NameDirectory::RegisterNameStatus::SUCCESS:
394 ui->lrcfg_registeredName->setReadOnly(true);
395 ui->registerButton->hide();
396 QMessageBox::information(this, "Username registered", name + " is registered, you can now share this name.");
397 break;
398 }
399
400}