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

NepomukDaemons

folderselectionmodel.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE Project
00002    Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
00003 
00004    Based on CollectionSetup.cpp from the Amarok project
00005    (C) 2003 Scott Wheeler <wheeler@kde.org>
00006    (C) 2004 Max Howell <max.howell@methylblue.com>
00007    (C) 2004 Mark Kretschmann <markey@web.de>
00008    (C) 2008 Seb Ruiz <ruiz@kde.org>
00009    (C) 2008 Sebastian Trueg <trueg@kde.org>
00010 
00011    This library is free software; you can redistribute it and/or
00012    modify it under the terms of the GNU Library General Public
00013    License version 2 as published by the Free Software Foundation.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023    Boston, MA 02110-1301, USA.
00024 */
00025 
00026 #include "folderselectionmodel.h"
00027 
00028 #include <QtCore/QDir>
00029 #include <QtCore/QFileInfo>
00030 #include <QtGui/QColor>
00031 #include <QtGui/QBrush>
00032 #include <QtGui/QPalette>
00033 
00034 #include <KDebug>
00035 #include <KIcon>
00036 #include <KLocale>
00037 
00038 
00039 FolderSelectionModel::FolderSelectionModel( QObject* parent )
00040     : QFileSystemModel( parent )
00041 {
00042     setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );
00043 }
00044 
00045 
00046 FolderSelectionModel::~FolderSelectionModel()
00047 {
00048 }
00049 
00050 
00051 Qt::ItemFlags FolderSelectionModel::flags( const QModelIndex &index ) const
00052 {
00053     Qt::ItemFlags flags = QFileSystemModel::flags( index );
00054     flags |= Qt::ItemIsUserCheckable;
00055     if( isForbiddenPath( filePath( index ) ) )
00056         flags ^= Qt::ItemIsEnabled; //disabled!
00057     return flags;
00058 }
00059 
00060 
00061 QVariant FolderSelectionModel::data( const QModelIndex& index, int role ) const
00062 {
00063     if( index.isValid() && index.column() == 0 ) {
00064         if( role == Qt::CheckStateRole ) {
00065             const IncludeState state = includeState( index );
00066             switch( state ) {
00067             case StateNone:
00068             case StateExclude:
00069             case StateExcludeInherited:
00070                 return Qt::Unchecked;
00071             case StateInclude:
00072             case StateIncludeInherited:
00073                 return Qt::Checked;
00074             }
00075         }
00076         else if( role == IncludeStateRole ) {
00077             return includeState( index );
00078         }
00079         else if( role == Qt::ForegroundRole ) {
00080             IncludeState state = includeState( index );
00081             QBrush brush = QFileSystemModel::data( index, role ).value<QBrush>();
00082             switch( state ) {
00083             case StateInclude:
00084             case StateIncludeInherited:
00085 //                brush = QPalette().brush( QPalette::Disabled, QPalette::Foreground );
00086                 break;
00087             case StateNone:
00088             case StateExclude:
00089             case StateExcludeInherited:
00090                 brush = QPalette().brush( QPalette::Disabled, QPalette::Foreground );
00091                 break;
00092             }
00093             return QVariant::fromValue( brush );
00094         }
00095         else if ( role == Qt::ToolTipRole ) {
00096             IncludeState state = includeState( index );
00097             if ( state == StateInclude || state == StateIncludeInherited ) {
00098                 return i18nc( "@info:tooltip %1 is the path of the folder in a listview",
00099                               "<filename>%1</filename><nl/>(will be indexed for desktop search)", filePath( index ) );
00100             }
00101             else {
00102                 return i18nc( "@info:tooltip %1 is the path of the folder in a listview",
00103                               "<filename>%1</filename><nl/> (will <emphasis>not</emphasis> be indexed for desktop search)", filePath( index ) );
00104             }
00105         }
00106         else if ( role == Qt::DecorationRole ) {
00107             if ( filePath( index ) == QDir::homePath() ) {
00108                 return KIcon( "user-home" );
00109             }
00110         }
00111     }
00112 
00113     return QFileSystemModel::data( index, role );
00114 }
00115 
00116 
00117 bool FolderSelectionModel::setData( const QModelIndex& index, const QVariant& value, int role )
00118 {
00119     if( index.isValid() && index.column() == 0 && role == Qt::CheckStateRole ) {
00120         const QString path = filePath( index );
00121         const IncludeState state = includeState( path );
00122 
00123         // here we ignore the check value, we treat it as a toggle
00124         // This is due to our using the Qt checking system in a virtual way
00125         if( state == StateInclude ||
00126             state == StateIncludeInherited ) {
00127             excludePath( path );
00128             return true;
00129         }
00130         else {
00131             includePath( path );
00132             return true;
00133         }
00134         return false;
00135     }
00136 
00137     return QFileSystemModel::setData( index, value, role );
00138 }
00139 
00140 
00141 namespace {
00142     void removeSubDirs( const QString& path, QSet<QString>& set ) {
00143         QSet<QString>::iterator it = set.begin();
00144         while( it != set.end() ) {
00145             if( it->startsWith( path ) )
00146                 it = set.erase( it );
00147             else
00148                 ++it;
00149         }
00150     }
00151 
00152     QModelIndex findLastLeaf( const QModelIndex& index, FolderSelectionModel* model ) {
00153         int rows = model->rowCount( index );
00154         if( rows > 0 ) {
00155             return findLastLeaf( model->index( rows-1, 0, index ), model );
00156         }
00157         else {
00158             return index;
00159         }
00160     }
00161 }
00162 
00163 void FolderSelectionModel::includePath( const QString& path )
00164 {
00165     if( !m_included.contains( path ) ) {
00166         // remove all subdirs
00167         removeSubDirs( path, m_included );
00168         removeSubDirs( path, m_excluded );
00169         m_excluded.remove( path );
00170 
00171         // only really include if the parent is not already included
00172         if( includeState( path ) != StateIncludeInherited ) {
00173             m_included.insert( path );
00174         }
00175         emit dataChanged( index( path ), findLastLeaf( index( path ), this ) );
00176     }
00177 }
00178 
00179 
00180 void FolderSelectionModel::excludePath( const QString& path )
00181 {
00182     if( !m_excluded.contains( path ) ) {
00183         // remove all subdirs
00184         removeSubDirs( path, m_included );
00185         removeSubDirs( path, m_excluded );
00186         m_included.remove( path );
00187 
00188         // only really exclude the path if a parent is included
00189         if( includeState( path ) == StateIncludeInherited ) {
00190             m_excluded.insert( path );
00191         }
00192         emit dataChanged( index( path ), findLastLeaf( index( path ), this ) );
00193     }
00194 }
00195 
00196 
00197 void FolderSelectionModel::setFolders( const QStringList& includeDirs, const QStringList& excludeDirs )
00198 {
00199     m_included = QSet<QString>::fromList( includeDirs );
00200     m_excluded = QSet<QString>::fromList( excludeDirs );
00201     reset();
00202 }
00203 
00204 
00205 QStringList FolderSelectionModel::includeFolders() const
00206 {
00207     return m_included.toList();
00208 }
00209 
00210 
00211 QStringList FolderSelectionModel::excludeFolders() const
00212 {
00213     return m_excluded.toList();
00214 }
00215 
00216 
00217 inline bool FolderSelectionModel::isForbiddenPath( const QString& path ) const
00218 {
00219     // we need the trailing slash otherwise we could forbid "/dev-music" for example
00220     QString _path = path.endsWith( "/" ) ? path : path + "/";
00221     QFileInfo fi( _path );
00222     return( _path.startsWith( "/proc/" ) ||
00223             _path.startsWith( "/dev/" ) ||
00224             _path.startsWith( "/sys/" ) ||
00225             !fi.isReadable() ||
00226             !fi.isExecutable() );
00227 }
00228 
00229 
00230 FolderSelectionModel::IncludeState FolderSelectionModel::includeState( const QModelIndex& index ) const
00231 {
00232     return includeState( filePath( index ) );
00233 }
00234 
00235 
00236 FolderSelectionModel::IncludeState FolderSelectionModel::includeState( const QString& path ) const
00237 {
00238     if( m_included.contains( path ) ) {
00239         return StateInclude;
00240     }
00241     else if( m_excluded.contains( path ) ) {
00242         return StateExclude;
00243     }
00244     else {
00245         QString parent = path.section( QDir::separator(), 0, -2, QString::SectionSkipEmpty|QString::SectionIncludeLeadingSep );
00246         if( parent.isEmpty() ) {
00247             return StateNone;
00248         }
00249         else {
00250             IncludeState state = includeState( parent );
00251             if( state == StateNone )
00252                 return StateNone;
00253             else if( state == StateInclude || state == StateIncludeInherited )
00254                 return StateIncludeInherited;
00255             else
00256                 return StateExcludeInherited;
00257         }
00258     }
00259 }
00260 
00261 #include "folderselectionmodel.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.7
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