-/*
- * This file is part of jSpeed.
- *
- * jSpeed is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * jSpeed is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <QtCore/QDir>
-#include <QtCore/QString>
-#include <QtCore/QDebug>
-#include <QtCore/QTimer>
-#include <QtGui/QApplication>
-#include <QtGui/QDesktopWidget>
-#include <QtGui/QStackedWidget>
-#include <QtXml/QDomNode>
-#include <QtXml/QDomDocument>
-#include <QtXml/QDomAttr>
-#include <QMaemo5InformationBox>
-#include "theme.h"
-#include "reader.h"
-#include "zipreader.h"
-#include "filereader.h"
-#include "themescreen.h"
-#include "detailscreen.h"
-#include "settings.h"
-
-namespace
-{
- QString const RESOURCE_DIR = ":/resources/themes/";
- QString const THEME_SUFFIX = ".jspeed";
-}
-
-Theme::Theme(DetailScreen* detailScreen, QWidget* parent): WidgetScreen(parent),
-portraitId_(-1), landscapeId_(-1), reader_(0),
-portrait_(0), landscape_(0), detailScreen_(detailScreen), portraitMode_(false)
-{
-}
-
-Theme::~Theme()
-{
-}
-
-bool Theme::load()
-{
- if(landscape_)
- {
- removeWidget(landscape_);
- delete landscape_;
- landscape_ = 0;
- landscapeId_ = -1;
- }
-
- if(portrait_)
- {
- removeWidget(portrait_);
- delete portrait_;
- portrait_ = 0;
- portraitId_ = -1;
- }
-
- QString theme = Settings::instance().value("theme", "default").toString();
-
- QString themeDir = getThemeDir();
-
- if(QFile::exists(themeDir + theme + THEME_SUFFIX))
- {
- reader_ = new ZipReader(themeDir + theme + THEME_SUFFIX);
-
- if(read())
- {
- return true;
- }
- else
- {
- QMaemo5InformationBox::information(this,
- tr("Unable to load theme: %1.").arg(error_),
- QMaemo5InformationBox::NoTimeout);
- }
- }
-
- theme = "default";
- Settings::instance().setValue("theme", theme);
-
- if(QFile::exists(RESOURCE_DIR + theme))
- {
- if(reader_)
- {
- delete reader_;
- }
-
- reader_ = new FileReader(RESOURCE_DIR + theme);
- return read();
- }
-
- error_ = "No themes found";
-
- return false;
-}
-
-bool Theme::read()
-{
- Q_ASSERT(reader_ != 0);
-
- if(!reader_->open())
- {
- error_ = reader_->errorString();
- return false;
- }
-
- QByteArray xmlData;
-
- if(!reader_->readFile("theme.xml", xmlData))
- {
- error_ = "Unable to find <b>theme.xml</b> from theme file";
- return false;
- }
-
- QDomDocument doc;
- int line = 0;
- int column = 0;
- QString msg;
-
- if(!doc.setContent(xmlData, false, &msg, &line, &column))
- {
- error_ = "Invalid xml file, " + msg + " (line "+line+", column "+column+")";
- return false;
- }
-
- QDomNodeList detailConfigs = doc.elementsByTagName("detailscreen");
-
- if(detailConfigs.size() > 1)
- {
- error_ = "Multiple <detailscreen> tags specified";
- return false;
- }
-
- if(detailConfigs.size() == 1)
- {
- detailScreen_->removeElements();
-
- QDomNode color = detailConfigs.at(0).attributes().namedItem("color");
-
- if(!color.isNull())
- {
- detailScreen_->setColor(color.toAttr().value());
- }
-
- detailScreen_->load(detailConfigs.at(0), reader_);
- }
-
- QDomNodeList orientations = doc.elementsByTagName("orientation");
-
- if(orientations.isEmpty())
- {
- error_ = "No <orientation> tags found";
- return false;
- }
-
- bool ok = true;
-
- for(int i = 0; i < orientations.size(); i++)
- {
- QDomNode data = orientations.at(i);
- QString type = data.attributes().namedItem("name").toAttr().value();
-
- if(type == "landscape")
- {
- if(landscape_)
- {
- error_ = "More than one <orientation name=\"landscape\"> specified";
- ok = false;
- }
-
- landscape_ = new ThemeScreen();
- ok = ok && landscape_->load(data, reader_);
-
- }
- else if(type == "portrait")
- {
- if(portrait_)
- {
- error_ = "More than one <orientation name=\"portrait\"> specified";
- ok = false;
- }
-
- portrait_ = new ThemeScreen();
- ok = ok && portrait_->load(data, reader_);
- }
- else
- {
- error_ = "Invalid orientation: " + type;
- ok = false;
- }
- }
-
- reader_->close();
- delete reader_;
- reader_ = 0;
-
- if(!portrait_ && !landscape_)
- {
- error_ = "No valid orientation tags found";
- ok = false;
- }
-
- if(ok)
- {
- if(landscape_)
- {
- landscapeId_ = addWidget(landscape_);
- connectSignals(landscape_);
- }
-
- if(portrait_)
- {
- portraitId_ = addWidget(portrait_);
- connectSignals(portrait_);
- }
-
- if(landscape_ && portrait_)
- {
- QRect rect = QApplication::desktop()->screenGeometry();
-
- if(rect.height() > rect.width())
- {
- setCurrentIndex(portraitId_);
- portraitMode_ = true;
- }
- else
- {
- setCurrentIndex(landscapeId_);
- portraitMode_ = false;
- }
- }
- }
- else
- {
- delete portrait_;
- portrait_ = 0;
- delete landscape_;
- landscape_ = 0;
- }
-
- return ok;
-}
-
-QString Theme::getThemeDir()
-{
- return Settings::getDir() + "themes" + QDir::separator();
-}
-
-QString const& Theme::getThemeSuffix()
-{
- return THEME_SUFFIX;
-}
-
-QString const& Theme::error() const
-{
- return error_;
-}
-
-bool Theme::portraitEnabled() const
-{
- return portrait_ != 0;
-}
-
-bool Theme::landscapeEnabled() const
-{
- return landscape_ != 0;
-}
-
-void Theme::reArrange()
-{
- QRect rect = QApplication::desktop()->screenGeometry();
-
- bool portrait = rect.height() > rect.width();
-
- if(portrait != portraitMode_)
- {
- if((portrait && portraitId_ == -1) ||
- (!portrait && landscapeId_ == -1))
- {
- return;
- }
-
- portraitMode_ = portrait;
-
- if(portrait)
- {
- setCurrentIndex(portraitId_);
- portrait_->reArrange();
- }
- else
- {
- setCurrentIndex(landscapeId_);
- landscape_->forceRepaint();
- //QTimer::singleShot(5000, landscape_, SLOT(forceRepaint()));
- }
- }
-}
-
-void Theme::flip()
-{
- if(portrait_)
- {
- portrait_->flip();
- }
-
- if(landscape_)
- {
- landscape_->flip();
- }
-}
-
-void Theme::connectSignals(ThemeScreen* screen)
-{
- connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
- connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
- connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
- connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));
-}
-