Show entries from translationDir rather than :/i18n in settingspage
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
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 2 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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "appearancesettingspage.h"
22
23 #include "buffersettings.h"
24 #include "chatviewsettings.h"
25 #include "qtui.h"
26 #include "qtuisettings.h"
27 #include "util.h"
28
29 #include <QDir>
30 #include <QStyleFactory>
31
32 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
33   : SettingsPage(tr("Appearance"), tr("General"), parent)
34 {
35   ui.setupUi(this);
36   initStyleComboBox();
37   initLanguageComboBox();
38
39 #ifndef HAVE_WEBKIT
40   ui.showWebPreview->hide();
41   ui.showWebPreview->setEnabled(false);
42 #endif
43
44   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
45     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
46   }
47   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
48     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
49   }
50 }
51
52 void AppearanceSettingsPage::initStyleComboBox() {
53   QStringList styleList = QStyleFactory::keys();
54   ui.styleComboBox->addItem(tr("<System Default>"));
55   foreach(QString style, styleList) {
56     ui.styleComboBox->addItem(style);
57   }
58 }
59
60 void AppearanceSettingsPage::initLanguageComboBox() {
61   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
62
63   foreach(QString translationFile, i18nDir.entryList()) {
64     QString localeName(translationFile.mid(8));
65     localeName.chop(3);
66     QLocale locale(localeName);
67     _locales << locale;
68     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
69   }
70
71 }
72
73 void AppearanceSettingsPage::defaults() {
74   ui.styleComboBox->setCurrentIndex(0);
75
76   widgetHasChanged();
77 }
78
79 void AppearanceSettingsPage::load() {
80   QtUiSettings uiSettings;
81
82   // Gui Style
83   settings["Style"] = uiSettings.value("Style", QString(""));
84   if(settings["Style"].toString() == "") {
85     ui.styleComboBox->setCurrentIndex(0);
86   } else {
87     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
88     QApplication::setStyle(settings["Style"].toString());
89   }
90
91   // Language
92   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
93   if(locale == QLocale::system())
94     ui.languageComboBox->setCurrentIndex(0);
95   else if(locale.language() == QLocale::C)
96     ui.languageComboBox->setCurrentIndex(1);
97   else
98     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
99   Quassel::loadTranslation(selectedLocale());
100
101   ChatViewSettings chatViewSettings;
102   SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
103
104   BufferSettings bufferSettings;
105   SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
106
107   setChangedState(false);
108 }
109
110 void AppearanceSettingsPage::save() {
111   QtUiSettings uiSettings;
112
113   if(ui.styleComboBox->currentIndex() < 1) {
114     uiSettings.setValue("Style", QString(""));
115   } else {
116     uiSettings.setValue("Style", ui.styleComboBox->currentText());
117   }
118
119   if(ui.languageComboBox->currentIndex() == 0) {
120     uiSettings.remove("Locale"); // force the default (QLocale::system())
121   } else {
122     uiSettings.setValue("Locale", selectedLocale());
123   }
124
125   ChatViewSettings chatViewSettings;
126   chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
127
128   BufferSettings bufferSettings;
129   bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
130
131   load();
132   setChangedState(false);
133 }
134
135 QLocale AppearanceSettingsPage::selectedLocale() const {
136   QLocale locale;
137   int index = ui.languageComboBox->currentIndex();
138   if(index == 0)
139     locale = QLocale::system();
140   else if(index == 1)
141     locale = QLocale::c();
142   else if(index > 1)
143     locale = _locales[index - 2];
144
145   return locale;
146 }
147
148 void AppearanceSettingsPage::widgetHasChanged() {
149   setChangedState(testHasChanged());
150 }
151
152 bool AppearanceSettingsPage::testHasChanged() {
153   if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
154   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
155
156   if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
157   if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
158
159   return false;
160 }
161
162
163
164