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"
31 #define DEFAULT_LANG "English (builtin)"
33 Config::Config(QWidget
*parent
) : QDialog(parent
)
35 programPath
= QFileInfo(qApp
->arguments().at(0)).absolutePath() + "/";
37 ui
.radioManualProxy
->setChecked(true);
38 QRegExpValidator
*proxyValidator
= new QRegExpValidator(this);
39 QRegExp
validate("[0-9]*");
40 proxyValidator
->setRegExp(validate
);
41 ui
.proxyPort
->setValidator(proxyValidator
);
43 ui
.radioSystemProxy
->setEnabled(false); // only on linux for now
45 // build language list and sort alphabetically
46 QStringList langs
= findLanguageFiles();
47 for(int i
= 0; i
< langs
.size(); ++i
)
48 lang
.insert(languageName(langs
[i
]), langs
[i
]);
49 lang
.insert(DEFAULT_LANG
, "");
50 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
51 while (i
!= lang
.constEnd()) {
52 ui
.listLanguages
->addItem(i
.key());
55 ui
.listLanguages
->setSelectionMode(QAbstractItemView::SingleSelection
);
56 connect(ui
.listLanguages
, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));
57 ui
.proxyPass
->setEchoMode(QLineEdit::Password
);
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()));
68 // disable unimplemented stuff
69 ui
.buttonCacheBrowse
->setEnabled(false);
70 ui
.cacheDisable
->setEnabled(false);
71 ui
.cacheOfflineMode
->setEnabled(false);
72 ui
.buttonCacheClear
->setEnabled(false);
74 //ui.buttonAutodetect->setEnabled(false);
81 qDebug() << "Config::accept()";
82 // proxy: save entered proxy values, not displayed.
83 if(ui
.radioManualProxy
->isChecked()) {
84 proxy
.setScheme("http");
85 proxy
.setUserName(ui
.proxyUser
->text());
86 proxy
.setPassword(ui
.proxyPass
->text());
87 proxy
.setHost(ui
.proxyHost
->text());
88 proxy
.setPort(ui
.proxyPort
->text().toInt());
90 userSettings
->setValue("defaults/proxy", proxy
.toString());
91 qDebug() << "new proxy:" << proxy
;
94 if(ui
.radioNoProxy
->isChecked()) proxyType
= "none";
95 else if(ui
.radioSystemProxy
->isChecked()) proxyType
= "system";
96 else proxyType
= "manual";
97 userSettings
->setValue("defaults/proxytype", proxyType
);
100 if(userSettings
->value("defaults/lang").toString() != language
)
101 QMessageBox::information(this, tr("Language changed"),
102 tr("You need to restart the application for the changed language to take effect."));
103 userSettings
->setValue("defaults/lang", language
);
106 QString mp
= ui
.mountPoint
->text();
107 if(QFileInfo(mp
).isDir())
108 userSettings
->setValue("defaults/mountpoint", mp
);
112 nplat
= ui
.treeDevices
->selectedItems().at(0)->data(0, Qt::UserRole
).toString();
113 userSettings
->setValue("defaults/platform", nplat
);
116 userSettings
->sync();
118 emit
settingsUpdated();
124 qDebug() << "Config::abort()";
129 void Config::setUserSettings(QSettings
*user
)
133 QUrl proxy
= userSettings
->value("defaults/proxy").toString();
136 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
137 else ui
.proxyPort
->setText("");
138 ui
.proxyHost
->setText(proxy
.host());
139 ui
.proxyUser
->setText(proxy
.userName());
140 ui
.proxyPass
->setText(proxy
.password());
142 QString proxyType
= userSettings
->value("defaults/proxytype").toString();
143 if(proxyType
== "manual") ui
.radioManualProxy
->setChecked(true);
144 else if(proxyType
== "system") ui
.radioSystemProxy
->setChecked(true);
145 else ui
.radioNoProxy
->setChecked(true);
147 // set language selection
148 QList
<QListWidgetItem
*> a
;
150 // find key for lang value
151 QMap
<QString
, QString
>::const_iterator i
= lang
.constBegin();
152 while (i
!= lang
.constEnd()) {
153 if(i
.value() == userSettings
->value("defaults/lang").toString() + ".qm") {
159 a
= ui
.listLanguages
->findItems(b
, Qt::MatchExactly
);
161 a
= ui
.listLanguages
->findItems(DEFAULT_LANG
, Qt::MatchExactly
);
163 ui
.listLanguages
->setCurrentItem(a
.at(0));
166 ui
.mountPoint
->setText(userSettings
->value("defaults/mountpoint").toString());
171 void Config::setDevices(QSettings
*dev
)
174 // setup devices table
175 qDebug() << "Config::setDevices()";
176 devices
->beginGroup("platforms");
177 QStringList a
= devices
->childKeys();
180 QMap
<QString
, QString
> manuf
;
181 QMap
<QString
, QString
> devcs
;
182 for(int it
= 0; it
< a
.size(); it
++) {
184 devices
->beginGroup("platforms");
185 curdev
= devices
->value(a
.at(it
), "null").toString();
188 devices
->beginGroup(curdev
);
189 curname
= devices
->value("name", "null").toString();
190 QString curbrand
= devices
->value("brand", "").toString();
192 manuf
.insertMulti(curbrand
, curdev
);
193 devcs
.insert(curdev
, curname
);
197 platform
= devcs
.value(userSettings
->value("defaults/platform").toString());
199 // set up devices table
200 ui
.treeDevices
->header()->hide();
201 ui
.treeDevices
->expandAll();
202 ui
.treeDevices
->setColumnCount(1);
203 QList
<QTreeWidgetItem
*> items
;
206 QStringList brands
= manuf
.uniqueKeys();
209 QTreeWidgetItem
*w3
= 0;
210 for(int c
= 0; c
< brands
.size(); c
++) {
211 qDebug() << brands
.at(c
);
212 w
= new QTreeWidgetItem();
213 w
->setFlags(Qt::ItemIsEnabled
);
214 w
->setText(0, brands
.at(c
));
215 // w->setData(0, Qt::DecorationRole, <icon>);
218 // go through platforms again for sake of order
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 if(curbrand
!= brands
.at(c
)) continue;
230 qDebug() << "adding:" << brands
.at(c
) << curname
<< curdev
;
231 w2
= new QTreeWidgetItem(w
, QStringList(curname
));
232 w2
->setData(0, Qt::UserRole
, curdev
);
233 if(platform
.contains(curname
)) {
234 w2
->setSelected(true);
235 w
->setExpanded(true);
236 w3
= w2
; // save pointer to hilight old selection
241 ui
.treeDevices
->insertTopLevelItems(0, items
);
243 ui
.treeDevices
->setCurrentItem(w3
); // hilight old selection
247 void Config::setNoProxy(bool checked
)
250 ui
.proxyPort
->setEnabled(i
);
251 ui
.proxyHost
->setEnabled(i
);
252 ui
.proxyUser
->setEnabled(i
);
253 ui
.proxyPass
->setEnabled(i
);
257 void Config::setSystemProxy(bool checked
)
260 ui
.proxyPort
->setEnabled(i
);
261 ui
.proxyHost
->setEnabled(i
);
262 ui
.proxyUser
->setEnabled(i
);
263 ui
.proxyPass
->setEnabled(i
);
265 // save values in input box
266 proxy
.setScheme("http");
267 proxy
.setUserName(ui
.proxyUser
->text());
268 proxy
.setPassword(ui
.proxyPass
->text());
269 proxy
.setHost(ui
.proxyHost
->text());
270 proxy
.setPort(ui
.proxyPort
->text().toInt());
271 // show system values in input box
273 QUrl envproxy
= QUrl(getenv("http_proxy"));
274 ui
.proxyHost
->setText(envproxy
.host());
275 ui
.proxyPort
->setText(QString("%1").arg(envproxy
.port()));
276 ui
.proxyUser
->setText(envproxy
.userName());
277 ui
.proxyPass
->setText(envproxy
.password());
281 ui
.proxyHost
->setText(proxy
.host());
283 ui
.proxyPort
->setText(QString("%1").arg(proxy
.port()));
284 else ui
.proxyPort
->setText("");
285 ui
.proxyUser
->setText(proxy
.userName());
286 ui
.proxyPass
->setText(proxy
.password());
292 QStringList
Config::findLanguageFiles()
294 QDir
dir(programPath
+ "/");
295 QStringList fileNames
;
296 fileNames
= dir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
298 QDir
resDir(":/lang");
299 fileNames
+= resDir
.entryList(QStringList("*.qm"), QDir::Files
, QDir::Name
);
302 qDebug() << "Config::findLanguageFiles()" << fileNames
;
308 QString
Config::languageName(const QString
&qmFile
)
310 QTranslator translator
;
312 if(!translator
.load(qmFile
, programPath
))
313 translator
.load(qmFile
, ":/lang");
315 return translator
.translate("Configure", "English");
319 void Config::updateLanguage()
321 qDebug() << "updateLanguage()";
322 QList
<QListWidgetItem
*> a
= ui
.listLanguages
->selectedItems();
324 language
= QFileInfo(lang
.value(a
.at(0)->text())).baseName();
328 void Config::browseFolder()
330 browser
= new BrowseDirtree(this);
331 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
332 browser
->setFilter(QDir::AllDirs
| QDir::NoDotAndDotDot
| QDir::NoSymLinks
);
333 #elif defined(Q_OS_WIN32)
334 browser
->setFilter(QDir::Drives
);
336 QDir
d(ui
.mountPoint
->text());
339 connect(browser
, SIGNAL(itemChanged(QString
)), this, SLOT(setMountpoint(QString
)));
343 void Config::setMountpoint(QString m
)
345 ui
.mountPoint
->setText(m
);
349 void Config::autodetect()
351 Autodetection
detector(this);
353 if(detector
.detect()) //let it detect
355 QString devicename
= detector
.getDevice();
356 //deexpand the platform
357 ui
.treeDevices
->selectedItems().at(0)->parent()->setExpanded(false);
358 //deselect the selected item
359 ui
.treeDevices
->selectedItems().at(0)->setSelected(false);
362 //enumerate al plattform items
363 QList
<QTreeWidgetItem
*> itmList
= ui
.treeDevices
->findItems("*",Qt::MatchWildcard
);
364 for(int i
=0; i
< itmList
.size();i
++)
366 //enumerate device items
367 for(int j
=0;j
< itmList
.at(i
)->childCount();j
++)
369 QString data
= itmList
.at(i
)->child(j
)->data(0, Qt::UserRole
).toString();
371 if( devicename
.contains(data
)) //item found
373 itmList
.at(i
)->child(j
)->setSelected(true); //select the item
374 itmList
.at(i
)->setExpanded(true); //expand the platform item
380 if(detector
.getMountPoint() != "" )
382 ui
.mountPoint
->setText(detector
.getMountPoint());
386 QMessageBox::warning(this, tr("Autodetection"),
387 tr("Could not detect a Mountpoint.\n"
388 "Select your Mountpoint manually."),
389 QMessageBox::Ok
,QMessageBox::Ok
);
394 QMessageBox::warning(this, tr("Autodetection"),
395 tr("Could not detect a device.\n"
396 "Select your device and Mountpoint manually."),
397 QMessageBox::Ok
,QMessageBox::Ok
);