after resizing the theme installation window don't use the scaled image for the real...
[Rockbox.git] / rbutil / rbutilqt / installthemes.cpp
blobd083231ba41fbba29985e3bd7110421e19102c63
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"
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()));
41 QString ThemesInstallWindow::resolution()
43 QString resolution;
44 devices->beginGroup(userSettings->value("defaults/platform").toString());
45 resolution = devices->value("resolution").toString();
46 devices->endGroup();
47 return resolution;
51 void ThemesInstallWindow::setDeviceSettings(QSettings *dev)
53 devices = dev;
54 qDebug() << "setDeviceSettings()" << devices;
58 void ThemesInstallWindow::setUserSettings(QSettings *user)
60 userSettings = user;
64 void ThemesInstallWindow::downloadInfo()
66 // try to get the current build information
67 getter = new HttpGet(this);
68 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
70 qDebug() << "downloading themes info";
71 themesInfo.open();
72 qDebug() << "file:" << themesInfo.fileName();
73 themesInfo.close();
75 QUrl url;
76 url = QUrl(devices->value("themes_url").toString() + "/rbutilqt.php?res=" + resolution());
77 qDebug() << "downloadInfo()" << url;
78 qDebug() << url.queryItems();
79 getter->setProxy(proxy);
80 getter->setFile(&themesInfo);
81 getter->getFile(url);
85 void ThemesInstallWindow::downloadDone(int id, bool error)
87 downloadDone(error);
88 qDebug() << "downloadDone(bool) =" << id << error;
92 void ThemesInstallWindow::downloadDone(bool error)
94 qDebug() << "downloadDone(bool) =" << error;
96 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
97 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
98 themesInfo.open();
100 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
101 QStringList tl = iniDetails.childGroups();
102 qDebug() << tl.size();
103 qDebug() << iniDetails.value("error/code").toString()
104 << iniDetails.value("error/description").toString()
105 << iniDetails.value("error/query").toString();
107 if(error) {
108 logger->addItem(tr("Network error: %1.\nPlease check your network and proxy settings.")
109 .arg(getter->errorString()), LOGERROR);
110 getter->abort();
111 logger->abort();
112 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
113 connect(logger, SIGNAL(closed()), this, SLOT(close()));
114 return;
116 // handle possible error codes
117 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
118 qDebug() << "error!";
119 logger->addItem(tr("the following error occured:\n%1")
120 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
121 logger->abort();
122 connect(logger, SIGNAL(closed()), this, SLOT(close()));
123 return;
125 logger->addItem(tr("done."), LOGOK);
126 logger->abort();
127 logger->close();
129 // setup list
130 for(int i = 0; i < tl.size(); i++) {
131 iniDetails.beginGroup(tl.at(i));
132 // skip all themes without name field set (i.e. error section)
133 if(iniDetails.value("name").toString().isEmpty()) continue;
134 QListWidgetItem *w = new QListWidgetItem;
135 w->setData(Qt::DisplayRole, iniDetails.value("name").toString());
136 w->setData(Qt::UserRole, tl.at(i));
137 ui.listThemes->addItem(w);
139 iniDetails.endGroup();
141 connect(ui.listThemes, SIGNAL(currentRowChanged(int)), this, SLOT(updateDetails(int)));
145 void ThemesInstallWindow::updateDetails(int row)
147 if(row == currentItem) return;
149 currentItem = row;
150 qDebug() << "updateDetails(int) =" << row;
151 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
152 ui.themeDescription->setText(tr("fetching details for %1")
153 .arg(ui.listThemes->item(row)->data(Qt::DisplayRole).toString()));
154 ui.themePreview->clear();
155 ui.themePreview->setText(tr("fetching preview ..."));
157 int size = 0;
159 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
160 size += iniDetails.value("size").toInt();
161 qDebug() << ui.listThemes->item(row)->data(Qt::UserRole).toString() << size;
162 iniDetails.endGroup();
163 ui.labelSize->setText(tr("Download size %1 kiB").arg(size));
165 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
167 QUrl img, txt;
168 txt = QUrl(QString(devices->value("themes_url").toString() + "/"
169 + iniDetails.value("descriptionfile").toString()));
170 img = QUrl(QString(devices->value("themes_url").toString() + "/"
171 + iniDetails.value("image").toString()));
172 qDebug() << "txt:" << txt;
173 qDebug() << "img:" << img;
175 QString text;
176 text = tr("<b>Author:</b> %1<hr/>").arg(iniDetails.value("author", tr("unknown")).toString());
177 text += tr("<b>Version:</b> %1<hr/>").arg(iniDetails.value("version", tr("unknown")).toString());
178 text += tr("<b>Description:</b> %1<br/>").arg(iniDetails.value("about", tr("no description")).toString());
179 ui.themeDescription->setHtml(text);
180 iniDetails.endGroup();
182 igetter.abort();
183 igetter.setProxy(proxy);
184 igetter.getFile(img);
185 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
189 void ThemesInstallWindow::updateImage(bool error)
191 qDebug() << "updateImage(bool) =" << error;
192 if(error) return;
194 QPixmap p;
195 if(!error) {
196 imgData = igetter.readAll();
197 if(imgData.isNull()) return;
198 p.loadFromData(imgData);
199 if(p.isNull()) {
200 ui.themePreview->clear();
201 ui.themePreview->setText(tr("no theme preview"));
203 else
204 ui.themePreview->setPixmap(p);
209 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
211 qDebug() << "resizeEvent(QResizeEvent*) =" << e;
213 QPixmap p, q;
214 QSize img;
215 img.setHeight(ui.themePreview->height());
216 img.setWidth(ui.themePreview->width());
218 p.loadFromData(imgData);
219 if(p.isNull()) return;
220 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
221 ui.themePreview->setScaledContents(false);
222 ui.themePreview->setPixmap(p);
227 void ThemesInstallWindow::show()
229 downloadInfo();
230 QDialog::show();
231 logger = new ProgressLoggerGui(this);
232 logger->show();
233 logger->addItem(tr("getting themes information ..."), LOGINFO);
234 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
235 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
239 void ThemesInstallWindow::abort()
241 igetter.abort();
242 logger->abort();
243 this->close();
247 void ThemesInstallWindow::setProxy(QUrl p)
249 proxy = p;
250 qDebug() << "setProxy()" << proxy;
254 void ThemesInstallWindow::accept()
256 if(ui.listThemes->selectedItems().size() == 0) {
257 this->close();
258 return;
260 QStringList themes;
261 QStringList names;
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 = devices->value("themes_url").toString() + "/" + iniDetails.value("archive").toString();
267 themes.append(zip);
268 names.append("Theme: " + ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
269 iniDetails.endGroup();
271 qDebug() << "installing themes:" << themes;
273 logger = new ProgressLoggerGui(this);
274 logger->show();
275 QString mountPoint = userSettings->value("defaults/mountpoint").toString();
276 qDebug() << "mountpoint:" << userSettings->value("defaults/mountpoint").toString();
277 // show dialog with error if mount point is wrong
278 if(!QFileInfo(mountPoint).isDir()) {
279 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
280 logger->abort();
281 return;
284 installer = new ZipInstaller(this);
285 installer->setUrl(themes);
286 installer->setProxy(proxy);
287 installer->setLogSection(names);
288 installer->setMountPoint(mountPoint);
289 installer->install(logger);
290 connect(logger, SIGNAL(closed()), this, SLOT(close()));