lib

KoTemplates.cpp

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., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, 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 <ksavefile.h>
00031 #include <kstandarddirs.h>
00032 #include <kiconloader.h>
00033 #include <kio/netaccess.h>
00034 
00035 #include <stdlib.h>
00036 
00037 
00038 KoTemplate::KoTemplate(const QString &name, const QString &description, const QString &file,
00039                        const QString &picture, const QString &fileName, const QString &_measureSystem,
00040                        bool hidden, bool touched) :
00041     m_name(name), m_descr(description), m_file(file), m_picture(picture), m_fileName(fileName),
00042     m_hidden(hidden), m_touched(touched), m_cached(false), m_measureSystem(_measureSystem)
00043 {
00044 }
00045 
00046 const QPixmap &KoTemplate::loadPicture( KInstance* instance ) {
00047 
00048     if(m_cached)
00049         return m_pixmap;
00050     m_cached=true;
00051     if ( m_picture[ 0 ] == '/' )
00052     {
00053         // ### TODO: use the class KoPicture instead of QImage to support non-image pictures
00054         QImage img( m_picture );
00055         if (img.isNull()) {
00056             kdWarning() << "Couldn't find icon " << m_picture << endl;
00057             m_pixmap=QPixmap();
00058             return m_pixmap;
00059         }
00060         const int maxHeightWidth = 128; // ### TODO: some people would surely like to have 128x128
00061         if (img.width() > maxHeightWidth || img.height() > maxHeightWidth) {
00062             img = img.smoothScale( maxHeightWidth, maxHeightWidth, QImage::ScaleMax );
00063         }
00064         m_pixmap.convertFromImage(img);
00065         return m_pixmap;
00066     } else { // relative path
00067         m_pixmap = instance->iconLoader()->loadIcon( m_picture, KIcon::Desktop, 128 );
00068         return m_pixmap;
00069     }
00070 }
00071 
00072 
00073 KoTemplateGroup::KoTemplateGroup(const QString &name, const QString &dir,
00074                                  int _sortingWeight, bool touched) :
00075     m_name(name), m_touched(touched), m_sortingWeight(_sortingWeight)
00076 {
00077     m_dirs.append(dir);
00078     m_templates.setAutoDelete(true);
00079 }
00080 
00081 bool KoTemplateGroup::isHidden() const {
00082 
00083     QPtrListIterator<KoTemplate> it(m_templates);
00084     bool hidden=true;
00085     while(it.current()!=0L && hidden) {
00086         hidden=it.current()->isHidden();
00087         ++it;
00088     }
00089     return hidden;
00090 }
00091 
00092 void KoTemplateGroup::setHidden(bool hidden) const {
00093 
00094     QPtrListIterator<KoTemplate> it(m_templates);
00095     for( ; it.current()!=0L; ++it)
00096         it.current()->setHidden(hidden);
00097     m_touched=true;
00098 }
00099 
00100 bool KoTemplateGroup::add(KoTemplate *t, bool force, bool touch) {
00101 
00102     KoTemplate *myTemplate=find(t->name());
00103     if(myTemplate==0L) {
00104         m_templates.append(t);
00105         m_touched=touch;
00106         return true;
00107     }
00108     else if(myTemplate && force) {
00109         //kdDebug() << "removing :" << myTemplate->fileName() << endl;
00110         QFile::remove( myTemplate->fileName()  );
00111         QFile::remove( myTemplate->picture() );
00112         QFile::remove( myTemplate->file() );
00113         m_templates.removeRef(myTemplate);
00114         m_templates.append(t);
00115         m_touched=touch;
00116         return true;
00117     }
00118     return false;
00119 }
00120 
00121 KoTemplate *KoTemplateGroup::find(const QString &name) const {
00122 
00123     QPtrListIterator<KoTemplate> it(m_templates);
00124     while(it.current() && it.current()->name()!=name)
00125         ++it;
00126     return it.current();
00127 }
00128 
00129 
00130 KoTemplateTree::KoTemplateTree(const QCString &templateType,
00131                                KInstance *instance, bool readTree) :
00132     m_templateType(templateType), m_instance(instance), m_defaultGroup(0L),
00133     m_defaultTemplate(0L) {
00134 
00135     m_groups.setAutoDelete(true);
00136     if(readTree)
00137         readTemplateTree();
00138 }
00139 
00140 void KoTemplateTree::readTemplateTree() {
00141 
00142     readGroups();
00143     readTemplates();
00144 }
00145 
00146 void KoTemplateTree::writeTemplateTree() {
00147     QString localDir=m_instance->dirs()->saveLocation(m_templateType);
00148 
00149     for(KoTemplateGroup *group=m_groups.first(); group!=0L; group=m_groups.next()) {
00150         //kdDebug() << "---------------------------------" << endl;
00151         //kdDebug() << "group: " << group->name() << endl;
00152 
00153         bool touched=false;
00154         for(KoTemplate *t=group->first(); t!=0L && !touched && !group->touched(); t=group->next())
00155             touched=t->touched();
00156 
00157         if(group->touched() || touched) {
00158             //kdDebug() << "touched" << endl;
00159             if(!group->isHidden()) {
00160                 //kdDebug() << "not hidden" << endl;
00161                 KStandardDirs::makeDir(localDir+group->name()); // create the local group dir
00162             }
00163             else {
00164                 //kdDebug() << "hidden" << endl;
00165                 if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
00166                     //kdDebug() << "local only" << endl;
00167                     KIO::NetAccess::del(group->dirs().first(), 0);
00168                     //kdDebug() << "removing: " << group->dirs().first() << endl;
00169                 }
00170                 else {
00171                     //kdDebug() << "global" << endl;
00172                     KStandardDirs::makeDir(localDir+group->name());
00173                 }
00174             }
00175         }
00176         for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
00177             if(t->touched()) {
00178                 //kdDebug() << "++template: " << t->name() << endl;
00179                 writeTemplate(t, group, localDir);
00180             }
00181             if(t->isHidden() && t->touched() ) {
00182                 //kdDebug() << "+++ delete local template ##############" << endl;
00183                 writeTemplate(t, group, localDir);
00184                 QFile::remove(t->file());
00185                 QFile::remove(t->picture());
00186             }
00187         }
00188     }
00189 }
00190 
00191 void KoTemplateTree::add(KoTemplateGroup *g) {
00192 
00193     KoTemplateGroup *group=find(g->name());
00194     if(group==0L)
00195         m_groups.append(g);
00196     else
00197         group->addDir(g->dirs().first()); // "...there can be only one..." (Queen)
00198 }
00199 
00200 KoTemplateGroup *KoTemplateTree::find(const QString &name) const {
00201 
00202     QPtrListIterator<KoTemplateGroup> it(m_groups);
00203     while(it.current() && it.current()->name()!=name)
00204         ++it;
00205     return it.current();
00206 }
00207 
00208 void KoTemplateTree::readGroups() {
00209 
00210     QStringList dirs = m_instance->dirs()->resourceDirs(m_templateType);
00211     for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
00212         //kdDebug() << "dir: " << *it << endl;
00213         QDir dir(*it);
00214         // avoid the annoying warning
00215         if(!dir.exists())
00216             continue;
00217         dir.setFilter(QDir::Dirs);
00218         QStringList templateDirs=dir.entryList();
00219         for(QStringList::ConstIterator tdirIt=templateDirs.begin(); tdirIt!=templateDirs.end(); ++tdirIt) {
00220             if(*tdirIt=="." || *tdirIt=="..") // we don't want to check those dirs :)
00221                 continue;
00222             QDir templateDir(*it+*tdirIt);
00223             QString name=*tdirIt;
00224             QString defaultTab;
00225             int sortingWeight = 1000;
00226             if(templateDir.exists(".directory")) {
00227                 KSimpleConfig config(templateDir.absPath()+"/.directory", true);
00228                 config.setDesktopGroup();
00229                 name=config.readEntry("Name");
00230                 defaultTab=config.readEntry("X-KDE-DefaultTab");
00231                 sortingWeight=config.readNumEntry("X-KDE-SortingWeight", 1000);
00232                 //kdDebug() << "name: " << name <<endl;
00233             }
00234             KoTemplateGroup *g=new KoTemplateGroup(name, *it+*tdirIt+QChar('/'), sortingWeight);
00235             add(g);
00236             if(defaultTab=="true")
00237                 m_defaultGroup=g;
00238         }
00239     }
00240 }
00241 
00242 void KoTemplateTree::readTemplates() {
00243 
00244     QPtrListIterator<KoTemplateGroup> groupIt(m_groups);
00245     for( ; groupIt.current()!=0L; ++groupIt) {
00246         QStringList dirs=groupIt.current()->dirs();
00247         for(QStringList::ConstIterator it=dirs.begin(); it!=dirs.end(); ++it) {
00248             QDir d(*it);
00249             if( !d.exists() )
00250                 continue;
00251             QStringList files=d.entryList( QDir::Files | QDir::Readable, QDir::Name );
00252             for(unsigned int i=0; i<files.count(); ++i) {
00253                 QString filePath = *it + files[i];
00254                 //kdDebug() << "filePath: " << filePath << endl;
00255                 QString icon;
00256                 QString text;
00257                 QString description;
00258                 QString hidden_str;
00259                 QString fileName;
00260                 bool hidden=false;
00261                 bool defaultTemplate = false;
00262                 QString templatePath;
00263                 QString measureSystem;
00264                 // If a desktop file, then read the name from it.
00265                 // Otherwise (or if no name in it?) use file name
00266                 if (KDesktopFile::isDesktopFile(filePath)) {
00267                     KSimpleConfig config(filePath, true);
00268                     config.setDesktopGroup();
00269                     if (config.readEntry("Type")=="Link") {
00270                         text=config.readEntry("Name");
00271                         fileName=filePath;
00272                         description=config.readEntry("Comment");
00273                         //kdDebug() << "name: " << text << endl;
00274                         icon=config.readEntry("Icon");
00275                         if(icon[0]!='/' && // allow absolute paths for icons
00276                            QFile::exists(*it+icon)) // allow icons from icontheme
00277                             icon=*it+icon;
00278                         //kdDebug() << "icon2: " << icon << endl;
00279                         hidden=config.readBoolEntry("X-KDE-Hidden", false);
00280                         defaultTemplate = config.readBoolEntry("X-KDE-DefaultTemplate", false);
00281                         measureSystem=config.readEntry("X-KDE-MeasureSystem").lower();
00282                         //kdDebug() << "hidden: " << hidden_str << endl;
00283                         templatePath=config.readPathEntry("URL");
00284                         //kdDebug() << "Link to : " << templatePath << endl;
00285                         if(templatePath[0]!='/') {
00286                             if(templatePath.left(6)=="file:/") // I doubt this will happen
00287                                 templatePath=templatePath.right(templatePath.length()-6);
00288                             //else
00289                             //  kdDebug() << "dirname=" << *it << endl;
00290                             templatePath=*it+templatePath;
00291                             //kdDebug() << "templatePath: " << templatePath << endl;
00292                         }
00293                     } else
00294                         continue; // Invalid
00295                 }
00296                 // The else if and the else branch are here for compat. with the old system
00297                 else if ( files[i].right(4) != ".png" )
00298                     // Ignore everything that is not a PNG file
00299                     continue;
00300                 else {
00301                     // Found a PNG file - the template must be here in the same dir.
00302                     icon = filePath;
00303                     QFileInfo fi(filePath);
00304                     text = fi.baseName();
00305                     templatePath = filePath; // Note that we store the .png file as the template !
00306                     // That's the way it's always been done. Then the app replaces the extension...
00307                 }
00308                 KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName,
00309                                              measureSystem, hidden);
00310                 groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't
00311                                                          // "touch" the group to avoid useless
00312                                                          // creation of dirs in .kde/blah/...
00313                 if ( defaultTemplate )
00314                     m_defaultTemplate = t;
00315             }
00316         }
00317     }
00318 }
00319 
00320 void KoTemplateTree::writeTemplate(KoTemplate *t, KoTemplateGroup *group,
00321                                    const QString &localDir) {
00322     QString fileName;
00323     if ( t->isHidden() )
00324     {
00325         fileName = t->fileName();
00326         // try to remove the file
00327         if ( QFile::remove(fileName) || !QFile::exists(fileName) )
00328         {
00329             QFile::remove( t->name() );
00330             QFile::remove( t->picture() );
00331             return;
00332         }
00333     }
00334     // be sure that the template's file name is unique so we don't overwrite an other
00335     QString const path = localDir + group->name() + '/';
00336     QString const name = KoTemplates::stripWhiteSpace( t->name() );
00337     fileName = path + name + ".desktop";
00338     if ( t->isHidden() && QFile::exists(fileName) )
00339         return;
00340     QString fill;
00341     while ( KIO::NetAccess::exists( fileName, true, 0 ) )
00342     {
00343         fill += '_';
00344         fileName = path + fill + name + ".desktop";
00345     }
00346 
00347     KSimpleConfig config( fileName );
00348     config.setDesktopGroup();
00349     config.writeEntry("Type", "Link");
00350     config.writePathEntry("URL", t->file());
00351     config.writeEntry("Name", t->name());
00352     config.writeEntry("Icon", t->picture());
00353     config.writeEntry("X-KDE-Hidden", t->isHidden());
00354 }
00355 
00356 namespace KoTemplates {
00357 QString stripWhiteSpace(const QString &string) {
00358 
00359     QString ret;
00360     for(unsigned int i=0; i<string.length(); ++i) {
00361         QChar tmp(string[i]);
00362         if(!tmp.isSpace())
00363             ret+=tmp;
00364     }
00365     return ret;
00366 }
00367 }
KDE Home | KDE Accessibility Home | Description of Access Keys