Check target / mountpoint selection before performing any installation.
[Rockbox.git] / rbutil / rbutilqt / installthemes.cpp
blobcf04280654e62326471613470e4728e6b5db9111
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()));
38 connect(ui.buttonOkAll, SIGNAL(clicked()), this, SLOT(acceptAll()));
41 ThemesInstallWindow::~ThemesInstallWindow()
43 if(infocachedir!="")
44 recRmdir(infocachedir);
47 QString ThemesInstallWindow::resolution()
49 QString resolution;
50 devices->beginGroup(userSettings->value("platform").toString());
51 resolution = devices->value("resolution").toString();
52 devices->endGroup();
53 return resolution;
57 void ThemesInstallWindow::setDeviceSettings(QSettings *dev)
59 devices = dev;
60 qDebug() << "setDeviceSettings()" << devices;
64 void ThemesInstallWindow::setUserSettings(QSettings *user)
66 userSettings = user;
70 void ThemesInstallWindow::downloadInfo()
72 // try to get the current build information
73 getter = new HttpGet(this);
74 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
76 qDebug() << "downloading themes info";
77 themesInfo.open();
78 qDebug() << "file:" << themesInfo.fileName();
79 themesInfo.close();
81 QUrl url;
82 url = QUrl(devices->value("themes_url").toString() + "/rbutilqt.php?res=" + resolution());
83 qDebug() << "downloadInfo()" << url;
84 qDebug() << url.queryItems();
85 getter->setProxy(proxy);
86 if(userSettings->value("offline").toBool())
87 getter->setCache(userSettings->value("cachepath", QDir::tempPath()).toString());
88 getter->setFile(&themesInfo);
89 getter->getFile(url);
93 void ThemesInstallWindow::downloadDone(int id, bool error)
95 downloadDone(error);
96 qDebug() << "downloadDone(bool) =" << id << error;
100 void ThemesInstallWindow::downloadDone(bool error)
102 qDebug() << "downloadDone(bool) =" << error;
104 disconnect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
105 disconnect(logger, SIGNAL(aborted()), this, SLOT(close()));
106 themesInfo.open();
108 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
109 QStringList tl = iniDetails.childGroups();
110 qDebug() << tl.size();
111 qDebug() << iniDetails.value("error/code").toString()
112 << iniDetails.value("error/description").toString()
113 << iniDetails.value("error/query").toString();
115 if(error) {
116 logger->addItem(tr("Network error: %1.\nPlease check your network and proxy settings.")
117 .arg(getter->errorString()), LOGERROR);
118 getter->abort();
119 logger->abort();
120 disconnect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
121 connect(logger, SIGNAL(closed()), this, SLOT(close()));
122 return;
124 // handle possible error codes
125 if(iniDetails.value("error/code").toInt() != 0 || !iniDetails.contains("error/code")) {
126 qDebug() << "error!";
127 logger->addItem(tr("the following error occured:\n%1")
128 .arg(iniDetails.value("error/description", "unknown error").toString()), LOGERROR);
129 logger->abort();
130 connect(logger, SIGNAL(closed()), this, SLOT(close()));
131 return;
133 logger->addItem(tr("done."), LOGOK);
134 logger->abort();
135 logger->close();
137 // setup list
138 for(int i = 0; i < tl.size(); i++) {
139 iniDetails.beginGroup(tl.at(i));
140 // skip all themes without name field set (i.e. error section)
141 if(iniDetails.value("name").toString().isEmpty()) continue;
142 QListWidgetItem *w = new QListWidgetItem;
143 w->setData(Qt::DisplayRole, iniDetails.value("name").toString());
144 w->setData(Qt::UserRole, tl.at(i));
145 ui.listThemes->addItem(w);
147 iniDetails.endGroup();
149 connect(ui.listThemes, SIGNAL(currentRowChanged(int)), this, SLOT(updateDetails(int)));
153 void ThemesInstallWindow::updateDetails(int row)
155 if(row == currentItem) return;
157 currentItem = row;
158 qDebug() << "updateDetails(int) =" << row;
159 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
160 ui.themeDescription->setText(tr("fetching details for %1")
161 .arg(ui.listThemes->item(row)->data(Qt::DisplayRole).toString()));
162 ui.themePreview->clear();
163 ui.themePreview->setText(tr("fetching preview ..."));
165 int size = 0;
167 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
168 size += iniDetails.value("size").toInt();
169 qDebug() << ui.listThemes->item(row)->data(Qt::UserRole).toString() << size;
170 iniDetails.endGroup();
171 ui.labelSize->setText(tr("Download size %1 kiB").arg(size));
173 iniDetails.beginGroup(ui.listThemes->item(row)->data(Qt::UserRole).toString());
175 QUrl img, txt;
176 txt = QUrl(QString(devices->value("themes_url").toString() + "/"
177 + iniDetails.value("descriptionfile").toString()));
178 img = QUrl(QString(devices->value("themes_url").toString() + "/"
179 + iniDetails.value("image").toString()));
180 qDebug() << "txt:" << txt;
181 qDebug() << "img:" << img;
183 QString text;
184 text = tr("<b>Author:</b> %1<hr/>").arg(iniDetails.value("author", tr("unknown")).toString());
185 text += tr("<b>Version:</b> %1<hr/>").arg(iniDetails.value("version", tr("unknown")).toString());
186 text += tr("<b>Description:</b> %1<hr/>").arg(iniDetails.value("about", tr("no description")).toString());
188 ui.themeDescription->setHtml(text);
189 iniDetails.endGroup();
191 igetter.abort();
192 igetter.setProxy(proxy);
193 if(!userSettings->value("cachedisable").toBool())
194 igetter.setCache(userSettings->value("cachepath", QDir::tempPath()).toString());
195 else
197 if(infocachedir=="")
199 infocachedir = QDir::tempPath()+"rbutil-themeinfo";
200 QDir d = QDir::temp();
201 d.mkdir("rbutil-themeinfo");
203 igetter.setCache(infocachedir);
205 igetter.getFile(img);
206 connect(&igetter, SIGNAL(done(bool)), this, SLOT(updateImage(bool)));
210 void ThemesInstallWindow::updateImage(bool error)
212 qDebug() << "updateImage(bool) =" << error;
213 if(error) return;
215 QPixmap p;
216 if(!error) {
217 imgData = igetter.readAll();
218 if(imgData.isNull()) return;
219 p.loadFromData(imgData);
220 if(p.isNull()) {
221 ui.themePreview->clear();
222 ui.themePreview->setText(tr("no theme preview"));
224 else
225 ui.themePreview->setPixmap(p);
230 void ThemesInstallWindow::resizeEvent(QResizeEvent* e)
232 qDebug() << "resizeEvent(QResizeEvent*) =" << e;
234 QPixmap p, q;
235 QSize img;
236 img.setHeight(ui.themePreview->height());
237 img.setWidth(ui.themePreview->width());
239 p.loadFromData(imgData);
240 if(p.isNull()) return;
241 q = p.scaled(img, Qt::KeepAspectRatio, Qt::SmoothTransformation);
242 ui.themePreview->setScaledContents(false);
243 ui.themePreview->setPixmap(p);
248 void ThemesInstallWindow::show()
250 downloadInfo();
251 QDialog::show();
252 logger = new ProgressLoggerGui(this);
253 logger->show();
254 logger->addItem(tr("getting themes information ..."), LOGINFO);
255 connect(logger, SIGNAL(aborted()), getter, SLOT(abort()));
256 connect(logger, SIGNAL(aborted()), this, SLOT(close()));
260 void ThemesInstallWindow::abort()
262 igetter.abort();
263 logger->abort();
264 this->close();
268 void ThemesInstallWindow::setProxy(QUrl p)
270 proxy = p;
271 qDebug() << "setProxy()" << proxy;
275 void ThemesInstallWindow::acceptAll()
277 ui.listThemes->selectAll();
278 accept();
281 void ThemesInstallWindow::accept()
283 if(ui.listThemes->selectedItems().size() == 0) {
284 this->close();
285 return;
287 QStringList themes;
288 QStringList names;
289 QStringList version;
290 QString zip;
291 QSettings iniDetails(themesInfo.fileName(), QSettings::IniFormat, this);
292 for(int i = 0; i < ui.listThemes->selectedItems().size(); i++) {
293 iniDetails.beginGroup(ui.listThemes->selectedItems().at(i)->data(Qt::UserRole).toString());
294 zip = devices->value("themes_url").toString()
295 + "/" + iniDetails.value("archive").toString();
296 themes.append(zip);
297 names.append("Theme: " +
298 ui.listThemes->selectedItems().at(i)->data(Qt::DisplayRole).toString());
299 // if no version info is available use installation (current) date
300 version.append(iniDetails.value("version",
301 QDate().currentDate().toString("yyyyMMdd")).toString());
302 iniDetails.endGroup();
304 qDebug() << "installing themes:" << themes;
306 logger = new ProgressLoggerGui(this);
307 logger->show();
308 QString mountPoint = userSettings->value("mountpoint").toString();
309 qDebug() << "mountpoint:" << userSettings->value("mountpoint").toString();
310 // show dialog with error if mount point is wrong
311 if(!QFileInfo(mountPoint).isDir()) {
312 logger->addItem(tr("Mount point is wrong!"),LOGERROR);
313 logger->abort();
314 return;
317 installer = new ZipInstaller(this);
318 installer->setUrl(themes);
319 installer->setProxy(proxy);
320 installer->setLogSection(names);
321 installer->setLogVersion(version);
322 installer->setMountPoint(mountPoint);
323 if(!userSettings->value("cachedisable").toBool())
324 installer->setCache(userSettings->value("cachepath", QDir::tempPath()).toString());
325 installer->install(logger);
326 connect(logger, SIGNAL(closed()), this, SLOT(close()));