summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/configure.cpp
blob: fc67cb410eed8cd65d5a1398946642ba34d27c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Riebeling
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <QMessageBox>
#include <QProgressDialog>
#include <QFileDialog>
#include <QUrl>
#ifdef QT_MULTIMEDIA_LIB
#include <QSound>
#endif

#include "version.h"
#include "configure.h"
#include "autodetection.h"
#include "ui_configurefrm.h"
#include "encoderbase.h"
#include "ttsbase.h"
#include "system.h"
#include "encttscfggui.h"
#include "rbsettings.h"
#include "serverinfo.h"
#include "systeminfo.h"
#include "utils.h"
#include "comboboxviewdelegate.h"
#if defined(Q_OS_WIN32)
#if defined(UNICODE)
#define _UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#endif
#include "rbutilqt.h"

#include "systrace.h"
#include "Logger.h"

#define DEFAULT_LANG "English (en)"
#define DEFAULT_LANG_CODE "en"

Config::Config(QWidget *parent,int index) : QDialog(parent)
{
    programPath = qApp->applicationDirPath() + "/";
    ui.setupUi(this);
    ui.tabConfiguration->setCurrentIndex(index);
    ui.radioManualProxy->setChecked(true);

    // build language list and sort alphabetically
    QStringList langs = findLanguageFiles();
    for(int i = 0; i < langs.size(); ++i)
        lang.insert(languageName(langs.at(i))
            + QString(" (%1)").arg(langs.at(i)), langs.at(i));
    lang.insert(DEFAULT_LANG, DEFAULT_LANG_CODE);
    QMap<QString, QString>::const_iterator i = lang.constBegin();
    while (i != lang.constEnd()) {
        ui.listLanguages->addItem(i.key());
        i++;
    }

    ComboBoxViewDelegate *delegate = new ComboBoxViewDelegate(this);
    ui.mountPoint->setItemDelegate(delegate);
#if !defined(DBG)
    ui.mountPoint->setEditable(false);
#endif

    ui.listLanguages->setSelectionMode(QAbstractItemView::SingleSelection);
    ui.proxyPass->setEchoMode(QLineEdit::Password);
    ui.treeDevices->setAlternatingRowColors(true);
    ui.listLanguages->setAlternatingRowColors(true);

    /* Explicitly set some widgets to have left-to-right layout */
    ui.treeDevices->setLayoutDirection(Qt::LeftToRight);
    ui.mountPoint->setLayoutDirection(Qt::LeftToRight);
    ui.proxyHost->setLayoutDirection(Qt::LeftToRight);
    ui.proxyPort->setLayoutDirection(Qt::LeftToRight);
    ui.proxyUser->setLayoutDirection(Qt::LeftToRight);
    ui.proxyPass->setLayoutDirection(Qt::LeftToRight);
    ui.listLanguages->setLayoutDirection(Qt::LeftToRight);
    ui.cachePath->setLayoutDirection(Qt::LeftToRight);
    ui.comboTts->setLayoutDirection(Qt::LeftToRight);

    this->setModal(true);

    connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(abort()));
    connect(ui.radioNoProxy, SIGNAL(toggled(bool)), this, SLOT(setNoProxy(bool)));
    connect(ui.radioSystemProxy, SIGNAL(toggled(bool)), this, SLOT(setSystemProxy(bool)));
    connect(ui.refreshMountPoint, SIGNAL(clicked()), this, SLOT(refreshMountpoint()));
    connect(ui.buttonAutodetect,SIGNAL(clicked()),this,SLOT(autodetect()));
    connect(ui.buttonCacheBrowse, SIGNAL(clicked()), this, SLOT(browseCache()));
    connect(ui.buttonCacheClear, SIGNAL(clicked()), this, SLOT(cacheClear()));
    connect(ui.configTts, SIGNAL(clicked()), this, SLOT(configTts()));
    connect(ui.configEncoder, SIGNAL(clicked()), this, SLOT(configEnc()));
    connect(ui.comboTts, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTtsState(int)));
    connect(ui.treeDevices, SIGNAL(itemSelectionChanged()), this, SLOT(updateEncState()));
    connect(ui.testTTS,SIGNAL(clicked()),this,SLOT(testTts()));
    connect(ui.showDisabled, SIGNAL(toggled(bool)), this, SLOT(showDisabled(bool)));
    connect(ui.mountPoint, SIGNAL(editTextChanged(QString)), this, SLOT(updateMountpoint(QString)));
    connect(ui.mountPoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMountpoint(int)));
    connect(ui.checkShowProxyPassword, SIGNAL(toggled(bool)), this, SLOT(showProxyPassword(bool)));
    // delete this dialog after it finished automatically.
    connect(this, SIGNAL(finished(int)), this, SLOT(deleteLater()));

    setUserSettings();
    setDevices();
}


void Config::accept()
{
    LOG_INFO() << "checking configuration";
    QString errormsg = tr("The following errors occurred:") + "<ul>";
    bool error = false;

    // proxy: save entered proxy values, not displayed.
    if(ui.radioManualProxy->isChecked()) {
        proxy.setScheme("http");
        proxy.setUserName(ui.proxyUser->text());
        proxy.setPassword(ui.proxyPass->text());
        proxy.setHost(ui.proxyHost->text());
        proxy.setPort(ui.proxyPort->value());
    }

    // Encode the password using base64 before storing it to the configuration
    // file.
    // There are two reasons for doing this:
    // - QUrl::toEncoded() has problems with some characters like the colon and
    //   @. Those are not percent encoded, causing the string getting parsed
    //   wrongly when reading it back (see FS#12166).
    // - The password is cleartext in the configuration file.
    //   While using base64 doesn't provide any real security either it's at
    //   least better than plaintext.
    //   Since this program is open source any fixed mechanism to obfuscate /
    //   encrypt the password isn't much help either since anyone interested in
    //   the password can look at the sources. The best way would be to
    //   eventually use host OS functionality to store the password.
    QUrl p = proxy;
    p.setPassword(proxy.password().toUtf8().toBase64());
    RbSettings::setValue(RbSettings::Proxy, p.toString());
    LOG_INFO() << "setting proxy to:" << proxy.toString(QUrl::RemovePassword);
    // proxy type
    QString proxyType;
    if(ui.radioNoProxy->isChecked()) proxyType = "none";
    else if(ui.radioSystemProxy->isChecked()) proxyType = "system";
    else proxyType = "manual";
    RbSettings::setValue(RbSettings::ProxyType, proxyType);

    RbSettings::setValue(RbSettings::Language, language);

    // make sure mountpoint is read from dropdown box
    if(mountpoint.isEmpty()) {
        updateMountpoint(ui.mountPoint->currentIndex());
    }

    // mountpoint
    if(mountpoint.isEmpty()) {
        errormsg += "<li>" + tr("No mountpoint given") + "</li>";
        error = true;
    }
    else if(!QFileInfo(mountpoint).exists()) {
        errormsg += "<li>" + tr("Mountpoint does not exist") + "</li>";
        error = true;
    }
    else if(!QFileInfo(mountpoint).isDir()) {
        errormsg += "<li>" + tr("Mountpoint is not a directory.") + "</li>";
        error = true;
    }
    else if(!QFileInfo(mountpoint).isWritable()) {
        errormsg += "<li>" + tr("Mountpoint is not writeable") + "</li>";
        error = true;
    }
    else {
        RbSettings::setValue(RbSettings::Mountpoint,
                QDir::fromNativeSeparators(mountpoint));
    }

    // platform
    QString nplat;
    if(ui.treeDevices->selectedItems().size() != 0) {
        nplat = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
        RbSettings::setValue(RbSettings::Platform, nplat);
    }
    else {
        errormsg += "<li>" + tr("No player selected") + "</li>";
        error = true;
    }

    // cache settings
    if(QFileInfo(ui.cachePath->text()).isDir()) {
        if(!QFileInfo(ui.cachePath->text()).isWritable()) {
            errormsg += "<li>" + tr("Cache path not writeable. Leave path empty "
                        "to default to systems temporary path.") + "</li>";
            error = true;
        }
        else
            RbSettings::setValue(RbSettings::CachePath, ui.cachePath->text());
    }
    else // default to system temp path
        RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
    RbSettings::setValue(RbSettings::CacheDisabled, ui.cacheDisable->isChecked());

    // tts settings
    RbSettings::setValue(RbSettings::UseTtsCorrections, ui.ttsCorrections->isChecked());
    int i = ui.comboTts->currentIndex();
    RbSettings::setValue(RbSettings::Tts, ui.comboTts->itemData(i).toString());

    RbSettings::setValue(RbSettings::RbutilVersion, PUREVERSION);

    errormsg += "</ul>";
    errormsg += tr("You need to fix the above errors before you can continue.");

    if(error) {
        QMessageBox::critical(this, tr("Configuration error"), errormsg);
    }
    else {
        // sync settings
        RbSettings::sync();
        this->close();
        emit settingsUpdated();
    }
}


void Config::abort()
{
    LOG_INFO() << "aborted.";
    this->close();
}


void Config::setUserSettings()
{
    // set proxy
    proxy.setUrl(RbSettings::value(RbSettings::Proxy).toString(),
            QUrl::StrictMode);
    // password is base64 encoded in configuration.
    QByteArray pw = QByteArray::fromBase64(proxy.password().toUtf8());
    proxy.setPassword(pw);

    ui.proxyPort->setValue(proxy.port());
    ui.proxyHost->setText(proxy.host());
    ui.proxyUser->setText(proxy.userName());
    ui.proxyPass->setText(proxy.password());

    QString proxyType = RbSettings::value(RbSettings::ProxyType).toString();
    if(proxyType == "manual") ui.radioManualProxy->setChecked(true);
    else if(proxyType == "system") ui.radioSystemProxy->setChecked(true);
    else ui.radioNoProxy->setChecked(true);

    // set language selection
    QList<QListWidgetItem*> a;
    QString b;
    // find key for lang value
    QMap<QString, QString>::const_iterator i = lang.constBegin();
    QString l = RbSettings::value(RbSettings::Language).toString();
    if(l.isEmpty())
        l = QLocale::system().name();
    while (i != lang.constEnd()) {
        if(i.value() == l) {
            b = i.key();
            break;
        }
        else if(l.startsWith(i.value(), Qt::CaseInsensitive)) {
            // check if there is a base language (en -> en_US, etc.)
            b = i.key();
            break;
        }
        i++;
    }
    a = ui.listLanguages->findItems(b, Qt::MatchExactly);
    if(a.size() > 0)
        ui.listLanguages->setCurrentItem(a.at(0));
    // don't connect before language list has been set up to prevent
    // triggering the signal by selecting the saved language.
    connect(ui.listLanguages, SIGNAL(itemSelectionChanged()), this, SLOT(updateLanguage()));

    // devices tab
    refreshMountpoint();
    mountpoint = QDir::toNativeSeparators(RbSettings::value(RbSettings::Mountpoint).toString());
    setMountpoint(mountpoint);

    // cache tab
    if(!QFileInfo(RbSettings::value(RbSettings::CachePath).toString()).isDir())
        RbSettings::setValue(RbSettings::CachePath, QDir::tempPath());
    ui.cachePath->setText(QDir::toNativeSeparators(RbSettings::value(RbSettings::CachePath).toString()));
    ui.cacheDisable->setChecked(RbSettings::value(RbSettings::CacheDisabled).toBool());
    updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());

    // TTS tab
    ui.ttsCorrections->setChecked(RbSettings::value(RbSettings::UseTtsCorrections).toBool());
}


void Config::updateCacheInfo(QString path)
{
    QList<QFileInfo> fs;
    fs = QDir(path + "/rbutil-cache/").entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
    qint64 sz = 0;
    for(int i = 0; i < fs.size(); i++) {
        sz += fs.at(i).size();
    }
    ui.cacheSize->setText(tr("Current cache size is %L1 kiB.")
            .arg(sz/1024));
}


void Config::showProxyPassword(bool show)
{
    if(show)
        ui.proxyPass->setEchoMode(QLineEdit::Normal);
    else
        ui.proxyPass->setEchoMode(QLineEdit::Password);
}


void Config::showDisabled(bool show)
{
    LOG_INFO() << "disabled targets shown:" << show;
    if(show)
        QMessageBox::warning(this, tr("Showing disabled targets"),
                tr("You just enabled showing targets that are marked disabled. "
                   "Disabled targets are not recommended to end users. Please "
                   "use this option only if you know what you are doing."));
    setDevices();

}


