configurationwidget.cpp 15.2 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
2
 * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
Edric Milaret's avatar
Edric Milaret committed
3
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
4 5
 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>          *
 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
Edric Milaret's avatar
Edric Milaret committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *                                                                         *
 * 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 "configurationwidget.h"
#include "ui_configurationwidget.h"

24
#include <QMessageBox>
25 26 27
#include <QDir>
#include <QStandardPaths>
#include <QFileDialog>
28
#include <QPropertyAnimation>
Edric Milaret's avatar
Edric Milaret committed
29
#include <QtConcurrent/QtConcurrent>
30

Edric Milaret's avatar
Edric Milaret committed
31 32 33 34
#include "video/devicemodel.h"
#include "video/channel.h"
#include "video/resolution.h"
#include "video/rate.h"
35
#include "video/previewmanager.h"
Edric Milaret's avatar
Edric Milaret committed
36

37 38 39 40
#include "audio/settings.h"
#include "audio/outputdevicemodel.h"
#include "audio/inputdevicemodel.h"

41 42
#include "media/recordingmodel.h"

43
#include "accountserializationadapter.h"
44 45
#include "accountstatedelegate.h"
#include "settingskey.h"
46
#include "utils.h"
Edric Milaret's avatar
Edric Milaret committed
47
#include "pathpassworddialog.h"
Edric Milaret's avatar
Edric Milaret committed
48
#include "photoboothdialog.h"
Edric Milaret's avatar
Edric Milaret committed
49
#include "wizarddialog.h"
50

Edric Milaret's avatar
Edric Milaret committed
51 52 53
#include "accountmodel.h"
#include "protocolmodel.h"
#include "accountdetails.h"
54
#include "callmodel.h"
55 56
#include "ringtonemodel.h"
#include "categorizedhistorymodel.h"
Edric Milaret's avatar
Edric Milaret committed
57 58 59
#include "profilemodel.h"
#include "profile.h"
#include "person.h"
Edric Milaret's avatar
Edric Milaret committed
60

61
#include "winsparkle.h"
Edric Milaret's avatar
Edric Milaret committed
62

63 64
#include "deleteaccountdialog.h"

Edric Milaret's avatar
Edric Milaret committed
65
ConfigurationWidget::ConfigurationWidget(QWidget *parent) :
66
    NavWidget(parent),
Edric Milaret's avatar
Edric Milaret committed
67
    ui(new Ui::ConfigurationWidget),
Edric Milaret's avatar
Edric Milaret committed
68 69
    accountModel_(&AccountModel::instance()),
    deviceModel_(&Video::DeviceModel::instance()),
