1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Riebeling
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 ****************************************************************************/
22 #include "configure.h"
23 #include "autodetection.h"
24 #include "ui_configurefrm.h"
25 #include "browsedirtree.h"
29 #define DEFAULT_LANG "English (builtin)"
31 Config::Config(QWidget
*parent
) : QDialog(parent
)
33 programPath
= qApp
->applicationDirPath() + "/";
35 ui
.radioManualProxy
->setChecked(true);
36 QRegExpValidator
*proxyValidator
= new QRegExpValidator(this);
37 QRegExp
validate("[0-9]*");
38 proxyValidator
->setRegExp(validate
);
39 ui
.proxyPort
->setValidator(proxyValidator
);
41 ui
.radioSystemProxy
->setEnabled(false); // only on linux for now
43 // build language list and sort alphabetically
44 QStringList langs
= findLanguageFiles();
45 for(int i
= 0; i
< langs
.size(); ++i
)
46 lang
.insert(languageName(langs
[i
]), langs
[i
]);
47 lang
.insert(DEFAULT_LANG
, "");
48 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
49 while (i
!= lang
.constEnd()) {
50 ui
.listLanguages
->addItem(i
.key());
53 ui
.listLanguages
->setSelectionMode(QAbstractItemView::SingleSelection
);
54 connect(ui
.listLanguages
, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
55 ui
.proxyPass
->setEchoMode(QLineEdit::Password
);
56 ui
.treeDevices
->setAlternatingRowColors(true);
57 ui
.listLanguages
->setAlternatingRowColors(true);
61 connect(ui
.buttonOk
, SIGNAL(clicked()), this, SLOT(accept()));
62 connect(ui
.buttonCancel
, SIGNAL(clicked()), this, SLOT(abort()));
63 connect(ui
.radioNoProxy
, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
64 connect(ui
.radioSystemProxy
, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
65 connect(ui
.browseMountPoint
, SIGNAL(clicked()), this, SLOT(browseFolder()));
66 connect(ui
.buttonAutodetect
,SIGNAL(clicked()),this,SLOT(autodetect()));
67 connect(ui
.buttonCacheBrowse
, SIGNAL(clicked()), this, SLOT(browseCache()));
68 connect(ui
.buttonCacheClear
, SIGNAL(clicked()), this, SLOT(cacheClear()));
69 connect(ui
.browseTts
, SIGNAL(clicked()), this, SLOT(browseTts()));
70 connect(ui
.comboEncoder
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEncOpts(int)));
71 connect(ui
.comboTts
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsOpts(int)));
80 qDebug() << "Config::accept()";
81 // proxy: save entered proxy values, not displayed.
82 if(ui
.radioManualProxy
->isChecked()) {
83 proxy
.setScheme("http");
84 proxy
.setUserName(ui
.proxyUser
->text());
85 proxy
.setPassword(ui
.proxyPass
->text());
86 proxy
.setHost(ui
.proxyHost
->text());
87 proxy
.setPort(ui
.proxyPort
->text().toInt());
89 userSettings
->setValue("defaults/proxy", proxy
.toString());
90 qDebug() << "new proxy:" << proxy
;
93 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
94 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
95 else proxyType
= "manual";
96 userSettings
->setValue("defaults/proxytype", proxyType
);
99 if(userSettings
->value("defaults/lang").toString() != language
)
100 QMessageBox::information(this, tr("Language changed"),
101 tr("You need to restart the application for the changed language to take effect."));
102 userSettings
->setValue("defaults/lang", language
);
105 QString mp
= ui
.mountPoint
->text();
106 if(QFileInfo(mp
).isDir())
107 userSettings
->setValue("defaults/mountpoint", mp
);
111 if(ui
.treeDevices
->selectedItems().size() != 0) {
112 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
113 userSettings
->setValue("defaults/platform", nplat
);
117 if(QFileInfo(ui
.cachePath
->text()).isDir())
118 userSettings
->setValue("defaults/cachepath", ui
.cachePath
->text());
119 else // default to system temp path
120 userSettings
->setValue("defaults/cachepath", QDir::tempPath());
121 userSettings
->setValue("defaults/cachedisable", ui
.cacheDisable
->isChecked());
122 userSettings
->setValue("defaults/offline", ui
.cacheOfflineMode
->isChecked());
125 if(QFileInfo(ui
.ttsExecutable
->text()).isExecutable())
126 userSettings
->setValue("ttsbin", ui
.ttsExecutable
->text());
127 userSettings
->setValue("ttsopts", ui
.ttsOptions
->text());
128 if(QFileInfo(ui
.encoderExecutable
->text()).isExecutable())
129 userSettings
->setValue("encbin", ui
.encoderExecutable
->text());
130 userSettings
->setValue("ttsopts", ui
.ttsOptions
->text());
132 preset
= ui
.comboEncoder
->itemData(ui
.comboEncoder
->currentIndex(), Qt::UserRole
).toString();
133 userSettings
->setValue("encpreset", preset
);
134 preset
= ui
.comboTts
->itemData(ui
.comboTts
->currentIndex(), Qt::UserRole
).toString();
135 userSettings
->setValue("ttspreset", preset
);
138 userSettings
->sync();
140 emit
settingsUpdated();
146 qDebug() << "Config::abort()";
151 void Config::setUserSettings(QSettings
*user
)
155 proxy
= userSettings
->value("defaults/proxy").toString();
158 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
159 else ui
.proxyPort
->setText("");
160 ui
.proxyHost
->setText(proxy
.host());
161 ui
.proxyUser
->setText(proxy
.userName());
162 ui
.proxyPass
->setText(proxy
.password());
164 QString proxyType
= userSettings
->value("defaults/proxytype").toString();
165 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
166 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
167 else ui
.radioNoProxy
->setChecked(true);
169 // set language selection
170 QList
<QListWidgetItem
*> a
;
172 // find key for lang value
173 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
174 while (i
!= lang
.constEnd()) {
175 if(i
.value() == userSettings
->value("defaults/lang").toString() + ".qm") {
181 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
183 a
= ui
.listLanguages
->findItems(DEFAULT_LANG
, Qt::MatchExactly
);
185 ui
.listLanguages
->setCurrentItem(a
.at(0));
188 ui
.mountPoint
->setText(userSettings
->value("defaults/mountpoint").toString());
191 if(!QFileInfo(userSettings
->value("defaults/cachepath").toString()).isDir())
192 userSettings
->setValue("defaults/cachepath", QDir::tempPath());
193 ui
.cachePath
->setText(userSettings
->value("defaults/cachepath").toString());
194 ui
.cacheDisable
->setChecked(userSettings
->value("defaults/cachedisable", true).toBool());
195 ui
.cacheOfflineMode
->setChecked(userSettings
->value("defaults/offline").toBool());
196 QList
<QFileInfo
> fs
= QDir(userSettings
->value("defaults/cachepath").toString() + "/rbutil-cache/").entryInfoList(QDir::Files
| QDir::NoDotAndDotDot
);
198 for(int i
= 0; i
< fs
.size(); i
++) {
199 sz
+= fs
.at(i
).size();
200 qDebug() << fs
.at(i
).fileName() << fs
.at(i
).size();
202 ui
.cacheSize
->setText(tr("Current cache size is %1 kiB.")
208 void Config::setDevices(QSettings
*dev
)
211 // setup devices table
212 qDebug() << "Config::setDevices()";
213 devices
->beginGroup("platforms");
214 QStringList a
= devices
->childKeys();
217 QMap
<QString
, QString
> manuf
;
218 QMap
<QString
, QString
> devcs
;
219 for(int it
= 0; it
< a
.size(); it
++) {
221 devices
->beginGroup("platforms");
222 curdev
= devices
->value(a
.at(it
), "null").toString();
225 devices
->beginGroup(curdev
);
226 curname
= devices
->value("name", "null").toString();
227 QString curbrand
= devices
->value("brand", "").toString();
229 manuf
.insertMulti(curbrand
, curdev
);
230 devcs
.insert(curdev
, curname
);
234 platform
= devcs
.value(userSettings
->value("defaults/platform").toString());
236 // set up devices table
237 ui
.treeDevices
->header()->hide();
238 ui
.treeDevices
->expandAll();
239 ui
.treeDevices
->setColumnCount(1);
240 QList
<QTreeWidgetItem
*> items
;
243 QStringList brands
= manuf
.uniqueKeys();
246 QTreeWidgetItem
*w3
= 0;
247 for(int c
= 0; c
< brands
.size(); c
++) {
248 qDebug() << brands
.at(c
);
249 w
= new QTreeWidgetItem();
250 w
->setFlags(Qt::ItemIsEnabled
);
251 w
->setText(0, brands
.at(c
));
252 // w->setData(0, Qt::DecorationRole, <icon>);
255 // go through platforms again for sake of order
256 for(int it
= 0; it
< a
.size(); it
++) {
258 devices
->beginGroup("platforms");
259 curdev
= devices
->value(a
.at(it
), "null").toString();
262 devices
->beginGroup(curdev
);
263 curname
= devices
->value("name", "null").toString();
264 QString curbrand
= devices
->value("brand", "").toString();
266 if(curbrand
!= brands
.at(c
)) continue;
267 qDebug() << "adding:" << brands
.at(c
) << curname
<< curdev
;
268 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
269 w2
->setData(0, Qt::UserRole
, curdev
);
270 if(platform
.contains(curname
)) {
271 w2
->setSelected(true);
272 w
->setExpanded(true);
273 w3
= w2
; // save pointer to hilight old selection
278 ui
.treeDevices
->insertTopLevelItems(0, items
);
280 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
285 devices
->beginGroup("encoders");
286 keys
= devices
->allKeys();
287 for(int i
=0; i
< keys
.size();i
++)
288 ui
.comboEncoder
->addItem(devices
->value(keys
.at(i
), "null").toString(),
292 devices
->beginGroup("tts");
293 keys
= devices
->allKeys();
294 for(int i
=0; i
< keys
.size();i
++)
295 ui
.comboTts
->addItem(devices
->value(keys
.at(i
), "null").toString(), keys
.at(i
));
299 index
= ui
.comboTts
->findData(userSettings
->value("ttspreset").toString(),
300 Qt::UserRole
, Qt::MatchExactly
);
301 ui
.comboTts
->setCurrentIndex(index
);
302 updateTtsOpts(index
);
303 ui
.ttsExecutable
->setText(userSettings
->value("ttsbin").toString());
305 index
= ui
.comboEncoder
->findData(userSettings
->value("encpreset").toString(),
306 Qt::UserRole
, Qt::MatchExactly
);
307 ui
.comboEncoder
->setCurrentIndex(index
);
308 updateEncOpts(index
);
309 ui
.encoderExecutable
->setText(userSettings
->value("encbin").toString());
314 void Config::updateEncOpts(int index
)
316 qDebug() << "updateEncOpts()";
319 QString c
= ui
.comboEncoder
->itemData(index
, Qt::UserRole
).toString();
320 devices
->beginGroup(c
);
321 ui
.encoderOptions
->setText(devices
->value("options").toString());
322 edit
= devices
->value("edit").toBool();
323 ui
.encoderOptions
->setEnabled(edit
);
324 e
= devices
->value("encoder").toString();
327 // try to autodetect encoder
328 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
329 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
330 #elif defined(Q_OS_WIN)
331 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
334 ui
.encoderExecutable
->setEnabled(true);
335 for(int i
= 0; i
< path
.size(); i
++) {
336 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
337 #if defined(Q_OS_WIN)
338 executable
+= ".exe";
339 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
340 executable
= ex
.join("");
342 if(QFileInfo(executable
).isExecutable()) {
343 qDebug() << "found:" << executable
;
344 ui
.encoderExecutable
->setText(QDir::toNativeSeparators(executable
));
345 // disallow changing the detected path if non-customizable profile
347 ui
.encoderExecutable
->setEnabled(false);
354 void Config::updateTtsOpts(int index
)
358 QString c
= ui
.comboTts
->itemData(index
, Qt::UserRole
).toString();
359 devices
->beginGroup(c
);
360 edit
= devices
->value("edit").toBool();
361 ui
.ttsOptions
->setText(devices
->value("options").toString());
362 ui
.ttsOptions
->setEnabled(devices
->value("edit").toBool());
363 e
= devices
->value("tts").toString();
366 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
367 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
368 #elif defined(Q_OS_WIN)
369 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
372 ui
.ttsExecutable
->setEnabled(true);
373 for(int i
= 0; i
< path
.size(); i
++) {
374 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
375 #if defined(Q_OS_WIN)
376 executable
+= ".exe";
377 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
378 executable
= ex
.join("");
380 qDebug() << executable
;
381 if(QFileInfo(executable
).isExecutable()) {
382 ui
.ttsExecutable
->setText(QDir::toNativeSeparators(executable
));
383 // disallow changing the detected path if non-customizable profile
385 ui
.ttsExecutable
->setEnabled(false);
392 void Config::setNoProxy(bool checked
)
395 ui
.proxyPort
->setEnabled(i
);
396 ui
.proxyHost
->setEnabled(i
);
397 ui
.proxyUser
->setEnabled(i
);
398 ui
.proxyPass
->setEnabled(i
);
402 void Config::setSystemProxy(bool checked
)
405 ui
.proxyPort
->setEnabled(i
);
406 ui
.proxyHost
->setEnabled(i
);
407 ui
.proxyUser
->setEnabled(i
);
408 ui
.proxyPass
->setEnabled(i
);
410 // save values in input box
411 proxy
.setScheme("http");
412 proxy
.setUserName(ui
.proxyUser
->text());
413 proxy
.setPassword(ui
.proxyPass
->text());
414 proxy
.setHost(ui
.proxyHost
->text());
415 proxy
.setPort(ui
.proxyPort
->text().toInt());
416 // show system values in input box
418 QUrl envproxy
= QUrl(getenv("http_proxy"));
419 ui
.proxyHost
->setText(envproxy
.host());
420 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
421 ui
.proxyUser
->setText(envproxy
.userName());
422 ui
.proxyPass
->setText(envproxy
.password());
426 ui
.proxyHost
->setText(proxy
.host());
428 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
429 else ui
.proxyPort
->setText("");
430 ui
.proxyUser
->setText(proxy
.userName());
431 ui
.proxyPass
->setText(proxy
.password());
437 QStringList
Config::findLanguageFiles()
439 QDir
dir(programPath
);
440 QStringList fileNames
;
441 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
443 QDir
resDir(":/lang");
444 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
447 qDebug() << "Config::findLanguageFiles()" << fileNames
;
453 QString
Config::languageName(const QString
&qmFile
)
455 QTranslator translator
;
457 if(!translator
.load(qmFile
, programPath
))
458 translator
.load(qmFile
, ":/lang");
460 return translator
.translate("Configure", "English");
464 void Config::updateLanguage()
466 qDebug() << "updateLanguage()";
467 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
469 language
= QFileInfo(lang
.value(a
.at(0)->text())).baseName();
473 void Config::browseFolder()
475 browser
= new BrowseDirtree(this);
476 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
477 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
478 #elif defined(Q_OS_WIN32)
479 browser
->setFilter(QDir::Drives
);
481 QDir
d(ui
.mountPoint
->text());
484 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
488 void Config::browseCache()
490 cbrowser
= new BrowseDirtree(this);
491 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
492 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
493 #elif defined(Q_OS_WIN32)
494 cbrowser
->setFilter(QDir::Drives
);
496 QDir
d(ui
.cachePath
->text());
499 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
502 void Config::setMountpoint(QString m
)
504 ui
.mountPoint
->setText(m
);
508 void Config::setCache(QString c
)
510 ui
.cachePath
->setText(c
);
514 void Config::autodetect()
516 Autodetection
detector(this);
518 if(detector
.detect()) //let it detect
520 QString devicename
= detector
.getDevice();
521 //deexpand the platform
522 ui
.treeDevices
->selectedItems().at(0)->parent()->setExpanded(false);
523 //deselect the selected item
524 ui
.treeDevices
->selectedItems().at(0)->setSelected(false);
527 //enumerate al plattform items
528 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
529 for(int i
=0; i
< itmList
.size();i
++)
531 //enumerate device items
532 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
534 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
536 if( devicename
.contains(data
)) //item found
538 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
539 itmList
.at(i
)->setExpanded(true); //expand the platform item
545 if(detector
.getMountPoint() != "" )
547 ui
.mountPoint
->setText(detector
.getMountPoint());
551 QMessageBox::warning(this, tr("Autodetection"),
552 tr("Could not detect a Mountpoint.\n"
553 "Select your Mountpoint manually."),
554 QMessageBox::Ok
,QMessageBox::Ok
);
559 QMessageBox::warning(this, tr("Autodetection"),
560 tr("Could not detect a device.\n"
561 "Select your device and Mountpoint manually."),
562 QMessageBox::Ok
,QMessageBox::Ok
);
567 void Config::cacheClear()
569 if(QMessageBox::critical(this, tr("Really delete cache?"),
570 tr("Do you really want to delete the cache? "
571 "Make absolutely sure this setting is correct as it will "
572 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
573 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
576 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
577 if(!QFileInfo(cache
).isDir()) {
578 QMessageBox::critical(this, tr("Path wrong!"),
579 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
584 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
587 for(int i
= 0; i
< fn
.size(); i
++) {
588 QString f
= cache
+ fn
.at(i
);
590 qDebug() << "removed:" << f
;
595 void Config::browseTts()
597 BrowseDirtree
browser(this);
598 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
600 if(QFileInfo(ui
.ttsExecutable
->text()).isDir())
602 QDir
d(ui
.ttsExecutable
->text());
605 if(browser
.exec() == QDialog::Accepted
)
607 qDebug() << browser
.getSelected();
608 QString exe
= browser
.getSelected();
609 if(!QFileInfo(exe
).isExecutable())
611 ui
.ttsExecutable
->setText(exe
);