sort languages in settings
[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 "qtui.h"
25 #include "qtuisettings.h"
26 #include "qtuistyle.h"
27
28 #include <QCheckBox>
29 #include <QFileDialog>
30 #include <QStyleFactory>
31 #include <QFile>
32 #include <QDir>
33
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
35   : SettingsPage(tr("Interface"), QString(), parent)
36 {
37   ui.setupUi(this);
38
39 #ifdef Q_WS_MAC
40   ui.minimizeOnClose->hide();
41 #endif
42 #ifdef QT_NO_SYSTEMTRAYICON
43   ui.useSystemTrayIcon->hide();
44 #endif
45
46   initAutoWidgets();
47   initStyleComboBox();
48   initLanguageComboBox();
49
50   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
51     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
52   }
53   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
54     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55   }
56
57   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
58
59   connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
60   connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61   connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62
63   connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
64   connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65   connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
66
67   connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
68   connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69   connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
70 }
71
72 void AppearanceSettingsPage::initStyleComboBox() {
73   QStringList styleList = QStyleFactory::keys();
74   ui.styleComboBox->addItem(tr("<System Default>"));
75   foreach(QString style, styleList) {
76     ui.styleComboBox->addItem(style);
77   }
78 }
79
80 void AppearanceSettingsPage::initLanguageComboBox() {
81   QDir i18nDir(Quassel::translationDirPath(), "*.qm");
82
83   QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
84   foreach(QString translationFile, i18nDir.entryList()) {
85     if(!rx.exactMatch(translationFile))
86       continue;
87     if(!rx.cap(1).isEmpty())
88       continue;
89     QLocale locale(rx.cap(2));
90     _locales << locale;
91     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
92   }
93   ui.languageComboBox->model()->sort(0);
94 }
95
96 void AppearanceSettingsPage::defaults() {
97   ui.styleComboBox->setCurrentIndex(0);
98   ui.languageComboBox->setCurrentIndex(1);
99
100   SettingsPage::defaults();
101   widgetHasChanged();
102 }
103
104 void AppearanceSettingsPage::load() {
105   QtUiSettings uiSettings;
106
107   // Gui Style
108   QString style = uiSettings.value("Style", QString("")).toString();
109   if(style.isEmpty()) {
110     ui.styleComboBox->setCurrentIndex(0);
111   } else {
112     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
113   }
114   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
115
116   // Language
117   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
118   if(locale == QLocale::system())
119     ui.languageComboBox->setCurrentIndex(1);
120   else if(locale.language() == QLocale::C)  // we use C for "untranslated"
121     ui.languageComboBox->setCurrentIndex(0);
122   else
123     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
124   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
125   Quassel::loadTranslation(selectedLocale());
126
127   // bufferSettings:
128   BufferSettings bufferSettings;
129   int redirectTarget = bufferSettings.userNoticesTarget();
130   SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
131   SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
132   SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
133
134   redirectTarget = bufferSettings.serverNoticesTarget();
135   SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
136   SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
137   SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
138
139   redirectTarget = bufferSettings.errorMsgsTarget();
140   SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
141   SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
142   SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
143
144   SettingsPage::load();
145   setChangedState(false);
146 }
147
148 void AppearanceSettingsPage::save() {
149   QtUiSettings uiSettings;
150
151   if(ui.styleComboBox->currentIndex() < 1) {
152     uiSettings.setValue("Style", QString(""));
153   } else {
154     uiSettings.setValue("Style", ui.styleComboBox->currentText());
155     QApplication::setStyle(ui.styleComboBox->currentText());
156   }
157   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
158
159   if(ui.languageComboBox->currentIndex() == 1) {
160     uiSettings.remove("Locale"); // force the default (QLocale::system())
161   } else {
162     uiSettings.setValue("Locale", selectedLocale());
163   }
164   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
165
166   bool needsStyleReload =
167         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
168     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
169
170   BufferSettings bufferSettings;
171   int redirectTarget = 0;
172   if(ui.userNoticesInDefaultBuffer->isChecked())
173     redirectTarget |= BufferSettings::DefaultBuffer;
174   if(ui.userNoticesInStatusBuffer->isChecked())
175     redirectTarget |= BufferSettings::StatusBuffer;
176   if(ui.userNoticesInCurrentBuffer->isChecked())
177     redirectTarget |= BufferSettings::CurrentBuffer;
178   bufferSettings.setUserNoticesTarget(redirectTarget);
179
180   redirectTarget = 0;
181   if(ui.serverNoticesInDefaultBuffer->isChecked())
182     redirectTarget |= BufferSettings::DefaultBuffer;
183   if(ui.serverNoticesInStatusBuffer->isChecked())
184     redirectTarget |= BufferSettings::StatusBuffer;
185   if(ui.serverNoticesInCurrentBuffer->isChecked())
186     redirectTarget |= BufferSettings::CurrentBuffer;
187   bufferSettings.setServerNoticesTarget(redirectTarget);
188
189   redirectTarget = 0;
190   if(ui.errorMsgsInDefaultBuffer->isChecked())
191     redirectTarget |= BufferSettings::DefaultBuffer;
192   if(ui.errorMsgsInStatusBuffer->isChecked())
193     redirectTarget |= BufferSettings::StatusBuffer;
194   if(ui.errorMsgsInCurrentBuffer->isChecked())
195     redirectTarget |= BufferSettings::CurrentBuffer;
196   bufferSettings.setErrorMsgsTarget(redirectTarget);
197
198   SettingsPage::save();
199   setChangedState(false);
200   if(needsStyleReload)
201     QtUi::style()->reload();
202 }
203
204 QLocale AppearanceSettingsPage::selectedLocale() const {
205   QLocale locale;
206   int index = ui.languageComboBox->currentIndex();
207   if(index == 1)
208     locale = QLocale::system();
209   else if(index == 0)
210     locale = QLocale::c();
211   else if(index > 1)
212     locale = _locales[index - 2];
213
214   return locale;
215 }
216
217 void AppearanceSettingsPage::chooseStyleSheet() {
218   QString dir = ui.customStyleSheetPath->property("storedValue").toString();
219   if(!dir.isEmpty() && QFile(dir).exists())
220     dir = QDir(dir).absolutePath();
221   else
222     dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
223
224   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
225   if(!name.isEmpty())
226     ui.customStyleSheetPath->setText(name);
227 }
228
229 void AppearanceSettingsPage::widgetHasChanged() {
230   setChangedState(testHasChanged());
231 }
232
233 bool AppearanceSettingsPage::testHasChanged() {
234   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
235   if(ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
236
237   if(SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
238   if(SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
239   if(SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
240
241   if(SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
242   if(SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
243   if(SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
244
245   if(SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
246   if(SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
247   if(SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
248
249   return false;
250 }