source: SMSSender/frontend/gui/ui/settingsdialog.cpp @ 374:159b8dc48877

separation-frontend-backend
Last change on this file since 374:159b8dc48877 was 374:159b8dc48877, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Code cleanup in some classes.
File size: 16.0 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2014, gorrión. See http://smssender.gorrion.ch
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#include "settingsdialog.h"
19
20#include <QCloseEvent>
21#include <QDebug>
22#include <QMessageBox>
23#include <QNetworkProxy>
24
25#include "frontend/business/settings.h"
26#include "frontend/business/suffixmanager.h"
27#include "frontend/business/updatemanager.h"
28#include "frontend/gui/ui/updatedialog.h"
29
30namespace UI {
31
32// FIXME: Move the definition into UI::Settings
33const int PAGE_INDEX_GENERAL   = 0;
34const int PAGE_INDEX_NETWORK   = 1;
35const int PAGE_INDEX_SHORTCUTS = 2;
36
37SettingsDialog::SettingsDialog(QWidget* parent, Settings *settings, SuffixManager *suffixManager,
38                               UpdateManager *updateManager)
39    : QDialog(parent)
40    , m_settings(settings)
41    , m_suffixManager(suffixManager)
42    , m_updateManager(updateManager)
43{
44        ui.setupUi(this);
45
46        setFixedSize(size()); // Make the dialog not resizable
47
48        initGeneralPage();
49        initProxyPage();
50        initShortcutsPage();
51
52        // Default start page
53        ui.lstSettingGroups->setCurrentRow(PAGE_INDEX_GENERAL);
54        ui.stackedWidget->setCurrentWidget(ui.pageGeneral);
55}
56
57void SettingsDialog::closeEvent(QCloseEvent *e){
58    int erroneousPage = -1;
59    if (!validateGeneralPage())   erroneousPage = PAGE_INDEX_GENERAL;
60    if (!validateProxyPage())     erroneousPage = PAGE_INDEX_NETWORK;
61    if (!validateShortcutsPage()) erroneousPage = PAGE_INDEX_SHORTCUTS;
62
63    if (erroneousPage != -1) {
64        ui.lstSettingGroups->setCurrentRow(erroneousPage);
65        e->ignore();
66        return;
67    }
68
69    saveGeneralPage();
70    saveProxyPage();
71    saveShortcutsPage();
72
73    e->accept();
74    accept();
75    QDialog::closeEvent(e); // inherit
76}
77
78QString languageToString(const QLocale& locale) {
79    switch (locale.language()) {
80        case QLocale::English:
81            return QString::fromUtf8("English");
82
83        case QLocale::French:
84            return QString::fromUtf8("Français");
85
86        case QLocale::Italian:
87            return QString::fromUtf8("Italiano");
88
89        case QLocale::German:
90            switch (locale.country()) {
91                case QLocale::Switzerland:
92                    return QString::fromUtf8("Schwizerdütsch");
93
94                default:
95                    return QString::fromUtf8("Deutsch");
96            }
97
98        default:
99            return "";
100    }
101}
102void SettingsDialog::initGeneralPage(){
103    localeNameToLocale_.clear();
104    ui.lstLocale->clear();
105    foreach (QLocale locale, m_settings->getLocales()) {
106        localeNameToLocale_.insert(languageToString(locale), locale);
107        ui.lstLocale->addItem(languageToString(locale), locale);
108    }
109    ui.lstLocale->setCurrentIndex(ui.lstLocale->findText(languageToString(m_settings->getLocale())));
110    ui.lstLocale->model()->sort(0);
111
112    ui.ckbCheckForUpdates->setChecked(m_settings->getCheckForUpdates());
113    ui.ckbShowSentConfirmation->setChecked(m_settings->getShowSentConfirmation());
114    ui.ckbAddSuffix->setChecked(m_settings->getAddSuffix());
115
116    QString suffix = m_suffixManager->suffix();
117    suffix.replace("\n", "\\n");
118    ui.edtSuffixText->setPlainText(suffix);
119}
120
121void SettingsDialog::initProxyPage(){
122    proxyTypes_.insert(QNetworkProxy::Socks5Proxy, tr("SOCKS5"));
123    proxyTypes_.insert(QNetworkProxy::HttpProxy, tr("HTTP"));
124
125    ui.lstProxyType->clear();
126    foreach (QString type, proxyTypes_) {
127        ui.lstProxyType->addItem(type);
128    }
129    ui.lstProxyType->setCurrentIndex(ui.lstProxyType->findText(proxyTypes_.value(m_settings->getProxyType())));
130
131    ui.ckbUseProxy->setChecked(m_settings->isProxyEnabled());
132    ui.edtProxyHost->setText(m_settings->getProxyHost());
133    ui.edtProxyPort->setText(QString::number(m_settings->getProxyPort()));
134    ui.ckbUseProxyAuth->setChecked(m_settings->isProxyAuthEnabled());
135    ui.edtProxyUsername->setText(m_settings->getProxyUsername());
136    ui.edtProxyPassword->setText(m_settings->getProxyPassword());
137}
138
139void SettingsDialog::initShortcutsPage(){
140    shortcutKeys_.insert(Qt::CTRL,                       tr("Ctrl"));
141    shortcutKeys_.insert(Qt::CTRL + Qt::ALT,             tr("Ctrl+Alt"));
142    shortcutKeys_.insert(Qt::CTRL + Qt::ALT + Qt::SHIFT, tr("Ctrl+Alt+Shift"));
143
144    QString alphabet = "ADEFGHIJKLMNOPQRSTUVWXYZ";
145    foreach (QChar chr, alphabet) {
146        shortcutChars_.append(chr);
147    }
148
149    shortcutSelects_.insert(ui.selShortcutClearMessageTextKeys, ui.selShortcutClearMessageTextChar);
150    shortcutSelects_.insert(ui.selShortcutClearRecipientsKeys, ui.selShortcutClearRecipientsChar);
151    shortcutSelects_.insert(ui.selShortcutNewMessageKeys, ui.selShortcutNewMessageChar);
152    shortcutSelects_.insert(ui.selShortcutSendKeys, ui.selShortcutSendChar);
153
154    foreach (QComboBox* chr, shortcutSelects_) {
155        chr->clear();
156        chr->addItems(shortcutChars_);
157
158        shortcutSelects_.key(chr)->clear();
159        shortcutSelects_.key(chr)->addItems(shortcutKeys_.values());
160    }
161
162    QKeySequence s;
163
164    // New message
165    s = m_settings->getNewMessageShortCut();
166    setSelectionFromShortcutAccessKeys(s, ui.selShortcutNewMessageKeys);
167    ui.selShortcutNewMessageChar->setCurrentIndex(ui.selShortcutNewMessageChar->findText(s.toString().right(1), Qt::MatchFixedString));
168
169    // Clear message text
170    s = m_settings->getClearMessageTextShortCut();
171    setSelectionFromShortcutAccessKeys(s, ui.selShortcutClearMessageTextKeys);
172    ui.selShortcutClearMessageTextChar->setCurrentIndex(ui.selShortcutClearMessageTextChar->findText(s.toString().right(1), Qt::MatchFixedString));
173
174    // Clear recipients
175    s = m_settings->getClearRecipientsShortCut();
176    setSelectionFromShortcutAccessKeys(s, ui.selShortcutClearRecipientsKeys);
177    ui.selShortcutClearRecipientsChar->setCurrentIndex(ui.selShortcutClearRecipientsChar->findText(s.toString().right(1), Qt::MatchFixedString));
178
179    // Send message
180    s = m_settings->getSendShortCut();
181    setSelectionFromShortcutAccessKeys(s, ui.selShortcutSendKeys);
182    ui.selShortcutSendChar->setCurrentIndex(ui.selShortcutSendChar->findText(s.toString().right(1), Qt::MatchFixedString));
183
184
185    foreach (QComboBox* chr, shortcutSelects_) {
186        connect(chr, SIGNAL(currentIndexChanged(int)),
187                this, SLOT(on_selShortcut_currentIndexChanged()));
188        connect(shortcutSelects_.key(chr), SIGNAL(currentIndexChanged(int)),
189                this, SLOT(on_selShortcut_currentIndexChanged()));
190    }
191    on_selShortcut_currentIndexChanged();
192}
193
194
195bool SettingsDialog::validateGeneralPage() {
196    if (ui.ckbAddSuffix->isChecked() && ui.edtSuffixText->toPlainText().isEmpty()) {
197        ui.ckbAddSuffix->setChecked(false);
198    }
199
200    return true;
201}
202bool SettingsDialog::validateProxyPage() {
203    if (ui.ckbUseProxy->isChecked()) {
204        if (ui.edtProxyHost->text().trimmed().isEmpty() ||
205            ui.edtProxyPort->text().trimmed().isEmpty())
206        {
207            QMessageBox::warning(this, tr("Incomplete settings"),
208                                 tr("Please fill the proxy host and port."),
209                                 QMessageBox::Ok, QMessageBox::Ok);
210            return false;
211        }
212
213        bool validPort;
214        int  port = ui.edtProxyPort->text().toInt(&validPort);
215        if (!validPort || (port <= 0) || (port >= 65536)) {
216            QMessageBox::warning(this, tr("Incomplete settings"),
217                                 tr("The proxy port has to be a numeric value between 0 and 65536."),
218                                 QMessageBox::Ok, QMessageBox::Ok);
219            return false;
220        }
221
222        if (ui.ckbUseProxyAuth->isChecked()) {
223            if (ui.edtProxyUsername->text().trimmed().isEmpty() ||
224                ui.edtProxyPassword->text().trimmed().isEmpty())
225            {
226                QMessageBox::warning(this, tr("Incomplete settings"),
227                                     tr("Please fill both your proxy username and password."),
228                                     QMessageBox::Ok, QMessageBox::Ok);
229                return false;
230            }
231        }
232    }
233
234    return true;
235}
236bool SettingsDialog::validateShortcutsPage() {
237    foreach (QComboBox* chr, shortcutSelects_) {
238        QComboBox* key = shortcutSelects_.key(chr);
239
240        QKeySequence shortcut(getShortcutAccessKeysFromSelection(key), QKeySequence(chr->currentText().at(0)));
241
242        if (shortcut.isEmpty()) {
243            QMessageBox::warning(this, tr("Incomplete settings"),
244                                 tr("Please choose for each shortcut an unique access key - char pair."),
245                                 QMessageBox::Ok, QMessageBox::Ok);
246            return false;
247        }
248    }
249    return true;
250}
251
252void SettingsDialog::saveGeneralPage(){
253    m_settings->setLocale(ui.lstLocale->itemData(ui.lstLocale->currentIndex()).value<QLocale>());
254
255    m_settings->setCheckForUpdates(ui.ckbCheckForUpdates->isChecked());
256    m_settings->setShowSentConfirmation(ui.ckbShowSentConfirmation->isChecked());
257    m_settings->setAddSuffix(ui.ckbAddSuffix->isChecked());
258
259    QString suffix = ui.edtSuffixText->toPlainText();
260    suffix.replace("\\n", "\n");
261    m_suffixManager->setUserSuffix(suffix);
262}
263void SettingsDialog::saveProxyPage(){
264    m_settings->setProxyEnabled(ui.ckbUseProxy->isChecked());
265    m_settings->setProxyType(proxyTypes_.key(ui.lstProxyType->currentText()));
266    m_settings->setProxyHost(ui.edtProxyHost->text());
267    m_settings->setProxyPort(ui.edtProxyPort->text().toInt());
268    m_settings->setProxyAuthEnabled(ui.ckbUseProxyAuth->isChecked());
269    m_settings->setProxyUsername(ui.edtProxyUsername->text());
270    m_settings->setProxyPassword(ui.edtProxyPassword->text());
271}
272void SettingsDialog::saveShortcutsPage(){
273    QKeySequence shortcut;
274
275    shortcut = QKeySequence(ui.selShortcutNewMessageKeys->currentText() + "+" + ui.selShortcutNewMessageChar->currentText());
276    m_settings->setNewMessageShortCut(shortcut);
277
278    shortcut = QKeySequence(ui.selShortcutClearMessageTextKeys->currentText() + "+" + ui.selShortcutClearMessageTextChar->currentText());
279    m_settings->setClearMessageTextShortCut(shortcut);
280
281    shortcut = QKeySequence(ui.selShortcutClearRecipientsKeys->currentText() + "+" + ui.selShortcutClearRecipientsChar->currentText());
282    m_settings->setClearRecipientsShortCut(shortcut);
283
284    shortcut = QKeySequence(ui.selShortcutSendKeys->currentText() + "+" + ui.selShortcutSendChar->currentText());
285    m_settings->setSendShortCut(shortcut);
286}
287
288
289
290void SettingsDialog::setSelectionFromShortcutAccessKeys(QKeySequence shortcut, QComboBox* comboBox) {
291    int index = 0;
292    foreach (int accessKeys, shortcutKeys_.keys()) {
293        if ((shortcut[0] & accessKeys) == accessKeys) {
294            index = comboBox->findText(shortcutKeys_.value(accessKeys));
295        }
296    }
297    comboBox->setCurrentIndex(index);
298}
299
300int SettingsDialog::getShortcutAccessKeysFromSelection(QComboBox* comboBox) {
301    int accessKeys = 0;
302    if (shortcutKeys_.values().contains(comboBox->currentText())) {
303        accessKeys = shortcutKeys_.key(comboBox->currentText());
304    }
305    return accessKeys;
306}
307
308
309void _updateItemList(QComboBox* combobox, const QStringList& newItems) {
310    int i = 0; // newEntries
311    int j = 0; // combobox->items
312    while ((i < newItems.size()) || (j < combobox->count())) {
313        if ((i >= newItems.size())
314                || ((newItems.at(i) > combobox->itemText(j)) && (combobox->itemText(j) != ""))) {
315            combobox->removeItem(j);
316        } else {
317            if ((newItems.at(i) < combobox->itemText(j)) || (combobox->itemText(j) == "")) {
318                combobox->insertItem(j, newItems.at(i));
319            }
320
321            ++i;
322            ++j;
323        }
324    }
325}
326void SettingsDialog::on_selShortcut_currentIndexChanged() {
327    foreach (QComboBox* chr, shortcutSelects_) {
328        QComboBox* keys = shortcutSelects_.key(chr);
329
330        // make all combinations possible
331        QStringList availableKeys(shortcutKeys_.values());
332        QStringList availableChars(shortcutChars_);
333
334        // remove globaly used shortcuts
335        if (keys->currentText() == shortcutKeys_.value(Qt::CTRL)) {
336            availableChars.removeAll("A"); // Ctrl + a
337            availableChars.removeAll("C"); // Ctrl + c
338            availableChars.removeAll("X"); // Ctrl + x
339            availableChars.removeAll("V"); // Ctrl + v
340        }
341
342        // remove already used combinations
343        foreach (QComboBox* chr2, shortcutSelects_) {
344            QComboBox* keys2 = shortcutSelects_.key(chr2);
345
346            if (chr == chr2) {
347                continue;
348            }
349
350            if (chr->currentText() == chr2->currentText()) {
351                availableKeys.removeAll(keys2->currentText());
352            }
353
354            if (keys->currentText() == keys2->currentText()) {
355                availableChars.removeAll(chr2->currentText());
356            }
357        }
358
359        // Adjust the item lists
360        disconnect(chr, SIGNAL(currentIndexChanged(int)),
361                   this, SLOT(on_selShortcut_currentIndexChanged()));
362        disconnect(keys, SIGNAL(currentIndexChanged(int)),
363                   this, SLOT(on_selShortcut_currentIndexChanged()));
364
365        _updateItemList(keys, availableKeys);
366        _updateItemList(chr, availableChars);
367
368        connect(keys, SIGNAL(currentIndexChanged(int)),
369                this, SLOT(on_selShortcut_currentIndexChanged()));
370        connect(chr, SIGNAL(currentIndexChanged(int)),
371                this, SLOT(on_selShortcut_currentIndexChanged()));
372    }
373}
374
375void SettingsDialog::on_lstSettingGroups_currentItemChanged(QListWidgetItem* newGroup, QListWidgetItem*){
376        QWidget* page;
377        switch (ui.lstSettingGroups->row(newGroup)){
378                case PAGE_INDEX_NETWORK:
379                        page = ui.pageNetwork;
380                        break;
381
382                case PAGE_INDEX_SHORTCUTS:
383                        page = ui.pageShortcuts;
384                        break;
385
386                case PAGE_INDEX_GENERAL:
387                default:
388                        page = ui.pageGeneral;
389                        break;
390        }
391        ui.stackedWidget->setCurrentWidget(page);
392}
393
394void SettingsDialog::on_btnSearchForUpdate_clicked() {
395    if (m_updateManager->isUpdateAvailable(UpdateManager::utEdge)) {
396        onUpdateAvailable(m_updateManager->newestVersion(UpdateManager::utEdge), UpdateManager::utEdge);
397    } else {
398        connect(m_updateManager, SIGNAL(updateAvailable(const SVersion&, UpdateManager::UpdateType)),
399                this, SLOT(onUpdateAvailable(const SVersion&, UpdateManager::UpdateType)));
400        connect(m_updateManager, SIGNAL(noUpdateAvailable(UpdateManager::UpdateType)),
401                this, SLOT(onNoUpdateAvailable(UpdateManager::UpdateType)), Qt::UniqueConnection);
402
403        m_updateManager->checkForUpdates();
404    }
405}
406void SettingsDialog::onUpdateAvailable(const SVersion& version, UpdateManager::UpdateType updateType) {
407    Q_UNUSED(updateType);
408
409    QScopedPointer<UpdateDialog> d(new UpdateDialog(this, version));
410    d->exec();
411}
412void SettingsDialog::onNoUpdateAvailable(UpdateManager::UpdateType updateType) {
413    if (updateType != UpdateManager::utEdge) {
414        return;
415    }
416
417    QMessageBox::information(this, tr("No update available."),
418                                tr("You already have the newest version."),
419                                QMessageBox::Ok, QMessageBox::Ok);
420}
421
422void SettingsDialog::on_btnUseDefaultSuffix_clicked() {
423    m_suffixManager->resetUserSuffix();
424
425    QString suffix = m_suffixManager->suffix();
426    suffix.replace("\n", "\\n");
427    ui.edtSuffixText->setPlainText(suffix);
428
429    ui.ckbAddSuffix->setChecked(true);
430}
431void SettingsDialog::on_ckbAddSuffix_stateChanged(int state){
432        ui.wdtSuffix->setEnabled(state == Qt::Checked);
433}
434
435void SettingsDialog::on_ckbUseProxy_stateChanged(int state){
436        ui.wdtProxy->setEnabled(state == Qt::Checked);
437}
438
439void SettingsDialog::on_ckbUseProxyAuth_stateChanged(int state){
440        ui.wdtProxyAuth->setEnabled(state == Qt::Checked);
441}
442
443} // namespace UI
Note: See TracBrowser for help on using the repository browser.