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"
30 #include "encttscfggui.h"
31 #include "rbsettings.h"
34 #if defined(Q_OS_WIN32)
42 #define DEFAULT_LANG "English (en)"
43 #define DEFAULT_LANG_CODE "en"
45 Config::Config(QWidget
*parent
,int index
) : QDialog(parent
)
47 programPath
= qApp
->applicationDirPath() + "/";
49 ui
.tabConfiguration
->setCurrentIndex(index
);
50 ui
.radioManualProxy
->setChecked(true);
51 QRegExpValidator
*proxyValidator
= new QRegExpValidator(this);
52 QRegExp
validate("[0-9]*");
53 proxyValidator
->setRegExp(validate
);
54 ui
.proxyPort
->setValidator(proxyValidator
);
55 #if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN32)
56 ui
.radioSystemProxy
->setEnabled(false); // not on macox for now
58 // build language list and sort alphabetically
59 QStringList langs
= findLanguageFiles();
60 for(int i
= 0; i
< langs
.size(); ++i
)
61 lang
.insert(languageName(langs
.at(i
))
62 + QString(" (%1)").arg(langs
.at(i
)), langs
.at(i
));
63 lang
.insert(DEFAULT_LANG
, DEFAULT_LANG_CODE
);
64 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
65 while (i
!= lang
.constEnd()) {
66 ui
.listLanguages
->addItem(i
.key());
69 ui
.listLanguages
->setSelectionMode(QAbstractItemView::SingleSelection
);
70 ui
.proxyPass
->setEchoMode(QLineEdit::Password
);
71 ui
.treeDevices
->setAlternatingRowColors(true);
72 ui
.listLanguages
->setAlternatingRowColors(true);
76 connect(ui
.buttonOk
, SIGNAL(clicked()), this, SLOT(accept()));
77 connect(ui
.buttonCancel
, SIGNAL(clicked()), this, SLOT(abort()));
78 connect(ui
.radioNoProxy
, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
79 connect(ui
.radioSystemProxy
, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
80 connect(ui
.browseMountPoint
, SIGNAL(clicked()), this, SLOT(browseFolder()));
81 connect(ui
.buttonAutodetect
,SIGNAL(clicked()),this,SLOT(autodetect()));
82 connect(ui
.buttonCacheBrowse
, SIGNAL(clicked()), this, SLOT(browseCache()));
83 connect(ui
.buttonCacheClear
, SIGNAL(clicked()), this, SLOT(cacheClear()));
84 connect(ui
.configTts
, SIGNAL(clicked()), this, SLOT(configTts()));
85 connect(ui
.configEncoder
, SIGNAL(clicked()), this, SLOT(configEnc()));
86 connect(ui
.comboTts
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
87 connect(ui
.treeDevices
, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
95 qDebug() << "Config::accept()";
96 QString errormsg
= tr("The following errors occurred:") + "<ul>";
99 // proxy: save entered proxy values, not displayed.
100 if(ui
.radioManualProxy
->isChecked()) {
101 proxy
.setScheme("http");
102 proxy
.setUserName(ui
.proxyUser
->text());
103 proxy
.setPassword(ui
.proxyPass
->text());
104 proxy
.setHost(ui
.proxyHost
->text());
105 proxy
.setPort(ui
.proxyPort
->text().toInt());
108 RbSettings::setValue(RbSettings::Proxy
, proxy
.toString());
109 qDebug() << "new proxy:" << proxy
;
112 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
113 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
114 else proxyType
= "manual";
115 RbSettings::setValue(RbSettings::ProxyType
, proxyType
);
118 if(RbSettings::value(RbSettings::Language
).toString() != language
&& !language
.isEmpty()) {
119 QMessageBox::information(this, tr("Language changed"),
120 tr("You need to restart the application for the changed language to take effect."));
121 RbSettings::setValue(RbSettings::Language
, language
);
125 QString mp
= ui
.mountPoint
->text();
127 errormsg
+= "<li>" + tr("No mountpoint given") + "</li>";
130 else if(!QFileInfo(mp
).exists()) {
131 errormsg
+= "<li>" + tr("Mountpoint does not exist") + "</li>";
134 else if(!QFileInfo(mp
).isDir()) {
135 errormsg
+= "<li>" + tr("Mountpoint is not a directory.") + "</li>";
138 else if(!QFileInfo(mp
).isWritable()) {
139 errormsg
+= "<li>" + tr("Mountpoint is not writeable") + "</li>";
143 RbSettings::setValue(RbSettings::Mountpoint
, QDir::fromNativeSeparators(mp
));
148 if(ui
.treeDevices
->selectedItems().size() != 0) {
149 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
150 RbSettings::setValue(RbSettings::Platform
, nplat
);
153 errormsg
+= "<li>" + tr("No player selected") + "</li>";
158 if(QFileInfo(ui
.cachePath
->text()).isDir()) {
159 if(!QFileInfo(ui
.cachePath
->text()).isWritable()) {
160 errormsg
+= "<li>" + tr("Cache path not writeable. Leave path empty "
161 "to default to systems temporary path.") + "</li>";
165 RbSettings::setValue(RbSettings::CachePath
, ui
.cachePath
->text());
167 else // default to system temp path
168 RbSettings::setValue(RbSettings::CachePath
, QDir::tempPath());
169 RbSettings::setValue(RbSettings::CacheDisabled
, ui
.cacheDisable
->isChecked());
170 RbSettings::setValue(RbSettings::CacheOffline
, ui
.cacheOfflineMode
->isChecked());
173 int i
= ui
.comboTts
->currentIndex();
174 RbSettings::setValue(RbSettings::Tts
, ui
.comboTts
->itemData(i
).toString());
176 RbSettings::setValue(RbSettings::RbutilVersion
, PUREVERSION
);
179 errormsg
+= tr("You need to fix the above errors before you can continue.");
182 QMessageBox::critical(this, tr("Configuration error"), errormsg
);
188 emit
settingsUpdated();
195 qDebug() << "Config::abort()";
199 void Config::setUserSettings()
202 proxy
= RbSettings::value(RbSettings::Proxy
).toString();
205 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
206 else ui
.proxyPort
->setText("");
207 ui
.proxyHost
->setText(proxy
.host());
208 ui
.proxyUser
->setText(proxy
.userName());
209 ui
.proxyPass
->setText(proxy
.password());
211 QString proxyType
= RbSettings::value(RbSettings::ProxyType
).toString();
212 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
213 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
214 else ui
.radioNoProxy
->setChecked(true);
216 // set language selection
217 QList
<QListWidgetItem
*> a
;
219 // find key for lang value
220 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
221 QString l
= RbSettings::value(RbSettings::Language
).toString();
223 l
= QLocale::system().name();
224 while (i
!= lang
.constEnd()) {
229 else if(l
.startsWith(i
.value(), Qt::CaseInsensitive
)) {
230 // check if there is a base language (en -> en_US, etc.)
236 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
238 ui
.listLanguages
->setCurrentItem(a
.at(0));
239 // don't connect before language list has been set up to prevent
240 // triggering the signal by selecting the saved language.
241 connect(ui
.listLanguages
, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
244 ui
.mountPoint
->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint
).toString()));
247 if(!QFileInfo(RbSettings::value(RbSettings::CachePath
).toString()).isDir())
248 RbSettings::setValue(RbSettings::CachePath
, QDir::tempPath());
249 ui
.cachePath
->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath
).toString()));
250 ui
.cacheDisable
->setChecked(RbSettings::value(RbSettings::CacheDisabled
).toBool());
251 ui
.cacheOfflineMode
->setChecked(RbSettings::value(RbSettings::CacheOffline
).toBool());
252 updateCacheInfo(RbSettings::value(RbSettings::CachePath
).toString());
256 void Config::updateCacheInfo(QString path
)
259 fs
= QDir(path
+ "/rbutil-cache/").entryInfoList(QDir::Files
| QDir::NoDotAndDotDot
);
261 for(int i
= 0; i
< fs
.size(); i
++) {
262 sz
+= fs
.at(i
).size();
263 qDebug() << fs
.at(i
).fileName() << fs
.at(i
).size();
265 ui
.cacheSize
->setText(tr("Current cache size is %L1 kiB.")
270 void Config::setDevices()
273 // setup devices table
274 qDebug() << "Config::setDevices()";
276 QStringList platformList
= RbSettings::platforms();
278 QMap
<QString
, QString
> manuf
;
279 QMap
<QString
, QString
> devcs
;
280 for(int it
= 0; it
< platformList
.size(); it
++)
282 QString curname
= RbSettings::name(platformList
.at(it
));
283 QString curbrand
= RbSettings::brand(platformList
.at(it
));
284 manuf
.insertMulti(curbrand
, platformList
.at(it
));
285 devcs
.insert(platformList
.at(it
), curname
);
289 platform
= devcs
.value(RbSettings::value(RbSettings::Platform
).toString());
291 // set up devices table
292 ui
.treeDevices
->header()->hide();
293 ui
.treeDevices
->expandAll();
294 ui
.treeDevices
->setColumnCount(1);
295 QList
<QTreeWidgetItem
*> items
;
298 QStringList brands
= manuf
.uniqueKeys();
301 QTreeWidgetItem
*w3
= 0;
302 for(int c
= 0; c
< brands
.size(); c
++) {
303 qDebug() << brands
.at(c
);
304 w
= new QTreeWidgetItem();
305 w
->setFlags(Qt::ItemIsEnabled
);
306 w
->setText(0, brands
.at(c
));
309 // go through platforms again for sake of order
310 for(int it
= 0; it
< platformList
.size(); it
++) {
312 QString curname
= RbSettings::name(platformList
.at(it
));
313 QString curbrand
= RbSettings::brand(platformList
.at(it
));
315 if(curbrand
!= brands
.at(c
)) continue;
316 qDebug() << "adding:" << brands
.at(c
) << curname
;
317 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
318 w2
->setData(0, Qt::UserRole
, platformList
.at(it
));
320 if(platform
.contains(curname
)) {
321 w2
->setSelected(true);
322 w
->setExpanded(true);
323 w3
= w2
; // save pointer to hilight old selection
328 ui
.treeDevices
->insertTopLevelItems(0, items
);
330 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
338 QStringList ttslist
= TTSBase::getTTSList();
339 for(int a
= 0; a
< ttslist
.size(); a
++)
340 ui
.comboTts
->addItem(TTSBase::getTTSName(ttslist
.at(a
)), ttslist
.at(a
));
341 //update index of combobox
342 int index
= ui
.comboTts
->findData(RbSettings::value(RbSettings::Tts
).toString());
343 if(index
< 0) index
= 0;
344 ui
.comboTts
->setCurrentIndex(index
);
345 updateTtsState(index
);
350 void Config::updateTtsState(int index
)
352 QString ttsName
= ui
.comboTts
->itemData(index
).toString();
353 TTSBase
* tts
= TTSBase::getTTS(this,ttsName
);
357 ui
.configTTSstatus
->setText(tr("Configuration OK"));
358 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
362 ui
.configTTSstatus
->setText(tr("Configuration INVALID"));
363 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
367 void Config::updateEncState()
369 // FIXME: this is a workaround to make the encoder follow the device selection
370 // even with the settings (and thus the device) being saved. Needs to be redone
371 // properly later by extending the settings object
372 if(ui
.treeDevices
->selectedItems().size() == 0)
375 QString devname
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
376 QString olddevice
= RbSettings::value(RbSettings::Platform
).toString();
377 RbSettings::setValue(RbSettings::Platform
, devname
);
378 QString encoder
= RbSettings::value(RbSettings::CurEncoder
).toString();
379 ui
.encoderName
->setText(EncBase::getEncoderName(RbSettings::value(RbSettings::CurEncoder
).toString()));
380 RbSettings::setValue(RbSettings::Platform
, olddevice
);
382 EncBase
* enc
= EncBase::getEncoder(this,encoder
);
386 ui
.configEncstatus
->setText(tr("Configuration OK"));
387 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
391 ui
.configEncstatus
->setText(tr("Configuration INVALID"));
392 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
396 void Config::setNoProxy(bool checked
)
399 ui
.proxyPort
->setEnabled(i
);
400 ui
.proxyHost
->setEnabled(i
);
401 ui
.proxyUser
->setEnabled(i
);
402 ui
.proxyPass
->setEnabled(i
);
406 void Config::setSystemProxy(bool checked
)
409 ui
.proxyPort
->setEnabled(i
);
410 ui
.proxyHost
->setEnabled(i
);
411 ui
.proxyUser
->setEnabled(i
);
412 ui
.proxyPass
->setEnabled(i
);
414 // save values in input box
415 proxy
.setScheme("http");
416 proxy
.setUserName(ui
.proxyUser
->text());
417 proxy
.setPassword(ui
.proxyPass
->text());
418 proxy
.setHost(ui
.proxyHost
->text());
419 proxy
.setPort(ui
.proxyPort
->text().toInt());
420 // show system values in input box
421 QUrl envproxy
= Detect::systemProxy();
423 ui
.proxyHost
->setText(envproxy
.host());
425 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
426 ui
.proxyUser
->setText(envproxy
.userName());
427 ui
.proxyPass
->setText(envproxy
.password());
431 ui
.proxyHost
->setText(proxy
.host());
433 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
434 else ui
.proxyPort
->setText("");
435 ui
.proxyUser
->setText(proxy
.userName());
436 ui
.proxyPass
->setText(proxy
.password());
442 QStringList
Config::findLanguageFiles()
444 QDir
dir(programPath
);
445 QStringList fileNames
;
447 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
449 QDir
resDir(":/lang");
450 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
452 QRegExp
exp("^rbutil_(.*)\\.qm");
453 for(int i
= 0; i
< fileNames
.size(); i
++) {
454 QString a
= fileNames
.at(i
);
455 a
.replace(exp
, "\\1");
459 qDebug() << "Config::findLanguageFiles()" << langs
;
465 QString
Config::languageName(const QString
&qmFile
)
467 QTranslator translator
;
469 QString file
= "rbutil_" + qmFile
;
470 if(!translator
.load(file
, programPath
))
471 translator
.load(file
, ":/lang");
473 return translator
.translate("Configure", "English",
474 "This is the localized language name, i.e. your language.");
478 void Config::updateLanguage()
480 qDebug() << "updateLanguage()";
481 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
483 language
= lang
.value(a
.at(0)->text());
484 qDebug() << language
;
488 void Config::browseFolder()
490 browser
= new BrowseDirtree(this,tr("Select your device"));
491 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
492 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
493 #elif defined(Q_OS_WIN32)
494 browser
->setFilter(QDir::Drives
);
496 #if defined(Q_OS_MACX)
497 browser
->setRoot("/Volumes");
498 #elif defined(Q_OS_LINUX)
499 browser
->setDir("/media");
501 if( ui
.mountPoint
->text() != "" )
503 browser
->setDir(ui
.mountPoint
->text());
506 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
510 void Config::browseCache()
512 cbrowser
= new BrowseDirtree(this);
513 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
514 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
515 #elif defined(Q_OS_WIN32)
516 cbrowser
->setFilter(QDir::Drives
| QDir::AllDirs
| QDir::NoDotAndDotDot
);
518 cbrowser
->setDir(ui
.cachePath
->text());
519 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
524 void Config::setMountpoint(QString m
)
526 ui
.mountPoint
->setText(m
);
530 void Config::setCache(QString c
)
532 ui
.cachePath
->setText(c
);
537 void Config::autodetect()
539 Autodetection
detector(this);
540 // disable tree during detection as "working" feedback.
541 // TODO: replace the tree view with a splash screen during this time.
542 ui
.treeDevices
->setEnabled(false);
543 this->setCursor(Qt::WaitCursor
);
544 QCoreApplication::processEvents();
546 if(detector
.detect()) //let it detect
548 QString devicename
= detector
.getDevice();
549 // deexpand all items
550 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
551 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
552 //deselect the selected item(s)
553 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
554 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
557 // enumerate all platform items
558 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
559 for(int i
=0; i
< itmList
.size();i
++)
561 //enumerate device items
562 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
564 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
566 if(devicename
== data
) // item found
568 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
569 itmList
.at(i
)->setExpanded(true); //expand the platform item
570 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
577 if(!detector
.errdev().isEmpty()) {
579 if(detector
.errdev() == "sansae200")
580 text
= tr("Sansa e200 in MTP mode found!\n"
581 "You need to change your player to MSC mode for installation. ");
582 if(detector
.errdev() == "h10")
583 text
= tr("H10 20GB in MTP mode found!\n"
584 "You need to change your player to UMS mode for installation. ");
585 text
+= tr("Unless you changed this installation will fail!");
587 QMessageBox::critical(this, tr("Fatal error"), text
, QMessageBox::Ok
);
590 if(!detector
.incompatdev().isEmpty()) {
592 // we need to set the platform here to get the brand from the
594 RbSettings::setValue(RbSettings::Platform
, detector
.incompatdev());
595 text
= tr("Detected an unsupported %1 player variant. Sorry, "
596 "Rockbox doesn't run on your player.")
597 .arg(RbSettings::value(RbSettings::CurBrand
).toString());
599 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
600 text
, QMessageBox::Ok
);
604 if(detector
.getMountPoint() != "" )
606 ui
.mountPoint
->setText(QDir::toNativeSeparators(detector
.getMountPoint()));
610 QMessageBox::warning(this, tr("Autodetection"),
611 tr("Could not detect a Mountpoint.\n"
612 "Select your Mountpoint manually."),
613 QMessageBox::Ok
,QMessageBox::Ok
);
619 QMessageBox::warning(this, tr("Autodetection"),
620 tr("Could not detect a device.\n"
621 "Select your device and Mountpoint manually."),
622 QMessageBox::Ok
,QMessageBox::Ok
);
625 ui
.treeDevices
->setEnabled(true);
628 void Config::cacheClear()
630 if(QMessageBox::critical(this, tr("Really delete cache?"),
631 tr("Do you really want to delete the cache? "
632 "Make absolutely sure this setting is correct as it will "
633 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
634 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
637 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
638 if(!QFileInfo(cache
).isDir()) {
639 QMessageBox::critical(this, tr("Path wrong!"),
640 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
645 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
648 for(int i
= 0; i
< fn
.size(); i
++) {
649 QString f
= cache
+ fn
.at(i
);
651 qDebug() << "removed:" << f
;
653 updateCacheInfo(RbSettings::value(RbSettings::CachePath
).toString());
657 void Config::configTts()
659 int index
= ui
.comboTts
->currentIndex();
660 TTSBase
* tts
= TTSBase::getTTS(this,ui
.comboTts
->itemData(index
).toString());
662 EncTtsCfgGui
gui(this,tts
,TTSBase::getTTSName(RbSettings::value(RbSettings::Tts
).toString()));
664 updateTtsState(ui
.comboTts
->currentIndex());
668 void Config::configEnc()
670 // FIXME: this is a workaround to make the encoder follow the device selection
671 // even with the settings (and thus the device) being saved. Needs to be redone
672 // properly later by extending the settings object
673 if(ui
.treeDevices
->selectedItems().size() == 0)
676 QString devname
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
677 QString olddevice
= RbSettings::value(RbSettings::CurrentPlatform
).toString();
678 RbSettings::setValue(RbSettings::CurrentPlatform
,devname
);
679 QString encoder
= RbSettings::value(RbSettings::CurEncoder
).toString();
680 ui
.encoderName
->setText(EncBase::getEncoderName(RbSettings::value(RbSettings::CurEncoder
).toString()));
681 RbSettings::setValue(RbSettings::CurrentPlatform
,olddevice
);
684 EncBase
* enc
= EncBase::getEncoder(this,encoder
);
686 EncTtsCfgGui
gui(this,enc
,EncBase::getEncoderName(encoder
));