sort languages in settings V2
[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[QLocale::languageToString(locale.language())] = locale;
91   }
92   foreach(QString language, _locales.keys()) {
93     ui.languageComboBox->addItem(language);
94   }
95 }
96
97 void AppearanceSettingsPage::defaults() {
98   ui.styleComboBox->setCurrentIndex(0);
99   ui.languageComboBox->setCurrentIndex(1);
100
101   SettingsPage::defaults();
102   widgetHasChanged();
103 }
104
105 void AppearanceSettingsPage::load() {
106   QtUiSettings uiSettings;
107
108   // Gui Style
109   QString style = uiSettings.value("Style", QString("")).toString();
110   if(style.isEmpty()) {
111     ui.styleComboBox->setCurrentIndex(0);
112   } else {
113     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
114   }
115   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
116
117   // Language
118   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
119   if(locale == QLocale::system())
120     ui.languageComboBox->setCurrentIndex(1);
121   else if(locale.language() == QLocale::C)  // we use C for "untranslated"
122     ui.languageComboBox->setCurrentIndex(0);
123   else
124     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
125   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
126   Quassel::loadTranslation(selectedLocale());
127
128   // bufferSettings:
129   BufferSettings bufferSettings;
130   int redirectTarget = bufferSettings.userNoticesTarget();
131   SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
132   SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
133   SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
134
135   redirectTarget = bufferSettings.serverNoticesTarget();
136   SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
137   SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
138   SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
139
140   redirectTarget = bufferSettings.errorMsgsTarget();
141   SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
142   SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
143   SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
144
145   SettingsPage::load();
146   setChangedState(false);
147 }
148
149 void AppearanceSettingsPage::save() {
150   QtUiSettings uiSettings;
151
152   if(ui.styleComboBox->currentIndex() < 1) {
153     uiSettings.setValue("Style", QString(""));
154   } else {
155     uiSettings.setValue("Style", ui.styleComboBox->currentText());
156     QApplication::setStyle(ui.styleComboBox->currentText());
157   }
158   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
159
160   if(ui.languageComboBox->currentIndex() == 1) {
161     uiSettings.remove("Locale"); // force the default (QLocale::system())
162   } else {
163     uiSettings.setValue("Locale", selectedLocale());
164   }
165   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
166
167   bool needsStyleReload =
168         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
169     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
170
171   BufferSettings bufferSettings;
172   int redirectTarget = 0;
173   if(ui.userNoticesInDefaultBuffer->isChecked())
174     redirectTarget |= BufferSettings::DefaultBuffer;
175   if(ui.userNoticesInStatusBuffer->isChecked())
176     redirectTarget |= BufferSettings::StatusBuffer;
177   if(ui.userNoticesInCurrentBuffer->isChecked())
178     redirectTarget |= BufferSettings::CurrentBuffer;
179   bufferSettings.setUserNoticesTarget(redirectTarget);
180
181   redirectTarget = 0;
182   if(ui.serverNoticesInDefaultBuffer->isChecked())
183     redirectTarget |= BufferSettings::DefaultBuffer;
184   if(ui.serverNoticesInStatusBuffer->isChecked())
185     redirectTarget |= BufferSettings::StatusBuffer;
186   if(ui.serverNoticesInCurrentBuffer->isChecked())
187     redirectTarget |= BufferSettings::CurrentBuffer;
188   bufferSettings.setServerNoticesTarget(redirectTarget);
189
190   redirectTarget = 0;
191   if(ui.errorMsgsInDefaultBuffer->isChecked())
192     redirectTarget |= BufferSettings::DefaultBuffer;
193   if(ui.errorMsgsInStatusBuffer->isChecked())
194     redirectTarget |= BufferSettings::StatusBuffer;
195   if(ui.errorMsgsInCurrentBuffer->isChecked())
196     redirectTarget |= BufferSettings::CurrentBuffer;
197   bufferSettings.setErrorMsgsTarget(redirectTarget);
198
199   SettingsPage::save();
200   setChangedState(false);
201   if(needsStyleReload)
202     QtUi::style()->reload();
203 }
204
205 QLocale AppearanceSettingsPage::selectedLocale() const {
206   QLocale locale;
207   int index = ui.languageComboBox->currentIndex();
208   if(index == 1)
209     locale = QLocale::system();
210   else if(index == 0)
211     locale = QLocale::c();
212   else if(index > 1)
213     locale = _locales.values()[index - 2];
214
215   return locale;
216 }
217
218 void AppearanceSettingsPage::chooseStyleSheet() {
219   QString dir = ui.customStyleSheetPath->property("storedValue").toString();
220   if(!dir.isEmpty() && QFile(dir).exists())
221     dir = QDir(dir).absolutePath();
222   else
223     dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
224
225   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
226   if(!name.isEmpty())
227     ui.customStyleSheetPath->setText(name);
228 }
229
230 void AppearanceSettingsPage::widgetHasChanged() {
231   setChangedState(testHasChanged());
232 }
233
234 bool AppearanceSettingsPage::testHasChanged() {
235   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
236   if(ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
237
238   if(SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
239   if(SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
240   if(SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
241
242   if(SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
243   if(SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
244   if(SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
245
246   if(SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
247   if(SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
248   if(SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
249
250   return false;
251 }