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] checking configuration";
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() << "[Config] setting proxy to:" << 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] aborted.";
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();
264 ui
.cacheSize
->setText(tr("Current cache size is %L1 kiB.")
269 void Config::setDevices()
272 // setup devices table
273 qDebug() << "[Config] setting up devices list";
275 QStringList platformList
= RbSettings::platforms();
277 QMap
<QString
, QString
> manuf
;
278 QMap
<QString
, QString
> devcs
;
279 for(int it
= 0; it
< platformList
.size(); it
++)
281 QString curname
= RbSettings::name(platformList
.at(it
));
282 QString curbrand
= RbSettings::brand(platformList
.at(it
));
283 manuf
.insertMulti(curbrand
, platformList
.at(it
));
284 devcs
.insert(platformList
.at(it
), curname
);
288 platform
= devcs
.value(RbSettings::value(RbSettings::Platform
).toString());
290 // set up devices table
291 ui
.treeDevices
->header()->hide();
292 ui
.treeDevices
->expandAll();
293 ui
.treeDevices
->setColumnCount(1);
294 QList
<QTreeWidgetItem
*> items
;
297 QStringList brands
= manuf
.uniqueKeys();
300 QTreeWidgetItem
*w3
= 0;
301 for(int c
= 0; c
< brands
.size(); c
++) {
302 w
= new QTreeWidgetItem();
303 w
->setFlags(Qt::ItemIsEnabled
);
304 w
->setText(0, brands
.at(c
));
307 // go through platforms again for sake of order
308 for(int it
= 0; it
< platformList
.size(); it
++) {
310 QString curname
= RbSettings::name(platformList
.at(it
));
311 QString curbrand
= RbSettings::brand(platformList
.at(it
));
313 if(curbrand
!= brands
.at(c
)) continue;
314 qDebug() << "[Config] add supported device:" << brands
.at(c
) << curname
;
315 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
316 w2
->setData(0, Qt::UserRole
, platformList
.at(it
));
318 if(platform
.contains(curname
)) {
319 w2
->setSelected(true);
320 w
->setExpanded(true);
321 w3
= w2
; // save pointer to hilight old selection
326 ui
.treeDevices
->insertTopLevelItems(0, items
);
328 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
336 QStringList ttslist
= TTSBase::getTTSList();
337 for(int a
= 0; a
< ttslist
.size(); a
++)
338 ui
.comboTts
->addItem(TTSBase::getTTSName(ttslist
.at(a
)), ttslist
.at(a
));
339 //update index of combobox
340 int index
= ui
.comboTts
->findData(RbSettings::value(RbSettings::Tts
).toString());
341 if(index
< 0) index
= 0;
342 ui
.comboTts
->setCurrentIndex(index
);
343 updateTtsState(index
);
348 void Config::updateTtsState(int index
)
350 QString ttsName
= ui
.comboTts
->itemData(index
).toString();
351 TTSBase
* tts
= TTSBase::getTTS(this,ttsName
);
355 ui
.configTTSstatus
->setText(tr("Configuration OK"));
356 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
360 ui
.configTTSstatus
->setText(tr("Configuration INVALID"));
361 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
365 void Config::updateEncState()
367 // FIXME: this is a workaround to make the encoder follow the device selection
368 // even with the settings (and thus the device) being saved. Needs to be redone
369 // properly later by extending the settings object
370 if(ui
.treeDevices
->selectedItems().size() == 0)
373 QString devname
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
374 QString olddevice
= RbSettings::value(RbSettings::Platform
).toString();
375 RbSettings::setValue(RbSettings::Platform
, devname
);
376 QString encoder
= RbSettings::value(RbSettings::CurEncoder
).toString();
377 ui
.encoderName
->setText(EncBase::getEncoderName(RbSettings::value(RbSettings::CurEncoder
).toString()));
378 RbSettings::setValue(RbSettings::Platform
, olddevice
);
380 EncBase
* enc
= EncBase::getEncoder(this,encoder
);
384 ui
.configEncstatus
->setText(tr("Configuration OK"));
385 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
389 ui
.configEncstatus
->setText(tr("Configuration INVALID"));
390 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
394 void Config::setNoProxy(bool checked
)
397 ui
.proxyPort
->setEnabled(i
);
398 ui
.proxyHost
->setEnabled(i
);
399 ui
.proxyUser
->setEnabled(i
);
400 ui
.proxyPass
->setEnabled(i
);
404 void Config::setSystemProxy(bool checked
)
407 ui
.proxyPort
->setEnabled(i
);
408 ui
.proxyHost
->setEnabled(i
);
409 ui
.proxyUser
->setEnabled(i
);
410 ui
.proxyPass
->setEnabled(i
);
412 // save values in input box
413 proxy
.setScheme("http");
414 proxy
.setUserName(ui
.proxyUser
->text());
415 proxy
.setPassword(ui
.proxyPass
->text());
416 proxy
.setHost(ui
.proxyHost
->text());
417 proxy
.setPort(ui
.proxyPort
->text().toInt());
418 // show system values in input box
419 QUrl envproxy
= Detect::systemProxy();
421 ui
.proxyHost
->setText(envproxy
.host());
423 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
424 ui
.proxyUser
->setText(envproxy
.userName());
425 ui
.proxyPass
->setText(envproxy
.password());
429 ui
.proxyHost
->setText(proxy
.host());
431 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
432 else ui
.proxyPort
->setText("");
433 ui
.proxyUser
->setText(proxy
.userName());
434 ui
.proxyPass
->setText(proxy
.password());
440 QStringList
Config::findLanguageFiles()
442 QDir
dir(programPath
);
443 QStringList fileNames
;
445 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
447 QDir
resDir(":/lang");
448 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
450 QRegExp
exp("^rbutil_(.*)\\.qm");
451 for(int i
= 0; i
< fileNames
.size(); i
++) {
452 QString a
= fileNames
.at(i
);
453 a
.replace(exp
, "\\1");
457 qDebug() << "[Config] available lang files:" << langs
;
463 QString
Config::languageName(const QString
&qmFile
)
465 QTranslator translator
;
467 QString file
= "rbutil_" + qmFile
;
468 if(!translator
.load(file
, programPath
))
469 translator
.load(file
, ":/lang");
471 return translator
.translate("Configure", "English",
472 "This is the localized language name, i.e. your language.");
476 void Config::updateLanguage()
478 qDebug() << "[Config] update selected language";
479 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
481 language
= lang
.value(a
.at(0)->text());
482 qDebug() << "[Config] new language:" << language
;
486 void Config::browseFolder()
488 browser
= new BrowseDirtree(this,tr("Select your device"));
489 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
490 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
491 #elif defined(Q_OS_WIN32)
492 browser
->setFilter(QDir::Drives
);
494 #if defined(Q_OS_MACX)
495 browser
->setRoot("/Volumes");
496 #elif defined(Q_OS_LINUX)
497 browser
->setDir("/media");
499 if( ui
.mountPoint
->text() != "" )
501 browser
->setDir(ui
.mountPoint
->text());
504 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
508 void Config::browseCache()
510 cbrowser
= new BrowseDirtree(this);
511 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
512 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
513 #elif defined(Q_OS_WIN32)
514 cbrowser
->setFilter(QDir::Drives
| QDir::AllDirs
| QDir::NoDotAndDotDot
);
516 cbrowser
->setDir(ui
.cachePath
->text());
517 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
522 void Config::setMountpoint(QString m
)
524 ui
.mountPoint
->setText(m
);
528 void Config::setCache(QString c
)
530 ui
.cachePath
->setText(c
);
535 void Config::autodetect()
537 Autodetection
detector(this);
538 // disable tree during detection as "working" feedback.
539 // TODO: replace the tree view with a splash screen during this time.
540 ui
.treeDevices
->setEnabled(false);
541 this->setCursor(Qt::WaitCursor
);
542 QCoreApplication::processEvents();
544 if(detector
.detect()) //let it detect
546 QString devicename
= detector
.getDevice();
547 // deexpand all items
548 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
549 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
550 //deselect the selected item(s)
551 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
552 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
555 // enumerate all platform items
556 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
557 for(int i
=0; i
< itmList
.size();i
++)
559 //enumerate device items
560 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
562 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
564 if(devicename
== data
) // item found
566 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
567 itmList
.at(i
)->setExpanded(true); //expand the platform item
568 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
575 if(!detector
.errdev().isEmpty()) {
577 if(detector
.errdev() == "sansae200")
578 text
= tr("Sansa e200 in MTP mode found!\n"
579 "You need to change your player to MSC mode for installation. ");
580 if(detector
.errdev() == "h10")
581 text
= tr("H10 20GB in MTP mode found!\n"
582 "You need to change your player to UMS mode for installation. ");
583 text
+= tr("Unless you changed this installation will fail!");
585 QMessageBox::critical(this, tr("Fatal error"), text
, QMessageBox::Ok
);
588 if(!detector
.incompatdev().isEmpty()) {
590 // we need to set the platform here to get the brand from the
592 RbSettings::setValue(RbSettings::Platform
, detector
.incompatdev());
593 text
= tr("Detected an unsupported %1 player variant. Sorry, "
594 "Rockbox doesn't run on your player.")
595 .arg(RbSettings::value(RbSettings::CurBrand
).toString());
597 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
598 text
, QMessageBox::Ok
);
602 if(detector
.getMountPoint() != "" )
604 ui
.mountPoint
->setText(QDir::toNativeSeparators(detector
.getMountPoint()));
608 QMessageBox::warning(this, tr("Autodetection"),
609 tr("Could not detect a Mountpoint.\n"
610 "Select your Mountpoint manually."),
611 QMessageBox::Ok
,QMessageBox::Ok
);
617 QMessageBox::warning(this, tr("Autodetection"),
618 tr("Could not detect a device.\n"
619 "Select your device and Mountpoint manually."),
620 QMessageBox::Ok
,QMessageBox::Ok
);
623 ui
.treeDevices
->setEnabled(true);
626 void Config::cacheClear()
628 if(QMessageBox::critical(this, tr("Really delete cache?"),
629 tr("Do you really want to delete the cache? "
630 "Make absolutely sure this setting is correct as it will "
631 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
632 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
635 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
636 if(!QFileInfo(cache
).isDir()) {
637 QMessageBox::critical(this, tr("Path wrong!"),
638 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
643 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
645 for(int i
= 0; i
< fn
.size(); i
++) {
646 QString f
= cache
+ fn
.at(i
);
649 updateCacheInfo(RbSettings::value(RbSettings::CachePath
).toString());
653 void Config::configTts()
655 int index
= ui
.comboTts
->currentIndex();
656 TTSBase
* tts
= TTSBase::getTTS(this,ui
.comboTts
->itemData(index
).toString());
658 EncTtsCfgGui
gui(this,tts
,TTSBase::getTTSName(RbSettings::value(RbSettings::Tts
).toString()));
660 updateTtsState(ui
.comboTts
->currentIndex());
664 void Config::configEnc()
666 // FIXME: this is a workaround to make the encoder follow the device selection
667 // even with the settings (and thus the device) being saved. Needs to be redone
668 // properly later by extending the settings object
669 if(ui
.treeDevices
->selectedItems().size() == 0)
672 QString devname
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
673 QString olddevice
= RbSettings::value(RbSettings::CurrentPlatform
).toString();
674 RbSettings::setValue(RbSettings::CurrentPlatform
,devname
);
675 QString encoder
= RbSettings::value(RbSettings::CurEncoder
).toString();
676 ui
.encoderName
->setText(EncBase::getEncoderName(RbSettings::value(RbSettings::CurEncoder
).toString()));
677 RbSettings::setValue(RbSettings::CurrentPlatform
,olddevice
);
680 EncBase
* enc
= EncBase::getEncoder(this,encoder
);
682 EncTtsCfgGui
gui(this,enc
,EncBase::getEncoderName(encoder
));