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 m_totalFiles++; 00126 //kDebug(7007) << "file -> " << m_totalSize; 00127 } 00128 } else { 00129 m_totalFiles++; 00130 } 00131 } 00132 //kDebug(7007) << "finished"; 00133 q->emitResult(); 00134 } 00135 00136 void DirectorySizeJobPrivate::startNextJob( const KUrl & url ) 00137 { 00138 Q_Q(DirectorySizeJob); 00139 //kDebug(7007) << url; 00140 KIO::ListJob * listJob = KIO::listRecursive( url, KIO::HideProgressInfo ); 00141 q->connect( listJob, SIGNAL(entries( KIO::Job *, const KIO::UDSEntryList& )), 00142 SLOT( slotEntries( KIO::Job*, const KIO::UDSEntryList& ))); 00143 q->addSubjob( listJob ); 00144 } 00145 00146 void DirectorySizeJobPrivate::slotEntries( KIO::Job*, const KIO::UDSEntryList & list ) 00147 { 00148 KIO::UDSEntryList::ConstIterator it = list.begin(); 00149 const KIO::UDSEntryList::ConstIterator end = list.end(); 00150 for (; it != end; ++it) { 00151 00152 const KIO::UDSEntry& entry = *it; 00153 const KIO::filesize_t size = entry.numberValue(KIO::UDSEntry::UDS_SIZE, 0); 00154 const QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME ); 00155 if (name == ".") { 00156 m_totalSize += size; 00157 //kDebug(7007) << "'.': added" << size << "->" << m_totalSize; 00158 } else if (name != "..") { 00159 if (!entry.isLink()) 00160 m_totalSize += size; 00161 if (!entry.isDir()) 00162 m_totalFiles++; 00163 else 00164 m_totalSubdirs++; 00165 //kDebug(7007) << name << ":" << size << "->" << m_totalSize; 00166 } 00167 } 00168 } 00169 00170 void DirectorySizeJob::slotResult( KJob * job ) 00171 { 00172 Q_D(DirectorySizeJob); 00173 //kDebug(7007) << d->m_totalSize; 00174 removeSubjob(job); 00175 if (d->m_currentItem < d->m_lstItems.count()) 00176 { 00177 d->processNextItem(); 00178 } 00179 else 00180 { 00181 if (job->error()) { 00182 setError( job->error() ); 00183 setErrorText( job->errorText() ); 00184 } 00185 emitResult(); 00186 } 00187 } 00188 00189 //static 00190 DirectorySizeJob * KIO::directorySize( const KUrl & directory ) 00191 { 00192 return DirectorySizeJobPrivate::newJob(directory); // useless - but consistent with other jobs 00193 } 00194 00195 //static 00196 DirectorySizeJob * KIO::directorySize( const KFileItemList & lstItems ) 00197 { 00198 return DirectorySizeJobPrivate::newJob(lstItems); 00199 } 00200 00201 #include "directorysizejob.moc"
KDE 4.6 API Reference