fix a possible segfault upon invalid selection.
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blob4e2ab518ba03d38cacddccb317c2798047be93a5
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 m_dp = dp;
35 m_dp->addItem(tr("Downloading file %1.%2")
36 .arg(QFileInfo(m_url).baseName(), QFileInfo(m_url).completeSuffix()),LOGINFO);
38 // temporary file needs to be opened to get the filename
39 downloadFile.open();
40 m_file = downloadFile.fileName();
41 downloadFile.close();
42 // get the real file.
43 getter = new HttpGet(this);
44 getter->setProxy(m_proxy);
45 getter->setFile(&downloadFile);
46 getter->getFile(QUrl(m_url));
48 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
49 connect(getter, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
50 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
51 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
54 void ZipInstaller::downloadRequestFinished(int id, bool error)
56 qDebug() << "Install::downloadRequestFinished" << id << error;
57 qDebug() << "error:" << getter->errorString();
59 downloadDone(error);
62 void ZipInstaller::downloadDone(bool error)
64 qDebug() << "Install::downloadDone, error:" << error;
65 QStringList zipContents; // needed later
66 // update progress bar
68 int max = m_dp->getProgressMax();
69 if(max == 0) {
70 max = 100;
71 m_dp->setProgressMax(max);
73 m_dp->setProgressValue(max);
74 if(getter->httpResponse() != 200) {
75 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
76 m_dp->abort();
77 emit done(true);
78 return;
80 if(error) {
81 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
82 m_dp->abort();
83 emit done(true);
84 return;
86 else m_dp->addItem(tr("Download finished."),LOGOK);
88 if(m_unzip) {
89 // unzip downloaded file
90 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
92 m_dp->addItem(tr("Extracting file."),LOGINFO);
94 qDebug() << "file to unzip: " << m_file;
95 UnZip::ErrorCode ec;
96 UnZip uz;
97 ec = uz.openArchive(m_file);
98 if(ec != UnZip::Ok) {
99 m_dp->addItem(tr("Opening archive failed: %1.")
100 .arg(uz.formatError(ec)),LOGERROR);
101 m_dp->abort();
102 downloadFile.remove();
103 emit done(false);
104 return;
107 ec = uz.extractAll(m_mountpoint);
108 if(ec != UnZip::Ok) {
109 m_dp->addItem(tr("Extracting failed: %1.")
110 .arg(uz.formatError(ec)),LOGERROR);
111 m_dp->abort();
112 downloadFile.remove();
113 emit done(false);
114 return;
116 // prepare file list for log
117 zipContents = uz.fileList();
119 else {
120 // only copy the downloaded file to the output location / name
121 m_dp->addItem(tr("Installing file."), LOGINFO);
122 qDebug() << "saving downloaded file (no extraction)";
124 downloadFile.open(); // copy fails if file is not opened (filename issue?)
125 // make sure the required path is existing
126 QString path = QFileInfo(m_mountpoint + m_target).absolutePath();
127 QDir p;
128 p.mkpath(path);
129 // QFile::copy() doesn't overwrite files, so remove old one first
130 QFile(m_mountpoint + m_target).remove();
131 if(!downloadFile.copy(m_mountpoint + m_target)) {
132 m_dp->addItem(tr("Installing file failed."), LOGERROR);
133 m_dp->abort();
134 downloadFile.remove();
135 emit done(false);
136 return;
139 // add file to log
140 zipContents.append(m_mountpoint + m_target);
143 m_dp->addItem(tr("Creating installation log"),LOGINFO);
144 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
146 installlog.beginGroup(m_logsection);
147 for(int i = 0; i < zipContents.size(); i++)
149 installlog.setValue(zipContents.at(i),installlog.value(zipContents.at(i),0).toInt()+1);
151 installlog.endGroup();
153 // remove temporary file
154 downloadFile.remove();
156 m_dp->addItem(tr("Installation finished successfully."),LOGOK);
157 m_dp->abort();
158 emit done(false);
161 void ZipInstaller::updateDataReadProgress(int read, int total)
163 m_dp->setProgressMax(total);
164 m_dp->setProgressValue(read);
165 qDebug() << "progress:" << read << "/" << total;