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 <QtCore/QTimer>
23 #include <QtGui/QStackedWidget>
24 #include <QtXml/QDomNode>
25 #include <QtXml/QDomDocument>
26 #include <QtXml/QDomAttr>
27 #include <QMaemo5InformationBox>
28 #include "themeloader.h"
30 #include "zipreader.h"
31 #include "filereader.h"
32 #include "themescreen.h"
33 #include "detailscreen.h"
35 #include "widgetscreen.h"
39 QString const RESOURCE_DIR = ":/resources/themes/";
40 QString const THEME_SUFFIX = ".jspeed";
43 ThemeLoader::ThemeLoader(WidgetScreen* mainScreen, WidgetScreen* detailScreen, QObject* parent): QObject(parent),
44 reader_(0), mainScreen_(mainScreen), detailScreen_(detailScreen)
48 ThemeLoader::~ThemeLoader()
52 bool ThemeLoader::load()
54 QString theme = Settings::instance().value("theme", "default").toString();
56 QString themeDir = getThemeDir();
58 if(QFile::exists(themeDir + theme + THEME_SUFFIX))
60 reader_ = new ZipReader(themeDir + theme + THEME_SUFFIX);
68 QMaemo5InformationBox::information(mainScreen_,
69 tr("Unable to load theme: %1.").arg(error_),
70 QMaemo5InformationBox::NoTimeout);
75 Settings::instance().setValue("theme", theme);
77 if(QFile::exists(RESOURCE_DIR + theme))
84 reader_ = new FileReader(RESOURCE_DIR + theme);
88 error_ = "No themes found";
93 bool ThemeLoader::read()
95 Q_ASSERT(reader_ != 0);
99 error_ = reader_->errorString();
105 if(!reader_->readFile("theme.xml", xmlData))
107 error_ = "Unable to find <b>theme.xml</b> from theme file";
116 if(!doc.setContent(xmlData, false, &msg, &line, &column))
118 error_ = "Invalid xml file, " + msg + " (line " + QString::number(line)+", column " + QString::number(column) + ")";
122 detailScreen_->clear();
123 detailScreen_->addScreen(new DetailScreen, WidgetScreen::LANDSCAPE);
124 detailScreen_->addScreen(new DetailScreen, WidgetScreen::PORTRAIT);
125 loadScreen("detailscreen", doc, detailScreen_);
127 mainScreen_->clear();
128 mainScreen_->addScreen(new ThemeScreen, WidgetScreen::LANDSCAPE);
129 mainScreen_->addScreen(new ThemeScreen, WidgetScreen::PORTRAIT);
130 bool ret = loadScreen("mainscreen", doc, mainScreen_);
131 mainScreen_->removeUnloaded();
138 bool ThemeLoader::loadScreen(QString const& tag, QDomDocument const& doc, WidgetScreen* screen)
140 QDomNodeList orientations = doc.elementsByTagName(tag);
142 if(orientations.isEmpty())
144 error_ = "No <"+tag+"> tags found";
150 for(int i = 0; i < orientations.size(); i++)
152 QDomNode data = orientations.at(i);
153 QString type = data.attributes().namedItem("orientation").toAttr().value();
154 QDomNode color = data.attributes().namedItem("color");
156 if(type == "landscape")
158 if(screen->orientationLoaded(WidgetScreen::LANDSCAPE))
160 error_ = "More than one <"+tag+" orientation=\"landscape\"> specified";
164 ok = ok && screen->load(WidgetScreen::LANDSCAPE, data, reader_);
166 if(ok && !color.isNull())
168 screen->setColor(WidgetScreen::LANDSCAPE, color.toAttr().value());
172 else if(type == "portrait")
174 if(screen->orientationLoaded(WidgetScreen::PORTRAIT))
176 error_ = "More than one <"+tag+" orientation=\"portrait\"> specified";
180 ok = ok && screen->load(WidgetScreen::PORTRAIT, data, reader_);
182 if(ok && !color.isNull())
184 screen->setColor(WidgetScreen::PORTRAIT, color.toAttr().value());
187 else if(type == "both")
189 if(screen->orientationLoaded(WidgetScreen::PORTRAIT) ||
190 screen->orientationLoaded(WidgetScreen::LANDSCAPE))
192 error_ = "Multiple orientations specified for same screen";
196 ok = ok && screen->load(data, reader_);
198 if(ok && !color.isNull())
200 screen->setColor(color.toAttr().value());
205 error_ = "Invalid orientation: " + type;
213 QString ThemeLoader::getThemeDir()
215 return Settings::getDir() + "themes" + QDir::separator();
218 QString const& ThemeLoader::getThemeSuffix()
223 QString const& ThemeLoader::error() const