~kubuntu-members/libkdegames/4.11

« back to all changes in this revision

Viewing changes to libkdegamesprivate/kgametheme.cpp

  • Committer: Stefan Majewsky
  • Date: 2012-05-01 15:34:35 UTC
  • Revision ID: git-v1:82376fb5ca6f29f862641b6ca68603cb76258831
Begin to move stuff into libkdegamesprivate.

The build is now broken because I'm moving stuff without adjusting the
CMake files. But I figured it's cleaner to have the move in one commit
and the various edits in CMake and source files in the next commits.

svn path=/trunk/KDE/kdegames/libkdegames/; revision=1292461

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2007 Mauricio Piacentini   <mauricio@tabuleiro.com>
 
3
    Copyright (C) 2007 Matt Williams   <matt@milliams.com>
 
4
 
 
5
    This library is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 2 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "kgametheme.h"
 
21
 
 
22
#include <KStandardDirs>
 
23
#include <KConfig>
 
24
#include <KConfigGroup>
 
25
#include <KDebug>
 
26
#include <QtCore/QFile>
 
27
#include <QtCore/QFileInfo>
 
28
#include <QtCore/QMap>
 
29
#include <QtGui/QPixmap>
 
30
 
 
31
class KGameThemePrivate
 
32
{
 
33
    public:
 
34
        KGameThemePrivate() : loaded(false) {}
 
35
 
 
36
        QMap<QString, QString> themeproperties;
 
37
        QString fullPath; ///< Full path e.g. "/opt/kde/share/apps/appname/default.desktop"
 
38
        QString fileName; ///< just e.g. "default.desktop"
 
39
        QString graphics; ///< The full path of the svg file
 
40
        QPixmap preview;
 
41
        QString prefix; ///< Filepath of the .desktop file without the filename e.g. "/opt/kde/share/apps/appname/"
 
42
        QString themeGroup;
 
43
 
 
44
        bool loaded;
 
45
};
 
46
 
 
47
KGameTheme::KGameTheme(const QString &themeGroup)
 
48
    : d(new KGameThemePrivate)
 
49
{
 
50
    d->themeGroup = themeGroup;
 
51
    //KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
 
52
}
 
53
 
 
54
KGameTheme::~KGameTheme() {
 
55
    delete d;
 
56
}
 
57
 
 
58
bool KGameTheme::loadDefault()
 
59
{
 
60
    return load(QLatin1String( "themes/default.desktop" )); //TODO make this editable to match custom directories.
 
61
                                           // If this ever changes change findThemes in KGameThemeSelectorPrivate too
 
62
}
 
63
 
 
64
#define kThemeVersionFormat 1
 
65
 
 
66
bool KGameTheme::load(const QString &fileName) {
 
67
    if( fileName.isEmpty() )
 
68
    {
 
69
        kDebug(11000) << "Refusing to load theme with no name";
 
70
        return false;
 
71
    }
 
72
    QString filePath = KStandardDirs::locate("appdata", fileName);
 
73
    kDebug(11000) << "Attempting to load .desktop at" << filePath;
 
74
    if (filePath.isEmpty()) {
 
75
        return false;
 
76
    }
 
77
 
 
78
    // verify if it is a valid file first and if we can open it
 
79
    QFile themefile(filePath);
 
80
    if (!themefile.open(QIODevice::ReadOnly)) {
 
81
        kDebug(11000) << "Could not open .desktop theme file" << filePath;
 
82
        return false;
 
83
    }
 
84
    d->prefix = QFileInfo(themefile).absolutePath() + QLatin1Char( '/' );
 
85
    themefile.close();
 
86
 
 
87
    KConfig themeconfig(filePath, KConfig::SimpleConfig);
 
88
    if (!themeconfig.hasGroup(d->themeGroup))
 
89
    {
 
90
        kDebug(11000) << "Config group" << d->themeGroup << "does not exist in" << filePath;
 
91
        return false;
 
92
    }
 
93
    KConfigGroup group = themeconfig.group(d->themeGroup);
 
94
 
 
95
    //Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
 
96
    d->themeproperties = group.entryMap();
 
97
 
 
98
    //Version control
 
99
    int themeversion = group.readEntry("VersionFormat",0);
 
100
    //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
 
101
    if (themeversion > kThemeVersionFormat) {
 
102
        return false;
 
103
    }
 
104
 
 
105
    QString graphName = group.readEntry("FileName");
 
106
    //d->graphics = KStandardDirs::locate("appdata", graphName);
 
107
    d->graphics = d->prefix + graphName;
 
108
    if (d->graphics.isEmpty()) return false;
 
109
 
 
110
    // let's see if svg file exists and can be opened
 
111
    QFile svgFile( d->graphics );
 
112
    if ( !svgFile.open( QIODevice::ReadOnly ) ) {
 
113
        kDebug(11000) << "Could not open file" << d->graphics;
 
114
        return false;
 
115
    }
 
116
 
 
117
    QString previewName = group.readEntry("Preview");
 
118
    //QString graphicsPath = KStandardDirs::locate("appdata", previewName);
 
119
    QString graphicsPath = d->prefix + previewName;
 
120
    d->preview = QPixmap(graphicsPath);
 
121
 
 
122
    d->fileName = fileName;
 
123
    d->fullPath = filePath;
 
124
    d->loaded = true;
 
125
    return true;
 
126
}
 
127
 
 
128
QString KGameTheme::property(const QString &key) const
 
129
{
 
130
    if(!d->loaded)
 
131
    {
 
132
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
133
        return QString();
 
134
    }
 
135
    KConfig themeconfig(path(), KConfig::SimpleConfig);
 
136
    KConfigGroup group = themeconfig.group(d->themeGroup);
 
137
    return group.readEntry(key, QString());
 
138
}
 
139
 
 
140
QString KGameTheme::path() const {
 
141
    if(!d->loaded)
 
142
    {
 
143
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
144
        return QString();
 
145
    }
 
146
    return d->fullPath;
 
147
}
 
148
 
 
149
QString KGameTheme::fileName() const {
 
150
    if(!d->loaded)
 
151
    {
 
152
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
153
        return QString();
 
154
    }
 
155
    return d->fileName;
 
156
}
 
157
 
 
158
QString KGameTheme::graphics() const {
 
159
    if(!d->loaded)
 
160
    {
 
161
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
162
        return QString();
 
163
    }
 
164
    return d->graphics;
 
165
}
 
166
 
 
167
QPixmap KGameTheme::preview() const {
 
168
    if(!d->loaded)
 
169
    {
 
170
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
171
        return QPixmap();
 
172
    }
 
173
    return d->preview;
 
174
}
 
175
 
 
176
QString KGameTheme::themeProperty(const QString &key) const {
 
177
    if(!d->loaded)
 
178
    {
 
179
        kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
 
180
        return QString();
 
181
    }
 
182
    return d->themeproperties[key];
 
183
}