create an UnZip derived class for zip file extraction to allow showing progress while...
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blob6514c6d13dc0ed0cc7b6af18d8211351730be900
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 "installzip.h"
21 #include "rbunzip.h"
24 ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
26 m_unzip = true;
27 m_usecache = false;
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 m_logver = m_verlist.at(runner);
40 installStart();
45 void ZipInstaller::installContinue()
47 qDebug() << "installContinue()";
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();
60 else {
61 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
62 m_dp->abort();
64 emit done(false);
65 return;
71 void ZipInstaller::installStart()
73 qDebug() << "installStart()";
75 m_dp->addItem(tr("Downloading file %1.%2")
76 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
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);
91 getter->setFile(downloadFile);
93 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
94 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
95 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
97 getter->getFile(QUrl(m_url));
101 void ZipInstaller::downloadDone(bool error)
103 qDebug() << "Install::downloadDone, error:" << error;
104 QStringList zipContents; // needed later
105 // update progress bar
107 int max = m_dp->getProgressMax();
108 if(max == 0) {
109 max = 100;
110 m_dp->setProgressMax(max);
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;
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;
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;
132 m_dp->addItem(tr("Extracting file."),LOGINFO);
133 QCoreApplication::processEvents();
135 qDebug() << "file to unzip: " << m_file;
136 UnZip::ErrorCode ec;
137 RbUnZip uz;
138 connect(&uz, SIGNAL(unzipProgress(int, int)), this, SLOT(updateDataReadProgress(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;
151 ec = uz.extractArchive(m_mountpoint);
152 // TODO: better handling of aborted unzip operation.
153 if(ec != UnZip::Ok) {
154 m_dp->addItem(tr("Extracting failed: %1.")
155 .arg(uz.formatError(ec)),LOGERROR);
156 m_dp->abort();
157 m_dp->setProgressMax(1);
158 m_dp->setProgressValue(1);
160 emit done(true);
161 return;
163 // prepare file list for log
164 zipContents = uz.fileList();
166 else {
167 // only copy the downloaded file to the output location / name
168 m_dp->addItem(tr("Installing file."), LOGINFO);
169 qDebug() << "saving downloaded file (no extraction)";
171 downloadFile->open(); // copy fails if file is not opened (filename issue?)
172 // make sure the required path is existing
173 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
174 QDir p;
175 p.mkpath(path);
176 // QFile::copy() doesn't overwrite files, so remove old one first
177 QFile(m_mountpoint + m_target).remove();
178 if(!downloadFile->copy(m_mountpoint + m_target)) {
179 m_dp->addItem(tr("Installing file failed."), LOGERROR);
180 m_dp->abort();
181 emit done(true);
182 return;
185 // add file to log
186 zipContents.append( m_target);
189 m_dp->addItem(tr("Creating installation log"),LOGINFO);
190 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
192 installlog.beginGroup(m_logsection);
193 for(int i = 0; i < zipContents.size(); i++)
195 installlog.setValue(zipContents.at(i), m_logver);
197 installlog.endGroup();
198 installlog.sync();
200 emit cont();
203 void ZipInstaller::updateDataReadProgress(int read, int total)
205 m_dp->setProgressMax(total);
206 m_dp->setProgressValue(read);
207 //qDebug() << "progress:" << read << "/" << total;