summaryrefslogtreecommitdiff
path: root/utils/themeeditor/gui/targetdownloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/themeeditor/gui/targetdownloader.cpp')
-rw-r--r--utils/themeeditor/gui/targetdownloader.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/utils/themeeditor/gui/targetdownloader.cpp b/utils/themeeditor/gui/targetdownloader.cpp
new file mode 100644
index 0000000000..c5b4bf1fe4
--- /dev/null
+++ b/utils/themeeditor/gui/targetdownloader.cpp
@@ -0,0 +1,138 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Robert Bieber
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "targetdownloader.h"
23#include "ui_targetdownloader.h"
24
25#include "quazip.h"
26#include "quazipfile.h"
27#include "quazipfileinfo.h"
28
29#include <QNetworkRequest>
30#include <QNetworkReply>
31#include <QCloseEvent>
32
33#include <QDebug>
34
35TargetDownloader::TargetDownloader(QWidget *parent, QString path) :
36 QDialog(parent),
37 ui(new Ui::TargetDownloader), reply(0), cancelled(false)
38{
39 ui->setupUi(this);
40
41 QObject::connect(ui->cancelButton, SIGNAL(clicked()),
42 this, SLOT(cancel()));
43
44 manager = new QNetworkAccessManager();
45
46 fout.setFileName(path);
47 if(fout.open(QFile::WriteOnly))
48 {
49 ui->label->setText(tr("Downloading targetdb"));
50
51 QNetworkRequest request;
52 request.setUrl(QUrl("http://svn.rockbox.org/viewvc.cgi/trunk/utils/"
53 "themeeditor/resources/targetdb"));
54 request.setRawHeader("User-Agent", "Rockbox Theme Editor");
55
56 reply = manager->get(request);
57
58 QObject::connect(reply, SIGNAL(readyRead()),
59 this, SLOT(dataReceived()));
60 QObject::connect(reply, SIGNAL(finished()),
61 this, SLOT(finished()));
62 QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
63 this, SLOT(progress(qint64,qint64)));
64 }
65 else
66 {
67 ui->label->setText(tr("Error: Couldn't open output file"));
68 }
69
70}
71
72TargetDownloader::~TargetDownloader()
73{
74 delete ui;
75 fout.close();
76 manager->deleteLater();
77
78 if(reply)
79 {
80 reply->abort();
81 reply->deleteLater();
82 }
83}
84
85void TargetDownloader::cancel()
86{
87 cancelled = true;
88
89 if(reply)
90 {
91 reply->abort();
92 reply->deleteLater();
93 reply = 0;
94 }
95
96 fout.close();
97 fout.remove();
98
99 close();
100}
101
102void TargetDownloader::dataReceived()
103{
104 fout.write(reply->readAll());
105}
106
107void TargetDownloader::progress(qint64 bytes, qint64 available)
108{
109 if(available > 0)
110 {
111 ui->progressBar->setMaximum(available);
112 ui->progressBar->setValue(bytes);
113 }
114}
115
116void TargetDownloader::finished()
117{
118 if(cancelled)
119 return;
120
121 fout.close();
122 reply->deleteLater();
123 reply = 0;
124 ui->label->setText(tr("Download complete"));
125 hide();
126 this->deleteLater();
127}
128
129void TargetDownloader::netError(QNetworkReply::NetworkError code)
130{
131 ui->label->setText(tr("Network error: ") + reply->errorString());
132}
133
134void TargetDownloader::closeEvent(QCloseEvent *event)
135{
136 cancel();
137 event->accept();
138}