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 ****************************************************************************/
23 #include "configure.h"
24 #include "autodetection.h"
25 #include "ui_configurefrm.h"
26 #include "browsedirtree.h"
32 #if defined(Q_OS_WIN32)
40 #define DEFAULT_LANG "English (en)"
41 #define DEFAULT_LANG_CODE "en"
43 Config::Config(QWidget
*parent
,int index
) : QDialog(parent
)
45 programPath
= qApp
->applicationDirPath() + "/";
47 ui
.tabConfiguration
->setCurrentIndex(index
);
48 ui
.radioManualProxy
->setChecked(true);
49 QRegExpValidator
*proxyValidator
= new QRegExpValidator(this);
50 QRegExp
validate("[0-9]*");
51 proxyValidator
->setRegExp(validate
);
52 ui
.proxyPort
->setValidator(proxyValidator
);
53 #if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN32)
54 ui
.radioSystemProxy
->setEnabled(false); // not on macox for now
56 // build language list and sort alphabetically
57 QStringList langs
= findLanguageFiles();
58 for(int i
= 0; i
< langs
.size(); ++i
)
59 lang
.insert(languageName(langs
.at(i
))
60 + QString(" (%1)").arg(langs
.at(i
)), langs
.at(i
));
61 lang
.insert(DEFAULT_LANG
, DEFAULT_LANG_CODE
);
62 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
63 while (i
!= lang
.constEnd()) {
64 ui
.listLanguages
->addItem(i
.key());
67 ui
.listLanguages
->setSelectionMode(QAbstractItemView::SingleSelection
);
68 ui
.proxyPass
->setEchoMode(QLineEdit::Password
);
69 ui
.treeDevices
->setAlternatingRowColors(true);
70 ui
.listLanguages
->setAlternatingRowColors(true);
74 connect(ui
.buttonOk
, SIGNAL(clicked()), this, SLOT(accept()));
75 connect(ui
.buttonCancel
, SIGNAL(clicked()), this, SLOT(abort()));
76 connect(ui
.radioNoProxy
, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
77 connect(ui
.radioSystemProxy
, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
78 connect(ui
.browseMountPoint
, SIGNAL(clicked()), this, SLOT(browseFolder()));
79 connect(ui
.buttonAutodetect
,SIGNAL(clicked()),this,SLOT(autodetect()));
80 connect(ui
.buttonCacheBrowse
, SIGNAL(clicked()), this, SLOT(browseCache()));
81 connect(ui
.buttonCacheClear
, SIGNAL(clicked()), this, SLOT(cacheClear()));
82 connect(ui
.configTts
, SIGNAL(clicked()), this, SLOT(configTts()));
83 connect(ui
.configEncoder
, SIGNAL(clicked()), this, SLOT(configEnc()));
84 connect(ui
.comboTts
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
85 connect(ui
.treeDevices
, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
93 qDebug() << "Config::accept()";
94 // proxy: save entered proxy values, not displayed.
95 if(ui
.radioManualProxy
->isChecked()) {
96 proxy
.setScheme("http");
97 proxy
.setUserName(ui
.proxyUser
->text());
98 proxy
.setPassword(ui
.proxyPass
->text());
99 proxy
.setHost(ui
.proxyHost
->text());
100 proxy
.setPort(ui
.proxyPort
->text().toInt());
103 settings
->setProxy(proxy
.toString());
104 qDebug() << "new proxy:" << proxy
;
107 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
108 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
109 else proxyType
= "manual";
110 settings
->setProxyType(proxyType
);
113 if(settings
->curLang() != language
&& !language
.isEmpty()) {
114 QMessageBox::information(this, tr("Language changed"),
115 tr("You need to restart the application for the changed language to take effect."));
116 settings
->setLang(language
);
120 QString mp
= ui
.mountPoint
->text();
121 if(QFileInfo(mp
).isDir())
122 settings
->setMountpoint(QDir::fromNativeSeparators(mp
));
126 if(ui
.treeDevices
->selectedItems().size() != 0) {
127 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
128 settings
->setCurPlatform(nplat
);
132 if(QFileInfo(ui
.cachePath
->text()).isDir())
133 settings
->setCachePath(ui
.cachePath
->text());
134 else // default to system temp path
135 settings
->setCachePath( QDir::tempPath());
136 settings
->setCacheDisable(ui
.cacheDisable
->isChecked());
137 settings
->setCacheOffline(ui
.cacheOfflineMode
->isChecked());
140 int i
= ui
.comboTts
->currentIndex();
141 settings
->setCurTTS(ui
.comboTts
->itemData(i
).toString());
143 settings
->setCurVersion(PUREVERSION
);
148 emit
settingsUpdated();
154 qDebug() << "Config::abort()";
158 void Config::setSettings(RbSettings
* sett
)
166 void Config::setUserSettings()
169 proxy
= settings
->proxy();
172 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
173 else ui
.proxyPort
->setText("");
174 ui
.proxyHost
->setText(proxy
.host());
175 ui
.proxyUser
->setText(proxy
.userName());
176 ui
.proxyPass
->setText(proxy
.password());
178 QString proxyType
= settings
->proxyType();
179 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
180 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
181 else ui
.radioNoProxy
->setChecked(true);
183 // set language selection
184 QList
<QListWidgetItem
*> a
;
186 // find key for lang value
187 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
188 while (i
!= lang
.constEnd()) {
189 if(i
.value() == settings
->curLang()) {
193 else if(settings
->curLang().startsWith(i
.value(), Qt::CaseInsensitive
)) {
194 // check if there is a base language (en -> en_US, etc.)
200 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
202 ui
.listLanguages
->setCurrentItem(a
.at(0));
203 // don't connect before language list has been set up to prevent
204 // triggering the signal by selecting the saved language.
205 connect(ui
.listLanguages
, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
208 ui
.mountPoint
->setText(QDir::toNativeSeparators(settings
->mountpoint()));
211 if(!QFileInfo(settings
->cachePath()).isDir())
212 settings
->setCachePath(QDir::tempPath());
213 ui
.cachePath
->setText(settings
->cachePath());
214 ui
.cacheDisable
->setChecked(settings
->cacheDisabled());
215 ui
.cacheOfflineMode
->setChecked(settings
->cacheOffline());
216 updateCacheInfo(settings
->cachePath());
220 void Config::updateCacheInfo(QString path
)
223 fs
= QDir(path
+ "/rbutil-cache/").entryInfoList(QDir::Files
| QDir::NoDotAndDotDot
);
225 for(int i
= 0; i
< fs
.size(); i
++) {
226 sz
+= fs
.at(i
).size();
227 qDebug() << fs
.at(i
).fileName() << fs
.at(i
).size();
229 ui
.cacheSize
->setText(tr("Current cache size is %L1 kiB.")
234 void Config::setDevices()
237 // setup devices table
238 qDebug() << "Config::setDevices()";
240 QStringList platformList
= settings
->allPlatforms();
242 QMap
<QString
, QString
> manuf
;
243 QMap
<QString
, QString
> devcs
;
244 for(int it
= 0; it
< platformList
.size(); it
++)
246 QString curname
= settings
->name(platformList
.at(it
));
247 QString curbrand
= settings
->brand(platformList
.at(it
));
248 manuf
.insertMulti(curbrand
, platformList
.at(it
));
249 devcs
.insert(platformList
.at(it
), curname
);
253 platform
= devcs
.value(settings
->curPlatform());
255 // set up devices table
256 ui
.treeDevices
->header()->hide();
257 ui
.treeDevices
->expandAll();
258 ui
.treeDevices
->setColumnCount(1);
259 QList
<QTreeWidgetItem
*> items
;
262 QStringList brands
= manuf
.uniqueKeys();
265 QTreeWidgetItem
*w3
= 0;
266 for(int c
= 0; c
< brands
.size(); c
++) {
267 qDebug() << brands
.at(c
);
268 w
= new QTreeWidgetItem();
269 w
->setFlags(Qt::ItemIsEnabled
);
270 w
->setText(0, brands
.at(c
));
273 // go through platforms again for sake of order
274 for(int it
= 0; it
< platformList
.size(); it
++) {
276 QString curname
= settings
->name(platformList
.at(it
));
277 QString curbrand
= settings
->brand(platformList
.at(it
));
279 if(curbrand
!= brands
.at(c
)) continue;
280 qDebug() << "adding:" << brands
.at(c
) << curname
;
281 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
282 w2
->setData(0, Qt::UserRole
, platformList
.at(it
));
284 if(platform
.contains(curname
)) {
285 w2
->setSelected(true);
286 w
->setExpanded(true);
287 w3
= w2
; // save pointer to hilight old selection
292 ui
.treeDevices
->insertTopLevelItems(0, items
);
294 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
302 QStringList ttslist
= TTSBase::getTTSList();
303 for(int a
= 0; a
< ttslist
.size(); a
++)
304 ui
.comboTts
->addItem(TTSBase::getTTSName(ttslist
.at(a
)), ttslist
.at(a
));
305 //update index of combobox
306 int index
= ui
.comboTts
->findData(settings
->curTTS());
307 if(index
< 0) index
= 0;
308 ui
.comboTts
->setCurrentIndex(index
);
309 updateTtsState(index
);
314 void Config::updateTtsState(int index
)
316 QString ttsName
= ui
.comboTts
->itemData(index
).toString();
317 TTSBase
* tts
= TTSBase::getTTS(ttsName
);
318 tts
->setCfg(settings
);
322 ui
.configTTSstatus
->setText(tr("Configuration OK"));
323 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
327 ui
.configTTSstatus
->setText(tr("Configuration INVALID"));
328 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
332 void Config::updateEncState()
334 // FIXME: this is a workaround to make the encoder follow the device selection
335 // even with the settings (and thus the device) being saved. Needs to be redone
336 // properly later by extending the settings object
337 if(ui
.treeDevices
->selectedItems().size() == 0)
340 QString devname
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
341 QString olddevice
= settings
->curPlatform();
342 settings
->setCurPlatform(devname
);
343 QString encoder
= settings
->curEncoder();
344 ui
.encoderName
->setText(EncBase::getEncoderName(settings
->curEncoder()));
345 settings
->setCurPlatform(olddevice
);
347 EncBase
* enc
= EncBase::getEncoder(encoder
);
348 enc
->setCfg(settings
);
352 ui
.configEncstatus
->setText(tr("Configuration OK"));
353 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
357 ui
.configEncstatus
->setText(tr("Configuration INVALID"));
358 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
362 void Config::setNoProxy(bool checked
)
365 ui
.proxyPort
->setEnabled(i
);
366 ui
.proxyHost
->setEnabled(i
);
367 ui
.proxyUser
->setEnabled(i
);
368 ui
.proxyPass
->setEnabled(i
);
372 void Config::setSystemProxy(bool checked
)
375 ui
.proxyPort
->setEnabled(i
);
376 ui
.proxyHost
->setEnabled(i
);
377 ui
.proxyUser
->setEnabled(i
);
378 ui
.proxyPass
->setEnabled(i
);
380 // save values in input box
381 proxy
.setScheme("http");
382 proxy
.setUserName(ui
.proxyUser
->text());
383 proxy
.setPassword(ui
.proxyPass
->text());
384 proxy
.setHost(ui
.proxyHost
->text());
385 proxy
.setPort(ui
.proxyPort
->text().toInt());
386 // show system values in input box
387 QUrl envproxy
= Detect::systemProxy();
389 ui
.proxyHost
->setText(envproxy
.host());
391 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
392 ui
.proxyUser
->setText(envproxy
.userName());
393 ui
.proxyPass
->setText(envproxy
.password());
397 ui
.proxyHost
->setText(proxy
.host());
399 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
400 else ui
.proxyPort
->setText("");
401 ui
.proxyUser
->setText(proxy
.userName());
402 ui
.proxyPass
->setText(proxy
.password());
408 QStringList
Config::findLanguageFiles()
410 QDir
dir(programPath
);
411 QStringList fileNames
;
413 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
415 QDir
resDir(":/lang");
416 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
418 QRegExp
exp("^rbutil_(.*)\\.qm");
419 for(int i
= 0; i
< fileNames
.size(); i
++) {
420 QString a
= fileNames
.at(i
);
421 a
.replace(exp
, "\\1");
425 qDebug() << "Config::findLanguageFiles()" << langs
;
431 QString
Config::languageName(const QString
&qmFile
)
433 QTranslator translator
;
435 QString file
= "rbutil_" + qmFile
;
436 if(!translator
.load(file
, programPath
))
437 translator
.load(file
, ":/lang");
439 return translator
.translate("Configure", "English",
440 "This is the localized language name, i.e. your language.");
444 void Config::updateLanguage()
446 qDebug() << "updateLanguage()";
447 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
449 language
= lang
.value(a
.at(0)->text());
450 qDebug() << language
;
454 void Config::browseFolder()
456 browser
= new BrowseDirtree(this,tr("Select your device"));
457 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
458 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
459 #elif defined(Q_OS_WIN32)
460 browser
->setFilter(QDir::Drives
);
462 #if defined(Q_OS_MACX)
463 browser
->setRoot("/Volumes");
464 #elif defined(Q_OS_LINUX)
465 browser
->setDir("/media");
467 if( ui
.mountPoint
->text() != "" )
469 browser
->setDir(ui
.mountPoint
->text());
472 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
476 void Config::browseCache()
478 cbrowser
= new BrowseDirtree(this);
479 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
480 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
481 #elif defined(Q_OS_WIN32)
482 cbrowser
->setFilter(QDir::Drives
);
484 cbrowser
->setDir(ui
.cachePath
->text());
485 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
490 void Config::setMountpoint(QString m
)
492 ui
.mountPoint
->setText(m
);
496 void Config::setCache(QString c
)
498 ui
.cachePath
->setText(c
);
503 void Config::autodetect()
505 Autodetection
detector(this);
506 detector
.setSettings(settings
);
507 // disable tree during detection as "working" feedback.
508 // TODO: replace the tree view with a splash screen during this time.
509 ui
.treeDevices
->setEnabled(false);
510 QCoreApplication::processEvents();
512 if(detector
.detect()) //let it detect
514 QString devicename
= detector
.getDevice();
515 // deexpand all items
516 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
517 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
518 //deselect the selected item(s)
519 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
520 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
523 // enumerate all platform items
524 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
525 for(int i
=0; i
< itmList
.size();i
++)
527 //enumerate device items
528 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
530 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
532 if(devicename
== data
) // item found
534 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
535 itmList
.at(i
)->setExpanded(true); //expand the platform item
536 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
542 if(!detector
.errdev().isEmpty()) {
544 if(detector
.errdev() == "sansae200")
545 text
= tr("Sansa e200 in MTP mode found!\n"
546 "You need to change your player to MSC mode for installation. ");
547 if(detector
.errdev() == "h10")
548 text
= tr("H10 20GB in MTP mode found!\n"
549 "You need to change your player to UMS mode for installation. ");
550 text
+= tr("Unless you changed this installation will fail!");
552 QMessageBox::critical(this, tr("Fatal error"), text
, QMessageBox::Ok
);
555 if(!detector
.incompatdev().isEmpty()) {
557 // we need to set the platform here to get the brand from the
559 settings
->setCurPlatform(detector
.incompatdev());
560 text
= tr("Detected an unsupported %1 player variant. Sorry, "
561 "Rockbox doesn't run on your player.").arg(settings
->curBrand());
563 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
564 text
, QMessageBox::Ok
);
568 if(detector
.getMountPoint() != "" )
570 ui
.mountPoint
->setText(QDir::toNativeSeparators(detector
.getMountPoint()));
574 QMessageBox::warning(this, tr("Autodetection"),
575 tr("Could not detect a Mountpoint.\n"
576 "Select your Mountpoint manually."),
577 QMessageBox::Ok
,QMessageBox::Ok
);
582 QMessageBox::warning(this, tr("Autodetection"),
583 tr("Could not detect a device.\n"
584 "Select your device and Mountpoint manually."),
585 QMessageBox::Ok
,QMessageBox::Ok
);
588 ui
.treeDevices
->setEnabled(true);
591 void Config::cacheClear()
593 if(QMessageBox::critical(this, tr("Really delete cache?"),
594 tr("Do you really want to delete the cache? "
595 "Make absolutely sure this setting is correct as it will "
596 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
597 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
600 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
601 if(!QFileInfo(cache
).isDir()) {
602 QMessageBox::critical(this, tr("Path wrong!"),
603 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
608 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
611 for(int i
= 0; i
< fn
.size(); i
++) {
612 QString f
= cache
+ fn
.at(i
);
614 qDebug() << "removed:" << f
;
616 updateCacheInfo(settings
->cachePath());
620 void Config::configTts()
622 int index
= ui
.comboTts
->currentIndex();
623 TTSBase
* tts
= TTSBase::getTTS(ui
.comboTts
->itemData(index
).toString());
625 tts
->setCfg(settings
);
627 updateTtsState(ui
.comboTts
->currentIndex());
631 void Config::configEnc()
633 EncBase
* enc
= EncBase::getEncoder(settings
->curEncoder());
635 enc
->setCfg(settings
);