pcmbuf: need ifdef to fix yellow
[kugel-rb.git] / rbutil / rbutilqt / themesinstallwindow.cpp
blobc36560a47b65398d0faf4b8698fdc15f3feff9c8
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"
29 ThemesInstallWindow::ThemesInstallWindow(QWidget *parent) : QDialog(parent)
31 ui.setupUi(this);
32 ui.listThemes->setAlternatingRowColors(true);
33 ui.listThemes->setSelectionMode(QAbstractItemView::ExtendedSelection);
34 ui.listThemes->setSortingEnabled(true);
35 ui.themePreview->clear();
36 ui.themePreview->setText(tr("no theme selected"));
37 ui.labelSize->setText(tr("no selection"));
38 ui.themeDescription->setLayoutDirection(Qt::LeftToRight);
40 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
41 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
42 connect(ui.listThemes, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
43 this, SLOT(updateDetails(QListWidgetItem*, QListWidgetItem*)));
44 connect(ui.listThemes, SIGNAL(itemSelectionChanged()), this, SLOT(updateSize()));
45 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
48 ThemesInstallWindow::~ThemesInstallWindow()
50 if(infocachedir!="")
51 recRmdir(infocachedir);
55 void ThemesInstallWindow::downloadInfo()
57 // try to get the current build information
58 getter = new HttpGet(this);
60 themesInfo.open();
61 qDebug() << "[Themes] downloading info to" << themesInfo.fileName();
62 themesInfo.close();
64 QUrl url;
65 url = QUrl(RbSettings::value(RbSettings::ThemesUrl).toString()
66 + "/rbutilqt.php?target="
67 + RbSettings::value(RbSettings::CurConfigureModel).toString());
68 qDebug() << "[Themes] Info URL:" << url << "Query:" << url.queryItems();
69 if(RbSettings::value(RbSettings::CacheOffline).toBool())
70 getter->setCache(true);
71 getter->setFile(&themesInfo);
73 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
74 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
75 getter->getFile(url);
79 void ThemesInstallWindow::downloadDone(int id, bool error)
81 downloadDone(error);
82 qDebug() << "[Themes] Download" << id << "done, error:" << error;
86 void ThemesInstallWindow::downloadDone(bool error)
88 qDebug() << "[Themes] Download done, error:" << error;
90 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
91 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
92 themesInfo.open();
94 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
95 QStringList tl = iniDetails.childGroups();
96 qDebug() << "[Themes] Theme site result:"
97 << iniDetails.value("error/code").toString()
98 << iniDetails.value("error/description").toString()
99 << iniDetails.value("error/query").toString();
101 if(error) {
102 logger->addItem(tr("Network error: %1.\n"
103 "Please check your network and proxy settings.")
104 .arg(getter->errorString()), LOGERROR);
105 getter->abort();
106 logger->setFinished();
107 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
108 connect(logger, SIGNAL(closed()), this, SLOT(close()));
109 return;
111 // handle possible error codes
112 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
113 qDebug() << "[Themes] Theme site returned an error:"
114 << iniDetails.value("error/code");
115 logger->addItem(tr("the following error occured:\n%1")
116 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
117 logger->setFinished();
118 connect(logger, SIGNAL(closed()), this, SLOT(close()));
119 return;
121 logger->addItem(tr("done."), LOGOK);
122 logger->setFinished();
123 logger->close();
125 // setup list
126 for(int i = 0; i < tl.size(); i++) {
127 iniDetails.beginGroup(tl.at(i));
128 // skip all themes without name field set (i.e. error section)
129 if(iniDetails.value("name").toString().isEmpty()) {
130 iniDetails.endGroup();
131 continue;
133 qDebug() << "[Themes] adding to list:" << tl.at(i);
134 // convert to unicode and replace HTML-specific entities
135 QByteArray raw = iniDetails.value("name").toByteArray();
136 QTextCodec* codec = QTextCodec::codecForHtml(raw);
137 QString name = codec->toUnicode(raw);
138 name.replace("&quot;", "\"").replace("&amp;", "&");
139 name.replace("&lt;", "<").replace("&gt;", ">");
140 QListWidgetItem *w = new QListWidgetItem;
141 w->setData(Qt::DisplayRole, name.trimmed());
142 w->setData(Qt::UserRole, tl.at(i));
143 ui.listThemes->addItem(w);
145 iniDetails.endGroup();
147 // check if there's a themes "MOTD" available
148 if(iniDetails.contains("status/msg")) {
149 // check if there's a localized msg available
150 QString lang = RbSettings::value(RbSettings::Language).toString().split("_").at(0);
151 QString msg;
152 if(iniDetails.contains("status/msg." + lang))
153 msg = iniDetails.value("status/msg." + lang).toString();
154 else
155 msg = iniDetails.value("status/msg").toString();
156 qDebug() << "[Themes] MOTD" << msg;
157 if(!msg.isEmpty())
158 QMessageBox::information(this, tr("Information"), msg);
163 void ThemesInstallWindow::updateSize(void)
165 long size = 0;
166 // sum up size for all selected themes
167 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
168 int items = ui.listThemes->selectedItems().size();
169 for(int i = 0; i < items; i++) {
170 iniDetails.beginGroup(ui.listThemes->selectedItems()
171 .at(i)->data(Qt::UserRole).toString());
172 size += iniDetails.value("size").toInt();
173 iniDetails.endGroup();
175 ui.labelSize->setText(tr("Download size %L1 kiB (%n item(s))", "", items)
176 .arg((size + 512) / 1024));
180 void ThemesInstallWindow::updateDetails(QListWidgetItem* cur, QListWidgetItem* prev)
182 if(cur == prev)
183 return;
185 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
187 QCoreApplication::processEvents();
188 ui.themeDescription->setText(tr("fetching details for %1")
189 .arg(cur->data(Qt::DisplayRole).toString()));
190 ui.themePreview->clear();
191 ui.themePreview->setText(tr("fetching preview ..."));
192 imgData.clear();
194 iniDetails.beginGroup(cur->data(Qt::UserRole).toString());
196 QUrl img, txt;
197 txt = QUrl(QString(RbSettings::value(RbSettings::ThemesUrl).toString() + "/"
198 + iniDetails.value("descriptionfile").toString()));
199 img = QUrl(QString(RbSettings::value(RbSettings::ThemesUrl).toString() + "/"
200 + iniDetails.value("image").toString()));
202 QString text;
203 QTextCodec* codec = QTextCodec::codecForName("UTF-8");
204 text = tr("<b>Author:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
205 .value("author", tr("unknown")).toByteArray()));
206 text += tr("<b>Version:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
207 .value("version", tr("unknown")).toByteArray()));
208 text += tr("<b>Description:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
209 .value("about", tr("no description")).toByteArray()));
211 text.trimmed();
212 text.replace("\n", "<br/>");
213 ui.themeDescription->setHtml(text);
214 iniDetails.endGroup();
216 igetter.abort();
217 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
218 igetter.setCache(true);
219 else
221 if(infocachedir=="")
223 infocachedir = QDir::tempPath() + "rbutil-themeinfo";
224 QDir d = QDir::temp();
225 d.mkdir("rbutil-themeinfo");
227 igetter.setCache(infocachedir);
230 igetter.getFile(img);
234 void ThemesInstallWindow::updateImage(bool error)
236 qDebug() << "[Themes] Updating image:"<< !error;
238 if(error) {
239 ui.themePreview->clear();
240 ui.themePreview->setText(tr("Retrieving theme preview failed.\n"
241 "HTTP response code: %1").arg(igetter.httpResponse()));
242 return;
245 QPixmap p;
246 if(!error) {
247 imgData = igetter.readAll();
248 if(imgData.isNull()) return;
249 p.loadFromData(imgData);
250 if(p.isNull()) {
251 ui.themePreview->clear();
252 ui.themePreview->setText(tr("no theme preview"));
254 else
255 ui.themePreview->setPixmap(p);
260 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
262 qDebug() << "[Themes]" << e;
264 QPixmap p, q;
265 QSize img;
266 img.setHeight(ui.themePreview->height());
267 img.setWidth(ui.themePreview->width());
269 p.loadFromData(imgData);
270 if(p.isNull()) return;
271 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
272 ui.themePreview->setScaledContents(false);
273 ui.themePreview->setPixmap(p);
278 void ThemesInstallWindow::show()
280 QDialog::show();
281 logger = new ProgressLoggerGui(this);
282 logger->show();
283 logger->addItem(tr("getting themes information ..."), LOGINFO);
285 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
287 downloadInfo();
292 void ThemesInstallWindow::abort()
294 igetter.abort();
295 logger->setFinished();
296 this->close();
300 void ThemesInstallWindow::accept()
302 if(ui.listThemes->selectedItems().size() == 0) {
303 this->close();
304 return;
306 QStringList themes;
307 QStringList names;
308 QStringList version;
309 QString zip;
310 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
311 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
312 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
313 zip = RbSettings::value(RbSettings::ThemesUrl).toString()
314 + "/" + iniDetails.value("archive").toString();
315 themes.append(zip);
316 names.append("Theme: " +
317 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
318 // if no version info is available use installation (current) date
319 version.append(iniDetails.value("version",
320 QDate().currentDate().toString("yyyyMMdd")).toString());
321 iniDetails.endGroup();
323 qDebug() << "[Themes] installing:" << themes;
325 logger = new ProgressLoggerGui(this);
326 logger->show();
327 QString mountPoint = RbSettings::value(RbSettings::Mountpoint).toString();
328 qDebug() << "[Themes] mountpoint:" << mountPoint;
329 // show dialog with error if mount point is wrong
330 if(!QFileInfo(mountPoint).isDir()) {
331 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
332 logger->setFinished();
333 return;
336 installer = new ZipInstaller(this);
337 installer->setUrl(themes);
338 installer->setLogSection(names);
339 installer->setLogVersion(version);
340 installer->setMountPoint(mountPoint);
341 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
342 installer->setCache(true);
344 connect(logger, SIGNAL(closed()), this, SLOT(close()));
345 connect(installer, SIGNAL(logItem(QString, int)), logger, SLOT(addItem(QString, int)));
346 connect(installer, SIGNAL(logProgress(int, int)), logger, SLOT(setProgress(int, int)));
347 connect(installer, SIGNAL(done(bool)), logger, SLOT(setFinished()));
348 connect(logger, SIGNAL(aborted()), installer, SLOT(abort()));
349 installer->install();