accountdetails.cpp 12.3 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015 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->setColumnCount(4);
43
    QStringList audioHeader {tr("Enabled"), tr("Name"), tr("Bitrate"), tr("Samplerate")};
Edric Milaret's avatar
Edric Milaret committed
44 45 46 47 48 49
    ui->audioCodecView->setHorizontalHeaderLabels(audioHeader);
    ui->audioCodecView->verticalHeader()->hide();
    ui->audioCodecView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->audioCodecView->setShowGrid(false);

    ui->videoCodecView->setColumnCount(3);
50
    QStringList videoHeader {tr("Enabled"), tr("Name"), tr("Bitrate")};
Edric Milaret's avatar
Edric Milaret committed
51 52 53 54 55
    ui->videoCodecView->setHorizontalHeaderLabels(videoHeader);
    ui->videoCodecView->verticalHeader()->hide();
    ui->videoCodecView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->videoCodecView->setShowGrid(false);

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

58 59 60
    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
61 62 63 64 65 66 67 68 69 70
}

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

void
AccountDetails::reloadCodec(CodecType type)
{
71 72 73
    ui->audioCodecView->disconnect();
    ui->videoCodecView->disconnect();

Edric Milaret's avatar
Edric Milaret committed
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
    auto selectedIdx = codecModel_->selectionModel()->currentIndex();

    if (type != CodecType::VIDEO) {
        ui->audioCodecView->clearContents();
        ui->audioCodecView->setRowCount(codecModel_->audioCodecs()->rowCount());

        for (int i = 0; i < codecModel_->audioCodecs()->rowCount(); i++) {

            auto idx = codecModel_->audioCodecs()->index(i, 0);

            auto checkBoxItem = new QTableWidgetItem();
            checkBoxItem->setCheckState(
                        codecModel_->audioCodecs()->data(idx ,
                                                         Qt::CheckStateRole).toBool()
                        ? Qt::Checked : Qt::Unchecked);
            ui->audioCodecView->setItem(i, 0, checkBoxItem);

            auto item = new QTableWidgetItem(
                        codecModel_->audioCodecs()->data(idx).toString());
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->audioCodecView->setItem(i, 1, item);

            item = new QTableWidgetItem(
                        codecModel_->audioCodecs()->
                        data(idx,CodecModel::Role::BITRATE).toString());
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->audioCodecView->setItem(i, 2, item);

            item = new QTableWidgetItem(
                        codecModel_->audioCodecs()->
                        data(idx, CodecModel::Role::SAMPLERATE).toString());
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->audioCodecView->setItem(i, 3, item);

            if (codecModel_->audioCodecs()->mapToSource(idx) == selectedIdx) {
                ui->audioCodecView->setCurrentCell(i, 0);
            }
        }
    }
    if (type != CodecType::AUDIO) {
        ui->videoCodecView->clearContents();
        ui->videoCodecView->setRowCount(codecModel_->videoCodecs()->rowCount());
        for (int i = 0; i < codecModel_->videoCodecs()->rowCount(); i++) {

            auto idx = codecModel_->videoCodecs()->index(i, 0);

            auto checkBoxItem = new QTableWidgetItem();
            checkBoxItem->setCheckState(codecModel_->videoCodecs()->
                                        data(idx , Qt::CheckStateRole).toBool()
                                        ? Qt::Checked : Qt::Unchecked);
            ui->videoCodecView->setItem(i, 0, checkBoxItem);

            auto item = new QTableWidgetItem(
                        codecModel_->videoCodecs()->data(idx).toString());
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->videoCodecView->setItem(i, 1, item);

            item = new QTableWidgetItem(codecModel_->videoCodecs()->
                                        data(idx,CodecModel::Role::BITRATE).toString());
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->videoCodecView->setItem(i, 2, item);

            if (codecModel_->videoCodecs()->mapToSource(idx) == selectedIdx) {
                ui->videoCodecView->setCurrentCell(i, 0);
            }
        }
    }
141 142 143 144
    connect(ui->audioCodecView, SIGNAL(cellChanged(int,int)),
            this, SLOT(audio_codec_checked(int, int)));
    connect(ui->videoCodecView, SIGNAL(cellChanged(int,int)),
            this, SLOT(video_codec_checked(int,int)));
145 146 147 148
    connect(ui->audioCodecView, SIGNAL(itemSelectionChanged()),
            this, SLOT(on_audioCodecView_itemSelectionChanged()));
    connect(ui->videoCodecView, SIGNAL(itemSelectionChanged()),
            this, SLOT(on_videoCodecView_itemSelectionChanged()));
Edric Milaret's avatar
Edric Milaret committed
149 150 151 152 153
}