void Config::setDevices()
{

    // setup devices table
    LOG_INFO() << "setting up devices list";

    QStringList platformList;
    if(ui.showDisabled->isChecked())
        platformList = SystemInfo::platforms(SystemInfo::PlatformAllDisabled);
    else
        platformList = SystemInfo::platforms(SystemInfo::PlatformAll);

    QMultiMap <QString, QString> manuf;
    for(int it = 0; it < platformList.size(); it++)
    {
        QString curbrand = SystemInfo::platformValue(
                    SystemInfo::Brand, platformList.at(it)).toString();
        manuf.insert(curbrand, platformList.at(it));
    }

    // set up devices table
    ui.treeDevices->header()->hide();
    ui.treeDevices->expandAll();
    ui.treeDevices->setColumnCount(1);
    QList<QTreeWidgetItem *> items;

    // get manufacturers
    QStringList brands = manuf.uniqueKeys();
    QTreeWidgetItem *w;
    QTreeWidgetItem *w2;
    QTreeWidgetItem *w3 = 0;

    QString selected = RbSettings::value(RbSettings::Platform).toString();
    for(int c = 0; c < brands.size(); c++) {
        w = new QTreeWidgetItem();
        w->setFlags(Qt::ItemIsEnabled);
        w->setText(0, brands.at(c));
        items.append(w);
        // go through platforms and add all players matching the current brand
        for(int it = 0; it < platformList.size(); it++) {
            // skip if not current brand
            if(!manuf.values(brands.at(c)).contains(platformList.at(it)))
                continue;
            // construct display name
            QString curname = SystemInfo::platformValue(
                SystemInfo::Name, platformList.at(it)).toString()
                + " (" + ServerInfo::instance()->statusAsString(platformList.at(it)) + ")";
            LOG_INFO() << "add supported device:" << brands.at(c) << curname;
            w2 = new QTreeWidgetItem(w, QStringList(curname));
            w2->setData(0, Qt::UserRole, platformList.at(it));

            if(platformList.at(it) == selected) {
                w2->setSelected(true);
                w->setExpanded(true);
                w3 = w2; // save pointer to hilight old selection
            }
            items.append(w2);
        }
    }
    // remove any old items in list
    QTreeWidgetItem* widgetitem;
    do {
        widgetitem = ui.treeDevices->takeTopLevelItem(0);
        delete widgetitem;
    }
    while(widgetitem);
    // add new items
    ui.treeDevices->insertTopLevelItems(0, items);
    if(w3 != 0) {
        ui.treeDevices->setCurrentItem(w3); // hilight old selection
        ui.treeDevices->scrollToItem(w3);
    }

    // tts / encoder tab

    //encoders
    updateEncState();

    //tts
    QStringList ttslist = TTSBase::getTTSList();
    for(int a = 0; a < ttslist.size(); a++)
        ui.comboTts->addItem(TTSBase::getTTSName(ttslist.at(a)), ttslist.at(a));
    //update index of combobox
    int index = ui.comboTts->findData(RbSettings::value(RbSettings::Tts).toString());
    if(index < 0) index = 0;
    ui.comboTts->setCurrentIndex(index);
    updateTtsState(index);

}


void Config::updateTtsState(int index)
{
    QString ttsName = ui.comboTts->itemData(index).toString();
    TTSBase* tts = TTSBase::getTTS(this,ttsName);

    if(!tts)
    {
        QMessageBox::critical(this, tr("TTS error"),
            tr("The selected TTS failed to initialize. You can't use this TTS."));
        return;
    }

    if(tts->configOk())
    {
        ui.configTTSstatus->setText(tr("Configuration OK"));
        ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.svg")));
#ifdef QT_MULTIMEDIA_LIB
        ui.testTTS->setEnabled(true);
#else
        ui.testTTS->setEnabled(false);
#endif
    }
    else
    {
        ui.configTTSstatus->setText(tr("Configuration INVALID"));
        ui.configTTSstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.svg")));
        ui.testTTS->setEnabled(false);
    }

    delete tts; /* Config objects are never deleted (in fact, they are leaked..), so we can't rely on QObject,
                   since that would delete the TTSBase instance on application exit*/
}

void Config::updateEncState()
{
    if(ui.treeDevices->selectedItems().size() == 0)
        return;

    QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
    QString encoder = SystemInfo::platformValue(
                        SystemInfo::Encoder, devname).toString();
    ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(
                        SystemInfo::Encoder, devname).toString()));

    EncoderBase* enc = EncoderBase::getEncoder(this,encoder);

    if(enc->configOk())
    {
        ui.configEncstatus->setText(tr("Configuration OK"));
        ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/go-next.svg")));
    }
    else
    {
        ui.configEncstatus->setText(tr("Configuration INVALID"));
        ui.configEncstatusimg->setPixmap(QPixmap(QString::fromUtf8(":/icons/dialog-error.svg")));
    }
}


