00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "kdirsize.h"
00021
#include <kdebug.h>
00022
#include <kglobal.h>
00023
#include <qapplication.h>
00024
#include <qtimer.h>
00025
#include <config-kfile.h>
00026
00027
using namespace KIO;
00028
00029 KDirSize::KDirSize(
const KURL & directory )
00030 : KIO::Job(false ), m_bAsync(true), m_totalSize(0L), m_totalFiles(0L), m_totalSubdirs(0L)
00031 {
00032 startNextJob( directory );
00033 }
00034
00035 KDirSize::KDirSize(
const KFileItemList & lstItems )
00036 : KIO::Job(false ), m_bAsync(true), m_totalSize(0L), m_totalFiles(0L), m_totalSubdirs(0L), m_lstItems(lstItems)
00037 {
00038 QTimer::singleShot( 0,
this, SLOT(processList()) );
00039 }
00040
00041
void KDirSize::processList()
00042 {
00043
while (!m_lstItems.isEmpty())
00044 {
00045
KFileItem * item = m_lstItems.first();
00046 m_lstItems.removeFirst();
00047
if ( !item->
isLink() )
00048 {
00049
if ( item->
isDir() )
00050 {
00051
kdDebug(kfile_area) <<
"KDirSize::processList dir -> listing" <<
endl;
00052
KURL url = item->
url();
00053 startNextJob( url );
00054
return;
00055 }
00056
else
00057 {
00058 m_totalSize += item->
size();
00059
00060
00061 }
00062 }
00063 }
00064
kdDebug(kfile_area) <<
"KDirSize::processList finished" <<
endl;
00065
if ( !m_bAsync )
00066 qApp->exit_loop();
00067
emitResult();
00068 }
00069
00070
void KDirSize::startNextJob(
const KURL & url )
00071 {
00072
KIO::ListJob * listJob =
KIO::listRecursive( url,
false );
00073 connect( listJob, SIGNAL(entries(
KIO::Job *,
00074
const KIO::UDSEntryList& )),
00075 SLOT( slotEntries(
KIO::Job*,
00076
const KIO::UDSEntryList& )));
00077
addSubjob( listJob );
00078 m_totalSubdirs--;
00079 }
00080
00081
void KDirSize::slotEntries(
KIO::Job*,
const KIO::UDSEntryList & list )
00082 {
00083
static const QString& dotdot =
KGlobal::staticQString(
".." );
00084
KIO::UDSEntryListConstIterator it = list.begin();
00085
KIO::UDSEntryListConstIterator end = list.end();
00086
for (; it !=
end; ++it) {
00087 KIO::UDSEntry::ConstIterator it2 = (*it).begin();
00088
KIO::filesize_t size = 0;
00089
bool isLink =
false;
00090
bool isDir =
false;
00091
QString name;
00092
for( ; it2 != (*it).end(); it2++ ) {
00093
switch( (*it2).m_uds ) {
00094
case KIO::UDS_NAME:
00095
name = (*it2).m_str;
00096
break;
00097
case KIO::UDS_LINK_DEST:
00098 isLink = !(*it2).m_str.isEmpty();
00099
break;
00100
case KIO::UDS_SIZE:
00101 size = ((*it2).m_long);
00102
break;
00103
case KIO::UDS_FILE_TYPE:
00104 isDir = S_ISDIR((*it2).m_long);
00105
break;
00106
default:
00107
break;
00108 }
00109 }
00110
if (
name != dotdot )
00111 {
00112
if (!isLink)
00113 m_totalSize += size;
00114
if (!isDir)
00115 m_totalFiles++;
00116
else
00117 m_totalSubdirs++;
00118
00119 }
00120 }
00121 }
00122
00123
00124 KDirSize *
KDirSize::dirSizeJob(
const KURL & directory )
00125 {
00126
return new KDirSize( directory );
00127 }
00128
00129
00130 KDirSize *
KDirSize::dirSizeJob(
const KFileItemList & lstItems )
00131 {
00132
return new KDirSize( lstItems );
00133 }
00134
00135
00136 KIO::filesize_t KDirSize::dirSize(
const KURL & directory )
00137 {
00138
KDirSize *
dirSize =
dirSizeJob( directory );
00139 dirSize->
setSync();
00140 qApp->enter_loop();
00141
return dirSize->
totalSize();
00142 }
00143
00144
00145 void KDirSize::slotResult(
KIO::Job * job )
00146 {
00147
kdDebug(kfile_area) <<
" KDirSize::slotResult( KIO::Job * job ) m_lstItems:" << m_lstItems.count() <<
endl;
00148
if ( !m_lstItems.isEmpty() )
00149 {
00150 subjobs.remove(job);
00151 processList();
00152 }
00153
else
00154 {
00155
if ( !m_bAsync )
00156 qApp->exit_loop();
00157
KIO::Job::slotResult( job );
00158 }
00159 }
00160
00161
void KDirSize::virtual_hook(
int id,
void* data )
00162 { KIO::Job::virtual_hook(
id, data ); }
00163
00164
#include "kdirsize.moc"