set missing keywords property.
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blobfe16333a067019774877af93cb505f79c80d77d8
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;
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 qDebug() << "runner is now at" << runner << "size is" << m_urllist.size();
50 if(runner < m_urllist.size()) {
51 qDebug() << "==> runner at" << runner;
52 m_dp->addItem(tr("done."), LOGOK);
53 m_url = m_urllist.at(runner);
54 m_logsection = m_loglist.at(runner);
55 installStart();
57 else {
58 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
59 m_dp->abort();
61 emit done(false);
62 return;
68 void ZipInstaller::installStart()
70 qDebug() << "installStart()";
72 m_dp->addItem(tr("Downloading file %1.%2")
73 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
75 // temporary file needs to be opened to get the filename
76 // make sure to get a fresh one on each run.
77 // making this a parent of the temporary file ensures the file gets deleted
78 // after the class object gets destroyed.
79 downloadFile = new QTemporaryFile(this);
80 downloadFile->open();
81 m_file = downloadFile->fileName();
82 downloadFile->close();
83 // get the real file.
84 getter = new HttpGet(this);
85 getter->setProxy(m_proxy);
86 getter->setFile(downloadFile);
87 getter->getFile(QUrl(m_url));
89 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
90 connect(getter, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
91 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
92 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
96 void ZipInstaller::downloadRequestFinished(int id, bool error)
98 qDebug() << "Install::downloadRequestFinished" << id << error;
99 qDebug() << "error:" << getter->errorString();
101 downloadDone(error);
105 void ZipInstaller::downloadDone(bool error)
107 qDebug() << "Install::downloadDone, error:" << error;
108 QStringList zipContents; // needed later
109 // update progress bar
111 int max = m_dp->getProgressMax();
112 if(max == 0) {
113 max = 100;
114 m_dp->setProgressMax(max);
116 m_dp->setProgressValue(max);
117 if(getter->httpResponse() != 200) {
118 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
119 m_dp->abort();
120 emit done(true);
121 return;
123 if(error) {
124 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
125 m_dp->abort();
126 emit done(true);
127 return;
129 else m_dp->addItem(tr("Download finished."),LOGOK);
131 if(m_unzip) {
132 // unzip downloaded file
133 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
135 m_dp->addItem(tr("Extracting file."),LOGINFO);
137 qDebug() << "file to unzip: " << m_file;
138 UnZip::ErrorCode ec;
139 UnZip uz;
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->abort();
145 emit done(false);
146 return;
149 ec = uz.extractAll(m_mountpoint);
150 if(ec != UnZip::Ok) {
151 m_dp->addItem(tr("Extracting failed: %1.")
152 .arg(uz.formatError(ec)),LOGERROR);
153 m_dp->abort();
154 emit done(false);
155 return;
157 // prepare file list for log
158 zipContents = uz.fileList();
160 else {
161 // only copy the downloaded file to the output location / name
162 m_dp->addItem(tr("Installing file."), LOGINFO);
163 qDebug() << "saving downloaded file (no extraction)";
165 downloadFile->open(); // copy fails if file is not opened (filename issue?)
166 // make sure the required path is existing
167 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
168 QDir p;
169 p.mkpath(path);
170 // QFile::copy() doesn't overwrite files, so remove old one first
171 QFile(m_mountpoint + m_target).remove();
172 if(!downloadFile->copy(m_mountpoint + m_target)) {
173 m_dp->addItem(tr("Installing file failed."), LOGERROR);
174 m_dp->abort();
175 emit done(false);
176 return;
179 // add file to log
180 zipContents.append( m_target);
183 m_dp->addItem(tr("Creating installation log"),LOGINFO);
184 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
186 installlog.beginGroup(m_logsection);
187 for(int i = 0; i < zipContents.size(); i++)
189 installlog.setValue(zipContents.at(i),installlog.value(zipContents.at(i),0).toInt()+1);
191 installlog.endGroup();
193 emit cont();
196 void ZipInstaller::updateDataReadProgress(int read, int total)
198 m_dp->setProgressMax(total);
199 m_dp->setProgressValue(read);
200 //qDebug() << "progress:" << read << "/" << total;