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"
36 #include "themescheduler.h"
40 QString const RESOURCE_DIR = ":/resources/themes/";
41 QString const THEME_SUFFIX = ".jspeed";
44 ThemeLoader::ThemeLoader(WidgetScreen* mainScreen, WidgetScreen* detailScreen, QObject* parent): QObject(parent),
45 reader_(0), mainScreen_(mainScreen), detailScreen_(detailScreen)
49 ThemeLoader::~ThemeLoader()
53 bool ThemeLoader::load()
57 connect(&(ThemeScheduler::instance()), SIGNAL(themeChanged()), this, SLOT(load()));
60 QString theme = ThemeScheduler::instance().currentTheme();
69 QString themeDir = getThemeDir();
71 if(QFile::exists(themeDir + theme_ + THEME_SUFFIX))
73 reader_ = new ZipReader(themeDir + theme_ + THEME_SUFFIX);
81 QMaemo5InformationBox::information(mainScreen_,
82 tr("Unable to load theme: %1.").arg(error_),
83 QMaemo5InformationBox::NoTimeout);
87 theme_ = ThemeScheduler::instance().getDefaultTheme();
88 Settings::instance().setValue("theme", theme_);
90 if(QFile::exists(RESOURCE_DIR + theme_))
97 reader_ = new FileReader(RESOURCE_DIR + theme_);
101 error_ = "No themes found";
106 bool ThemeLoader::read()
108 Q_ASSERT(reader_ != 0);
112 error_ = reader_->errorString();
118 if(!reader_->readFile("theme.xml", xmlData))
120 error_ = "Unable to find <b>theme.xml</b> from theme file";
129 if(!doc.setContent(xmlData, false, &msg, &line, &column))
131 error_ = "Invalid xml file, " + msg + " (line " + QString::number(line)+", column " + QString::number(column) + ")";
135 detailScreen_->clear();
136 detailScreen_->addScreen(new DetailScreen, WidgetScreen::LANDSCAPE);
137 detailScreen_->addScreen(new DetailScreen, WidgetScreen::PORTRAIT);
138 loadScreen("detailscreen", doc, detailScreen_);
140 mainScreen_->clear();
141 mainScreen_->addScreen(new ThemeScreen, WidgetScreen::LANDSCAPE);
142 mainScreen_->addScreen(new ThemeScreen, WidgetScreen::PORTRAIT);
143 bool ret = loadScreen("mainscreen", doc, mainScreen_);
144 mainScreen_->removeUnloaded();
146 detailScreen_->reArrange();
147 mainScreen_->reArrange();
154 bool ThemeLoader::loadScreen(QString const& tag, QDomDocument const& doc, WidgetScreen* screen)
156 QDomNodeList orientations = doc.elementsByTagName(tag);
158 if(orientations.isEmpty())
160 error_ = "No <"+tag+"> tags found";
166 for(int i = 0; i < orientations.size(); i++)
168 QDomNode data = orientations.at(i);
169 QString type = data.attributes().namedItem("orientation").toAttr().value();
170 QDomNode color = data.attributes().namedItem("color");
172 if(type == "landscape")
174 if(screen->orientationLoaded(WidgetScreen::LANDSCAPE))
176 error_ = "More than one <"+tag+" orientation=\"landscape\"> specified";
180 ok = ok && screen->load(WidgetScreen::LANDSCAPE, data, reader_);
182 if(ok && !color.isNull())
184 screen->setColor(WidgetScreen::LANDSCAPE, color.toAttr().value());
188 else if(type == "portrait")
190 if(screen->orientationLoaded(WidgetScreen::PORTRAIT))
192 error_ = "More than one <"+tag+" orientation=\"portrait\"> specified";
196 ok = ok && screen->load(WidgetScreen::PORTRAIT, data, reader_);
198 if(ok && !color.isNull())
200 screen->setColor(WidgetScreen::PORTRAIT, color.toAttr().value());
203 else if(type == "both")
205 if(screen->orientationLoaded(WidgetScreen::PORTRAIT) ||
206 screen->orientationLoaded(WidgetScreen::LANDSCAPE))
208 error_ = "Multiple orientations specified for same screen";
212 ok = ok && screen->load(data, reader_);
214 if(ok && !color.isNull())
216 screen->setColor(color.toAttr().value());
221 error_ = "Invalid orientation: " + type;
229 QString ThemeLoader::getThemeDir()
231 return Settings::getDir() + "themes" + QDir::separator();
234 QString const& ThemeLoader::getThemeSuffix()
239 QString const& ThemeLoader::error() const