Handle language change events in widgets.
[maemo-rb.git] / rbutil / rbutilqt / base / bootloaderinstallbase.cpp
blob7a9258accc5e172541d56fdfdc1109a05aeb6a73
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2008 by Dominik Riebeling
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
20 #include <QtCore>
22 #include "bootloaderinstallbase.h"
23 #include "utils.h"
24 #include "ziputil.h"
26 #if defined(Q_OS_MACX)
27 #include <sys/param.h>
28 #include <sys/ucred.h>
29 #include <sys/mount.h>
30 #endif
33 BootloaderInstallBase::BootloaderType BootloaderInstallBase::installed(void)
35 return BootloaderUnknown;
39 BootloaderInstallBase::Capabilities BootloaderInstallBase::capabilities(void)
41 return 0;
45 void BootloaderInstallBase::downloadBlStart(QUrl source)
47 m_http.setFile(&m_tempfile);
48 m_http.setCache(true);
49 connect(&m_http, SIGNAL(done(bool)), this, SLOT(downloadBlFinish(bool)));
50 // connect the http read signal to our logProgess *signal*
51 // to immediately emit it without any helper function.
52 connect(&m_http, SIGNAL(dataReadProgress(int, int)),
53 this, SIGNAL(logProgress(int, int)));
54 m_http.getFile(source);
58 void BootloaderInstallBase::downloadReqFinished(int id, bool error)
60 qDebug() << "[BootloaderInstallBase] Download Request" << id
61 << "finished, error:" << m_http.errorString();
63 downloadBlFinish(error);
67 void BootloaderInstallBase::downloadBlFinish(bool error)
69 qDebug() << "[BootloaderInstallBase] Downloading bootloader finished, error:"
70 << error;
72 // update progress bar
73 emit logProgress(100, 100);
75 if(m_http.httpResponse() != 200) {
76 emit logItem(tr("Download error: received HTTP error %1.")
77 .arg(m_http.errorString()), LOGERROR);
78 emit done(true);
79 return;
81 if(error) {
82 emit logItem(tr("Download error: %1")
83 .arg(m_http.error()), LOGERROR);
84 emit done(true);
85 return;
87 else if(m_http.isCached())
88 emit logItem(tr("Download finished (cache used)."), LOGOK);
89 else
90 emit logItem(tr("Download finished."), LOGOK);
92 QCoreApplication::processEvents();
93 m_blversion = m_http.timestamp();
94 emit downloadDone();
98 void BootloaderInstallBase::installBlfile(void)
100 qDebug() << "[BootloaderInstallBase] installBlFile(void)";
104 //! @brief backup OF file.
105 //! @param to folder to write backup file to. Folder will get created.
106 //! @return true on success, false on error.
107 bool BootloaderInstallBase::backup(QString to)
109 qDebug() << "[BootloaderInstallBase] Backing up bootloader file";
110 QDir targetDir(".");
111 emit logItem(tr("Creating backup of original firmware file."), LOGINFO);
112 if(!targetDir.mkpath(to)) {
113 emit logItem(tr("Creating backup folder failed"), LOGERROR);
114 return false;
116 QString tofile = to + "/" + QFileInfo(m_blfile).fileName();
117 qDebug() << "[BootloaderInstallBase] trying to backup" << m_blfile << "to" << tofile;
118 if(!QFile::copy(Utils::resolvePathCase(m_blfile), tofile)) {
119 emit logItem(tr("Creating backup copy failed."), LOGERROR);
120 return false;
122 emit logItem(tr("Backup created."), LOGOK);
123 return true;
127 //! @brief log installation to logfile.
128 //! @param mode action to perform. 0: add to log, 1: remove from log.
129 //! @return 0 on success
130 int BootloaderInstallBase::logInstall(LogMode mode)
132 int result = 0;
133 QString section = m_blurl.path().section('/', -1);
134 QSettings s(m_logfile, QSettings::IniFormat, this);
135 emit logItem(tr("Creating installation log"), LOGINFO);
137 if(mode == LogAdd) {
138 s.setValue("Bootloader/" + section, m_blversion.toString(Qt::ISODate));
139 qDebug() << "[BootloaderInstallBase] Writing log, version:"
140 << m_blversion.toString(Qt::ISODate);
142 else {
143 s.remove("Bootloader/" + section);
145 s.sync();
147 emit logItem(tr("Installation log created"), LOGOK);
149 return result;
153 #if defined(Q_OS_MACX)
154 void BootloaderInstallBase::waitRemount()
156 m_remountTries = 600;
157 emit logItem(tr("Waiting for system to remount player"), LOGINFO);
159 QTimer::singleShot(100, this, SLOT(checkRemount()));
161 #endif
164 void BootloaderInstallBase::checkRemount()
166 #if defined(Q_OS_MACX)
167 if(m_remountTries--) {
168 int status = 0;
169 // check if device has been remounted
170 QCoreApplication::processEvents();
171 int num;
172 struct statfs *mntinf;
174 num = getmntinfo(&mntinf, MNT_WAIT);
175 while(num--) {
176 if(QString(mntinf->f_mntfromname).startsWith(m_remountDevice)
177 && QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive))
178 status = 1;
179 mntinf++;
181 if(!status) {
182 // still not remounted, restart timer.
183 QTimer::singleShot(500, this, SLOT(checkRemount()));
184 qDebug() << "[BootloaderInstallBase] Player not remounted yet" << m_remountDevice;
186 else {
187 emit logItem(tr("Player remounted"), LOGINFO);
188 emit remounted(true);
191 else {
192 emit logItem(tr("Timeout on remount"), LOGERROR);
193 emit remounted(false);
195 #endif
199 //! @brief set list of possible bootloader files and pick the existing one.
200 //! @param sl list of possible bootloader files.
201 void BootloaderInstallBase::setBlFile(QStringList sl)
203 // figue which of the possible bootloader filenames is correct.
204 for(int a = 0; a < sl.size(); a++) {
205 if(!Utils::resolvePathCase(sl.at(a)).isEmpty()) {
206 m_blfile = sl.at(a);
209 if(m_blfile.isEmpty()) {
210 m_blfile = sl.at(0);
215 bool BootloaderInstallBase::setOfFile(QString of, QStringList blfile)
217 bool found = false;
218 ZipUtil z(this);
219 // check if the file set is in zip format
220 if(z.open(of)) {
221 emit logItem(tr("Zip file format detected"), LOGINFO);
222 QStringList contents = z.files();
223 qDebug() << "[BootloaderInstallBase] archive contains:" << contents;
224 for(int i = 0; i < blfile.size(); ++i) {
225 // strip any path, we don't know the structure in the zip
226 QString f = QFileInfo(blfile.at(i)).fileName();
227 qDebug() << "[BootloaderInstallBase] searching archive for" << f;
228 // contents.indexOf() works case sensitive. Since the filename
229 // casing is unknown (and might change) do this manually.
230 // FIXME: support files in folders
231 for(int j = 0; j < contents.size(); ++j) {
232 if(contents.at(j).compare(f, Qt::CaseInsensitive) == 0) {
233 found = true;
234 emit logItem(tr("Extracting firmware %1 from archive")
235 .arg(f), LOGINFO);
236 // store in class temporary file
237 m_tempof.open();
238 m_offile = m_tempof.fileName();
239 m_tempof.close();
240 if(!z.extractArchive(m_offile, contents.at(j))) {
241 emit logItem(tr("Error extracting firmware from archive"), LOGERROR);
242 found = false;
243 break;
245 break;
249 if(!found) {
250 emit logItem(tr("Could not find firmware in archive"), LOGERROR);
254 else {
255 m_offile = of;
256 found = true;
258 return found;