For multiprocessor targets, do the thread_exit routine such that we don't need to...
[kugel-rb.git] / rbutil / rbutilqt / themesinstallwindow.cpp
blobcb06b47b1f452d93d307ecdedbe0cd84434e62c5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
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 <QtGui>
22 #include "ui_themesinstallfrm.h"
23 #include "themesinstallwindow.h"
24 #include "zipinstaller.h"
25 #include "progressloggergui.h"
26 #include "utils.h"
27 #include "rbsettings.h"
28 #include "systeminfo.h"
29 #include "rockboxinfo.h"
30 #include "version.h"
32 ThemesInstallWindow::ThemesInstallWindow(QWidget *parent) : QDialog(parent)
34 ui.setupUi(this);
35 ui.listThemes->setAlternatingRowColors(true);
36 ui.listThemes->setSelectionMode(QAbstractItemView::ExtendedSelection);
37 ui.listThemes->setSortingEnabled(true);
38 ui.themePreview->clear();
39 ui.themePreview->setText(tr("no theme selected"));
40 ui.labelSize->setText(tr("no selection"));
41 ui.listThemes->setLayoutDirection(Qt::LeftToRight);
42 ui.themeDescription->setLayoutDirection(Qt::LeftToRight);
44 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
45 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
46 connect(ui.listThemes, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
47 this, SLOT(updateDetails(QListWidgetItem*, QListWidgetItem*)));
48 connect(ui.listThemes, SIGNAL(itemSelectionChanged()), this, SLOT(updateSize()));
49 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
52 ThemesInstallWindow::~ThemesInstallWindow()
54 if(infocachedir!="")
55 Utils::recursiveRmdir(infocachedir);
59 void ThemesInstallWindow::downloadInfo()
61 // try to get the current build information
62 getter = new HttpGet(this);
63 RockboxInfo installInfo
64 = RockboxInfo(RbSettings::value(RbSettings::Mountpoint).toString());
66 themesInfo.open();
67 qDebug() << "[Themes] downloading info to" << themesInfo.fileName();
68 themesInfo.close();
70 QString infoUrl = SystemInfo::value(SystemInfo::ThemesInfoUrl).toString();
71 infoUrl.replace("%TARGET%",
72 SystemInfo::value(SystemInfo::CurConfigureModel).toString());
73 infoUrl.replace("%REVISION%", installInfo.revision());
74 infoUrl.replace("%RELEASE%", installInfo.release());
75 infoUrl.replace("%RBUTILVER%", VERSION);
76 QUrl url = QUrl(infoUrl);
77 qDebug() << "[Themes] Info URL:" << url << "Query:" << url.queryItems();
78 if(RbSettings::value(RbSettings::CacheOffline).toBool())
79 getter->setCache(true);
80 getter->setFile(&themesInfo);
82 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
83 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
84 getter->getFile(url);
88 void ThemesInstallWindow::downloadDone(int id, bool error)
90 downloadDone(error);
91 qDebug() << "[Themes] Download" << id << "done, error:" << error;
95 void ThemesInstallWindow::downloadDone(bool error)
97 qDebug() << "[Themes] Download done, error:" << error;
99 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
100 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
101 themesInfo.open();
103 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
104 QStringList tl = iniDetails.childGroups();
105 qDebug() << "[Themes] Theme site result:"
106 << iniDetails.value("error/code").toString()
107 << iniDetails.value("error/description").toString()
108 << iniDetails.value("error/query").toString();
110 if(error) {
111 logger->addItem(tr("Network error: %1.\n"
112 "Please check your network and proxy settings.")
113 .arg(getter->errorString()), LOGERROR);
114 getter->abort();
115 logger->setFinished();
116 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
117 connect(logger, SIGNAL(closed()), this, SLOT(close()));
118 return;
120 // handle possible error codes
121 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
122 qDebug() << "[Themes] Theme site returned an error:"
123 << iniDetails.value("error/code");
124 logger->addItem(tr("the following error occured:\n%1")
125 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
126 logger->setFinished();
127 connect(logger, SIGNAL(closed()), this, SLOT(close()));
128 return;
130 logger->addItem(tr("done."), LOGOK);
131 logger->setFinished();
132 logger->close();
134 // setup list
135 for(int i = 0; i < tl.size(); i++) {
136 iniDetails.beginGroup(tl.at(i));
137 // skip all themes without name field set (i.e. error section)
138 if(iniDetails.value("name").toString().isEmpty()) {
139 iniDetails.endGroup();
140 continue;
142 qDebug() << "[Themes] adding to list:" << tl.at(i);
143 // convert to unicode and replace HTML-specific entities
144 QByteArray raw = iniDetails.value("name").toByteArray();
145 QTextCodec* codec = QTextCodec::codecForHtml(raw);
146 QString name = codec->toUnicode(raw);
147 name.replace("&quot;", "\"").replace("&amp;", "&");
148 name.replace("&lt;", "<").replace("&gt;", ">");
149 QListWidgetItem *w = new QListWidgetItem;
150 w->setData(Qt::DisplayRole, name.trimmed());
151 w->setData(Qt::UserRole, tl.at(i));
152 ui.listThemes->addItem(w);
154 iniDetails.endGroup();
156 // check if there's a themes "MOTD" available
157 if(iniDetails.contains("status/msg")) {
158 // check if there's a localized msg available
159 QString lang = RbSettings::value(RbSettings::Language).toString().split("_").at(0);
160 QString msg;
161 if(iniDetails.contains("status/msg." + lang))
162 msg = iniDetails.value("status/msg." + lang).toString();
163 else
164 msg = iniDetails.value("status/msg").toString();
165 qDebug() << "[Themes] MOTD" << msg;
166 if(!msg.isEmpty())
167 QMessageBox::information(this, tr("Information"), msg);
172 void ThemesInstallWindow::updateSize(void)
174 long size = 0;
175 // sum up size for all selected themes
176 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
177 int items = ui.listThemes->selectedItems().size();
178 for(int i = 0; i < items; i++) {
179 iniDetails.beginGroup(ui.listThemes->selectedItems()
180 .at(i)->data(Qt::UserRole).toString());
181 size += iniDetails.value("size").toInt();
182 iniDetails.endGroup();
184 ui.labelSize->setText(tr("Download size %L1 kiB (%n item(s))", "", items)
185 .arg((size + 512) / 1024));
189 void ThemesInstallWindow::updateDetails(QListWidgetItem* cur, QListWidgetItem* prev)
191 if(cur == prev)
192 return;
194 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
196 QCoreApplication::processEvents();
197 ui.themeDescription->setText(tr("fetching details for %1")
198 .arg(cur->data(Qt::DisplayRole).toString()));
199 ui.themePreview->clear();
200 ui.themePreview->setText(tr("fetching preview ..."));
201 imgData.clear();
203 iniDetails.beginGroup(cur->data(Qt::UserRole).toString());
205 QUrl img, txt;
206 txt = QUrl(QString(SystemInfo::value(SystemInfo::ThemesUrl).toString() + "/"
207 + iniDetails.value("descriptionfile").toString()));
208 img = QUrl(QString(SystemInfo::value(SystemInfo::ThemesUrl).toString() + "/"
209 + iniDetails.value("image").toString()));
211 QString text;
212 QTextCodec* codec = QTextCodec::codecForName("UTF-8");
213 text = tr("<b>Author:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
214 .value("author", tr("unknown")).toByteArray()));
215 text += tr("<b>Version:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
216 .value("version", tr("unknown")).toByteArray()));
217 text += tr("<b>Description:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
218 .value("about", tr("no description")).toByteArray()));
220 text.trimmed();
221 text.replace("\n", "<br/>");
222 ui.themeDescription->setHtml(text);
223 iniDetails.endGroup();
225 igetter.abort();
226 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
227 igetter.setCache(true);
228 else
230 if(infocachedir=="")
232 infocachedir = QDir::tempPath() + "rbutil-themeinfo";
233 QDir d = QDir::temp();
234 d.mkdir("rbutil-themeinfo");
236 igetter.setCache(infocachedir);
239 igetter.getFile(img);
243 void ThemesInstallWindow::updateImage(bool error)
245 qDebug() << "[Themes] Updating image:"<< !error;
247 if(error) {
248 ui.themePreview->clear();
249 ui.themePreview->setText(tr("Retrieving theme preview failed.\n"
250 "HTTP response code: %1").arg(igetter.httpResponse()));
251 return;
254 QPixmap p;
255 if(!error) {
256 imgData = igetter.readAll();
257 if(imgData.isNull()) return;
258 p.loadFromData(imgData);
259 if(p.isNull()) {
260 ui.themePreview->clear();
261 ui.themePreview->setText(tr("no theme preview"));
263 else
264 ui.themePreview->setPixmap(p);
269 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
271 qDebug() << "[Themes]" << e;
273 QPixmap p, q;
274 QSize img;
275 img.setHeight(ui.themePreview->height());
276 img.setWidth(ui.themePreview->width());
278 p.loadFromData(imgData);
279 if(p.isNull()) return;
280 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
281 ui.themePreview->setScaledContents(false);
282 ui.themePreview->setPixmap(p);
287 void ThemesInstallWindow::show()
289 QDialog::show();
290 logger = new ProgressLoggerGui(this);
291 logger->show();
292 logger->addItem(tr("getting themes information ..."), LOGINFO);
294 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
296 downloadInfo();
301 void ThemesInstallWindow::abort()
303 igetter.abort();
304 logger->setFinished();
305 this->close();
309 void ThemesInstallWindow::accept()
311 if(ui.listThemes->selectedItems().size() == 0) {
312 this->close();
313 return;
315 QStringList themes;
316 QStringList names;
317 QStringList version;
318 QString zip;
319 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
320 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
321 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
322 zip = SystemInfo::value(SystemInfo::ThemesUrl).toString()
323 + "/" + iniDetails.value("archive").toString();
324 themes.append(zip);
325 names.append("Theme: " +
326 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
327 // if no version info is available use installation (current) date
328 version.append(iniDetails.value("version",
329 QDate().currentDate().toString("yyyyMMdd")).toString());
330 iniDetails.endGroup();
332 qDebug() << "[Themes] installing:" << themes;
334 logger = new ProgressLoggerGui(this);
335 logger->show();
336 QString mountPoint = RbSettings::value(RbSettings::Mountpoint).toString();
337 qDebug() << "[Themes] mountpoint:" << mountPoint;
338 // show dialog with error if mount point is wrong
339 if(!QFileInfo(mountPoint).isDir()) {
340 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
341 logger->setFinished();
342 return;
345 installer = new ZipInstaller(this);
346 installer->setUrl(themes);
347 installer->setLogSection(names);
348 installer->setLogVersion(version);
349 installer->setMountPoint(mountPoint);
350 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
351 installer->setCache(true);
353 connect(logger, SIGNAL(closed()), this, SLOT(close()));
354 connect(installer, SIGNAL(logItem(QString, int)), logger, SLOT(addItem(QString, int)));
355 connect(installer, SIGNAL(logProgress(int, int)), logger, SLOT(setProgress(int, int)));
356 connect(installer, SIGNAL(done(bool)), logger, SLOT(setFinished()));
357 connect(logger, SIGNAL(aborted()), installer, SLOT(abort()));
358 installer->install();