Properly export sleep_timer_call from main_menu.c in exported_menus.h
[kugel-rb.git] / rbutil / rbutilqt / configure.cpp
blob9e1978974fd5ac20f8cb3629e78c989c21f7ac4f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id$
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 ****************************************************************************/
20 #include <QtGui>
22 #include "version.h"
23 #include "configure.h"
24 #include "autodetection.h"
25 #include "ui_configurefrm.h"
26 #include "encoders.h"
27 #include "ttsbase.h"
28 #include "system.h"
29 #include "encttscfggui.h"
30 #include "rbsettings.h"
31 #include "serverinfo.h"
32 #include "systeminfo.h"
33 #include "utils.h"
34 #include "comboboxviewdelegate.h"
35 #if defined(Q_OS_WIN32)
36 #if defined(UNICODE)
37 #define _UNICODE
38 #endif
39 #include <tchar.h>
40 #include <windows.h>
41 #endif
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 // language
151 if(RbSettings::value(RbSettings::Language).toString() != language
152 && !language.isEmpty()) {
153 QMessageBox::information(this, tr("Language changed"),
154 tr("You need to restart the application for the changed language "
155 "to take effect."));
157 RbSettings::setValue(RbSettings::Language, language);
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 int i = ui.comboTts->currentIndex();
209 RbSettings::setValue(RbSettings::Tts, ui.comboTts->itemData(i).toString());
211 RbSettings::setValue(RbSettings::RbutilVersion, PUREVERSION);
213 errormsg += "</ul>";
214 errormsg += tr("You need to fix the above errors before you can continue.");
216 if(error) {
217 QMessageBox::critical(this, tr("Configuration error"), errormsg);
219 else {
220 // sync settings
221 RbSettings::sync();
222 this->close();
223 emit settingsUpdated();
228 void Config::abort()
230 qDebug() << "[Config] aborted.";
231 this->close();
235 void Config::setUserSettings()
237 // set proxy
238 proxy.setEncodedUrl(RbSettings::value(RbSettings::Proxy).toByteArray());
240 if(proxy.port() > 0)
241 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
242 else ui.proxyPort->setText("");
243 ui.proxyHost->setText(proxy.host());
244 ui.proxyUser->setText(proxy.userName());
245 ui.proxyPass->setText(proxy.password());
247 QString proxyType = RbSettings::value(RbSettings::ProxyType).toString();
248 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
249 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
250 else ui.radioNoProxy->setChecked(true);
252 // set language selection
253 QList<QListWidgetItem*> a;
254 QString b;
255 // find key for lang value
256 QMap<QString, QString>::const_iterator i = lang.constBegin();
257 QString l = RbSettings::value(RbSettings::Language).toString();
258 if(l.isEmpty())
259 l = QLocale::system().name();
260 while (i != lang.constEnd()) {
261 if(i.value() == l) {
262 b = i.key();
263 break;
265 else if(l.startsWith(i.value(), Qt::CaseInsensitive)) {
266 // check if there is a base language (en -> en_US, etc.)
267 b = i.key();
268 break;
270 i++;
272 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
273 if(a.size() > 0)
274 ui.listLanguages->setCurrentItem(a.at(0));
275 // don't connect before language list has been set up to prevent
276 // triggering the signal by selecting the saved language.
277 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
279 // devices tab
280 refreshMountpoint();
281 mountpoint = QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint).toString());
282 setMountpoint(mountpoint);
284 // cache tab
285 if(!QFileInfo(RbSettings::value(RbSettings::CachePath).toString()).isDir())
286 RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
287 ui.cachePath->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath).toString()));
288 ui.cacheDisable->setChecked(RbSettings::value(RbSettings::CacheDisabled).toBool());
289 ui.cacheOfflineMode->setChecked(RbSettings::value(RbSettings::CacheOffline).toBool());
290 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
294 void Config::updateCacheInfo(QString path)
296 QList<QFileInfo> fs;
297 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
298 qint64 sz = 0;
299 for(int i = 0; i < fs.size(); i++) {
300 sz += fs.at(i).size();
302 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
303 .arg(sz/1024));
307 void Config::showDisabled(bool show)
309 qDebug() << "[Config] disabled targets shown:" << show;
310 if(show)
311 QMessageBox::warning(this, tr("Showing disabled targets"),
312 tr("You just enabled showing targets that are marked disabled. "
313 "Disabled targets are not recommended to end users. Please "
314 "use this option only if you know what you are doing."));
315 setDevices();
320 void Config::setDevices()
323 // setup devices table
324 qDebug() << "[Config] setting up devices list";
326 QStringList platformList;
327 if(ui.showDisabled->isChecked())
328 platformList = SystemInfo::platforms(SystemInfo::PlatformAllDisabled);
329 else
330 platformList = SystemInfo::platforms(SystemInfo::PlatformAll);
332 QMap <QString, QString> manuf;
333 for(int it = 0; it < platformList.size(); it++)
335 QString curbrand = SystemInfo::platformValue(platformList.at(it),
336 SystemInfo::CurBrand).toString();
337 manuf.insertMulti(curbrand, platformList.at(it));
340 // set up devices table
341 ui.treeDevices->header()->hide();
342 ui.treeDevices->expandAll();
343 ui.treeDevices->setColumnCount(1);
344 QList<QTreeWidgetItem *> items;
346 // get manufacturers
347 QStringList brands = manuf.uniqueKeys();
348 QTreeWidgetItem *w;
349 QTreeWidgetItem *w2;
350 QTreeWidgetItem *w3 = 0;
352 QString selected = RbSettings::value(RbSettings::Platform).toString();
353 for(int c = 0; c < brands.size(); c++) {
354 w = new QTreeWidgetItem();
355 w->setFlags(Qt::ItemIsEnabled);
356 w->setText(0, brands.at(c));
357 items.append(w);
358 // go through platforms and add all players matching the current brand
359 for(int it = 0; it < platformList.size(); it++) {
360 // skip if not current brand
361 if(!manuf.values(brands.at(c)).contains(platformList.at(it)))
362 continue;
363 // construct display name
364 QString curname = SystemInfo::platformValue(platformList.at(it),
365 SystemInfo::CurName).toString() +
366 " (" +ServerInfo::platformValue(platformList.at(it),
367 ServerInfo::CurStatus).toString() +")";
368 qDebug() << "[Config] add supported device:" << brands.at(c) << curname;
369 w2 = new QTreeWidgetItem(w, QStringList(curname));
370 w2->setData(0, Qt::UserRole, platformList.at(it));
372 if(platformList.at(it) == selected) {
373 w2->setSelected(true);
374 w->setExpanded(true);
375 w3 = w2; // save pointer to hilight old selection
377 items.append(w2);
380 // remove any old items in list
381 QTreeWidgetItem* widgetitem;
382 do {
383 widgetitem = ui.treeDevices->takeTopLevelItem(0);
384 delete widgetitem;
386 while(widgetitem);
387 // add new items
388 ui.treeDevices->insertTopLevelItems(0, items);
389 if(w3 != 0) {
390 ui.treeDevices->setCurrentItem(w3); // hilight old selection
391 ui.treeDevices->scrollToItem(w3);
394 // tts / encoder tab
396 //encoders
397 updateEncState();
399 //tts
400 QStringList ttslist = TTSBase::getTTSList();
401 for(int a = 0; a < ttslist.size(); a++)
402 ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
403 //update index of combobox
404 int index = ui.comboTts->findData(RbSettings::value(RbSettings::Tts).toString());
405 if(index < 0) index = 0;
406 ui.comboTts->setCurrentIndex(index);
407 updateTtsState(index);
412 void Config::updateTtsState(int index)
414 QString ttsName = ui.comboTts->itemData(index).toString();
415 TTSBase* tts = TTSBase::getTTS(this,ttsName);
417 if(tts->configOk())
419 ui.configTTSstatus->setText(tr("Configuration OK"));
420 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
421 ui.testTTS->setEnabled(true);
423 else
425 ui.configTTSstatus->setText(tr("Configuration INVALID"));
426 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
427 ui.testTTS->setEnabled(false);
430 delete tts; /* Config objects are never deleted (in fact, they are leaked..), so we can't rely on QObject,
431 since that would delete the TTSBase instance on application exit*/
434 void Config::updateEncState()
436 if(ui.treeDevices->selectedItems().size() == 0)
437 return;
439 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
440 QString encoder = SystemInfo::platformValue(devname,
441 SystemInfo::CurEncoder).toString();
442 ui.encoderName->setText(EncBase::getEncoderName(SystemInfo::platformValue(devname,
443 SystemInfo::CurEncoder).toString()));
445 EncBase* enc = EncBase::getEncoder(this,encoder);
447 if(enc->configOk())
449 ui.configEncstatus->setText(tr("Configuration OK"));
450 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
452 else
454 ui.configEncstatus->setText(tr("Configuration INVALID"));
455 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
460 void Config::setNoProxy(bool checked)
462 bool i = !checked;
463 ui.proxyPort->setEnabled(i);
464 ui.proxyHost->setEnabled(i);
465 ui.proxyUser->setEnabled(i);
466 ui.proxyPass->setEnabled(i);
470 void Config::setSystemProxy(bool checked)
472 ui.proxyPort->setEnabled(!checked);
473 ui.proxyHost->setEnabled(!checked);
474 ui.proxyUser->setEnabled(!checked);
475 ui.proxyPass->setEnabled(!checked);
476 if(checked) {
477 // save values in input box
478 proxy.setScheme("http");
479 proxy.setUserName(ui.proxyUser->text());
480 proxy.setPassword(ui.proxyPass->text());
481 proxy.setHost(ui.proxyHost->text());
482 proxy.setPort(ui.proxyPort->text().toInt());
483 // show system values in input box
484 QUrl envproxy = System::systemProxy();
485 qDebug() << "[Config] setting system proxy" << envproxy;
487 ui.proxyHost->setText(envproxy.host());
488 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
489 ui.proxyUser->setText(envproxy.userName());
490 ui.proxyPass->setText(envproxy.password());
492 if(envproxy.host().isEmpty() || envproxy.port() == -1) {
493 qDebug() << "[Config] sytem proxy is invalid.";
494 QMessageBox::warning(this, tr("Proxy Detection"),
495 tr("The System Proxy settings are invalid!\n"
496 "Rockbox Utility can't work with this proxy settings. "
497 "Make sure the system proxy is set correctly. Note that "
498 "\"proxy auto-config (PAC)\" scripts are not supported by "
499 "Rockbox Utility. If your system uses this you need "
500 "to use manual proxy settings."),
501 QMessageBox::Ok ,QMessageBox::Ok);
502 // the current proxy settings are invalid. Check the saved proxy
503 // type again.
504 if(RbSettings::value(RbSettings::ProxyType).toString() == "manual")
505 ui.radioManualProxy->setChecked(true);
506 else
507 ui.radioNoProxy->setChecked(true);
511 else {
512 ui.proxyHost->setText(proxy.host());
513 if(proxy.port() > 0)
514 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
515 else ui.proxyPort->setText("");
516 ui.proxyUser->setText(proxy.userName());
517 ui.proxyPass->setText(proxy.password());
523 QStringList Config::findLanguageFiles()
525 QDir dir(programPath);
526 QStringList fileNames;
527 QStringList langs;
528 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
530 QDir resDir(":/lang");
531 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
533 QRegExp exp("^rbutil_(.*)\\.qm");
534 for(int i = 0; i < fileNames.size(); i++) {
535 QString a = fileNames.at(i);
536 a.replace(exp, "\\1");
537 langs.append(a);
539 langs.sort();
540 qDebug() << "[Config] available lang files:" << langs;
542 return langs;
546 QString Config::languageName(const QString &qmFile)
548 QTranslator translator;
550 QString file = "rbutil_" + qmFile;
551 if(!translator.load(file, programPath))
552 translator.load(file, ":/lang");
554 return translator.translate("Configure", "English",
555 "This is the localized language name, i.e. your language.");
559 void Config::updateLanguage()
561 qDebug() << "[Config] update selected language";
562 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
563 if(a.size() > 0)
564 language = lang.value(a.at(0)->text());
565 qDebug() << "[Config] new language:" << language;
569 void Config::browseCache()
571 QString old = ui.cachePath->text();
572 if(!QFileInfo(old).isDir())
573 old = QDir::tempPath();
574 QString c = QFileDialog::getExistingDirectory(this, tr("Set Cache Path"), old);
575 if(c.isEmpty())
576 c = old;
577 else if(!QFileInfo(c).isDir())
578 c = QDir::tempPath();
579 ui.cachePath->setText(QDir::toNativeSeparators(c));
580 updateCacheInfo(c);
584 void Config::refreshMountpoint()
586 // avoid QComboBox to send signals during rebuild to avoid changing to an
587 // unwanted item.
588 ui.mountPoint->blockSignals(true);
589 ui.mountPoint->clear();
590 QStringList mps = Utils::mountpoints();
591 for(int i = 0; i < mps.size(); ++i) {
592 // add mountpoint as user data so we can change the displayed string
593 // later (to include volume label or similar)
594 // Skip unwritable mountpoints, they are not useable for us.
595 if(QFileInfo(mps.at(i)).isWritable()) {
596 QString description = QString("%1 (%2 GiB of %3 GiB free)")
597 .arg(Utils::filesystemName(mps.at(i)))
598 .arg((double)Utils::filesystemFree(mps.at(i))/(1<<30), 0, 'f', 2)
599 .arg((double)Utils::filesystemTotal(mps.at(i))/(1<<30), 0, 'f', 2);
600 ui.mountPoint->addItem(QDir::toNativeSeparators(mps.at(i)), description);
603 if(!mountpoint.isEmpty()) {
604 setMountpoint(mountpoint);
606 ui.mountPoint->blockSignals(false);
610 void Config::updateMountpoint(QString m)
612 if(!m.isEmpty()) {
613 mountpoint = QDir::fromNativeSeparators(m);
614 qDebug() << "[Config] Mountpoint set to" << mountpoint;
619 void Config::updateMountpoint(int idx)
621 if(idx == -1) {
622 return;
624 QString mp = ui.mountPoint->itemText(idx);
625 if(!mp.isEmpty()) {
626 mountpoint = QDir::fromNativeSeparators(mp);
627 qDebug() << "[Config] Mountpoint set to" << mountpoint;
632 void Config::setMountpoint(QString m)
634 if(m.isEmpty()) {
635 return;
637 int index = ui.mountPoint->findText(QDir::toNativeSeparators(m));
638 if(index != -1) {
639 ui.mountPoint->setCurrentIndex(index);
641 else {
642 // keep a mountpoint that is not in the list for convenience (to allow
643 // easier development)
644 ui.mountPoint->addItem(QDir::toNativeSeparators(m));
645 ui.mountPoint->setCurrentIndex(ui.mountPoint->findText(m));
647 qDebug() << "[Config] Mountpoint set to" << mountpoint;
651 void Config::autodetect()
653 Autodetection detector(this);
654 // disable tree during detection as "working" feedback.
655 // TODO: replace the tree view with a splash screen during this time.
656 ui.treeDevices->setEnabled(false);
657 this->setCursor(Qt::WaitCursor);
658 QCoreApplication::processEvents();
660 if(detector.detect()) //let it detect
662 QString devicename = detector.getDevice();
663 // deexpand all items
664 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
665 ui.treeDevices->topLevelItem(a)->setExpanded(false);
666 //deselect the selected item(s)
667 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
668 ui.treeDevices->selectedItems().at(a)->setSelected(false);
670 // find the new item
671 // enumerate all platform items
672 QList<QTreeWidgetItem*> itmList
673 = ui.treeDevices->findItems("*",Qt::MatchWildcard);
674 for(int i=0; i< itmList.size();i++)
676 //enumerate device items
677 for(int j=0;j < itmList.at(i)->childCount();j++)
679 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
680 // unset bold flag
681 QFont f = itmList.at(i)->child(j)->font(0);
682 f.setBold(false);
683 itmList.at(i)->child(j)->setFont(0, f);
685 if(devicename == data) // item found
687 f.setBold(true);
688 itmList.at(i)->child(j)->setFont(0, f);
689 itmList.at(i)->child(j)->setSelected(true); //select the item
690 itmList.at(i)->setExpanded(true); //expand the platform item
691 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
692 ui.treeDevices->scrollToItem(itmList.at(i)->child(j));
693 break;
697 this->unsetCursor();
699 if(!detector.errdev().isEmpty()) {
700 QString text;
701 if(SystemInfo::platformValue(detector.errdev(),
702 SystemInfo::CurBootloaderMethod) == "ipod") {
703 text = tr("%1 \"MacPod\" found!\n"
704 "Rockbox needs a FAT formatted Ipod (so-called \"WinPod\") "
705 "to run. ").arg(SystemInfo::platformValue(
706 detector.errdev(), SystemInfo::CurName).toString());
708 // treat all other errors as MTP device for now.
709 else {
710 text = tr("%1 in MTP mode found!\n"
711 "You need to change your player to MSC mode for installation. ")
712 .arg(SystemInfo::platformValue(detector.errdev(),
713 SystemInfo::CurName).toString());
715 text += tr("Until you change this installation will fail!");
717 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
718 return;
720 if(!detector.incompatdev().isEmpty()) {
721 QString text;
722 text = tr("Detected an unsupported player:\n%1\n"
723 "Sorry, Rockbox doesn't run on your player.")
724 .arg(SystemInfo::platformValue(detector.incompatdev(),
725 SystemInfo::CurName).toString());
727 QMessageBox::critical(this, tr("Fatal: player incompatible"),
728 text, QMessageBox::Ok);
729 return;
732 if(detector.getMountPoint() != "" )
734 setMountpoint(detector.getMountPoint());
736 else
738 QMessageBox::warning(this, tr("Autodetection"),
739 tr("Could not detect a Mountpoint.\n"
740 "Select your Mountpoint manually."),
741 QMessageBox::Ok ,QMessageBox::Ok);
744 else
746 this->unsetCursor();
747 QMessageBox::warning(this, tr("Autodetection"),
748 tr("Could not detect a device.\n"
749 "Select your device and Mountpoint manually."),
750 QMessageBox::Ok ,QMessageBox::Ok);
753 ui.treeDevices->setEnabled(true);
757 void Config::cacheClear()
759 if(QMessageBox::critical(this, tr("Really delete cache?"),
760 tr("Do you really want to delete the cache? "
761 "Make absolutely sure this setting is correct as it will "
762 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
763 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
764 return;
766 QString cache = ui.cachePath->text() + "/rbutil-cache/";
767 if(!QFileInfo(cache).isDir()) {
768 QMessageBox::critical(this, tr("Path wrong!"),
769 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
770 return;
772 QDir dir(cache);
773 QStringList fn;
774 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
776 for(int i = 0; i < fn.size(); i++) {
777 QString f = cache + fn.at(i);
778 QFile::remove(f);
780 updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
784 void Config::configTts()
786 int index = ui.comboTts->currentIndex();
787 TTSBase* tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
789 EncTtsCfgGui gui(this,tts,TTSBase::getTTSName(ui.comboTts->itemData(index).toString()));
790 gui.exec();
791 updateTtsState(ui.comboTts->currentIndex());
792 delete tts; /* Config objects are never deleted (in fact, they are
793 leaked..), so we can't rely on QObject, since that would
794 delete the TTSBase instance on application exit */
797 void Config::testTts()
799 QString errstr;
800 int index = ui.comboTts->currentIndex();
801 TTSBase* tts;
803 ui.testTTS->setEnabled(false);
804 tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
805 if(!tts->configOk())
807 QMessageBox::warning(this,tr("TTS configuration invalid"),
808 tr("TTS configuration invalid. \n Please configure TTS engine."));
809 return;
811 if(!tts->start(&errstr))
813 QMessageBox::warning(this,tr("Could not start TTS engine."),
814 tr("Could not start TTS engine.\n") + errstr
815 + tr("\nPlease configure TTS engine."));
816 ui.testTTS->setEnabled(true);
817 return;
820 QString filename;
821 QTemporaryFile file(this);
822 // keep filename empty if the TTS can do speaking for itself.
823 if(!(tts->capabilities() & TTSBase::CanSpeak)) {
824 file.open();
825 filename = file.fileName();
826 file.close();
829 if(tts->voice(tr("Rockbox Utility Voice Test"),filename,&errstr) == FatalError)
831 tts->stop();
832 QMessageBox::warning(this,tr("Could not voice test string."),
833 tr("Could not voice test string.\n") + errstr
834 + tr("\nPlease configure TTS engine."));
835 ui.testTTS->setEnabled(false);
836 return;
838 tts->stop();
839 if(!filename.isEmpty()) {
840 #if defined(Q_OS_LINUX)
841 QString exe = Utils::findExecutable("aplay");
842 if(exe == "") exe = Utils::findExecutable("play");
843 if(exe != "")
845 QProcess::execute(exe+" "+filename);
847 #else
848 QSound::play(filename);
849 #endif
851 ui.testTTS->setEnabled(true);
852 delete tts; /* Config objects are never deleted (in fact, they are
853 leaked..), so we can't rely on QObject, since that would
854 delete the TTSBase instance on application exit */
857 void Config::configEnc()
859 if(ui.treeDevices->selectedItems().size() == 0)
860 return;
862 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
863 QString encoder = SystemInfo::platformValue(devname,
864 SystemInfo::CurEncoder).toString();
865 ui.encoderName->setText(EncBase::getEncoderName(SystemInfo::platformValue(devname,
866 SystemInfo::CurEncoder).toString()));
869 EncBase* enc = EncBase::getEncoder(this,encoder);
871 EncTtsCfgGui gui(this,enc,EncBase::getEncoderName(encoder));
872 gui.exec();
874 updateEncState();