void Config::setNoProxy(bool checked)
{
    ui.proxyPort->setEnabled(!checked);
    ui.proxyHost->setEnabled(!checked);
    ui.proxyUser->setEnabled(!checked);
    ui.proxyPass->setEnabled(!checked);
    ui.checkShowProxyPassword->setEnabled(!checked);
    ui.checkShowProxyPassword->setChecked(false);
    showProxyPassword(false);
}


void Config::setSystemProxy(bool checked)
{
    setNoProxy(checked);
    if(checked) {
        // save values in input box
        proxy.setScheme("http");
        proxy.setUserName(ui.proxyUser->text());
        proxy.setPassword(ui.proxyPass->text());
        proxy.setHost(ui.proxyHost->text());
        proxy.setPort(ui.proxyPort->value());
        // show system values in input box
        QUrl envproxy = System::systemProxy();
        LOG_INFO() << "setting system proxy" << envproxy;

        ui.proxyHost->setText(envproxy.host());
        ui.proxyPort->setValue(envproxy.port());
        ui.proxyUser->setText(envproxy.userName());
        ui.proxyPass->setText(envproxy.password());

        if(envproxy.host().isEmpty() || envproxy.port() == -1) {
            LOG_WARNING() << "system proxy is invalid.";
            QMessageBox::warning(this, tr("Proxy Detection"),
                    tr("The System Proxy settings are invalid!\n"
                        "Rockbox Utility can't work with this proxy settings. "
                        "Make sure the system proxy is set correctly. Note that "
                        "\"proxy auto-config (PAC)\" scripts are not supported by "
                        "Rockbox Utility. If your system uses this you need "
                        "to use manual proxy settings."),
                    QMessageBox::Ok ,QMessageBox::Ok);
            // the current proxy settings are invalid. Check the saved proxy
            // type again.
            if(RbSettings::value(RbSettings::ProxyType).toString() == "manual")
                ui.radioManualProxy->setChecked(true);
            else
                ui.radioNoProxy->setChecked(true);
        }

    }
    else {
        ui.proxyHost->setText(proxy.host());
        ui.proxyPort->setValue(proxy.port());
        ui.proxyUser->setText(proxy.userName());
        ui.proxyPass->setText(proxy.password());
    }

}


QStringList Config::findLanguageFiles()
{
    QDir dir(programPath);
    QStringList fileNames;
    QStringList langs;
    fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);

    QDir resDir(":/lang");
    fileNames += resDir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);

    QRegExp exp("^rbutil_(.*)\\.qm");
    for(int i = 0; i < fileNames.size(); i++) {
        QString a = fileNames.at(i);
        a.replace(exp, "\\1");
        langs.append(a);
    }
    langs.sort();
    LOG_INFO() << "available lang files:" << langs;

    return langs;
}


QString Config::languageName(const QString &qmFile)
{
    QTranslator translator;

    QString file = "rbutil_" + qmFile;
    if(!translator.load(file, programPath))
        translator.load(file, ":/lang");

    return translator.translate("Configure", "English",
        "This is the localized language name, i.e. your language.");
}


