kio Library API Documentation

kdirsize.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2000 David Faure <faure@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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kdirsize.h"
00021 #include <kdebug.h>
00022 #include <qapplication.h>
00023 #include <qtimer.h>
00024 #include <config-kfile.h>
00025 
00026 using namespace KIO;
00027 
00028 KDirSize::KDirSize( const KURL & directory )
00029     : KIO::Job(false /*No GUI*/), m_bAsync(true), m_totalSize(0L)
00030 {
00031     startNextJob( directory );
00032 }
00033 
00034 KDirSize::KDirSize( const KFileItemList & lstItems )
00035     : KIO::Job(false /*No GUI*/), m_bAsync(true), m_totalSize(0L), m_lstItems(lstItems)
00036 {
00037     QTimer::singleShot( 0, this, SLOT(processList()) );
00038 }
00039 
00040 void KDirSize::processList()
00041 {
00042     while (!m_lstItems.isEmpty())
00043     {
00044         KFileItem * item = m_lstItems.first();
00045         m_lstItems.removeFirst();
00046         if ( !item->isLink() )
00047         {
00048             if ( item->isDir() )
00049             {
00050                 kdDebug(kfile_area) << "KDirSize::processList dir -> listing" << endl;
00051                 KURL url = item->url();
00052                 startNextJob( url );
00053                 return; // we'll come back later, when this one's finished
00054             }
00055             else
00056             {
00057                 m_totalSize += item->size();
00058 // no long long with kdDebug()
00059 //            kdDebug(kfile_area) << "KDirSize::processList file -> " << m_totalSize << endl;
00060             }
00061         }
00062     }
00063     kdDebug(kfile_area) << "KDirSize::processList finished" << endl;
00064     if ( !m_bAsync )
00065         qApp->exit_loop();
00066     emitResult();
00067 }
00068 
00069 void KDirSize::startNextJob( const KURL & url )
00070 {
00071     KIO::ListJob * listJob = KIO::listRecursive( url, false /* no GUI */ );
00072     connect( listJob, SIGNAL(entries( KIO::Job *,
00073                                       const KIO::UDSEntryList& )),
00074              SLOT( slotEntries( KIO::Job*,
00075                                 const KIO::UDSEntryList& )));
00076     addSubjob( listJob );
00077 }
00078 
00079 void KDirSize::slotEntries( KIO::Job*, const KIO::UDSEntryList & list )
00080 {
00081     KIO::UDSEntryListConstIterator it = list.begin();
00082     KIO::UDSEntryListConstIterator end = list.end();
00083     for (; it != end; ++it) {
00084         KIO::UDSEntry::ConstIterator it2 = (*it).begin();
00085         KIO::filesize_t size = 0;
00086         bool isLink = false;
00087         QString name;
00088         for( ; it2 != (*it).end(); it2++ ) {
00089           switch( (*it2).m_uds ) {
00090             case KIO::UDS_NAME:
00091               name = (*it2).m_str;
00092               break;
00093             case KIO::UDS_LINK_DEST:
00094               isLink = !(*it2).m_str.isEmpty();
00095               break;
00096             case KIO::UDS_SIZE:
00097               size = ((*it2).m_long);
00098               break;
00099             default:
00100               break;
00101           }
00102         }
00103         if ( !isLink && name != QString::fromLatin1("..") )
00104         {
00105             m_totalSize += size;
00106             //kdDebug(kfile_area) << name << ":" << size << endl;
00107         }
00108     }
00109 }
00110 
00111 //static
00112 KDirSize * KDirSize::dirSizeJob( const KURL & directory )
00113 {
00114     return new KDirSize( directory ); // useless - but consistent with other jobs
00115 }
00116 
00117 //static
00118 KDirSize * KDirSize::dirSizeJob( const KFileItemList & lstItems )
00119 {
00120     return new KDirSize( lstItems );
00121 }
00122 
00123 //static
00124 KIO::filesize_t KDirSize::dirSize( const KURL & directory )
00125 {
00126     KDirSize * dirSize = dirSizeJob( directory );
00127     dirSize->setSync();
00128     qApp->enter_loop();
00129     return dirSize->totalSize();
00130 }
00131 
00132 
00133 void KDirSize::slotResult( KIO::Job * job )
00134 {
00135     kdDebug(kfile_area) << " KDirSize::slotResult( KIO::Job * job ) m_lstItems:" << m_lstItems.count() << endl;
00136     if ( !m_lstItems.isEmpty() )
00137     {
00138         subjobs.remove(job); // Remove job, but don't kill this job.
00139         processList();
00140     }
00141     else
00142     {
00143         if ( !m_bAsync )
00144             qApp->exit_loop();
00145         KIO::Job::slotResult( job );
00146     }
00147 }
00148 
00149 void KDirSize::virtual_hook( int id, void* data )
00150 { KIO::Job::virtual_hook( id, data ); }
00151 
00152 #include "kdirsize.moc"
KDE Logo
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:25 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001