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