test test test
[kugel-rb.git] / rbutil / rbutilqt / themesinstallwindow.cpp
blob9a9d5957baccb8ff979fd939db1e763694a99974
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"));
39 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
40 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
41 connect(ui.listThemes, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
42 this, SLOT(updateDetails(QListWidgetItem*, QListWidgetItem*)));
43 connect(ui.listThemes, SIGNAL(itemSelectionChanged()), this, SLOT(updateSize()));
44 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
47 ThemesInstallWindow::~ThemesInstallWindow()
49 if(infocachedir!="")
50 recRmdir(infocachedir);
54 void ThemesInstallWindow::downloadInfo()
56 // try to get the current build information
57 getter = new HttpGet(this);
59 themesInfo.open();
60 qDebug() << "[Themes] downloading info to" << themesInfo.fileName();
61 themesInfo.close();
63 QUrl url;
64 url = QUrl(RbSettings::value(RbSettings::ThemesUrl).toString()
65 + "/rbutilqt.php?target="
66 + RbSettings::value(RbSettings::CurConfigureModel).toString());
67 qDebug() << "[Themes] Info URL:" << url << "Query:" << url.queryItems();
68 if(RbSettings::value(RbSettings::CacheOffline).toBool())
69 getter->setCache(true);
70 getter->setFile(&themesInfo);
72 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
73 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
74 getter->getFile(url);
78 void ThemesInstallWindow::downloadDone(int id, bool error)
80 downloadDone(error);
81 qDebug() << "[Themes] Download" << id << "done, error:" << error;
85 void ThemesInstallWindow::downloadDone(bool error)
87 qDebug() << "[Themes] Download done, error:" << error;
89 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
90 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
91 themesInfo.open();
93 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
94 QStringList tl = iniDetails.childGroups();
95 qDebug() << "[Themes] Theme site result:"
96 << iniDetails.value("error/code").toString()
97 << iniDetails.value("error/description").toString()
98 << iniDetails.value("error/query").toString();
100 if(error) {
101 logger->addItem(tr("Network error: %1.\n"
102 "Please check your network and proxy settings.")
103 .arg(getter->errorString()), LOGERROR);
104 getter->abort();
105 logger->setFinished();
106 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
107 connect(logger, SIGNAL(closed()), this, SLOT(close()));
108 return;
110 // handle possible error codes
111 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
112 qDebug() << "[Themes] Theme site returned an error:"
113 << iniDetails.value("error/code");
114 logger->addItem(tr("the following error occured:\n%1")
115 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
116 logger->setFinished();
117 connect(logger, SIGNAL(closed()), this, SLOT(close()));
118 return;
120 logger->addItem(tr("done."), LOGOK);
121 logger->setFinished();
122 logger->close();
124 // setup list
125 for(int i = 0; i < tl.size(); i++) {
126 iniDetails.beginGroup(tl.at(i));
127 // skip all themes without name field set (i.e. error section)
128 if(iniDetails.value("name").toString().isEmpty()) {
129 iniDetails.endGroup();
130 continue;
132 qDebug() << "[Themes] adding to list:" << tl.at(i);
133 // convert to unicode and replace HTML-specific entities
134 QByteArray raw = iniDetails.value("name").toByteArray();
135 QTextCodec* codec = QTextCodec::codecForHtml(raw);
136 QString name = codec->toUnicode(raw);
137 name.replace("&quot;", "\"").replace("&amp;", "&");
138 name.replace("&lt;", "<").replace("&gt;", ">");
139 QListWidgetItem *w = new QListWidgetItem;
140 w->setData(Qt::DisplayRole, name.trimmed());
141 w->setData(Qt::UserRole, tl.at(i));
142 ui.listThemes->addItem(w);
144 iniDetails.endGroup();
146 // check if there's a themes "MOTD" available
147 if(iniDetails.contains("status/msg")) {
148 // check if there's a localized msg available
149 QString lang = RbSettings::value(RbSettings::Language).toString().split("_").at(0);
150 QString msg;
151 if(iniDetails.contains("status/msg." + lang))
152 msg = iniDetails.value("status/msg." + lang).toString();
153 else
154 msg = iniDetails.value("status/msg").toString();
155 qDebug() << "[Themes] MOTD" << msg;
156 if(!msg.isEmpty())
157 QMessageBox::information(this, tr("Information"), msg);
162 void ThemesInstallWindow::updateSize(void)
164 long size = 0;
165 // sum up size for all selected themes
166 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
167 int items = ui.listThemes->selectedItems().size();
168 for(int i = 0; i < items; i++) {
169 iniDetails.beginGroup(ui.listThemes->selectedItems()
170 .at(i)->data(Qt::UserRole).toString());
171 size += iniDetails.value("size").toInt();
172 iniDetails.endGroup();
174 ui.labelSize->setText(tr("Download size %L1 kiB (%n item(s))", "", items)
175 .arg((size + 512) / 1024));
179 void ThemesInstallWindow::updateDetails(QListWidgetItem* cur, QListWidgetItem* prev)
181 if(cur == prev)
182 return;
184 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
186 QCoreApplication::processEvents();
187 ui.themeDescription->setText(tr("fetching details for %1")
188 .arg(cur->data(Qt::DisplayRole).toString()));
189 ui.themePreview->clear();
190 ui.themePreview->setText(tr("fetching preview ..."));
191 imgData.clear();
193 iniDetails.beginGroup(cur->data(Qt::UserRole).toString());
195 QUrl img, txt;
196 txt = QUrl(QString(RbSettings::value(RbSettings::ThemesUrl).toString() + "/"
197 + iniDetails.value("descriptionfile").toString()));
198 img = QUrl(QString(RbSettings::value(RbSettings::ThemesUrl).toString() + "/"
199 + iniDetails.value("image").toString()));
201 QString text;
202 QTextCodec* codec = QTextCodec::codecForName("UTF-8");
203 text = tr("<b>Author:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
204 .value("author", tr("unknown")).toByteArray()));
205 text += tr("<b>Version:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
206 .value("version", tr("unknown")).toByteArray()));
207 text += tr("<b>Description:</b> %1<hr/>").arg(codec->toUnicode(iniDetails
208 .value("about", tr("no description")).toByteArray()));
210 text.trimmed();
211 text.replace("\n", "<br/>");
212 ui.themeDescription->setHtml(text);
213 iniDetails.endGroup();
215 igetter.abort();
216 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
217 igetter.setCache(true);
218 else
220 if(infocachedir=="")
222 infocachedir = QDir::tempPath() + "rbutil-themeinfo";
223 QDir d = QDir::temp();
224 d.mkdir("rbutil-themeinfo");
226 igetter.setCache(infocachedir);
229 igetter.getFile(img);
233 void ThemesInstallWindow::updateImage(bool error)
235 qDebug() << "[Themes] Updating image:"<< !error;
237 if(error) {
238 ui.themePreview->clear();
239 ui.themePreview->setText(tr("Retrieving theme preview failed.\n"
240 "HTTP response code: %1").arg(igetter.httpResponse()));
241 return;
244 QPixmap p;
245 if(!error) {
246 imgData = igetter.readAll();
247 if(imgData.isNull()) return;
248 p.loadFromData(imgData);
249 if(p.isNull()) {
250 ui.themePreview->clear();
251 ui.themePreview->setText(tr("no theme preview"));
253 else
254 ui.themePreview->setPixmap(p);
259 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
261 qDebug() << "[Themes]" << e;
263 QPixmap p, q;
264 QSize img;
265 img.setHeight(ui.themePreview->height());
266 img.setWidth(ui.themePreview->width());
268 p.loadFromData(imgData);
269 if(p.isNull()) return;
270 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
271 ui.themePreview->setScaledContents(false);
272 ui.themePreview->setPixmap(p);
277 void ThemesInstallWindow::show()
279 QDialog::show();
280 logger = new ProgressLoggerGui(this);
281 logger->show();
282 logger->addItem(tr("getting themes information ..."), LOGINFO);
284 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
286 downloadInfo();
291 void ThemesInstallWindow::abort()
293 igetter.abort();
294 logger->setFinished();
295 this->close();
299 void ThemesInstallWindow::accept()
301 if(ui.listThemes->selectedItems().size() == 0) {
302 this->close();
303 return;
305 QStringList themes;
306 QStringList names;
307 QStringList version;
308 QString zip;
309 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
310 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
311 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
312 zip = RbSettings::value(RbSettings::ThemesUrl).toString()
313 + "/" + iniDetails.value("archive").toString();
314 themes.append(zip);
315 names.append("Theme: " +
316 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
317 // if no version info is available use installation (current) date
318 version.append(iniDetails.value("version",
319 QDate().currentDate().toString("yyyyMMdd")).toString());
320 iniDetails.endGroup();
322 qDebug() << "[Themes] installing:" << themes;
324 logger = new ProgressLoggerGui(this);
325 logger->show();
326 QString mountPoint = RbSettings::value(RbSettings::Mountpoint).toString();
327 qDebug() << "[Themes] mountpoint:" << mountPoint;
328 // show dialog with error if mount point is wrong
329 if(!QFileInfo(mountPoint).isDir()) {
330 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
331 logger->setFinished();
332 return;
335 installer = new ZipInstaller(this);
336 installer->setUrl(themes);
337 installer->setLogSection(names);
338 installer->setLogVersion(version);
339 installer->setMountPoint(mountPoint);
340 if(!RbSettings::value(RbSettings::CacheDisabled).toBool())
341 installer->setCache(true);
343 connect(logger, SIGNAL(closed()), this, SLOT(close()));
344 connect(installer, SIGNAL(logItem(QString, int)), logger, SLOT(addItem(QString, int)));
345 connect(installer, SIGNAL(logProgress(int, int)), logger, SLOT(setProgress(int, int)));
346 connect(installer, SIGNAL(done(bool)), logger, SLOT(setFinished()));
347 connect(logger, SIGNAL(aborted()), installer, SLOT(abort()));
348 installer->install();