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 #if defined(Q_OS_WIN32)
39 #define DEFAULT_LANG "English (C)"
41 Config::Config(QWidget
*parent
,int index
) : QDialog(parent
)
43 programPath
= qApp
->applicationDirPath() + "/";
45 ui
.tabConfiguration
->setCurrentIndex(index
);
46 ui
.radioManualProxy
->setChecked(true);
47 QRegExpValidator
*proxyValidator
= new QRegExpValidator(this);
48 QRegExp
validate("[0-9]*");
49 proxyValidator
->setRegExp(validate
);
50 ui
.proxyPort
->setValidator(proxyValidator
);
51 #if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN32)
52 ui
.radioSystemProxy
->setEnabled(false); // not on macox for now
54 // build language list and sort alphabetically
55 QStringList langs
= findLanguageFiles();
56 for(int i
= 0; i
< langs
.size(); ++i
)
57 lang
.insert(languageName(langs
.at(i
)) + tr(" (%1)").arg(langs
.at(i
)), langs
.at(i
));
58 lang
.insert(DEFAULT_LANG
, "");
59 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
60 while (i
!= lang
.constEnd()) {
61 ui
.listLanguages
->addItem(i
.key());
64 ui
.listLanguages
->setSelectionMode(QAbstractItemView::SingleSelection
);
65 connect(ui
.listLanguages
, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
66 ui
.proxyPass
->setEchoMode(QLineEdit::Password
);
67 ui
.treeDevices
->setAlternatingRowColors(true);
68 ui
.listLanguages
->setAlternatingRowColors(true);
72 connect(ui
.buttonOk
, SIGNAL(clicked()), this, SLOT(accept()));
73 connect(ui
.buttonCancel
, SIGNAL(clicked()), this, SLOT(abort()));
74 connect(ui
.radioNoProxy
, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
75 connect(ui
.radioSystemProxy
, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
76 connect(ui
.browseMountPoint
, SIGNAL(clicked()), this, SLOT(browseFolder()));
77 connect(ui
.buttonAutodetect
,SIGNAL(clicked()),this,SLOT(autodetect()));
78 connect(ui
.buttonCacheBrowse
, SIGNAL(clicked()), this, SLOT(browseCache()));
79 connect(ui
.buttonCacheClear
, SIGNAL(clicked()), this, SLOT(cacheClear()));
80 connect(ui
.configTts
, SIGNAL(clicked()), this, SLOT(configTts()));
81 connect(ui
.configEncoder
, SIGNAL(clicked()), this, SLOT(configEnc()));
82 connect(ui
.comboTts
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
90 qDebug() << "Config::accept()";
91 // proxy: save entered proxy values, not displayed.
92 if(ui
.radioManualProxy
->isChecked()) {
93 proxy
.setScheme("http");
94 proxy
.setUserName(ui
.proxyUser
->text());
95 proxy
.setPassword(ui
.proxyPass
->text());
96 proxy
.setHost(ui
.proxyHost
->text());
97 proxy
.setPort(ui
.proxyPort
->text().toInt());
100 settings
->setProxy(proxy
.toString());
101 qDebug() << "new proxy:" << proxy
;
104 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
105 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
106 else proxyType
= "manual";
107 settings
->setProxyType(proxyType
);
110 if(settings
->curLang() != language
)
111 QMessageBox::information(this, tr("Language changed"),
112 tr("You need to restart the application for the changed language to take effect."));
113 settings
->setLang(language
);
116 QString mp
= ui
.mountPoint
->text();
117 if(QFileInfo(mp
).isDir())
118 settings
->setMountpoint(QDir::fromNativeSeparators(mp
));
122 if(ui
.treeDevices
->selectedItems().size() != 0) {
123 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
124 settings
->setCurPlatform(nplat
);
128 if(QFileInfo(ui
.cachePath
->text()).isDir())
129 settings
->setCachePath(ui
.cachePath
->text());
130 else // default to system temp path
131 settings
->setCachePath( QDir::tempPath());
132 settings
->setCacheDisable(ui
.cacheDisable
->isChecked());
133 settings
->setCacheOffline(ui
.cacheOfflineMode
->isChecked());
136 int i
= ui
.comboTts
->currentIndex();
137 settings
->setCurTTS(ui
.comboTts
->itemData(i
).toString());
142 emit
settingsUpdated();
148 qDebug() << "Config::abort()";
152 void Config::setSettings(RbSettings
* sett
)
160 void Config::setUserSettings()
163 proxy
= settings
->proxy();
166 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
167 else ui
.proxyPort
->setText("");
168 ui
.proxyHost
->setText(proxy
.host());
169 ui
.proxyUser
->setText(proxy
.userName());
170 ui
.proxyPass
->setText(proxy
.password());
172 QString proxyType
= settings
->proxyType();
173 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
174 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
175 else ui
.radioNoProxy
->setChecked(true);
177 // set language selection
178 QList
<QListWidgetItem
*> a
;
180 // find key for lang value
181 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
182 while (i
!= lang
.constEnd()) {
183 if(i
.value() == settings
->curLang()) {
189 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
191 a
= ui
.listLanguages
->findItems(DEFAULT_LANG
, Qt::MatchExactly
);
193 ui
.listLanguages
->setCurrentItem(a
.at(0));
196 ui
.mountPoint
->setText(QDir::toNativeSeparators(settings
->mountpoint()));
199 if(!QFileInfo(settings
->cachePath()).isDir())
200 settings
->setCachePath(QDir::tempPath());
201 ui
.cachePath
->setText(settings
->cachePath());
202 ui
.cacheDisable
->setChecked(settings
->cacheDisabled());
203 ui
.cacheOfflineMode
->setChecked(settings
->cacheOffline());
204 updateCacheInfo(settings
->cachePath());
208 void Config::updateCacheInfo(QString path
)
211 fs
= QDir(path
+ "/rbutil-cache/").entryInfoList(QDir::Files
| QDir::NoDotAndDotDot
);
213 for(int i
= 0; i
< fs
.size(); i
++) {
214 sz
+= fs
.at(i
).size();
215 qDebug() << fs
.at(i
).fileName() << fs
.at(i
).size();
217 ui
.cacheSize
->setText(tr("Current cache size is %L1 kiB.")
222 void Config::setDevices()
225 // setup devices table
226 qDebug() << "Config::setDevices()";
228 QStringList platformList
= settings
->allPlatforms();
230 QMap
<QString
, QString
> manuf
;
231 QMap
<QString
, QString
> devcs
;
232 for(int it
= 0; it
< platformList
.size(); it
++)
234 QString curname
= settings
->name(platformList
.at(it
));
235 QString curbrand
= settings
->brand(platformList
.at(it
));
236 manuf
.insertMulti(curbrand
, platformList
.at(it
));
237 devcs
.insert(platformList
.at(it
), curname
);
241 platform
= devcs
.value(settings
->curPlatform());
243 // set up devices table
244 ui
.treeDevices
->header()->hide();
245 ui
.treeDevices
->expandAll();
246 ui
.treeDevices
->setColumnCount(1);
247 QList
<QTreeWidgetItem
*> items
;
250 QStringList brands
= manuf
.uniqueKeys();
253 QTreeWidgetItem
*w3
= 0;
254 for(int c
= 0; c
< brands
.size(); c
++) {
255 qDebug() << brands
.at(c
);
256 w
= new QTreeWidgetItem();
257 w
->setFlags(Qt::ItemIsEnabled
);
258 w
->setText(0, brands
.at(c
));
261 // go through platforms again for sake of order
262 for(int it
= 0; it
< platformList
.size(); it
++) {
264 QString curname
= settings
->name(platformList
.at(it
));
265 QString curbrand
= settings
->brand(platformList
.at(it
));
267 if(curbrand
!= brands
.at(c
)) continue;
268 qDebug() << "adding:" << brands
.at(c
) << curname
;
269 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
270 w2
->setData(0, Qt::UserRole
, platformList
.at(it
));
272 if(platform
.contains(curname
)) {
273 w2
->setSelected(true);
274 w
->setExpanded(true);
275 w3
= w2
; // save pointer to hilight old selection
280 ui
.treeDevices
->insertTopLevelItems(0, items
);
282 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
290 QStringList ttslist
= TTSBase::getTTSList();
291 for(int a
= 0; a
< ttslist
.size(); a
++)
292 ui
.comboTts
->addItem(TTSBase::getTTSName(ttslist
.at(a
)), ttslist
.at(a
));
293 //update index of combobox
294 int index
= ui
.comboTts
->findData(settings
->curTTS());
295 if(index
< 0) index
= 0;
296 ui
.comboTts
->setCurrentIndex(index
);
297 updateTtsState(index
);
302 void Config::updateTtsState(int index
)
304 QString ttsName
= ui
.comboTts
->itemData(index
).toString();
305 TTSBase
* tts
= TTSBase::getTTS(ttsName
);
306 tts
->setCfg(settings
);
310 ui
.configTTSstatus
->setText("Configuration OK");
311 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
315 ui
.configTTSstatus
->setText("Configuration INVALID");
316 ui
.configTTSstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
320 void Config::updateEncState()
322 ui
.encoderName
->setText(EncBase::getEncoderName(settings
->curEncoder()));
323 QString encoder
= settings
->curEncoder();
324 EncBase
* enc
= EncBase::getEncoder(encoder
);
325 enc
->setCfg(settings
);
329 ui
.configEncstatus
->setText("Configuration OK");
330 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
334 ui
.configEncstatus
->setText("Configuration INVALID");
335 ui
.configEncstatusimg
->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
339 void Config::setNoProxy(bool checked
)
342 ui
.proxyPort
->setEnabled(i
);
343 ui
.proxyHost
->setEnabled(i
);
344 ui
.proxyUser
->setEnabled(i
);
345 ui
.proxyPass
->setEnabled(i
);
349 void Config::setSystemProxy(bool checked
)
352 ui
.proxyPort
->setEnabled(i
);
353 ui
.proxyHost
->setEnabled(i
);
354 ui
.proxyUser
->setEnabled(i
);
355 ui
.proxyPass
->setEnabled(i
);
357 // save values in input box
358 proxy
.setScheme("http");
359 proxy
.setUserName(ui
.proxyUser
->text());
360 proxy
.setPassword(ui
.proxyPass
->text());
361 proxy
.setHost(ui
.proxyHost
->text());
362 proxy
.setPort(ui
.proxyPort
->text().toInt());
363 // show system values in input box
364 QUrl envproxy
= systemProxy();
366 ui
.proxyHost
->setText(envproxy
.host());
368 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
369 ui
.proxyUser
->setText(envproxy
.userName());
370 ui
.proxyPass
->setText(envproxy
.password());
374 ui
.proxyHost
->setText(proxy
.host());
376 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
377 else ui
.proxyPort
->setText("");
378 ui
.proxyUser
->setText(proxy
.userName());
379 ui
.proxyPass
->setText(proxy
.password());
385 QStringList
Config::findLanguageFiles()
387 QDir
dir(programPath
);
388 QStringList fileNames
;
390 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
392 QDir
resDir(":/lang");
393 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
395 QRegExp
exp("^rbutil_(.*)\\.qm");
396 for(int i
= 0; i
< fileNames
.size(); i
++) {
397 QString a
= fileNames
.at(i
);
398 a
.replace(exp
, "\\1");
402 qDebug() << "Config::findLanguageFiles()" << langs
;
408 QString
Config::languageName(const QString
&qmFile
)
410 QTranslator translator
;
412 QString file
= "rbutil_" + qmFile
;
413 if(!translator
.load(file
, programPath
))
414 translator
.load(file
, ":/lang");
416 return translator
.translate("Configure", "English");
420 void Config::updateLanguage()
422 qDebug() << "updateLanguage()";
423 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
425 language
= lang
.value(a
.at(0)->text());
426 qDebug() << language
;
430 void Config::browseFolder()
432 browser
= new BrowseDirtree(this,tr("Select your device"));
433 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
434 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
435 #elif defined(Q_OS_WIN32)
436 browser
->setFilter(QDir::Drives
);
438 #if defined(Q_OS_MACX)
439 browser
->setRoot("/Volumes");
440 #elif defined(Q_OS_LINUX)
441 browser
->setDir("/media");
443 if( ui
.mountPoint
->text() != "" )
445 browser
->setDir(ui
.mountPoint
->text());
448 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
452 void Config::browseCache()
454 cbrowser
= new BrowseDirtree(this);
455 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
456 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
457 #elif defined(Q_OS_WIN32)
458 cbrowser
->setFilter(QDir::Drives
);
460 cbrowser
->setDir(ui
.cachePath
->text());
461 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
466 void Config::setMountpoint(QString m
)
468 ui
.mountPoint
->setText(m
);
472 void Config::setCache(QString c
)
474 ui
.cachePath
->setText(c
);
479 void Config::autodetect()
481 Autodetection
detector(this);
482 detector
.setSettings(settings
);
484 if(detector
.detect()) //let it detect
486 QString devicename
= detector
.getDevice();
487 // deexpand all items
488 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
489 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
490 //deselect the selected item(s)
491 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
492 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
495 // enumerate all platform items
496 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
497 for(int i
=0; i
< itmList
.size();i
++)
499 //enumerate device items
500 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
502 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
504 if(devicename
== data
) // item found
506 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
507 itmList
.at(i
)->setExpanded(true); //expand the platform item
508 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
514 if(!detector
.errdev().isEmpty()) {
516 if(detector
.errdev() == "sansae200")
517 text
= tr("Sansa e200 in MTP mode found!\n"
518 "You need to change your player to MSC mode for installation. ");
519 if(detector
.errdev() == "h10")
520 text
= tr("H10 20GB in MTP mode found!\n"
521 "You need to change your player to UMS mode for installation. ");
522 text
+= tr("Unless you changed this installation will fail!");
524 QMessageBox::critical(this, tr("Fatal error"), text
, QMessageBox::Ok
);
527 if(!detector
.incompatdev().isEmpty()) {
529 // we need to set the platform here to get the brand from the
531 settings
->setCurPlatform(detector
.incompatdev());
532 text
= tr("Detected an unsupported %1 player variant. Sorry, "
533 "Rockbox doesn't run on your player.").arg(settings
->curBrand());
535 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
536 text
, QMessageBox::Ok
);
540 if(detector
.getMountPoint() != "" )
542 ui
.mountPoint
->setText(QDir::toNativeSeparators(detector
.getMountPoint()));
546 QMessageBox::warning(this, tr("Autodetection"),
547 tr("Could not detect a Mountpoint.\n"
548 "Select your Mountpoint manually."),
549 QMessageBox::Ok
,QMessageBox::Ok
);
554 QMessageBox::warning(this, tr("Autodetection"),
555 tr("Could not detect a device.\n"
556 "Select your device and Mountpoint manually."),
557 QMessageBox::Ok
,QMessageBox::Ok
);
562 void Config::cacheClear()
564 if(QMessageBox::critical(this, tr("Really delete cache?"),
565 tr("Do you really want to delete the cache? "
566 "Make absolutely sure this setting is correct as it will "
567 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
568 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
571 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
572 if(!QFileInfo(cache
).isDir()) {
573 QMessageBox::critical(this, tr("Path wrong!"),
574 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
579 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
582 for(int i
= 0; i
< fn
.size(); i
++) {
583 QString f
= cache
+ fn
.at(i
);
585 qDebug() << "removed:" << f
;
587 updateCacheInfo(settings
->cachePath());
591 void Config::configTts()
593 int index
= ui
.comboTts
->currentIndex();
594 TTSBase
* tts
= TTSBase::getTTS(ui
.comboTts
->itemData(index
).toString());
596 tts
->setCfg(settings
);
598 updateTtsState(ui
.comboTts
->currentIndex());
602 void Config::configEnc()
604 EncBase
* enc
= EncBase::getEncoder(settings
->curEncoder());
606 enc
->setCfg(settings
);