Edric Milaret's avatar
Edric Milaret committed
70 71 72 73
    accountDetails_(new AccountDetails())
{
    ui->setupUi(this);

74 75 76 77 78 79 80 81 82 83 84 85 86
    connect(ui->exitSettingsButton, &QPushButton::clicked, this, [=]() {
        if (CallModel::instance().getActiveCalls().size() == 0
                && Video::PreviewManager::instance().isPreviewing()) {
            Video::PreviewManager::instance().stopPreview();
        }
        accountModel_->save();
        accountDetails_->save();
    });

    connect(ui->exitSettingsButton, &QPushButton::clicked, this, [=]() {
        emit NavigationRequested(ScreenEnum::CallScreen);
    });

Edric Milaret's avatar
Edric Milaret committed
87
    ui->accountView->setModel(accountModel_);
Edric Milaret's avatar
Edric Milaret committed
88 89
    accountStateDelegate_ = new AccountStateDelegate();
    ui->accountView->setItemDelegate(accountStateDelegate_);
Edric Milaret's avatar
Edric Milaret committed
90

91 92 93 94 95 96 97
    // connect delete button to popup trigger
    connect(ui->deleteAccountBtn, &QPushButton::clicked, [=](){
        auto idx = ui->accountView->currentIndex();
        DeleteAccountDialog dialog(idx);
        dialog.exec();
    });

Edric Milaret's avatar
Edric Milaret committed
98 99
    isLoading_ = true;
    ui->deviceBox->setModel(deviceModel_);
100 101
    connect(deviceModel_, SIGNAL(currentIndexChanged(int)),
            this, SLOT(deviceIndexChanged(int)));
Edric Milaret's avatar
Edric Milaret committed
102

103
    AccountModel::instance().selectionModel()->clear();
Edric Milaret's avatar
Edric Milaret committed
104
    ui->accountView->setSelectionModel(AccountModel::instance().selectionModel());
Edric Milaret's avatar
Edric Milaret committed
105
    connect(ui->accountView->selectionModel(),
106 107
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(accountSelected(QItemSelection)));
Edric Milaret's avatar
Edric Milaret committed
108 109 110 111

    ui->accountView->setCurrentIndex(accountModel_->index(0));
    ui->accountDetailLayout->addWidget(accountDetails_);
    ui->accountTypeBox->setModel(accountModel_->protocolModel());
112
    ui->accountTypeBox->setCurrentIndex(ui->accountTypeBox->findText("RING"));
113
    ui->startupBox->setChecked(Utils::CheckStartupLink());
114

115
    ui->historyDaySettingsSpinBox->setValue(
Edric Milaret's avatar
Edric Milaret committed
116
                CategorizedHistoryModel::instance().historyLimit());
117 118
    ui->closeOrMinCheckBox->setChecked(settings_.value(
                                           SettingsKey::closeOrMinimized).toBool());
Edric Milaret's avatar
Edric Milaret committed
119 120
    ui->notificationCheckBox->setChecked(settings_.value(
                                           SettingsKey::enableNotifications).toBool());
121
    connect(ui->stackedWidget, &QStackedWidget::currentChanged, [](int index) {
122
        if (index == 1
Edric Milaret's avatar
Edric Milaret committed
123 124
                && CallModel::instance().getActiveCalls().size() == 0) {
            Video::PreviewManager::instance().startPreview();
125
        } else {
Edric Milaret's avatar
Edric Milaret committed
126 127 128
            if (CallModel::instance().getActiveCalls().size() == 0
                    && Video::PreviewManager::instance().isPreviewing()) {
                Video::PreviewManager::instance().stopPreview();
129 130 131
            }
        }
    });
132 133

    ui->videoView->setIsFullPreview(true);
134 135 136 137 138 139 140 141 142 143 144 145 146

    auto recordPath = Media::RecordingModel::instance().recordPath();
    if (recordPath.isEmpty()) {
        recordPath = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
        Media::RecordingModel::instance().setRecordPath(recordPath);
    }
    ui->recordPath->setText(Media::RecordingModel::instance().recordPath());

    ui->alwaysRecordCheckBox->setChecked(Media::RecordingModel::instance().isAlwaysRecording());
    connect(ui->alwaysRecordCheckBox, &QCheckBox::clicked, [](bool checked){
        Media::RecordingModel::instance().setAlwaysRecording(checked);
    });

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    connect(ui->generalTabButton, &QPushButton::toggled, [=] (bool toggled) {
        if (toggled) {
            ui->stackedWidget->setCurrentWidget(ui->generalPage);
            ui->videoTabButton->setChecked(false);
            ui->accountTabButton->setChecked(false);
        }
    });

    connect(ui->videoTabButton, &QPushButton::toggled, [=] (bool toggled) {
        if (toggled) {
            ui->stackedWidget->setCurrentWidget(ui->videoPage);
            ui->accountTabButton->setChecked(false);
            ui->generalTabButton->setChecked(false);
        }
    });

    connect(ui->accountTabButton, &QPushButton::toggled, [=] (bool toggled) {
        if (toggled) {
            ui->stackedWidget->setCurrentWidget(ui->accountPage);
            ui->videoTabButton->setChecked(false);
            ui->generalTabButton->setChecked(false);
        }
    });

    ui->generalTabButton->setChecked(true);

173 174 175 176 177 178 179 180 181 182
    auto inputModel = Audio::Settings::instance().inputDeviceModel();
    auto outputModel = Audio::Settings::instance().outputDeviceModel();

    ui->outputComboBox->setModel(outputModel);
    ui->inputComboBox->setModel(inputModel);
    ui->outputComboBox->setCurrentIndex(outputModel->selectionModel()->currentIndex().row());
    ui->inputComboBox->setCurrentIndex(inputModel->selectionModel()->currentIndex().row());
    connect(ui->outputComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(outputIndexChanged(int)));
    connect(ui->inputComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(inputIndexChanged(int)));

Edric Milaret's avatar
Edric Milaret committed
183 184 185
    auto profile = ProfileModel::instance().selectedProfile();
    ui->avatarButton->setIcon(QPixmap::fromImage(Utils::getCirclePhoto(profile->person()->photo().value<QImage>(), ui->avatarButton->width())));
    ui->profileNameEdit->setText(profile->person()->formattedName());
186 187 188 189

    //temporary fix hiding imports buttons
    ui->exportButton->hide();
    ui->importButton->hide();
190 191

    ui->intervalUpdateCheckSpinBox->setEnabled(true);
192 193
}

