Always save manually entered proxy values.
[Rockbox.git] / rbutil / rbutilqt / configure.cpp
blob989581529faa446400c55f7985a097b7f5602ee3
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 = qApp->applicationDirPath() + "/";
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);
58 ui.treeDevices->setAlternatingRowColors(true);
59 ui.listLanguages->setAlternatingRowColors(true);
61 this->setModal(true);
63 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
64 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
65 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
66 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
67 connect(ui.browseMountPoint, SIGNAL(clicked()), this, SLOT(browseFolder()));
68 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
70 // disable unimplemented stuff
71 ui.buttonCacheBrowse->setEnabled(false);
72 ui.cacheDisable->setEnabled(false);
73 ui.cacheOfflineMode->setEnabled(false);
74 ui.buttonCacheClear->setEnabled(false);
76 //ui.buttonAutodetect->setEnabled(false);
81 void Config::accept()
83 qDebug() << "Config::accept()";
84 // proxy: save entered proxy values, not displayed.
85 if(ui.radioManualProxy->isChecked()) {
86 proxy.setScheme("http");
87 proxy.setUserName(ui.proxyUser->text());
88 proxy.setPassword(ui.proxyPass->text());
89 proxy.setHost(ui.proxyHost->text());
90 proxy.setPort(ui.proxyPort->text().toInt());
92 userSettings->setValue("defaults/proxy", proxy.toString());
93 qDebug() << "new proxy:" << proxy;
94 // proxy type
95 QString proxyType;
96 if(ui.radioNoProxy->isChecked()) proxyType = "none";
97 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
98 else proxyType = "manual";
99 userSettings->setValue("defaults/proxytype", proxyType);
101 // language
102 if(userSettings->value("defaults/lang").toString() != language)
103 QMessageBox::information(this, tr("Language changed"),
104 tr("You need to restart the application for the changed language to take effect."));
105 userSettings->setValue("defaults/lang", language);
107 // mountpoint
108 QString mp = ui.mountPoint->text();
109 if(QFileInfo(mp).isDir())
110 userSettings->setValue("defaults/mountpoint", mp);
112 // platform
113 QString nplat;
114 if(ui.treeDevices->selectedItems().size() != 0) {
115 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
116 userSettings->setValue("defaults/platform", nplat);
119 // sync settings
120 userSettings->sync();
121 this->close();
122 emit settingsUpdated();
126 void Config::abort()
128 qDebug() << "Config::abort()";
129 this->close();
133 void Config::setUserSettings(QSettings *user)
135 userSettings = user;
136 // set proxy
137 proxy = userSettings->value("defaults/proxy").toString();
139 if(proxy.port() > 0)
140 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
141 else ui.proxyPort->setText("");
142 ui.proxyHost->setText(proxy.host());
143 ui.proxyUser->setText(proxy.userName());
144 ui.proxyPass->setText(proxy.password());
146 QString proxyType = userSettings->value("defaults/proxytype").toString();
147 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
148 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
149 else ui.radioNoProxy->setChecked(true);
151 // set language selection
152 QList<QListWidgetItem*> a;
153 QString b;
154 // find key for lang value
155 QMap<QString, QString>::const_iterator i = lang.constBegin();
156 while (i != lang.constEnd()) {
157 if(i.value() == userSettings->value("defaults/lang").toString() + ".qm") {
158 b = i.key();
159 break;
161 i++;
163 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
164 if(a.size() <= 0)
165 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
166 if(a.size() > 0)
167 ui.listLanguages->setCurrentItem(a.at(0));
169 // devices tab
170 ui.mountPoint->setText(userSettings->value("defaults/mountpoint").toString());
175 void Config::setDevices(QSettings *dev)
177 devices = dev;
178 // setup devices table
179 qDebug() << "Config::setDevices()";
180 devices->beginGroup("platforms");
181 QStringList a = devices->childKeys();
182 devices->endGroup();
184 QMap <QString, QString> manuf;
185 QMap <QString, QString> devcs;
186 for(int it = 0; it < a.size(); it++) {
187 QString curdev;
188 devices->beginGroup("platforms");
189 curdev = devices->value(a.at(it), "null").toString();
190 devices->endGroup();
191 QString curname;
192 devices->beginGroup(curdev);
193 curname = devices->value("name", "null").toString();
194 QString curbrand = devices->value("brand", "").toString();
195 devices->endGroup();
196 manuf.insertMulti(curbrand, curdev);
197 devcs.insert(curdev, curname);
200 QString platform;
201 platform = devcs.value(userSettings->value("defaults/platform").toString());
203 // set up devices table
204 ui.treeDevices->header()->hide();
205 ui.treeDevices->expandAll();
206 ui.treeDevices->setColumnCount(1);
207 QList<QTreeWidgetItem *> items;
209 // get manufacturers
210 QStringList brands = manuf.uniqueKeys();
211 QTreeWidgetItem *w;
212 QTreeWidgetItem *w2;
213 QTreeWidgetItem *w3 = 0;
214 for(int c = 0; c < brands.size(); c++) {
215 qDebug() << brands.at(c);
216 w = new QTreeWidgetItem();
217 w->setFlags(Qt::ItemIsEnabled);
218 w->setText(0, brands.at(c));
219 // w->setData(0, Qt::DecorationRole, <icon>);
220 items.append(w);
222 // go through platforms again for sake of order
223 for(int it = 0; it < a.size(); it++) {
224 QString curdev;
225 devices->beginGroup("platforms");
226 curdev = devices->value(a.at(it), "null").toString();
227 devices->endGroup();
228 QString curname;
229 devices->beginGroup(curdev);
230 curname = devices->value("name", "null").toString();
231 QString curbrand = devices->value("brand", "").toString();
232 devices->endGroup();
233 if(curbrand != brands.at(c)) continue;
234 qDebug() << "adding:" << brands.at(c) << curname << curdev;
235 w2 = new QTreeWidgetItem(w, QStringList(curname));
236 w2->setData(0, Qt::UserRole, curdev);
237 if(platform.contains(curname)) {
238 w2->setSelected(true);
239 w->setExpanded(true);
240 w3 = w2; // save pointer to hilight old selection
242 items.append(w2);
245 ui.treeDevices->insertTopLevelItems(0, items);
246 if(w3 != 0)
247 ui.treeDevices->setCurrentItem(w3); // hilight old selection
251 void Config::setNoProxy(bool checked)
253 bool i = !checked;
254 ui.proxyPort->setEnabled(i);
255 ui.proxyHost->setEnabled(i);
256 ui.proxyUser->setEnabled(i);
257 ui.proxyPass->setEnabled(i);
261 void Config::setSystemProxy(bool checked)
263 bool i = !checked;
264 ui.proxyPort->setEnabled(i);
265 ui.proxyHost->setEnabled(i);
266 ui.proxyUser->setEnabled(i);
267 ui.proxyPass->setEnabled(i);
268 if(checked) {
269 // save values in input box
270 proxy.setScheme("http");
271 proxy.setUserName(ui.proxyUser->text());
272 proxy.setPassword(ui.proxyPass->text());
273 proxy.setHost(ui.proxyHost->text());
274 proxy.setPort(ui.proxyPort->text().toInt());
275 // show system values in input box
276 #ifdef __linux
277 QUrl envproxy = QUrl(getenv("http_proxy"));
278 ui.proxyHost->setText(envproxy.host());
279 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
280 ui.proxyUser->setText(envproxy.userName());
281 ui.proxyPass->setText(envproxy.password());
282 #endif
284 else {
285 ui.proxyHost->setText(proxy.host());
286 if(proxy.port() > 0)
287 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
288 else ui.proxyPort->setText("");
289 ui.proxyUser->setText(proxy.userName());
290 ui.proxyPass->setText(proxy.password());
296 QStringList Config::findLanguageFiles()
298 QDir dir(programPath);
299 QStringList fileNames;
300 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
302 QDir resDir(":/lang");
303 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
305 fileNames.sort();
306 qDebug() << "Config::findLanguageFiles()" << fileNames;
308 return fileNames;
312 QString Config::languageName(const QString &qmFile)
314 QTranslator translator;
316 if(!translator.load(qmFile, programPath))
317 translator.load(qmFile, ":/lang");
319 return translator.translate("Configure", "English");
323 void Config::updateLanguage()
325 qDebug() << "updateLanguage()";
326 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
327 if(a.size() > 0)
328 language = QFileInfo(lang.value(a.at(0)->text())).baseName();
332 void Config::browseFolder()
334 browser = new BrowseDirtree(this);
335 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
336 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
337 #elif defined(Q_OS_WIN32)
338 browser->setFilter(QDir::Drives);
339 #endif
340 QDir d(ui.mountPoint->text());
341 browser->setDir(d);
342 browser->show();
343 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
347 void Config::setMountpoint(QString m)
349 ui.mountPoint->setText(m);
353 void Config::autodetect()
355 Autodetection detector(this);
357 if(detector.detect()) //let it detect
359 QString devicename = detector.getDevice();
360 //deexpand the platform
361 ui.treeDevices->selectedItems().at(0)->parent()->setExpanded(false);
362 //deselect the selected item
363 ui.treeDevices->selectedItems().at(0)->setSelected(false);
365 // find the new item
366 //enumerate al plattform items
367 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
368 for(int i=0; i< itmList.size();i++)
370 //enumerate device items
371 for(int j=0;j < itmList.at(i)->childCount();j++)
373 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
375 if( devicename.contains(data)) //item found
377 itmList.at(i)->child(j)->setSelected(true); //select the item
378 itmList.at(i)->setExpanded(true); //expand the platform item
379 break;
384 if(detector.getMountPoint() != "" )
386 ui.mountPoint->setText(detector.getMountPoint());
388 else
390 QMessageBox::warning(this, tr("Autodetection"),
391 tr("Could not detect a Mountpoint.\n"
392 "Select your Mountpoint manually."),
393 QMessageBox::Ok ,QMessageBox::Ok);
396 else
398 QMessageBox::warning(this, tr("Autodetection"),
399 tr("Could not detect a device.\n"
400 "Select your device and Mountpoint manually."),
401 QMessageBox::Ok ,QMessageBox::Ok);