configurationwidget.cpp 7.98 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "configurationwidget.h"
#include "ui_configurationwidget.h"

22 23
#include <QMessageBox>

24 25 26 27
#include "video/devicemodel.h"
#include "video/channel.h"
#include "video/resolution.h"
#include "video/rate.h"
28
#include "video/previewmanager.h"
29

30
#include "accountserializationadapter.h"
31 32
#include "accountstatedelegate.h"
#include "settingskey.h"
33
#include "utils.h"
34

35 36 37
#include "accountmodel.h"
#include "protocolmodel.h"
#include "accountdetails.h"
38
#include "callmodel.h"
39 40
#include "ringtonemodel.h"
#include "categorizedhistorymodel.h"
41

42 43 44
#ifdef ENABLE_AUTOUPDATE
#include "winsparkle.h"
#endif
45 46 47 48

ConfigurationWidget::ConfigurationWidget(QWidget *parent) :
    NavWidget(Nav, parent),
    ui(new Ui::ConfigurationWidget),
Edric Milaret's avatar
Edric Milaret committed
49 50
    accountModel_(&AccountModel::instance()),
    deviceModel_(&Video::DeviceModel::instance()),
51 52 53 54 55
    accountDetails_(new AccountDetails())
{
    ui->setupUi(this);

    ui->accountView->setModel(accountModel_);
Edric Milaret's avatar
Edric Milaret committed
56 57
    accountStateDelegate_ = new AccountStateDelegate();
    ui->accountView->setItemDelegate(accountStateDelegate_);
58 59 60

    isLoading_ = true;
    ui->deviceBox->setModel(deviceModel_);
61 62
    connect(deviceModel_, SIGNAL(currentIndexChanged(int)),
            this, SLOT(deviceIndexChanged(int)));
63 64

    connect(ui->accountView->selectionModel(),
65 66
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(accountSelected(QItemSelection)));
67 68 69 70

    ui->accountView->setCurrentIndex(accountModel_->index(0));
    ui->accountDetailLayout->addWidget(accountDetails_);
    ui->accountTypeBox->setModel(accountModel_->protocolModel());
71
    ui->startupBox->setChecked(Utils::CheckStartupLink());
72

Edric Milaret's avatar
Edric Milaret committed
73
    ui->ringtonesBox->setModel(&RingtoneModel::instance());
74
    ui->historyDaySettingsSpinBox->setValue(
Edric Milaret's avatar
Edric Milaret committed
75
                CategorizedHistoryModel::instance().historyLimit());
76 77
    ui->closeOrMinCheckBox->setChecked(settings_.value(
                                           SettingsKey::closeOrMinimized).toBool());
78
    connect(ui->tabWidget, &QTabWidget::currentChanged, [](int index) {
79
        if (index == 1
Edric Milaret's avatar
Edric Milaret committed
80 81
                && CallModel::instance().getActiveCalls().size() == 0) {
            Video::PreviewManager::instance().startPreview();
82
        } else {
Edric Milaret's avatar
Edric Milaret committed
83 84 85
            if (CallModel::instance().getActiveCalls().size() == 0
                    && Video::PreviewManager::instance().isPreviewing()) {
                Video::PreviewManager::instance().stopPreview();
86 87 88
            }
        }
    });
89 90 91 92 93 94
#ifndef ENABLE_AUTOUPDATE
    ui->checkUpdateButton->hide();
    ui->intervalUpdateCheckSpinBox->hide();
    ui->updateDayLabel->hide();
    ui->autoUpdateCheckBox->hide();
#endif
95 96 97 98
}

void
ConfigurationWidget::showEvent(QShowEvent *event) {
99 100 101 102 103 104 105

#ifdef ENABLE_AUTOUPDATE
    if (win_sparkle_get_automatic_check_for_updates()) {
        ui->autoUpdateCheckBox->setChecked(true);
    }
    ui->intervalUpdateCheckSpinBox->setValue(win_sparkle_get_update_check_interval() / 86400);
#endif
106 107
    QWidget::showEvent(event);
    if (ui->tabWidget->currentIndex() == 1
Edric Milaret's avatar
Edric Milaret committed
108 109
            && CallModel::instance().getActiveCalls().size() == 0) {
        Video::PreviewManager::instance().startPreview();
110
    }
111 112
}

