accountdetails.cpp 7.89 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
Edric Milaret's avatar
Edric Milaret committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Author: Edric Ladent Milaret <edric.ladent-milaret@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 "accountdetails.h"
#include "ui_accountdetails.h"

#include <QSortFilterProxyModel>
23 24
#include <QFileDialog>
#include <QPushButton>
Edric Milaret's avatar
Edric Milaret committed
25 26 27

#include "accountdetails.h"
#include "audio/codecmodel.h"
28 29 30
#include "protocolmodel.h"
#include "certificate.h"
#include "ciphermodel.h"
Edric Milaret's avatar
Edric Milaret committed
31 32 33 34 35

AccountDetails::AccountDetails(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AccountDetails),
    codecModel_(nullptr),
Edric Milaret's avatar
Edric Milaret committed
36
    currentAccount_(nullptr)
Edric Milaret's avatar
Edric Milaret committed
37 38 39
{
    ui->setupUi(this);

40 41
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);

Edric Milaret's avatar
Edric Milaret committed
42
    ui->audioCodecView->setShowGrid(false);
43 44
    ui->audioCodecView->verticalHeader()->hide();
    ui->audioCodecView->horizontalHeader()->hide();
Edric Milaret's avatar
Edric Milaret committed
45
    ui->videoCodecView->setShowGrid(false);
46
    ui->videoCodecView->verticalHeader()->hide();
Edric Milaret's avatar
Edric Milaret committed
47
    ui->videoCodecView->horizontalHeader()->hide();
Edric Milaret's avatar
Edric Milaret committed
48

49
    ui->lrcfg_username->setAlignment(Qt::AlignCenter);
Edric Milaret's avatar
Edric Milaret committed
50

51 52 53
    connect(ui->lrcfg_tlsCaListCertificate, SIGNAL(clicked(bool)), this, SLOT(onCertButtonClicked()));
    connect(ui->lrcfg_tlsCertificate, SIGNAL(clicked(bool)), this, SLOT(onCertButtonClicked()));
    connect(ui->lrcfg_tlsPrivateKeyCertificate, SIGNAL(clicked(bool)), this, SLOT(onCertButtonClicked()));
Edric Milaret's avatar
Edric Milaret committed
54 55 56 57 58 59 60 61 62 63
}

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

