summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/htmlexport/theme.cpp
blob: c720de67eb318a0b45f53a6f2e197115f0834137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
A KIPI plugin to generate HTML image galleries
Copyright 2006 by Aurelien Gateau <aurelien dot gateau at free.fr>

This program 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 2
of the License, or (at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.

*/

// Self
#include "theme.h"

// TQt
#include <tqfile.h>
#include <tqtextstream.h>

// KDE
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kstandarddirs.h>
#include <kurl.h>

// Local
#include "colorthemeparameter.h"
#include "intthemeparameter.h"
#include "listthemeparameter.h"
#include "stringthemeparameter.h"

namespace KIPIHTMLExport {


static const char* AUTHOR_GROUP = "X-HTMLExport Author";
static const char* PARAMETER_GROUP_PREFIX = "X-HTMLExport Parameter ";
static const char* PARAMETER_TYPE_KEY = "Type";

static const char* STRING_PARAMETER_TYPE = "string";
static const char* LIST_PARAMETER_TYPE = "list";
static const char* COLOR_PARAMETER_TYPE = "color";
static const char* INT_PARAMETER_TYPE = "int";

static Theme::List sList;


struct Theme::Private {
	KDesktopFile* mDesktopFile;
	KURL mURL;
	ParameterList mParameterList;

	/**
	 * Return the list of parameters defined in the desktop file. We need to
	 * parse the file ourself to preserve parameter order.
	 */
	TQStringList readParameterNameList(const TQString& desktopFileName) {
		TQStringList list;
		TQFile file(desktopFileName);
		if (!file.open(IO_ReadOnly)) {
			return TQStringList();
		}

		TQTextStream stream(&file);
		stream.setEncoding(TQTextStream::UnicodeUTF8);
		TQString prefix = TQString("[") + PARAMETER_GROUP_PREFIX;
		while (!stream.atEnd()) {
			TQString line = stream.readLine();
			line = line.stripWhiteSpace();
			if (!line.startsWith(prefix)) {
				continue;
			}
			// Remove opening bracket and group prefix
			line = line.mid(prefix.length());

			// Remove closing bracket
			line.truncate(line.length() - 1);

			list.append(line);
		}

		return list;
	}

	void init(const TQString& desktopFileName) {
		mDesktopFile=new KDesktopFile(desktopFileName, true /*read only*/);
		mURL.setPath(desktopFileName);

		TQStringList parameterNameList = readParameterNameList(desktopFileName);
		readParameters(parameterNameList);
	}

	void readParameters(const TQStringList& list) {
		TQStringList::ConstIterator it=list.begin(), end=list.end();
		for (;it!=end; ++it) {
			TQString group = PARAMETER_GROUP_PREFIX + *it;
			TQCString internalName = (*it).utf8();

			TDEConfigGroupSaver saver(mDesktopFile, group);
			TQString type = mDesktopFile->readEntry(PARAMETER_TYPE_KEY);
			AbstractThemeParameter* parameter;
			if (type == STRING_PARAMETER_TYPE) {
				parameter = new StringThemeParameter();
			} else if (type == LIST_PARAMETER_TYPE) {
				parameter = new ListThemeParameter();
			} else if (type == COLOR_PARAMETER_TYPE) {
				parameter = new ColorThemeParameter();
			} else if (type == INT_PARAMETER_TYPE) {
				parameter = new IntThemeParameter();
			} else {
				kdWarning() << "Parameter '" << internalName << "' has unknown type '" << type << "'. Falling back to string type\n";
				parameter = new StringThemeParameter();
			}
			parameter->init(internalName, mDesktopFile);
			mParameterList << parameter;
		}
	}
};


Theme::Theme() {
	d=new Private;
}


Theme::~Theme() {
	delete d->mDesktopFile;
	delete d;
}


const Theme::List& Theme::getList() {
	if (sList.isEmpty()) {
		TQStringList internalNameList;
		TQStringList list=TDEGlobal::instance()->dirs()->findAllResources("data", "kipiplugin_htmlexport/themes/*/*.desktop");
		TQStringList::Iterator it=list.begin(), end=list.end();
		for (;it!=end; ++it) {
			Theme* theme=new Theme;
			theme->d->init(*it);
			TQString internalName = theme->internalName();
			if (!internalNameList.contains(internalName)) {
				sList << Theme::Ptr(theme);
				internalNameList << internalName;
			}
		}
	}
	return sList;
}


Theme::Ptr Theme::findByInternalName(const TQString& internalName) {
	const Theme::List& lst=getList();
	Theme::List::ConstIterator it=lst.begin(), end=lst.end();
	for (; it!=end; ++it) {
		Theme::Ptr theme = *it;
		if (theme->internalName() == internalName) {
			return theme;
		}
	}
	return 0;
}


TQString Theme::internalName() const {
	KURL url = d->mURL;
	url.setFileName("");
	return url.fileName();
}


TQString Theme::name() const {
	return d->mDesktopFile->readName();
}


TQString Theme::comment() const {
	return d->mDesktopFile->readComment();
}


TQString Theme::directory() const {
	return d->mURL.directory();
}


TQString Theme::authorName() const {
	TDEConfigGroupSaver saver(d->mDesktopFile, AUTHOR_GROUP);
	return d->mDesktopFile->readEntry("Name");
}


TQString Theme::authorUrl() const {
	TDEConfigGroupSaver saver(d->mDesktopFile, AUTHOR_GROUP);
	return d->mDesktopFile->readEntry("Url");
}


Theme::ParameterList Theme::parameterList() const {
	return d->mParameterList;
}


} // namespace