codec.link must be created with -DCODEC
[kugel-rb.git] / rbutil / rbutilqt / installthemes.cpp
blobfe225b81b4f13f94c39883186bb434424bb507ce
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_installthemesfrm.h"
23 #include "installthemes.h"
24 #include "installzip.h"
25 #include "progressloggergui.h"
26 #include "utils.h"
28 ThemesInstallWindow::ThemesInstallWindow(QWidget *parent) : QDialog(parent)
30 ui.setupUi(this);
31 ui.listThemes->setAlternatingRowColors(true);
32 ui.listThemes->setSelectionMode(QAbstractItemView::ExtendedSelection);
33 ui.themePreview->clear();
34 ui.themePreview->setText(tr("no theme selected"));
36 connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
37 connect(ui.buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
40 ThemesInstallWindow::~ThemesInstallWindow()
42 if(infocachedir!="")
43 recRmdir(infocachedir);
46 QString ThemesInstallWindow::resolution()
48 return settings->curResolution();
52 void ThemesInstallWindow::downloadInfo()
54 // try to get the current build information
55 getter = new HttpGet(this);
57 qDebug() << "downloading themes info";
58 themesInfo.open();
59 qDebug() << "file:" << themesInfo.fileName();
60 themesInfo.close();
62 QUrl url;
63 url = QUrl(settings->themeUrl() + "/rbutilqt.php?res=" + resolution());
64 qDebug() << "downloadInfo()" << url;
65 qDebug() << url.queryItems();
66 if(settings->cacheOffline())
67 getter->setCache(true);
68 getter->setFile(&themesInfo);
70 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
71 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
72 getter->getFile(url);
76 void ThemesInstallWindow::downloadDone(int id, bool error)
78 downloadDone(error);
79 qDebug() << "downloadDone(bool) =" << id << error;
83 void ThemesInstallWindow::downloadDone(bool error)
85 qDebug() << "downloadDone(bool) =" << error;
87 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
88 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
89 themesInfo.open();
91 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
92 QStringList tl = iniDetails.childGroups();
93 qDebug() << tl.size();
94 qDebug() << iniDetails.value("error/code").toString()
95 << iniDetails.value("error/description").toString()
96 << iniDetails.value("error/query").toString();
98 if(error) {
99 logger->addItem(tr("Network error: %1.\nPlease check your network and proxy settings.")
100 .arg(getter->errorString()), LOGERROR);
101 getter->abort();
102 logger->abort();
103 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
104 connect(logger, SIGNAL(closed()), this, SLOT(close()));
105 return;
107 // handle possible error codes
108 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
109 qDebug() << "error!";
110 logger->addItem(tr("the following error occured:\n%1")
111 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
112 logger->abort();
113 connect(logger, SIGNAL(closed()), this, SLOT(close()));
114 return;
116 logger->addItem(tr("done."), LOGOK);
117 logger->abort();
118 logger->close();
120 // setup list
121 for(int i = 0; i < tl.size(); i++) {
122 iniDetails.beginGroup(tl.at(i));
123 // skip all themes without name field set (i.e. error section)
124 if(iniDetails.value("name").toString().isEmpty()) continue;
125 QListWidgetItem *w = new QListWidgetItem;
126 w->setData(Qt::DisplayRole, iniDetails.value("name").toString());
127 w->setData(Qt::UserRole, tl.at(i));
128 ui.listThemes->addItem(w);
130 iniDetails.endGroup();
132 connect(ui.listThemes, SIGNAL(currentRowChanged(int)), this, SLOT(updateDetails(int)));
136 void ThemesInstallWindow::updateDetails(int row)
138 if(row == currentItem) return;
140 currentItem = row;
141 qDebug() << "updateDetails(int) =" << row;
142 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
143 ui.themeDescription->setText(tr("fetching details for %1")
144 .arg(ui.listThemes->item(row)->data(Qt::DisplayRole).toString()));
145 ui.themePreview->clear();
146 ui.themePreview->setText(tr("fetching preview ..."));
148 int size = 0;
150 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
151 size += iniDetails.value("size").toInt();
152 qDebug() << ui.listThemes->item(row)->data(Qt::UserRole).toString() << size;
153 iniDetails.endGroup();
154 ui.labelSize->setText(tr("Download size %L1 kiB").arg(size));
156 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
158 QUrl img, txt;
159 txt = QUrl(QString(settings->themeUrl() + "/"
160 + iniDetails.value("descriptionfile").toString()));
161 img = QUrl(QString(settings->themeUrl() + "/"
162 + iniDetails.value("image").toString()));
163 qDebug() << "txt:" << txt;
164 qDebug() << "img:" << img;
166 QString text;
167 text = tr("<b>Author:</b> %1<hr/>").arg(iniDetails.value("author", tr("unknown")).toString());
168 text += tr("<b>Version:</b> %1<hr/>").arg(iniDetails.value("version", tr("unknown")).toString());
169 text += tr("<b>Description:</b> %1<hr/>").arg(iniDetails.value("about", tr("no description")).toString());
171 ui.themeDescription->setHtml(text);
172 iniDetails.endGroup();
174 igetter.abort();
175 if(!settings->cacheDisabled())
176 igetter.setCache(true);
177 else
179 if(infocachedir=="")
181 infocachedir = QDir::tempPath()+"rbutil-themeinfo";
182 QDir d = QDir::temp();
183 d.mkdir("rbutil-themeinfo");
185 igetter.setCache(infocachedir);
187 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
188 igetter.getFile(img);
192 void ThemesInstallWindow::updateImage(bool error)
194 qDebug() << "updateImage(bool) =" << error;
196 if(error) return;
198 QPixmap p;
199 if(!error) {
200 imgData = igetter.readAll();
201 if(imgData.isNull()) return;
202 p.loadFromData(imgData);
203 if(p.isNull()) {
204 ui.themePreview->clear();
205 ui.themePreview->setText(tr("no theme preview"));
207 else
208 ui.themePreview->setPixmap(p);
213 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
215 qDebug() << "resizeEvent(QResizeEvent*) =" << e;
217 QPixmap p, q;
218 QSize img;
219 img.setHeight(ui.themePreview->height());
220 img.setWidth(ui.themePreview->width());
222 p.loadFromData(imgData);
223 if(p.isNull()) return;
224 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
225 ui.themePreview->setScaledContents(false);
226 ui.themePreview->setPixmap(p);
231 void ThemesInstallWindow::show()
233 QDialog::show();
234 logger = new ProgressLoggerGui(this);
235 logger->show();
236 logger->addItem(tr("getting themes information ..."), LOGINFO);
238 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
240 downloadInfo();
245 void ThemesInstallWindow::abort()
247 igetter.abort();
248 logger->abort();
249 this->close();
253 void ThemesInstallWindow::accept()
255 if(ui.listThemes->selectedItems().size() == 0) {
256 this->close();
257 return;
259 QStringList themes;
260 QStringList names;
261 QStringList version;
262 QString zip;
263 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
264 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
265 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
266 zip = settings->themeUrl()
267 + "/" + iniDetails.value("archive").toString();
268 themes.append(zip);
269 names.append("Theme: " +
270 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
271 // if no version info is available use installation (current) date
272 version.append(iniDetails.value("version",
273 QDate().currentDate().toString("yyyyMMdd")).toString());
274 iniDetails.endGroup();
276 qDebug() << "installing themes:" << themes;
278 logger = new ProgressLoggerGui(this);
279 logger->show();
280 QString mountPoint = settings->mountpoint();
281 qDebug() << "mountpoint:" << mountPoint;
282 // show dialog with error if mount point is wrong
283 if(!QFileInfo(mountPoint).isDir()) {
284 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
285 logger->abort();
286 return;
289 installer = new ZipInstaller(this);
290 installer->setUrl(themes);
291 installer->setLogSection(names);
292 installer->setLogVersion(version);
293 installer->setMountPoint(mountPoint);
294 if(!settings->cacheDisabled())
295 installer->setCache(true);
297 connect(logger, SIGNAL(closed()), this, SLOT(close()));
298 installer->install(logger);