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 (C)"
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
.at(i
)) + tr(" (%1)").arg(langs
.at(i
)), langs
.at(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("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("proxytype", proxyType
);
98 if(userSettings
->value("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("lang", language
);
104 QString mp
= ui
.mountPoint
->text();
105 if(QFileInfo(mp
).isDir())
106 userSettings
->setValue("mountpoint", mp
);
110 if(ui
.treeDevices
->selectedItems().size() != 0) {
111 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
112 userSettings
->setValue("platform", nplat
);
116 if(QFileInfo(ui
.cachePath
->text()).isDir())
117 userSettings
->setValue("cachepath", ui
.cachePath
->text());
118 else // default to system temp path
119 userSettings
->setValue("cachepath", QDir::tempPath());
120 userSettings
->setValue("cachedisable", ui
.cacheDisable
->isChecked());
121 userSettings
->setValue("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("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("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("lang").toString()) {
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("mountpoint").toString());
190 if(!QFileInfo(userSettings
->value("cachepath").toString()).isDir())
191 userSettings
->setValue("cachepath", QDir::tempPath());
192 ui
.cachePath
->setText(userSettings
->value("cachepath").toString());
193 ui
.cacheDisable
->setChecked(userSettings
->value("cachedisable", true).toBool());
194 ui
.cacheOfflineMode
->setChecked(userSettings
->value("offline").toBool());
195 QList
<QFileInfo
> fs
= QDir(userSettings
->value("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("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
));
253 // go through platforms again for sake of order
254 for(int it
= 0; it
< a
.size(); it
++) {
256 devices
->beginGroup("platforms");
257 curdev
= devices
->value(a
.at(it
), "null").toString();
260 devices
->beginGroup(curdev
);
261 curname
= devices
->value("name", "null").toString();
262 QString curbrand
= devices
->value("brand", "").toString();
263 QString curicon
= devices
->value("icon", "").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
);
270 // icon.addFile(":/icons/devices/" + curicon + "-tiny.png");
271 // w2->setIcon(0, icon);
272 // ui.treeDevices->setIconSize(QSize(32, 32));
273 if(platform
.contains(curname
)) {
274 w2
->setSelected(true);
275 w
->setExpanded(true);
276 w3
= w2
; // save pointer to hilight old selection
281 ui
.treeDevices
->insertTopLevelItems(0, items
);
283 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
288 devices
->beginGroup("encoders");
289 keys
= devices
->allKeys();
290 for(int i
=0; i
< keys
.size();i
++)
291 ui
.comboEncoder
->addItem(devices
->value(keys
.at(i
), "null").toString(),
295 devices
->beginGroup("tts");
296 keys
= devices
->allKeys();
297 for(int i
=0; i
< keys
.size();i
++)
298 ui
.comboTts
->addItem(devices
->value(keys
.at(i
), "null").toString(), keys
.at(i
));
302 index
= ui
.comboTts
->findData(userSettings
->value("ttspreset").toString(),
303 Qt::UserRole
, Qt::MatchExactly
);
304 if(index
< 0) index
= 0;
305 ui
.comboTts
->setCurrentIndex(index
);
306 updateTtsOpts(index
);
307 ui
.ttsExecutable
->setText(userSettings
->value("ttsbin").toString());
309 index
= ui
.comboEncoder
->findData(userSettings
->value("encpreset").toString(),
310 Qt::UserRole
, Qt::MatchExactly
);
311 if(index
< 0) index
= 0;
312 ui
.comboEncoder
->setCurrentIndex(index
);
313 updateEncOpts(index
);
314 ui
.encoderExecutable
->setText(userSettings
->value("encbin").toString());
319 void Config::updateEncOpts(int index
)
321 qDebug() << "updateEncOpts()";
324 QString c
= ui
.comboEncoder
->itemData(index
, Qt::UserRole
).toString();
325 devices
->beginGroup(c
);
326 ui
.encoderOptions
->setText(devices
->value("options").toString());
327 edit
= devices
->value("edit").toBool();
328 ui
.encoderOptions
->setEnabled(edit
);
329 e
= devices
->value("encoder").toString();
332 // try to autodetect encoder
333 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
334 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
335 #elif defined(Q_OS_WIN)
336 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
339 ui
.encoderExecutable
->setEnabled(true);
340 for(int i
= 0; i
< path
.size(); i
++) {
341 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
342 #if defined(Q_OS_WIN)
343 executable
+= ".exe";
344 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
345 executable
= ex
.join("");
347 if(QFileInfo(executable
).isExecutable()) {
348 qDebug() << "found:" << executable
;
349 ui
.encoderExecutable
->setText(QDir::toNativeSeparators(executable
));
350 // disallow changing the detected path if non-customizable profile
352 ui
.encoderExecutable
->setEnabled(false);
359 void Config::updateTtsOpts(int index
)
363 QString c
= ui
.comboTts
->itemData(index
, Qt::UserRole
).toString();
364 devices
->beginGroup(c
);
365 edit
= devices
->value("edit").toBool();
366 ui
.ttsOptions
->setText(devices
->value("options").toString());
367 ui
.ttsOptions
->setEnabled(devices
->value("edit").toBool());
368 e
= devices
->value("tts").toString();
371 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
372 QStringList path
= QString(getenv("PATH")).split(":", QString::SkipEmptyParts
);
373 #elif defined(Q_OS_WIN)
374 QStringList path
= QString(getenv("PATH")).split(";", QString::SkipEmptyParts
);
377 ui
.ttsExecutable
->setEnabled(true);
378 for(int i
= 0; i
< path
.size(); i
++) {
379 QString executable
= QDir::fromNativeSeparators(path
.at(i
)) + "/" + e
;
380 #if defined(Q_OS_WIN)
381 executable
+= ".exe";
382 QStringList ex
= executable
.split("\"", QString::SkipEmptyParts
);
383 executable
= ex
.join("");
385 qDebug() << executable
;
386 if(QFileInfo(executable
).isExecutable()) {
387 ui
.ttsExecutable
->setText(QDir::toNativeSeparators(executable
));
388 // disallow changing the detected path if non-customizable profile
390 ui
.ttsExecutable
->setEnabled(false);
397 void Config::setNoProxy(bool checked
)
400 ui
.proxyPort
->setEnabled(i
);
401 ui
.proxyHost
->setEnabled(i
);
402 ui
.proxyUser
->setEnabled(i
);
403 ui
.proxyPass
->setEnabled(i
);
407 void Config::setSystemProxy(bool checked
)
410 ui
.proxyPort
->setEnabled(i
);
411 ui
.proxyHost
->setEnabled(i
);
412 ui
.proxyUser
->setEnabled(i
);
413 ui
.proxyPass
->setEnabled(i
);
415 // save values in input box
416 proxy
.setScheme("http");
417 proxy
.setUserName(ui
.proxyUser
->text());
418 proxy
.setPassword(ui
.proxyPass
->text());
419 proxy
.setHost(ui
.proxyHost
->text());
420 proxy
.setPort(ui
.proxyPort
->text().toInt());
421 // show system values in input box
423 QUrl envproxy
= QUrl(getenv("http_proxy"));
424 ui
.proxyHost
->setText(envproxy
.host());
425 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
426 ui
.proxyUser
->setText(envproxy
.userName());
427 ui
.proxyPass
->setText(envproxy
.password());
431 ui
.proxyHost
->setText(proxy
.host());
433 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
434 else ui
.proxyPort
->setText("");
435 ui
.proxyUser
->setText(proxy
.userName());
436 ui
.proxyPass
->setText(proxy
.password());
442 QStringList
Config::findLanguageFiles()
444 QDir
dir(programPath
);
445 QStringList fileNames
;
447 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
449 QDir
resDir(":/lang");
450 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
452 QRegExp
exp("^rbutil_(.*)\\.qm");
453 for(int i
= 0; i
< fileNames
.size(); i
++) {
454 QString a
= fileNames
.at(i
);
455 a
.replace(exp
, "\\1");
459 qDebug() << "Config::findLanguageFiles()" << langs
;
465 QString
Config::languageName(const QString
&qmFile
)
467 QTranslator translator
;
469 QString file
= "rbutil_" + qmFile
;
470 if(!translator
.load(file
, programPath
))
471 translator
.load(file
, ":/lang");
473 return translator
.translate("Configure", "English");
477 void Config::updateLanguage()
479 qDebug() << "updateLanguage()";
480 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
482 language
= lang
.value(a
.at(0)->text());
483 qDebug() << language
;
487 void Config::browseFolder()
489 browser
= new BrowseDirtree(this);
490 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
491 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
492 #elif defined(Q_OS_WIN32)
493 browser
->setFilter(QDir::Drives
);
495 QDir
d(ui
.mountPoint
->text());
497 #if defined(Q_OS_MACX)
498 browser
->setRoot("/Volumes");
499 #elif defined(Q_OS_LINUX)
500 browser
->setRoot("/Media");
503 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
507 void Config::browseCache()
509 cbrowser
= new BrowseDirtree(this);
510 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
511 cbrowser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
512 #elif defined(Q_OS_WIN32)
513 cbrowser
->setFilter(QDir::Drives
);
515 QDir
d(ui
.cachePath
->text());
518 connect(cbrowser
, SIGNAL(itemChanged(QString
)), this, SLOT(setCache(QString
)));
521 void Config::setMountpoint(QString m
)
523 ui
.mountPoint
->setText(m
);
527 void Config::setCache(QString c
)
529 ui
.cachePath
->setText(c
);
533 void Config::autodetect()
535 Autodetection
detector(this);
537 if(detector
.detect()) //let it detect
539 QString devicename
= detector
.getDevice();
540 // deexpand all items
541 for(int a
= 0; a
< ui
.treeDevices
->topLevelItemCount(); a
++)
542 ui
.treeDevices
->topLevelItem(a
)->setExpanded(false);
543 //deselect the selected item(s)
544 for(int a
= 0; a
< ui
.treeDevices
->selectedItems().size(); a
++)
545 ui
.treeDevices
->selectedItems().at(a
)->setSelected(false);
548 // enumerate all platform items
549 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
550 for(int i
=0; i
< itmList
.size();i
++)
552 //enumerate device items
553 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
555 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
557 if(devicename
== data
) // item found
559 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
560 itmList
.at(i
)->setExpanded(true); //expand the platform item
561 //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
567 if(detector
.getMountPoint() != "" )
569 ui
.mountPoint
->setText(detector
.getMountPoint());
573 QMessageBox::warning(this, tr("Autodetection"),
574 tr("Could not detect a Mountpoint.\n"
575 "Select your Mountpoint manually."),
576 QMessageBox::Ok
,QMessageBox::Ok
);
581 QMessageBox::warning(this, tr("Autodetection"),
582 tr("Could not detect a device.\n"
583 "Select your device and Mountpoint manually."),
584 QMessageBox::Ok
,QMessageBox::Ok
);
589 void Config::cacheClear()
591 if(QMessageBox::critical(this, tr("Really delete cache?"),
592 tr("Do you really want to delete the cache? "
593 "Make absolutely sure this setting is correct as it will "
594 "remove <b>all</b> files in this folder!").arg(ui
.cachePath
->text()),
595 QMessageBox::Yes
| QMessageBox::No
) != QMessageBox::Yes
)
598 QString cache
= ui
.cachePath
->text() + "/rbutil-cache/";
599 if(!QFileInfo(cache
).isDir()) {
600 QMessageBox::critical(this, tr("Path wrong!"),
601 tr("The cache path is invalid. Aborting."), QMessageBox::Ok
);
606 fn
= dir
.entryList(QStringList("*"), QDir::Files
, QDir::Name
);
609 for(int i
= 0; i
< fn
.size(); i
++) {
610 QString f
= cache
+ fn
.at(i
);
612 qDebug() << "removed:" << f
;
617 void Config::browseTts()
619 BrowseDirtree
browser(this);
620 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
622 if(QFileInfo(ui
.ttsExecutable
->text()).isDir())
624 QDir
d(ui
.ttsExecutable
->text());
627 if(browser
.exec() == QDialog::Accepted
)
629 qDebug() << browser
.getSelected();
630 QString exe
= browser
.getSelected();
631 if(!QFileInfo(exe
).isExecutable())
633 ui
.ttsExecutable
->setText(exe
);
639 void Config::browseEnc()
641 BrowseDirtree
browser(this);
642 browser
.setFilter(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
);
644 if(QFileInfo(ui
.encoderExecutable
->text()).isDir())
646 QDir
d(ui
.encoderExecutable
->text());
649 if(browser
.exec() == QDialog::Accepted
)
651 qDebug() << browser
.getSelected();
652 QString exe
= browser
.getSelected();
653 if(!QFileInfo(exe
).isExecutable())
655 ui
.encoderExecutable
->setText(exe
);