summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoTemplates.cpp
blob: 2d0ba5298adade79564a2825b4ad3388c95bf71b (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* This file is part of the KDE project
   Copyright (C) 2000 Werner Trobin <trobin@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <KoTemplates.h>

#include <tqdir.h>
#include <tqimage.h>
#include <tqprinter.h>

#include <kdesktopfile.h>
#include <ksimpleconfig.h>
#include <kdebug.h>
#include <tdeversion.h>
#include <kinstance.h>
#include <ksavefile.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <tdeio/netaccess.h>
#include <tdelocale.h>

#include <stdlib.h>


KoTemplate::KoTemplate(const TQString &name, const TQString &description, const TQString &file,
                       const TQString &picture, const TQString &fileName, const TQString &_measureSystem,
                       bool hidden, bool touched) :
    m_name(name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
    m_hidden(hidden), m_touched(touched), m_cached(false), m_measureSystem(_measureSystem)
{
}

const TQPixmap &KoTemplate::loadPicture( TDEInstance* instance ) {

    if(m_cached)
        return m_pixmap;
    m_cached=true;
    if ( m_picture[ 0 ] == '/' )
    {
        // ### TODO: use the class KoPicture instead of TQImage to support non-image pictures
        TQImage img( m_picture );
        if (img.isNull()) {
            kdWarning() << "Couldn't find icon " << m_picture << endl;
            m_pixmap=TQPixmap();
            return m_pixmap;
        }
        const int maxHeightWidth = 128; // ### TODO: some people would surely like to have 128x128
        if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
            img = img.smoothScale( maxHeightWidth, maxHeightWidth, TQ_ScaleMax );
        }
        m_pixmap.convertFromImage(img);
        return m_pixmap;
    } else { // relative path
        m_pixmap = instance->iconLoader()->loadIcon( m_picture, TDEIcon::Desktop, 128 );
        return m_pixmap;
    }
}


KoTemplateGroup::KoTemplateGroup(const TQString &name, const TQString &dir,
                                 int _sortingWeight, bool touched) :
    m_name(name), m_touched(touched), m_sortingWeight(_sortingWeight)
{
    m_dirs.append(dir);
    m_templates.setAutoDelete(true);
}

bool KoTemplateGroup::isHidden() const {

    TQPtrListIterator<KoTemplate> it(m_templates);
    bool hidden=true;
    while(it.current()!=0L && hidden) {
        hidden=it.current()->isHidden();
        ++it;
    }
    return hidden;
}

void KoTemplateGroup::setHidden(bool hidden) const {

    TQPtrListIterator<KoTemplate> it(m_templates);
    for( ; it.current()!=0L; ++it)
        it.current()->setHidden(hidden);
    m_touched=true;
}

bool KoTemplateGroup::add(KoTemplate *t, bool force, bool touch) {

    KoTemplate *myTemplate=find(t->name());
    if(myTemplate==0L) {
        m_templates.append(t);
        m_touched=touch;
        return true;
    }
    else if(myTemplate && force) {
        //kdDebug() << "removing :" << myTemplate->fileName() << endl;
        TQFile::remove( myTemplate->fileName()  );
        TQFile::remove( myTemplate->picture() );
        TQFile::remove( myTemplate->file() );
        m_templates.removeRef(myTemplate);
        m_templates.append(t);
        m_touched=touch;
        return true;
    }
    return false;
}

KoTemplate *KoTemplateGroup::find(const TQString &name) const {

    TQPtrListIterator<KoTemplate> it(m_templates);
    while(it.current() && it.current()->name()!=name)
        ++it;
    return it.current();
}


KoTemplateTree::KoTemplateTree(const TQCString &templateType,
                               TDEInstance *instance, bool readTree) :
    m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
    m_defaultTemplate(0L) {

    m_groups.setAutoDelete(true);
    if(readTree)
        readTemplateTree();
}

void KoTemplateTree::readTemplateTree() {

    readGroups();
    readTemplates();
}

void KoTemplateTree::writeTemplateTree() {
    TQString localDir=m_instance->dirs()->saveLocation(m_templateType);

    for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) {
        //kdDebug() << "---------------------------------" << endl;
        //kdDebug() << "group: " << group->name() << endl;

        bool touched=false;
        for(KoTemplate *t=group->first(); t!=0L && !touched && !group->touched(); t=group->next())
            touched=t->touched();

        if(group->touched() || touched) {
            //kdDebug() << "touched" << endl;
            if(!group->isHidden()) {
                //kdDebug() << "not hidden" << endl;
                TDEStandardDirs::makeDir(localDir+group->name()); // create the local group dir
            }
            else {
                //kdDebug() << "hidden" << endl;
                if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
                    //kdDebug() << "local only" << endl;
                    TDEIO::NetAccess::del(group->dirs().first(), 0);
                    //kdDebug() << "removing: " << group->dirs().first() << endl;
                }
                else {
                    //kdDebug() << "global" << endl;
                    TDEStandardDirs::makeDir(localDir+group->name());
                }
            }
        }
        for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
            if(t->touched()) {
                //kdDebug() << "++template: " << t->name() << endl;
                writeTemplate(t, group, localDir);
            }
            if(t->isHidden() && t->touched() ) {
                //kdDebug() << "+++ delete local template ##############" << endl;
                writeTemplate(t, group, localDir);
                TQFile::remove(t->file());
                TQFile::remove(t->picture());
            }
        }
    }
}

void KoTemplateTree::add(KoTemplateGroup *g) {

    KoTemplateGroup *group=find(g->name());
    if(group==0L)
        m_groups.append(g);
    else
        group->addDir(g->dirs().first()); // "...there can be only one..." (Queen)
}

KoTemplateGroup *KoTemplateTree::find(const TQString &name) const {

    TQPtrListIterator<KoTemplateGroup> it(m_groups);
    while(it.current() && it.current()->name()!=name)
        ++it;
    return it.current();
}

void KoTemplateTree::readGroups() {

    TQStringList dirs = m_instance->dirs()->resourceDirs(m_templateType);
    for(TQStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
        //kdDebug() << "dir: " << *it << endl;
        TQDir dir(*it);
        // avoid the annoying warning
        if(!dir.exists())
            continue;
        dir.setFilter(TQDir::Dirs);
        TQStringList templateDirs=dir.entryList();
        for(TQStringList::ConstIterator tdirIt=templateDirs.begin(); tdirIt!=templateDirs.end(); ++tdirIt) {
            if(*tdirIt=="." || *tdirIt=="..") // we don't want to check those dirs :)
                continue;
            TQDir templateDir(*it+*tdirIt);
            TQString name=*tdirIt;
            TQString defaultTab;
            int sortingWeight = 1000;
            if(templateDir.exists(".directory")) {
                KSimpleConfig config(templateDir.absPath()+"/.directory", true);
                config.setDesktopGroup();
                name=config.readEntry("Name");
                defaultTab=config.readEntry("X-TDE-DefaultTab");
                sortingWeight=config.readNumEntry("X-TDE-SortingWeight", 1000);
                //kdDebug() << "name: " << name <<endl;
            }
            KoTemplateGroup *g=new KoTemplateGroup(name, *it+*tdirIt+TQChar('/'), sortingWeight);
            add(g);
            if(defaultTab=="true")
                m_defaultGroup=g;
        }
    }
}

