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"
38 QString const RESOURCE_DIR = ":/resources/themes/";
39 QString const THEME_SUFFIX = ".jspeed";
42 Theme::Theme(QWidget* parent): WidgetScreen(parent),
43 portraitId_(-1), landscapeId_(-1), reader_(0),
44 portrait_(0), landscape_(0), portraitMode_(false)
56 removeWidget(landscape_);
64 removeWidget(portrait_);
70 QString theme = Settings::instance().value("theme", "default").toString();
72 QString themeDir = getThemeDir();
74 if(QFile::exists(themeDir + theme + THEME_SUFFIX))
76 reader_ = new ZipReader(themeDir + theme + THEME_SUFFIX);
80 qDebug() << "Ladattiin " << theme;
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 orientations = doc.elementsByTagName("orientation");
141 if(orientations.isEmpty())
143 error_ = "No <orientation> tags found";
149 for(int i = 0; i < orientations.size(); i++)
151 QDomNode data = orientations.at(i);
152 QString type = data.attributes().namedItem("name").toAttr().value();
154 if(type == "landscape")
158 error_ = "More than one <orientation name=\"landscape\"> specified";
162 landscape_ = new ThemeScreen();
163 ok = ok && landscape_->load(data, reader_);
166 else if(type == "portrait")
170 error_ = "More than one <orientation name=\"portrait\"> specified";
174 portrait_ = new ThemeScreen();
175 ok = ok && portrait_->load(data, reader_);
179 error_ = "Invalid orientation: " + type;
188 if(!portrait_ && !landscape_)
190 error_ = "No valid orientation tags found";
198 landscapeId_ = addWidget(landscape_);
199 connectSignals(landscape_);
204 portraitId_ = addWidget(portrait_);
205 connectSignals(portrait_);
219 QString Theme::getThemeDir()
221 return Settings::getDir() + "themes" + QDir::separator();
224 QString const& Theme::getThemeSuffix()
229 QString const& Theme::error() const
234 bool Theme::portraitEnabled() const
236 return portraitId_ != -1;
239 bool Theme::landscapeEnabled() const
241 return landscapeId_ != -1;
244 void Theme::reArrange()
246 QRect rect = QApplication::desktop()->screenGeometry();
248 bool portrait = rect.height() > rect.width();
250 if(portrait != portraitMode_)
252 if((portrait && portraitId_ == -1) ||
253 (!portrait && landscapeId_ == -1))
258 portraitMode_ = portrait;
262 setCurrentIndex(portraitId_);
263 portrait_->reArrange();
267 setCurrentIndex(landscapeId_);
268 landscape_->reArrange();
286 void Theme::connectSignals(ThemeScreen* screen)
288 connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
289 connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
290 connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
291 connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));