1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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"
23 #include "zip/unzip.h"
25 ZipInstaller::ZipInstaller(QObject
* parent
): QObject(parent
)
31 void ZipInstaller::install(ProgressloggerInterface
* 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
40 m_file
= downloadFile
.fileName();
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();
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();
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
);
81 m_dp
->addItem(tr("Download error: %1").arg(getter
->errorString()),LOGERROR
);
86 else m_dp
->addItem(tr("Download finished."),LOGOK
);
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
;
97 ec
= uz
.openArchive(m_file
);
99 m_dp
->addItem(tr("Opening archive failed: %1.")
100 .arg(uz
.formatError(ec
)),LOGERROR
);
102 downloadFile
.remove();
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
);
112 downloadFile
.remove();
116 // prepare file list for log
117 zipContents
= uz
.fileList();
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();
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
);
134 downloadFile
.remove();
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
);
161 void ZipInstaller::updateDataReadProgress(int read
, int total
)
163 m_dp
->setProgressMax(total
);
164 m_dp
->setProgressValue(read
);
165 qDebug() << "progress:" << read
<< "/" << total
;