void KoTemplateTree::readTemplates() {
    TQString dontShow = "imperial";

    if(TDEGlobal::locale()->pageSize() == TQPrinter::Letter) {
        dontShow = "metric";
    }

    TQPtrListIterator<KoTemplateGroup> groupIt(m_groups);
    for( ; groupIt.current()!=0L; ++groupIt) {
        TQStringList dirs=groupIt.current()->dirs();
        for(TQStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
            TQDir d(*it);
            if( !d.exists() )
                continue;
            TQStringList files=d.entryList( TQDir::Files | TQDir::Readable, TQDir::Name );
            for(unsigned int i=0; i<files.count(); ++i) {
                TQString filePath = *it + files[i];
                //kdDebug() << "filePath: " << filePath << endl;
                TQString icon;
                TQString text;
                TQString description;
                TQString hidden_str;
                TQString fileName;
                bool hidden=false;
                bool defaultTemplate = false;
                TQString templatePath;
                TQString measureSystem;
                // If a desktop file, then read the name from it.
                // Otherwise (or if no name in it?) use file name
                if (KDesktopFile::isDesktopFile(filePath)) {
                    KSimpleConfig config(filePath, true);
                    config.setDesktopGroup();
                    if (config.readEntry("Type")=="Link") {
                        text=config.readEntry("Name");
                        fileName=filePath;
                        description=config.readEntry("Comment");
                        //kdDebug() << "name: " << text << endl;
                        icon=config.readEntry("Icon");
                        if(icon[0]!='/' && // allow absolute paths for icons
                           TQFile::exists(*it+icon)) // allow icons from icontheme
                            icon=*it+icon;
                        //kdDebug() << "icon2: " << icon << endl;
                        hidden=config.readBoolEntry("X-TDE-Hidden", false);
                        defaultTemplate = config.readBoolEntry("X-TDE-DefaultTemplate", false);
                        measureSystem=config.readEntry("X-TDE-MeasureSystem").lower();

                        // Don't add a template that is for the wrong measure system
                        if(measureSystem == dontShow)
                            continue;

                        //kdDebug() << "hidden: " << hidden_str << endl;
                        templatePath=config.readPathEntry("URL");
                        //kdDebug() << "Link to : " << templatePath << endl;
                        if(templatePath[0]!='/') {
                            if(templatePath.left(6)=="file:/") // I doubt this will happen
                                templatePath=templatePath.right(templatePath.length()-6);
                            //else
                            //  kdDebug() << "dirname=" << *it << endl;
                            templatePath=*it+templatePath;
                            //kdDebug() << "templatePath: " << templatePath << endl;
                        }
                    } else
                        continue; // Invalid
                }
                // The else if and the else branch are here for compat. with the old system
                else if ( files[i].right(4) != ".png" )
                    // Ignore everything that is not a PNG file
                    continue;
                else {
                    // Found a PNG file - the template must be here in the same dir.
                    icon = filePath;
                    TQFileInfo fi(filePath);
                    text = fi.baseName();
                    templatePath = filePath; // Note that we store the .png file as the template !
                    // That's the way it's always been done. Then the app replaces the extension...
                }
                KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName,
                                             measureSystem, hidden);
                groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't
                                                         // "touch" the group to avoid useless
                                                         // creation of dirs in .kde/blah/...
                if ( defaultTemplate )
                    m_defaultTemplate = t;
            }
        }
    }
}

void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
                                   const TQString &localDir) {
    TQString fileName;
    if ( t->isHidden() )
    {
        fileName = t->fileName();
        // try to remove the file
        if ( TQFile::remove(fileName) || !TQFile::exists(fileName) )
        {
            TQFile::remove( t->name() );
            TQFile::remove( t->picture() );
            return;
        }
    }
    // be sure that the template's file name is unique so we don't overwrite an other
    TQString const path = localDir + group->name() + '/';
    TQString const name = KoTemplates::stripWhiteSpace( t->name() );
    fileName = path + name + ".desktop";
    if ( t->isHidden() && TQFile::exists(fileName) )
        return;
    TQString fill;
    while ( TDEIO::NetAccess::exists( fileName, true, 0 ) )
    {
        fill += '_';
        fileName = path + fill + name + ".desktop";
    }

    KSimpleConfig config( fileName );
    config.setDesktopGroup();
    config.writeEntry("Type", "Link");
    config.writePathEntry("URL", t->file());
    config.writeEntry("Name", t->name());
    config.writeEntry("Icon", t->picture());
    config.writeEntry("X-TDE-Hidden", t->isHidden());
}

namespace KoTemplates {
TQString stripWhiteSpace(const TQString &string) {

    TQString ret;
    for(unsigned int i=0; i<string.length(); ++i) {
        TQChar tmp(string[i]);
        if(!tmp.isSpace())
            ret+=tmp;
    }
    return ret;
}
}