~raimar/navitconfigurator/NavitConfigurator

« back to all changes in this revision

Viewing changes to src/navitconf/gui/DialogDownload.cpp

  • Committer: Raimar
  • Date: 2012-07-18 20:58:12 UTC
  • Revision ID: git-v1:da49d479d527774f27cdc54f6985f4cacecc43f3
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "DialogDownload.h"
 
2
 
 
3
#include <QtCore/QDebug>
 
4
 
 
5
#include <QtGui/QMessageBox>
 
6
 
 
7
#include <navitconf/file/Settings.h>
 
8
#include <navitconf/file/Unzip.h>
 
9
 
 
10
QString DialogDownload::download(QWidget* parent) {
 
11
        DialogDownload dialog(parent);
 
12
        dialog.exec();
 
13
        return dialog.getFilename();
 
14
}
 
15
 
 
16
DialogDownload::DialogDownload(QWidget* parent)
 
17
:
 
18
                QDialog(parent),
 
19
                netDownload(this)
 
20
{
 
21
        ui.setupUi(this);
 
22
        bool hasLastDownload = false;
 
23
        try {
 
24
                hasLastDownload = QFile::exists(Settings::getInstance().getNavitInstallationFile());
 
25
        } catch(QString& e) {
 
26
                QMessageBox::critical(parent, tr("Error"), e);
 
27
        }
 
28
        ui.rbUseLastDownload->setEnabled(hasLastDownload);
 
29
        ui.rbUseLastDownload->setChecked(hasLastDownload);
 
30
}
 
31
 
 
32
DialogDownload::~DialogDownload() {
 
33
}
 
34
 
 
35
void DialogDownload::accept() {
 
36
        ui.buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
 
37
        QString zipFile;
 
38
        try {
 
39
                zipFile = Settings::getInstance().getNavitInstallationFile();
 
40
        } catch(QString& e) {
 
41
                QMessageBox::critical(this, tr("Error"), e);
 
42
        }
 
43
        if (!zipFile.isEmpty()) {
 
44
                if (ui.rbDownloadCurrentVersion->isChecked()) {
 
45
                        QString url(ui.tbxDownloadURL->text());
 
46
                        ui.progressBar->setMaximum(ui.progressBar->width());
 
47
                        netDownload.download(ui.tbxDownloadURL->text(), zipFile); // asyn download
 
48
                        filename.clear();
 
49
                } else {
 
50
                        extractNavitXml(zipFile);
 
51
                }
 
52
        }
 
53
}
 
54
 
 
55
void DialogDownload::downloadProgress(float percent) {
 
56
        ui.progressBar->setValue(ui.progressBar->maximum() * percent);
 
57
}
 
58
 
 
59
void DialogDownload::downloadFinished(const QString& filename) {
 
60
        extractNavitXml(filename);
 
61
}
 
62
 
 
63
void DialogDownload::downloadError(const QString& msg) {
 
64
        QMessageBox::critical(this, tr("Error"), msg);
 
65
        ui.buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
 
66
}
 
67
 
 
68
QString DialogDownload::getFilename() {
 
69
        return filename;
 
70
}
 
71
 
 
72
void DialogDownload::extractNavitXml(const QString& zipFile) {
 
73
        char resolution = 'm';
 
74
        if (ui.rbHighResolution->isChecked()) {
 
75
                resolution = 'h';
 
76
        } else if (ui.rbLowResolution->isChecked()) {
 
77
                resolution = 'l';
 
78
        }
 
79
        try {
 
80
                QString dirSettings(Settings::getInstance().getDir().absolutePath());
 
81
                // extract all files from archive
 
82
                Unzip unzip(zipFile, dirSettings);
 
83
                for (int i = 0; i < unzip.getNumberOfFiles(); i++) {
 
84
                        if (!unzip.getNextFilename().startsWith("lib/")) { // ignore files in the lib folder
 
85
                                unzip.extractNext();
 
86
                        }
 
87
                }
 
88
                filename = dirSettings + "/res/raw/navit" + resolution + "dpi.xml";
 
89
                setVisible(false);
 
90
        } catch(QString& e) {
 
91
                QMessageBox::critical(this, tr("Error"), e);
 
92
        }
 
93
}