A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
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
35FontDownloader::FontDownloader(QWidget *parent, QString path) :
36 QDialog(parent),
37 ui(new Ui::FontDownloader), dir(path), 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 if(!dir.exists())
47 dir.mkpath(path);
48
49 if(dir.isReadable())
50 {
51 fout.setFileName(dir.absolutePath() + "/fonts.zip");
52 if(fout.open(QFile::WriteOnly))
53 {
54 ui->label->setText(tr("Downloading font pack"));
55
56 QNetworkRequest request;
57 request.setUrl(QUrl("http://download.rockbox.org"
58 "/daily/fonts/rockbox-fonts.zip"));
59 request.setRawHeader("User-Agent", "Rockbox Theme Editor");
60
61 reply = manager->get(request);
62
63 QObject::connect(reply, SIGNAL(readyRead()),
64 this, SLOT(dataReceived()));
65 QObject::connect(reply, SIGNAL(finished()),
66 this, SLOT(finished()));
67 QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
68 this, SLOT(progress(qint64,qint64)));
69 }
70 else
71 {
72 ui->label->setText(tr("Error: Couldn't open archive file"));
73 }
74 }
75 else
76 {
77 ui->label->setText(tr("Error: Fonts directory not readable"));
78 }
79
80}
81
82FontDownloader::~FontDownloader()
83{
84 delete ui;
85 fout.close();
86 manager->deleteLater();
87
88 if(reply)
89 {
90 reply->abort();
91 reply->deleteLater();
92 }
93}
94
95void FontDownloader::cancel()
96{
97 cancelled = true;
98
99 if(reply)
100 {
101 reply->abort();
102 reply->deleteLater();
103 reply = 0;
104 }
105 fout.close();
106 fout.remove();
107
108 close();
109}
110
111void FontDownloader::dataReceived()
112{
113 fout.write(reply->readAll());
114}
115
116void FontDownloader::progress(qint64 bytes, qint64 available)
117{
118 if(available > 0)
119 {
120 ui->progressBar->setMaximum(available);
121 ui->progressBar->setValue(bytes);
122 }
123}
124
125void FontDownloader::finished()
126{
127 if(cancelled)
128 return;
129
130 fout.close();
131
132 reply->deleteLater();
133 reply = 0;
134 ui->label->setText(tr("Download complete"));
135
136 /* Extracting the ZIP archive */
137 QuaZip archive(fout.fileName());
138 QuaZipFile zipFile(&archive);
139 archive.open(QuaZip::mdUnzip);
140
141 bool more;
142 for(more = archive.goToFirstFile(); more; more = archive.goToNextFile())
143 {
144 if(archive.getCurrentFileName().split("/").last() == "")
145 continue;
146
147 QFile fontFile(dir.absolutePath() + "/" +
148 archive.getCurrentFileName().split("/").last());
149 fontFile.open(QFile::WriteOnly);
150
151 zipFile.open(QIODevice::ReadOnly);
152 fontFile.write(zipFile.readAll());
153 zipFile.close();
154
155 fontFile.close();
156 }
157
158 archive.close();
159 QFile::remove(dir.absolutePath() + "/fonts.zip");
160
161 hide();
162 this->deleteLater();
163}
164
165void FontDownloader::netError(QNetworkReply::NetworkError code)
166{
167 ui->label->setText(tr("Network error: ") + reply->errorString());
168}
169
170void FontDownloader::closeEvent(QCloseEvent *event)
171{
172 cancel();
173 event->accept();
174}