Implement download caching. Set the folder for the cache data in the configuration...
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blob154cc9665b6148c3215124fce6fb454a498abbbc
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"
22 #include "zip/zip.h"
23 #include "zip/unzip.h"
25 ZipInstaller::ZipInstaller(QObject* parent): QObject(parent)
27 m_unzip = true;
28 m_cache = "";
32 void ZipInstaller::install(ProgressloggerInterface *dp)
34 qDebug() << "install(ProgressloggerInterface*)";
35 m_dp = dp;
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();
46 void ZipInstaller::installContinue()
48 qDebug() << "installContinue()";
50 runner++; // this gets called when a install finished, so increase first.
51 qDebug() << "runner is now at" << runner << "size is" << m_urllist.size();
52 if(runner < m_urllist.size()) {
53 qDebug() << "==> runner at" << runner;
54 m_dp->addItem(tr("done."), LOGOK);
55 m_url = m_urllist.at(runner);
56 m_logsection = m_loglist.at(runner);
57 if(runner < m_verlist.size()) m_logver = m_verlist.at(runner);
58 else m_logver = "0";
59 installStart();
61 else {
62 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
63 m_dp->abort();
65 emit done(false);
66 return;
72 void ZipInstaller::installStart()
74 qDebug() << "installStart()";
76 m_dp->addItem(tr("Downloading file %1.%2")
77 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
79 // temporary file needs to be opened to get the filename
80 // make sure to get a fresh one on each run.
81 // making this a parent of the temporary file ensures the file gets deleted
82 // after the class object gets destroyed.
83 downloadFile = new QTemporaryFile(this);
84 downloadFile->open();
85 m_file = downloadFile->fileName();
86 downloadFile->close();
87 // get the real file.
88 getter = new HttpGet(this);
89 getter->setProxy(m_proxy);
90 if(m_cache.exists()) {
91 getter->setCache(m_cache);
92 qDebug() << "installzip: setting cache to" << m_cache;
94 getter->setFile(downloadFile);
95 getter->getFile(QUrl(m_url));
97 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
98 connect(getter, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
99 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
100 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
104 void ZipInstaller::downloadRequestFinished(int id, bool error)
106 qDebug() << "Install::downloadRequestFinished" << id << error;
107 qDebug() << "error:" << getter->errorString();
109 downloadDone(error);
113 void ZipInstaller::downloadDone(bool error)
115 qDebug() << "Install::downloadDone, error:" << error;
116 QStringList zipContents; // needed later
117 // update progress bar
119 int max = m_dp->getProgressMax();
120 if(max == 0) {
121 max = 100;
122 m_dp->setProgressMax(max);
124 m_dp->setProgressValue(max);
125 if(getter->httpResponse() != 200 && !getter->isCached()) {
126 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
127 m_dp->abort();
128 emit done(true);
129 return;
131 if(getter->isCached()) m_dp->addItem(tr("Cached file used."), LOGINFO);
132 if(error) {
133 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
134 m_dp->abort();
135 emit done(true);
136 return;
138 else m_dp->addItem(tr("Download finished."),LOGOK);
140 if(m_unzip) {
141 // unzip downloaded file
142 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
144 m_dp->addItem(tr("Extracting file."),LOGINFO);
146 qDebug() << "file to unzip: " << m_file;
147 UnZip::ErrorCode ec;
148 UnZip uz;
149 ec = uz.openArchive(m_file);
150 if(ec != UnZip::Ok) {
151 m_dp->addItem(tr("Opening archive failed: %1.")
152 .arg(uz.formatError(ec)),LOGERROR);
153 m_dp->abort();
154 emit done(false);
155 return;
158 ec = uz.extractAll(m_mountpoint);
159 if(ec != UnZip::Ok) {
160 m_dp->addItem(tr("Extracting failed: %1.")
161 .arg(uz.formatError(ec)),LOGERROR);
162 m_dp->abort();
163 emit done(false);
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 m_dp->addItem(tr("Installing file."), LOGINFO);
172 qDebug() << "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 m_dp->addItem(tr("Installing file failed."), LOGERROR);
183 m_dp->abort();
184 emit done(false);
185 return;
188 // add file to log
189 zipContents.append( m_target);
192 m_dp->addItem(tr("Creating installation log"),LOGINFO);
193 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
195 installlog.beginGroup(m_logsection);
196 for(int i = 0; i < zipContents.size(); i++)
198 installlog.setValue(zipContents.at(i), m_logver);
200 installlog.endGroup();
201 installlog.sync();
203 emit cont();
206 void ZipInstaller::updateDataReadProgress(int read, int total)
208 m_dp->setProgressMax(total);
209 m_dp->setProgressValue(read);
210 //qDebug() << "progress:" << read << "/" << total;