if no proxy type is set default to none.
[Rockbox.git] / rbutil / rbutilqt / configure.cpp
blob6d1b727444ae1ad6d24961058288814bf4f7c54b
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 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
113 userSettings->setValue("defaults/platform", nplat);
115 // sync settings
116 userSettings->sync();
117 this->close();
118 emit settingsUpdated();
122 void Config::abort()
124 qDebug() << "Config::abort()";
125 this->close();
129 void Config::setUserSettings(QSettings *user)
131 userSettings = user;
132 // set proxy
133 QUrl proxy = userSettings->value("defaults/proxy").toString();
135 if(proxy.port() > 0)
136 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
137 else ui.proxyPort->setText("");
138 ui.proxyHost->setText(proxy.host());
139 ui.proxyUser->setText(proxy.userName());
140 ui.proxyPass->setText(proxy.password());
142 QString proxyType = userSettings->value("defaults/proxytype").toString();
143 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
144 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
145 else ui.radioNoProxy->setChecked(true);
147 // set language selection
148 QList<QListWidgetItem*> a;
149 QString b;
150 // find key for lang value
151 QMap<QString, QString>::const_iterator i = lang.constBegin();
152 while (i != lang.constEnd()) {
153 if(i.value() == userSettings->value("defaults/lang").toString() + ".qm") {
154 b = i.key();
155 break;
157 i++;
159 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
160 if(a.size() <= 0)
161 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
162 if(a.size() > 0)
163 ui.listLanguages->setCurrentItem(a.at(0));
165 // devices tab
166 ui.mountPoint->setText(userSettings->value("defaults/mountpoint").toString());
171 void Config::setDevices(QSettings *dev)
173 devices = dev;
174 // setup devices table
175 qDebug() << "Config::setDevices()";
176 devices->beginGroup("platforms");
177 QStringList a = devices->childKeys();
178 devices->endGroup();
180 QMap <QString, QString> manuf;
181 QMap <QString, QString> devcs;
182 for(int it = 0; it < a.size(); it++) {
183 QString curdev;
184 devices->beginGroup("platforms");
185 curdev = devices->value(a.at(it), "null").toString();
186 devices->endGroup();
187 QString curname;
188 devices->beginGroup(curdev);
189 curname = devices->value("name", "null").toString();
190 QString curbrand = devices->value("brand", "").toString();
191 devices->endGroup();
192 manuf.insertMulti(curbrand, curdev);
193 devcs.insert(curdev, curname);
196 QString platform;
197 platform = devcs.value(userSettings->value("defaults/platform").toString());
199 // set up devices table
200 ui.treeDevices->header()->hide();
201 ui.treeDevices->expandAll();
202 ui.treeDevices->setColumnCount(1);
203 QList<QTreeWidgetItem *> items;
205 // get manufacturers
206 QStringList brands = manuf.uniqueKeys();
207 QTreeWidgetItem *w;
208 QTreeWidgetItem *w2;
209 QTreeWidgetItem *w3 = 0;
210 for(int c = 0; c < brands.size(); c++) {
211 qDebug() << brands.at(c);
212 w = new QTreeWidgetItem();
213 w->setFlags(Qt::ItemIsEnabled);
214 w->setText(0, brands.at(c));
215 // w->setData(0, Qt::DecorationRole, <icon>);
216 items.append(w);
218 // go through platforms again for sake of order
219 for(int it = 0; it < a.size(); it++) {
220 QString curdev;
221 devices->beginGroup("platforms");
222 curdev = devices->value(a.at(it), "null").toString();
223 devices->endGroup();
224 QString curname;
225 devices->beginGroup(curdev);
226 curname = devices->value("name", "null").toString();
227 QString curbrand = devices->value("brand", "").toString();
228 devices->endGroup();
229 if(curbrand != brands.at(c)) continue;
230 qDebug() << "adding:" << brands.at(c) << curname << curdev;
231 w2 = new QTreeWidgetItem(w, QStringList(curname));
232 w2->setData(0, Qt::UserRole, curdev);
233 if(platform.contains(curname)) {
234 w2->setSelected(true);
235 w->setExpanded(true);
236 w3 = w2; // save pointer to hilight old selection
238 items.append(w2);
241 ui.treeDevices->insertTopLevelItems(0, items);
242 if(w3 != 0)
243 ui.treeDevices->setCurrentItem(w3); // hilight old selection
247 void Config::setNoProxy(bool checked)
249 bool i = !checked;
250 ui.proxyPort->setEnabled(i);
251 ui.proxyHost->setEnabled(i);
252 ui.proxyUser->setEnabled(i);
253 ui.proxyPass->setEnabled(i);
257 void Config::setSystemProxy(bool checked)
259 bool i = !checked;
260 ui.proxyPort->setEnabled(i);
261 ui.proxyHost->setEnabled(i);
262 ui.proxyUser->setEnabled(i);
263 ui.proxyPass->setEnabled(i);
264 if(checked) {
265 // save values in input box
266 proxy.setScheme("http");
267 proxy.setUserName(ui.proxyUser->text());
268 proxy.setPassword(ui.proxyPass->text());
269 proxy.setHost(ui.proxyHost->text());
270 proxy.setPort(ui.proxyPort->text().toInt());
271 // show system values in input box
272 #ifdef __linux
273 QUrl envproxy = QUrl(getenv("http_proxy"));
274 ui.proxyHost->setText(envproxy.host());
275 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
276 ui.proxyUser->setText(envproxy.userName());
277 ui.proxyPass->setText(envproxy.password());
278 #endif
280 else {
281 ui.proxyHost->setText(proxy.host());
282 if(proxy.port() > 0)
283 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
284 else ui.proxyPort->setText("");
285 ui.proxyUser->setText(proxy.userName());
286 ui.proxyPass->setText(proxy.password());
292 QStringList Config::findLanguageFiles()
294 QDir dir(programPath + "/");
295 QStringList fileNames;
296 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
298 QDir resDir(":/lang");
299 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
301 fileNames.sort();
302 qDebug() << "Config::findLanguageFiles()" << fileNames;
304 return fileNames;
308 QString Config::languageName(const QString &qmFile)
310 QTranslator translator;
312 if(!translator.load(qmFile, programPath))
313 translator.load(qmFile, ":/lang");
315 return translator.translate("Configure", "English");
319 void Config::updateLanguage()
321 qDebug() << "updateLanguage()";
322 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
323 if(a.size() > 0)
324 language = QFileInfo(lang.value(a.at(0)->text())).baseName();
328 void Config::browseFolder()
330 browser = new BrowseDirtree(this);
331 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
332 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
333 #elif defined(Q_OS_WIN32)
334 browser->setFilter(QDir::Drives);
335 #endif
336 QDir d(ui.mountPoint->text());
337 browser->setDir(d);
338 browser->show();
339 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
343 void Config::setMountpoint(QString m)
345 ui.mountPoint->setText(m);
349 void Config::autodetect()
351 Autodetection detector(this);
353 if(detector.detect()) //let it detect
355 QString devicename = detector.getDevice();
356 //deexpand the platform
357 ui.treeDevices->selectedItems().at(0)->parent()->setExpanded(false);
358 //deselect the selected item
359 ui.treeDevices->selectedItems().at(0)->setSelected(false);
361 // find the new item
362 //enumerate al plattform items
363 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
364 for(int i=0; i< itmList.size();i++)
366 //enumerate device items
367 for(int j=0;j < itmList.at(i)->childCount();j++)
369 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
371 if( devicename.contains(data)) //item found
373 itmList.at(i)->child(j)->setSelected(true); //select the item
374 itmList.at(i)->setExpanded(true); //expand the platform item
375 break;
380 if(detector.getMountPoint() != "" )
382 ui.mountPoint->setText(detector.getMountPoint());
384 else
386 QMessageBox::warning(this, tr("Autodetection"),
387 tr("Could not detect a Mountpoint.\n"
388 "Select your Mountpoint manually."),
389 QMessageBox::Ok ,QMessageBox::Ok);
392 else
394 QMessageBox::warning(this, tr("Autodetection"),
395 tr("Could not detect a device.\n"
396 "Select your device and Mountpoint manually."),
397 QMessageBox::Ok ,QMessageBox::Ok);