Fix buttonbar and line padding in time&date screen.
[maemo-rb.git] / rbutil / rbutilqt / configure.cpp
blob872fcfaa8401a8c6edb8ba6d233f37c094c3d7b9
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 "encoders.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 #define DEFAULT_LANG "English (en)"
44 #define DEFAULT_LANG_CODE "en"
46 Config::Config(QWidget *parent,int index) : QDialog(parent)
48 programPath = qApp->applicationDirPath() + "/";
49 ui.setupUi(this);
50 ui.tabConfiguration->setCurrentIndex(index);
51 ui.radioManualProxy->setChecked(true);
52 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
53 QRegExp validate("[0-9]*");
54 proxyValidator->setRegExp(validate);
55 ui.proxyPort->setValidator(proxyValidator);
57 // build language list and sort alphabetically
58 QStringList langs = findLanguageFiles();
59 for(int i = 0; i < langs.size(); ++i)
60 lang.insert(languageName(langs.at(i))
61 + QString(" (%1)").arg(langs.at(i)), langs.at(i));
62 lang.insert(DEFAULT_LANG, DEFAULT_LANG_CODE);
63 QMap<QString, QString>::const_iterator i = lang.constBegin();
64 while (i != lang.constEnd()) {
65 ui.listLanguages->addItem(i.key());
66 i++;
69 ComboBoxViewDelegate *delegate = new ComboBoxViewDelegate(this);
70 ui.mountPoint->setItemDelegate(delegate);
71 #if !defined(DBG)
72 ui.mountPoint->setEditable(false);
73 #endif
75 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
76 ui.proxyPass->setEchoMode(QLineEdit::Password);
77 ui.treeDevices->setAlternatingRowColors(true);
78 ui.listLanguages->setAlternatingRowColors(true);
80 /* Explicitly set some widgets to have left-to-right layout */
81 ui.treeDevices->setLayoutDirection(Qt::LeftToRight);
82 ui.mountPoint->setLayoutDirection(Qt::LeftToRight);
83 ui.proxyHost->setLayoutDirection(Qt::LeftToRight);
84 ui.proxyPort->setLayoutDirection(Qt::LeftToRight);
85 ui.proxyUser->setLayoutDirection(Qt::LeftToRight);
86 ui.proxyPass->setLayoutDirection(Qt::LeftToRight);
87 ui.listLanguages->setLayoutDirection(Qt::LeftToRight);
88 ui.cachePath->setLayoutDirection(Qt::LeftToRight);
89 ui.comboTts->setLayoutDirection(Qt::LeftToRight);
91 this->setModal(true);
93 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
94 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
95 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
96 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
97 connect(ui.refreshMountPoint, SIGNAL(clicked()), this, SLOT(refreshMountpoint()));
98 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
99 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
100 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
101 connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
102 connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
103 connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
104 connect(ui.treeDevices, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
105 connect(ui.testTTS,SIGNAL(clicked()),this,SLOT(testTts()));
106 connect(ui.showDisabled, SIGNAL(toggled(bool)), this, SLOT(showDisabled(bool)));
107 connect(ui.mountPoint, SIGNAL(editTextChanged(QString)), this, SLOT(updateMountpoint(QString)));
108 connect(ui.mountPoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMountpoint(int)));
109 // delete this dialog after it finished automatically.
110 connect(this, SIGNAL(finished(int)), this, SLOT(deleteLater()));
112 setUserSettings();
113 setDevices();
117 void Config::accept()
119 qDebug() << "[Config] checking configuration";
120 QString errormsg = tr("The following errors occurred:") + "<ul>";
121 bool error = false;
123 // proxy: save entered proxy values, not displayed.
124 if(ui.radioManualProxy->isChecked()) {
125 proxy.setScheme("http");
126 proxy.setUserName(ui.proxyUser->text());
127 proxy.setPassword(ui.proxyPass->text());
128 proxy.setHost(ui.proxyHost->text());
129 proxy.setPort(ui.proxyPort->text().toInt());
132 // QUrl::toEncoded() doesn't encode a colon in the password correctly,
133 // which will result in errors during parsing the string.
134 // QUrl::toPercentEncoding() does work as expected, so build the string to
135 // store in the configuration file manually.
136 QString proxystring = "http://"
137 + QString(QUrl::toPercentEncoding(proxy.userName())) + ":"
138 + QString(QUrl::toPercentEncoding(proxy.password())) + "@"
139 + proxy.host() + ":"
140 + QString::number(proxy.port());
141 RbSettings::setValue(RbSettings::Proxy, proxystring);
142 qDebug() << "[Config] setting proxy to:" << proxy;
143 // proxy type
144 QString proxyType;
145 if(ui.radioNoProxy->isChecked()) proxyType = "none";
146 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
147 else proxyType = "manual";
148 RbSettings::setValue(RbSettings::ProxyType, proxyType);
150 RbSettings::setValue(RbSettings::Language, language);
152 // mountpoint
153 if(mountpoint.isEmpty()) {
154 errormsg += "<li>" + tr("No mountpoint given") + "</li>";
155 error = true;
157 else if(!QFileInfo(mountpoint).exists()) {
158 errormsg += "<li>" + tr("Mountpoint does not exist") + "</li>";
159 error = true;
161 else if(!QFileInfo(mountpoint).isDir()) {
162 errormsg += "<li>" + tr("Mountpoint is not a directory.") + "</li>";
163 error = true;
165 else if(!QFileInfo(mountpoint).isWritable()) {
166 errormsg += "<li>" + tr("Mountpoint is not writeable") + "</li>";
167 error = true;
169 else {
170 RbSettings::setValue(RbSettings::Mountpoint,
171 QDir::fromNativeSeparators(mountpoint));
174 // platform
175 QString nplat;
176 if(ui.treeDevices->selectedItems().size() != 0) {
177 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
178 RbSettings::setValue(RbSettings::Platform, nplat);
180 else {
181 errormsg += "<li>" + tr("No player selected") + "</li>";
182 error = true;
185 // cache settings
186 if(QFileInfo(ui.cachePath->text()).isDir()) {
187 if(!QFileInfo(ui.cachePath->text()).isWritable()) {
188 errormsg += "<li>" + tr("Cache path not writeable. Leave path empty "
189 "to default to systems temporary path.") + "</li>";
190 error = true;
192 else
193 RbSettings::setValue(RbSettings::CachePath, ui.cachePath->text());
195 else // default to system temp path
196 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
197 RbSettings::setValue(RbSettings::CacheDisabled, ui.cacheDisable->isChecked());
198 RbSettings::setValue(RbSettings::CacheOffline, ui.cacheOfflineMode->isChecked());
200 // tts settings
201 RbSettings::setValue(RbSettings::UseTtsCorrections, ui.ttsCorrections->isChecked());
202 int i = ui.comboTts->currentIndex();
203 RbSettings::setValue(RbSettings::Tts, ui.comboTts->itemData(i).toString());
205 RbSettings::setValue(RbSettings::RbutilVersion, PUREVERSION);
207 errormsg += "</ul>";
208 errormsg += tr("You need to fix the above errors before you can continue.");
210 if(error) {
211 QMessageBox::critical(this, tr("Configuration error"), errormsg);
213 else {
214 // sync settings
215 RbSettings::sync();
216 this->close();
217 emit settingsUpdated();
222 void Config::abort()
224 qDebug() << "[Config] aborted.";
225 this->close();
229 void Config::setUserSettings()
231 // set proxy
232 proxy.setEncodedUrl(RbSettings::value(RbSettings::Proxy).toByteArray());
234 if(proxy.port() > 0)
235 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
236 else ui.proxyPort->setText("");
237 ui.proxyHost->setText(proxy.host());
238 ui.proxyUser->setText(proxy.userName());
239 ui.proxyPass->setText(proxy.password());
241 QString proxyType = RbSettings::value(RbSettings::ProxyType).toString();
242 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
243 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
244 else ui.radioNoProxy->setChecked(true);
246 // set language selection
247 QList<QListWidgetItem*> a;
248 QString b;
249 // find key for lang value
250 QMap<QString, QString>::const_iterator i = lang.constBegin();
251 QString l = RbSettings::value(RbSettings::Language).toString();
252 if(l.isEmpty())
253 l = QLocale::system().name();
254 while (i != lang.constEnd()) {
255 if(i.value() == l) {
256 b = i.key();
257 break;
259 else if(l.startsWith(i.value(), Qt::CaseInsensitive)) {
260 // check if there is a base language (en -> en_US, etc.)
261 b = i.key();
262 break;
264 i++;
266 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
267 if(a.size() > 0)
268 ui.listLanguages->setCurrentItem(a.at(0));
269 // don't connect before language list has been set up to prevent
270 // triggering the signal by selecting the saved language.
271 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
273 // devices tab
274 refreshMountpoint();
275 mountpoint = QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint).toString());
276 setMountpoint(mountpoint);
278 // cache tab
279 if(!QFileInfo(RbSettings::value(RbSettings::CachePath).toString()).isDir())
280 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
281 ui.cachePath->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath).toString()));
282 ui.cacheDisable->setChecked(RbSettings::value(RbSettings::CacheDisabled).toBool());
283 ui.cacheOfflineMode->setChecked(RbSettings::value(RbSettings::CacheOffline).toBool());
284 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
286 // TTS tab
287 ui.ttsCorrections->setChecked(RbSettings::value(RbSettings::UseTtsCorrections).toBool());
291 void Config::updateCacheInfo(QString path)
293 QList<QFileInfo> fs;
294 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
295 qint64 sz = 0;
296 for(int i = 0; i < fs.size(); i++) {
297 sz += fs.at(i).size();
299 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
300 .arg(sz/1024));
304 void Config::showDisabled(bool show)
306 qDebug() << "[Config] disabled targets shown:" << show;
307 if(show)
308 QMessageBox::warning(this, tr("Showing disabled targets"),
309 tr("You just enabled showing targets that are marked disabled. "
310 "Disabled targets are not recommended to end users. Please "
311 "use this option only if you know what you are doing."));
312 setDevices();
317 void Config::setDevices()
320 // setup devices table
321 qDebug() << "[Config] setting up devices list";
323 QStringList platformList;
324 if(ui.showDisabled->isChecked())
325 platformList = SystemInfo::platforms(SystemInfo::PlatformAllDisabled);
326 else
327 platformList = SystemInfo::platforms(SystemInfo::PlatformAll);
329 QMap <QString, QString> manuf;
330 for(int it = 0; it < platformList.size(); it++)
332 QString curbrand = SystemInfo::platformValue(platformList.at(it),
333 SystemInfo::CurBrand).toString();
334 manuf.insertMulti(curbrand, platformList.at(it));
337 // set up devices table
338 ui.treeDevices->header()->hide();
339 ui.treeDevices->expandAll();
340 ui.treeDevices->setColumnCount(1);
341 QList<QTreeWidgetItem *> items;
343 // get manufacturers
344 QStringList brands = manuf.uniqueKeys();
345 QTreeWidgetItem *w;
346 QTreeWidgetItem *w2;
347 QTreeWidgetItem *w3 = 0;
349 QString selected = RbSettings::value(RbSettings::Platform).toString();
350 for(int c = 0; c < brands.size(); c++) {
351 w = new QTreeWidgetItem();
352 w->setFlags(Qt::ItemIsEnabled);
353 w->setText(0, brands.at(c));
354 items.append(w);
355 // go through platforms and add all players matching the current brand
356 for(int it = 0; it < platformList.size(); it++) {
357 // skip if not current brand
358 if(!manuf.values(brands.at(c)).contains(platformList.at(it)))
359 continue;
360 // construct display name
361 QString curname = SystemInfo::platformValue(platformList.at(it),
362 SystemInfo::CurName).toString() +
363 " (" +ServerInfo::platformValue(platformList.at(it),
364 ServerInfo::CurStatus).toString() +")";
365 qDebug() << "[Config] add supported device:" << brands.at(c) << curname;
366 w2 = new QTreeWidgetItem(w, QStringList(curname));
367 w2->setData(0, Qt::UserRole, platformList.at(it));
369 if(platformList.at(it) == selected) {
370 w2->setSelected(true);
371 w->setExpanded(true);
372 w3 = w2; // save pointer to hilight old selection
374 items.append(w2);
377 // remove any old items in list
378 QTreeWidgetItem* widgetitem;
379 do {
380 widgetitem = ui.treeDevices->takeTopLevelItem(0);
381 delete widgetitem;
383 while(widgetitem);
384 // add new items
385 ui.treeDevices->insertTopLevelItems(0, items);
386 if(w3 != 0) {
387 ui.treeDevices->setCurrentItem(w3); // hilight old selection
388 ui.treeDevices->scrollToItem(w3);
391 // tts / encoder tab
393 //encoders
394 updateEncState();
396 //tts
397 QStringList ttslist = TTSBase::getTTSList();
398 for(int a = 0; a < ttslist.size(); a++)
399 ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
400 //update index of combobox
401 int index = ui.comboTts->findData(RbSettings::value(RbSettings::Tts).toString());
402 if(index < 0) index = 0;
403 ui.comboTts->setCurrentIndex(index);
404 updateTtsState(index);
409 void Config::updateTtsState(int index)
411 QString ttsName = ui.comboTts->itemData(index).toString();
412 TTSBase* tts = TTSBase::getTTS(this,ttsName);
414 if(tts->configOk())
416 ui.configTTSstatus->setText(tr("Configuration OK"));
417 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
418 ui.testTTS->setEnabled(true);
420 else
422 ui.configTTSstatus->setText(tr("Configuration INVALID"));
423 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
424 ui.testTTS->setEnabled(false);
427 delete tts; /* Config objects are never deleted (in fact, they are leaked..), so we can't rely on QObject,
428 since that would delete the TTSBase instance on application exit*/
431 void Config::updateEncState()
433 if(ui.treeDevices->selectedItems().size() == 0)
434 return;
436 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
437 QString encoder = SystemInfo::platformValue(devname,
438 SystemInfo::CurEncoder).toString();
439 ui.encoderName->setText(EncBase::getEncoderName(SystemInfo::platformValue(devname,
440 SystemInfo::CurEncoder).toString()));
442 EncBase* enc = EncBase::getEncoder(this,encoder);
444 if(enc->configOk())
446 ui.configEncstatus->setText(tr("Configuration OK"));
447 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
449 else
451 ui.configEncstatus->setText(tr("Configuration INVALID"));
452 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
457 void Config::setNoProxy(bool checked)
459 bool i = !checked;
460 ui.proxyPort->setEnabled(i);
461 ui.proxyHost->setEnabled(i);
462 ui.proxyUser->setEnabled(i);
463 ui.proxyPass->setEnabled(i);
467 void Config::setSystemProxy(bool checked)
469 ui.proxyPort->setEnabled(!checked);
470 ui.proxyHost->setEnabled(!checked);
471 ui.proxyUser->setEnabled(!checked);
472 ui.proxyPass->setEnabled(!checked);
473 if(checked) {
474 // save values in input box
475 proxy.setScheme("http");
476 proxy.setUserName(ui.proxyUser->text());
477 proxy.setPassword(ui.proxyPass->text());
478 proxy.setHost(ui.proxyHost->text());
479 proxy.setPort(ui.proxyPort->text().toInt());
480 // show system values in input box
481 QUrl envproxy = System::systemProxy();
482 qDebug() << "[Config] setting system proxy" << envproxy;
484 ui.proxyHost->setText(envproxy.host());
485 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
486 ui.proxyUser->setText(envproxy.userName());
487 ui.proxyPass->setText(envproxy.password());
489 if(envproxy.host().isEmpty() || envproxy.port() == -1) {
490 qDebug() << "[Config] sytem proxy is invalid.";
491 QMessageBox::warning(this, tr("Proxy Detection"),
492 tr("The System Proxy settings are invalid!\n"
493 "Rockbox Utility can't work with this proxy settings. "
494 "Make sure the system proxy is set correctly. Note that "
495 "\"proxy auto-config (PAC)\" scripts are not supported by "
496 "Rockbox Utility. If your system uses this you need "
497 "to use manual proxy settings."),
498 QMessageBox::Ok ,QMessageBox::Ok);
499 // the current proxy settings are invalid. Check the saved proxy
500 // type again.
501 if(RbSettings::value(RbSettings::ProxyType).toString() == "manual")
502 ui.radioManualProxy->setChecked(true);
503 else
504 ui.radioNoProxy->setChecked(true);
508 else {
509 ui.proxyHost->setText(proxy.host());
510 if(proxy.port() > 0)
511 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
512 else ui.proxyPort->setText("");
513 ui.proxyUser->setText(proxy.userName());
514 ui.proxyPass->setText(proxy.password());
520 QStringList Config::findLanguageFiles()
522 QDir dir(programPath);
523 QStringList fileNames;
524 QStringList langs;
525 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
527 QDir resDir(":/lang");
528 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
530 QRegExp exp("^rbutil_(.*)\\.qm");
531 for(int i = 0; i < fileNames.size(); i++) {
532 QString a = fileNames.at(i);
533 a.replace(exp, "\\1");
534 langs.append(a);
536 langs.sort();
537 qDebug() << "[Config] available lang files:" << langs;
539 return langs;
543 QString Config::languageName(const QString &qmFile)
545 QTranslator translator;
547 QString file = "rbutil_" + qmFile;
548 if(!translator.load(file, programPath))
549 translator.load(file, ":/lang");
551 return translator.translate("Configure", "English",
552 "This is the localized language name, i.e. your language.");
556 void Config::updateLanguage()
558 qDebug() << "[Config] update selected language";
560 // remove all old translators
561 for(int i = 0; i < RbUtilQt::translators.size(); ++i) {
562 qApp->removeTranslator(RbUtilQt::translators.at(i));
563 // do not delete old translators, this confuses Qt.
565 RbUtilQt::translators.clear();
566 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
567 if(a.size() > 0)
568 language = lang.value(a.at(0)->text());
569 qDebug() << "[Config] new language:" << language;
571 QString applang = QLocale::system().name();
572 QTranslator *translator = new QTranslator(qApp);
573 QTranslator *qttrans = new QTranslator(qApp);
574 QString absolutePath = QCoreApplication::instance()->applicationDirPath();
576 if(!translator->load("rbutil_" + language, absolutePath))
577 translator->load("rbutil_" + language, ":/lang");
578 if(!qttrans->load("qt_" + language,
579 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
580 qttrans->load("qt_" + language, ":/lang");
582 qApp->installTranslator(translator);
583 qApp->installTranslator(qttrans);
584 //: This string is used to indicate the writing direction. Translate it
585 //: to "RTL" (without quotes) for RTL languages. Anything else will get
586 //: treated as LTR language.
587 if(QObject::tr("LTR") == "RTL")
588 qApp->setLayoutDirection(Qt::RightToLeft);
589 else
590 qApp->setLayoutDirection(Qt::LeftToRight);
592 RbUtilQt::translators.append(translator);
593 RbUtilQt::translators.append(qttrans);
595 QLocale::setDefault(language);
600 void Config::browseCache()
602 QString old = ui.cachePath->text();
603 if(!QFileInfo(old).isDir())
604 old = QDir::tempPath();
605 QString c = QFileDialog::getExistingDirectory(this, tr("Set Cache Path"), old);
606 if(c.isEmpty())
607 c = old;
608 else if(!QFileInfo(c).isDir())
609 c = QDir::tempPath();
610 ui.cachePath->setText(QDir::toNativeSeparators(c));
611 updateCacheInfo(c);
615 void Config::refreshMountpoint()
617 // avoid QComboBox to send signals during rebuild to avoid changing to an
618 // unwanted item.
619 ui.mountPoint->blockSignals(true);
620 ui.mountPoint->clear();
621 QStringList mps = Utils::mountpoints();
622 for(int i = 0; i < mps.size(); ++i) {
623 // add mountpoint as user data so we can change the displayed string
624 // later (to include volume label or similar)
625 // Skip unwritable mountpoints, they are not useable for us.
626 if(QFileInfo(mps.at(i)).isWritable()) {
627 QString description = QString("%1 (%2 GiB of %3 GiB free)")
628 .arg(Utils::filesystemName(mps.at(i)))
629 .arg((double)Utils::filesystemFree(mps.at(i))/(1<<30), 0, 'f', 2)
630 .arg((double)Utils::filesystemTotal(mps.at(i))/(1<<30), 0, 'f', 2);
631 ui.mountPoint->addItem(QDir::toNativeSeparators(mps.at(i)), description);
634 if(!mountpoint.isEmpty()) {
635 setMountpoint(mountpoint);
637 ui.mountPoint->blockSignals(false);
641 void Config::updateMountpoint(QString m)
643 if(!m.isEmpty()) {
644 mountpoint = QDir::fromNativeSeparators(m);
645 qDebug() << "[Config] Mountpoint set to" << mountpoint;
650 void Config::updateMountpoint(int idx)
652 if(idx == -1) {
653 return;
655 QString mp = ui.mountPoint->itemText(idx);
656 if(!mp.isEmpty()) {
657 mountpoint = QDir::fromNativeSeparators(mp);
658 qDebug() << "[Config] Mountpoint set to" << mountpoint;
663 void Config::setMountpoint(QString m)
665 if(m.isEmpty()) {
666 return;
668 int index = ui.mountPoint->findText(QDir::toNativeSeparators(m));
669 if(index != -1) {
670 ui.mountPoint->setCurrentIndex(index);
672 else {
673 // keep a mountpoint that is not in the list for convenience (to allow
674 // easier development)
675 ui.mountPoint->addItem(QDir::toNativeSeparators(m));
676 ui.mountPoint->setCurrentIndex(ui.mountPoint->findText(m));
678 qDebug() << "[Config] Mountpoint set to" << mountpoint;
682 void Config::autodetect()
684 Autodetection detector(this);
685 // disable tree during detection as "working" feedback.
686 // TODO: replace the tree view with a splash screen during this time.
687 ui.treeDevices->setEnabled(false);
688 this->setCursor(Qt::WaitCursor);
689 QCoreApplication::processEvents();
691 if(detector.detect()) //let it detect
693 QString devicename = detector.getDevice();
694 // deexpand all items
695 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
696 ui.treeDevices->topLevelItem(a)->setExpanded(false);
697 //deselect the selected item(s)
698 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
699 ui.treeDevices->selectedItems().at(a)->setSelected(false);
701 // find the new item
702 // enumerate all platform items
703 QList<QTreeWidgetItem*> itmList
704 = ui.treeDevices->findItems("*",Qt::MatchWildcard);
705 for(int i=0; i< itmList.size();i++)
707 //enumerate device items
708 for(int j=0;j < itmList.at(i)->childCount();j++)
710 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
711 // unset bold flag
712 QFont f = itmList.at(i)->child(j)->font(0);
713 f.setBold(false);
714 itmList.at(i)->child(j)->setFont(0, f);
716 if(devicename == data) // item found
718 f.setBold(true);
719 itmList.at(i)->child(j)->setFont(0, f);
720 itmList.at(i)->child(j)->setSelected(true); //select the item
721 itmList.at(i)->setExpanded(true); //expand the platform item
722 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
723 ui.treeDevices->scrollToItem(itmList.at(i)->child(j));
724 break;
728 this->unsetCursor();
730 if(!detector.errdev().isEmpty()) {
731 QString text;
732 if(SystemInfo::platformValue(detector.errdev(),
733 SystemInfo::CurBootloaderMethod) == "ipod") {
734 text = tr("%1 \"MacPod\" found!\n"
735 "Rockbox needs a FAT formatted Ipod (so-called \"WinPod\") "
736 "to run. ").arg(SystemInfo::platformValue(
737 detector.errdev(), SystemInfo::CurName).toString());
739 // treat all other errors as MTP device for now.
740 else {
741 text = tr("%1 in MTP mode found!\n"
742 "You need to change your player to MSC mode for installation. ")
743 .arg(SystemInfo::platformValue(detector.errdev(),
744 SystemInfo::CurName).toString());
746 text += tr("Until you change this installation will fail!");
748 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
749 return;
751 if(!detector.incompatdev().isEmpty()) {
752 QString text;
753 text = tr("Detected an unsupported player:\n%1\n"
754 "Sorry, Rockbox doesn't run on your player.")
755 .arg(SystemInfo::platformValue(detector.incompatdev(),
756 SystemInfo::CurName).toString());
758 QMessageBox::critical(this, tr("Fatal: player incompatible"),
759 text, QMessageBox::Ok);
760 return;
763 if(detector.getMountPoint() != "" )
765 setMountpoint(detector.getMountPoint());
767 else
769 QMessageBox::warning(this, tr("Autodetection"),
770 tr("Could not detect a Mountpoint.\n"
771 "Select your Mountpoint manually."),
772 QMessageBox::Ok ,QMessageBox::Ok);
775 else
777 this->unsetCursor();
778 QMessageBox::warning(this, tr("Autodetection"),
779 tr("Could not detect a device.\n"
780 "Select your device and Mountpoint manually."),
781 QMessageBox::Ok ,QMessageBox::Ok);
784 ui.treeDevices->setEnabled(true);
788 void Config::cacheClear()
790 if(QMessageBox::critical(this, tr("Really delete cache?"),
791 tr("Do you really want to delete the cache? "
792 "Make absolutely sure this setting is correct as it will "
793 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
794 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
795 return;
797 QString cache = ui.cachePath->text() + "/rbutil-cache/";
798 if(!QFileInfo(cache).isDir()) {
799 QMessageBox::critical(this, tr("Path wrong!"),
800 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
801 return;
803 QDir dir(cache);
804 QStringList fn;
805 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
807 for(int i = 0; i < fn.size(); i++) {
808 QString f = cache + fn.at(i);
809 QFile::remove(f);
811 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
815 void Config::configTts()
817 int index = ui.comboTts->currentIndex();
818 TTSBase* tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
820 EncTtsCfgGui gui(this,tts,TTSBase::getTTSName(ui.comboTts->itemData(index).toString()));
821 gui.exec();
822 updateTtsState(ui.comboTts->currentIndex());
823 delete tts; /* Config objects are never deleted (in fact, they are
824 leaked..), so we can't rely on QObject, since that would
825 delete the TTSBase instance on application exit */
828 void Config::testTts()
830 QString errstr;
831 int index = ui.comboTts->currentIndex();
832 TTSBase* tts;
834 ui.testTTS->setEnabled(false);
835 tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
836 if(!tts->configOk())
838 QMessageBox::warning(this,tr("TTS configuration invalid"),
839 tr("TTS configuration invalid. \n Please configure TTS engine."));
840 return;
842 if(!tts->start(&errstr))
844 QMessageBox::warning(this,tr("Could not start TTS engine."),
845 tr("Could not start TTS engine.\n") + errstr
846 + tr("\nPlease configure TTS engine."));
847 ui.testTTS->setEnabled(true);
848 return;
851 QString filename;
852 QTemporaryFile file(this);
853 // keep filename empty if the TTS can do speaking for itself.
854 if(!(tts->capabilities() & TTSBase::CanSpeak)) {
855 file.open();
856 filename = file.fileName();
857 file.close();
860 if(tts->voice(tr("Rockbox Utility Voice Test"),filename,&errstr) == FatalError)
862 tts->stop();
863 QMessageBox::warning(this,tr("Could not voice test string."),
864 tr("Could not voice test string.\n") + errstr
865 + tr("\nPlease configure TTS engine."));
866 ui.testTTS->setEnabled(false);
867 return;
869 tts->stop();
870 if(!filename.isEmpty()) {
871 #if defined(Q_OS_LINUX)
872 QString exe = Utils::findExecutable("aplay");
873 if(exe == "") exe = Utils::findExecutable("play");
874 if(exe != "")
876 QProcess::execute(exe+" "+filename);
878 #else
879 QSound::play(filename);
880 #endif
882 ui.testTTS->setEnabled(true);
883 delete tts; /* Config objects are never deleted (in fact, they are
884 leaked..), so we can't rely on QObject, since that would
885 delete the TTSBase instance on application exit */
888 void Config::configEnc()
890 if(ui.treeDevices->selectedItems().size() == 0)
891 return;
893 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
894 QString encoder = SystemInfo::platformValue(devname,
895 SystemInfo::CurEncoder).toString();
896 ui.encoderName->setText(EncBase::getEncoderName(SystemInfo::platformValue(devname,
897 SystemInfo::CurEncoder).toString()));
900 EncBase* enc = EncBase::getEncoder(this,encoder);
902 EncTtsCfgGui gui(this,enc,EncBase::getEncoderName(encoder));
903 gui.exec();
905 updateEncState();
909 void Config::changeEvent(QEvent *e)
911 if(e->type() == QEvent::LanguageChange) {
912 ui.retranslateUi(this);
913 updateCacheInfo(ui.cachePath->text());
914 } else {
915 QWidget::changeEvent(e);