Oops, part 2: I shouldn't remove an icon that is still in use. Also convert some...
[Rockbox.git] / rbutil / rbutilqt / installthemes.cpp
blob33bee10b420eaa8c82f4cd4722b889e79957ba52
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 if(userSettings->value("defaults/offline").toBool())
81 getter->setCache(userSettings->value("defaults/cachepath", QDir::tempPath()).toString());
82 getter->setFile(&themesInfo);
83 getter->getFile(url);
87 void ThemesInstallWindow::downloadDone(int id, bool error)
89 downloadDone(error);
90 qDebug() << "downloadDone(bool) =" << id << error;
94 void ThemesInstallWindow::downloadDone(bool error)
96 qDebug() << "downloadDone(bool) =" << error;
98 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
99 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
100 themesInfo.open();
102 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
103 QStringList tl = iniDetails.childGroups();
104 qDebug() << tl.size();
105 qDebug() << iniDetails.value("error/code").toString()
106 << iniDetails.value("error/description").toString()
107 << iniDetails.value("error/query").toString();
109 if(error) {
110 logger->addItem(tr("Network error: %1.\nPlease check your network and proxy settings.")
111 .arg(getter->errorString()), LOGERROR);
112 getter->abort();
113 logger->abort();
114 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
115 connect(logger, SIGNAL(closed()), this, SLOT(close()));
116 return;
118 // handle possible error codes
119 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
120 qDebug() << "error!";
121 logger->addItem(tr("the following error occured:\n%1")
122 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
123 logger->abort();
124 connect(logger, SIGNAL(closed()), this, SLOT(close()));
125 return;
127 logger->addItem(tr("done."), LOGOK);
128 logger->abort();
129 logger->close();
131 // setup list
132 for(int i = 0; i < tl.size(); i++) {
133 iniDetails.beginGroup(tl.at(i));
134 // skip all themes without name field set (i.e. error section)
135 if(iniDetails.value("name").toString().isEmpty()) continue;
136 QListWidgetItem *w = new QListWidgetItem;
137 w->setData(Qt::DisplayRole, iniDetails.value("name").toString());
138 w->setData(Qt::UserRole, tl.at(i));
139 ui.listThemes->addItem(w);
141 iniDetails.endGroup();
143 connect(ui.listThemes, SIGNAL(currentRowChanged(int)), this, SLOT(updateDetails(int)));
147 void ThemesInstallWindow::updateDetails(int row)
149 if(row == currentItem) return;
151 currentItem = row;
152 qDebug() << "updateDetails(int) =" << row;
153 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
154 ui.themeDescription->setText(tr("fetching details for %1")
155 .arg(ui.listThemes->item(row)->data(Qt::DisplayRole).toString()));
156 ui.themePreview->clear();
157 ui.themePreview->setText(tr("fetching preview ..."));
159 int size = 0;
161 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
162 size += iniDetails.value("size").toInt();
163 qDebug() << ui.listThemes->item(row)->data(Qt::UserRole).toString() << size;
164 iniDetails.endGroup();
165 ui.labelSize->setText(tr("Download size %1 kiB").arg(size));
167 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
169 QUrl img, txt;
170 txt = QUrl(QString(devices->value("themes_url").toString() + "/"
171 + iniDetails.value("descriptionfile").toString()));
172 img = QUrl(QString(devices->value("themes_url").toString() + "/"
173 + iniDetails.value("image").toString()));
174 qDebug() << "txt:" << txt;
175 qDebug() << "img:" << img;
177 QString text;
178 text = tr("<b>Author:</b> %1<hr/>").arg(iniDetails.value("author", tr("unknown")).toString());
179 text += tr("<b>Version:</b> %1<hr/>").arg(iniDetails.value("version", tr("unknown")).toString());
180 text += tr("<b>Description:</b> %1<hr/>").arg(iniDetails.value("about", tr("no description")).toString());
182 ui.themeDescription->setHtml(text);
183 iniDetails.endGroup();
185 igetter.abort();
186 igetter.setProxy(proxy);
187 if(!userSettings->value("defaults/cachedisable").toBool())
188 igetter.setCache(userSettings->value("defaults/cachepath", QDir::tempPath()).toString());
189 igetter.getFile(img);
190 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
194 void ThemesInstallWindow::updateImage(bool error)
196 qDebug() << "updateImage(bool) =" << error;
197 if(error) return;
199 QPixmap p;
200 if(!error) {
201 imgData = igetter.readAll();
202 if(imgData.isNull()) return;
203 p.loadFromData(imgData);
204 if(p.isNull()) {
205 ui.themePreview->clear();
206 ui.themePreview->setText(tr("no theme preview"));
208 else
209 ui.themePreview->setPixmap(p);
214 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
216 qDebug() << "resizeEvent(QResizeEvent*) =" << e;
218 QPixmap p, q;
219 QSize img;
220 img.setHeight(ui.themePreview->height());
221 img.setWidth(ui.themePreview->width());
223 p.loadFromData(imgData);
224 if(p.isNull()) return;
225 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
226 ui.themePreview->setScaledContents(false);
227 ui.themePreview->setPixmap(p);
232 void ThemesInstallWindow::show()
234 downloadInfo();
235 QDialog::show();
236 logger = new ProgressLoggerGui(this);
237 logger->show();
238 logger->addItem(tr("getting themes information ..."), LOGINFO);
239 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
240 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
244 void ThemesInstallWindow::abort()
246 igetter.abort();
247 logger->abort();
248 this->close();
252 void ThemesInstallWindow::setProxy(QUrl p)
254 proxy = p;
255 qDebug() << "setProxy()" << proxy;
259 void ThemesInstallWindow::accept()
261 if(ui.listThemes->selectedItems().size() == 0) {
262 this->close();
263 return;
265 QStringList themes;
266 QStringList names;
267 QStringList version;
268 QString zip;
269 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
270 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
271 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
272 zip = devices->value("themes_url").toString()
273 + "/" + iniDetails.value("archive").toString();
274 themes.append(zip);
275 names.append("Theme: " +
276 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
277 // if no version info is available use installation (current) date
278 version.append(iniDetails.value("version",
279 QDate().currentDate().toString("yyyyMMdd")).toString());
280 iniDetails.endGroup();
282 qDebug() << "installing themes:" << themes;
284 logger = new ProgressLoggerGui(this);
285 logger->show();
286 QString mountPoint = userSettings->value("defaults/mountpoint").toString();
287 qDebug() << "mountpoint:" << userSettings->value("defaults/mountpoint").toString();
288 // show dialog with error if mount point is wrong
289 if(!QFileInfo(mountPoint).isDir()) {
290 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
291 logger->abort();
292 return;
295 installer = new ZipInstaller(this);
296 installer->setUrl(themes);
297 installer->setProxy(proxy);
298 installer->setLogSection(names);
299 installer->setLogVersion(version);
300 installer->setMountPoint(mountPoint);
301 if(!userSettings->value("defaults/cachedisable").toBool())
302 installer->setCache(userSettings->value("defaults/cachepath", QDir::tempPath()).toString());
303 installer->install(logger);
304 connect(logger, SIGNAL(closed()), this, SLOT(close()));