Only update mountpoint on Accept when empty.
[maemo-rb.git] / rbutil / rbutilqt / configure.cpp
blob9dee3f40c69ad6324bfcbc673198f40161203822
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 <QtGui>
21 #include "version.h"
22 #include "configure.h"
23 #include "autodetection.h"
24 #include "ui_configurefrm.h"
25 #include "encoderbase.h"
26 #include "ttsbase.h"
27 #include "system.h"
28 #include "encttscfggui.h"
29 #include "rbsettings.h"
30 #include "serverinfo.h"
31 #include "systeminfo.h"
32 #include "utils.h"
33 #include "comboboxviewdelegate.h"
34 #if defined(Q_OS_WIN32)
35 #if defined(UNICODE)
36 #define _UNICODE
37 #endif
38 #include <tchar.h>
39 #include <windows.h>
40 #endif
41 #include "rbutilqt.h"
43 #include "systrace.h"
45 #define DEFAULT_LANG "English (en)"
46 #define DEFAULT_LANG_CODE "en"
48 Config::Config(QWidget *parent,int index) : QDialog(parent)
50 programPath = qApp->applicationDirPath() + "/";
51 ui.setupUi(this);
52 ui.tabConfiguration->setCurrentIndex(index);
53 ui.radioManualProxy->setChecked(true);
54 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
55 QRegExp validate("[0-9]*");
56 proxyValidator->setRegExp(validate);
57 ui.proxyPort->setValidator(proxyValidator);
59 // build language list and sort alphabetically
60 QStringList langs = findLanguageFiles();
61 for(int i = 0; i < langs.size(); ++i)
62 lang.insert(languageName(langs.at(i))
63 + QString(" (%1)").arg(langs.at(i)), langs.at(i));
64 lang.insert(DEFAULT_LANG, DEFAULT_LANG_CODE);
65 QMap<QString, QString>::const_iterator i = lang.constBegin();
66 while (i != lang.constEnd()) {
67 ui.listLanguages->addItem(i.key());
68 i++;
71 ComboBoxViewDelegate *delegate = new ComboBoxViewDelegate(this);
72 ui.mountPoint->setItemDelegate(delegate);
73 #if !defined(DBG)
74 ui.mountPoint->setEditable(false);
75 #endif
77 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
78 ui.proxyPass->setEchoMode(QLineEdit::Password);
79 ui.treeDevices->setAlternatingRowColors(true);
80 ui.listLanguages->setAlternatingRowColors(true);
82 /* Explicitly set some widgets to have left-to-right layout */
83 ui.treeDevices->setLayoutDirection(Qt::LeftToRight);
84 ui.mountPoint->setLayoutDirection(Qt::LeftToRight);
85 ui.proxyHost->setLayoutDirection(Qt::LeftToRight);
86 ui.proxyPort->setLayoutDirection(Qt::LeftToRight);
87 ui.proxyUser->setLayoutDirection(Qt::LeftToRight);
88 ui.proxyPass->setLayoutDirection(Qt::LeftToRight);
89 ui.listLanguages->setLayoutDirection(Qt::LeftToRight);
90 ui.cachePath->setLayoutDirection(Qt::LeftToRight);
91 ui.comboTts->setLayoutDirection(Qt::LeftToRight);
93 this->setModal(true);
95 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
96 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
97 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
98 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
99 connect(ui.refreshMountPoint, SIGNAL(clicked()), this, SLOT(refreshMountpoint()));
100 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
101 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
102 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
103 connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
104 connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
105 connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
106 connect(ui.treeDevices, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
107 connect(ui.testTTS,SIGNAL(clicked()),this,SLOT(testTts()));
108 connect(ui.showDisabled, SIGNAL(toggled(bool)), this, SLOT(showDisabled(bool)));
109 connect(ui.mountPoint, SIGNAL(editTextChanged(QString)), this, SLOT(updateMountpoint(QString)));
110 connect(ui.mountPoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMountpoint(int)));
111 // delete this dialog after it finished automatically.
112 connect(this, SIGNAL(finished(int)), this, SLOT(deleteLater()));
114 setUserSettings();
115 setDevices();
119 void Config::accept()
121 qDebug() << "[Config] checking configuration";
122 QString errormsg = tr("The following errors occurred:") + "<ul>";
123 bool error = false;
125 // proxy: save entered proxy values, not displayed.
126 if(ui.radioManualProxy->isChecked()) {
127 proxy.setScheme("http");
128 proxy.setUserName(ui.proxyUser->text());
129 proxy.setPassword(ui.proxyPass->text());
130 proxy.setHost(ui.proxyHost->text());
131 proxy.setPort(ui.proxyPort->text().toInt());
134 // QUrl::toEncoded() doesn't encode a colon in the password correctly,
135 // which will result in errors during parsing the string.
136 // QUrl::toPercentEncoding() does work as expected, so build the string to
137 // store in the configuration file manually.
138 QString proxystring = "http://"
139 + QString(QUrl::toPercentEncoding(proxy.userName())) + ":"
140 + QString(QUrl::toPercentEncoding(proxy.password())) + "@"
141 + proxy.host() + ":"
142 + QString::number(proxy.port());
143 RbSettings::setValue(RbSettings::Proxy, proxystring);
144 qDebug() << "[Config] setting proxy to:" << proxy;
145 // proxy type
146 QString proxyType;
147 if(ui.radioNoProxy->isChecked()) proxyType = "none";
148 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
149 else proxyType = "manual";
150 RbSettings::setValue(RbSettings::ProxyType, proxyType);
152 RbSettings::setValue(RbSettings::Language, language);
154 // make sure mountpoint is read from dropdown box
155 if(mountpoint.isEmpty()) {
156 updateMountpoint(ui.mountPoint->currentIndex());
159 // mountpoint
160 if(mountpoint.isEmpty()) {
161 errormsg += "<li>" + tr("No mountpoint given") + "</li>";
162 error = true;
164 else if(!QFileInfo(mountpoint).exists()) {
165 errormsg += "<li>" + tr("Mountpoint does not exist") + "</li>";
166 error = true;
168 else if(!QFileInfo(mountpoint).isDir()) {
169 errormsg += "<li>" + tr("Mountpoint is not a directory.") + "</li>";
170 error = true;
172 else if(!QFileInfo(mountpoint).isWritable()) {
173 errormsg += "<li>" + tr("Mountpoint is not writeable") + "</li>";
174 error = true;
176 else {
177 RbSettings::setValue(RbSettings::Mountpoint,
178 QDir::fromNativeSeparators(mountpoint));
181 // platform
182 QString nplat;
183 if(ui.treeDevices->selectedItems().size() != 0) {
184 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
185 RbSettings::setValue(RbSettings::Platform, nplat);
187 else {
188 errormsg += "<li>" + tr("No player selected") + "</li>";
189 error = true;
192 // cache settings
193 if(QFileInfo(ui.cachePath->text()).isDir()) {
194 if(!QFileInfo(ui.cachePath->text()).isWritable()) {
195 errormsg += "<li>" + tr("Cache path not writeable. Leave path empty "
196 "to default to systems temporary path.") + "</li>";
197 error = true;
199 else
200 RbSettings::setValue(RbSettings::CachePath, ui.cachePath->text());
202 else // default to system temp path
203 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
204 RbSettings::setValue(RbSettings::CacheDisabled, ui.cacheDisable->isChecked());
205 RbSettings::setValue(RbSettings::CacheOffline, ui.cacheOfflineMode->isChecked());
207 // tts settings
208 RbSettings::setValue(RbSettings::UseTtsCorrections, ui.ttsCorrections->isChecked());
209 int i = ui.comboTts->currentIndex();
210 RbSettings::setValue(RbSettings::Tts, ui.comboTts->itemData(i).toString());
212 RbSettings::setValue(RbSettings::RbutilVersion, PUREVERSION);
214 errormsg += "</ul>";
215 errormsg += tr("You need to fix the above errors before you can continue.");
217 if(error) {
218 QMessageBox::critical(this, tr("Configuration error"), errormsg);
220 else {
221 // sync settings
222 RbSettings::sync();
223 this->close();
224 emit settingsUpdated();
229 void Config::abort()
231 qDebug() << "[Config] aborted.";
232 this->close();
236 void Config::setUserSettings()
238 // set proxy
239 proxy.setEncodedUrl(RbSettings::value(RbSettings::Proxy).toByteArray());
241 if(proxy.port() > 0)
242 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
243 else ui.proxyPort->setText("");
244 ui.proxyHost->setText(proxy.host());
245 ui.proxyUser->setText(proxy.userName());
246 ui.proxyPass->setText(proxy.password());
248 QString proxyType = RbSettings::value(RbSettings::ProxyType).toString();
249 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
250 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
251 else ui.radioNoProxy->setChecked(true);
253 // set language selection
254 QList<QListWidgetItem*> a;
255 QString b;
256 // find key for lang value
257 QMap<QString, QString>::const_iterator i = lang.constBegin();
258 QString l = RbSettings::value(RbSettings::Language).toString();
259 if(l.isEmpty())
260 l = QLocale::system().name();
261 while (i != lang.constEnd()) {
262 if(i.value() == l) {
263 b = i.key();
264 break;
266 else if(l.startsWith(i.value(), Qt::CaseInsensitive)) {
267 // check if there is a base language (en -> en_US, etc.)
268 b = i.key();
269 break;
271 i++;
273 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
274 if(a.size() > 0)
275 ui.listLanguages->setCurrentItem(a.at(0));
276 // don't connect before language list has been set up to prevent
277 // triggering the signal by selecting the saved language.
278 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
280 // devices tab
281 refreshMountpoint();
282 mountpoint = QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint).toString());
283 setMountpoint(mountpoint);
285 // cache tab
286 if(!QFileInfo(RbSettings::value(RbSettings::CachePath).toString()).isDir())
287 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
288 ui.cachePath->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath).toString()));
289 ui.cacheDisable->setChecked(RbSettings::value(RbSettings::CacheDisabled).toBool());
290 ui.cacheOfflineMode->setChecked(RbSettings::value(RbSettings::CacheOffline).toBool());
291 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
293 // TTS tab
294 ui.ttsCorrections->setChecked(RbSettings::value(RbSettings::UseTtsCorrections).toBool());
298 void Config::updateCacheInfo(QString path)
300 QList<QFileInfo> fs;
301 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
302 qint64 sz = 0;
303 for(int i = 0; i < fs.size(); i++) {
304 sz += fs.at(i).size();
306 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
307 .arg(sz/1024));
311 void Config::showDisabled(bool show)
313 qDebug() << "[Config] disabled targets shown:" << show;
314 if(show)
315 QMessageBox::warning(this, tr("Showing disabled targets"),
316 tr("You just enabled showing targets that are marked disabled. "
317 "Disabled targets are not recommended to end users. Please "
318 "use this option only if you know what you are doing."));
319 setDevices();
324 void Config::setDevices()
327 // setup devices table
328 qDebug() << "[Config] setting up devices list";
330 QStringList platformList;
331 if(ui.showDisabled->isChecked())
332 platformList = SystemInfo::platforms(SystemInfo::PlatformAllDisabled);
333 else
334 platformList = SystemInfo::platforms(SystemInfo::PlatformAll);
336 QMap <QString, QString> manuf;
337 for(int it = 0; it < platformList.size(); it++)
339 QString curbrand = SystemInfo::platformValue(platformList.at(it),
340 SystemInfo::CurBrand).toString();
341 manuf.insertMulti(curbrand, platformList.at(it));
344 // set up devices table
345 ui.treeDevices->header()->hide();
346 ui.treeDevices->expandAll();
347 ui.treeDevices->setColumnCount(1);
348 QList<QTreeWidgetItem *> items;
350 // get manufacturers
351 QStringList brands = manuf.uniqueKeys();
352 QTreeWidgetItem *w;
353 QTreeWidgetItem *w2;
354 QTreeWidgetItem *w3 = 0;
356 QString selected = RbSettings::value(RbSettings::Platform).toString();
357 for(int c = 0; c < brands.size(); c++) {
358 w = new QTreeWidgetItem();
359 w->setFlags(Qt::ItemIsEnabled);
360 w->setText(0, brands.at(c));
361 items.append(w);
362 // go through platforms and add all players matching the current brand
363 for(int it = 0; it < platformList.size(); it++) {
364 // skip if not current brand
365 if(!manuf.values(brands.at(c)).contains(platformList.at(it)))
366 continue;
367 // construct display name
368 QString curname = SystemInfo::platformValue(platformList.at(it),
369 SystemInfo::CurName).toString() +
370 " (" +ServerInfo::platformValue(platformList.at(it),
371 ServerInfo::CurStatus).toString() +")";
372 qDebug() << "[Config] add supported device:" << brands.at(c) << curname;
373 w2 = new QTreeWidgetItem(w, QStringList(curname));
374 w2->setData(0, Qt::UserRole, platformList.at(it));
376 if(platformList.at(it) == selected) {
377 w2->setSelected(true);
378 w->setExpanded(true);
379 w3 = w2; // save pointer to hilight old selection
381 items.append(w2);
384 // remove any old items in list
385 QTreeWidgetItem* widgetitem;
386 do {
387 widgetitem = ui.treeDevices->takeTopLevelItem(0);
388 delete widgetitem;
390 while(widgetitem);
391 // add new items
392 ui.treeDevices->insertTopLevelItems(0, items);
393 if(w3 != 0) {
394 ui.treeDevices->setCurrentItem(w3); // hilight old selection
395 ui.treeDevices->scrollToItem(w3);
398 // tts / encoder tab
400 //encoders
401 updateEncState();
403 //tts
404 QStringList ttslist = TTSBase::getTTSList();
405 for(int a = 0; a < ttslist.size(); a++)
406 ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
407 //update index of combobox
408 int index = ui.comboTts->findData(RbSettings::value(RbSettings::Tts).toString());
409 if(index < 0) index = 0;
410 ui.comboTts->setCurrentIndex(index);
411 updateTtsState(index);
416 void Config::updateTtsState(int index)
418 QString ttsName = ui.comboTts->itemData(index).toString();
419 TTSBase* tts = TTSBase::getTTS(this,ttsName);
421 if(tts->configOk())
423 ui.configTTSstatus->setText(tr("Configuration OK"));
424 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
425 ui.testTTS->setEnabled(true);
427 else
429 ui.configTTSstatus->setText(tr("Configuration INVALID"));
430 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
431 ui.testTTS->setEnabled(false);
434 delete tts; /* Config objects are never deleted (in fact, they are leaked..), so we can't rely on QObject,
435 since that would delete the TTSBase instance on application exit*/
438 void Config::updateEncState()
440 if(ui.treeDevices->selectedItems().size() == 0)
441 return;
443 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
444 QString encoder = SystemInfo::platformValue(devname,
445 SystemInfo::CurEncoder).toString();
446 ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(devname,
447 SystemInfo::CurEncoder).toString()));
449 EncoderBase* enc = EncoderBase::getEncoder(this,encoder);
451 if(enc->configOk())
453 ui.configEncstatus->setText(tr("Configuration OK"));
454 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
456 else
458 ui.configEncstatus->setText(tr("Configuration INVALID"));
459 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
464 void Config::setNoProxy(bool checked)
466 bool i = !checked;
467 ui.proxyPort->setEnabled(i);
468 ui.proxyHost->setEnabled(i);
469 ui.proxyUser->setEnabled(i);
470 ui.proxyPass->setEnabled(i);
474 void Config::setSystemProxy(bool checked)
476 ui.proxyPort->setEnabled(!checked);
477 ui.proxyHost->setEnabled(!checked);
478 ui.proxyUser->setEnabled(!checked);
479 ui.proxyPass->setEnabled(!checked);
480 if(checked) {
481 // save values in input box
482 proxy.setScheme("http");
483 proxy.setUserName(ui.proxyUser->text());
484 proxy.setPassword(ui.proxyPass->text());
485 proxy.setHost(ui.proxyHost->text());
486 proxy.setPort(ui.proxyPort->text().toInt());
487 // show system values in input box
488 QUrl envproxy = System::systemProxy();
489 qDebug() << "[Config] setting system proxy" << envproxy;
491 ui.proxyHost->setText(envproxy.host());
492 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
493 ui.proxyUser->setText(envproxy.userName());
494 ui.proxyPass->setText(envproxy.password());
496 if(envproxy.host().isEmpty() || envproxy.port() == -1) {
497 qDebug() << "[Config] sytem proxy is invalid.";
498 QMessageBox::warning(this, tr("Proxy Detection"),
499 tr("The System Proxy settings are invalid!\n"
500 "Rockbox Utility can't work with this proxy settings. "
501 "Make sure the system proxy is set correctly. Note that "
502 "\"proxy auto-config (PAC)\" scripts are not supported by "
503 "Rockbox Utility. If your system uses this you need "
504 "to use manual proxy settings."),
505 QMessageBox::Ok ,QMessageBox::Ok);
506 // the current proxy settings are invalid. Check the saved proxy
507 // type again.
508 if(RbSettings::value(RbSettings::ProxyType).toString() == "manual")
509 ui.radioManualProxy->setChecked(true);
510 else
511 ui.radioNoProxy->setChecked(true);
515 else {
516 ui.proxyHost->setText(proxy.host());
517 if(proxy.port() > 0)
518 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
519 else ui.proxyPort->setText("");
520 ui.proxyUser->setText(proxy.userName());
521 ui.proxyPass->setText(proxy.password());
527 QStringList Config::findLanguageFiles()
529 QDir dir(programPath);
530 QStringList fileNames;
531 QStringList langs;
532 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
534 QDir resDir(":/lang");
535 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
537 QRegExp exp("^rbutil_(.*)\\.qm");
538 for(int i = 0; i < fileNames.size(); i++) {
539 QString a = fileNames.at(i);
540 a.replace(exp, "\\1");
541 langs.append(a);
543 langs.sort();
544 qDebug() << "[Config] available lang files:" << langs;
546 return langs;
550 QString Config::languageName(const QString &qmFile)
552 QTranslator translator;
554 QString file = "rbutil_" + qmFile;
555 if(!translator.load(file, programPath))
556 translator.load(file, ":/lang");
558 return translator.translate("Configure", "English",
559 "This is the localized language name, i.e. your language.");
563 void Config::updateLanguage()
565 qDebug() << "[Config] update selected language";
567 // remove all old translators
568 for(int i = 0; i < RbUtilQt::translators.size(); ++i) {
569 qApp->removeTranslator(RbUtilQt::translators.at(i));
570 // do not delete old translators, this confuses Qt.
572 RbUtilQt::translators.clear();
573 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
574 if(a.size() > 0)
575 language = lang.value(a.at(0)->text());
576 qDebug() << "[Config] new language:" << language;
578 QString applang = QLocale::system().name();
579 QTranslator *translator = new QTranslator(qApp);
580 QTranslator *qttrans = new QTranslator(qApp);
581 QString absolutePath = QCoreApplication::instance()->applicationDirPath();
583 if(!translator->load("rbutil_" + language, absolutePath))
584 translator->load("rbutil_" + language, ":/lang");
585 if(!qttrans->load("qt_" + language,
586 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
587 qttrans->load("qt_" + language, ":/lang");
589 qApp->installTranslator(translator);
590 qApp->installTranslator(qttrans);
591 //: This string is used to indicate the writing direction. Translate it
592 //: to "RTL" (without quotes) for RTL languages. Anything else will get
593 //: treated as LTR language.
594 if(QObject::tr("LTR") == "RTL")
595 qApp->setLayoutDirection(Qt::RightToLeft);
596 else
597 qApp->setLayoutDirection(Qt::LeftToRight);
599 RbUtilQt::translators.append(translator);
600 RbUtilQt::translators.append(qttrans);
602 QLocale::setDefault(language);
607 void Config::browseCache()
609 QString old = ui.cachePath->text();
610 if(!QFileInfo(old).isDir())
611 old = QDir::tempPath();
612 QString c = QFileDialog::getExistingDirectory(this, tr("Set Cache Path"), old);
613 if(c.isEmpty())
614 c = old;
615 else if(!QFileInfo(c).isDir())
616 c = QDir::tempPath();
617 ui.cachePath->setText(QDir::toNativeSeparators(c));
618 updateCacheInfo(c);
622 void Config::refreshMountpoint()
624 // avoid QComboBox to send signals during rebuild to avoid changing to an
625 // unwanted item.
626 ui.mountPoint->blockSignals(true);
627 ui.mountPoint->clear();
628 QStringList mps = Utils::mountpoints();
629 for(int i = 0; i < mps.size(); ++i) {
630 // add mountpoint as user data so we can change the displayed string
631 // later (to include volume label or similar)
632 // Skip unwritable mountpoints, they are not useable for us.
633 if(QFileInfo(mps.at(i)).isWritable()) {
634 QString description = QString("%1 (%2 GiB of %3 GiB free)")
635 .arg(Utils::filesystemName(mps.at(i)))
636 .arg((double)Utils::filesystemFree(mps.at(i))/(1<<30), 0, 'f', 2)
637 .arg((double)Utils::filesystemTotal(mps.at(i))/(1<<30), 0, 'f', 2);
638 ui.mountPoint->addItem(QDir::toNativeSeparators(mps.at(i)), description);
641 if(!mountpoint.isEmpty()) {
642 setMountpoint(mountpoint);
644 ui.mountPoint->blockSignals(false);
648 void Config::updateMountpoint(QString m)
650 if(!m.isEmpty()) {
651 mountpoint = QDir::fromNativeSeparators(m);
652 qDebug() << "[Config] Mountpoint set to" << mountpoint;
657 void Config::updateMountpoint(int idx)
659 if(idx == -1) {
660 return;
662 QString mp = ui.mountPoint->itemText(idx);
663 if(!mp.isEmpty()) {
664 mountpoint = QDir::fromNativeSeparators(mp);
665 qDebug() << "[Config] Mountpoint set to" << mountpoint;
670 void Config::setMountpoint(QString m)
672 if(m.isEmpty()) {
673 return;
675 int index = ui.mountPoint->findText(QDir::toNativeSeparators(m));
676 if(index != -1) {
677 ui.mountPoint->setCurrentIndex(index);
679 else {
680 // keep a mountpoint that is not in the list for convenience (to allow
681 // easier development)
682 ui.mountPoint->addItem(QDir::toNativeSeparators(m));
683 ui.mountPoint->setCurrentIndex(ui.mountPoint->findText(m));
685 qDebug() << "[Config] Mountpoint set to" << mountpoint;
689 void Config::autodetect()
691 Autodetection detector(this);
692 // disable tree during detection as "working" feedback.
693 // TODO: replace the tree view with a splash screen during this time.
694 ui.treeDevices->setEnabled(false);
695 this->setCursor(Qt::WaitCursor);
696 QCoreApplication::processEvents();
698 if(detector.detect()) //let it detect
700 QString devicename = detector.getDevice();
701 // deexpand all items
702 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
703 ui.treeDevices->topLevelItem(a)->setExpanded(false);
704 //deselect the selected item(s)
705 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
706 ui.treeDevices->selectedItems().at(a)->setSelected(false);
708 // find the new item
709 // enumerate all platform items
710 QList<QTreeWidgetItem*> itmList
711 = ui.treeDevices->findItems("*",Qt::MatchWildcard);
712 for(int i=0; i< itmList.size();i++)
714 //enumerate device items
715 for(int j=0;j < itmList.at(i)->childCount();j++)
717 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
718 // unset bold flag
719 QFont f = itmList.at(i)->child(j)->font(0);
720 f.setBold(false);
721 itmList.at(i)->child(j)->setFont(0, f);
723 if(devicename == data) // item found
725 f.setBold(true);
726 itmList.at(i)->child(j)->setFont(0, f);
727 itmList.at(i)->child(j)->setSelected(true); //select the item
728 itmList.at(i)->setExpanded(true); //expand the platform item
729 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
730 ui.treeDevices->scrollToItem(itmList.at(i)->child(j));
731 break;
735 this->unsetCursor();
737 if(!detector.errdev().isEmpty()) {
738 QString text;
739 if(SystemInfo::platformValue(detector.errdev(),
740 SystemInfo::CurBootloaderMethod) == "ipod") {
741 text = tr("%1 \"MacPod\" found!\n"
742 "Rockbox needs a FAT formatted Ipod (so-called \"WinPod\") "
743 "to run. ").arg(SystemInfo::platformValue(
744 detector.errdev(), SystemInfo::CurName).toString());
746 // treat all other errors as MTP device for now.
747 else {
748 text = tr("%1 in MTP mode found!\n"
749 "You need to change your player to MSC mode for installation. ")
750 .arg(SystemInfo::platformValue(detector.errdev(),
751 SystemInfo::CurName).toString());
753 text += tr("Until you change this installation will fail!");
755 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
756 return;
758 if(!detector.incompatdev().isEmpty()) {
759 QString text;
760 text = tr("Detected an unsupported player:\n%1\n"
761 "Sorry, Rockbox doesn't run on your player.")
762 .arg(SystemInfo::platformValue(detector.incompatdev(),
763 SystemInfo::CurName).toString());
765 QMessageBox::critical(this, tr("Fatal: player incompatible"),
766 text, QMessageBox::Ok);
767 return;
770 if(detector.getMountPoint() != "" )
772 setMountpoint(detector.getMountPoint());
774 else
776 QMessageBox::warning(this, tr("Autodetection"),
777 tr("Could not detect a Mountpoint.\n"
778 "Select your Mountpoint manually."),
779 QMessageBox::Ok ,QMessageBox::Ok);
782 else
784 this->unsetCursor();
785 QMessageBox::warning(this, tr("Autodetection"),
786 tr("Could not detect a device.\n"
787 "Select your device and Mountpoint manually."),
788 QMessageBox::Ok ,QMessageBox::Ok);
791 ui.treeDevices->setEnabled(true);
795 void Config::cacheClear()
797 if(QMessageBox::critical(this, tr("Really delete cache?"),
798 tr("Do you really want to delete the cache? "
799 "Make absolutely sure this setting is correct as it will "
800 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
801 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
802 return;
804 QString cache = ui.cachePath->text() + "/rbutil-cache/";
805 if(!QFileInfo(cache).isDir()) {
806 QMessageBox::critical(this, tr("Path wrong!"),
807 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
808 return;
810 QDir dir(cache);
811 QStringList fn;
812 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
814 for(int i = 0; i < fn.size(); i++) {
815 QString f = cache + fn.at(i);
816 QFile::remove(f);
818 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
822 void Config::configTts()
824 int index = ui.comboTts->currentIndex();
825 TTSBase* tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
827 EncTtsCfgGui gui(this,tts,TTSBase::getTTSName(ui.comboTts->itemData(index).toString()));
828 gui.exec();
829 updateTtsState(ui.comboTts->currentIndex());
830 delete tts; /* Config objects are never deleted (in fact, they are
831 leaked..), so we can't rely on QObject, since that would
832 delete the TTSBase instance on application exit */
835 void Config::testTts()
837 QString errstr;
838 int index = ui.comboTts->currentIndex();
839 TTSBase* tts;
841 ui.testTTS->setEnabled(false);
842 tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
843 if(!tts->configOk())
845 QMessageBox::warning(this,tr("TTS configuration invalid"),
846 tr("TTS configuration invalid. \n Please configure TTS engine."));
847 return;
849 if(!tts->start(&errstr))
851 QMessageBox::warning(this,tr("Could not start TTS engine."),
852 tr("Could not start TTS engine.\n") + errstr
853 + tr("\nPlease configure TTS engine."));
854 ui.testTTS->setEnabled(true);
855 return;
858 QString filename;
859 QTemporaryFile file(this);
860 // keep filename empty if the TTS can do speaking for itself.
861 if(!(tts->capabilities() & TTSBase::CanSpeak)) {
862 file.open();
863 filename = file.fileName();
864 file.close();
867 if(tts->voice(tr("Rockbox Utility Voice Test"),filename,&errstr) == FatalError)
869 tts->stop();
870 QMessageBox::warning(this,tr("Could not voice test string."),
871 tr("Could not voice test string.\n") + errstr
872 + tr("\nPlease configure TTS engine."));
873 ui.testTTS->setEnabled(false);
874 return;
876 tts->stop();
877 if(!filename.isEmpty()) {
878 #if defined(Q_OS_LINUX)
879 QString exe = Utils::findExecutable("aplay");
880 if(exe == "") exe = Utils::findExecutable("play");
881 if(exe != "")
883 QProcess::execute(exe+" "+filename);
885 #else
886 QSound::play(filename);
887 #endif
889 ui.testTTS->setEnabled(true);
890 delete tts; /* Config objects are never deleted (in fact, they are
891 leaked..), so we can't rely on QObject, since that would
892 delete the TTSBase instance on application exit */
895 void Config::configEnc()
897 if(ui.treeDevices->selectedItems().size() == 0)
898 return;
900 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
901 QString encoder = SystemInfo::platformValue(devname,
902 SystemInfo::CurEncoder).toString();
903 ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(devname,
904 SystemInfo::CurEncoder).toString()));
907 EncoderBase* enc = EncoderBase::getEncoder(this,encoder);
909 EncTtsCfgGui gui(this,enc,EncoderBase::getEncoderName(encoder));
910 gui.exec();
912 updateEncState();
916 void Config::changeEvent(QEvent *e)
918 if(e->type() == QEvent::LanguageChange) {
919 ui.retranslateUi(this);
920 updateCacheInfo(ui.cachePath->text());
921 } else {
922 QWidget::changeEvent(e);