diff options
Diffstat (limited to 'utils/themeeditor/gui/fontdownloader.cpp')
-rw-r--r-- | utils/themeeditor/gui/fontdownloader.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/utils/themeeditor/gui/fontdownloader.cpp b/utils/themeeditor/gui/fontdownloader.cpp new file mode 100644 index 0000000000..7aa4cd8d57 --- /dev/null +++ b/utils/themeeditor/gui/fontdownloader.cpp | |||
@@ -0,0 +1,158 @@ | |||
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 "fontdownloader.h" | ||
23 | #include "ui_fontdownloader.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 | |||
35 | FontDownloader::FontDownloader(QWidget *parent, QString path) : | ||
36 | QDialog(parent), | ||
37 | ui(new Ui::FontDownloader), dir(path), reply(0) | ||
38 | { | ||
39 | ui->setupUi(this); | ||
40 | |||
41 | manager = new QNetworkAccessManager(); | ||
42 | |||
43 | if(dir.isReadable()) | ||
44 | { | ||
45 | fout.setFileName(dir.absolutePath() + "/fonts.zip"); | ||
46 | if(fout.open(QFile::WriteOnly)) | ||
47 | { | ||
48 | ui->label->setText(tr("Downloading font pack")); | ||
49 | |||
50 | QNetworkRequest request; | ||
51 | request.setUrl(QUrl("http://download.rockbox.org" | ||
52 | "/daily/fonts/rockbox-fonts.zip")); | ||
53 | request.setRawHeader("User-Agent", "Rockbox Theme Editor"); | ||
54 | |||
55 | reply = manager->get(request); | ||
56 | |||
57 | QObject::connect(reply, SIGNAL(readyRead()), | ||
58 | this, SLOT(dataReceived())); | ||
59 | QObject::connect(reply, SIGNAL(finished()), | ||
60 | this, SLOT(finished())); | ||
61 | QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), | ||
62 | this, SLOT(progress(qint64,qint64))); | ||
63 | } | ||
64 | else | ||
65 | { | ||
66 | ui->label->setText(tr("Error: Couldn't open archive file")); | ||
67 | } | ||
68 | } | ||
69 | else | ||
70 | { | ||
71 | ui->label->setText(tr("Error: Fonts directory not readable")); | ||
72 | } | ||
73 | |||
74 | } | ||
75 | |||
76 | FontDownloader::~FontDownloader() | ||
77 | { | ||
78 | delete ui; | ||
79 | fout.close(); | ||
80 | manager->deleteLater(); | ||
81 | |||
82 | if(reply) | ||
83 | { | ||
84 | reply->abort(); | ||
85 | reply->deleteLater(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | void FontDownloader::cancel() | ||
90 | { | ||
91 | if(reply) | ||
92 | { | ||
93 | reply->abort(); | ||
94 | reply->deleteLater(); | ||
95 | reply = 0; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | void FontDownloader::dataReceived() | ||
100 | { | ||
101 | fout.write(reply->readAll()); | ||
102 | } | ||
103 | |||
104 | void FontDownloader::progress(qint64 bytes, qint64 available) | ||
105 | { | ||
106 | if(available > 0) | ||
107 | { | ||
108 | ui->progressBar->setMaximum(available); | ||
109 | ui->progressBar->setValue(bytes); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | void FontDownloader::finished() | ||
114 | { | ||
115 | fout.close(); | ||
116 | reply->deleteLater(); | ||
117 | reply = 0; | ||
118 | ui->label->setText(tr("Download complete")); | ||
119 | |||
120 | /* Extracting the ZIP archive */ | ||
121 | QuaZip archive(fout.fileName()); | ||
122 | QuaZipFile zipFile(&archive); | ||
123 | archive.open(QuaZip::mdUnzip); | ||
124 | |||
125 | bool more; | ||
126 | for(more = archive.goToFirstFile(); more; more = archive.goToNextFile()) | ||
127 | { | ||
128 | if(archive.getCurrentFileName().split("/").last() == "") | ||
129 | continue; | ||
130 | |||
131 | QFile fontFile(dir.absolutePath() + "/" + | ||
132 | archive.getCurrentFileName().split("/").last()); | ||
133 | fontFile.open(QFile::WriteOnly); | ||
134 | |||
135 | zipFile.open(QIODevice::ReadOnly); | ||
136 | fontFile.write(zipFile.readAll()); | ||
137 | zipFile.close(); | ||
138 | |||
139 | fontFile.close(); | ||
140 | } | ||
141 | |||
142 | archive.close(); | ||
143 | QFile::remove(dir.absolutePath() + "/fonts.zip"); | ||
144 | |||
145 | hide(); | ||
146 | this->deleteLater(); | ||
147 | } | ||
148 | |||
149 | void FontDownloader::netError(QNetworkReply::NetworkError code) | ||
150 | { | ||
151 | ui->label->setText(tr("Network error: ") + reply->errorString()); | ||
152 | } | ||
153 | |||
154 | void FontDownloader::closeEvent(QCloseEvent *event) | ||
155 | { | ||
156 | cancel(); | ||
157 | event->accept(); | ||
158 | } | ||