extend ZipInstaller to support installing of multiple files at once (for use by the...
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blob14e1feb55f1bb8188c59af32be97dbe8ef844c35
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: installzip.cpp 13990 2007-07-25 22:26:10Z Dominik Wenger $
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 "installzip.h"
22 #include "zip/zip.h"
23 #include "zip/unzip.h"
25 ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
27 m_unzip = true;
31 void ZipInstaller::install(ProgressloggerInterface *dp)
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 installStart();
44 void ZipInstaller::installContinue()
46 qDebug() << "installContinue()";
48 runner++; // this gets called when a install finished, so increase first.
49 if(runner < m_urllist.size()) {
50 qDebug() << "==> runner at" << runner;
51 m_dp->addItem(tr("done."), LOGOK);
52 m_url = m_urllist.at(runner);
53 m_logsection = m_loglist.at(runner);
54 installStart();
56 else {
57 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
58 m_dp->abort();
60 emit done(false);
61 return;
67 void ZipInstaller::installStart()
69 qDebug() << "installStart()";
71 m_dp->addItem(tr("Downloading file %1.%2")
72 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
74 // temporary file needs to be opened to get the filename
75 // make sure to get a fresh one on each run.
76 // making this a parent of the temporary file ensures the file gets deleted
77 // after the class object gets destroyed.
78 downloadFile = new QTemporaryFile(this);
79 downloadFile->open();
80 m_file = downloadFile->fileName();
81 downloadFile->close();
82 // get the real file.
83 getter = new HttpGet(this);
84 getter->setProxy(m_proxy);
85 getter->setFile(downloadFile);
86 getter->getFile(QUrl(m_url));
88 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
89 connect(getter, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
90 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
91 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
95 void ZipInstaller::downloadRequestFinished(int id, bool error)
97 qDebug() << "Install::downloadRequestFinished" << id << error;
98 qDebug() << "error:" << getter->errorString();
100 downloadDone(error);
104 void ZipInstaller::downloadDone(bool error)
106 qDebug() << "Install::downloadDone, error:" << error;
107 QStringList zipContents; // needed later
108 // update progress bar
110 int max = m_dp->getProgressMax();
111 if(max == 0) {
112 max = 100;
113 m_dp->setProgressMax(max);
115 m_dp->setProgressValue(max);
116 if(getter->httpResponse() != 200) {
117 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
118 m_dp->abort();
119 emit done(true);
120 return;
122 if(error) {
123 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
124 m_dp->abort();
125 emit done(true);
126 return;
128 else m_dp->addItem(tr("Download finished."),LOGOK);
130 if(m_unzip) {
131 // unzip downloaded file
132 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
134 m_dp->addItem(tr("Extracting file."),LOGINFO);
136 qDebug() << "file to unzip: " << m_file;
137 UnZip::ErrorCode ec;
138 UnZip uz;
139 ec = uz.openArchive(m_file);
140 if(ec != UnZip::Ok) {
141 m_dp->addItem(tr("Opening archive failed: %1.")
142 .arg(uz.formatError(ec)),LOGERROR);
143 m_dp->abort();
144 emit done(false);
145 return;
148 ec = uz.extractAll(m_mountpoint);
149 if(ec != UnZip::Ok) {
150 m_dp->addItem(tr("Extracting failed: %1.")
151 .arg(uz.formatError(ec)),LOGERROR);
152 m_dp->abort();
153 emit done(false);
154 return;
156 // prepare file list for log
157 zipContents = uz.fileList();
159 else {
160 // only copy the downloaded file to the output location / name
161 m_dp->addItem(tr("Installing file."), LOGINFO);
162 qDebug() << "saving downloaded file (no extraction)";
164 downloadFile->open(); // copy fails if file is not opened (filename issue?)
165 // make sure the required path is existing
166 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
167 QDir p;
168 p.mkpath(path);
169 // QFile::copy() doesn't overwrite files, so remove old one first
170 QFile(m_mountpoint + m_target).remove();
171 if(!downloadFile->copy(m_mountpoint + m_target)) {
172 m_dp->addItem(tr("Installing file failed."), LOGERROR);
173 m_dp->abort();
174 emit done(false);
175 return;
178 // add file to log
179 zipContents.append( m_target);
182 m_dp->addItem(tr("Creating installation log"),LOGINFO);
183 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
185 installlog.beginGroup(m_logsection);
186 for(int i = 0; i < zipContents.size(); i++)
188 installlog.setValue(zipContents.at(i),installlog.value(zipContents.at(i),0).toInt()+1);
190 installlog.endGroup();
192 emit cont();
195 void ZipInstaller::updateDataReadProgress(int read, int total)
197 m_dp->setProgressMax(total);
198 m_dp->setProgressValue(read);
199 qDebug() << "progress:" << read << "/" << total;