Add some accelerator keys to the Actions menu.
[Rockbox.git] / rbutil / rbutilqt / configure.cpp
blobf583433b8a747b324c7d67810fde7306f2ef0b2f
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 settings->setCurTTS(ui.comboTts->currentText());
138 //encoder settings
139 settings->setCurEncoder(ui.comboEncoder->currentText());
141 // sync settings
142 settings->sync();
143 this->close();
144 emit settingsUpdated();
148 void Config::abort()
150 qDebug() << "Config::abort()";
151 this->close();
154 void Config::setSettings(RbSettings* sett)
156 settings = sett;
158 setUserSettings();
159 setDevices();
162 void Config::setUserSettings()
164 // set proxy
165 proxy = settings->proxy();
167 if(proxy.port() > 0)
168 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
169 else ui.proxyPort->setText("");
170 ui.proxyHost->setText(proxy.host());
171 ui.proxyUser->setText(proxy.userName());
172 ui.proxyPass->setText(proxy.password());
174 QString proxyType = settings->proxyType();
175 if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
176 else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
177 else ui.radioNoProxy->setChecked(true);
179 // set language selection
180 QList<QListWidgetItem*> a;
181 QString b;
182 // find key for lang value
183 QMap<QString, QString>::const_iterator i = lang.constBegin();
184 while (i != lang.constEnd()) {
185 if(i.value() == settings->curLang()) {
186 b = i.key();
187 break;
189 i++;
191 a = ui.listLanguages->findItems(b, Qt::MatchExactly);
192 if(a.size() <= 0)
193 a = ui.listLanguages->findItems(DEFAULT_LANG, Qt::MatchExactly);
194 if(a.size() > 0)
195 ui.listLanguages->setCurrentItem(a.at(0));
197 // devices tab
198 ui.mountPoint->setText(settings->mountpoint());
200 // cache tab
201 if(!QFileInfo(settings->cachePath()).isDir())
202 settings->setCachePath(QDir::tempPath());
203 ui.cachePath->setText(settings->cachePath());
204 ui.cacheDisable->setChecked(settings->cacheDisabled());
205 ui.cacheOfflineMode->setChecked(settings->cacheOffline());
206 updateCacheInfo(settings->cachePath());
210 void Config::updateCacheInfo(QString path)
212 QList<QFileInfo> fs;
213 fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
214 qint64 sz = 0;
215 for(int i = 0; i < fs.size(); i++) {
216 sz += fs.at(i).size();
217 qDebug() << fs.at(i).fileName() << fs.at(i).size();
219 ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
220 .arg(sz/1024));
224 void Config::setDevices()
227 // setup devices table
228 qDebug() << "Config::setDevices()";
230 QStringList platformList = settings->allPlatforms();
232 QMap <QString, QString> manuf;
233 QMap <QString, QString> devcs;
234 for(int it = 0; it < platformList.size(); it++)
236 QString curname = settings->name(platformList.at(it));
237 QString curbrand = settings->brand(platformList.at(it));
238 manuf.insertMulti(curbrand, platformList.at(it));
239 devcs.insert(platformList.at(it), curname);
242 QString platform;
243 platform = devcs.value(settings->curPlatform());
245 // set up devices table
246 ui.treeDevices->header()->hide();
247 ui.treeDevices->expandAll();
248 ui.treeDevices->setColumnCount(1);
249 QList<QTreeWidgetItem *> items;
251 // get manufacturers
252 QStringList brands = manuf.uniqueKeys();
253 QTreeWidgetItem *w;
254 QTreeWidgetItem *w2;
255 QTreeWidgetItem *w3 = 0;
256 for(int c = 0; c < brands.size(); c++) {
257 qDebug() << brands.at(c);
258 w = new QTreeWidgetItem();
259 w->setFlags(Qt::ItemIsEnabled);
260 w->setText(0, brands.at(c));
261 items.append(w);
263 // go through platforms again for sake of order
264 for(int it = 0; it < platformList.size(); it++) {
266 QString curname = settings->name(platformList.at(it));
267 QString curbrand = settings->brand(platformList.at(it));
269 if(curbrand != brands.at(c)) continue;
270 qDebug() << "adding:" << brands.at(c) << curname;
271 w2 = new QTreeWidgetItem(w, QStringList(curname));
272 w2->setData(0, Qt::UserRole, platformList.at(it));
274 if(platform.contains(curname)) {
275 w2->setSelected(true);
276 w->setExpanded(true);
277 w3 = w2; // save pointer to hilight old selection
279 items.append(w2);
282 ui.treeDevices->insertTopLevelItems(0, items);
283 if(w3 != 0)
284 ui.treeDevices->setCurrentItem(w3); // hilight old selection
286 // tts / encoder tab
288 //encoders
289 ui.comboEncoder->addItems(getEncoderList());
291 //update index of combobox
292 int index = ui.comboEncoder->findText(settings->curEncoder(),Qt::MatchExactly);
293 if(index < 0) index = 0;
294 ui.comboEncoder->setCurrentIndex(index);
295 updateEncState(index);
297 //tts
298 ui.comboTts->addItems(getTTSList());
301 //update index of combobox
302 index = ui.comboTts->findText(settings->curTTS(),Qt::MatchExactly);
303 if(index < 0) index = 0;
304 ui.comboTts->setCurrentIndex(index);
305 updateTtsState(index);
310 void Config::updateTtsState(int index)
312 QString ttsName = ui.comboTts->itemText(index);
313 TTSBase* tts = getTTS(ttsName);
314 tts->setCfg(settings);
316 if(tts->configOk())
318 ui.configTTSstatus->setText("Configuration OK");
319 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/icons/go-next.png")));
321 else
323 ui.configTTSstatus->setText("Configuration INVALID");
324 ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/icons/dialog-error.png")));
328 void Config::updateEncState(int index)
330 QString encoder = ui.comboEncoder->itemText(index);
331 EncBase* enc = getEncoder(encoder);
332 enc->setCfg(settings);
334 if(enc->configOk())
336 ui.configEncstatus->setText("Configuration OK");
337 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/icons/go-next.png")));
339 else
341 ui.configEncstatus->setText("Configuration INVALID");
342 ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/icons/dialog-error.png")));
346 void Config::setNoProxy(bool checked)
348 bool i = !checked;
349 ui.proxyPort->setEnabled(i);
350 ui.proxyHost->setEnabled(i);
351 ui.proxyUser->setEnabled(i);
352 ui.proxyPass->setEnabled(i);
356 void Config::setSystemProxy(bool checked)
358 bool i = !checked;
359 ui.proxyPort->setEnabled(i);
360 ui.proxyHost->setEnabled(i);
361 ui.proxyUser->setEnabled(i);
362 ui.proxyPass->setEnabled(i);
363 if(checked) {
364 // save values in input box
365 proxy.setScheme("http");
366 proxy.setUserName(ui.proxyUser->text());
367 proxy.setPassword(ui.proxyPass->text());
368 proxy.setHost(ui.proxyHost->text());
369 proxy.setPort(ui.proxyPort->text().toInt());
370 // show system values in input box
371 QUrl envproxy = systemProxy();
373 ui.proxyHost->setText(envproxy.host());
375 ui.proxyPort->setText(QString("%1").arg(envproxy.port()));
376 ui.proxyUser->setText(envproxy.userName());
377 ui.proxyPass->setText(envproxy.password());
380 else {
381 ui.proxyHost->setText(proxy.host());
382 if(proxy.port() > 0)
383 ui.proxyPort->setText(QString("%1").arg(proxy.port()));
384 else ui.proxyPort->setText("");
385 ui.proxyUser->setText(proxy.userName());
386 ui.proxyPass->setText(proxy.password());
392 QStringList Config::findLanguageFiles()
394 QDir dir(programPath);
395 QStringList fileNames;
396 QStringList langs;
397 fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
399 QDir resDir(":/lang");
400 fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
402 QRegExp exp("^rbutil_(.*)\\.qm");
403 for(int i = 0; i < fileNames.size(); i++) {
404 QString a = fileNames.at(i);
405 a.replace(exp, "\\1");
406 langs.append(a);
408 langs.sort();
409 qDebug() << "Config::findLanguageFiles()" << langs;
411 return langs;
415 QString Config::languageName(const QString &qmFile)
417 QTranslator translator;
419 QString file = "rbutil_" + qmFile;
420 if(!translator.load(file, programPath))
421 translator.load(file, ":/lang");
423 return translator.translate("Configure", "English");
427 void Config::updateLanguage()
429 qDebug() << "updateLanguage()";
430 QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
431 if(a.size() > 0)
432 language = lang.value(a.at(0)->text());
433 qDebug() << language;
437 void Config::browseFolder()
439 browser = new BrowseDirtree(this,tr("Select your device"));
440 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
441 browser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
442 #elif defined(Q_OS_WIN32)
443 browser->setFilter(QDir::Drives);
444 #endif
445 #if defined(Q_OS_MACX)
446 browser->setRoot("/Volumes");
447 #elif defined(Q_OS_LINUX)
448 browser->setDir("/media");
449 #endif
450 if( ui.mountPoint->text() != "" )
452 browser->setDir(ui.mountPoint->text());
454 browser->show();
455 connect(browser, SIGNAL(itemChanged(QString)), this, SLOT(setMountpoint(QString)));
459 void Config::browseCache()
461 cbrowser = new BrowseDirtree(this);
462 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
463 cbrowser->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
464 #elif defined(Q_OS_WIN32)
465 cbrowser->setFilter(QDir::Drives);
466 #endif
467 cbrowser->setDir(ui.cachePath->text());
468 cbrowser->show();
469 connect(cbrowser, SIGNAL(itemChanged(QString)), this, SLOT(setCache(QString)));
472 void Config::setMountpoint(QString m)
474 ui.mountPoint->setText(m);
478 void Config::setCache(QString c)
480 ui.cachePath->setText(c);
481 updateCacheInfo(c);
485 void Config::autodetect()
487 Autodetection detector(this);
489 if(detector.detect()) //let it detect
491 QString devicename = detector.getDevice();
492 // deexpand all items
493 for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
494 ui.treeDevices->topLevelItem(a)->setExpanded(false);
495 //deselect the selected item(s)
496 for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
497 ui.treeDevices->selectedItems().at(a)->setSelected(false);
499 // find the new item
500 // enumerate all platform items
501 QList<QTreeWidgetItem*> itmList= ui.treeDevices->findItems("*",Qt::MatchWildcard);
502 for(int i=0; i< itmList.size();i++)
504 //enumerate device items
505 for(int j=0;j < itmList.at(i)->childCount();j++)
507 QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
509 if(devicename == data) // item found
511 itmList.at(i)->child(j)->setSelected(true); //select the item
512 itmList.at(i)->setExpanded(true); //expand the platform item
513 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
514 break;
519 if(!detector.errdev().isEmpty()) {
520 QString text;
521 if(detector.errdev() == "sansae200")
522 text = tr("Sansa e200 in MTP mode found!\n"
523 "You need to change your player to MSC mode for installation. ");
524 if(detector.errdev() == "h10")
525 text = tr("H10 20GB in MTP mode found!\n"
526 "You need to change your player to UMS mode for installation. ");
527 text += tr("Unless you changed this installation will fail!");
529 QMessageBox::critical(this, tr("Fatal error"), text, QMessageBox::Ok);
530 return;
533 if(detector.getMountPoint() != "" )
535 ui.mountPoint->setText(detector.getMountPoint());
537 else
539 QMessageBox::warning(this, tr("Autodetection"),
540 tr("Could not detect a Mountpoint.\n"
541 "Select your Mountpoint manually."),
542 QMessageBox::Ok ,QMessageBox::Ok);
545 else
547 QMessageBox::warning(this, tr("Autodetection"),
548 tr("Could not detect a device.\n"
549 "Select your device and Mountpoint manually."),
550 QMessageBox::Ok ,QMessageBox::Ok);
555 void Config::cacheClear()
557 if(QMessageBox::critical(this, tr("Really delete cache?"),
558 tr("Do you really want to delete the cache? "
559 "Make absolutely sure this setting is correct as it will "
560 "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
561 QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
562 return;
564 QString cache = ui.cachePath->text() + "/rbutil-cache/";
565 if(!QFileInfo(cache).isDir()) {
566 QMessageBox::critical(this, tr("Path wrong!"),
567 tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
568 return;
570 QDir dir(cache);
571 QStringList fn;
572 fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);
573 qDebug() << fn;
575 for(int i = 0; i < fn.size(); i++) {
576 QString f = cache + fn.at(i);
577 QFile::remove(f);
578 qDebug() << "removed:" << f;
580 updateCacheInfo(settings->cachePath());
584 void Config::configTts()
586 TTSBase* tts =getTTS(ui.comboTts->currentText());
588 tts->setCfg(settings);
589 tts->showCfg();
590 updateTtsState(ui.comboTts->currentIndex());
594 void Config::configEnc()
596 EncBase* enc =getEncoder(ui.comboEncoder->currentText());
598 enc->setCfg(settings);
599 enc->showCfg();
600 updateEncState(ui.comboEncoder->currentIndex());