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

KIO

directorysizejob.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2000, 2006 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., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "directorysizejob.h"
00021 #include <kdebug.h>
00022 #include <QtCore/QTimer>
00023 
00024 #include "jobuidelegate.h"
00025 #include "job_p.h"
00026 
00027 namespace KIO
00028 {
00029     class DirectorySizeJobPrivate: public KIO::JobPrivate
00030     {
00031     public:
00032         DirectorySizeJobPrivate()
00033             : m_totalSize(0L)
00034             , m_totalFiles(0L)
00035             , m_totalSubdirs(0L)
00036             , m_currentItem(0)
00037         {
00038         }
00039         DirectorySizeJobPrivate( const KFileItemList & lstItems )
00040             : m_totalSize(0L)
00041             , m_totalFiles(0L)
00042             , m_totalSubdirs(0L)
00043             , m_lstItems(lstItems)
00044             , m_currentItem(0)
00045         {
00046         }
00047         KIO::filesize_t m_totalSize;
00048         KIO::filesize_t m_totalFiles;
00049         KIO::filesize_t m_totalSubdirs;
00050         KFileItemList m_lstItems;
00051         int m_currentItem;
00052 
00053         void startNextJob( const KUrl & url );
00054         void slotEntries( KIO::Job * , const KIO::UDSEntryList &);
00055         void processNextItem();
00056 
00057         Q_DECLARE_PUBLIC(DirectorySizeJob)
00058 
00059         static inline DirectorySizeJob *newJob( const KUrl & directory )
00060         {
00061             DirectorySizeJobPrivate *d = new DirectorySizeJobPrivate;
00062             DirectorySizeJob *job = new DirectorySizeJob(*d);
00063             job->setUiDelegate(new JobUiDelegate);
00064             d->startNextJob(directory);
00065             return job;
00066         }
00067 
00068         static inline DirectorySizeJob *newJob( const KFileItemList & lstItems )
00069         {
00070             DirectorySizeJobPrivate *d = new DirectorySizeJobPrivate(lstItems);
00071             DirectorySizeJob *job = new DirectorySizeJob(*d);
00072             job->setUiDelegate(new JobUiDelegate);
00073             QTimer::singleShot( 0, job, SLOT(processNextItem()) );
00074             return job;
00075         }
00076     };
00077 
00078 } // namespace KIO
00079 
00080 
00081 using namespace KIO;
00082 
00083 DirectorySizeJob::DirectorySizeJob(DirectorySizeJobPrivate &dd)
00084     : KIO::Job(dd)
00085 {
00086 }
00087 
00088 DirectorySizeJob::~DirectorySizeJob()
00089 {
00090 }
00091 
00092 KIO::filesize_t DirectorySizeJob::totalSize() const
00093 {
00094     return d_func()->m_totalSize;
00095 }
00096 
00097 KIO::filesize_t DirectorySizeJob::totalFiles() const
00098 {
00099     return d_func()->m_totalFiles;
00100 }
00101 
00102 KIO::filesize_t DirectorySizeJob::totalSubdirs() const
00103 {
00104     return d_func()->m_totalSubdirs;
00105 }
00106 
00107 void DirectorySizeJobPrivate::processNextItem()
00108 {
00109     Q_Q(DirectorySizeJob);
00110     while (m_currentItem < m_lstItems.count())
00111     {
00112         const KFileItem item = m_lstItems[m_currentItem++];
00113     if ( !item.isLink() )
00114     {
00115             if ( item.isDir() )
00116             {
00117                 //kDebug(7007) << "dir -> listing";
00118                 KUrl url = item.url();
00119                 startNextJob( url );
00120                 return; // we'll come back later, when this one's finished
00121             }
00122             else
00123             {
00124                 m_totalSize += item.size();
00125                 //kDebug(7007) << "file -> " << m_totalSize;
00126             }
00127     }
00128     }
00129     //kDebug(7007) << "finished";
00130     q->emitResult();
00131 }
00132 
00133 void DirectorySizeJobPrivate::startNextJob( const KUrl & url )
00134 {
00135     Q_Q(DirectorySizeJob);
00136     //kDebug(7007) << url;
00137     KIO::ListJob * listJob = KIO::listRecursive( url, KIO::HideProgressInfo );
00138     q->connect( listJob, SIGNAL(entries( KIO::Job *, const KIO::UDSEntryList& )),
00139                 SLOT( slotEntries( KIO::Job*, const KIO::UDSEntryList& )));
00140     q->addSubjob( listJob );
00141 }
00142 
00143 void DirectorySizeJobPrivate::slotEntries( KIO::Job*, const KIO::UDSEntryList & list )
00144 {
00145     KIO::UDSEntryList::ConstIterator it = list.begin();
00146     const KIO::UDSEntryList::ConstIterator end = list.end();
00147     for (; it != end; ++it) {
00148 
00149         const KIO::UDSEntry& entry = *it;
00150         const KIO::filesize_t size = entry.numberValue(KIO::UDSEntry::UDS_SIZE, 0);
00151         const QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME );
00152         if (name == ".") {
00153             m_totalSize += size;
00154             //kDebug(7007) << "'.': added" << size << "->" << m_totalSize;
00155         } else if (name != "..") {
00156             if (!entry.isLink())
00157               m_totalSize += size;
00158             if (!entry.isDir())
00159               m_totalFiles++;
00160             else
00161               m_totalSubdirs++;
00162             //kDebug(7007) << name << ":" << size << "->" << m_totalSize;
00163         }
00164     }
00165 }
00166 
00167 void DirectorySizeJob::slotResult( KJob * job )
00168 {
00169     Q_D(DirectorySizeJob);
00170     //kDebug(7007) << d->m_totalSize;
00171     removeSubjob(job);
00172     if (d->m_currentItem < d->m_lstItems.count())
00173     {
00174         d->processNextItem();
00175     }
00176     else
00177     {
00178         if (job->error()) {
00179             setError( job->error() );
00180             setErrorText( job->errorText() );
00181         }
00182         emitResult();
00183     }
00184 }
00185 
00186 //static
00187 DirectorySizeJob * KIO::directorySize( const KUrl & directory )
00188 {
00189     return DirectorySizeJobPrivate::newJob(directory); // useless - but consistent with other jobs
00190 }
00191 
00192 //static
00193 DirectorySizeJob * KIO::directorySize( const KFileItemList & lstItems )
00194 {
00195     return DirectorySizeJobPrivate::newJob(lstItems);
00196 }
00197 
00198 #include "directorysizejob.moc"

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs 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