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 ui
.comboTts
->setCurrentIndex(index
);
301 updateTtsOpts(index
);
302 ui
.ttsExecutable
->setText(userSettings
->value("ttsbin").toString());
304 index
= ui
.comboEncoder
->findData(userSettings
->value("encpreset").toString(),
305 Qt::UserRole
, Qt::MatchExactly
);
306 ui
.comboEncoder
->setCurrentIndex(index
);
307 updateEncOpts(index
);
308 ui
.encoderExecutable
->setText(userSettings
->value("encbin").toString());
313 void Config::updateEncOpts(int index
)
315 qDebug() << "updateEncOpts()";
318 QString c
= ui
.comboEncoder
->itemData(index
, Qt::UserRole
).toString();
319 devices
->beginGroup(c
);
320 ui
.encoderOptions
->setText(devices
->value("options").toString());
321 edit
= devices
->value("edit").toBool();
322 ui
.encoderOptions
->setEnabled(edit
);
323 e
= devices
->value("encoder").toString();
326 // try to autodetect encoder
327 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
328 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
329 #elif defined(Q_OS_WIN)
330 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
333 ui
.encoderExecutable
->setEnabled(true);
334 for(int i
= 0; i
< path
.size(); i
++) {
335 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
336 #if defined(Q_OS_WIN)
337 executable
+= ".exe";
338 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
339 executable
= ex
.join("");
341 if(QFileInfo(executable
).isExecutable()) {
342 qDebug() << "found:" << executable
;
343 ui
.encoderExecutable
->setText(QDir::toNativeSeparators(executable
));
344 // disallow changing the detected path if non-customizable profile
346 ui
.encoderExecutable
->setEnabled(false);
353 void Config::updateTtsOpts(int index
)
357 QString c
= ui
.comboTts
->itemData(index
, Qt::UserRole
).toString();
358 devices
->beginGroup(c
);
359 edit
= devices
->value("edit").toBool();
360 ui
.ttsOptions
->setText(devices
->value("options").toString());
361 ui
.ttsOptions
->setEnabled(devices
->value("edit").toBool());
362 e
= devices
->value("tts").toString();
365 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
366 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
367 #elif defined(Q_OS_WIN)
368 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
371 ui
.ttsExecutable
->setEnabled(true);
372 for(int i
= 0; i
< path
.size(); i
++) {
373 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
374 #if defined(Q_OS_WIN)
375 executable
+= ".exe";
376 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
377 executable
= ex
.join("");
379 qDebug() << executable
;
380 if(QFileInfo(executable
).isExecutable()) {
381 ui
.ttsExecutable
->setText(QDir::toNativeSeparators(executable
));
382 // disallow changing the detected path if non-customizable profile
384 ui
.ttsExecutable
->setEnabled(false);
391 void Config::setNoProxy(bool checked
)
394 ui
.proxyPort
->setEnabled(i
);
395 ui
.proxyHost
->setEnabled(i
);
396 ui
.proxyUser
->setEnabled(i
);
397 ui
.proxyPass
->setEnabled(i
);
401 void Config::setSystemProxy(bool checked
)
404 ui
.proxyPort
->setEnabled(i
);
405 ui
.proxyHost
->setEnabled(i
);
406 ui
.proxyUser
->setEnabled(i
);
407 ui
.proxyPass
->setEnabled(i
);
409 // save values in input box
410 proxy
.setScheme("http");
411 proxy
.setUserName(ui
.proxyUser
->text());
412 proxy
.setPassword(ui
.proxyPass
->text());
413 proxy
.setHost(ui
.proxyHost
->text());
414 proxy
.setPort(ui
.proxyPort
->text().toInt());
415 // show system values in input box
417 QUrl envproxy
= QUrl(getenv("http_proxy"));
418 ui
.proxyHost
->setText(envproxy
.host());
419 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
420 ui
.proxyUser
->setText(envproxy
.userName());
421 ui
.proxyPass
->setText(envproxy
.password());
425 ui
.proxyHost
->setText(proxy
.host());
427 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
428 else ui
.proxyPort
->setText("");
429 ui
.proxyUser
->setText(proxy
.userName());
430 ui
.proxyPass
->setText(proxy
.password());
436 QStringList
Config::findLanguageFiles()
438 QDir
dir(programPath
);
439 QStringList fileNames
;
440 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
442 QDir
resDir(":/lang");
443 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
446 qDebug() << "Config::findLanguageFiles()" << fileNames
;
452 QString
Config::languageName(const QString
&qmFile
)
454 QTranslator translator
;
456 if(!translator
.load(qmFile
, programPath
))
457 translator
.load(qmFile
, ":/lang");
459 return translator
.translate("Configure", "English");
463 void Config::updateLanguage()
465 qDebug() << "updateLanguage()";
466 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
468 language
= QFileInfo(lang
.value(a
.at(0)->text())).baseName();
472 void Config::browseFolder()
474 browser
= new BrowseDirtree(this);
475 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
476 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
477 #elif defined(Q_OS_WIN32)
478 browser
->setFilter(QDir::Drives
);
480 QDir
d(ui
.mountPoint
->text());
483 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
487 void Config::browseCache()
489 cbrowser
= new BrowseDirtree(this);
490 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
491 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
492 #elif defined(Q_OS_WIN32)
493 cbrowser
->setFilter(QDir::Drives
);
495 QDir
d(ui
.cachePath
->text());
498 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
501 void Config::setMountpoint(QString m
)
503 ui
.mountPoint
->setText(m
);
507 void Config::setCache(QString c
)
509 ui
.cachePath
->setText(c
);
513 void Config::autodetect()
515 Autodetection
detector(this);
517 if(detector
.detect()) //let it detect
519 QString devicename
= detector
.getDevice();
521 if(ui
.treeDevices
->selectedItems().size() > 0) {
522 // deexpand the platform
523 ui
.treeDevices
->selectedItems().at(0)->parent()->setExpanded(false);
524 //deselect the selected item
525 ui
.treeDevices
->selectedItems().at(0)->setSelected(false);
529 // enumerate all platform items
530 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
531 for(int i
=0; i
< itmList
.size();i
++)
533 //enumerate device items
534 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
536 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
538 if(devicename
== data
) // item found
540 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
541 itmList
.at(i
)->setExpanded(true); //expand the platform item
547 if(detector
.getMountPoint() != "" )
549 ui
.mountPoint
->setText(detector
.getMountPoint());
553 QMessageBox::warning(this, tr("Autodetection"),
554 tr("Could not detect a Mountpoint.\n"
555 "Select your Mountpoint manually."),
556 QMessageBox::Ok
,QMessageBox::Ok
);
561 QMessageBox::warning(this, tr("Autodetection"),
562 tr("Could not detect a device.\n"
563 "Select your device and Mountpoint manually."),
564 QMessageBox::Ok
,QMessageBox::Ok
);
569 void Config::cacheClear()
571 if(QMessageBox::critical(this, tr("Really delete cache?"),
572 tr("Do you really want to delete the cache? "
573 "Make absolutely sure this setting is correct as it will "
574 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
575 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
578 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
579 if(!QFileInfo(cache
).isDir()) {
580 QMessageBox::critical(this, tr("Path wrong!"),
581 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
586 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
589 for(int i
= 0; i
< fn
.size(); i
++) {
590 QString f
= cache
+ fn
.at(i
);
592 qDebug() << "removed:" << f
;
597 void Config::browseTts()
599 BrowseDirtree
browser(this);
600 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
602 if(QFileInfo(ui
.ttsExecutable
->text()).isDir())
604 QDir
d(ui
.ttsExecutable
->text());
607 if(browser
.exec() == QDialog::Accepted
)
609 qDebug() << browser
.getSelected();
610 QString exe
= browser
.getSelected();
611 if(!QFileInfo(exe
).isExecutable())
613 ui
.ttsExecutable
->setText(exe
);
619 void Config::browseEnc()
621 BrowseDirtree
browser(this);
622 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
624 if(QFileInfo(ui
.encoderExecutable
->text()).isDir())
626 QDir
d(ui
.encoderExecutable
->text());
629 if(browser
.exec() == QDialog::Accepted
)
631 qDebug() << browser
.getSelected();
632 QString exe
= browser
.getSelected();
633 if(!QFileInfo(exe
).isExecutable())
635 ui
.encoderExecutable
->setText(exe
);