Replaced TAB characters by spaces in sansapatcher
[kugel-rb.git] / rbutil / rbutilqt / configure.cpp
blob1589477cd73209124ce2942b9f85f21866bc0c35
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 "browsedirtree.h"
27 #include "encoders.h"
28 #include "tts.h"
29 #include "detect.h"
31 #include <stdio.h>
32 #if defined(Q_OS_WIN32)
33 #if defined(UNICODE)
34 #define _UNICODE
35 #endif
36 #include <tchar.h>
37 #include <windows.h>
38 #endif
40 #define DEFAULT_LANG "English (en)"
41 #define DEFAULT_LANG_CODE "en"
43 Config::Config(QWidget *parent,int index) : QDialog(parent)
45 programPath = qApp->applicationDirPath() + "/";
46 ui.setupUi(this);
47 ui.tabConfiguration->setCurrentIndex(index);
48 ui.radioManualProxy->setChecked(true);
49 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
50 QRegExp validate("[0-9]*");
51 proxyValidator->setRegExp(validate);
52 ui.proxyPort->setValidator(proxyValidator);
53 #if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN32)
54 ui.radioSystemProxy->setEnabled(false); // not on macox for now
55 #endif
56 // build language list and sort alphabetically
57 QStringList langs = findLanguageFiles();
58 for(int i = 0; i < langs.size(); ++i)
59 lang.insert(languageName(langs.at(i))
60 + QString(" (%1)").arg(langs.at(i)), langs.at(i));
61 lang.insert(DEFAULT_LANG, DEFAULT_LANG_CODE);
62 QMap<QString, QString>::const_iterator i = lang.constBegin();
63 while (i != lang.constEnd()) {
64 ui.listLanguages->addItem(i.key());
65 i++;
67 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
68 ui.proxyPass->setEchoMode(QLineEdit::Password);
69 ui.treeDevices->setAlternatingRowColors(true);
70 ui.listLanguages->setAlternatingRowColors(true);
72 this->setModal(true);
74 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
75 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
76 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
77 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
78 connect(ui.browseMountPoint, SIGNAL(clicked()), this, SLOT(browseFolder()));
79 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
80 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
81 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
82 connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
83 connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
84 connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
85 connect(ui.treeDevices, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
91 void Config::accept()
93 qDebug() << "Config::accept()";
94 // proxy: save entered proxy values, not displayed.
95 if(ui.radioManualProxy->isChecked()) {
96 proxy.setScheme("http");
97 proxy.setUserName(ui.proxyUser->text());
98 proxy.setPassword(ui.proxyPass->text());
99 proxy.setHost(ui.proxyHost->text());
100 proxy.setPort(ui.proxyPort->text().toInt());
103 settings->setProxy(proxy.toString());
104 qDebug() << "new proxy:" << proxy;
105 // proxy type
106 QString proxyType;
107 if(ui.radioNoProxy->isChecked()) proxyType = "none";
108 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
109 else proxyType = "manual";
110 settings->setProxyType(proxyType);
112 // language
113 if(settings->curLang() != language && !language.isEmpty()) {
114 QMessageBox::information(this, tr("Language changed"),
115 tr("You need to restart the application for the changed language to take effect."));
116 settings->setLang(language);
119 // mountpoint
120 QString mp = ui.mountPoint->text();
121 if(QFileInfo(mp).isDir())
122 settings->setMountpoint(QDir::fromNativeSeparators(mp));
124 // platform
125 QString nplat;
126 if(ui.treeDevices->selectedItems().size() != 0) {
127 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
128 settings->setCurPlatform(nplat);
131 // cache settings
132 if(QFileInfo(ui.cachePath->text()).isDir())
133 settings->setCachePath(ui.cachePath->text());
134 else // default to system temp path
135 settings->setCachePath( QDir::tempPath());
136 settings->setCacheDisable(ui.cacheDisable->isChecked());
137 settings->setCacheOffline(ui.cacheOfflineMode->isChecked());
139 // tts settings
140 int i = ui.comboTts->currentIndex();
141 settings->setCurTTS(ui.comboTts->itemData(i).toString());
143 settings->setCurVersion(PUREVERSION);
145 // sync settings
146 settings->sync();
147 this->close();
148 emit settingsUpdated();
152 void Config::abort()
154 qDebug() << "Config::abort()";
155 this->close();
158 void Config::setSettings(RbSettings* sett)
160 settings = sett;
162 setUserSettings();
163 setDevices();
166 void Config::setUserSettings()
168 // set proxy
169 proxy = settings->proxy();
171 if(proxy.port() > 0)
172 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
173 else ui.proxyPort->setText("");
174 ui.proxyHost->setText(proxy.host());
175 ui.proxyUser->setText(proxy.userName());
176 ui.proxyPass->setText(proxy.password());
178 QString proxyType = settings->proxyType();
179 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
180 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
181 else ui.radioNoProxy->setChecked(true);
183 // set language selection
184 QList<QListWidgetItem*> a;
185 QString b;
186 // find key for lang value
187 QMap<QString, QString>::const_iterator i = lang.constBegin();
188 while (i != lang.constEnd()) {
189 if(i.value() == settings->curLang()) {
190 b = i.key();
191 break;
193 else if(settings->curLang().startsWith(i.value(), Qt::CaseInsensitive)) {
194 // check if there is a base language (en -> en_US, etc.)
195 b = i.key();
196 break;
198 i++;
200 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
201 if(a.size() > 0)
202 ui.listLanguages->setCurrentItem(a.at(0));
203 // don't connect before language list has been set up to prevent
204 // triggering the signal by selecting the saved language.
205 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
207 // devices tab
208 ui.mountPoint->setText(QDir::toNativeSeparators(settings->mountpoint()));
210 // cache tab
211 if(!QFileInfo(settings->cachePath()).isDir())
212 settings->setCachePath(QDir::tempPath());
213 ui.cachePath->setText(settings->cachePath());
214 ui.cacheDisable->setChecked(settings->cacheDisabled());
215 ui.cacheOfflineMode->setChecked(settings->cacheOffline());
216 updateCacheInfo(settings->cachePath());
220 void Config::updateCacheInfo(QString path)
222 QList<QFileInfo> fs;
223 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
224 qint64 sz = 0;
225 for(int i = 0; i < fs.size(); i++) {
226 sz += fs.at(i).size();
227 qDebug() << fs.at(i).fileName() << fs.at(i).size();
229 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
230 .arg(sz/1024));
234 void Config::setDevices()
237 // setup devices table
238 qDebug() << "Config::setDevices()";
240 QStringList platformList = settings->allPlatforms();
242 QMap <QString, QString> manuf;
243 QMap <QString, QString> devcs;
244 for(int it = 0; it < platformList.size(); it++)
246 QString curname = settings->name(platformList.at(it));
247 QString curbrand = settings->brand(platformList.at(it));
248 manuf.insertMulti(curbrand, platformList.at(it));
249 devcs.insert(platformList.at(it), curname);
252 QString platform;
253 platform = devcs.value(settings->curPlatform());
255 // set up devices table
256 ui.treeDevices->header()->hide();
257 ui.treeDevices->expandAll();
258 ui.treeDevices->setColumnCount(1);
259 QList<QTreeWidgetItem *> items;
261 // get manufacturers
262 QStringList brands = manuf.uniqueKeys();
263 QTreeWidgetItem *w;
264 QTreeWidgetItem *w2;
265 QTreeWidgetItem *w3 = 0;
266 for(int c = 0; c < brands.size(); c++) {
267 qDebug() << brands.at(c);
268 w = new QTreeWidgetItem();
269 w->setFlags(Qt::ItemIsEnabled);
270 w->setText(0, brands.at(c));
271 items.append(w);
273 // go through platforms again for sake of order
274 for(int it = 0; it < platformList.size(); it++) {
276 QString curname = settings->name(platformList.at(it));
277 QString curbrand = settings->brand(platformList.at(it));
279 if(curbrand != brands.at(c)) continue;
280 qDebug() << "adding:" << brands.at(c) << curname;
281 w2 = new QTreeWidgetItem(w, QStringList(curname));
282 w2->setData(0, Qt::UserRole, platformList.at(it));
284 if(platform.contains(curname)) {
285 w2->setSelected(true);
286 w->setExpanded(true);
287 w3 = w2; // save pointer to hilight old selection
289 items.append(w2);
292 ui.treeDevices->insertTopLevelItems(0, items);
293 if(w3 != 0)
294 ui.treeDevices->setCurrentItem(w3); // hilight old selection
296 // tts / encoder tab
298 //encoders
299 updateEncState();
301 //tts
302 QStringList ttslist = TTSBase::getTTSList();
303 for(int a = 0; a < ttslist.size(); a++)
304 ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
305 //update index of combobox
306 int index = ui.comboTts->findData(settings->curTTS());
307 if(index < 0) index = 0;
308 ui.comboTts->setCurrentIndex(index);
309 updateTtsState(index);
314 void Config::updateTtsState(int index)
316 QString ttsName = ui.comboTts->itemData(index).toString();
317 TTSBase* tts = TTSBase::getTTS(ttsName);
318 tts->setCfg(settings);
320 if(tts->configOk())
322 ui.configTTSstatus->setText(tr("Configuration OK"));
323 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
325 else
327 ui.configTTSstatus->setText(tr("Configuration INVALID"));
328 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
332 void Config::updateEncState()
334 // FIXME: this is a workaround to make the encoder follow the device selection
335 // even with the settings (and thus the device) being saved. Needs to be redone
336 // properly later by extending the settings object
337 if(ui.treeDevices->selectedItems().size() == 0)
338 return;
340 QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
341 QString olddevice = settings->curPlatform();
342 settings->setCurPlatform(devname);
343 QString encoder = settings->curEncoder();
344 ui.encoderName->setText(EncBase::getEncoderName(settings->curEncoder()));
345 settings->setCurPlatform(olddevice);
347 EncBase* enc = EncBase::getEncoder(encoder);
348 enc->setCfg(settings);
350 if(enc->configOk())
352 ui.configEncstatus->setText(tr("Configuration OK"));
353 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
355 else
357 ui.configEncstatus->setText(tr("Configuration INVALID"));
358 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
362 void Config::setNoProxy(bool checked)
364 bool i = !checked;
365 ui.proxyPort->setEnabled(i);
366 ui.proxyHost->setEnabled(i);
367 ui.proxyUser->setEnabled(i);
368 ui.proxyPass->setEnabled(i);
372 void Config::setSystemProxy(bool checked)
374 bool i = !checked;
375 ui.proxyPort->setEnabled(i);
376 ui.proxyHost->setEnabled(i);
377 ui.proxyUser->setEnabled(i);
378 ui.proxyPass->setEnabled(i);
379 if(checked) {
380 // save values in input box
381 proxy.setScheme("http");
382 proxy.setUserName(ui.proxyUser->text());
383 proxy.setPassword(ui.proxyPass->text());
384 proxy.setHost(ui.proxyHost->text());
385 proxy.setPort(ui.proxyPort->text().toInt());
386 // show system values in input box
387 QUrl envproxy = Detect::systemProxy();
389 ui.proxyHost->setText(envproxy.host());
391 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
392 ui.proxyUser->setText(envproxy.userName());
393 ui.proxyPass->setText(envproxy.password());
396 else {
397 ui.proxyHost->setText(proxy.host());
398 if(proxy.port() > 0)
399 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
400 else ui.proxyPort->setText("");
401 ui.proxyUser->setText(proxy.userName());
402 ui.proxyPass->setText(proxy.password());
408 QStringList Config::findLanguageFiles()
410 QDir dir(programPath);
411 QStringList fileNames;
412 QStringList langs;
413 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
415 QDir resDir(":/lang");
416 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
418 QRegExp exp("^rbutil_(.*)\\.qm");
419 for(int i = 0; i < fileNames.size(); i++) {
420 QString a = fileNames.at(i);
421 a.replace(exp, "\\1");
422 langs.append(a);
424 langs.sort();
425 qDebug() << "Config::findLanguageFiles()" << langs;
427 return langs;
431 QString Config::languageName(const QString &qmFile)
433 QTranslator translator;
435 QString file = "rbutil_" + qmFile;
436 if(!translator.load(file, programPath))
437 translator.load(file, ":/lang");
439 return translator.translate("Configure", "English",
440 "This is the localized language name, i.e. your language.");
444 void Config::updateLanguage()
446 qDebug() << "updateLanguage()";
447 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
448 if(a.size() > 0)
449 language = lang.value(a.at(0)->text());
450 qDebug() << language;
454 void Config::browseFolder()
456 browser = new BrowseDirtree(this,tr("Select your device"));
457 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
458 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
459 #elif defined(Q_OS_WIN32)
460 browser->setFilter(QDir::Drives);
461 #endif
462 #if defined(Q_OS_MACX)
463 browser->setRoot("/Volumes");
464 #elif defined(Q_OS_LINUX)
465 browser->setDir("/media");
466 #endif
467 if( ui.mountPoint->text() != "" )
469 browser->setDir(ui.mountPoint->text());
471 browser->show();
472 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
476 void Config::browseCache()
478 cbrowser = new BrowseDirtree(this);
479 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
480 cbrowser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
481 #elif defined(Q_OS_WIN32)
482 cbrowser->setFilter(QDir::Drives);
483 #endif
484 cbrowser->setDir(ui.cachePath->text());
485 connect(cbrowser, SIGNAL(itemChanged(QString)), this, SLOT(setCache(QString)));
486 cbrowser->show();
490 void Config::setMountpoint(QString m)
492 ui.mountPoint->setText(m);
496 void Config::setCache(QString c)
498 ui.cachePath->setText(c);
499 updateCacheInfo(c);
503 void Config::autodetect()
505 Autodetection detector(this);
506 detector.setSettings(settings);
507 // disable tree during detection as "working" feedback.
508 // TODO: replace the tree view with a splash screen during this time.
509 ui.treeDevices->setEnabled(false);
510 QCoreApplication::processEvents();
512 if(detector.detect()) //let it detect
514 QString devicename = detector.getDevice();
515 // deexpand all items
516 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
517 ui.treeDevices->topLevelItem(a)->setExpanded(false);
518 //deselect the selected item(s)
519 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
520 ui.treeDevices->selectedItems().at(a)->setSelected(false);
522 // find the new item
523 // enumerate all platform items
524 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
525 for(int i=0; i< itmList.size();i++)
527 //enumerate device items
528 for(int j=0;j < itmList.at(i)->childCount();j++)
530 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
532 if(devicename == data) // item found
534 itmList.at(i)->child(j)->setSelected(true); //select the item
535 itmList.at(i)->setExpanded(true); //expand the platform item
536 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
537 break;
542 if(!detector.errdev().isEmpty()) {
543 QString text;
544 if(detector.errdev() == "sansae200")
545 text = tr("Sansa e200 in MTP mode found!\n"
546 "You need to change your player to MSC mode for installation. ");
547 if(detector.errdev() == "h10")
548 text = tr("H10 20GB in MTP mode found!\n"
549 "You need to change your player to UMS mode for installation. ");
550 text += tr("Unless you changed this installation will fail!");
552 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
553 return;
555 if(!detector.incompatdev().isEmpty()) {
556 QString text;
557 // we need to set the platform here to get the brand from the
558 // settings object
559 settings->setCurPlatform(detector.incompatdev());
560 text = tr("Detected an unsupported %1 player variant. Sorry, "
561 "Rockbox doesn't run on your player.").arg(settings->curBrand());
563 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
564 text, QMessageBox::Ok);
565 return;
568 if(detector.getMountPoint() != "" )
570 ui.mountPoint->setText(QDir::toNativeSeparators(detector.getMountPoint()));
572 else
574 QMessageBox::warning(this, tr("Autodetection"),
575 tr("Could not detect a Mountpoint.\n"
576 "Select your Mountpoint manually."),
577 QMessageBox::Ok ,QMessageBox::Ok);
580 else
582 QMessageBox::warning(this, tr("Autodetection"),
583 tr("Could not detect a device.\n"
584 "Select your device and Mountpoint manually."),
585 QMessageBox::Ok ,QMessageBox::Ok);
588 ui.treeDevices->setEnabled(true);
591 void Config::cacheClear()
593 if(QMessageBox::critical(this, tr("Really delete cache?"),
594 tr("Do you really want to delete the cache? "
595 "Make absolutely sure this setting is correct as it will "
596 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
597 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
598 return;
600 QString cache = ui.cachePath->text() + "/rbutil-cache/";
601 if(!QFileInfo(cache).isDir()) {
602 QMessageBox::critical(this, tr("Path wrong!"),
603 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
604 return;
606 QDir dir(cache);
607 QStringList fn;
608 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
609 qDebug() << fn;
611 for(int i = 0; i < fn.size(); i++) {
612 QString f = cache + fn.at(i);
613 QFile::remove(f);
614 qDebug() << "removed:" << f;
616 updateCacheInfo(settings->cachePath());
620 void Config::configTts()
622 int index = ui.comboTts->currentIndex();
623 TTSBase* tts = TTSBase::getTTS(ui.comboTts->itemData(index).toString());
625 tts->setCfg(settings);
626 tts->showCfg();
627 updateTtsState(ui.comboTts->currentIndex());
631 void Config::configEnc()
633 EncBase* enc = EncBase::getEncoder(settings->curEncoder());
635 enc->setCfg(settings);
636 enc->showCfg();
637 updateEncState();