• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

strigiconfigfile.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Nepomuk KDE project.
00003  * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "strigiconfigfile.h"
00022 #include "nepomukstrigi-config.h"
00023 
00024 #include <QtCore/QFile>
00025 #include <QtCore/QTextStream>
00026 #include <QtCore/QDir>
00027 #include <QtXml/QDomDocument>
00028 #include <QtXml/QDomElement>
00029 
00030 #include <KDebug>
00031 #include <KStandardDirs>
00032 
00033 
00034 namespace {
00035     bool convertBooleanXsdValue( const QString& value ) {
00036         return(  value.toLower() == QLatin1String( "true" ) ||
00037                  value.toLower() == QLatin1String( "1" ) );
00038     }
00039 }
00040 
00041 
00042 Nepomuk::StrigiConfigFile::StrigiConfigFile()
00043 {
00044     reset();
00045 }
00046 
00047 
00048 Nepomuk::StrigiConfigFile::StrigiConfigFile( const QString& filename )
00049 {
00050     reset();
00051     setFilename( filename );
00052 }
00053 
00054 
00055 Nepomuk::StrigiConfigFile::~StrigiConfigFile()
00056 {
00057 }
00058 
00059 
00060 void Nepomuk::StrigiConfigFile::setFilename( const QString& filename )
00061 {
00062     m_filename = filename;
00063 }
00064 
00065 
00066 void Nepomuk::StrigiConfigFile::reset()
00067 {
00068     m_useDBus = true;
00069     m_repositories.clear();
00070     m_excludeFilters.clear();
00071     m_includeFilters.clear();
00072 }
00073 
00074 
00075 bool Nepomuk::StrigiConfigFile::load()
00076 {
00077     kDebug() << m_filename;
00078 
00079     QFile file( m_filename );
00080     if ( file.open( QIODevice::ReadOnly ) ) {
00081         QDomDocument doc;
00082         if ( doc.setContent( &file ) ) {
00083             return readConfig( doc.documentElement() );
00084         }
00085         else {
00086             kDebug() << "Failed to parse" << m_filename;
00087             return false;
00088         }
00089     }
00090     else {
00091         kDebug() << "Could not open file" << m_filename;
00092         return false;
00093     }
00094 }
00095 
00096 
00097 bool Nepomuk::StrigiConfigFile::readConfig( const QDomElement& rootElement )
00098 {
00099     if ( rootElement.tagName() != "strigiDaemonConfiguration" ) {
00100         kDebug() << "Invalid configuration root tag:" << rootElement.tagName();
00101         return false;
00102     }
00103 
00104     m_useDBus = convertBooleanXsdValue( rootElement.attribute( "useDBus", QLatin1String( "1" ) ) );
00105 
00106     // read repository
00107     QDomElement repoElem = rootElement.firstChildElement( "repository" );
00108     while ( !repoElem.isNull() ) {
00109         Repository repo = readRepositoryConfig( repoElem );
00110         if ( repo.isValid() )
00111             addRepository( repo );
00112         repoElem = repoElem.nextSiblingElement( "repository" );
00113     }
00114 
00115     // read filters
00116     return readFilterConfig( rootElement.firstChildElement( "filters" ) );
00117 }
00118 
00119 
00120 Nepomuk::StrigiConfigFile::Repository Nepomuk::StrigiConfigFile::readRepositoryConfig( const QDomElement& repositoryElement )
00121 {
00122     Repository repo;
00123 
00124     QDomNamedNodeMap attributes = repositoryElement.attributes();
00125 
00126     // read repository configuration
00127     for ( int i = 0; i < attributes.size(); ++i ) {
00128         QDomNode attributeNode = attributes.item( i );
00129         QString attributeName = attributeNode.nodeName();
00130         QString attributeValue = attributeNode.nodeValue();
00131         if ( attributeName == "type" ) {
00132             repo.setType( attributeValue );
00133         }
00134         else if ( attributeName == "name" ) {
00135             repo.setName( attributeValue );
00136         }
00137         else if ( attributeName == "indexdir" ) {
00138             repo.setIndexDir( attributeValue );
00139         }
00140         else if ( attributeName == "writeable" ) {
00141             repo.setWriteable( convertBooleanXsdValue( attributeValue ) );
00142         }
00143         else if ( attributeName == "urlbase" ) {
00144             repo.setUrlBase( attributeValue );
00145         }
00146         else if ( attributeName == "pollingInterval" ) {
00147             repo.setPollingInterval( attributeValue.toInt() );
00148         }
00149         else {
00150             kDebug() << "Unknown config entry" << attributeName;
00151         }
00152     }
00153 
00154     // read indexed dirs
00155     QDomElement pathElement = repositoryElement.firstChildElement( "path" );
00156     while ( !pathElement.isNull() ) {
00157         QString path = pathElement.attribute( "path" );
00158         if ( !path.isEmpty() )
00159             repo.addIndexedDirectory( path );
00160         pathElement = pathElement.nextSiblingElement( "path" );
00161     }
00162 
00163     return repo;
00164 }
00165 
00166 
00167 bool Nepomuk::StrigiConfigFile::readFilterConfig( const QDomElement& filtersElement )
00168 {
00169     QDomElement filterElement = filtersElement.firstChildElement( "filter" );
00170 
00171     while ( !filterElement.isNull() ) {
00172         QString pattern = filterElement.attribute( "pattern" );
00173         QString inExclude = filterElement.attribute( "include" );
00174         if ( !pattern.isEmpty() && !inExclude.isEmpty() ) {
00175             if ( convertBooleanXsdValue( inExclude ) )
00176                 m_includeFilters << pattern;
00177             else
00178                 m_excludeFilters << pattern;
00179         }
00180         else {
00181             kDebug() << "Invalid filter rule.";
00182             return false;
00183         }
00184 
00185         filterElement = filterElement.nextSiblingElement( "filter" );
00186     }
00187 
00188     return true;
00189 }
00190 
00191 
00192 bool Nepomuk::StrigiConfigFile::save()
00193 {
00194     kDebug() << m_filename;
00195 
00196     QDomDocument doc;
00197     QDomElement rootElement = doc.createElement( "strigiDaemonConfiguration" );
00198     rootElement.setAttribute( "useDBus", useDBus() ? QLatin1String( "1" ) : QLatin1String( "0" ) );
00199     doc.appendChild( rootElement );
00200 
00201     // save repositories
00202     foreach( const Repository &repo, m_repositories ) {
00203         QDomElement repoElem = doc.createElement( "repository" );
00204         repoElem.setAttribute( "name", repo.name() );
00205         repoElem.setAttribute( "type", repo.type() );
00206         repoElem.setAttribute( "indexdir", repo.indexDir() );
00207         repoElem.setAttribute( "writeable", repo.writeable() ? QLatin1String( "1" ) : QLatin1String( "0" ) );
00208         repoElem.setAttribute( "urlbase", repo.urlBase() );
00209         repoElem.setAttribute( "pollingInterval", QString::number( repo.pollingInterval() ) );
00210 
00211         // add paths
00212         foreach( const QString &path, repo.indexedDirectories() ) {
00213             QDomElement pathElem = doc.createElement( "path" );
00214             pathElem.setAttribute( "path", path );
00215             repoElem.appendChild( pathElem );
00216         }
00217 
00218         rootElement.appendChild( repoElem );
00219     }
00220 
00221     // save filters
00222     QDomElement filtersElem = doc.createElement( "filters" );
00223     rootElement.appendChild( filtersElem );
00224     foreach( const QString &filter, m_includeFilters ) {
00225         QDomElement filterElem = doc.createElement( "filter" );
00226         filterElem.setAttribute( "pattern", filter );
00227         filterElem.setAttribute( "include", "1" );
00228         filtersElem.appendChild( filterElem );
00229     }
00230     foreach( const QString &filter, m_excludeFilters ) {
00231         QDomElement filterElem = doc.createElement( "filter" );
00232         filterElem.setAttribute( "pattern", filter );
00233         filterElem.setAttribute( "include", "0" );
00234         filtersElem.appendChild( filterElem );
00235     }
00236 
00237     // save to file
00238     KStandardDirs::makeDir( m_filename.section( '/', 0, -2 ) );
00239     QFile f( m_filename );
00240     if ( f.open( QIODevice::WriteOnly ) ) {
00241         QTextStream s( &f );
00242         s << doc;
00243         return true;
00244     }
00245     else {
00246         kDebug() << "Could not open" << m_filename << "for writing";
00247         return false;
00248     }
00249 }
00250 
00251 
00252 bool Nepomuk::StrigiConfigFile::useDBus() const
00253 {
00254     return m_useDBus;
00255 }
00256 
00257 
00258 QStringList Nepomuk::StrigiConfigFile::excludeFilters() const
00259 {
00260     return m_excludeFilters;
00261 }
00262 
00263 
00264 QStringList Nepomuk::StrigiConfigFile::includeFilters() const
00265 {
00266     return m_includeFilters;
00267 }
00268 
00269 
00270 QList<Nepomuk::StrigiConfigFile::Repository> Nepomuk::StrigiConfigFile::repositories() const
00271 {
00272     return m_repositories;
00273 }
00274 
00275 
00276 Nepomuk::StrigiConfigFile::Repository& Nepomuk::StrigiConfigFile::defaultRepository()
00277 {
00278     if ( m_repositories.isEmpty() ) {
00279         Repository repo;
00280         repo.setName( "localhost" ); // the default repository
00281         repo.setWriteable( true );
00282         repo.setPollingInterval( 180 ); // default value copied from Strigi sources
00283 #ifdef HAVE_STRIGI_SOPRANO_BACKEND
00284         repo.setType( "sopranobackend" ); // our default
00285 #else
00286         repo.setType( "clucene" );
00287         repo.setIndexDir( QDir::homePath() + "/.strigi/clucene" );
00288 #endif
00289         repo.addIndexedDirectory( QDir::homePath() );
00290         repo.addIndexedDirectory( QDir::homePath() + "/.kde" );
00291         addRepository( repo );
00292 
00293         // in case there are no repositories and no filters we also create
00294         // default filters
00295         if ( m_includeFilters.isEmpty() && m_excludeFilters.isEmpty() ) {
00296             // exclude hidden dirs and files
00297             m_excludeFilters << ".*/" << ".*" << "*~" << "*.part";
00298         }
00299     }
00300 
00301     return m_repositories.first();
00302 }
00303 
00304 
00305 const Nepomuk::StrigiConfigFile::Repository& Nepomuk::StrigiConfigFile::defaultRepository() const
00306 {
00307     return const_cast<StrigiConfigFile*>( this )->defaultRepository();
00308 }
00309 
00310 
00311 void Nepomuk::StrigiConfigFile::setUseDBus( bool b )
00312 {
00313     m_useDBus = b;
00314 }
00315 
00316 void Nepomuk::StrigiConfigFile::setExcludeFilters( const QStringList& filters )
00317 {
00318     m_excludeFilters = filters;
00319 }
00320 
00321 
00322 void Nepomuk::StrigiConfigFile::addExcludeFilter( const QString& filter )
00323 {
00324     m_excludeFilters << filter;
00325 }
00326 
00327 
00328 void Nepomuk::StrigiConfigFile::setIncludeFilters( const QStringList& filters )
00329 {
00330     m_includeFilters = filters;
00331 }
00332 
00333 
00334 void Nepomuk::StrigiConfigFile::addInludeFilter( const QString& filter )
00335 {
00336     m_includeFilters << filter;
00337 }
00338 
00339 void Nepomuk::StrigiConfigFile::setRepositories( const QList<Repository>& repos )
00340 {
00341     m_repositories = repos;
00342 }
00343 
00344 
00345 void Nepomuk::StrigiConfigFile::addRepository( const Repository& repo )
00346 {
00347     m_repositories << repo;
00348 }
00349 
00350 
00351 QString Nepomuk::StrigiConfigFile::defaultStrigiConfigFilePath()
00352 {
00353     return QDir::homePath() + "/.strigi/daemon.conf";
00354 }
00355 
00356 
00357 QTextStream& operator<<( QTextStream& s, const Nepomuk::StrigiConfigFile& scf )
00358 {
00359     s << "useDBus: " << scf.useDBus() << endl
00360       << "repositories:" << endl;
00361     foreach( const Nepomuk::StrigiConfigFile::Repository &repo, scf.repositories() ) {
00362         s << "   " << repo.name() << ":" << endl
00363           << "      " << "type: " << repo.type() << endl
00364           << "      " << "indexdir: " << repo.indexDir() << endl
00365           << "      " << "writeable: " << repo.writeable() << endl
00366           << "      " << "urlbase: " << repo.urlBase() << endl
00367           << "      " << "paths: " << repo.indexedDirectories().join( ":" ) << endl;
00368     }
00369     s << "include filters:" << endl;
00370     foreach( const QString &filter, scf.includeFilters() ) {
00371         s << "   " << filter << endl;
00372     }
00373     s << "exclude filters:" << endl;
00374     foreach( const QString &filter, scf.excludeFilters() ) {
00375         s << "   " << filter << endl;
00376     }
00377     return s;
00378 }
00379 
00380 #include "strigiconfigfile.moc"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal