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 #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         // ### TODO: use the class KoPicture instead of QImage to support non-image pictures
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; // ### TODO: some people would surely like to have 128x128
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 { // relative path
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         //kdDebug() << "removing :" << myTemplate->fileName() << endl;
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         //kdDebug() << "---------------------------------" << endl;
00153         //kdDebug() << "group: " << group->name() << endl;
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             //kdDebug() << "touched" << endl;
00161             if(!group->isHidden()) {
00162                 //kdDebug() << "not hidden" << endl;
00163                 KStandardDirs::makeDir(localDir+group->name()); // create the local group dir
00164             }
00165             else {
00166                 //kdDebug() << "hidden" << endl;
00167                 if(group->dirs().count()==1 && !group->dirs().grep(localDir).isEmpty()) {
00168                     //kdDebug() << "local only" << endl;
00169                     KIO::NetAccess::del(group->dirs().first(), 0);
00170                     //kdDebug() << "removing: " << group->dirs().first() << endl;
00171                 }
00172                 else {
00173                     //kdDebug() << "global" << endl;
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                 //kdDebug() << "++template: " << t->name() << endl;
00181                 writeTemplate(t, group, localDir);
00182             }
00183             if(t->isHidden() && t->touched() ) {
00184                 //kdDebug() << "+++ delete local template ##############" << endl;
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()); // "...there can be only one..." (Queen)
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         //kdDebug() << "dir: " << *it << endl;
00215         QDir dir(*it);
00216         // avoid the annoying warning
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=="..") // we don't want to check those dirs :)
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                 //kdDebug() << "name: " << name <<endl;
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                 //kdDebug() << "filePath: " << filePath << endl;
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                 // If a desktop file, then read the name from it.
00272                 // Otherwise (or if no name in it?) use file name
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                         //kdDebug() << "name: " << text << endl;
00281                         icon=config.readEntry("Icon");
00282                         if(icon[0]!='/' && // allow absolute paths for icons
00283                            QFile::exists(*it+icon)) // allow icons from icontheme
00284                             icon=*it+icon;
00285                         //kdDebug() << "icon2: " << icon << endl;
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                         // Don't add a template that is for the wrong measure system
00291                         if(measureSystem == dontShow)
00292                             continue;
00293 
00294                         //kdDebug() << "hidden: " << hidden_str << endl;
00295                         templatePath=config.readPathEntry("URL");
00296                         //kdDebug() << "Link to : " << templatePath << endl;
00297                         if(templatePath[0]!='/') {
00298                             if(templatePath.left(6)=="file:/") // I doubt this will happen
00299                                 templatePath=templatePath.right(templatePath.length()-6);
00300                             //else
00301                             //  kdDebug() << "dirname=" << *it << endl;
00302                             templatePath=*it+templatePath;
00303                             //kdDebug() << "templatePath: " << templatePath << endl;
00304                         }
00305                     } else
00306                         continue; // Invalid
00307                 }
00308                 // The else if and the else branch are here for compat. with the old system
00309                 else if ( files[i].right(4) != ".png" )
00310                     // Ignore everything that is not a PNG file
00311                     continue;
00312                 else {
00313                     // Found a PNG file - the template must be here in the same dir.
00314                     icon = filePath;
00315                     QFileInfo fi(filePath);
00316                     text = fi.baseName();
00317                     templatePath = filePath; // Note that we store the .png file as the template !
00318                     // That's the way it's always been done. Then the app replaces the extension...
00319                 }
00320                 KoTemplate *t=new KoTemplate(text, description, templatePath, icon, fileName,
00321                                              measureSystem, hidden);
00322                 groupIt.current()->add(t, false, false); // false -> we aren't a "user", false -> don't
00323                                                          // "touch" the group to avoid useless
00324                                                          // creation of dirs in .kde/blah/...
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         // try to remove the file
00339         if ( QFile::remove(fileName) || !QFile::exists(fileName) )
00340         {
00341             QFile::remove( t->name() );
00342             QFile::remove( t->picture() );
00343             return;
00344         }
00345     }
00346     // be sure that the template's file name is unique so we don't overwrite an other
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys