27
27
#include <kdebug.h>
28
28
#include <kdeversion.h>
29
29
#include <kinstance.h>
30
#include <ksavefile.h>
30
31
#include <kstandarddirs.h>
32
#include <kiconloader.h>
31
33
#include <kio/netaccess.h>
33
35
#include <stdlib.h>
36
38
KoTemplate::KoTemplate(const QString &name, const QString &description, const QString &file,
37
const QString &picture, bool hidden,
39
const QString &picture, const QString &fileName, bool hidden,
39
m_name(name), m_descr(description), m_file(file), m_picture(picture), m_hidden(hidden),
40
m_touched(touched), m_cached(false) {
41
m_name(name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
42
m_hidden(hidden), m_touched(touched), m_cached(false) {
43
const QPixmap &KoTemplate::loadPicture() {
45
const QPixmap &KoTemplate::loadPicture( KInstance* instance ) {
47
QImage img( m_picture );
49
kdWarning() << "Couldn't find icon " << m_picture << endl;
53
const int maxHeightWidth = 64;
54
if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
55
img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
57
m_pixmap.convertFromImage(img, QPixmap::Color);
50
if ( m_picture[ 0 ] == '/' )
52
// ### TODO: use the class KoPicture instead of QImage to support non-image pictures
53
QImage img( m_picture );
55
kdWarning() << "Couldn't find icon " << m_picture << endl;
59
const int maxHeightWidth = 64; // ### TODO: some people would surely like to have 128x128
60
if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
61
img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
63
m_pixmap.convertFromImage(img);
65
} else { // relative path
66
m_pixmap = instance->iconLoader()->loadIcon( m_picture, KIcon::Desktop, KIcon::SizeLarge /*48*/ );
97
106
else if(myTemplate && force) {
107
//kdDebug() << "removing :" << myTemplate->fileName() << endl;
108
QFile::remove( myTemplate->fileName() );
109
QFile::remove( myTemplate->picture() );
110
QFile::remove( myTemplate->file() );
98
111
m_templates.removeRef(myTemplate);
99
112
m_templates.append(t);
115
128
KoTemplateTree::KoTemplateTree(const QCString &templateType,
116
129
KInstance *instance, bool readTree) :
117
m_templateType(templateType), m_instance(instance), m_defaultGroup(0L) {
130
m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
131
m_defaultTemplate(0L) {
119
133
m_groups.setAutoDelete(true);
149
162
//kdDebug() << "hidden" << endl;
150
163
if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
151
164
//kdDebug() << "local only" << endl;
152
KIO::NetAccess::del(group->dirs().first());
165
KIO::NetAccess::del(group->dirs().first(), 0);
153
166
//kdDebug() << "removing: " << group->dirs().first() << endl;
163
176
//kdDebug() << "++template: " << t->name() << endl;
164
177
writeTemplate(t, group, localDir);
166
if(t->isHidden() && t->touched() && t->file().contains(localDir)) {
179
if(t->isHidden() && t->touched() ) {
167
180
//kdDebug() << "+++ delete local template ##############" << endl;
168
181
writeTemplate(t, group, localDir);
169
182
QFile::remove(t->file());
237
250
//kdDebug() << "filePath: " << filePath << endl;
241
254
QString hidden_str;
242
256
bool hidden=false;
257
bool defaultTemplate = false;
243
258
QString templatePath;
244
259
// If a desktop file, then read the name from it.
245
260
// Otherwise (or if no name in it?) use file name
248
263
config.setDesktopGroup();
249
264
if (config.readEntry("Type")=="Link") {
250
265
text=config.readEntry("Name");
266
hidden=config.readBoolEntry("X-KDE-Hidden", false);
251
268
description=config.readEntry("Comment");
252
//kdDebug() << "name: " << text << endl;
269
//kdDebug() << "name: " << text << endl;
253
270
icon=config.readEntry("Icon");
254
//kdDebug() << "icon1: " << icon << endl;
255
if(icon[0]!='/') // allow absolute paths for icons
271
if(icon[0]!='/' && // allow absolute paths for icons
272
QFile::exists(*it+icon)) // allow icons from icontheme
257
274
//kdDebug() << "icon2: " << icon << endl;
258
hidden_str=config.readEntry("X-KDE-Hidden");
259
if(hidden_str.lower()=="true")
275
hidden=config.readBoolEntry("X-KDE-Hidden", false);
276
defaultTemplate = config.readBoolEntry("X-KDE-DefaultTemplate", false);
261
277
//kdDebug() << "hidden: " << hidden_str << endl;
262
278
templatePath=config.readPathEntry("URL");
263
279
//kdDebug() << "Link to : " << templatePath << endl;
284
300
templatePath = filePath; // Note that we store the .png file as the template !
285
301
// That's the way it's always been done. Then the app replaces the extension...
287
KoTemplate *t=new KoTemplate(text, description, templatePath, icon, hidden);
303
KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName, hidden);
288
304
groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't
289
305
// "touch" the group to avoid useless
290
306
// creation of dirs in .kde/blah/...
307
if ( defaultTemplate )
308
m_defaultTemplate = t;
296
314
void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
297
315
const QString &localDir) {
319
fileName = t->fileName();
320
// try to remove the file
321
if ( QFile::remove(fileName) || !QFile::exists(fileName) )
323
QFile::remove( t->name() );
324
QFile::remove( t->picture() );
328
// be sure that the template's file name is unique so we don't overwrite an other
329
QString const path = localDir + group->name() + '/';
330
QString const name = KoTemplates::stripWhiteSpace( t->name() );
331
fileName = path + name + ".desktop";
332
if ( t->isHidden() && QFile::exists(fileName) )
335
while ( KIO::NetAccess::exists( fileName, true, 0 ) )
338
fileName = path + fill + name + ".desktop";
299
KSimpleConfig config(KoTemplates::stripWhiteSpace(localDir+group->name()+'/'+t->name()+".desktop"));
341
KSimpleConfig config( fileName );
300
342
config.setDesktopGroup();
301
343
config.writeEntry("Type", "Link");
302
#if KDE_IS_VERSION(3,1,3)
303
344
config.writePathEntry("URL", t->file());
305
config.writeEntry("URL", t->file());
307
345
config.writeEntry("Name", t->name());
308
346
config.writeEntry("Icon", t->picture());
309
347
config.writeEntry("X-KDE-Hidden", t->isHidden());