113 114
void
ConfigurationWidget::atExit() {
Edric Milaret's avatar
Edric Milaret committed
115 116 117
    if (CallModel::instance().getActiveCalls().size() == 0
            && Video::PreviewManager::instance().isPreviewing()) {
        Video::PreviewManager::instance().stopPreview();
118
    }
119 120 121 122 123 124 125
    accountModel_->save();
    accountDetails_->save();
}

ConfigurationWidget::~ConfigurationWidget()
{
    delete ui;
Edric Milaret's avatar
Edric Milaret committed
126
    delete accountStateDelegate_;
127 128
}

129 130 131 132 133
void
ConfigurationWidget::deviceIndexChanged(int index) {
    ui->deviceBox->setCurrentIndex(index);
}

134 135 136 137 138 139 140 141 142 143 144 145 146 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
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
ConfigurationWidget::accountSelected(QItemSelection itemSel) {
172

173
    Q_UNUSED(itemSel)
174 175 176 177
    auto account = accountModel_->getAccountByModelIndex(
                ui->accountView->currentIndex());
    accountDetails_->setAccount(account);
    AccountSerializationAdapter adapter(account, accountDetails_);
178 179 180 181 182 183 184 185 186 187 188 189 190 191
}

void
ConfigurationWidget::on_deleteAccountButton_clicked()
{
    auto account = accountModel_->getAccountByModelIndex(
                ui->accountView->currentIndex());
    if (account != accountModel_->ip2ip())
        accountModel_->remove(account);
}

void
ConfigurationWidget::on_addAccountButton_clicked()
{
192
    auto account = accountModel_->add(tr("New Account"),
193 194
                                      ui->accountTypeBox->model()->index(
                                          ui->accountTypeBox->currentIndex(), 0));
195
    account->setRingtonePath(Utils::GetRingtonePath());
196 197
    accountModel_->save();
}
198

199 200
void
ConfigurationWidget::on_startupBox_toggled(bool checked)
201 202 203 204 205 206
{
    if (checked)
        Utils::CreateStartupLink();
    else
        Utils::DeleteStartupLink();
}
207

208 209 210 211 212
void
ConfigurationWidget::on_clearHistoryButton_clicked()
{
    QMessageBox confirmationDialog;

213
    confirmationDialog.setText(tr("Are you sure you want to clear all your history?"));
214 215 216 217
    confirmationDialog.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    auto ret = confirmationDialog.exec();

    if (ret == QMessageBox::Ok)
Edric Milaret's avatar
Edric Milaret committed
218
        CategorizedHistoryModel::instance().clearAllCollections();
219 220 221 222 223
}

void
ConfigurationWidget::on_historyDaySettingsSpinBox_valueChanged(int limit)
{
Edric Milaret's avatar
Edric Milaret committed
224 225
    if (CategorizedHistoryModel::instance().historyLimit() != limit)
        CategorizedHistoryModel::instance().setHistoryLimit(limit);
226
}
227 228 229 230 231 232

void
ConfigurationWidget::on_closeOrMinCheckBox_toggled(bool checked)
{
    settings_.setValue(SettingsKey::closeOrMinimized, checked);
}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

void
ConfigurationWidget::on_checkUpdateButton_clicked()
{
#ifdef ENABLE_AUTOUPDATE
    win_sparkle_check_update_with_ui();
#endif
}

void
ConfigurationWidget::on_autoUpdateCheckBox_toggled(bool checked)
{
#ifdef ENABLE_AUTOUPDATE
    win_sparkle_set_automatic_check_for_updates(checked);
#endif
}

void
ConfigurationWidget::on_intervalUpdateCheckSpinBox_valueChanged(int arg1)
{
#ifdef ENABLE_AUTOUPDATE
    win_sparkle_set_update_check_interval(arg1 * 86400);
#endif
}