fix a possible segfault upon invalid selection.
[Rockbox.git] / rbutil / rbutilqt / configure.cpp
blobdf3fa4bc53c1a1825b93fa693d9c4d58a3e22147
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtGui>
22 #include "configure.h"
23 #include "autodetection.h"
24 #include "ui_configurefrm.h"
25 #include "browsedirtree.h"
27 #ifdef __linux
28 #include <stdio.h>
29 #endif
31 #define DEFAULT_LANG "English (builtin)"
33 Config::Config(QWidget *parent) : QDialog(parent)
35 programPath = QFileInfo(qApp->arguments().at(0)).absolutePath() + "/";
36 ui.setupUi(this);
37 ui.radioManualProxy->setChecked(true);
38 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
39 QRegExp validate("[0-9]*");
40 proxyValidator->setRegExp(validate);
41 ui.proxyPort->setValidator(proxyValidator);
42 #ifndef __linux
43 ui.radioSystemProxy->setEnabled(false); // only on linux for now
44 #endif
45 // build language list and sort alphabetically
46 QStringList langs = findLanguageFiles();
47 for(int i = 0; i < langs.size(); ++i)
48 lang.insert(languageName(langs[i]), langs[i]);
49 lang.insert(DEFAULT_LANG, "");
50 QMap<QString, QString>::const_iterator i = lang.constBegin();
51 while (i != lang.constEnd()) {
52 ui.listLanguages->addItem(i.key());
53 i++;
55 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
56 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
57 ui.proxyPass->setEchoMode(QLineEdit::Password);
59 this->setModal(true);
61 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
62 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
63 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
64 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
65 connect(ui.browseMountPoint, SIGNAL(clicked()), this, SLOT(browseFolder()));
66 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
68 // disable unimplemented stuff
69 ui.buttonCacheBrowse->setEnabled(false);
70 ui.cacheDisable->setEnabled(false);
71 ui.cacheOfflineMode->setEnabled(false);
72 ui.buttonCacheClear->setEnabled(false);
74 //ui.buttonAutodetect->setEnabled(false);
79 void Config::accept()
81 qDebug() << "Config::accept()";
82 // proxy: save entered proxy values, not displayed.
83 if(ui.radioManualProxy->isChecked()) {
84 proxy.setScheme("http");
85 proxy.setUserName(ui.proxyUser->text());
86 proxy.setPassword(ui.proxyPass->text());
87 proxy.setHost(ui.proxyHost->text());
88 proxy.setPort(ui.proxyPort->text().toInt());
90 userSettings->setValue("defaults/proxy", proxy.toString());
91 qDebug() << "new proxy:" << proxy;
92 // proxy type
93 QString proxyType;
94 if(ui.radioNoProxy->isChecked()) proxyType = "none";
95 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
96 else proxyType = "manual";
97 userSettings->setValue("defaults/proxytype", proxyType);
99 // language
100 if(userSettings->value("defaults/lang").toString() != language)
101 QMessageBox::information(this, tr("Language changed"),
102 tr("You need to restart the application for the changed language to take effect."));
103 userSettings->setValue("defaults/lang", language);
105 // mountpoint
106 QString mp = ui.mountPoint->text();
107 if(QFileInfo(mp).isDir())
108 userSettings->setValue("defaults/mountpoint", mp);
110 // platform
111 QString nplat;
112 if(ui.treeDevices->selectedItems().size() != 0) {
113 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
114 userSettings->setValue("defaults/platform", nplat);
117 // sync settings
118 userSettings->sync();
119 this->close();
120 emit settingsUpdated();
124 void Config::abort()
126 qDebug() << "Config::abort()";
127 this->close();
131 void Config::setUserSettings(QSettings *user)
133 userSettings = user;
134 // set proxy
135 QUrl proxy = userSettings->value("defaults/proxy").toString();
137 if(proxy.port() > 0)
138 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
139 else ui.proxyPort->setText("");
140 ui.proxyHost->setText(proxy.host());
141 ui.proxyUser->setText(proxy.userName());
142 ui.proxyPass->setText(proxy.password());
144 QString proxyType = userSettings->value("defaults/proxytype").toString();
145 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
146 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
147 else ui.radioNoProxy->setChecked(true);
149 // set language selection
150 QList<QListWidgetItem*> a;
151 QString b;
152 // find key for lang value
153 QMap<QString, QString>::const_iterator i = lang.constBegin();
154 while (i != lang.constEnd()) {
155 if(i.value() == userSettings->value("defaults/lang").toString() + ".qm") {
156 b = i.key();
157 break;
159 i++;
161 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
162 if(a.size() <= 0)
163 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
164 if(a.size() > 0)
165 ui.listLanguages->setCurrentItem(a.at(0));
167 // devices tab
168 ui.mountPoint->setText(userSettings->value("defaults/mountpoint").toString());
173 void Config::setDevices(QSettings *dev)
175 devices = dev;
176 // setup devices table
177 qDebug() << "Config::setDevices()";
178 devices->beginGroup("platforms");
179 QStringList a = devices->childKeys();
180 devices->endGroup();
182 QMap <QString, QString> manuf;
183 QMap <QString, QString> devcs;
184 for(int it = 0; it < a.size(); it++) {
185 QString curdev;
186 devices->beginGroup("platforms");
187 curdev = devices->value(a.at(it), "null").toString();
188 devices->endGroup();
189 QString curname;
190 devices->beginGroup(curdev);
191 curname = devices->value("name", "null").toString();
192 QString curbrand = devices->value("brand", "").toString();
193 devices->endGroup();
194 manuf.insertMulti(curbrand, curdev);
195 devcs.insert(curdev, curname);
198 QString platform;
199 platform = devcs.value(userSettings->value("defaults/platform").toString());
201 // set up devices table
202 ui.treeDevices->header()->hide();
203 ui.treeDevices->expandAll();
204 ui.treeDevices->setColumnCount(1);
205 QList<QTreeWidgetItem *> items;
207 // get manufacturers
208 QStringList brands = manuf.uniqueKeys();
209 QTreeWidgetItem *w;
210 QTreeWidgetItem *w2;
211 QTreeWidgetItem *w3 = 0;
212 for(int c = 0; c < brands.size(); c++) {
213 qDebug() << brands.at(c);
214 w = new QTreeWidgetItem();
215 w->setFlags(Qt::ItemIsEnabled);
216 w->setText(0, brands.at(c));
217 // w->setData(0, Qt::DecorationRole, <icon>);
218 items.append(w);
220 // go through platforms again for sake of order
221 for(int it = 0; it < a.size(); it++) {
222 QString curdev;
223 devices->beginGroup("platforms");
224 curdev = devices->value(a.at(it), "null").toString();
225 devices->endGroup();
226 QString curname;
227 devices->beginGroup(curdev);
228 curname = devices->value("name", "null").toString();
229 QString curbrand = devices->value("brand", "").toString();
230 devices->endGroup();
231 if(curbrand != brands.at(c)) continue;
232 qDebug() << "adding:" << brands.at(c) << curname << curdev;
233 w2 = new QTreeWidgetItem(w, QStringList(curname));
234 w2->setData(0, Qt::UserRole, curdev);
235 if(platform.contains(curname)) {
236 w2->setSelected(true);
237 w->setExpanded(true);
238 w3 = w2; // save pointer to hilight old selection
240 items.append(w2);
243 ui.treeDevices->insertTopLevelItems(0, items);
244 if(w3 != 0)
245 ui.treeDevices->setCurrentItem(w3); // hilight old selection
249 void Config::setNoProxy(bool checked)
251 bool i = !checked;
252 ui.proxyPort->setEnabled(i);
253 ui.proxyHost->setEnabled(i);
254 ui.proxyUser->setEnabled(i);
255 ui.proxyPass->setEnabled(i);
259 void Config::setSystemProxy(bool checked)
261 bool i = !checked;
262 ui.proxyPort->setEnabled(i);
263 ui.proxyHost->setEnabled(i);
264 ui.proxyUser->setEnabled(i);
265 ui.proxyPass->setEnabled(i);
266 if(checked) {
267 // save values in input box
268 proxy.setScheme("http");
269 proxy.setUserName(ui.proxyUser->text());
270 proxy.setPassword(ui.proxyPass->text());
271 proxy.setHost(ui.proxyHost->text());
272 proxy.setPort(ui.proxyPort->text().toInt());
273 // show system values in input box
274 #ifdef __linux
275 QUrl envproxy = QUrl(getenv("http_proxy"));
276 ui.proxyHost->setText(envproxy.host());
277 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
278 ui.proxyUser->setText(envproxy.userName());
279 ui.proxyPass->setText(envproxy.password());
280 #endif
282 else {
283 ui.proxyHost->setText(proxy.host());
284 if(proxy.port() > 0)
285 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
286 else ui.proxyPort->setText("");
287 ui.proxyUser->setText(proxy.userName());
288 ui.proxyPass->setText(proxy.password());
294 QStringList Config::findLanguageFiles()
296 QDir dir(programPath + "/");
297 QStringList fileNames;
298 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
300 QDir resDir(":/lang");
301 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
303 fileNames.sort();
304 qDebug() << "Config::findLanguageFiles()" << fileNames;
306 return fileNames;
310 QString Config::languageName(const QString &qmFile)
312 QTranslator translator;
314 if(!translator.load(qmFile, programPath))
315 translator.load(qmFile, ":/lang");
317 return translator.translate("Configure", "English");
321 void Config::updateLanguage()
323 qDebug() << "updateLanguage()";
324 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
325 if(a.size() > 0)
326 language = QFileInfo(lang.value(a.at(0)->text())).baseName();
330 void Config::browseFolder()
332 browser = new BrowseDirtree(this);
333 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
334 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
335 #elif defined(Q_OS_WIN32)
336 browser->setFilter(QDir::Drives);
337 #endif
338 QDir d(ui.mountPoint->text());
339 browser->setDir(d);
340 browser->show();
341 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
345 void Config::setMountpoint(QString m)
347 ui.mountPoint->setText(m);
351 void Config::autodetect()
353 Autodetection detector(this);
355 if(detector.detect()) //let it detect
357 QString devicename = detector.getDevice();
358 //deexpand the platform
359 ui.treeDevices->selectedItems().at(0)->parent()->setExpanded(false);
360 //deselect the selected item
361 ui.treeDevices->selectedItems().at(0)->setSelected(false);
363 // find the new item
364 //enumerate al plattform items
365 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
366 for(int i=0; i< itmList.size();i++)
368 //enumerate device items
369 for(int j=0;j < itmList.at(i)->childCount();j++)
371 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
373 if( devicename.contains(data)) //item found
375 itmList.at(i)->child(j)->setSelected(true); //select the item
376 itmList.at(i)->setExpanded(true); //expand the platform item
377 break;
382 if(detector.getMountPoint() != "" )
384 ui.mountPoint->setText(detector.getMountPoint());
386 else
388 QMessageBox::warning(this, tr("Autodetection"),
389 tr("Could not detect a Mountpoint.\n"
390 "Select your Mountpoint manually."),
391 QMessageBox::Ok ,QMessageBox::Ok);
394 else
396 QMessageBox::warning(this, tr("Autodetection"),
397 tr("Could not detect a device.\n"
398 "Select your device and Mountpoint manually."),
399 QMessageBox::Ok ,QMessageBox::Ok);