fix red and remove tabs
[kugel-rb.git] / rbutil / rbutilqt / configure.cpp
blobab926f1aa04db93733152a3db19adf1e41791527
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 "configure.h"
23 #include "autodetection.h"
24 #include "ui_configurefrm.h"
25 #include "browsedirtree.h"
26 #include "encoders.h"
27 #include "tts.h"
28 #include "utils.h"
30 #include <stdio.h>
31 #if defined(Q_OS_WIN32)
32 #if defined(UNICODE)
33 #define _UNICODE
34 #endif
35 #include <tchar.h>
36 #include <windows.h>
37 #endif
39 #define DEFAULT_LANG "English (C)"
41 Config::Config(QWidget *parent,int index) : QDialog(parent)
43 programPath = qApp->applicationDirPath() + "/";
44 ui.setupUi(this);
45 ui.tabConfiguration->setCurrentIndex(index);
46 ui.radioManualProxy->setChecked(true);
47 QRegExpValidator *proxyValidator = new QRegExpValidator(this);
48 QRegExp validate("[0-9]*");
49 proxyValidator->setRegExp(validate);
50 ui.proxyPort->setValidator(proxyValidator);
51 #if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN32)
52 ui.radioSystemProxy->setEnabled(false); // not on macox for now
53 #endif
54 // build language list and sort alphabetically
55 QStringList langs = findLanguageFiles();
56 for(int i = 0; i < langs.size(); ++i)
57 lang.insert(languageName(langs.at(i)) + tr(" (%1)").arg(langs.at(i)), langs.at(i));
58 lang.insert(DEFAULT_LANG, "");
59 QMap<QString, QString>::const_iterator i = lang.constBegin();
60 while (i != lang.constEnd()) {
61 ui.listLanguages->addItem(i.key());
62 i++;
64 ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
65 connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
66 ui.proxyPass->setEchoMode(QLineEdit::Password);
67 ui.treeDevices->setAlternatingRowColors(true);
68 ui.listLanguages->setAlternatingRowColors(true);
70 this->setModal(true);
72 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
73 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
74 connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
75 connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
76 connect(ui.browseMountPoint, SIGNAL(clicked()), this, SLOT(browseFolder()));
77 connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
78 connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
79 connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
80 connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
81 connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
82 connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
83 connect(ui.comboEncoder, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEncState(int)));
89 void Config::accept()
91 qDebug() << "Config::accept()";
92 // proxy: save entered proxy values, not displayed.
93 if(ui.radioManualProxy->isChecked()) {
94 proxy.setScheme("http");
95 proxy.setUserName(ui.proxyUser->text());
96 proxy.setPassword(ui.proxyPass->text());
97 proxy.setHost(ui.proxyHost->text());
98 proxy.setPort(ui.proxyPort->text().toInt());
101 settings->setProxy(proxy.toString());
102 qDebug() << "new proxy:" << proxy;
103 // proxy type
104 QString proxyType;
105 if(ui.radioNoProxy->isChecked()) proxyType = "none";
106 else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
107 else proxyType = "manual";
108 settings->setProxyType(proxyType);
110 // language
111 if(settings->curLang() != language)
112 QMessageBox::information(this, tr("Language changed"),
113 tr("You need to restart the application for the changed language to take effect."));
114 settings->setLang(language);
116 // mountpoint
117 QString mp = ui.mountPoint->text();
118 if(QFileInfo(mp).isDir())
119 settings->setMountpoint( mp);
121 // platform
122 QString nplat;
123 if(ui.treeDevices->selectedItems().size() != 0) {
124 nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
125 settings->setCurPlatform(nplat);
128 // cache settings
129 if(QFileInfo(ui.cachePath->text()).isDir())
130 settings->setCachePath(ui.cachePath->text());
131 else // default to system temp path
132 settings->setCachePath( QDir::tempPath());
133 settings->setCacheDisable(ui.cacheDisable->isChecked());
134 settings->setCacheOffline(ui.cacheOfflineMode->isChecked());
136 // tts settings
137 int i = ui.comboTts->currentIndex();
138 settings->setCurTTS(ui.comboTts->itemData(i).toString());
139 //encoder settings
140 i = ui.comboEncoder->currentIndex();
141 settings->setCurEncoder(ui.comboEncoder->itemData(i).toString());
143 // sync settings
144 settings->sync();
145 this->close();
146 emit settingsUpdated();
150 void Config::abort()
152 qDebug() << "Config::abort()";
153 this->close();
156 void Config::setSettings(RbSettings* sett)
158 settings = sett;
160 setUserSettings();
161 setDevices();
164 void Config::setUserSettings()
166 // set proxy
167 proxy = settings->proxy();
169 if(proxy.port() > 0)
170 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
171 else ui.proxyPort->setText("");
172 ui.proxyHost->setText(proxy.host());
173 ui.proxyUser->setText(proxy.userName());
174 ui.proxyPass->setText(proxy.password());
176 QString proxyType = settings->proxyType();
177 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
178 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
179 else ui.radioNoProxy->setChecked(true);
181 // set language selection
182 QList<QListWidgetItem*> a;
183 QString b;
184 // find key for lang value
185 QMap<QString, QString>::const_iterator i = lang.constBegin();
186 while (i != lang.constEnd()) {
187 if(i.value() == settings->curLang()) {
188 b = i.key();
189 break;
191 i++;
193 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
194 if(a.size() <= 0)
195 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
196 if(a.size() > 0)
197 ui.listLanguages->setCurrentItem(a.at(0));
199 // devices tab
200 ui.mountPoint->setText(settings->mountpoint());
202 // cache tab
203 if(!QFileInfo(settings->cachePath()).isDir())
204 settings->setCachePath(QDir::tempPath());
205 ui.cachePath->setText(settings->cachePath());
206 ui.cacheDisable->setChecked(settings->cacheDisabled());
207 ui.cacheOfflineMode->setChecked(settings->cacheOffline());
208 updateCacheInfo(settings->cachePath());
212 void Config::updateCacheInfo(QString path)
214 QList<QFileInfo> fs;
215 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
216 qint64 sz = 0;
217 for(int i = 0; i < fs.size(); i++) {
218 sz += fs.at(i).size();
219 qDebug() << fs.at(i).fileName() << fs.at(i).size();
221 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
222 .arg(sz/1024));
226 void Config::setDevices()
229 // setup devices table
230 qDebug() << "Config::setDevices()";
232 QStringList platformList = settings->allPlatforms();
234 QMap <QString, QString> manuf;
235 QMap <QString, QString> devcs;
236 for(int it = 0; it < platformList.size(); it++)
238 QString curname = settings->name(platformList.at(it));
239 QString curbrand = settings->brand(platformList.at(it));
240 manuf.insertMulti(curbrand, platformList.at(it));
241 devcs.insert(platformList.at(it), curname);
244 QString platform;
245 platform = devcs.value(settings->curPlatform());
247 // set up devices table
248 ui.treeDevices->header()->hide();
249 ui.treeDevices->expandAll();
250 ui.treeDevices->setColumnCount(1);
251 QList<QTreeWidgetItem *> items;
253 // get manufacturers
254 QStringList brands = manuf.uniqueKeys();
255 QTreeWidgetItem *w;
256 QTreeWidgetItem *w2;
257 QTreeWidgetItem *w3 = 0;
258 for(int c = 0; c < brands.size(); c++) {
259 qDebug() << brands.at(c);
260 w = new QTreeWidgetItem();
261 w->setFlags(Qt::ItemIsEnabled);
262 w->setText(0, brands.at(c));
263 items.append(w);
265 // go through platforms again for sake of order
266 for(int it = 0; it < platformList.size(); it++) {
268 QString curname = settings->name(platformList.at(it));
269 QString curbrand = settings->brand(platformList.at(it));
271 if(curbrand != brands.at(c)) continue;
272 qDebug() << "adding:" << brands.at(c) << curname;
273 w2 = new QTreeWidgetItem(w, QStringList(curname));
274 w2->setData(0, Qt::UserRole, platformList.at(it));
276 if(platform.contains(curname)) {
277 w2->setSelected(true);
278 w->setExpanded(true);
279 w3 = w2; // save pointer to hilight old selection
281 items.append(w2);
284 ui.treeDevices->insertTopLevelItems(0, items);
285 if(w3 != 0)
286 ui.treeDevices->setCurrentItem(w3); // hilight old selection
288 // tts / encoder tab
290 //encoders
291 int index;
292 QStringList encoders = EncBase::getEncoderList();
293 for(int a = 0; a < encoders.size(); a++)
294 ui.comboEncoder->addItem(EncBase::getEncoderName(encoders.at(a)), encoders.at(a));
295 //update index of combobox
296 index = ui.comboEncoder->findData(settings->curEncoder());
297 if(index < 0) index = 0;
298 ui.comboEncoder->setCurrentIndex(index);
299 updateEncState(index);
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 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("Configuration OK");
323 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
325 else
327 ui.configTTSstatus->setText("Configuration INVALID");
328 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
332 void Config::updateEncState(int index)
334 QString encoder = ui.comboEncoder->itemData(index).toString();
335 EncBase* enc = EncBase::getEncoder(encoder);
336 enc->setCfg(settings);
338 if(enc->configOk())
340 ui.configEncstatus->setText("Configuration OK");
341 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.png")));
343 else
345 ui.configEncstatus->setText("Configuration INVALID");
346 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.png")));
350 void Config::setNoProxy(bool checked)
352 bool i = !checked;
353 ui.proxyPort->setEnabled(i);
354 ui.proxyHost->setEnabled(i);
355 ui.proxyUser->setEnabled(i);
356 ui.proxyPass->setEnabled(i);
360 void Config::setSystemProxy(bool checked)
362 bool i = !checked;
363 ui.proxyPort->setEnabled(i);
364 ui.proxyHost->setEnabled(i);
365 ui.proxyUser->setEnabled(i);
366 ui.proxyPass->setEnabled(i);
367 if(checked) {
368 // save values in input box
369 proxy.setScheme("http");
370 proxy.setUserName(ui.proxyUser->text());
371 proxy.setPassword(ui.proxyPass->text());
372 proxy.setHost(ui.proxyHost->text());
373 proxy.setPort(ui.proxyPort->text().toInt());
374 // show system values in input box
375 QUrl envproxy = systemProxy();
377 ui.proxyHost->setText(envproxy.host());
379 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
380 ui.proxyUser->setText(envproxy.userName());
381 ui.proxyPass->setText(envproxy.password());
384 else {
385 ui.proxyHost->setText(proxy.host());
386 if(proxy.port() > 0)
387 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
388 else ui.proxyPort->setText("");
389 ui.proxyUser->setText(proxy.userName());
390 ui.proxyPass->setText(proxy.password());
396 QStringList Config::findLanguageFiles()
398 QDir dir(programPath);
399 QStringList fileNames;
400 QStringList langs;
401 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
403 QDir resDir(":/lang");
404 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
406 QRegExp exp("^rbutil_(.*)\\.qm");
407 for(int i = 0; i < fileNames.size(); i++) {
408 QString a = fileNames.at(i);
409 a.replace(exp, "\\1");
410 langs.append(a);
412 langs.sort();
413 qDebug() << "Config::findLanguageFiles()" << langs;
415 return langs;
419 QString Config::languageName(const QString &qmFile)
421 QTranslator translator;
423 QString file = "rbutil_" + qmFile;
424 if(!translator.load(file, programPath))
425 translator.load(file, ":/lang");
427 return translator.translate("Configure", "English");
431 void Config::updateLanguage()
433 qDebug() << "updateLanguage()";
434 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
435 if(a.size() > 0)
436 language = lang.value(a.at(0)->text());
437 qDebug() << language;
441 void Config::browseFolder()
443 browser = new BrowseDirtree(this,tr("Select your device"));
444 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
445 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
446 #elif defined(Q_OS_WIN32)
447 browser->setFilter(QDir::Drives);
448 #endif
449 #if defined(Q_OS_MACX)
450 browser->setRoot("/Volumes");
451 #elif defined(Q_OS_LINUX)
452 browser->setDir("/media");
453 #endif
454 if( ui.mountPoint->text() != "" )
456 browser->setDir(ui.mountPoint->text());
458 browser->show();
459 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
463 void Config::browseCache()
465 cbrowser = new BrowseDirtree(this);
466 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
467 cbrowser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
468 #elif defined(Q_OS_WIN32)
469 cbrowser->setFilter(QDir::Drives);
470 #endif
471 cbrowser->setDir(ui.cachePath->text());
472 connect(cbrowser, SIGNAL(itemChanged(QString)), this, SLOT(setCache(QString)));
473 cbrowser->show();
477 void Config::setMountpoint(QString m)
479 ui.mountPoint->setText(m);
483 void Config::setCache(QString c)
485 ui.cachePath->setText(c);
486 updateCacheInfo(c);
490 void Config::autodetect()
492 Autodetection detector(this);
493 detector.setSettings(settings);
495 if(detector.detect()) //let it detect
497 QString devicename = detector.getDevice();
498 // deexpand all items
499 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
500 ui.treeDevices->topLevelItem(a)->setExpanded(false);
501 //deselect the selected item(s)
502 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
503 ui.treeDevices->selectedItems().at(a)->setSelected(false);
505 // find the new item
506 // enumerate all platform items
507 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
508 for(int i=0; i< itmList.size();i++)
510 //enumerate device items
511 for(int j=0;j < itmList.at(i)->childCount();j++)
513 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
515 if(devicename == data) // item found
517 itmList.at(i)->child(j)->setSelected(true); //select the item
518 itmList.at(i)->setExpanded(true); //expand the platform item
519 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
520 break;
525 if(!detector.errdev().isEmpty()) {
526 QString text;
527 if(detector.errdev() == "sansae200")
528 text = tr("Sansa e200 in MTP mode found!\n"
529 "You need to change your player to MSC mode for installation. ");
530 if(detector.errdev() == "h10")
531 text = tr("H10 20GB in MTP mode found!\n"
532 "You need to change your player to UMS mode for installation. ");
533 text += tr("Unless you changed this installation will fail!");
535 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
536 return;
538 if(!detector.incompatdev().isEmpty()) {
539 QString text;
540 // we need to set the platform here to get the brand from the
541 // settings object
542 settings->setCurPlatform(detector.incompatdev());
543 text = tr("Detected an unsupported %1 player variant. Sorry, "
544 "Rockbox doesn't run on your player.").arg(settings->curBrand());
546 QMessageBox::critical(this, tr("Fatal error: incompatible player found"),
547 text, QMessageBox::Ok);
548 return;
551 if(detector.getMountPoint() != "" )
553 ui.mountPoint->setText(detector.getMountPoint());
555 else
557 QMessageBox::warning(this, tr("Autodetection"),
558 tr("Could not detect a Mountpoint.\n"
559 "Select your Mountpoint manually."),
560 QMessageBox::Ok ,QMessageBox::Ok);
563 else
565 QMessageBox::warning(this, tr("Autodetection"),
566 tr("Could not detect a device.\n"
567 "Select your device and Mountpoint manually."),
568 QMessageBox::Ok ,QMessageBox::Ok);
573 void Config::cacheClear()
575 if(QMessageBox::critical(this, tr("Really delete cache?"),
576 tr("Do you really want to delete the cache? "
577 "Make absolutely sure this setting is correct as it will "
578 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
579 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
580 return;
582 QString cache = ui.cachePath->text() + "/rbutil-cache/";
583 if(!QFileInfo(cache).isDir()) {
584 QMessageBox::critical(this, tr("Path wrong!"),
585 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
586 return;
588 QDir dir(cache);
589 QStringList fn;
590 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
591 qDebug() << fn;
593 for(int i = 0; i < fn.size(); i++) {
594 QString f = cache + fn.at(i);
595 QFile::remove(f);
596 qDebug() << "removed:" << f;
598 updateCacheInfo(settings->cachePath());
602 void Config::configTts()
604 int index = ui.comboTts->currentIndex();
605 TTSBase* tts = TTSBase::getTTS(ui.comboTts->itemData(index).toString());
607 tts->setCfg(settings);
608 tts->showCfg();
609 updateTtsState(ui.comboTts->currentIndex());
613 void Config::configEnc()
615 int index = ui.comboEncoder->currentIndex();
616 EncBase* enc = EncBase::getEncoder(ui.comboEncoder->itemData(index).toString());
618 enc->setCfg(settings);
619 enc->showCfg();
620 updateEncState(ui.comboEncoder->currentIndex());