1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Riebeling
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 ****************************************************************************/
22 #include "configure.h"
23 #include "autodetection.h"
24 #include "ui_configurefrm.h"
25 #include "browsedirtree.h"
31 #define DEFAULT_LANG "English (builtin)"
33 Config::Config(QWidget
*parent
) : QDialog(parent
)
35 programPath
= qApp
->applicationDirPath() + "/";
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
);
43 ui
.radioSystemProxy
->setEnabled(false); // only on linux for now
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());
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);
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()));
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
;
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
);
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
);
103 QString mp
= ui
.mountPoint
->text();
104 if(QFileInfo(mp
).isDir())
105 userSettings
->setValue("defaults/mountpoint", mp
);
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
);
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());
123 userSettings
->sync();
125 emit
settingsUpdated();
131 qDebug() << "Config::abort()";
136 void Config::setUserSettings(QSettings
*user
)
140 proxy
= userSettings
->value("defaults/proxy").toString();
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
;
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") {
166 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
168 a
= ui
.listLanguages
->findItems(DEFAULT_LANG
, Qt::MatchExactly
);
170 ui
.listLanguages
->setCurrentItem(a
.at(0));
173 ui
.mountPoint
->setText(userSettings
->value("defaults/mountpoint").toString());
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
);
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.")
192 void Config::setDevices(QSettings
*dev
)
195 // setup devices table
196 qDebug() << "Config::setDevices()";
197 devices
->beginGroup("platforms");
198 QStringList a
= devices
->childKeys();
201 QMap
<QString
, QString
> manuf
;
202 QMap
<QString
, QString
> devcs
;
203 for(int it
= 0; it
< a
.size(); it
++) {
205 devices
->beginGroup("platforms");
206 curdev
= devices
->value(a
.at(it
), "null").toString();
209 devices
->beginGroup(curdev
);
210 curname
= devices
->value("name", "null").toString();
211 QString curbrand
= devices
->value("brand", "").toString();
213 manuf
.insertMulti(curbrand
, curdev
);
214 devcs
.insert(curdev
, curname
);
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
;
227 QStringList brands
= manuf
.uniqueKeys();
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>);
239 // go through platforms again for sake of order
240 for(int it
= 0; it
< a
.size(); it
++) {
242 devices
->beginGroup("platforms");
243 curdev
= devices
->value(a
.at(it
), "null").toString();
246 devices
->beginGroup(curdev
);
247 curname
= devices
->value("name", "null").toString();
248 QString curbrand
= devices
->value("brand", "").toString();
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
262 ui
.treeDevices
->insertTopLevelItems(0, items
);
264 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
268 void Config::setNoProxy(bool 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
)
281 ui
.proxyPort
->setEnabled(i
);
282 ui
.proxyHost
->setEnabled(i
);
283 ui
.proxyUser
->setEnabled(i
);
284 ui
.proxyPass
->setEnabled(i
);
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
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());
302 ui
.proxyHost
->setText(proxy
.host());
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
);
323 qDebug() << "Config::findLanguageFiles()" << 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();
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
);
357 QDir
d(ui
.mountPoint
->text());
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
);
372 QDir
d(ui
.cachePath
->text());
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);
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
421 if(detector
.getMountPoint() != "" )
423 ui
.mountPoint
->setText(detector
.getMountPoint());
427 QMessageBox::warning(this, tr("Autodetection"),
428 tr("Could not detect a Mountpoint.\n"
429 "Select your Mountpoint manually."),
430 QMessageBox::Ok
,QMessageBox::Ok
);
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
)
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
);
460 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
463 for(int i
= 0; i
< fn
.size(); i
++) {
464 QString f
= cache
+ fn
.at(i
);
466 qDebug() << "removed:" << f
;