Krita Source Code Documentation
Loading...
Searching...
No Matches
KisTemplateTree.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2000 Werner Trobin <trobin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "KisTemplateTree.h"
8
9#include <QDir>
10#include <QPrinter>
11#include <QUrl>
12
13#include <kdesktopfile.h>
14#include <kconfig.h>
15#include <kis_debug.h>
16
17
18#include <KoResourcePaths.h>
19#include <klocalizedstring.h>
20#include <kconfiggroup.h>
21
22#include <KisTemplate.h>
23#include <KisTemplateGroup.h>
24#include <KisTemplates.h>
25
26static QString currentLocale()
27{
28 const QStringList languages = KLocalizedString::languages();
29 if (languages.isEmpty()) {
30 return QLocale().name();
31 } else {
32 return languages.first();
33 }
34}
35
36KisTemplateTree::KisTemplateTree(const QString &templatesResourcePath,
37 bool readTree)
38 : m_templatesResourcePath(templatesResourcePath)
39 , m_defaultGroup(0)
40 , m_defaultTemplate(0)
41{
42 if (readTree)
44}
45
50
56
58{
59 QString localDir = KoResourcePaths::saveLocation("templates");
60
61 Q_FOREACH (KisTemplateGroup *group, m_groups) {
62 //dbgUI <<"---------------------------------";
63 //dbgUI <<"group:" << group->name();
64
65 bool touched = false;
66 QList<KisTemplate*> templates = group->templates();
67 QList<KisTemplate*>::iterator it = templates.begin();
68 for (; it != templates.end() && !touched && !group->touched(); ++it)
69 touched = (*it)->touched();
70
71 if (group->touched() || touched) {
72 //dbgUI <<"touched";
73 if (!group->isHidden()) {
74 //dbgUI <<"not hidden";
75 QDir path;
76 path.mkpath(localDir + group->name()); // create the local group dir
77 } else {
78 //dbgUI <<"hidden";
79 if (group->dirs().count() == 1 && group->dirs().contains(localDir)) {
80 //dbgUI <<"local only";
81 QFile f(group->dirs().first());
82 f.remove();
83 //dbgUI <<"removing:" << group->dirs().first();
84 } else {
85 //dbgUI <<"global";
86 QDir path;
87 path.mkpath(localDir + group->name());
88 }
89 }
90 }
91 Q_FOREACH (KisTemplate *t, templates) {
92 if (t->touched()) {
93 //dbgUI <<"++template:" << t->name();
94 writeTemplate(t, group, localDir);
95 }
96 if (t->isHidden() && t->touched()) {
97 //dbgUI <<"+++ delete local template ##############";
98 writeTemplate(t, group, localDir);
99 QFile::remove(t->file());
100 QFile::remove(t->picture());
101 }
102 }
103 }
104}
105
107{
108
109 KisTemplateGroup *group = find(g->name());
110 if (group == 0) {
111 m_groups.append(g);
112 return true;
113 }
114
115 group->addDir(g->dirs().first()); // "...there can be only one..." (Queen)
116 delete g;
117 g = 0;
118 return false;
119}
120
121KisTemplateGroup *KisTemplateTree::find(const QString &name) const
122{
124 KisTemplateGroup* ret = 0;
125
126 while (it != m_groups.end()) {
127 if ((*it)->name() == name) {
128 ret = *it;
129 break;
130 }
131
132 ++it;
133 }
134
135 return ret;
136}
137
139{
140
141 QStringList dirs = KoResourcePaths::findDirs("templates");
142
143 Q_FOREACH (const QString & dirName, dirs) {
144 if (!dirName.contains("templates")) continue; // Hack around broken KoResourcePaths
145 QDir dir(dirName);
146 // avoid the annoying warning
147 if (!dir.exists())
148 continue;
149 QStringList templateDirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
150
151
152 Q_FOREACH (const QString & templateDirName, templateDirs) {
153 QDir templateDir(dirName + "/" + templateDirName);
154 QString name = templateDirName;
155 QString defaultTab;
156 int sortingWeight = 1000;
157 if (templateDir.exists(".directory")) {
158 KDesktopFile config(templateDir.absoluteFilePath(".directory"));
159 config.setLocale(currentLocale());
160 KConfigGroup dg = config.desktopGroup();
161 name = dg.readEntry("Name");
162 defaultTab = dg.readEntry("X-KDE-DefaultTab");
163 sortingWeight = dg.readEntry("X-KDE-SortingWeight", 1000);
164 }
165 KisTemplateGroup *g = new KisTemplateGroup(name, templateDir.absolutePath() + '/', sortingWeight);
166 if (add(g)) {
167 if (defaultTab == "true") {
168 m_defaultGroup = g;
169 }
170 }
171 }
172 }
173}
174
176{
177 QString dontShow = "imperial";
178 if ( QLocale().measurementSystem() == QLocale::ImperialSystem) {
179 dontShow = "metric";
180 }
181
182 Q_FOREACH (KisTemplateGroup* group, m_groups) {
183 QStringList dirs = group->dirs();
184 for (QStringList::ConstIterator it = dirs.constBegin(); it != dirs.constEnd(); ++it) {
185 QDir d(*it);
186 if (!d.exists())
187 continue;
188 QStringList files = d.entryList(QDir::Files | QDir::Readable, QDir::Name);
189 for (int i = 0; i < files.count(); ++i) {
190 QString filePath = *it + files[i];
191 //dbgUI <<"filePath:" << filePath;
192 QString icon;
193 QString text;
194 QString description;
195 QString hidden_str;
196 QString fileName;
197 bool hidden = false;
198 bool defaultTemplate = false;
199 QString templatePath;
200 QString measureSystem;
201 // If a desktop file, then read the name from it.
202 // Otherwise (or if no name in it?) use file name
203 if (KDesktopFile::isDesktopFile(filePath)) {
204 KConfig _config(filePath, KConfig::SimpleConfig);
205 _config.setLocale(currentLocale());
206 KConfigGroup config(&_config, "Desktop Entry");
207 if (config.readEntry("Type") == "Link") {
208 text = config.readEntry("Name");
209 fileName = filePath;
210 description = config.readEntry("Comment");
211 //dbgUI <<"name:" << text;
212 icon = config.readEntry("Icon");
213 if (icon[0] != '/' && // allow absolute paths for icons
214 QFile::exists(*it + icon)) // allow icons from icontheme
215 icon = *it + icon;
216 //dbgUI <<"icon2:" << icon;
217 hidden = config.readEntry("X-KDE-Hidden", false);
218 defaultTemplate = config.readEntry("X-KDE-DefaultTemplate", false);
219 measureSystem = config.readEntry("X-KDE-MeasureSystem").toLower();
220
221 // Don't add a template that is for the wrong measure system
222 if (measureSystem == dontShow)
223 continue;
224
225 //dbgUI <<"hidden:" << hidden_str;
226 templatePath = config.readPathEntry("URL", QString());
227 //dbgUI <<"Link to :" << templatePath;
228 if (templatePath[0] != '/') {
229 if (templatePath.left(6) == "file:/") // I doubt this will happen
230 templatePath = templatePath.right(templatePath.length() - 6);
231 //else
232 // dbgUI <<"dirname=" << *it;
233 templatePath = *it + templatePath;
234 //dbgUI <<"templatePath:" << templatePath;
235 }
236 } else
237 continue; // Invalid
238 }
239 // The else if and the else branch are here for compat. with the old system
240 else if (files[i].right(4) != ".png")
241 // Ignore everything that is not a PNG file
242 continue;
243 else {
244 // Found a PNG file - the template must be here in the same dir.
245 icon = filePath;
246 QFileInfo fi(filePath);
247 text = fi.completeBaseName();
248 templatePath = filePath; // Note that we store the .png file as the template !
249 // That's the way it's always been done. Then the app replaces the extension...
250 }
251 KisTemplate *t = new KisTemplate(text, description, templatePath, icon, fileName,
252 measureSystem, hidden);
253 group->add(t, false, false); // false -> we aren't a "user", false -> don't
254 // "touch" the group to avoid useless
255 // creation of dirs in .kde/blah/...
256 if (defaultTemplate)
258 }
259 }
260 }
261}
262
264 const QString &localDir)
265{
266 QString fileName;
267 if (t->isHidden()) {
268 fileName = t->fileName();
269 // try to remove the file
270 if (QFile::remove(fileName) || !QFile::exists(fileName)) {
271 QFile::remove(t->name());
272 QFile::remove(t->picture());
273 return;
274 }
275 }
276 // be sure that the template's file name is unique so we don't overwrite another
277 QString const path = localDir + group->name() + '/';
278 QString const name = KisTemplates::trimmed(t->name());
279 fileName = path + name + ".desktop";
280 if (t->isHidden() && QFile::exists(fileName))
281 return;
282 QString fill;
283 while (QFile(fileName).exists()) {
284 fill += '_';
285 fileName = path + fill + name + ".desktop";
286 }
287
288 KConfig _config(fileName, KConfig::SimpleConfig);
289 _config.setLocale(currentLocale());
290 KConfigGroup config(&_config, "Desktop Entry");
291 config.writeEntry("Type", "Link");
292 config.writePathEntry("URL", t->file());
293 config.writeEntry("Name", t->name());
294 config.writeEntry("Icon", t->picture());
295 config.writeEntry("X-KDE-Hidden", t->isHidden());
296}
static QString currentLocale()
QString name() const
QStringList dirs() const
QList< KisTemplate * > templates() const
bool touched() const
bool isHidden() const
If all children are hidden, we are hidden too.
void addDir(const QString &dir)
bool add(KisTemplate *t, bool force=false, bool touch=true)
void writeTemplate(KisTemplate *t, KisTemplateGroup *group, const QString &localDir)
KisTemplateGroup * m_defaultGroup
QList< KisTemplateGroup * > m_groups
bool add(KisTemplateGroup *g)
KisTemplate * m_defaultTemplate
KisTemplateGroup * find(const QString &name) const
KisTemplate * defaultTemplate() const
KisTemplateTree(const QString &templatesResourcePath, bool readTree=false)
QString picture() const
Definition KisTemplate.h:37
bool touched() const
Definition KisTemplate.h:52
QString name() const
Definition KisTemplate.h:28
QString file() const
Definition KisTemplate.h:34
QString fileName() const
Definition KisTemplate.h:40
bool isHidden() const
Definition KisTemplate.h:45
static QStringList findDirs(const QString &type)
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)
QString trimmed(const QString &string)