194 195
void ConfigurationWidget::showPreview()
{
196
    if (ui->stackedWidget->currentIndex() == 1
197 198 199 200 201 202 203 204 205 206
            && CallModel::instance().getActiveCalls().size() == 0) {
        ui->previewUnavailable->hide();
        ui->videoView->show();
        Video::PreviewManager::instance().startPreview();
    } else {
        ui->previewUnavailable->show();
        ui->videoView->hide();
    }
}

207
void
208 209
ConfigurationWidget::showEvent(QShowEvent *event)
{
210 211 212 213
    if (win_sparkle_get_automatic_check_for_updates()) {
        ui->autoUpdateCheckBox->setChecked(true);
    }
    ui->intervalUpdateCheckSpinBox->setValue(win_sparkle_get_update_check_interval() / 86400);
214
    QWidget::showEvent(event);
215
    showPreview();
Edric Milaret's avatar
Edric Milaret committed
216 217 218 219 220
}

ConfigurationWidget::~ConfigurationWidget()
{
    delete ui;
Edric Milaret's avatar
Edric Milaret committed
221
    delete accountStateDelegate_;
Edric Milaret's avatar
Edric Milaret committed
222 223
}

224
void
225 226
ConfigurationWidget::deviceIndexChanged(int index)
{
227 228 229
    ui->deviceBox->setCurrentIndex(index);
}

Edric Milaret's avatar
Edric Milaret committed
230 231 232 233 234 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
void
ConfigurationWidget::on_deviceBox_currentIndexChanged(int index)
{
    if (index < 0)
        return;

    if (!isLoading_)
        deviceModel_->setActive(index);

    auto device = deviceModel_->activeDevice();

    ui->sizeBox->clear();

    isLoading_ = true;
    if (device->channelList().size() > 0) {
        for (auto resolution : device->channelList()[0]->validResolutions()) {
            ui->sizeBox->addItem(resolution->name());
        }
    }
    ui->sizeBox->setCurrentIndex(
                device->channelList()[0]->activeResolution()->relativeIndex());
    isLoading_ = false;
}

void
ConfigurationWidget::on_sizeBox_currentIndexChanged(int index)
{
    auto device = deviceModel_->activeDevice();

    if (index < 0)
        return;
    if (!isLoading_)
        device->channelList()[0]->setActiveResolution(
                    device->channelList()[0]->validResolutions()[index]);
}

void
267 268 269 270 271 272 273 274 275
ConfigurationWidget::accountSelected(QItemSelection itemSel)
{
    if (itemSel.size())
        accountDetails_->show();
    else
        accountDetails_->hide();

    if (accountConnection_)
        disconnect(accountConnection_);
276 277 278 279

    auto account = accountModel_->getAccountByModelIndex(
                ui->accountView->currentIndex());
    accountDetails_->setAccount(account);
280
    if (account) {
Edric Milaret's avatar
Edric Milaret committed
281
        AccountSerializationAdapter adapter(account, accountDetails_);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        accountConnection_= connect(account,
                                    SIGNAL(propertyChanged(Account*,QString,QString,QString)),
                                    this,
                                    SLOT(accountPropertyChanged(Account*,QString,QString,QString)));
    }
}

void
ConfigurationWidget::accountPropertyChanged(Account* a,
                                            const QString& name,
                                            const QString& newVal,
                                            const QString& oldVal)
{
    Q_UNUSED(name)
    Q_UNUSED(newVal)
    Q_UNUSED(oldVal)
    accountDetails_->setAccount(a);
    AccountSerializationAdapter adapter(a, accountDetails_);
Edric Milaret's avatar
Edric Milaret committed
300 301 302 303 304
}

void
ConfigurationWidget::on_addAccountButton_clicked()
{
Edric Milaret's avatar
Edric Milaret committed
305 306 307 308 309 310 311 312 313
    auto type = ui->accountTypeBox->model()->index(ui->accountTypeBox->currentIndex(), 0);
    if (type.data()  == "RING") {
        WizardDialog dlg(WizardDialog::NEW_ACCOUNT);
        dlg.exec();
    } else {
        auto account = accountModel_->add(tr("New Account"), type);
        account->setRingtonePath(Utils::GetRingtonePath());
        accountModel_->save();
    }
Edric Milaret's avatar
Edric Milaret committed
314
}
315

316 317
void
ConfigurationWidget::on_startupBox_toggled(bool checked)
318 319 320 321 322 323
{
    if (checked)
        Utils::CreateStartupLink();
    else
        Utils::DeleteStartupLink();
}
324

