Voicefile: remove < and > from voice strings before speaking.
[maemo-rb.git] / rbutil / rbutilqt / base / zipinstaller.cpp
blob3f3179f5f22171054eb74e8f518360fd4ab75e09
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <QtCore>
20 #include "zipinstaller.h"
21 #include "utils.h"
22 #include "ziputil.h"
24 ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
26 m_unzip = true;
27 m_usecache = false;
31 void ZipInstaller::install()
33 qDebug() << "[ZipInstall] initializing installation";
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();
44 void ZipInstaller::abort()
46 qDebug() << "[ZipInstall] Aborted";
47 emit internalAborted();
51 void ZipInstaller::installContinue()
53 qDebug() << "[ZipInstall] continuing installation";
55 runner++; // this gets called when a install finished, so increase first.
56 qDebug() << "[ZipInstall] runner done:" << runner << "/" << m_urllist.size();
57 if(runner < m_urllist.size()) {
58 emit logItem(tr("done."), LOGOK);
59 m_url = m_urllist.at(runner);
60 m_logsection = m_loglist.at(runner);
61 if(runner < m_verlist.size()) m_logver = m_verlist.at(runner);
62 else m_logver = "0";
63 installStart();
65 else {
66 emit logItem(tr("Installation finished successfully."), LOGOK);
68 emit done(false);
69 return;
75 void ZipInstaller::installStart()
77 qDebug() << "[ZipInstall] starting installation";
79 emit logItem(tr("Downloading file %1.%2").arg(QFileInfo(m_url).baseName(),
80 QFileInfo(m_url).completeSuffix()),LOGINFO);
82 // temporary file needs to be opened to get the filename
83 // make sure to get a fresh one on each run.
84 // making this a parent of the temporary file ensures the file gets deleted
85 // after the class object gets destroyed.
86 downloadFile = new QTemporaryFile(this);
87 downloadFile->open();
88 m_file = downloadFile->fileName();
89 downloadFile->close();
90 // get the real file.
91 getter = new HttpGet(this);
92 if(m_usecache) {
93 getter->setCache(true);
95 getter->setFile(downloadFile);
97 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
98 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(logProgress(int, int)));
99 connect(this, SIGNAL(internalAborted()), getter, SLOT(abort()));
101 getter->getFile(QUrl(m_url));
105 void ZipInstaller::downloadDone(bool error)
107 qDebug() << "[ZipInstall] download done, error:" << error;
108 QStringList zipContents; // needed later
109 // update progress bar
111 emit logProgress(1, 1);
112 if(getter->httpResponse() != 200 && !getter->isCached()) {
113 emit logItem(tr("Download error: received HTTP error %1.")
114 .arg(getter->httpResponse()),LOGERROR);
115 emit done(true);
116 return;
118 if(getter->isCached())
119 emit logItem(tr("Cached file used."), LOGINFO);
120 if(error) {
121 emit logItem(tr("Download error: %1").arg(getter->errorString()), LOGERROR);
122 emit done(true);
123 return;
125 else emit logItem(tr("Download finished."),LOGOK);
126 QCoreApplication::processEvents();
127 if(m_unzip) {
128 // unzip downloaded file
129 qDebug() << "[ZipInstall] about to unzip " << m_file << "to" << m_mountpoint;
131 emit logItem(tr("Extracting file."), LOGINFO);
132 QCoreApplication::processEvents();
134 ZipUtil zip(this);
135 connect(&zip, SIGNAL(logProgress(int, int)), this, SIGNAL(logProgress(int, int)));
136 connect(&zip, SIGNAL(logItem(QString, int)), this, SIGNAL(logItem(QString, int)));
137 zip.open(m_file, QuaZip::mdUnzip);
138 // check for free space. Make sure after installation will still be
139 // some room for operating (also includes calculation mistakes due to
140 // cluster sizes on the player).
141 if((qint64)Utils::filesystemFree(m_mountpoint)
142 < (zip.totalUncompressedSize(Utils::filesystemClusterSize(m_mountpoint))
143 + 1000000)) {
144 emit logItem(tr("Not enough disk space! Aborting."), LOGERROR);
145 emit logProgress(1, 1);
146 emit done(true);
147 return;
149 zipContents = zip.files();
150 if(!zip.extractArchive(m_mountpoint)) {
151 emit logItem(tr("Extraction failed!"), LOGERROR);
152 emit logProgress(1, 1);
153 emit done(true);
154 return;
156 zip.close();
158 else {
159 // only copy the downloaded file to the output location / name
160 emit logItem(tr("Installing file."), LOGINFO);
161 qDebug() << "[ZipInstall] saving downloaded file (no extraction)";
163 downloadFile->open(); // copy fails if file is not opened (filename issue?)
164 // make sure the required path is existing
165 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
166 QDir p;
167 p.mkpath(path);
168 // QFile::copy() doesn't overwrite files, so remove old one first
169 QFile(m_mountpoint + m_target).remove();
170 if(!downloadFile->copy(m_mountpoint + m_target)) {
171 emit logItem(tr("Installing file failed."), LOGERROR);
172 emit done(true);
173 return;
176 // add file to log
177 zipContents.append(m_target);
180 emit logItem(tr("Creating installation log"),LOGINFO);
181 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
183 installlog.beginGroup(m_logsection);
184 for(int i = 0; i < zipContents.size(); i++)
186 installlog.setValue(zipContents.at(i), m_logver);
188 installlog.endGroup();
189 installlog.sync();
191 emit cont();