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
.browseEncoder
, SIGNAL(clicked()), this, SLOT(browseEnc()));
71 connect(ui
.comboEncoder
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateEncOpts(int)));
72 connect(ui
.comboTts
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsOpts(int)));
79 qDebug() << "Config::accept()";
80 // proxy: save entered proxy values, not displayed.
81 if(ui
.radioManualProxy
->isChecked()) {
82 proxy
.setScheme("http");
83 proxy
.setUserName(ui
.proxyUser
->text());
84 proxy
.setPassword(ui
.proxyPass
->text());
85 proxy
.setHost(ui
.proxyHost
->text());
86 proxy
.setPort(ui
.proxyPort
->text().toInt());
88 userSettings
->setValue("defaults/proxy", proxy
.toString());
89 qDebug() << "new proxy:" << proxy
;
92 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
93 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
94 else proxyType
= "manual";
95 userSettings
->setValue("defaults/proxytype", proxyType
);
98 if(userSettings
->value("defaults/lang").toString() != language
)
99 QMessageBox::information(this, tr("Language changed"),
100 tr("You need to restart the application for the changed language to take effect."));
101 userSettings
->setValue("defaults/lang", language
);
104 QString mp
= ui
.mountPoint
->text();
105 if(QFileInfo(mp
).isDir())
106 userSettings
->setValue("defaults/mountpoint", mp
);
110 if(ui
.treeDevices
->selectedItems().size() != 0) {
111 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
112 userSettings
->setValue("defaults/platform", nplat
);
116 if(QFileInfo(ui
.cachePath
->text()).isDir())
117 userSettings
->setValue("defaults/cachepath", ui
.cachePath
->text());
118 else // default to system temp path
119 userSettings
->setValue("defaults/cachepath", QDir::tempPath());
120 userSettings
->setValue("defaults/cachedisable", ui
.cacheDisable
->isChecked());
121 userSettings
->setValue("defaults/offline", ui
.cacheOfflineMode
->isChecked());
124 if(QFileInfo(ui
.ttsExecutable
->text()).isExecutable())
125 userSettings
->setValue("ttsbin", ui
.ttsExecutable
->text());
126 userSettings
->setValue("ttsopts", ui
.ttsOptions
->text());
127 if(QFileInfo(ui
.encoderExecutable
->text()).isExecutable())
128 userSettings
->setValue("encbin", ui
.encoderExecutable
->text());
129 userSettings
->setValue("ttsopts", ui
.ttsOptions
->text());
131 preset
= ui
.comboEncoder
->itemData(ui
.comboEncoder
->currentIndex(), Qt::UserRole
).toString();
132 userSettings
->setValue("encpreset", preset
);
133 preset
= ui
.comboTts
->itemData(ui
.comboTts
->currentIndex(), Qt::UserRole
).toString();
134 userSettings
->setValue("ttspreset", preset
);
137 userSettings
->sync();
139 emit
settingsUpdated();
145 qDebug() << "Config::abort()";
150 void Config::setUserSettings(QSettings
*user
)
154 proxy
= userSettings
->value("defaults/proxy").toString();
157 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
158 else ui
.proxyPort
->setText("");
159 ui
.proxyHost
->setText(proxy
.host());
160 ui
.proxyUser
->setText(proxy
.userName());
161 ui
.proxyPass
->setText(proxy
.password());
163 QString proxyType
= userSettings
->value("defaults/proxytype").toString();
164 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
165 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
166 else ui
.radioNoProxy
->setChecked(true);
168 // set language selection
169 QList
<QListWidgetItem
*> a
;
171 // find key for lang value
172 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
173 while (i
!= lang
.constEnd()) {
174 if(i
.value() == userSettings
->value("defaults/lang").toString() + ".qm") {
180 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
182 a
= ui
.listLanguages
->findItems(DEFAULT_LANG
, Qt::MatchExactly
);
184 ui
.listLanguages
->setCurrentItem(a
.at(0));
187 ui
.mountPoint
->setText(userSettings
->value("defaults/mountpoint").toString());
190 if(!QFileInfo(userSettings
->value("defaults/cachepath").toString()).isDir())
191 userSettings
->setValue("defaults/cachepath", QDir::tempPath());
192 ui
.cachePath
->setText(userSettings
->value("defaults/cachepath").toString());
193 ui
.cacheDisable
->setChecked(userSettings
->value("defaults/cachedisable", true).toBool());
194 ui
.cacheOfflineMode
->setChecked(userSettings
->value("defaults/offline").toBool());
195 QList
<QFileInfo
> fs
= QDir(userSettings
->value("defaults/cachepath").toString() + "/rbutil-cache/").entryInfoList(QDir::Files
| QDir::NoDotAndDotDot
);
197 for(int i
= 0; i
< fs
.size(); i
++) {
198 sz
+= fs
.at(i
).size();
199 qDebug() << fs
.at(i
).fileName() << fs
.at(i
).size();
201 ui
.cacheSize
->setText(tr("Current cache size is %1 kiB.")
207 void Config::setDevices(QSettings
*dev
)
210 // setup devices table
211 qDebug() << "Config::setDevices()";
212 devices
->beginGroup("platforms");
213 QStringList a
= devices
->childKeys();
216 QMap
<QString
, QString
> manuf
;
217 QMap
<QString
, QString
> devcs
;
218 for(int it
= 0; it
< a
.size(); it
++) {
220 devices
->beginGroup("platforms");
221 curdev
= devices
->value(a
.at(it
), "null").toString();
224 devices
->beginGroup(curdev
);
225 curname
= devices
->value("name", "null").toString();
226 QString curbrand
= devices
->value("brand", "").toString();
228 manuf
.insertMulti(curbrand
, curdev
);
229 devcs
.insert(curdev
, curname
);
233 platform
= devcs
.value(userSettings
->value("defaults/platform").toString());
235 // set up devices table
236 ui
.treeDevices
->header()->hide();
237 ui
.treeDevices
->expandAll();
238 ui
.treeDevices
->setColumnCount(1);
239 QList
<QTreeWidgetItem
*> items
;
242 QStringList brands
= manuf
.uniqueKeys();
245 QTreeWidgetItem
*w3
= 0;
246 for(int c
= 0; c
< brands
.size(); c
++) {
247 qDebug() << brands
.at(c
);
248 w
= new QTreeWidgetItem();
249 w
->setFlags(Qt::ItemIsEnabled
);
250 w
->setText(0, brands
.at(c
));
251 // w->setData(0, Qt::DecorationRole, <icon>);
254 // go through platforms again for sake of order
255 for(int it
= 0; it
< a
.size(); it
++) {
257 devices
->beginGroup("platforms");
258 curdev
= devices
->value(a
.at(it
), "null").toString();
261 devices
->beginGroup(curdev
);
262 curname
= devices
->value("name", "null").toString();
263 QString curbrand
= devices
->value("brand", "").toString();
265 if(curbrand
!= brands
.at(c
)) continue;
266 qDebug() << "adding:" << brands
.at(c
) << curname
<< curdev
;
267 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
268 w2
->setData(0, Qt::UserRole
, curdev
);
269 if(platform
.contains(curname
)) {
270 w2
->setSelected(true);
271 w
->setExpanded(true);
272 w3
= w2
; // save pointer to hilight old selection
277 ui
.treeDevices
->insertTopLevelItems(0, items
);
279 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
284 devices
->beginGroup("encoders");
285 keys
= devices
->allKeys();
286 for(int i
=0; i
< keys
.size();i
++)
287 ui
.comboEncoder
->addItem(devices
->value(keys
.at(i
), "null").toString(),
291 devices
->beginGroup("tts");
292 keys
= devices
->allKeys();
293 for(int i
=0; i
< keys
.size();i
++)
294 ui
.comboTts
->addItem(devices
->value(keys
.at(i
), "null").toString(), keys
.at(i
));
298 index
= ui
.comboTts
->findData(userSettings
->value("ttspreset").toString(),
299 Qt::UserRole
, Qt::MatchExactly
);
300 if(index
< 0) index
= 0;
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 if(index
< 0) index
= 0;
308 ui
.comboEncoder
->setCurrentIndex(index
);
309 updateEncOpts(index
);
310 ui
.encoderExecutable
->setText(userSettings
->value("encbin").toString());
315 void Config::updateEncOpts(int index
)
317 qDebug() << "updateEncOpts()";
320 QString c
= ui
.comboEncoder
->itemData(index
, Qt::UserRole
).toString();
321 devices
->beginGroup(c
);
322 ui
.encoderOptions
->setText(devices
->value("options").toString());
323 edit
= devices
->value("edit").toBool();
324 ui
.encoderOptions
->setEnabled(edit
);
325 e
= devices
->value("encoder").toString();
328 // try to autodetect encoder
329 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
330 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
331 #elif defined(Q_OS_WIN)
332 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
335 ui
.encoderExecutable
->setEnabled(true);
336 for(int i
= 0; i
< path
.size(); i
++) {
337 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
338 #if defined(Q_OS_WIN)
339 executable
+= ".exe";
340 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
341 executable
= ex
.join("");
343 if(QFileInfo(executable
).isExecutable()) {
344 qDebug() << "found:" << executable
;
345 ui
.encoderExecutable
->setText(QDir::toNativeSeparators(executable
));
346 // disallow changing the detected path if non-customizable profile
348 ui
.encoderExecutable
->setEnabled(false);
355 void Config::updateTtsOpts(int index
)
359 QString c
= ui
.comboTts
->itemData(index
, Qt::UserRole
).toString();
360 devices
->beginGroup(c
);
361 edit
= devices
->value("edit").toBool();
362 ui
.ttsOptions
->setText(devices
->value("options").toString());
363 ui
.ttsOptions
->setEnabled(devices
->value("edit").toBool());
364 e
= devices
->value("tts").toString();
367 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
368 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
369 #elif defined(Q_OS_WIN)
370 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
373 ui
.ttsExecutable
->setEnabled(true);
374 for(int i
= 0; i
< path
.size(); i
++) {
375 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
376 #if defined(Q_OS_WIN)
377 executable
+= ".exe";
378 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
379 executable
= ex
.join("");
381 qDebug() << executable
;
382 if(QFileInfo(executable
).isExecutable()) {
383 ui
.ttsExecutable
->setText(QDir::toNativeSeparators(executable
));
384 // disallow changing the detected path if non-customizable profile
386 ui
.ttsExecutable
->setEnabled(false);
393 void Config::setNoProxy(bool checked
)
396 ui
.proxyPort
->setEnabled(i
);
397 ui
.proxyHost
->setEnabled(i
);
398 ui
.proxyUser
->setEnabled(i
);
399 ui
.proxyPass
->setEnabled(i
);
403 void Config::setSystemProxy(bool checked
)
406 ui
.proxyPort
->setEnabled(i
);
407 ui
.proxyHost
->setEnabled(i
);
408 ui
.proxyUser
->setEnabled(i
);
409 ui
.proxyPass
->setEnabled(i
);
411 // save values in input box
412 proxy
.setScheme("http");
413 proxy
.setUserName(ui
.proxyUser
->text());
414 proxy
.setPassword(ui
.proxyPass
->text());
415 proxy
.setHost(ui
.proxyHost
->text());
416 proxy
.setPort(ui
.proxyPort
->text().toInt());
417 // show system values in input box
419 QUrl envproxy
= QUrl(getenv("http_proxy"));
420 ui
.proxyHost
->setText(envproxy
.host());
421 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
422 ui
.proxyUser
->setText(envproxy
.userName());
423 ui
.proxyPass
->setText(envproxy
.password());
427 ui
.proxyHost
->setText(proxy
.host());
429 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
430 else ui
.proxyPort
->setText("");
431 ui
.proxyUser
->setText(proxy
.userName());
432 ui
.proxyPass
->setText(proxy
.password());
438 QStringList
Config::findLanguageFiles()
440 QDir
dir(programPath
);
441 QStringList fileNames
;
442 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
444 QDir
resDir(":/lang");
445 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
448 qDebug() << "Config::findLanguageFiles()" << fileNames
;
454 QString
Config::languageName(const QString
&qmFile
)
456 QTranslator translator
;
458 if(!translator
.load(qmFile
, programPath
))
459 translator
.load(qmFile
, ":/lang");
461 return translator
.translate("Configure", "English");
465 void Config::updateLanguage()
467 qDebug() << "updateLanguage()";
468 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
470 language
= QFileInfo(lang
.value(a
.at(0)->text())).baseName();
474 void Config::browseFolder()
476 browser
= new BrowseDirtree(this);
477 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
478 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
479 #elif defined(Q_OS_WIN32)
480 browser
->setFilter(QDir::Drives
);
482 QDir
d(ui
.mountPoint
->text());
485 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
489 void Config::browseCache()
491 cbrowser
= new BrowseDirtree(this);
492 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
493 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
494 #elif defined(Q_OS_WIN32)
495 cbrowser
->setFilter(QDir::Drives
);
497 QDir
d(ui
.cachePath
->text());
500 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
503 void Config::setMountpoint(QString m
)
505 ui
.mountPoint
->setText(m
);
509 void Config::setCache(QString c
)
511 ui
.cachePath
->setText(c
);
515 void Config::autodetect()
517 Autodetection
detector(this);
519 if(detector
.detect()) //let it detect
521 QString devicename
= detector
.getDevice();
522 // deexpand all items
523 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
524 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
525 //deselect the selected item(s)
526 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
527 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
530 // enumerate all platform items
531 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
532 for(int i
=0; i
< itmList
.size();i
++)
534 //enumerate device items
535 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
537 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
539 if(devicename
== data
) // item found
541 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
542 itmList
.at(i
)->setExpanded(true); //expand the platform item
548 if(detector
.getMountPoint() != "" )
550 ui
.mountPoint
->setText(detector
.getMountPoint());
554 QMessageBox::warning(this, tr("Autodetection"),
555 tr("Could not detect a Mountpoint.\n"
556 "Select your Mountpoint manually."),
557 QMessageBox::Ok
,QMessageBox::Ok
);
562 QMessageBox::warning(this, tr("Autodetection"),
563 tr("Could not detect a device.\n"
564 "Select your device and Mountpoint manually."),
565 QMessageBox::Ok
,QMessageBox::Ok
);
570 void Config::cacheClear()
572 if(QMessageBox::critical(this, tr("Really delete cache?"),
573 tr("Do you really want to delete the cache? "
574 "Make absolutely sure this setting is correct as it will "
575 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
576 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
579 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
580 if(!QFileInfo(cache
).isDir()) {
581 QMessageBox::critical(this, tr("Path wrong!"),
582 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
587 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
590 for(int i
= 0; i
< fn
.size(); i
++) {
591 QString f
= cache
+ fn
.at(i
);
593 qDebug() << "removed:" << f
;
598 void Config::browseTts()
600 BrowseDirtree
browser(this);
601 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
603 if(QFileInfo(ui
.ttsExecutable
->text()).isDir())
605 QDir
d(ui
.ttsExecutable
->text());
608 if(browser
.exec() == QDialog::Accepted
)
610 qDebug() << browser
.getSelected();
611 QString exe
= browser
.getSelected();
612 if(!QFileInfo(exe
).isExecutable())
614 ui
.ttsExecutable
->setText(exe
);
620 void Config::browseEnc()
622 BrowseDirtree
browser(this);
623 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
625 if(QFileInfo(ui
.encoderExecutable
->text()).isDir())
627 QDir
d(ui
.encoderExecutable
->text());
630 if(browser
.exec() == QDialog::Accepted
)
632 qDebug() << browser
.getSelected();
633 QString exe
= browser
.getSelected();
634 if(!QFileInfo(exe
).isExecutable())
636 ui
.encoderExecutable
->setText(exe
);