Cache was intended to be disabled per default. Also make the "disable download cache...
[Rockbox.git] / rbutil / rbutilqt / configure.cpp
blobe6b97b214db9722bc673f9ef6adf6a7bace6febc
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()));
69 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
70 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
76 void Config::accept()
78 qDebug() << "Config::accept()";
79 // proxy: save entered proxy values, not displayed.
80 if(ui.radioManualProxy->isChecked()) {
81 proxy.setScheme("http");
82 proxy.setUserName(ui.proxyUser->text());
83 proxy.setPassword(ui.proxyPass->text());
84 proxy.setHost(ui.proxyHost->text());
85 proxy.setPort(ui.proxyPort->text().toInt());
87 userSettings->setValue("defaults/proxy", proxy.toString());
88 qDebug() << "new proxy:" << proxy;
89 // proxy type
90 QString proxyType;
91 if(ui.radioNoProxy->isChecked()) proxyType = "none";
92 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
93 else proxyType = "manual";
94 userSettings->setValue("defaults/proxytype", proxyType);
96 // language
97 if(userSettings->value("defaults/lang").toString() != language)
98 QMessageBox::information(this, tr("Language changed"),
99 tr("You need to restart the application for the changed language to take effect."));
100 userSettings->setValue("defaults/lang", language);
102 // mountpoint
103 QString mp = ui.mountPoint->text();
104 if(QFileInfo(mp).isDir())
105 userSettings->setValue("defaults/mountpoint", mp);
107 // platform
108 QString nplat;
109 if(ui.treeDevices->selectedItems().size() != 0) {
110 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
111 userSettings->setValue("defaults/platform", nplat);
114 // cache settings
115 if(QFileInfo(ui.cachePath->text()).isDir())
116 userSettings->setValue("defaults/cachepath", ui.cachePath->text());
117 else // default to system temp path
118 userSettings->setValue("defaults/cachepath", QDir::tempPath());
119 userSettings->setValue("defaults/cachedisable", ui.cacheDisable->isChecked());
120 userSettings->setValue("defaults/offline", ui.cacheOfflineMode->isChecked());
122 // sync settings
123 userSettings->sync();
124 this->close();
125 emit settingsUpdated();
129 void Config::abort()
131 qDebug() << "Config::abort()";
132 this->close();
136 void Config::setUserSettings(QSettings *user)
138 userSettings = user;
139 // set proxy
140 proxy = userSettings->value("defaults/proxy").toString();
142 if(proxy.port() > 0)
143 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
144 else ui.proxyPort->setText("");
145 ui.proxyHost->setText(proxy.host());
146 ui.proxyUser->setText(proxy.userName());
147 ui.proxyPass->setText(proxy.password());
149 QString proxyType = userSettings->value("defaults/proxytype").toString();
150 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
151 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
152 else ui.radioNoProxy->setChecked(true);
154 // set language selection
155 QList<QListWidgetItem*> a;
156 QString b;
157 // find key for lang value
158 QMap<QString, QString>::const_iterator i = lang.constBegin();
159 while (i != lang.constEnd()) {
160 if(i.value() == userSettings->value("defaults/lang").toString() + ".qm") {
161 b = i.key();
162 break;
164 i++;
166 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
167 if(a.size() <= 0)
168 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
169 if(a.size() > 0)
170 ui.listLanguages->setCurrentItem(a.at(0));
172 // devices tab
173 ui.mountPoint->setText(userSettings->value("defaults/mountpoint").toString());
175 // cache tab
176 if(!QFileInfo(userSettings->value("defaults/cachepath").toString()).isDir())
177 userSettings->setValue("defaults/cachepath", QDir::tempPath());
178 ui.cachePath->setText(userSettings->value("defaults/cachepath").toString());
179 ui.cacheDisable->setChecked(userSettings->value("defaults/cachedisable", true).toBool());
180 ui.cacheOfflineMode->setChecked(userSettings->value("defaults/offline").toBool());
181 QList<QFileInfo> fs = QDir(userSettings->value("defaults/cachepath").toString() + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
182 qint64 sz = 0;
183 for(int i = 0; i < fs.size(); i++) {
184 sz += fs.at(i).size();
185 qDebug() << fs.at(i).fileName() << fs.at(i).size();
187 ui.cacheSize->setText(tr("Current cache size is %1 kiB.")
188 .arg(sz/1024));
192 void Config::setDevices(QSettings *dev)
194 devices = dev;
195 // setup devices table
196 qDebug() << "Config::setDevices()";
197 devices->beginGroup("platforms");
198 QStringList a = devices->childKeys();
199 devices->endGroup();
201 QMap <QString, QString> manuf;
202 QMap <QString, QString> devcs;
203 for(int it = 0; it < a.size(); it++) {
204 QString curdev;
205 devices->beginGroup("platforms");
206 curdev = devices->value(a.at(it), "null").toString();
207 devices->endGroup();
208 QString curname;
209 devices->beginGroup(curdev);
210 curname = devices->value("name", "null").toString();
211 QString curbrand = devices->value("brand", "").toString();
212 devices->endGroup();
213 manuf.insertMulti(curbrand, curdev);
214 devcs.insert(curdev, curname);
217 QString platform;
218 platform = devcs.value(userSettings->value("defaults/platform").toString());
220 // set up devices table
221 ui.treeDevices->header()->hide();
222 ui.treeDevices->expandAll();
223 ui.treeDevices->setColumnCount(1);
224 QList<QTreeWidgetItem *> items;
226 // get manufacturers
227 QStringList brands = manuf.uniqueKeys();
228 QTreeWidgetItem *w;
229 QTreeWidgetItem *w2;
230 QTreeWidgetItem *w3 = 0;
231 for(int c = 0; c < brands.size(); c++) {
232 qDebug() << brands.at(c);
233 w = new QTreeWidgetItem();
234 w->setFlags(Qt::ItemIsEnabled);
235 w->setText(0, brands.at(c));
236 // w->setData(0, Qt::DecorationRole, <icon>);
237 items.append(w);
239 // go through platforms again for sake of order
240 for(int it = 0; it < a.size(); it++) {
241 QString curdev;
242 devices->beginGroup("platforms");
243 curdev = devices->value(a.at(it), "null").toString();
244 devices->endGroup();
245 QString curname;
246 devices->beginGroup(curdev);
247 curname = devices->value("name", "null").toString();
248 QString curbrand = devices->value("brand", "").toString();
249 devices->endGroup();
250 if(curbrand != brands.at(c)) continue;
251 qDebug() << "adding:" << brands.at(c) << curname << curdev;
252 w2 = new QTreeWidgetItem(w, QStringList(curname));
253 w2->setData(0, Qt::UserRole, curdev);
254 if(platform.contains(curname)) {
255 w2->setSelected(true);
256 w->setExpanded(true);
257 w3 = w2; // save pointer to hilight old selection
259 items.append(w2);
262 ui.treeDevices->insertTopLevelItems(0, items);
263 if(w3 != 0)
264 ui.treeDevices->setCurrentItem(w3); // hilight old selection
268 void Config::setNoProxy(bool checked)
270 bool i = !checked;
271 ui.proxyPort->setEnabled(i);
272 ui.proxyHost->setEnabled(i);
273 ui.proxyUser->setEnabled(i);
274 ui.proxyPass->setEnabled(i);
278 void Config::setSystemProxy(bool checked)
280 bool i = !checked;
281 ui.proxyPort->setEnabled(i);
282 ui.proxyHost->setEnabled(i);
283 ui.proxyUser->setEnabled(i);
284 ui.proxyPass->setEnabled(i);
285 if(checked) {
286 // save values in input box
287 proxy.setScheme("http");
288 proxy.setUserName(ui.proxyUser->text());
289 proxy.setPassword(ui.proxyPass->text());
290 proxy.setHost(ui.proxyHost->text());
291 proxy.setPort(ui.proxyPort->text().toInt());
292 // show system values in input box
293 #ifdef __linux
294 QUrl envproxy = QUrl(getenv("http_proxy"));
295 ui.proxyHost->setText(envproxy.host());
296 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
297 ui.proxyUser->setText(envproxy.userName());
298 ui.proxyPass->setText(envproxy.password());
299 #endif
301 else {
302 ui.proxyHost->setText(proxy.host());
303 if(proxy.port() > 0)
304 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
305 else ui.proxyPort->setText("");
306 ui.proxyUser->setText(proxy.userName());
307 ui.proxyPass->setText(proxy.password());
313 QStringList Config::findLanguageFiles()
315 QDir dir(programPath);
316 QStringList fileNames;
317 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
319 QDir resDir(":/lang");
320 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
322 fileNames.sort();
323 qDebug() << "Config::findLanguageFiles()" << fileNames;
325 return fileNames;
329 QString Config::languageName(const QString &qmFile)
331 QTranslator translator;
333 if(!translator.load(qmFile, programPath))
334 translator.load(qmFile, ":/lang");
336 return translator.translate("Configure", "English");
340 void Config::updateLanguage()
342 qDebug() << "updateLanguage()";
343 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
344 if(a.size() > 0)
345 language = QFileInfo(lang.value(a.at(0)->text())).baseName();
349 void Config::browseFolder()
351 browser = new BrowseDirtree(this);
352 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
353 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
354 #elif defined(Q_OS_WIN32)
355 browser->setFilter(QDir::Drives);
356 #endif
357 QDir d(ui.mountPoint->text());
358 browser->setDir(d);
359 browser->show();
360 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
364 void Config::browseCache()
366 cbrowser = new BrowseDirtree(this);
367 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
368 cbrowser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
369 #elif defined(Q_OS_WIN32)
370 cbrowser->setFilter(QDir::Drives);
371 #endif
372 QDir d(ui.cachePath->text());
373 cbrowser->setDir(d);
374 cbrowser->show();
375 connect(cbrowser, SIGNAL(itemChanged(QString)), this, SLOT(setCache(QString)));
378 void Config::setMountpoint(QString m)
380 ui.mountPoint->setText(m);
384 void Config::setCache(QString c)
386 ui.cachePath->setText(c);
390 void Config::autodetect()
392 Autodetection detector(this);
394 if(detector.detect()) //let it detect
396 QString devicename = detector.getDevice();
397 //deexpand the platform
398 ui.treeDevices->selectedItems().at(0)->parent()->setExpanded(false);
399 //deselect the selected item
400 ui.treeDevices->selectedItems().at(0)->setSelected(false);
402 // find the new item
403 //enumerate al plattform items
404 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
405 for(int i=0; i< itmList.size();i++)
407 //enumerate device items
408 for(int j=0;j < itmList.at(i)->childCount();j++)
410 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
412 if( devicename.contains(data)) //item found
414 itmList.at(i)->child(j)->setSelected(true); //select the item
415 itmList.at(i)->setExpanded(true); //expand the platform item
416 break;
421 if(detector.getMountPoint() != "" )
423 ui.mountPoint->setText(detector.getMountPoint());
425 else
427 QMessageBox::warning(this, tr("Autodetection"),
428 tr("Could not detect a Mountpoint.\n"
429 "Select your Mountpoint manually."),
430 QMessageBox::Ok ,QMessageBox::Ok);
433 else
435 QMessageBox::warning(this, tr("Autodetection"),
436 tr("Could not detect a device.\n"
437 "Select your device and Mountpoint manually."),
438 QMessageBox::Ok ,QMessageBox::Ok);
443 void Config::cacheClear()
445 if(QMessageBox::critical(this, tr("Really delete cache?"),
446 tr("Do you really want to delete the cache? "
447 "Make absolutely sure this setting is correct as it will "
448 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
449 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
450 return;
452 QString cache = ui.cachePath->text() + "/rbutil-cache/";
453 if(!QFileInfo(cache).isDir()) {
454 QMessageBox::critical(this, tr("Path wrong!"),
455 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
456 return;
458 QDir dir(cache);
459 QStringList fn;
460 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
461 qDebug() << fn;
463 for(int i = 0; i < fn.size(); i++) {
464 QString f = cache + fn.at(i);
465 QFile::remove(f);
466 qDebug() << "removed:" << f;