Move device and mountpoint selection to configuration to eliminate the need of asking...
[Rockbox.git] / rbutil / rbutilqt / installzip.cpp
blob4f99d7bc318a7543e38d07273d50c61c348f7296
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)
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;
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 // unzip downloaded file
89 qDebug() << "about to unzip the downloaded file" << m_file << "to" << m_mountpoint;
91 m_dp->addItem(tr("Extracting file."),LOGINFO);
93 qDebug() << "file to unzip: " << m_file;
94 UnZip::ErrorCode ec;
95 UnZip uz;
96 ec = uz.openArchive(m_file);
97 if(ec != UnZip::Ok) {
98 m_dp->addItem(tr("Opening archive failed: %1.")
99 .arg(uz.formatError(ec)),LOGERROR);
100 m_dp->abort();
101 emit done(false);
102 return;
105 ec = uz.extractAll(m_mountpoint);
106 if(ec != UnZip::Ok) {
107 m_dp->addItem(tr("Extracting failed: %1.")
108 .arg(uz.formatError(ec)),LOGERROR);
109 m_dp->abort();
110 emit done(false);
111 return;
114 m_dp->addItem(tr("creating installation log"),LOGINFO);
116 QStringList zipContents = uz.fileList();
118 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
120 installlog.beginGroup(m_logsection);
121 for(int i = 0; i < zipContents.size(); i++)
123 installlog.setValue(zipContents.at(i),installlog.value(zipContents.at(i),0).toInt()+1);
125 installlog.endGroup();
127 // remove temporary file
128 downloadFile.remove();
130 m_dp->addItem(tr("Extraction finished successfully."),LOGOK);
131 m_dp->abort();
132 emit done(false);
135 void ZipInstaller::updateDataReadProgress(int read, int total)
137 m_dp->setProgressMax(total);
138 m_dp->setProgressValue(read);
139 qDebug() << "progress:" << read << "/" << total;