summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/zipinstaller.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/zipinstaller.cpp')
-rw-r--r--rbutil/rbutilqt/zipinstaller.cpp214
1 files changed, 214 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/zipinstaller.cpp b/rbutil/rbutilqt/zipinstaller.cpp
new file mode 100644
index 0000000000..9723e10e69
--- /dev/null
+++ b/rbutil/rbutilqt/zipinstaller.cpp
@@ -0,0 +1,214 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "zipinstaller.h"
21#include "rbunzip.h"
22#include "utils.h"
23
24ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
25{
26 m_unzip = true;
27 m_usecache = false;
28}
29
30
31void ZipInstaller::install(ProgressloggerInterface *dp)
32{
33 qDebug() << "install(ProgressloggerInterface*)";
34 m_dp = dp;
35 runner = 0;
36 connect(this, SIGNAL(cont()), this, SLOT(installContinue()));
37 m_url = m_urllist.at(runner);
38 m_logsection = m_loglist.at(runner);
39 m_logver = m_verlist.at(runner);
40 installStart();
41
42}
43
44
45void ZipInstaller::installContinue()
46{
47 qDebug() << "installContinue()";
48
49 runner++; // this gets called when a install finished, so increase first.
50 qDebug() << "runner is now at" << runner << "size is" << m_urllist.size();
51 if(runner < m_urllist.size()) {
52 qDebug() << "==> runner at" << runner;
53 m_dp->addItem(tr("done."), LOGOK);
54 m_url = m_urllist.at(runner);
55 m_logsection = m_loglist.at(runner);
56 if(runner < m_verlist.size()) m_logver = m_verlist.at(runner);
57 else m_logver = "0";
58 installStart();
59 }
60 else {
61 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
62 m_dp->abort();
63
64 emit done(false);
65 return;
66 }
67
68}
69
70
71void ZipInstaller::installStart()
72{
73 qDebug() << "installStart()";
74
75 m_dp->addItem(tr("Downloading file %1.%2")
76 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
77
78 // temporary file needs to be opened to get the filename
79 // make sure to get a fresh one on each run.
80 // making this a parent of the temporary file ensures the file gets deleted
81 // after the class object gets destroyed.
82 downloadFile = new QTemporaryFile(this);
83 downloadFile->open();
84 m_file = downloadFile->fileName();
85 downloadFile->close();
86 // get the real file.
87 getter = new HttpGet(this);
88 if(m_usecache) {
89 getter->setCache(true);
90 }
91 getter->setFile(downloadFile);
92
93 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
94 connect(getter, SIGNAL(dataReadProgress(int, int)), m_dp, SLOT(setProgress(int, int)));
95 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
96
97 getter->getFile(QUrl(m_url));
98}
99
100
101void ZipInstaller::downloadDone(bool error)
102{
103 qDebug() << "Install::downloadDone, error:" << error;
104 QStringList zipContents; // needed later
105 // update progress bar
106
107 int max = m_dp->getProgressMax();
108 if(max == 0) {
109 max = 100;
110 m_dp->setProgressMax(max);
111 }
112 m_dp->setProgressValue(max);
113 if(getter->httpResponse() != 200 && !getter->isCached()) {
114 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
115 m_dp->abort();
116 emit done(true);
117 return;
118 }
119 if(getter->isCached()) m_dp->addItem(tr("Cached file used."), LOGINFO);
120 if(error) {
121 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
122 m_dp->abort();
123 emit done(true);
124 return;
125 }
126 else m_dp->addItem(tr("Download finished."),LOGOK);
127 QCoreApplication::processEvents();
128 if(m_unzip) {
129 // unzip downloaded file
130 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
131
132 m_dp->addItem(tr("Extracting file."),LOGINFO);
133 QCoreApplication::processEvents();
134
135 qDebug() << "file to unzip: " << m_file;
136 UnZip::ErrorCode ec;
137 RbUnZip uz;
138 connect(&uz, SIGNAL(unzipProgress(int, int)), m_dp, SLOT(setProgress(int, int)));
139 connect(m_dp, SIGNAL(aborted()), &uz, SLOT(abortUnzip()));
140 ec = uz.openArchive(m_file);
141 if(ec != UnZip::Ok) {
142 m_dp->addItem(tr("Opening archive failed: %1.")
143 .arg(uz.formatError(ec)),LOGERROR);
144 m_dp->setProgressMax(1);
145 m_dp->setProgressValue(1);
146 m_dp->abort();
147 emit done(true);
148 return;
149 }
150
151 // check for free space. Make sure after installation will still be
152 // some room for operating (also includes calculation mistakes due to
153 // cluster sizes on the player).
154 if(filesystemFree(m_mountpoint) < (uz.totalSize() + 1000000)) {
155 m_dp->addItem(tr("Not enough disk space! Aborting."), LOGERROR);
156 m_dp->abort();
157 m_dp->setProgressMax(1);
158 m_dp->setProgressValue(1);
159 emit done(true);
160 return;
161 }
162 ec = uz.extractArchive(m_mountpoint);
163 // TODO: better handling of aborted unzip operation.
164 if(ec != UnZip::Ok) {
165 m_dp->addItem(tr("Extracting failed: %1.")
166 .arg(uz.formatError(ec)),LOGERROR);
167 m_dp->abort();
168 m_dp->setProgressMax(1);
169 m_dp->setProgressValue(1);
170
171 emit done(true);
172 return;
173 }
174 // prepare file list for log
175 zipContents = uz.fileList();
176 }
177 else {
178 // only copy the downloaded file to the output location / name
179 m_dp->addItem(tr("Installing file."), LOGINFO);
180 qDebug() << "saving downloaded file (no extraction)";
181
182 downloadFile->open(); // copy fails if file is not opened (filename issue?)
183 // make sure the required path is existing
184 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
185 QDir p;
186 p.mkpath(path);
187 // QFile::copy() doesn't overwrite files, so remove old one first
188 QFile(m_mountpoint + m_target).remove();
189 if(!downloadFile->copy(m_mountpoint + m_target)) {
190 m_dp->addItem(tr("Installing file failed."), LOGERROR);
191 m_dp->abort();
192 emit done(true);
193 return;
194 }
195
196 // add file to log
197 zipContents.append( m_target);
198 }
199
200 m_dp->addItem(tr("Creating installation log"),LOGINFO);
201 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
202
203 installlog.beginGroup(m_logsection);
204 for(int i = 0; i < zipContents.size(); i++)
205 {
206 installlog.setValue(zipContents.at(i), m_logver);
207 }
208 installlog.endGroup();
209 installlog.sync();
210
211 emit cont();
212}
213
214