| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kfilefilter.cpp

    00001 /* This file is part of the KDE libraries
    00002
    00003    Copyright (c) 2001,2002 Carsten Pfeiffer <pfeiffer@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 (LGPL) 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., 59 Temple Place - Suite 330,
    00018    Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qregexp.h>
    00022
    00023 #include <kfileitem.h>
    00024 #include <kglobal.h>
    00025
    00026 #include "kfilefilter.h"
    00027
    00028 KSimpleFileFilter::KSimpleFileFilter()
    00029     : m_filterDotFiles( true ),
    00030       m_filterSpecials( true ),
    00031       m_modeFilter( 0 )
    00032 {
    00033     m_nameFilters.setAutoDelete( true );
    00034 }
    00035
    00036 KSimpleFileFilter::~KSimpleFileFilter()
    00037 {
    00038 }
    00039
    00040 void KSimpleFileFilter::setFilterDotFiles( bool filter )
    00041 {
    00042     m_filterDotFiles = filter;
    00043 }
    00044
    00045 void KSimpleFileFilter::setFilterSpecials( bool filter )
    00046 {
    00047     m_filterSpecials = filter;
    00048 }
    00049
    00050 void KSimpleFileFilter::setNameFilters( const QString& nameFilters )
    00051 {
    00052     // KDE 3.0 defaults
    00053     setNameFilters( nameFilters, false, ' ' );
    00054 }
    00055
    00056 void KSimpleFileFilter::setNameFilters( const QString& nameFilters,
    00057                                         bool caseSensitive,
    00058                                         const QChar& separator )
    00059 {
    00060     m_nameFilters.clear();
    00061
    00062     // Split on white space
    00063     QStringList list = QStringList::split(separator, nameFilters);
    00064
    00065     QStringList::ConstIterator it = list.begin();
    00066     for ( ; it != list.end(); ++it )
    00067         m_nameFilters.append(new QRegExp(*it, caseSensitive, true ));
    00068 }
    00069
    00070 void KSimpleFileFilter::setMimeFilters( const QStringList& mimeFilters )
    00071 {
    00072     m_mimeFilters = mimeFilters;
    00073 }
    00074
    00075 void KSimpleFileFilter::setModeFilter( mode_t mode )
    00076 {
    00077     m_modeFilter = mode;
    00078 }
    00079
    00080 bool KSimpleFileFilter::passesFilter( const KFileItem *item ) const
    00081 {
    00082     static const QString& dot    = KGlobal::staticQString(".");
    00083     static const QString& dotdot = KGlobal::staticQString("..");
    00084
    00085     const QString& name = item->name();
    00086
    00087     if ( m_filterDotFiles && name.at(0) == '.' )
    00088         return false;
    00089
    00090     if ( m_filterSpecials && (name == dot || name == dotdot) )
    00091         return false;
    00092
    00093     if ( m_modeFilter && !(m_modeFilter & item->mode()) )
    00094         return false;
    00095
    00096     if ( !m_mimeFilters.isEmpty() ) {
    00097         // correct or guessed mimetype -- we don't mind
    00098         const QString& mime = item->mimeTypePtr()->name();
    00099         bool ok = false;
    00100
    00101         QStringList::ConstIterator it = m_mimeFilters.begin();
    00102         for ( ; it != m_mimeFilters.end(); ++it ) {
    00103             if ( (*it) == mime ) { // match!
    00104                 ok = true;
    00105                 break;
    00106             }
    00107         }
    00108         if ( !ok )
    00109             return false;
    00110     }
    00111
    00112     if ( !m_nameFilters.isEmpty() ) {
    00113         bool ok = false;
    00114
    00115         QPtrListIterator<QRegExp> it( m_nameFilters );
    00116         for ( ; it.current(); ++it ) {
    00117             if ( it.current()->exactMatch( name ) ) { // match!
    00118                 ok = true;
    00119                 break;
    00120             }
    00121         }
    00122         if ( !ok )
    00123             return false;
    00124     }
    00125
    00126     return true; // passes the filter!
    00127 }
    00128
    00129 void KFileFilter::virtual_hook( int, void* )
    00130 { /*BASE::virtual_hook( id, data );*/ }
    00131
    00132 void KSimpleFileFilter::virtual_hook( int id, void* data )
    00133 { KFileFilter::virtual_hook( id, data ); }
    00134

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:27 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001