void Config::updateLanguage()
{
    LOG_INFO() << "update selected language";

    // remove all old translators
    for(int i = 0; i < RbUtilQt::translators.size(); ++i) {
        qApp->removeTranslator(RbUtilQt::translators.at(i));
        // do not delete old translators, this confuses Qt.
    }
    RbUtilQt::translators.clear();
    QList<QListWidgetItem*> a = ui.listLanguages->selectedItems();
    if(a.size() > 0)
        language = lang.value(a.at(0)->text());
    LOG_INFO() << "new language:" << language;

    QTranslator *translator = new QTranslator(qApp);
    QTranslator *qttrans = new QTranslator(qApp);
    QString absolutePath = QCoreApplication::instance()->applicationDirPath();

    if(!translator->load("rbutil_" + language, absolutePath))
        translator->load("rbutil_" + language, ":/lang");
    if(!qttrans->load("qt_" + language,
                QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
        qttrans->load("qt_" + language, ":/lang");

    qApp->installTranslator(translator);
    qApp->installTranslator(qttrans);
    //: This string is used to indicate the writing direction. Translate it
    //: to "RTL" (without quotes) for RTL languages. Anything else will get
    //: treated as LTR language.
    if(QObject::tr("LTR") == "RTL")
        qApp->setLayoutDirection(Qt::RightToLeft);
    else
        qApp->setLayoutDirection(Qt::LeftToRight);

    RbUtilQt::translators.append(translator);
    RbUtilQt::translators.append(qttrans);

    QLocale::setDefault(QLocale(language));

}


void Config::browseCache()
{
    QString old = ui.cachePath->text();
    if(!QFileInfo(old).isDir())
        old = QDir::tempPath();
    QString c = QFileDialog::getExistingDirectory(this, tr("Set Cache Path"), old);
    if(c.isEmpty())
        c = old;
    else if(!QFileInfo(c).isDir())
        c = QDir::tempPath();
    ui.cachePath->setText(QDir::toNativeSeparators(c));
    updateCacheInfo(c);
}


void Config::refreshMountpoint()
{
    // avoid QComboBox to send signals during rebuild to avoid changing to an
    // unwanted item.
    ui.mountPoint->blockSignals(true);
    ui.mountPoint->clear();
    QStringList mps = Utils::mountpoints(Utils::MountpointsSupported);
    for(int i = 0; i < mps.size(); ++i) {
        // add mountpoint as user data so we can change the displayed string
        // later (to include volume label or similar)
        // Skip unwritable mountpoints, they are not useable for us.
        if(QFileInfo(mps.at(i)).isWritable()) {
            QString description = tr("%1 (%2 GiB of %3 GiB free)")
                .arg(Utils::filesystemName(mps.at(i)))
                .arg((double)Utils::filesystemFree(mps.at(i))/(1<<30), 0, 'f', 2)
                .arg((double)Utils::filesystemTotal(mps.at(i))/(1<<30), 0, 'f', 2);
            ui.mountPoint->addItem(QDir::toNativeSeparators(mps.at(i)), description);
        }
        else {
            LOG_WARNING() << "mountpoint not writable, skipping:" << mps.at(i);
        }
    }
    if(!mountpoint.isEmpty()) {
        setMountpoint(mountpoint);
    }
    ui.mountPoint->blockSignals(false);
}


void Config::updateMountpoint(QString m)
{
    if(!m.isEmpty()) {
        mountpoint = QDir::fromNativeSeparators(m);
        LOG_INFO() << "Mountpoint set to" << mountpoint;
    }
}


void Config::updateMountpoint(int idx)
{
    if(idx == -1) {
        return;
    }
    QString mp = ui.mountPoint->itemText(idx);
    if(!mp.isEmpty()) {
        mountpoint = QDir::fromNativeSeparators(mp);
        LOG_INFO() << "Mountpoint set to" << mountpoint;
    }
}


void Config::setMountpoint(QString m)
{
    if(m.isEmpty()) {
        return;
    }
    int index = ui.mountPoint->findText(QDir::toNativeSeparators(m));
    if(index != -1) {
        ui.mountPoint->setCurrentIndex(index);
    }
    else {
        // keep a mountpoint that is not in the list for convenience (to allow
        // easier development)
        ui.mountPoint->addItem(QDir::toNativeSeparators(m));
        ui.mountPoint->setCurrentIndex(ui.mountPoint->findText(m));
    }
    LOG_INFO() << "Mountpoint set to" << mountpoint;
}


void Config::autodetect()
{
    Autodetection detector(this);
    // disable tree during detection as "working" feedback.
    // TODO: replace the tree view with a splash screen during this time.
    ui.treeDevices->setEnabled(false);
    this->setCursor(Qt::WaitCursor);
    QCoreApplication::processEvents();

    detector.detect();
    QList<struct Autodetection::Detected> detected;
    detected = detector.detected();
    this->unsetCursor();
    if(detected.size() > 1) {
        // FIXME: handle multiple found players.
        QString msg;
        msg = tr("Multiple devices have been detected. Please disconnect "
                 "all players but one and try again.");
        msg += "<br/>";
        msg += tr("Detected devices:");
        msg += "<ul>";
        for(int i = 0; i < detected.size(); ++i) {
            QString mp = detected.at(i).mountpoint;
            if(mp.isEmpty()) {
                mp = tr("(unknown)");
            }
            msg += QString("<li>%1</li>").arg(tr("%1 at %2").arg(
                        SystemInfo::platformValue(
                            SystemInfo::PlatformName, detected.at(i).device).toString(),
                        QDir::toNativeSeparators(mp)));
        }
        msg += "</ul>";
        msg += tr("Note: detecting connected devices might be ambiguous. "
                  "You might have less devices connected than listed. "
                  "In this case it might not be possible to detect your "
                  "player unambiguously.");
        QMessageBox::information(this, tr("Device Detection"), msg);
        ui.treeDevices->setEnabled(true);
    }
    else if(detected.size() == 0) {
        QMessageBox::warning(this, tr("Device Detection"),
                tr("Could not detect a device.\n"
                   "Select your device and Mountpoint manually."),
                   QMessageBox::Ok ,QMessageBox::Ok);
        ui.treeDevices->setEnabled(true);
    }
    else if(detected.at(0).status != Autodetection::PlayerOk
            && detected.at(0).status != Autodetection::PlayerAmbiguous) {
        QString msg;
        switch(detected.at(0).status) {
            case Autodetection::PlayerIncompatible:
                msg += tr("Detected an unsupported player:\n%1\n"
                          "Sorry, Rockbox doesn't run on your player.")
                          .arg(SystemInfo::platformValue(
                               SystemInfo::Name, detected.at(0).device).toString());
                break;
            case Autodetection::PlayerMtpMode:
                msg = tr("%1 in MTP mode found!\n"
                         "You need to change your player to MSC mode for installation. ")
                         .arg(SystemInfo::platformValue(
                                    SystemInfo::Name, detected.at(0).device).toString());
                break;
            case Autodetection::PlayerWrongFilesystem:
                if(SystemInfo::platformValue(
                            SystemInfo::BootloaderMethod, detected.at(0).device) == "ipod") {
                    msg = tr("%1 \"MacPod\" found!\n"
                            "Rockbox needs a FAT formatted Ipod (so-called \"WinPod\") "
                            "to run. ").arg(SystemInfo::platformValue(
                                    SystemInfo::Name, detected.at(0).device).toString());
                }
                else {
                    msg = tr("The player contains an incompatible filesystem.\n"
                            "Make sure you selected the correct mountpoint and "
                            "the player is set up to use a filesystem compatible "
                            "with Rockbox.");
                }
                break;
            case Autodetection::PlayerError:
            default:
                msg += tr("An unknown error occured during player detection.");
                break;
        }
        QMessageBox::information(this, tr("Device Detection"), msg);
        ui.treeDevices->setEnabled(true);
    }
    else {
        selectDevice(detected.at(0).device, detected.at(0).mountpoint);
    }

}

void Config::selectDevice(QString device, QString mountpoint)
{
    // collapse all items
    for(int a = 0; a < ui.treeDevices->topLevelItemCount(); a++)
        ui.treeDevices->topLevelItem(a)->setExpanded(false);
    // deselect the selected item(s)
    for(int a = 0; a < ui.treeDevices->selectedItems().size(); a++)
        ui.treeDevices->selectedItems().at(a)->setSelected(false);

    // find the new item
    // enumerate all platform items
    QList<QTreeWidgetItem*> itmList
        = ui.treeDevices->findItems("*",Qt::MatchWildcard);
    for(int i=0; i< itmList.size();i++)
    {
        //enumerate device items
        for(int j=0;j < itmList.at(i)->childCount();j++)
        {
            QString data = itmList.at(i)->child(j)->data(0, Qt::UserRole).toString();
            // unset bold flag
            QFont f = itmList.at(i)->child(j)->font(0);
            f.setBold(false);
            itmList.at(i)->child(j)->setFont(0, f);

            if(device == data) // item found
            {
                f.setBold(true);
                itmList.at(i)->child(j)->setFont(0, f);
                itmList.at(i)->child(j)->setSelected(true); //select the item
                itmList.at(i)->setExpanded(true); //expand the platform item
                //ui.treeDevices->indexOfTopLevelItem(itmList.at(i)->child(j));
                ui.treeDevices->scrollToItem(itmList.at(i)->child(j));
                break;
            }
        }
    }
    this->unsetCursor();

    if(!mountpoint.isEmpty())
    {
        setMountpoint(mountpoint);
    }
    else
    {
        QMessageBox::warning(this, tr("Autodetection"),
                tr("Could not detect a Mountpoint.\n"
                    "Select your Mountpoint manually."),
                QMessageBox::Ok, QMessageBox::Ok);
    }
    ui.treeDevices->setEnabled(true);
}


void Config::cacheClear()
{
    if(QMessageBox::critical(this, tr("Really delete cache?"),
       tr("Do you really want to delete the cache? "
         "Make absolutely sure this setting is correct as it will "
         "remove <b>all</b> files in this folder!").arg(ui.cachePath->text()),
       QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
        return;

    QString cache = ui.cachePath->text() + "/rbutil-cache/";
    if(!QFileInfo(cache).isDir()) {
        QMessageBox::critical(this, tr("Path wrong!"),
            tr("The cache path is invalid. Aborting."), QMessageBox::Ok);
        return;
    }
    QDir dir(cache);
    QStringList fn;
    fn = dir.entryList(QStringList("*"), QDir::Files, QDir::Name);

    for(int i = 0; i < fn.size(); i++) {
        QString f = cache + fn.at(i);
        QFile::remove(f);
    }
    updateCacheInfo(RbSettings::value(RbSettings::CachePath).toString());
}


void Config::configTts()
{
    int index = ui.comboTts->currentIndex();
    TTSBase* tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
    EncTtsCfgGui gui(this,tts,TTSBase::getTTSName(ui.comboTts->itemData(index).toString()));
    gui.exec();
    updateTtsState(ui.comboTts->currentIndex());
    delete tts; /* Config objects are never deleted (in fact, they are
                   leaked..), so we can't rely on QObject, since that would
                   delete the TTSBase instance on application exit */
}

void Config::testTts()
{
#ifdef QT_MULTIMEDIA_LIB
    QString errstr;
    int index = ui.comboTts->currentIndex();
    TTSBase* tts;
    tts = TTSBase::getTTS(this,ui.comboTts->itemData(index).toString());
    if(!tts)
    {
        QMessageBox::critical(this, tr("TTS error"),
            tr("The selected TTS failed to initialize. You can't use this TTS."));
        return;
    }
    ui.testTTS->setEnabled(false);
    if(!tts->configOk())
    {
        QMessageBox::warning(this,tr("TTS configuration invalid"),
                tr("TTS configuration invalid. \n Please configure TTS engine."));
        return;
    }
    if(!tts->start(&errstr))
    {
        QMessageBox::warning(this,tr("Could not start TTS engine."),
                tr("Could not start TTS engine.\n") + errstr
                + tr("\nPlease configure TTS engine."));
        ui.testTTS->setEnabled(true);
        return;
    }

    QString filename;
    QTemporaryFile file(this);
    // keep filename empty if the TTS can do speaking for itself.
    if(!(tts->capabilities() & TTSBase::CanSpeak)) {
        file.open();
        filename = file.fileName();
        file.close();
    }

    if(tts->voice(tr("Rockbox Utility Voice Test"),filename,&errstr) == FatalError)
    {
        tts->stop();
        QMessageBox::warning(this,tr("Could not voice test string."),
                tr("Could not voice test string.\n") + errstr
                + tr("\nPlease configure TTS engine."));
        ui.testTTS->setEnabled(false);
        return;
    }
    tts->stop();
    if(!filename.isEmpty()) {
        QSound::play(filename);
    }
    ui.testTTS->setEnabled(true);
    delete tts; /* Config objects are never deleted (in fact, they are
                   leaked..), so we can't rely on QObject, since that would
                   delete the TTSBase instance on application exit */
#endif
}

void Config::configEnc()
{
    if(ui.treeDevices->selectedItems().size() == 0)
        return;

    QString devname = ui.treeDevices->selectedItems().at(0)->data(0, Qt::UserRole).toString();
    QString encoder = SystemInfo::platformValue(
                    SystemInfo::Encoder, devname).toString();
    ui.encoderName->setText(EncoderBase::getEncoderName(SystemInfo::platformValue(
                    SystemInfo::Encoder, devname).toString()));


    EncoderBase* enc = EncoderBase::getEncoder(this,encoder);

    EncTtsCfgGui gui(this,enc,EncoderBase::getEncoderName(encoder));
    gui.exec();

    updateEncState();
}


void Config::changeEvent(QEvent *e)
{
    if(e->type() == QEvent::LanguageChange) {
        ui.retranslateUi(this);
        updateCacheInfo(ui.cachePath->text());
    } else {
        QWidget::changeEvent(e);
    }
}