libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / fullscreentheme.cpp
blob93debfed3f42dd89287f30844104293eec6ee2cd
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
21 // Self
22 #include "fullscreentheme.h"
24 // Qt
25 #include <QDir>
26 #include <QFile>
27 #include <QString>
29 // KDE
30 #include <kcomponentdata.h>
31 #include <kmacroexpander.h>
32 #include <kstandarddirs.h>
33 #include <kstringhandler.h>
35 // Local
36 #include <lib/gwenviewconfig.h>
38 namespace Gwenview {
41 static const char* THEME_BASE_DIR = "fullscreenthemes/";
44 struct FullScreenThemePrivate {
45 QString mThemeDir;
46 QString mStyleSheet;
50 FullScreenTheme::FullScreenTheme(const QString& themeName)
51 : d(new FullScreenThemePrivate) {
52 // Get theme dir
53 d->mThemeDir = KStandardDirs::locate("appdata", THEME_BASE_DIR + themeName + '/');
54 if (d->mThemeDir.isEmpty()) {
55 kWarning() << "Couldn't find fullscreen theme" << themeName;
56 return;
59 // Read css file
60 QString styleSheetPath = d->mThemeDir + "/style.css";
61 QFile file(styleSheetPath);
62 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
63 kWarning() << "Couldn't open" << styleSheetPath;
64 return;
66 QString styleSheet = QString::fromUtf8(file.readAll());
68 d->mStyleSheet = replaceThemeVars(styleSheet);
72 FullScreenTheme::~FullScreenTheme() {
73 delete d;
77 QString FullScreenTheme::styleSheet() const {
78 return d->mStyleSheet;
82 QString FullScreenTheme::replaceThemeVars(const QString& styleSheet) {
83 QHash<QString, QString> macros;
84 macros["themeDir"] = d->mThemeDir;
85 return KMacroExpander::expandMacros(styleSheet, macros, QLatin1Char('$'));
89 QString FullScreenTheme::currentThemeName() {
90 return GwenviewConfig::fullScreenTheme();
94 void FullScreenTheme::setCurrentThemeName(const QString& name) {
95 GwenviewConfig::setFullScreenTheme(name);
99 static bool themeNameLessThan(const QString& s1, const QString& s2) {
100 return KStringHandler::naturalCompare(s1.toLower(), s2.toLower()) < 0;
104 QStringList FullScreenTheme::themeNameList() {
105 QStringList list;
106 QStringList themeBaseDirs =
107 KGlobal::mainComponent().dirs()
108 ->findDirs("appdata", THEME_BASE_DIR);
109 Q_FOREACH(const QString& themeBaseDir, themeBaseDirs) {
110 QDir dir(themeBaseDir);
111 list += dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
113 qSort(list.begin(), list.end(), themeNameLessThan);
115 return list;
119 } // namespace