Fix advanced EQ menu
[maemo-rb.git] / rbutil / rbutilqt / configure.cpp
blob075a4876b9ce5f9f8965d9a177eb5ab9982712eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <QMessageBox>
20 #include <QProgressDialog>
21 #include <QFileDialog>
22 #include <QUrl>
24 #include "version.h"
25 #include "configure.h"
26 #include "autodetection.h"
27 #include "ui_configurefrm.h"
28 #include "encoderbase.h"
29 #include "ttsbase.h"
30 #include "system.h"
31 #include "encttscfggui.h"
32 #include "rbsettings.h"
33 #include "serverinfo.h"
34 #include "systeminfo.h"
35 #include "utils.h"
36 #include "comboboxviewdelegate.h"
37 #if defined(Q_OS_WIN32)
38 #if defined(UNICODE)
39 #define _UNICODE
40 #endif
41 #include <tchar.h>
42 #include <windows.h>
43 #endif
44 #include "rbutilqt.h"
46 #include "systrace.h"
48 #define DEFAULT_LANG "English (en)"
49 #define DEFAULT_LANG_CODE "en"
51 Config::Config(QWidget *parent,int index) : QDialog(parent)
53 programPath = qApp->applicationDirPath() + "/";
54 ui.setupUi(this);
55 ui.tabConfiguration->setCurrentIndex(index);
56 ui.radioManualProxy->setChecked(true);
57 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
58 QRegExp validate("[0-9]*");
59 proxyValidator->setRegExp(validate);
60 ui.proxyPort->setValidator(proxyValidator);
62 // build language list and sort alphabetically
63 QStringList langs = findLanguageFiles();
64 for(int i = 0; i < langs.size(); ++i)
65 lang.insert(languageName(langs.at(i))
66 + QString(" (%1)").arg(langs.at(i)), langs.at(i));
67 lang.insert(DEFAULT_LANG, DEFAULT_LANG_CODE);
68 QMap<QString, QString>::const_iterator i = lang.constBegin();
69 while (i != lang.constEnd()) {
70 ui.listLanguages->addItem(i.key());
71 i++;
74 ComboBoxViewDelegate *delegate = new ComboBoxViewDelegate(this);
75 ui.mountPoint->setItemDelegate(delegate);
76 #if !defined(DBG)
77 ui.mountPoint->setEditable(false);
78 #endif
80 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
81 ui.proxyPass->setEchoMode(QLineEdit::Password);
82 ui.treeDevices->setAlternatingRowColors(true);
83 ui.listLanguages->setAlternatingRowColors(true);
85 /* Explicitly set some widgets to have left-to-right layout */
86 ui.treeDevices->setLayoutDirection(Qt::LeftToRight);
87 ui.mountPoint->setLayoutDirection(Qt::LeftToRight);
88 ui.proxyHost->setLayoutDirection(Qt::LeftToRight);
89 ui.proxyPort->setLayoutDirection(Qt::LeftToRight);
90 ui.proxyUser->setLayoutDirection(Qt::LeftToRight);
91 ui.proxyPass->setLayoutDirection(Qt::LeftToRight);
92 ui.listLanguages->setLayoutDirection(Qt::LeftToRight);
93 ui.cachePath->setLayoutDirection(Qt::LeftToRight);
94 ui.comboTts->setLayoutDirection(Qt::LeftToRight);
96 this->setModal(true);
98 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
99 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
100 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
101 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
102 connect(ui.refreshMountPoint, SIGNAL(clicked()), this, SLOT(refreshMountpoint()));
103 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
104 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
105 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
106 connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
107 connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
108 connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
109 connect(ui.treeDevices, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
110 connect(ui.testTTS,SIGNAL(clicked()),this,SLOT(testTts()));
111 connect(ui.showDisabled, SIGNAL(toggled(bool)), this, SLOT(showDisabled(bool)));
112 connect(ui.mountPoint, SIGNAL(editTextChanged(QString)), this, SLOT(updateMountpoint(QString)));
113 connect(ui.mountPoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMountpoint(int)));
114 // delete this dialog after it finished automatically.
115 connect(this, SIGNAL(finished(int)), this, SLOT(deleteLater()));
117 setUserSettings();
118 setDevices();
122 void Config::accept()
124 qDebug() << "[Config] checking configuration";
125 QString errormsg = tr("The following errors occurred:") + "<ul>";
126 bool error = false;
128 // proxy: save entered proxy values, not displayed.
129 if(ui.radioManualProxy->isChecked()) {
130 proxy.setScheme("http");
131 proxy.setUserName(ui.proxyUser->text());
132 proxy.setPassword(ui.proxyPass->text());
133 proxy.setHost(ui.proxyHost->text());
134 proxy.setPort(ui.proxyPort->text().toInt());
137 // QUrl::toEncoded() doesn't encode a colon in the password correctly,
138 // which will result in errors during parsing the string.
139 // QUrl::toPercentEncoding() does work as expected, so build the string to
140 // store in the configuration file manually.
141 QString proxystring = "http://"
142 + QString(QUrl::toPercentEncoding(proxy.userName())) + ":"
143 + QString(QUrl::toPercentEncoding(proxy.password())) + "@"
144 + proxy.host() + ":"
145 + QString::number(proxy.port());
146 RbSettings::setValue(RbSettings::Proxy, proxystring);
147 qDebug() << "[Config] setting proxy to:" << proxy;
148 // proxy type
149 QString proxyType;
150 if(ui.radioNoProxy->isChecked()) proxyType = "none";
151 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
152 else proxyType = "manual";
153 RbSettings::setValue(RbSettings::ProxyType, proxyType);
155 RbSettings::setValue(RbSettings::Language, language);
157 // make sure mountpoint is read from dropdown box
158 if(mountpoint.isEmpty()) {
159 updateMountpoint(ui.mountPoint->currentIndex());
162 // mountpoint
163 if(mountpoint.isEmpty()) {
164 errormsg += "<li>" + tr("No mountpoint given") + "</li>";
165 error = true;
167 else if(!QFileInfo(mountpoint).exists()) {
168 errormsg += "<li>" + tr("Mountpoint does not exist") + "</li>";
169 error = true;
171 else if(!QFileInfo(mountpoint).isDir()) {
172 errormsg += "<li>" + tr("Mountpoint is not a directory.") + "</li>";
173 error = true;
175 else if(!QFileInfo(mountpoint).isWritable()) {
176 errormsg += "<li>" + tr("Mountpoint is not writeable") + "</li>";
177 error = true;
179 else {
180 RbSettings::setValue(RbSettings::Mountpoint,
181 QDir::fromNativeSeparators(mountpoint));
184 // platform
185 QString nplat;
186 if(ui.treeDevices->selectedItems().size() != 0) {
187 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
188 RbSettings::setValue(RbSettings::Platform, nplat);
190 else {
191 errormsg += "<li>" + tr("No player selected") + "</li>";
192 error = true;
195 // cache settings
196 if(QFileInfo(ui.cachePath->text()).isDir()) {
197 if(!QFileInfo(ui.cachePath->text()).isWritable()) {
198 errormsg += "<li>" + tr("Cache path not writeable. Leave path empty "
199 "to default to systems temporary path.") + "</li>";
200 error = true;
202 else
203 RbSettings::setValue(RbSettings::CachePath, ui.cachePath->text());
205 else // default to system temp path
206 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
207 RbSettings::setValue(RbSettings::CacheDisabled, ui.cacheDisable->isChecked());
209 // tts settings
210 RbSettings::setValue(RbSettings::UseTtsCorrections, ui.ttsCorrections->isChecked());
211 int i = ui.comboTts->currentIndex();
212 RbSettings::setValue(RbSettings::Tts, ui.comboTts->itemData(i).toString());
214 RbSettings::setValue(RbSettings::RbutilVersion, PUREVERSION);
216 errormsg += "</ul>";
217 errormsg += tr("You need to fix the above errors before you can continue.");
219 if(error) {
220 QMessageBox::critical(this, tr("Configuration error"), errormsg);
222 else {
223 // sync settings
224 RbSettings::sync();
225 this->close();
226 emit settingsUpdated();
231 void Config::abort()
233 qDebug() << "[Config] aborted.";
234 this->close();
238 void Config::setUserSettings()
240 // set proxy
241 proxy.setEncodedUrl(RbSettings::value(RbSettings::Proxy).toByteArray());
243 if(proxy.port() > 0)
244 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
245 else ui.proxyPort->setText("");
246 ui.proxyHost->setText(proxy.host());
247 ui.proxyUser->setText(proxy.userName());
248 ui.proxyPass->setText(proxy.password());
250 QString proxyType = RbSettings::value(RbSettings::ProxyType).toString();
251 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
252 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
253 else ui.radioNoProxy->setChecked(true);
255 // set language selection
256 QList<QListWidgetItem*> a;
257 QString b;
258 // find key for lang value
259 QMap<QString, QString>::const_iterator i = lang.constBegin();
260 QString l = RbSettings::value(RbSettings::Language).toString();
261 if(l.isEmpty())
262 l = QLocale::system().name();
263 while (i != lang.constEnd()) {
264 if(i.value() == l) {
265 b = i.key();
266 break;
268 else if(l.startsWith(i.value(), Qt::CaseInsensitive)) {
269 // check if there is a base language (en -> en_US, etc.)
270 b = i.key();
271 break;
273 i++;
275 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
276 if(a.size() > 0)
277 ui.listLanguages->setCurrentItem(a.at(0));
278 // don't connect before language list has been set up to prevent
279 // triggering the signal by selecting the saved language.
280 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
282 // devices tab
283 refreshMountpoint();
284 mountpoint = QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint).toString());
285 setMountpoint(mountpoint);
287 // cache tab
288 if(!QFileInfo(RbSettings::value(RbSettings::CachePath).toString()).isDir())
289 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
290 ui.cachePath->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath).toString()));
291 ui.cacheDisable->setChecked(RbSettings::value(RbSettings::CacheDisabled).toBool());
292 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
294 // TTS tab
295 ui.ttsCorrections->setChecked(RbSettings::value(RbSettings::UseTtsCorrections).toBool());
299 void Config::updateCacheInfo(QString path)
301 QList<QFileInfo> fs;
302 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
303 qint64 sz = 0;
304 for(int i = 0; i < fs.size(); i++) {
305 sz += fs.at(i).size();
307 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
308 .arg(sz/1024));
312 void Config::showDisabled(bool show)
314 qDebug() << "[Config] disabled targets shown:" << show;
315 if(show)
316 QMessageBox::warning(this, tr("Showing disabled targets"),
317 tr("You just enabled showing targets that are marked disabled. "
318 "Disabled targets are not recommended to end users. Please "
319 "use this option only if you know what you are doing."));
320 setDevices();
325 void Config::setDevices()
328 // setup devices table
329 qDebug() << "[Config] setting up devices list";
331 QStringList platformList;
332 if(ui.showDisabled->isChecked())
333 platformList = SystemInfo::platforms(SystemInfo::PlatformAllDisabled);
334 else
335 platformList = SystemInfo::platforms(SystemInfo::PlatformAll);
337 QMap <QString, QString> manuf;
338 for(int it = 0; it < platformList.size(); it++)
340 QString curbrand = SystemInfo::platformValue(platformList.at(it),
341 SystemInfo::CurBrand).toString();
342 manuf.insertMulti(curbrand, platformList.at(it));
345 // set up devices table
346 ui.treeDevices->header()->hide();
347 ui.treeDevices->expandAll();
348 ui.treeDevices->setColumnCount(1);
349 QList<QTreeWidgetItem *> items;
351 // get manufacturers
352 QStringList brands = manuf.uniqueKeys();
353 QTreeWidgetItem *w;
354 QTreeWidgetItem *w2;
355 QTreeWidgetItem *w3 = 0;
357 QString selected = RbSettings::value(RbSettings::Platform).toString();
358 for(int c = 0; c < brands.size(); c++) {
359 w = new QTreeWidgetItem();
360 w->setFlags(Qt::ItemIsEnabled);
361 w->setText(0, brands.at(c));
362 items.append(w);
363 // go through platforms and add all players matching the current brand
364 for(int it = 0; it < platformList.size(); it++) {
365 // skip if not current brand
366 if(!manuf.values(brands.at(c)).contains(platformList.at(it)))
367 continue;
368 // construct display name
369 QString curname = SystemInfo::platformValue(platformList.at(it),
370 SystemInfo::CurName).toString() +
371 " (" +ServerInfo::platformValue(platformList.at(it),
372 ServerInfo::CurStatus).toString() +")";
373 qDebug() << "[Config] add supported device:" << brands.at(c) << curname;
374 w2 = new QTreeWidgetItem(w, QStringList(curname));
375 w2->setData(0, Qt::UserRole, platformList.at(it));
377 if(platformList.at(it) == selected) {
378 w2->setSelected(true);
379 w->setExpanded(true);
380 w3 = w2; // save pointer to hilight old selection
382 items.append(w2);
385 // remove any old items in list
386 QTreeWidgetItem* widgetitem;
387 do {
388 widgetitem = ui.treeDevices->takeTopLevelItem(0);
389 delete widgetitem;
391 while(widgetitem);
392 // add new items
393 ui.treeDevices->insertTopLevelItems(0, items);
394 if(w3 != 0) {
395 ui.treeDevices->setCurrentItem(w3); // hilight old selection
396 ui.treeDevices->scrollToItem(w3);
399 // tts / encoder tab
401 //encoders
402 updateEncState();
404 //tts
405 QStringList ttslist = TTSBase::getTTSList();
406 for(int a = 0; a < ttslist.size(); a++)
407 ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
408 //update index of combobox
409 int index = ui.comboTts->findData(RbSettings::value(RbSettings::Tts).toString());
410 if(index < 0) index = 0;
411 ui.comboTts->setCurrentIndex(index);
412 updateTtsState(index);
417 void Config::updateTtsState(int index)
419 QString ttsName = ui.comboTts->itemData(index).toString();
420 TTSBase* tts = TTSBase::getTTS(this,ttsName);
422 if(!tts)
424 QMessageBox::critical(this, tr("TTS error"),
425 tr("The selected TTS failed to initialize. You can't use this TTS."));
426 return;
429 if(tts->configOk())
431 ui.configTTSstatus->setText(tr("Configuration OK"));
432 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
433 ui.testTTS->setEnabled(true);
435 else
437 ui.configTTSstatus->setText(tr("Configuration INVALID"));
438 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
439 ui.testTTS->setEnabled(false);
442 delete tts; /* Config objects are never deleted (in fact, they are leaked..), so we can't rely on QObject,
443 since that would delete the TTSBase instance on application exit*/
446 void Config::updateEncState()
448 if(ui.treeDevices->selectedItems().size() == 0)
449 return;
451 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
452 QString encoder = SystemInfo::platformValue(devname,
453 SystemInfo::CurEncoder).toString();
454 ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(devname,
455 SystemInfo::CurEncoder).toString()));
457 EncoderBase* enc = EncoderBase::getEncoder(this,encoder);
459 if(enc->configOk())
461 ui.configEncstatus->setText(tr("Configuration OK"));
462 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
464 else
466 ui.configEncstatus->setText(tr("Configuration INVALID"));
467 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
472 void Config::setNoProxy(bool checked)
474 bool i = !checked;
475 ui.proxyPort->setEnabled(i);
476 ui.proxyHost->setEnabled(i);
477 ui.proxyUser->setEnabled(i);
478 ui.proxyPass->setEnabled(i);
482 void Config::setSystemProxy(bool checked)
484 ui.proxyPort->setEnabled(!checked);
485 ui.proxyHost->setEnabled(!checked);
486 ui.proxyUser->setEnabled(!checked);
487 ui.proxyPass->setEnabled(!checked);
488 if(checked) {
489 // save values in input box
490 proxy.setScheme("http");
491 proxy.setUserName(ui.proxyUser->text());
492 proxy.setPassword(ui.proxyPass->text());
493 proxy.setHost(ui.proxyHost->text());
494 proxy.setPort(ui.proxyPort->text().toInt());
495 // show system values in input box
496 QUrl envproxy = System::systemProxy();
497 qDebug() << "[Config] setting system proxy" << envproxy;
499 ui.proxyHost->setText(envproxy.host());
500 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
501 ui.proxyUser->setText(envproxy.userName());
502 ui.proxyPass->setText(envproxy.password());
504 if(envproxy.host().isEmpty() || envproxy.port() == -1) {
505 qDebug() << "[Config] sytem proxy is invalid.";
506 QMessageBox::warning(this, tr("Proxy Detection"),
507 tr("The System Proxy settings are invalid!\n"
508 "Rockbox Utility can't work with this proxy settings. "
509 "Make sure the system proxy is set correctly. Note that "
510 "\"proxy auto-config (PAC)\" scripts are not supported by "
511 "Rockbox Utility. If your system uses this you need "
512 "to use manual proxy settings."),
513 QMessageBox::Ok ,QMessageBox::Ok);
514 // the current proxy settings are invalid. Check the saved proxy
515 // type again.
516 if(RbSettings::value(RbSettings::ProxyType).toString() == "manual")
517 ui.radioManualProxy->setChecked(true);
518 else
519 ui.radioNoProxy->setChecked(true);
523 else {
524 ui.proxyHost->setText(proxy.host());
525 if(proxy.port() > 0)
526 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
527 else ui.proxyPort->setText("");
528 ui.proxyUser->setText(proxy.userName());
529 ui.proxyPass->setText(proxy.password());
535 QStringList Config::findLanguageFiles()
537 QDir dir(programPath);
538 QStringList fileNames;
539 QStringList langs;
540 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
542 QDir resDir(":/lang");
543 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
545 QRegExp exp("^rbutil_(.*)\\.qm");
546 for(int i = 0; i < fileNames.size(); i++) {
547 QString a = fileNames.at(i);
548 a.replace(exp, "\\1");
549 langs.append(a);
551 langs.sort();
552 qDebug() << "[Config] available lang files:" << langs;
554 return langs;
558 QString Config::languageName(const QString &qmFile)
560 QTranslator translator;
562 QString file = "rbutil_" + qmFile;
563 if(!translator.load(file, programPath))
564 translator.load(file, ":/lang");
566 return translator.translate("Configure", "English",
567 "This is the localized language name, i.e. your language.");
571 void Config::updateLanguage()
573 qDebug() << "[Config] update selected language";
575 // remove all old translators
576 for(int i = 0; i < RbUtilQt::translators.size(); ++i) {
577 qApp->removeTranslator(RbUtilQt::translators.at(i));
578 // do not delete old translators, this confuses Qt.
580 RbUtilQt::translators.clear();
581 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
582 if(a.size() > 0)
583 language = lang.value(a.at(0)->text());
584 qDebug() << "[Config] new language:" << language;
586 QString applang = QLocale::system().name();
587 QTranslator *translator = new QTranslator(qApp);
588 QTranslator *qttrans = new QTranslator(qApp);
589 QString absolutePath = QCoreApplication::instance()->applicationDirPath();
591 if(!translator->load("rbutil_" + language, absolutePath))
592 translator->load("rbutil_" + language, ":/lang");
593 if(!qttrans->load("qt_" + language,
594 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
595 qttrans->load("qt_" + language, ":/lang");
597 qApp->installTranslator(translator);
598 qApp->installTranslator(qttrans);
599 //: This string is used to indicate the writing direction. Translate it
600 //: to "RTL" (without quotes) for RTL languages. Anything else will get
601 //: treated as LTR language.
602 if(QObject::tr("LTR") == "RTL")
603 qApp->setLayoutDirection(Qt::RightToLeft);
604 else
605 qApp->setLayoutDirection(Qt::LeftToRight);
607 RbUtilQt::translators.append(translator);
608 RbUtilQt::translators.append(qttrans);
610 QLocale::setDefault(language);
615 void Config::browseCache()
617 QString old = ui.cachePath->text();
618 if(!QFileInfo(old).isDir())
619 old = QDir::tempPath();
620 QString c = QFileDialog::getExistingDirectory(this, tr("Set Cache Path"), old);
621 if(c.isEmpty())
622 c = old;
623 else if(!QFileInfo(c).isDir())
624 c = QDir::tempPath();
625 ui.cachePath->setText(QDir::toNativeSeparators(c));
626 updateCacheInfo(c);
630 void Config::refreshMountpoint()
632 // avoid QComboBox to send signals during rebuild to avoid changing to an
633 // unwanted item.
634 ui.mountPoint->blockSignals(true);
635 ui.mountPoint->clear();
636 QStringList mps = Utils::mountpoints();
637 for(int i = 0; i < mps.size(); ++i) {
638 // add mountpoint as user data so we can change the displayed string
639 // later (to include volume label or similar)
640 // Skip unwritable mountpoints, they are not useable for us.
641 if(QFileInfo(mps.at(i)).isWritable()) {
642 QString description = QString("%1 (%2 GiB of %3 GiB free)")
643 .arg(Utils::filesystemName(mps.at(i)))
644 .arg((double)Utils::filesystemFree(mps.at(i))/(1<<30), 0, 'f', 2)
645 .arg((double)Utils::filesystemTotal(mps.at(i))/(1<<30), 0, 'f', 2);
646 ui.mountPoint->addItem(QDir::toNativeSeparators(mps.at(i)), description);
649 if(!mountpoint.isEmpty()) {
650 setMountpoint(mountpoint);
652 ui.mountPoint->blockSignals(false);
656 void Config::updateMountpoint(QString m)
658 if(!m.isEmpty()) {
659 mountpoint = QDir::fromNativeSeparators(m);
660 qDebug() << "[Config] Mountpoint set to" << mountpoint;
665 void Config::updateMountpoint(int idx)
667 if(idx == -1) {
668 return;
670 QString mp = ui.mountPoint->itemText(idx);
671 if(!mp.isEmpty()) {
672 mountpoint = QDir::fromNativeSeparators(mp);
673 qDebug() << "[Config] Mountpoint set to" << mountpoint;
678 void Config::setMountpoint(QString m)
680 if(m.isEmpty()) {
681 return;
683 int index = ui.mountPoint->findText(QDir::toNativeSeparators(m));
684 if(index != -1) {
685 ui.mountPoint->setCurrentIndex(index);
687 else {
688 // keep a mountpoint that is not in the list for convenience (to allow
689 // easier development)
690 ui.mountPoint->addItem(QDir::toNativeSeparators(m));
691 ui.mountPoint->setCurrentIndex(ui.mountPoint->findText(m));
693 qDebug() << "[Config] Mountpoint set to" << mountpoint;
697 void Config::autodetect()
699 Autodetection detector(this);
700 // disable tree during detection as "working" feedback.
701 // TODO: replace the tree view with a splash screen during this time.
702 ui.treeDevices->setEnabled(false);
703 this->setCursor(Qt::WaitCursor);
704 QCoreApplication::processEvents();
706 if(detector.detect()) //let it detect
708 QString devicename = detector.getDevice();
709 // deexpand all items
710 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
711 ui.treeDevices->topLevelItem(a)->setExpanded(false);
712 //deselect the selected item(s)
713 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
714 ui.treeDevices->selectedItems().at(a)->setSelected(false);
716 // find the new item
717 // enumerate all platform items
718 QList<QTreeWidgetItem*> itmList
719 = ui.treeDevices->findItems("*",Qt::MatchWildcard);
720 for(int i=0; i< itmList.size();i++)
722 //enumerate device items
723 for(int j=0;j < itmList.at(i)->childCount();j++)
725 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
726 // unset bold flag
727 QFont f = itmList.at(i)->child(j)->font(0);
728 f.setBold(false);
729 itmList.at(i)->child(j)->setFont(0, f);
731 if(devicename == data) // item found
733 f.setBold(true);
734 itmList.at(i)->child(j)->setFont(0, f);
735 itmList.at(i)->child(j)->setSelected(true); //select the item
736 itmList.at(i)->setExpanded(true); //expand the platform item
737 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
738 ui.treeDevices->scrollToItem(itmList.at(i)->child(j));
739 break;
743 this->unsetCursor();
745 if(!detector.errdev().isEmpty()) {
746 QString text;
747 if(SystemInfo::platformValue(detector.errdev(),
748 SystemInfo::CurBootloaderMethod) == "ipod") {
749 text = tr("%1 \"MacPod\" found!\n"
750 "Rockbox needs a FAT formatted Ipod (so-called \"WinPod\") "
751 "to run. ").arg(SystemInfo::platformValue(
752 detector.errdev(), SystemInfo::CurName).toString());
754 // treat all other errors as MTP device for now.
755 else {
756 text = tr("%1 in MTP mode found!\n"
757 "You need to change your player to MSC mode for installation. ")
758 .arg(SystemInfo::platformValue(detector.errdev(),
759 SystemInfo::CurName).toString());
761 text += tr("Until you change this installation will fail!");
763 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
764 return;
766 if(!detector.incompatdev().isEmpty()) {
767 QString text;
768 text = tr("Detected an unsupported player:\n%1\n"
769 "Sorry, Rockbox doesn't run on your player.")
770 .arg(SystemInfo::platformValue(detector.incompatdev(),
771 SystemInfo::CurName).toString());
773 QMessageBox::critical(this, tr("Fatal: player incompatible"),
774 text, QMessageBox::Ok);
775 return;
778 if(detector.getMountPoint() != "" )
780 setMountpoint(detector.getMountPoint());
782 else
784 QMessageBox::warning(this, tr("Autodetection"),
785 tr("Could not detect a Mountpoint.\n"
786 "Select your Mountpoint manually."),
787 QMessageBox::Ok ,QMessageBox::Ok);
790 else
792 this->unsetCursor();
793 QMessageBox::warning(this, tr("Autodetection"),
794 tr("Could not detect a device.\n"
795 "Select your device and Mountpoint manually."),
796 QMessageBox::Ok ,QMessageBox::Ok);
799 ui.treeDevices->setEnabled(true);
803 void Config::cacheClear()
805 if(QMessageBox::critical(this, tr("Really delete cache?"),
806 tr("Do you really want to delete the cache? "
807 "Make absolutely sure this setting is correct as it will "
808 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
809 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
810 return;
812 QString cache = ui.cachePath->text() + "/rbutil-cache/";
813 if(!QFileInfo(cache).isDir()) {
814 QMessageBox::critical(this, tr("Path wrong!"),
815 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
816 return;
818 QDir dir(cache);
819 QStringList fn;
820 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
822 for(int i = 0; i < fn.size(); i++) {
823 QString f = cache + fn.at(i);
824 QFile::remove(f);
826 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
830 void Config::configTts()
832 int index = ui.comboTts->currentIndex();
833 TTSBase* tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
835 EncTtsCfgGui gui(this,tts,TTSBase::getTTSName(ui.comboTts->itemData(index).toString()));
836 gui.exec();
837 updateTtsState(ui.comboTts->currentIndex());
838 delete tts; /* Config objects are never deleted (in fact, they are
839 leaked..), so we can't rely on QObject, since that would
840 delete the TTSBase instance on application exit */
843 void Config::testTts()
845 QString errstr;
846 int index = ui.comboTts->currentIndex();
847 TTSBase* tts;
848 tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
849 if(!tts)
851 QMessageBox::critical(this, tr("TTS error"),
852 tr("The selected TTS failed to initialize. You can't use this TTS."));
853 return;
855 ui.testTTS->setEnabled(false);
856 if(!tts->configOk())
858 QMessageBox::warning(this,tr("TTS configuration invalid"),
859 tr("TTS configuration invalid. \n Please configure TTS engine."));
860 return;
862 if(!tts->start(&errstr))
864 QMessageBox::warning(this,tr("Could not start TTS engine."),
865 tr("Could not start TTS engine.\n") + errstr
866 + tr("\nPlease configure TTS engine."));
867 ui.testTTS->setEnabled(true);
868 return;
871 QString filename;
872 QTemporaryFile file(this);
873 // keep filename empty if the TTS can do speaking for itself.
874 if(!(tts->capabilities() & TTSBase::CanSpeak)) {
875 file.open();
876 filename = file.fileName();
877 file.close();
880 if(tts->voice(tr("Rockbox Utility Voice Test"),filename,&errstr) == FatalError)
882 tts->stop();
883 QMessageBox::warning(this,tr("Could not voice test string."),
884 tr("Could not voice test string.\n") + errstr
885 + tr("\nPlease configure TTS engine."));
886 ui.testTTS->setEnabled(false);
887 return;
889 tts->stop();
890 if(!filename.isEmpty()) {
891 #if defined(Q_OS_LINUX)
892 QString exe = Utils::findExecutable("aplay");
893 if(exe == "") exe = Utils::findExecutable("play");
894 if(exe != "")
896 QProcess::execute(exe+" "+filename);
898 #else
899 QSound::play(filename);
900 #endif
902 ui.testTTS->setEnabled(true);
903 delete tts; /* Config objects are never deleted (in fact, they are
904 leaked..), so we can't rely on QObject, since that would
905 delete the TTSBase instance on application exit */
908 void Config::configEnc()
910 if(ui.treeDevices->selectedItems().size() == 0)
911 return;
913 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
914 QString encoder = SystemInfo::platformValue(devname,
915 SystemInfo::CurEncoder).toString();
916 ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(devname,
917 SystemInfo::CurEncoder).toString()));
920 EncoderBase* enc = EncoderBase::getEncoder(this,encoder);
922 EncTtsCfgGui gui(this,enc,EncoderBase::getEncoderName(encoder));
923 gui.exec();
925 updateEncState();
929 void Config::changeEvent(QEvent *e)
931 if(e->type() == QEvent::LanguageChange) {
932 ui.retranslateUi(this);
933 updateCacheInfo(ui.cachePath->text());
934 } else {
935 QWidget::changeEvent(e);