2 * This file is part of jSpeed.
4 * jSpeed is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * jSpeed is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QDir>
20 #include <QtCore/QString>
21 #include <QtCore/QDebug>
22 #include <QtGui/QApplication>
23 #include <QtGui/QDesktopWidget>
24 #include <QtGui/QStackedWidget>
25 #include <QtXml/QDomNode>
26 #include <QtXml/QDomDocument>
27 #include <QtXml/QDomAttr>
28 #include <QMaemo5InformationBox>
31 #include "zipreader.h"
32 #include "filereader.h"
33 #include "themescreen.h"
34 #include "detailscreen.h"
39 QString const RESOURCE_DIR = ":/resources/themes/";
40 QString const THEME_SUFFIX = ".jspeed";
43 Theme::Theme(DetailScreen* detailScreen, QWidget* parent): WidgetScreen(parent),
44 portraitId_(-1), landscapeId_(-1), reader_(0),
45 portrait_(0), landscape_(0), detailScreen_(detailScreen), portraitMode_(false)
57 removeWidget(landscape_);
65 removeWidget(portrait_);
71 QString theme = Settings::instance().value("theme", "default").toString();
73 QString themeDir = getThemeDir();
75 if(QFile::exists(themeDir + theme + THEME_SUFFIX))
77 reader_ = new ZipReader(themeDir + theme + THEME_SUFFIX);
85 QMaemo5InformationBox::information(this,
86 tr("Unable to load theme: %1.").arg(error_),
87 QMaemo5InformationBox::NoTimeout);
92 Settings::instance().setValue("theme", theme);
94 if(QFile::exists(RESOURCE_DIR + theme))
101 reader_ = new FileReader(RESOURCE_DIR + theme);
105 error_ = "No themes found";
112 Q_ASSERT(reader_ != 0);
116 error_ = reader_->errorString();
122 if(!reader_->readFile("theme.xml", xmlData))
124 error_ = "Unable to find <b>theme.xml</b> from theme file";
133 if(!doc.setContent(xmlData, false, &msg, &line, &column))
135 error_ = "Invalid xml file, " + msg + " (line "+line+", column "+column+")";
139 QDomNodeList detailConfigs = doc.elementsByTagName("detailscreen");
141 if(detailConfigs.size() > 1)
143 error_ = "Multiple <detailscreen> tags specified";
147 if(detailConfigs.size() == 1)
149 detailScreen_->removeElements();
151 QDomNode color = detailConfigs.at(0).attributes().namedItem("color");
155 detailScreen_->setColor(color.toAttr().value());
158 detailScreen_->load(detailConfigs.at(0), reader_);
161 QDomNodeList orientations = doc.elementsByTagName("orientation");
163 if(orientations.isEmpty())
165 error_ = "No <orientation> tags found";
171 for(int i = 0; i < orientations.size(); i++)
173 QDomNode data = orientations.at(i);
174 QString type = data.attributes().namedItem("name").toAttr().value();
176 if(type == "landscape")
180 error_ = "More than one <orientation name=\"landscape\"> specified";
184 landscape_ = new ThemeScreen();
185 ok = ok && landscape_->load(data, reader_);
188 else if(type == "portrait")
192 error_ = "More than one <orientation name=\"portrait\"> specified";
196 portrait_ = new ThemeScreen();
197 ok = ok && portrait_->load(data, reader_);
201 error_ = "Invalid orientation: " + type;
210 if(!portrait_ && !landscape_)
212 error_ = "No valid orientation tags found";
220 landscapeId_ = addWidget(landscape_);
221 connectSignals(landscape_);
226 portraitId_ = addWidget(portrait_);
227 connectSignals(portrait_);
230 if(landscape_ && portrait_)
232 QRect rect = QApplication::desktop()->screenGeometry();
234 if(rect.height() > rect.width())
236 setCurrentIndex(portraitId_);
237 portraitMode_ = true;
241 setCurrentIndex(landscapeId_);
242 portraitMode_ = false;
257 QString Theme::getThemeDir()
259 return Settings::getDir() + "themes" + QDir::separator();
262 QString const& Theme::getThemeSuffix()
267 QString const& Theme::error() const
272 bool Theme::portraitEnabled() const
274 return portrait_ != 0;
277 bool Theme::landscapeEnabled() const
279 return landscape_ != 0;
282 void Theme::reArrange()
284 QRect rect = QApplication::desktop()->screenGeometry();
286 bool portrait = rect.height() > rect.width();
288 if(portrait != portraitMode_)
290 if((portrait && portraitId_ == -1) ||
291 (!portrait && landscapeId_ == -1))
296 portraitMode_ = portrait;
300 setCurrentIndex(portraitId_);
301 portrait_->reArrange();
305 setCurrentIndex(landscapeId_);
306 landscape_->reArrange();
324 void Theme::connectSignals(ThemeScreen* screen)
326 connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
327 connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
328 connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
329 connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));