325 326 327 328 329
void
ConfigurationWidget::on_clearHistoryButton_clicked()
{
    QMessageBox confirmationDialog;

330
    confirmationDialog.setText(tr("Are you sure you want to clear all your history?"));
331 332 333 334
    confirmationDialog.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    auto ret = confirmationDialog.exec();

    if (ret == QMessageBox::Ok)
Edric Milaret's avatar
Edric Milaret committed
335
        CategorizedHistoryModel::instance().clearAllCollections();
336 337 338 339 340
}

void
ConfigurationWidget::on_historyDaySettingsSpinBox_valueChanged(int limit)
{
Edric Milaret's avatar
Edric Milaret committed
341 342
    if (CategorizedHistoryModel::instance().historyLimit() != limit)
        CategorizedHistoryModel::instance().setHistoryLimit(limit);
343
}
344 345 346 347 348 349

void
ConfigurationWidget::on_closeOrMinCheckBox_toggled(bool checked)
{
    settings_.setValue(SettingsKey::closeOrMinimized, checked);
}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

void
ConfigurationWidget::on_checkUpdateButton_clicked()
{
    win_sparkle_check_update_with_ui();
}

void
ConfigurationWidget::on_autoUpdateCheckBox_toggled(bool checked)
{
    win_sparkle_set_automatic_check_for_updates(checked);
}

void
ConfigurationWidget::on_intervalUpdateCheckSpinBox_valueChanged(int arg1)
{
    win_sparkle_set_update_check_interval(arg1 * 86400);
}
368 369

void
370
ConfigurationWidget::on_stackedWidget_currentChanged(int index)
371 372 373 374
{
    Q_UNUSED(index)
    showPreview();
}
375 376 377 378 379 380 381 382 383 384 385 386 387

void
ConfigurationWidget::on_recordPath_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Choose Directory"),
                                                 Media::RecordingModel::instance().recordPath(),
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);
    if (not dir.isEmpty()) {
        Media::RecordingModel::instance().setRecordPath(dir);
        ui->recordPath->setText(dir);
    }
}
388 389 390 391 392 393 394 395 396 397 398 399 400 401

void
ConfigurationWidget::outputIndexChanged(int index)
{
    auto outputModel = Audio::Settings::instance().outputDeviceModel();
    outputModel->selectionModel()->setCurrentIndex(outputModel->index(index), QItemSelectionModel::ClearAndSelect);
}

void
ConfigurationWidget::inputIndexChanged(int index)
{
    auto inputModel = Audio::Settings::instance().inputDeviceModel();
    inputModel->selectionModel()->setCurrentIndex(inputModel->index(index), QItemSelectionModel::ClearAndSelect);
}
Edric Milaret's avatar
Edric Milaret committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

void
ConfigurationWidget::on_importButton_clicked()
{
    PathPasswordDialog dlg;
    if (dlg.exec() == QDialog::Accepted)
        if (AccountModel::instance().importAccounts(dlg.path_, dlg.password_) > 0)
            errorDlg_.showMessage(tr("An error occured while importing account."));
}

void
ConfigurationWidget::on_exportButton_clicked()
{
    PathPasswordDialog dlg;
    dlg.exportMode = true;
    if (dlg.exec() == QDialog::Accepted) {
        auto func = [](QString path, QString password)
        {
            AccountModel::instance().exportAccounts(
            {AccountModel::instance().selectedAccount()->id()},
                        path,
                        password);
        };
        QtConcurrent::run(func, dlg.path_, dlg.password_);
    }
}
Edric Milaret's avatar
Edric Milaret committed
428 429 430 431 432 433 434

void
ConfigurationWidget::on_avatarButton_clicked()
{
    PhotoBoothDialog dlg;
    dlg.exec();
    if (dlg.result() == QDialog::Accepted) {
435
        auto image = QImage(dlg.getOutputFileName());
Edric Milaret's avatar
Edric Milaret committed
436 437 438 439 440 441 442 443 444 445 446 447 448
        auto avatar = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
        ProfileModel::instance().selectedProfile()->person()->setPhoto(avatar);
        ProfileModel::instance().selectedProfile()->save();
        ui->avatarButton->setIcon(QPixmap::fromImage(Utils::getCirclePhoto(avatar, ui->avatarButton->width())));
    }
}

void
ConfigurationWidget::on_profileNameEdit_textEdited(const QString& name)
{
    ProfileModel::instance().selectedProfile()->person()->setFormattedName(name);
    ProfileModel::instance().selectedProfile()->save();
}
Edric Milaret's avatar
Edric Milaret committed
449 450 451 452 453 454

void
ConfigurationWidget::on_notificationCheckBox_toggled(bool checked)
{
    settings_.setValue(SettingsKey::enableNotifications, checked);
}