void
AccountDetails::setAccount(Account* currentAccount) {

154 155 156 157
    if (currentAccount_) {
        currentAccount_->performAction(Account::EditAction::SAVE);
    }

Edric Milaret's avatar
Edric Milaret committed
158 159 160 161
    currentAccount_ = currentAccount;

    codecModel_ = currentAccount->codecModel();

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    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
185
        break;
186 187
    case DtmfType::OverSip:
        ui->sipRadio->setChecked(true);
Edric Milaret's avatar
Edric Milaret committed
188 189 190
        break;
    }

191 192 193 194 195 196 197 198 199
    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
200

201
    if (currentAccount_->tlsCaListCertificate())
202
        ui->lrcfg_tlsCaListCertificate->setText(currentAccount_->tlsCaListCertificate()->path());
203
    if (currentAccount_->tlsCertificate())
204 205 206
        ui->lrcfg_tlsCertificate->setText(currentAccount_->tlsCertificate()->path());
    if (not currentAccount_->tlsPrivateKey().isEmpty())
        ui->lrcfg_tlsPrivateKeyCertificate->setText(currentAccount_->tlsPrivateKey());
207

Edric Milaret's avatar
Edric Milaret committed
208
#ifdef Q_OS_WIN32
209 210
    certMap_[ui->lrcfg_tlsCaListCertificate->objectName()] = &currentAccount_->setTlsCaListCertificate;
    certMap_[ui->lrcfg_tlsCertificate->objectName()] = &currentAccount_->setTlsCertificate;
211
    certMap_[ui->lrcfg_tlsPrivateKeyCertificate->objectName()] = &currentAccount_->setTlsPrivateKey;
Edric Milaret's avatar
Edric Milaret committed
212
#endif
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

    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());

    reloadCodec();
Edric Milaret's avatar
Edric Milaret committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
}

void
AccountDetails::audio_codec_checked(int row, int column) {
    if (column != 0)
        return;
    auto item = ui->audioCodecView->item(row, 0);
    auto idx = codecModel_->audioCodecs()->index(row, 0);
    codecModel_->audioCodecs()->setData(idx, item->checkState(),
                                        Qt::CheckStateRole);
}

void
AccountDetails::video_codec_checked(int row, int column) {
    if (column != 0)
        return;
    auto item = ui->videoCodecView->item(row, 0);
    auto idx = codecModel_->videoCodecs()->index(row, 0);
    codecModel_->videoCodecs()->setData(idx, item->checkState(),
                                        Qt::CheckStateRole);
}

void
AccountDetails::on_upAudioButton_clicked()
{
    codecModel_->moveUp();
    reloadCodec(CodecType::AUDIO);
}

void
AccountDetails::on_downAudioButton_clicked()
{
    codecModel_->moveDown();
    reloadCodec(CodecType::AUDIO);
}

void
AccountDetails::on_upVideoButton_clicked()
{
    codecModel_->moveUp();
    reloadCodec(CodecType::VIDEO);
}

void
AccountDetails::on_downVideoButton_clicked()
{
    codecModel_->moveDown();
    reloadCodec(CodecType::VIDEO);
}

void
AccountDetails::on_audioCodecView_itemSelectionChanged()
{
    int row = ui->audioCodecView->currentRow();
    auto idx = codecModel_->audioCodecs()->index(row, 0);
    auto srcIdx = codecModel_->audioCodecs()->mapToSource(idx);
    codecModel_->selectionModel()->setCurrentIndex(srcIdx,
                                                   QItemSelectionModel::Select);
}

void
AccountDetails::on_videoCodecView_itemSelectionChanged()
{
    int row = ui->videoCodecView->currentRow();
    auto idx = codecModel_->videoCodecs()->index(row, 0);
    auto srcIdx = codecModel_->videoCodecs()->mapToSource(idx);
    codecModel_->selectionModel()->setCurrentIndex(srcIdx,
                                                   QItemSelectionModel::Select);
}

void
AccountDetails::save() {
Edric Milaret's avatar
Edric Milaret committed
307
    codecModel_->performAction(CodecModel::EditAction::SAVE);
Edric Milaret's avatar
Edric Milaret committed
308 309 310 311 312 313 314 315 316 317 318
}

void
AccountDetails::on_tabWidget_currentChanged(int index)
{
    if (index == 1) {
        ui->audioCodecView->setCurrentItem(ui->audioCodecView->item(0, 0));
    } else if (index == 2) {
        ui->videoCodecView->setCurrentItem(ui->videoCodecView->item(0, 0));
    }
}
Edric Milaret's avatar
Edric Milaret committed
319

320 321
void
AccountDetails::onCertButtonClicked()
Edric Milaret's avatar
Edric Milaret committed
322
{
323 324 325 326 327 328 329 330 331
    QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"),
    "",
    tr("Files (*)"));

    auto sender = QObject::sender();

    (currentAccount_->*certMap_[sender->objectName()])(fileName);

    static_cast<QPushButton*>(sender)->setText(fileName);
Edric Milaret's avatar
Edric Milaret committed
332
}