lib Library API Documentation

koTemplates.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 2000 Werner Trobin <trobin@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include <koTemplates.h> 00021 00022 #include <qdir.h> 00023 #include <qimage.h> 00024 00025 #include <kdesktopfile.h> 00026 #include <ksimpleconfig.h> 00027 #include <kdebug.h> 00028 #include <kdeversion.h> 00029 #include <kinstance.h> 00030 #include <kstandarddirs.h> 00031 #include <kio/netaccess.h> 00032 00033 #include <stdlib.h> 00034 00035 00036 KoTemplate::KoTemplate(const QString &name, const QString &description, const QString &file, 00037 const QString &picture, bool hidden, 00038 bool touched) : 00039 m_name(name), m_descr(description), m_file(file), m_picture(picture), m_hidden(hidden), 00040 m_touched(touched), m_cached(false) { 00041 } 00042 00043 const QPixmap &KoTemplate::loadPicture() { 00044 00045 if(m_cached) 00046 return m_pixmap; 00047 QImage img( m_picture ); 00048 if (img.isNull()) { 00049 kdWarning() << "Couldn't find icon " << m_picture << endl; 00050 m_pixmap=QPixmap(); 00051 return m_pixmap; 00052 } 00053 const int maxHeightWidth = 64; 00054 if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) { 00055 img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax ); 00056 } 00057 m_pixmap.convertFromImage(img, QPixmap::Color); 00058 m_cached=true; 00059 return m_pixmap; 00060 } 00061 00062 00063 KoTemplateGroup::KoTemplateGroup(const QString &name, const QString &dir, 00064 bool touched) : 00065 m_name(name), m_touched(touched) { 00066 m_dirs.append(dir); 00067 m_templates.setAutoDelete(true); 00068 } 00069 00070 bool KoTemplateGroup::isHidden() const { 00071 00072 QPtrListIterator<KoTemplate> it(m_templates); 00073 bool hidden=true; 00074 while(it.current()!=0L && hidden) { 00075 hidden=it.current()->isHidden(); 00076 ++it; 00077 } 00078 return hidden; 00079 } 00080 00081 void KoTemplateGroup::setHidden(bool hidden) const { 00082 00083 QPtrListIterator<KoTemplate> it(m_templates); 00084 for( ; it.current()!=0L; ++it) 00085 it.current()->setHidden(hidden); 00086 m_touched=true; 00087 } 00088 00089 bool KoTemplateGroup::add(KoTemplate *t, bool force, bool touch) { 00090 00091 KoTemplate *myTemplate=find(t->name()); 00092 if(myTemplate==0L) { 00093 m_templates.append(t); 00094 m_touched=touch; 00095 return true; 00096 } 00097 else if(myTemplate && force) { 00098 m_templates.removeRef(myTemplate); 00099 m_templates.append(t); 00100 m_touched=touch; 00101 return true; 00102 } 00103 return false; 00104 } 00105 00106 KoTemplate *KoTemplateGroup::find(const QString &name) const { 00107 00108 QPtrListIterator<KoTemplate> it(m_templates); 00109 while(it.current() && it.current()->name()!=name) 00110 ++it; 00111 return it.current(); 00112 } 00113 00114 00115 KoTemplateTree::KoTemplateTree(const QCString &templateType, 00116 KInstance *instance, bool readTree) : 00117 m_templateType(templateType), m_instance(instance), m_defaultGroup(0L) { 00118 00119 m_groups.setAutoDelete(true); 00120 if(readTree) 00121 readTemplateTree(); 00122 } 00123 00124 void KoTemplateTree::readTemplateTree() { 00125 00126 readGroups(); 00127 readTemplates(); 00128 } 00129 00130 void KoTemplateTree::writeTemplateTree() { 00131 00132 QString localDir=m_instance->dirs()->saveLocation(m_templateType); 00133 00134 for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) { 00135 //kdDebug() << "---------------------------------" << endl; 00136 //kdDebug() << "group: " << group->name() << endl; 00137 00138 bool touched=false; 00139 for(KoTemplate *t=group->first(); t!=0L && !touched && !group->touched(); t=group->next()) 00140 touched=t->touched(); 00141 00142 if(group->touched() || touched) { 00143 //kdDebug() << "touched" << endl; 00144 if(!group->isHidden()) { 00145 //kdDebug() << "not hidden" << endl; 00146 KStandardDirs::makeDir(localDir+group->name()); // create the local group dir 00147 } 00148 else { 00149 //kdDebug() << "hidden" << endl; 00150 if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) { 00151 //kdDebug() << "local only" << endl; 00152 KIO::NetAccess::del(group->dirs().first()); 00153 //kdDebug() << "removing: " << group->dirs().first() << endl; 00154 } 00155 else { 00156 //kdDebug() << "global" << endl; 00157 KStandardDirs::makeDir(localDir+group->name()); 00158 } 00159 } 00160 } 00161 for(KoTemplate *t=group->first(); t!=0L; t=group->next()) { 00162 if(t->touched()) { 00163 //kdDebug() << "++template: " << t->name() << endl; 00164 writeTemplate(t, group, localDir); 00165 } 00166 if(t->isHidden() && t->touched() && t->file().contains(localDir)) { 00167 //kdDebug() << "+++ delete local template ##############" << endl; 00168 writeTemplate(t, group, localDir); 00169 QFile::remove(t->file()); 00170 QFile::remove(t->picture()); 00171 } 00172 } 00173 } 00174 } 00175 00176 void KoTemplateTree::add(KoTemplateGroup *g) { 00177 00178 KoTemplateGroup *group=find(g->name()); 00179 if(group==0L) 00180 m_groups.append(g); 00181 else 00182 group->addDir(g->dirs().first()); // "...there can be only one..." (Queen) 00183 } 00184 00185 KoTemplateGroup *KoTemplateTree::find(const QString &name) const { 00186 00187 QPtrListIterator<KoTemplateGroup> it(m_groups); 00188 while(it.current() && it.current()->name()!=name) 00189 ++it; 00190 return it.current(); 00191 } 00192 00193 void KoTemplateTree::readGroups() { 00194 00195 QStringList dirs = m_instance->dirs()->resourceDirs(m_templateType); 00196 for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) { 00197 //kdDebug() << "dir: " << *it << endl; 00198 QDir dir(*it); 00199 // avoid the annoying warning 00200 if(!dir.exists()) 00201 continue; 00202 dir.setFilter(QDir::Dirs); 00203 QStringList templateDirs=dir.entryList(); 00204 for(QStringList::ConstIterator tdirIt=templateDirs.begin(); tdirIt!=templateDirs.end(); ++tdirIt) { 00205 if(*tdirIt=="." || *tdirIt=="..") // we don't want to check those dirs :) 00206 continue; 00207 QDir templateDir(*it+*tdirIt); 00208 QString name=*tdirIt; 00209 QString defaultTab; 00210 if(templateDir.exists(".directory")) { 00211 KSimpleConfig config(templateDir.absPath()+"/.directory", true); 00212 config.setDesktopGroup(); 00213 name=config.readEntry("Name"); 00214 defaultTab=config.readEntry("X-KDE-DefaultTab"); 00215 //kdDebug() << "name: " << name <<endl; 00216 } 00217 KoTemplateGroup *g=new KoTemplateGroup(name, *it+*tdirIt+QChar('/')); 00218 add(g); 00219 if(defaultTab=="true") 00220 m_defaultGroup=g; 00221 } 00222 } 00223 } 00224 00225 void KoTemplateTree::readTemplates() { 00226 00227 QPtrListIterator<KoTemplateGroup> groupIt(m_groups); 00228 for( ; groupIt.current()!=0L; ++groupIt) { 00229 QStringList dirs=groupIt.current()->dirs(); 00230 for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) { 00231 QDir d(*it); 00232 if( !d.exists() ) 00233 continue; 00234 QStringList files=d.entryList( QDir::Files | QDir::Readable, QDir::Name ); 00235 for(unsigned int i=0; i<files.count(); ++i) { 00236 QString filePath = *it + files[i]; 00237 //kdDebug() << "filePath: " << filePath << endl; 00238 QString icon; 00239 QString text; 00240 QString description; 00241 QString hidden_str; 00242 bool hidden=false; 00243 QString templatePath; 00244 // If a desktop file, then read the name from it. 00245 // Otherwise (or if no name in it?) use file name 00246 if (KDesktopFile::isDesktopFile(filePath)) { 00247 KSimpleConfig config(filePath, true); 00248 config.setDesktopGroup(); 00249 if (config.readEntry("Type")=="Link") { 00250 text=config.readEntry("Name"); 00251 description=config.readEntry("Comment"); 00252 //kdDebug() << "name: " << text << endl; 00253 icon=config.readEntry("Icon"); 00254 //kdDebug() << "icon1: " << icon << endl; 00255 if(icon[0]!='/') // allow absolute paths for icons 00256 icon=*it + icon; 00257 //kdDebug() << "icon2: " << icon << endl; 00258 hidden_str=config.readEntry("X-KDE-Hidden"); 00259 if(hidden_str.lower()=="true") 00260 hidden=true; 00261 //kdDebug() << "hidden: " << hidden_str << endl; 00262 templatePath=config.readPathEntry("URL"); 00263 //kdDebug() << "Link to : " << templatePath << endl; 00264 if(templatePath[0]!='/') { 00265 if(templatePath.left(6)=="file:/") // I doubt this will happen 00266 templatePath=templatePath.right(templatePath.length()-6); 00267 //else 00268 // kdDebug() << "dirname=" << *it << endl; 00269 templatePath=*it+templatePath; 00270 //kdDebug() << "templatePath: " << templatePath << endl; 00271 } 00272 } else 00273 continue; // Invalid 00274 } 00275 // The else if and the else branch are here for compat. with the old system 00276 else if ( files[i].right(4) != ".png" ) 00277 // Ignore everything that is not a PNG file 00278 continue; 00279 else { 00280 // Found a PNG file - the template must be here in the same dir. 00281 icon = filePath; 00282 QFileInfo fi(filePath); 00283 text = fi.baseName(); 00284 templatePath = filePath; // Note that we store the .png file as the template ! 00285 // That's the way it's always been done. Then the app replaces the extension... 00286 } 00287 KoTemplate *t=new KoTemplate(text, description, templatePath, icon, hidden); 00288 groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't 00289 // "touch" the group to avoid useless 00290 // creation of dirs in .kde/blah/... 00291 } 00292 } 00293 } 00294 } 00295 00296 void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group, 00297 const QString &localDir) { 00298 00299 KSimpleConfig config(KoTemplates::stripWhiteSpace(localDir+group->name()+'/'+t->name()+".desktop")); 00300 config.setDesktopGroup(); 00301 config.writeEntry("Type", "Link"); 00302 #if KDE_IS_VERSION(3,1,3) 00303 config.writePathEntry("URL", t->file()); 00304 #else 00305 config.writeEntry("URL", t->file()); 00306 #endif 00307 config.writeEntry("Name", t->name()); 00308 config.writeEntry("Icon", t->picture()); 00309 config.writeEntry("X-KDE-Hidden", t->isHidden()); 00310 } 00311 00312 namespace KoTemplates { 00313 QString stripWhiteSpace(const QString &string) { 00314 00315 QString ret; 00316 for(unsigned int i=0; i<string.length(); ++i) { 00317 QChar tmp(string[i]); 00318 if(!tmp.isSpace()) 00319 ret+=tmp; 00320 } 00321 return ret; 00322 } 00323 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 24 18:22:27 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003