20#include <KoTemplates.h>
26#include <kdesktopfile.h>
27#include <ksimpleconfig.h>
29#include <tdeinstance.h>
31#include <kstandarddirs.h>
32#include <kiconloader.h>
33#include <tdeio/netaccess.h>
39KoTemplate::KoTemplate(
const TQString &name,
const TQString &description,
const TQString &file,
40 const TQString &picture,
const TQString &fileName,
const TQString &_measureSystem,
41 bool hidden,
bool touched) :
42 m_name(
name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
43 m_hidden(hidden), m_touched(touched), m_cached(false), m_measureSystem(_measureSystem)
47const TQPixmap &KoTemplate::loadPicture( TDEInstance* instance ) {
52 if ( m_picture[ 0 ] ==
'/' )
55 TQImage img( m_picture );
57 kdWarning() <<
"Couldn't find icon " << m_picture << endl;
61 const int maxHeightWidth = 128;
62 if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
63 img = img.smoothScale( maxHeightWidth, maxHeightWidth, TQImage::ScaleMax );
65 m_pixmap.convertFromImage(img);
68 m_pixmap = instance->iconLoader()->loadIcon( m_picture, TDEIcon::Desktop, 128 );
74KoTemplateGroup::KoTemplateGroup(
const TQString &name,
const TQString &dir,
75 int _sortingWeight,
bool touched) :
76 m_name(
name), m_touched(touched), m_sortingWeight(_sortingWeight)
79 m_templates.setAutoDelete(
true);
82bool KoTemplateGroup::isHidden()
const {
84 TQPtrListIterator<KoTemplate> it(m_templates);
86 while(it.current()!=0L && hidden) {
87 hidden=it.current()->isHidden();
93void KoTemplateGroup::setHidden(
bool hidden)
const {
95 TQPtrListIterator<KoTemplate> it(m_templates);
96 for( ; it.current()!=0L; ++it)
97 it.current()->setHidden(hidden);
101bool KoTemplateGroup::add(KoTemplate *t,
bool force,
bool touch) {
103 KoTemplate *myTemplate=find(t->name());
105 m_templates.append(t);
109 else if(myTemplate && force) {
111 TQFile::remove( myTemplate->fileName() );
112 TQFile::remove( myTemplate->picture() );
113 TQFile::remove( myTemplate->file() );
114 m_templates.removeRef(myTemplate);
115 m_templates.append(t);
122KoTemplate *KoTemplateGroup::find(
const TQString &name)
const {
124 TQPtrListIterator<KoTemplate> it(m_templates);
125 while(it.current() && it.current()->name()!=name)
131KoTemplateTree::KoTemplateTree(
const TQCString &templateType,
132 TDEInstance *instance,
bool readTree) :
133 m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
134 m_defaultTemplate(0L) {
136 m_groups.setAutoDelete(
true);
141void KoTemplateTree::readTemplateTree() {
147void KoTemplateTree::writeTemplateTree() {
148 TQString localDir=m_instance->dirs()->saveLocation(m_templateType);
150 for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) {
155 for(KoTemplate *t=group->first(); t!=0L && !touched && !group->touched(); t=group->next())
156 touched=t->touched();
158 if(group->touched() || touched) {
160 if(!group->isHidden()) {
162 TDEStandardDirs::makeDir(localDir+group->name());
166 if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
168 TDEIO::NetAccess::del(group->dirs().first(), 0);
173 TDEStandardDirs::makeDir(localDir+group->name());
177 for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
180 writeTemplate(t, group, localDir);
182 if(t->isHidden() && t->touched() ) {
184 writeTemplate(t, group, localDir);
185 TQFile::remove(t->file());
186 TQFile::remove(t->picture());
192void KoTemplateTree::add(KoTemplateGroup *g) {
194 KoTemplateGroup *group=find(g->name());
198 group->addDir(g->dirs().first());
201KoTemplateGroup *KoTemplateTree::find(
const TQString &name)
const {
203 TQPtrListIterator<KoTemplateGroup> it(m_groups);
204 while(it.current() && it.current()->name()!=name)
209void KoTemplateTree::readGroups() {
211 TQStringList dirs = m_instance->dirs()->resourceDirs(m_templateType);
212 for(TQStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
218 dir.setFilter(TQDir::Dirs);
219 TQStringList templateDirs=dir.entryList();
220 for(TQStringList::ConstIterator tdirIt=templateDirs.begin(); tdirIt!=templateDirs.end(); ++tdirIt) {
221 if(*tdirIt==
"." || *tdirIt==
"..")
223 TQDir templateDir(*it+*tdirIt);
224 TQString
name=*tdirIt;
226 int sortingWeight = 1000;
227 if(templateDir.exists(
".directory")) {
228 KSimpleConfig config(templateDir.absPath()+
"/.directory",
true);
229 config.setDesktopGroup();
230 name=config.readEntry(
"Name");
231 defaultTab=config.readEntry(
"X-TDE-DefaultTab");
232 sortingWeight=config.readNumEntry(
"X-TDE-SortingWeight", 1000);
235 KoTemplateGroup *g=
new KoTemplateGroup(name, *it+*tdirIt+TQChar(
'/'), sortingWeight);
237 if(defaultTab==
"true")
243void KoTemplateTree::readTemplates() {
244 TQString dontShow =
"imperial";
246 if(TDEGlobal::locale()->pageSize() == TQPrinter::Letter) {
250 TQPtrListIterator<KoTemplateGroup> groupIt(m_groups);
251 for( ; groupIt.current()!=0L; ++groupIt) {
252 TQStringList dirs=groupIt.current()->dirs();
253 for(TQStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
257 TQStringList files=d.entryList( TQDir::Files | TQDir::Readable, TQDir::Name );
258 for(
unsigned int i=0; i<files.count(); ++i) {
259 TQString filePath = *it + files[i];
263 TQString description;
267 bool defaultTemplate =
false;
268 TQString templatePath;
269 TQString measureSystem;
272 if (KDesktopFile::isDesktopFile(filePath)) {
273 KSimpleConfig config(filePath,
true);
274 config.setDesktopGroup();
275 if (config.readEntry(
"Type")==
"Link") {
276 text=config.readEntry(
"Name");
278 description=config.readEntry(
"Comment");
280 icon=config.readEntry(
"Icon");
282 TQFile::exists(*it+icon))
285 hidden=config.readBoolEntry(
"X-TDE-Hidden",
false);
286 defaultTemplate = config.readBoolEntry(
"X-TDE-DefaultTemplate",
false);
287 measureSystem=config.readEntry(
"X-TDE-MeasureSystem").lower();
290 if(measureSystem == dontShow)
294 templatePath=config.readPathEntry(
"URL");
296 if(templatePath[0]!=
'/') {
297 if(templatePath.left(6)==
"file:/")
298 templatePath=templatePath.right(templatePath.length()-6);
301 templatePath=*it+templatePath;
308 else if ( files[i].right(4) !=
".png" )
314 TQFileInfo fi(filePath);
315 text = fi.baseName();
316 templatePath = filePath;
319 KoTemplate *t=
new KoTemplate(text, description, templatePath, icon, fileName,
320 measureSystem, hidden);
321 groupIt.current()->add(t,
false,
false);
324 if ( defaultTemplate )
325 m_defaultTemplate = t;
331void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
332 const TQString &localDir) {
336 fileName = t->fileName();
338 if ( TQFile::remove(fileName) || !TQFile::exists(fileName) )
340 TQFile::remove( t->name() );
341 TQFile::remove( t->picture() );
346 TQString
const path = localDir + group->name() +
'/';
347 TQString
const name = KoTemplates::stripWhiteSpace( t->name() );
348 fileName = path +
name +
".desktop";
349 if ( t->isHidden() && TQFile::exists(fileName) )
352 while ( TDEIO::NetAccess::exists( fileName,
true, 0 ) )
355 fileName = path + fill +
name +
".desktop";
358 KSimpleConfig config( fileName );
359 config.setDesktopGroup();
360 config.writeEntry(
"Type",
"Link");
361 config.writePathEntry(
"URL", t->file());
362 config.writeEntry(
"Name", t->name());
363 config.writeEntry(
"Icon", t->picture());
364 config.writeEntry(
"X-TDE-Hidden", t->isHidden());
367namespace KoTemplates {
368TQString stripWhiteSpace(
const TQString &
string) {
371 for(
unsigned int i=0; i<
string.length(); ++i) {
372 TQChar tmp(
string[i]);
const char * name(StdAction id)