Fixed some little bugs in the theme loader, and cleaned the code.
[tagua/yd.git] / src / themeinfo.cpp
blob9719ad32085b93cc0bb1590b1f8d9b3693079990
1 /*
2 Copyright (c) 2006-2007 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006-2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program 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 */
11 #include "themeinfo.h"
13 #include "settings.h"
15 #include <QHash>
16 #include <QDir>
17 #include <QFileInfo>
18 #include <KDesktopFile>
20 ThemeInfo::operator bool() const {
21 return !file_name.isEmpty();
24 void ThemeInfo::save(Settings& s) {
25 s["file-name"] = file_name;
26 s["name"] = name;
27 s["description"] = description;
29 SettingArray s_variants = s.group("variants").newArray("variant");
30 foreach (QString v, variants) {
31 Settings s_var = s_variants.append();
32 s_var["name"] = v;
35 s["last-modified"] = last_modified.toString();
36 s["desktop-file"] = desktopFile;
39 ThemeInfo ThemeInfo::fromSettings(const Settings& s) {
40 ThemeInfo res;
42 s["file-name"] >> res.file_name;
43 s["name"] >> res.name;
44 s["description"] >> res.description;
45 s["desktop-file"] >> res.desktopFile;
46 res.last_modified = QDateTime::fromString(s["last-modified"].value<QString>());
48 SettingArray variants = s.group("variants").array("variant");
49 foreach (Settings var, variants) {
50 res.variants.append(var["name"].value<QString>());
53 return res;
56 ThemeInfo ThemeInfo::fromDesktopFile(const QString& desktop_file) {
57 ThemeInfo res;
59 QFileInfo desktop_file_info(desktop_file);
60 res.desktopFile = desktop_file;
61 if (!KDesktopFile::isDesktopFile(desktop_file)) {
62 return res;
65 KDesktopFile theme(desktop_file);
66 KConfigGroup themeData = theme.desktopGroup();
68 res.name = theme.readName();
69 res.description = theme.readComment();
70 res.description.replace("|", "\n");
71 res.variants = themeData.readEntry("X-Tagua-Variants").split(QRegExp("\\s*,\\s*"));
72 res.file_name = themeData.readEntry("X-Tagua-Script");
73 if (res.file_name.isEmpty()) {
74 // use a file with the same name as the .desktop, but with .lua extension
75 res.file_name = desktop_file;
76 res.file_name.replace(QRegExp("\\.desktop$"), ".lua");
78 else {
79 if (!QDir::isAbsolutePath(res.file_name)) {
80 // the file is relative to the .desktop directory
81 res.file_name = QDir(desktop_file_info.path()).filePath(res.file_name);
84 res.last_modified = desktop_file_info.lastModified();
86 return res;
89 uint qHash(const ThemeInfo& info) {
90 return qHash(info.file_name);
93 bool ThemeInfo::operator==(const ThemeInfo& info) const {
94 return desktopFile == info.desktopFile;