00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <KoTemplates.h>
00021
00022 #include <qdir.h>
00023 #include <qimage.h>
00024 #include <qprinter.h>
00025
00026 #include <kdesktopfile.h>
00027 #include <ksimpleconfig.h>
00028 #include <kdebug.h>
00029 #include <kdeversion.h>
00030 #include <kinstance.h>
00031 #include <ksavefile.h>
00032 #include <kstandarddirs.h>
00033 #include <kiconloader.h>
00034 #include <kio/netaccess.h>
00035 #include <klocale.h>
00036
00037 #include <stdlib.h>
00038
00039
00040 KoTemplate::KoTemplate(const QString &name, const QString &description, const QString &file,
00041 const QString &picture, const QString &fileName, const QString &_measureSystem,
00042 bool hidden, bool touched) :
00043 m_name(name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
00044 m_hidden(hidden), m_touched(touched), m_cached(false), m_measureSystem(_measureSystem)
00045 {
00046 }
00047
00048 const QPixmap &KoTemplate::loadPicture( KInstance* instance ) {
00049
00050 if(m_cached)
00051 return m_pixmap;
00052 m_cached=true;
00053 if ( m_picture[ 0 ] == '/' )
00054 {
00055
00056 QImage img( m_picture );
00057 if (img.isNull()) {
00058 kdWarning() << "Couldn't find icon " << m_picture << endl;
00059 m_pixmap=QPixmap();
00060 return m_pixmap;
00061 }
00062 const int maxHeightWidth = 128;
00063 if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
00064 img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
00065 }
00066 m_pixmap.convertFromImage(img);
00067 return m_pixmap;
00068 } else {
00069 m_pixmap = instance->iconLoader()->loadIcon( m_picture, KIcon::Desktop, 128 );
00070 return m_pixmap;
00071 }
00072 }
00073
00074
00075 KoTemplateGroup::KoTemplateGroup(const QString &name, const QString &dir,
00076 int _sortingWeight, bool touched) :
00077 m_name(name), m_touched(touched), m_sortingWeight(_sortingWeight)
00078 {
00079 m_dirs.append(dir);
00080 m_templates.setAutoDelete(true);
00081 }
00082
00083 bool KoTemplateGroup::isHidden() const {
00084
00085 QPtrListIterator<KoTemplate> it(m_templates);
00086 bool hidden=true;
00087 while(it.current()!=0L && hidden) {
00088 hidden=it.current()->isHidden();
00089 ++it;
00090 }
00091 return hidden;
00092 }
00093
00094 void KoTemplateGroup::setHidden(bool hidden) const {
00095
00096 QPtrListIterator<KoTemplate> it(m_templates);
00097 for( ; it.current()!=0L; ++it)
00098 it.current()->setHidden(hidden);
00099 m_touched=true;
00100 }
00101
00102 bool KoTemplateGroup::add(KoTemplate *t, bool force, bool touch) {
00103
00104 KoTemplate *myTemplate=find(t->name());
00105 if(myTemplate==0L) {
00106 m_templates.append(t);
00107 m_touched=touch;
00108 return true;
00109 }
00110 else if(myTemplate && force) {
00111
00112 QFile::remove( myTemplate->fileName() );
00113 QFile::remove( myTemplate->picture() );
00114 QFile::remove( myTemplate->file() );
00115 m_templates.removeRef(myTemplate);
00116 m_templates.append(t);
00117 m_touched=touch;
00118 return true;
00119 }
00120 return false;
00121 }
00122
00123 KoTemplate *KoTemplateGroup::find(const QString &name) const {
00124
00125 QPtrListIterator<KoTemplate> it(m_templates);
00126 while(it.current() && it.current()->name()!=name)
00127 ++it;
00128 return it.current();
00129 }
00130
00131
00132 KoTemplateTree::KoTemplateTree(const QCString &templateType,
00133 KInstance *instance, bool readTree) :
00134 m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
00135 m_defaultTemplate(0L) {
00136
00137 m_groups.setAutoDelete(true);
00138 if(readTree)
00139 readTemplateTree();
00140 }
00141
00142 void KoTemplateTree::readTemplateTree() {
00143
00144 readGroups();
00145 readTemplates();
00146 }
00147
00148 void KoTemplateTree::writeTemplateTree() {
00149 QString localDir=m_instance->dirs()->saveLocation(m_templateType);
00150
00151 for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) {
00152
00153
00154
00155 bool touched=false;
00156 for(KoTemplate *t=group->first(); t!=0L && !touched && !group->touched(); t=group->next())
00157 touched=t->touched();
00158
00159 if(group->touched() || touched) {
00160
00161 if(!group->isHidden()) {
00162
00163 KStandardDirs::makeDir(localDir+group->name());
00164 }
00165 else {
00166
00167 if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
00168
00169 KIO::NetAccess::del(group->dirs().first(), 0);
00170
00171 }
00172 else {
00173
00174 KStandardDirs::makeDir(localDir+group->name());
00175 }
00176 }
00177 }
00178 for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
00179 if(t->touched()) {
00180
00181 writeTemplate(t, group, localDir);
00182 }
00183 if(t->isHidden() && t->touched() ) {
00184
00185 writeTemplate(t, group, localDir);
00186 QFile::remove(t->file());
00187 QFile::remove(t->picture());
00188 }
00189 }
00190 }
00191 }
00192
00193 void KoTemplateTree::add(KoTemplateGroup *g) {
00194
00195 KoTemplateGroup *group=find(g->name());
00196 if(group==0L)
00197 m_groups.append(g);
00198 else
00199 group->addDir(g->dirs().first());
00200 }
00201
00202 KoTemplateGroup *KoTemplateTree::find(const QString &name) const {
00203
00204 QPtrListIterator<KoTemplateGroup> it(m_groups);
00205 while(it.current() && it.current()->name()!=name)
00206 ++it;
00207 return it.current();
00208 }
00209
00210 void KoTemplateTree::readGroups() {
00211
00212 QStringList dirs = m_instance->dirs()->resourceDirs(m_templateType);
00213 for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
00214
00215 QDir dir(*it);
00216
00217 if(!dir.exists())
00218 continue;
00219 dir.setFilter(QDir::Dirs);
00220 QStringList templateDirs=dir.entryList();
00221 for(QStringList::ConstIterator tdirIt=templateDirs.begin(); tdirIt!=templateDirs.end(); ++tdirIt) {
00222 if(*tdirIt=="." || *tdirIt=="..")
00223 continue;
00224 QDir templateDir(*it+*tdirIt);
00225 QString name=*tdirIt;
00226 QString defaultTab;
00227 int sortingWeight = 1000;
00228 if(templateDir.exists(".directory")) {
00229 KSimpleConfig config(templateDir.absPath()+"/.directory", true);
00230 config.setDesktopGroup();
00231 name=config.readEntry("Name");
00232 defaultTab=config.readEntry("X-KDE-DefaultTab");
00233 sortingWeight=config.readNumEntry("X-KDE-SortingWeight", 1000);
00234
00235 }
00236 KoTemplateGroup *g=new KoTemplateGroup(name, *it+*tdirIt+QChar('/'), sortingWeight);
00237 add(g);
00238 if(defaultTab=="true")
00239 m_defaultGroup=g;
00240 }
00241 }
00242 }
00243
00244 void KoTemplateTree::readTemplates() {
00245 QString dontShow = "imperial";
00246
00247 if(KGlobal::locale()->pageSize() == QPrinter::Letter) {
00248 dontShow = "metric";
00249 }
00250
00251 QPtrListIterator<KoTemplateGroup> groupIt(m_groups);
00252 for( ; groupIt.current()!=0L; ++groupIt) {
00253 QStringList dirs=groupIt.current()->dirs();
00254 for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
00255 QDir d(*it);
00256 if( !d.exists() )
00257 continue;
00258 QStringList files=d.entryList( QDir::Files | QDir::Readable, QDir::Name );
00259 for(unsigned int i=0; i<files.count(); ++i) {
00260 QString filePath = *it + files[i];
00261
00262 QString icon;
00263 QString text;
00264 QString description;
00265 QString hidden_str;
00266 QString fileName;
00267 bool hidden=false;
00268 bool defaultTemplate = false;
00269 QString templatePath;
00270 QString measureSystem;
00271
00272
00273 if (KDesktopFile::isDesktopFile(filePath)) {
00274 KSimpleConfig config(filePath, true);
00275 config.setDesktopGroup();
00276 if (config.readEntry("Type")=="Link") {
00277 text=config.readEntry("Name");
00278 fileName=filePath;
00279 description=config.readEntry("Comment");
00280
00281 icon=config.readEntry("Icon");
00282 if(icon[0]!='/' &&
00283 QFile::exists(*it+icon))
00284 icon=*it+icon;
00285
00286 hidden=config.readBoolEntry("X-KDE-Hidden", false);
00287 defaultTemplate = config.readBoolEntry("X-KDE-DefaultTemplate", false);
00288 measureSystem=config.readEntry("X-KDE-MeasureSystem").lower();
00289
00290
00291 if(measureSystem == dontShow)
00292 continue;
00293
00294
00295 templatePath=config.readPathEntry("URL");
00296
00297 if(templatePath[0]!='/') {
00298 if(templatePath.left(6)=="file:/")
00299 templatePath=templatePath.right(templatePath.length()-6);
00300
00301
00302 templatePath=*it+templatePath;
00303
00304 }
00305 } else
00306 continue;
00307 }
00308
00309 else if ( files[i].right(4) != ".png" )
00310
00311 continue;
00312 else {
00313
00314 icon = filePath;
00315 QFileInfo fi(filePath);
00316 text = fi.baseName();
00317 templatePath = filePath;
00318
00319 }
00320 KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName,
00321 measureSystem, hidden);
00322 groupIt.current()->add(t, false, false);
00323
00324
00325 if ( defaultTemplate )
00326 m_defaultTemplate = t;
00327 }
00328 }
00329 }
00330 }
00331
00332 void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
00333 const QString &localDir) {
00334 QString fileName;
00335 if ( t->isHidden() )
00336 {
00337 fileName = t->fileName();
00338
00339 if ( QFile::remove(fileName) || !QFile::exists(fileName) )
00340 {
00341 QFile::remove( t->name() );
00342 QFile::remove( t->picture() );
00343 return;
00344 }
00345 }
00346
00347 QString const path = localDir + group->name() + '/';
00348 QString const name = KoTemplates::stripWhiteSpace( t->name() );
00349 fileName = path + name + ".desktop";
00350 if ( t->isHidden() && QFile::exists(fileName) )
00351 return;
00352 QString fill;
00353 while ( KIO::NetAccess::exists( fileName, true, 0 ) )
00354 {
00355 fill += '_';
00356 fileName = path + fill + name + ".desktop";
00357 }
00358
00359 KSimpleConfig config( fileName );
00360 config.setDesktopGroup();
00361 config.writeEntry("Type", "Link");
00362 config.writePathEntry("URL", t->file());
00363 config.writeEntry("Name", t->name());
00364 config.writeEntry("Icon", t->picture());
00365 config.writeEntry("X-KDE-Hidden", t->isHidden());
00366 }
00367
00368 namespace KoTemplates {
00369 QString stripWhiteSpace(const QString &string) {
00370
00371 QString ret;
00372 for(unsigned int i=0; i<string.length(); ++i) {
00373 QChar tmp(string[i]);
00374 if(!tmp.isSpace())
00375 ret+=tmp;
00376 }
00377 return ret;
00378 }
00379 }