# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kfileview.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 1998 Stephan Kulow <coolo@kde.org>
    00003                   1998 Daniel Grana <grana@ie.iwi.unibe.ch>
    00004                   2001 Carsten Pfeiffer <pfeiffer@kde.org>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public License
    00017     along with this library; see the file COPYING.LIB.  If not, write to
    00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019     Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include <assert.h>
    00023 #include <stdlib.h>
    00024
    00025 #include <kaction.h>
    00026 #include <kapplication.h>
    00027 #include <kdebug.h>
    00028 #include <kglobal.h>
    00029 #include <klocale.h>
    00030 #include <kstandarddirs.h>
    00031
    00032 #include "config-kfile.h"
    00033 #include "kfileview.h"
    00034
    00035 #ifdef Unsorted // the "I hate X.h" modus
    00036 #undef Unsorted
    00037 #endif
    00038
    00039 QDir::SortSpec KFileView::defaultSortSpec = static_cast<QDir::SortSpec>(QDir::Name | QDir::IgnoreCase | QDir::DirsFirst);
    00040
    00041 class KFileView::KFileViewPrivate
    00042 {
    00043 public:
    00044     KFileViewPrivate()
    00045     {
    00046         actions = 0;
    00047     }
    00048
    00049     ~KFileViewPrivate()
    00050     {
    00051         if( actions ) {
    00052             actions->clear();
    00053             delete actions;
    00054         }
    00055     }
    00056
    00057     QGuardedPtr<KActionCollection> actions;
    00058 };
    00059
    00060
    00061 KFileView::KFileView()
    00062 {
    00063     d = new KFileViewPrivate();
    00064     m_sorting  = KFileView::defaultSortSpec;
    00065
    00066     sig = new KFileViewSignaler();
    00067     sig->setName("view-signaller");
    00068
    00069     m_selectedList = 0L;
    00070     filesNumber = 0;
    00071     dirsNumber = 0;
    00072
    00073     view_mode = All;
    00074     selection_mode = KFile::Single;
    00075     m_viewName = i18n("Unknown View");
    00076
    00077     myOnlyDoubleClickSelectsFiles = false;
    00078     m_itemList.setAutoDelete( false ); // just references
    00079 }
    00080
    00081 KFileView::~KFileView()
    00082 {
    00083     delete d;
    00084     delete sig;
    00085     delete m_selectedList;
    00086 }
    00087
    00088 void KFileView::setParentView(KFileView *parent)
    00089 {
    00090     if ( parent ) { // pass all signals right to our parent
    00091         QObject::connect(sig, SIGNAL( activatedMenu(const KFileItem *,
    00092                                                         const QPoint& ) ),
    00093                 parent->sig, SIGNAL( activatedMenu(const KFileItem *,
    00094                                                         const QPoint& )));
    00095         QObject::connect(sig, SIGNAL( dirActivated(const KFileItem *)),
    00096                 parent->sig, SIGNAL( dirActivated(const KFileItem*)));
    00097         QObject::connect(sig, SIGNAL( fileSelected(const KFileItem *)),
    00098                 parent->sig, SIGNAL( fileSelected(const KFileItem*)));
    00099         QObject::connect(sig, SIGNAL( fileHighlighted(const KFileItem *) ),
    00100                             parent->sig,SIGNAL(fileHighlighted(const KFileItem*)));
    00101         QObject::connect(sig, SIGNAL( sortingChanged( QDir::SortSpec ) ),
    00102                             parent->sig, SIGNAL(sortingChanged( QDir::SortSpec)));
    00103     }
    00104 }
    00105
    00106 bool KFileView::updateNumbers(const KFileItem *i)
    00107 {
    00108     if (!( viewMode() & Files ) && i->isFile())
    00109         return false;
    00110
    00111     if (!( viewMode() & Directories ) && i->isDir())
    00112         return false;
    00113
    00114     if (i->isDir())
    00115         dirsNumber++;
    00116     else
    00117         filesNumber++;
    00118
    00119     return true;
    00120 }
    00121
    00122 void qt_qstring_stats();
    00123
    00124 // filter out files if we're in directory mode and count files/directories
    00125 // and insert into the view
    00126 void KFileView::addItemList(const KFileItemList& list)
    00127 {
    00128     KFileItem *tmp;
    00129
    00130     for (KFileItemListIterator it(list); (tmp = it.current()); ++it)
    00131     {
    00132         if (!updateNumbers(tmp))
    00133             continue;
    00134
    00135         insertItem( tmp );
    00136     }
    00137
    00138 #ifdef Q2HELPER
    00139     qt_qstring_stats();
    00140 #endif
    00141 }
    00142
    00143 void KFileView::insertItem( KFileItem * )
    00144 {
    00145 }
    00146
    00147 void KFileView::setSorting(QDir::SortSpec new_sort)
    00148 {
    00149     m_sorting = new_sort;
    00150 }
    00151
    00152 void KFileView::clear()
    00153 {
    00154     m_itemList.clear();
    00155     filesNumber = 0;
    00156     dirsNumber = 0;
    00157     clearView();
    00158 }
    00159
    00160 void KFileView::sortReversed()
    00161 {
    00162     int spec = sorting();
    00163
    00164     setSorting( static_cast<QDir::SortSpec>( spec ^ QDir::Reversed ) );
    00165 }
    00166
    00167 #if 0
    00168 int KFileView::compareItems(const KFileItem *fi1, const KFileItem *fi2) const
    00169 {
    00170     static const QString &dirup = KGlobal::staticQString("..");
    00171     bool bigger = true;
    00172     bool keepFirst = false;
    00173     bool dirsFirst = ((m_sorting & QDir::DirsFirst) == QDir::DirsFirst);
    00174
    00175     if (fi1 == fi2)
    00176         return 0;
    00177
    00178     // .. is always bigger, independent of the sort criteria
    00179     if ( fi1->name() == dirup ) {
    00180         bigger = false;
    00181         keepFirst = dirsFirst;
    00182     }
    00183     else if ( fi2->name() == dirup ) {
    00184         bigger = true;
    00185         keepFirst = dirsFirst;
    00186     }
    00187
    00188     else {
    00189         if ( fi1->isDir() != fi2->isDir() && dirsFirst ) {
    00190             bigger = fi2->isDir();
    00191             keepFirst = true;
    00192         }
    00193         else {
    00194
    00195             QDir::SortSpec sort = static_cast<QDir::SortSpec>(m_sorting & QDir::SortByMask);
    00196
    00197             //if (fi1->isDir() || fi2->isDir())
    00198             // sort = static_cast<QDir::SortSpec>(KFileView::defaultSortSpec & QDir::SortByMask);
    00199
    00200             switch (sort) {
    00201                 case QDir::Name:
    00202                 default:
    00203 sort_by_name:
    00204                     if ( (m_sorting & QDir::IgnoreCase) == QDir::IgnoreCase )
    00205                         bigger = (fi1->name( true ) > fi2->name( true ));
    00206                     else
    00207                         bigger = (fi1->name() > fi2->name());
    00208                     break;
    00209                 case QDir::Time:
    00210                 {
    00211                     time_t t1 = fi1->time( KIO::UDS_MODIFICATION_TIME );
    00212                     time_t t2 = fi2->time( KIO::UDS_MODIFICATION_TIME );
    00213                     if ( t1 != t2 ) {
    00214                         bigger = (t1 > t2);
    00215                         break;
    00216                     }
    00217
    00218                     // Sort by name if both items have the same timestamp.
    00219                     // Don't honor the reverse flag tho.
    00220                     else {
    00221                         keepFirst = true;
    00222                         goto sort_by_name;
    00223                     }
    00224                 }
    00225                 case QDir::Size:
    00226                 {
    00227                     KIO::filesize_t s1 = fi1->size();
    00228                     KIO::filesize_t s2 = fi2->size();
    00229                     if ( s1 != s2 ) {
    00230                         bigger = (s1 > s2);
    00231                         break;
    00232                     }
    00233
    00234                     // Sort by name if both items have the same size.
    00235                     // Don't honor the reverse flag tho.
    00236                     else {
    00237                         keepFirst = true;
    00238                         goto sort_by_name;
    00239                     }
    00240                 }
    00241                 case QDir::Unsorted:
    00242                     bigger = true;  // nothing
    00243                     break;
    00244             }
    00245         }
    00246     }
    00247
    00248     if (reversed && !keepFirst ) // don't reverse dirs to the end!
    00249       bigger = !bigger;
    00250
    00251     return (bigger ? 1 : -1);
    00252 }
    00253 #endif
    00254
    00255 void  KFileView::updateView(bool f)
    00256 {
    00257     widget()->repaint(f);
    00258 }
    00259
    00260 void KFileView::updateView(const KFileItem *)
    00261 {
    00262 }
    00263
    00264 void KFileView::setCurrentItem(const QString &filename )
    00265 {
    00266     if (!filename.isNull()) {
    00267         KFileItem *item;
    00268         for ( (item = firstFileItem()); item; item = nextItem( item ) ) {
    00269             if (item->name() == filename) {
    00270                 setCurrentItem( item );
    00271                 return;
    00272             }
    00273         }
    00274     }
    00275
    00276     kdDebug(kfile_area) << "setCurrentItem: no match found: " << filename << endl;
    00277 }
    00278
    00279 const KFileItemList * KFileView::items() const
    00280 {
    00281     KFileItem *item = 0L;
    00282
    00283     // only ever use m_itemList in this method!
    00284     m_itemList.clear();
    00285     for ( (item = firstFileItem()); item; item = nextItem( item ) )
    00286         m_itemList.append( item );
    00287
    00288     return &m_itemList;
    00289 }
    00290
    00291
    00292 const KFileItemList * KFileView::selectedItems() const
    00293 {
    00294     if ( !m_selectedList )
    00295         m_selectedList = new KFileItemList;
    00296
    00297     m_selectedList->clear();
    00298
    00299     KFileItem *item;
    00300     for ( (item = firstFileItem()); item; item = nextItem( item ) ) {
    00301         if ( isSelected( item ) )
    00302             m_selectedList->append( item );
    00303     }
    00304
    00305     return m_selectedList;
    00306 }
    00307
    00308 void KFileView::selectAll()
    00309 {
    00310     if (selection_mode == KFile::NoSelection || selection_mode== KFile::Single)
    00311         return;
    00312
    00313     KFileItem *item = 0L;
    00314     for ( (item = firstFileItem()); item; item = nextItem( item ) )
    00315         setSelected( item, true );
    00316 }
    00317
    00318
    00319 void KFileView::invertSelection()
    00320 {
    00321     KFileItem *item = 0L;
    00322     for ( (item = firstFileItem()); item; item = nextItem( item ) )
    00323         setSelected( item, !isSelected( item ) );
    00324 }
    00325
    00326
    00327 void KFileView::setSelectionMode( KFile::SelectionMode sm )
    00328 {
    00329     selection_mode = sm;
    00330 }
    00331
    00332 KFile::SelectionMode KFileView::selectionMode() const
    00333 {
    00334     return selection_mode;
    00335 }
    00336
    00337 void KFileView::setViewMode( ViewMode vm )
    00338 {
    00339     view_mode = vm;
    00340 }
    00341
    00342 void KFileView::removeItem( const KFileItem *item )
    00343 {
    00344     if ( !item )
    00345         return;
    00346
    00347     if ( item->isDir() )
    00348         dirsNumber--;
    00349     else
    00350         filesNumber--;
    00351
    00352     if ( m_selectedList )
    00353         m_selectedList->removeRef( item );
    00354 }
    00355
    00356 void KFileView::listingCompleted()
    00357 {
    00358     // empty default impl.
    00359 }
    00360
    00361 KActionCollection * KFileView::actionCollection() const
    00362 {
    00363     if ( !d->actions )
    00364         d->actions = new KActionCollection( widget(), "KFileView::d->actions" );
    00365     return d->actions;
    00366 }
    00367
    00368 void KFileView::readConfig( KConfig *, const QString&  )
    00369 {
    00370 }
    00371
    00372 void KFileView::writeConfig( KConfig *, const QString& )
    00373 {
    00374 }
    00375
    00376 QString KFileView::sortingKey( const QString& value, bool isDir, int sortSpec )
    00377 {
    00378     bool reverse   = sortSpec & QDir::Reversed;
    00379     bool dirsFirst = sortSpec & QDir::DirsFirst;
    00380     char start = (isDir && dirsFirst) ? (reverse ? '2' : '0') : '1';
    00381     QString result = (sortSpec & QDir::IgnoreCase) ? value.lower() : value;
    00382     return result.prepend( start );
    00383 }
    00384
    00385 QString KFileView::sortingKey( KIO::filesize_t value, bool isDir, int sortSpec)
    00386 {
    00387     bool reverse = sortSpec & QDir::Reversed;
    00388     bool dirsFirst = sortSpec & QDir::DirsFirst;
    00389     char start = (isDir && dirsFirst) ? (reverse ? '2' : '0') : '1';
    00390     return KIO::number( value ).rightJustify( 24, '0' ).prepend( start );
    00391 }
    00392
    00393 void KFileView::virtual_hook( int, void* )
    00394 { /*BASE::virtual_hook( id, data );*/ }
    00395
    00396 #include "kfileview.moc"

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:36 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001