void
AccountDetails::setAccount(Account* currentAccount) {

64 65 66 67
    if (currentAccount_) {
        currentAccount_->performAction(Account::EditAction::SAVE);
    }

Edric Milaret's avatar
Edric Milaret committed
68 69
    currentAccount_ = currentAccount;

70 71
    ui->lrcfg_username->setReadOnly(currentAccount_->protocol() == Account::Protocol::RING);

Edric Milaret's avatar
Edric Milaret committed
72
    codecModel_ = currentAccount->codecModel();
73 74 75 76 77 78
    ui->audioCodecView->setModel(codecModel_->audioCodecs());
    ui->videoCodecView->setModel(codecModel_->videoCodecs());
    connect(ui->audioCodecView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(audioCodecSelectionChanged(QItemSelection,QItemSelection)));
    connect(ui->videoCodecView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(videoCodecSelectionChanged(QItemSelection,QItemSelection)));
Edric Milaret's avatar
Edric Milaret committed
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    ui->typeValueLabel->setText(currentAccount_->protocolModel()->
                                selectionModel()->currentIndex().data().value<QString>());

    ui->publishGroup->disconnect();

    if (currentAccount_->isPublishedSameAsLocal())
        ui->puslishedSameAsLocalRadio->setChecked(true);
    else
        ui->customPublishedRadio->setChecked(true);

    ui->publishGroup->setId(ui->puslishedSameAsLocalRadio, 1);
    ui->publishGroup->setId(ui->customPublishedRadio, 0);
    ui->lrcfg_publishedAddress->setEnabled(!currentAccount_->isPublishedSameAsLocal());
    ui->lrcfg_publishedPort->setEnabled(!currentAccount_->isPublishedSameAsLocal());

    connect(ui->publishGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
        [=](int id) {
        currentAccount_->setPublishedSameAsLocal(static_cast<bool>(id));
    });

    switch (currentAccount_->DTMFType()) {
    case DtmfType::OverRtp:
        ui->rtpRadio->setChecked(true);
Edric Milaret's avatar
Edric Milaret committed
103
        break;
104 105
    case DtmfType::OverSip:
        ui->sipRadio->setChecked(true);
Edric Milaret's avatar
Edric Milaret committed
106 107 108
        break;
    }

109 110 111 112 113 114 115 116 117
    ui->dtmfGroup->disconnect();
    ui->dtmfGroup->setId(ui->rtpRadio, DtmfType::OverRtp);
    ui->dtmfGroup->setId(ui->sipRadio, DtmfType::OverSip);

    connect(ui->dtmfGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
        [=](int id){ currentAccount_->setDTMFType(static_cast<DtmfType>(id)); });

    ui->keyExchangeModelCombo->setModel(currentAccount_->keyExchangeModel());
    ui->tlsProtocoCombo->setModel(currentAccount_->tlsMethodModel());
Edric Milaret's avatar
Edric Milaret committed
118

119
    if (currentAccount_->tlsCaListCertificate())
120
        ui->lrcfg_tlsCaListCertificate->setText(currentAccount_->tlsCaListCertificate()->path());
121
    if (currentAccount_->tlsCertificate())
122 123 124
        ui->lrcfg_tlsCertificate->setText(currentAccount_->tlsCertificate()->path());
    if (not currentAccount_->tlsPrivateKey().isEmpty())
        ui->lrcfg_tlsPrivateKeyCertificate->setText(currentAccount_->tlsPrivateKey());
125

126
#ifdef Q_OS_WIN
127 128
    certMap_[ui->lrcfg_tlsCaListCertificate->objectName()] = &currentAccount_->setTlsCaListCertificate;
    certMap_[ui->lrcfg_tlsCertificate->objectName()] = &currentAccount_->setTlsCertificate;
129
    certMap_[ui->lrcfg_tlsPrivateKeyCertificate->objectName()] = &currentAccount_->setTlsPrivateKey;
Edric Milaret's avatar
Edric Milaret committed
130
#endif
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    ui->srtpEnabled->disconnect();
    connect(ui->srtpEnabled, &QCheckBox::toggled, [=](bool checked) {
        currentAccount_->setSrtpEnabled(checked);
        ui->lrcfg_srtpRtpFallback->setEnabled(checked);
        ui->keyExchangeModelCombo->setEnabled(checked);
    });

    ui->srtpEnabled->setChecked(currentAccount_->isSrtpEnabled());

    if (currentAccount_->cipherModel()->useDefault())
        ui->defaultCipherRadio->setChecked(true);
    else
        ui->customCipherRadio->setChecked(true);

    connect(ui->defaultCipherRadio, &QRadioButton::toggled, [=](bool checked) {
        currentAccount_->cipherModel()->setUseDefault(checked);
    });

    ui->cipherListView->setModel(currentAccount_->cipherModel());
Edric Milaret's avatar
Edric Milaret committed
151 152 153
}

void
154
AccountDetails::on_upAudioButton_clicked() {
Edric Milaret's avatar
Edric Milaret committed
155 156 157 158
    codecModel_->moveUp();
}

void
159
AccountDetails::on_downAudioButton_clicked() {
Edric Milaret's avatar
Edric Milaret committed
160 161 162 163
    codecModel_->moveDown();
}

void
164
AccountDetails::on_upVideoButton_clicked() {
Edric Milaret's avatar
Edric Milaret committed
165 166 167 168
    codecModel_->moveUp();
}

void
169
AccountDetails::on_downVideoButton_clicked() {
Edric Milaret's avatar
Edric Milaret committed
170 171 172 173 174
    codecModel_->moveDown();
}

void
AccountDetails::save() {
Edric Milaret's avatar
Edric Milaret committed
175
    codecModel_->performAction(CodecModel::EditAction::SAVE);
Edric Milaret's avatar
Edric Milaret committed
176 177 178
}

void
179
AccountDetails::onCertButtonClicked() {
180 181 182 183 184 185 186
    QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"),
    "",
    tr("Files (*)"));

    auto sender = QObject::sender();

    (currentAccount_->*certMap_[sender->objectName()])(fileName);
187 188
    if (not fileName.isEmpty())
        static_cast<QPushButton*>(sender)->setText(fileName);
Edric Milaret's avatar
Edric Milaret committed
189
}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

void
AccountDetails::audioCodecSelectionChanged(const QItemSelection& selected,
                                           const QItemSelection& deselected) {
    Q_UNUSED(deselected)
    if (not codecModel_ || selected.empty())
        return;
    auto idx = codecModel_->audioCodecs()->mapToSource(selected.indexes().at(0));
    codecModel_->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
}

void
AccountDetails::videoCodecSelectionChanged(const QItemSelection& selected,
                                           const QItemSelection& deselected) {
    Q_UNUSED(deselected)
    if (not codecModel_ || selected.empty())
        return;
    auto idx = codecModel_->videoCodecs()->mapToSource(selected.indexes().at(0));
    codecModel_->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
}