Make sure to include audiohw.h in settings.h or the definition of struct user_setting...
[kugel-rb.git] / rbutil / rbutilqt / base / zipinstaller.cpp
blob1822d3c9ed8c1e36816c5c351da5e623e6000cd4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include "zipinstaller.h"
22 #include "rbunzip.h"
23 #include "utils.h"
25 ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
27 m_unzip = true;
28 m_usecache = false;
32 void ZipInstaller::install()
34 qDebug() << "[ZipInstall] initializing installation";
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();
45 void ZipInstaller::abort()
47 qDebug() << "[ZipInstall] Aborted";
48 emit internalAborted();
52 void ZipInstaller::installContinue()
54 qDebug() << "[ZipInstall] continuing installation";
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();
66 else {
67 emit logItem(tr("Installation finished successfully."), LOGOK);
69 emit done(false);
70 return;
76 void ZipInstaller::installStart()
78 qDebug() << "[ZipInstall] starting installation";
80 emit logItem(tr("Downloading file %1.%2").arg(QFileInfo(m_url).baseName(),
81 QFileInfo(m_url).completeSuffix()),LOGINFO);
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);
96 getter->setFile(downloadFile);
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()));
102 getter->getFile(QUrl(m_url));
106 void ZipInstaller::downloadDone(bool error)
108 qDebug() << "[ZipInstall] download done, error:" << error;
109 QStringList zipContents; // needed later
110 // update progress bar
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;
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;
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;
132 emit logItem(tr("Extracting file."), LOGINFO);
133 QCoreApplication::processEvents();
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;
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(Utils::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;
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;
166 // prepare file list for log
167 zipContents = uz.fileList();
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)";
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;
187 // add file to log
188 zipContents.append( m_target);
191 emit logItem(tr("Creating installation log"),LOGINFO);
192 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
194 installlog.beginGroup(m_logsection);
195 for(int i = 0; i < zipContents.size(); i++)
197 installlog.setValue(zipContents.at(i), m_logver);
199 installlog.endGroup();
200 installlog.sync();
202 emit cont();