• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
directorysizejob.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2000, 2006 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "directorysizejob.h"
21#include <kdebug.h>
22#include <QtCore/QTimer>
23
24#include "jobuidelegate.h"
25#include "job_p.h"
26
27namespace KIO
28{
29 class DirectorySizeJobPrivate: public KIO::JobPrivate
30 {
31 public:
32 DirectorySizeJobPrivate()
33 : m_totalSize(0L)
34 , m_totalFiles(0L)
35 , m_totalSubdirs(0L)
36 , m_currentItem(0)
37 {
38 }
39 DirectorySizeJobPrivate( const KFileItemList & lstItems )
40 : m_totalSize(0L)
41 , m_totalFiles(0L)
42 , m_totalSubdirs(0L)
43 , m_lstItems(lstItems)
44 , m_currentItem(0)
45 {
46 }
47 KIO::filesize_t m_totalSize;
48 KIO::filesize_t m_totalFiles;
49 KIO::filesize_t m_totalSubdirs;
50 KFileItemList m_lstItems;
51 int m_currentItem;
52 QHash<long, QSet<long> > m_visitedInodes; // device -> set of inodes
53
54 void startNextJob( const KUrl & url );
55 void slotEntries( KIO::Job * , const KIO::UDSEntryList &);
56 void processNextItem();
57
58 Q_DECLARE_PUBLIC(DirectorySizeJob)
59
60 static inline DirectorySizeJob *newJob( const KUrl & directory )
61 {
62 DirectorySizeJobPrivate *d = new DirectorySizeJobPrivate;
63 DirectorySizeJob *job = new DirectorySizeJob(*d);
64 job->setUiDelegate(new JobUiDelegate);
65 d->startNextJob(directory);
66 return job;
67 }
68
69 static inline DirectorySizeJob *newJob( const KFileItemList & lstItems )
70 {
71 DirectorySizeJobPrivate *d = new DirectorySizeJobPrivate(lstItems);
72 DirectorySizeJob *job = new DirectorySizeJob(*d);
73 job->setUiDelegate(new JobUiDelegate);
74 QTimer::singleShot( 0, job, SLOT(processNextItem()) );
75 return job;
76 }
77 };
78
79} // namespace KIO
80
81
82using namespace KIO;
83
84DirectorySizeJob::DirectorySizeJob(DirectorySizeJobPrivate &dd)
85 : KIO::Job(dd)
86{
87}
88
89DirectorySizeJob::~DirectorySizeJob()
90{
91}
92
93KIO::filesize_t DirectorySizeJob::totalSize() const
94{
95 return d_func()->m_totalSize;
96}
97
98KIO::filesize_t DirectorySizeJob::totalFiles() const
99{
100 return d_func()->m_totalFiles;
101}
102
103KIO::filesize_t DirectorySizeJob::totalSubdirs() const
104{
105 return d_func()->m_totalSubdirs;
106}
107
108void DirectorySizeJobPrivate::processNextItem()
109{
110 Q_Q(DirectorySizeJob);
111 while (m_currentItem < m_lstItems.count())
112 {
113 const KFileItem item = m_lstItems[m_currentItem++];
114 if ( !item.isLink() )
115 {
116 if ( item.isDir() )
117 {
118 //kDebug(7007) << "dir -> listing";
119 KUrl url = item.url();
120 startNextJob( url );
121 return; // we'll come back later, when this one's finished
122 }
123 else
124 {
125 m_totalSize += item.size();
126 m_totalFiles++;
127 //kDebug(7007) << "file -> " << m_totalSize;
128 }
129 } else {
130 m_totalFiles++;
131 }
132 }
133 //kDebug(7007) << "finished";
134 q->emitResult();
135}
136
137void DirectorySizeJobPrivate::startNextJob( const KUrl & url )
138{
139 Q_Q(DirectorySizeJob);
140 //kDebug(7007) << url;
141 KIO::ListJob * listJob = KIO::listRecursive( url, KIO::HideProgressInfo );
142 listJob->addMetaData("details", "3");
143 q->connect( listJob, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
144 SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList)));
145 q->addSubjob( listJob );
146}
147
148void DirectorySizeJobPrivate::slotEntries( KIO::Job*, const KIO::UDSEntryList & list )
149{
150 KIO::UDSEntryList::ConstIterator it = list.begin();
151 const KIO::UDSEntryList::ConstIterator end = list.end();
152 for (; it != end; ++it) {
153
154 const KIO::UDSEntry& entry = *it;
155 const long device = entry.numberValue(KIO::UDSEntry::UDS_DEVICE_ID, 0);
156 if (device) {
157 // Hard-link detection (#67939)
158 const long inode = entry.numberValue(KIO::UDSEntry::UDS_INODE, 0);
159 QSet<long> & visitedInodes = m_visitedInodes[device]; // find or insert
160 if (visitedInodes.contains(inode)) {
161 continue;
162 }
163 visitedInodes.insert(inode);
164 }
165 const KIO::filesize_t size = entry.numberValue(KIO::UDSEntry::UDS_SIZE, 0);
166 const QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME );
167 if (name == ".") {
168 m_totalSize += size;
169 //kDebug(7007) << "'.': added" << size << "->" << m_totalSize;
170 } else if (name != "..") {
171 if (!entry.isLink())
172 m_totalSize += size;
173 if (!entry.isDir())
174 m_totalFiles++;
175 else
176 m_totalSubdirs++;
177 //kDebug(7007) << name << ":" << size << "->" << m_totalSize;
178 }
179 }
180}
181
182void DirectorySizeJob::slotResult( KJob * job )
183{
184 Q_D(DirectorySizeJob);
185 //kDebug(7007) << d->m_totalSize;
186 removeSubjob(job);
187 if (d->m_currentItem < d->m_lstItems.count())
188 {
189 d->processNextItem();
190 }
191 else
192 {
193 if (job->error()) {
194 setError( job->error() );
195 setErrorText( job->errorText() );
196 }
197 emitResult();
198 }
199}
200
201//static
202DirectorySizeJob * KIO::directorySize( const KUrl & directory )
203{
204 return DirectorySizeJobPrivate::newJob(directory); // useless - but consistent with other jobs
205}
206
207//static
208DirectorySizeJob * KIO::directorySize( const KFileItemList & lstItems )
209{
210 return DirectorySizeJobPrivate::newJob(lstItems);
211}
212
213#include "directorysizejob.moc"
KFileItemList
List of KFileItems, which adds a few helper methods to QList<KFileItem>.
Definition: kfileitem.h:675
KFileItem
A KFileItem is a generic class to handle a file, local or remote.
Definition: kfileitem.h:46
KFileItem::size
KIO::filesize_t size() const
Returns the size of the file, if known.
Definition: kfileitem.cpp:610
KFileItem::isLink
bool isLink() const
Returns true if this item represents a link in the UNIX sense of a link.
Definition: kfileitem.cpp:1567
KFileItem::isDir
bool isDir() const
Returns true if this item represents a directory.
Definition: kfileitem.cpp:1141
KFileItem::url
KUrl url() const
Returns the url of the file.
Definition: kfileitem.cpp:1543
KIO::DirectorySizeJob
Computes a directory size (similar to "du", but doesn't give the same results since we simply sum up ...
Definition: directorysizejob.h:36
KIO::DirectorySizeJob::totalSize
KIO::filesize_t totalSize() const
Definition: directorysizejob.cpp:93
KIO::DirectorySizeJob::DirectorySizeJob
DirectorySizeJob(DirectorySizeJobPrivate &dd)
Definition: directorysizejob.cpp:84
KIO::DirectorySizeJob::totalFiles
KIO::filesize_t totalFiles() const
Definition: directorysizejob.cpp:98
KIO::DirectorySizeJob::~DirectorySizeJob
~DirectorySizeJob()
Definition: directorysizejob.cpp:89
KIO::DirectorySizeJob::slotResult
virtual void slotResult(KJob *job)
Definition: directorysizejob.cpp:182
KIO::DirectorySizeJob::totalSubdirs
KIO::filesize_t totalSubdirs() const
Definition: directorysizejob.cpp:103
KIO::JobPrivate
Definition: job_p.h:40
KIO::JobUiDelegate
A UI delegate tuned to be used with KIO Jobs.
Definition: jobuidelegate.h:40
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:94
KIO::Job::removeSubjob
virtual bool removeSubjob(KJob *job)
Mark a sub job as being done.
Definition: job.cpp:118
KIO::Job::addMetaData
void addMetaData(const QString &key, const QString &value)
Add key/value pair to the meta data that is sent to the slave.
Definition: job.cpp:264
KIO::ListJob
A ListJob is allows you to get the get the content of a directory.
Definition: jobclasses.h:936
KIO::UDSEntry
Universal Directory Service.
Definition: udsentry.h:59
KIO::UDSEntry::stringValue
QString stringValue(uint field) const
Definition: udsentry.cpp:73
KIO::UDSEntry::numberValue
long long numberValue(uint field, long long defaultValue=0) const
Definition: udsentry.cpp:78
KIO::UDSEntry::isLink
bool isLink() const
Definition: udsentry.cpp:89
KIO::UDSEntry::UDS_SIZE
@ UDS_SIZE
Size of the file.
Definition: udsentry.h:144
KIO::UDSEntry::UDS_DEVICE_ID
@ UDS_DEVICE_ID
Device number for this file, used to detect hardlinks.
Definition: udsentry.h:258
KIO::UDSEntry::UDS_NAME
@ UDS_NAME
Filename - as displayed in directory listings etc.
Definition: udsentry.h:163
KIO::UDSEntry::UDS_INODE
@ UDS_INODE
Inode number for this file, used to detect hardlinks.
Definition: udsentry.h:261
KIO::UDSEntry::isDir
bool isDir() const
Definition: udsentry.cpp:84
KJob
KJob::setErrorText
void setErrorText(const QString &errorText)
KJob::emitResult
void emitResult()
KJob::error
int error() const
KJob::setError
void setError(int errorCode)
KJob::errorText
QString errorText() const
KJob::setUiDelegate
void setUiDelegate(KJobUiDelegate *delegate)
KUrl
QHash
QList< UDSEntry >
QSet
directorysizejob.h
job_p.h
jobuidelegate.h
kdebug.h
KIO
A namespace for KIO globals.
Definition: kbookmarkmenu.h:55
KIO::listRecursive
ListJob * listRecursive(const KUrl &url, JobFlags flags=DefaultFlags, bool includeHidden=true)
The same as the previous method, but recurses subdirectories.
Definition: job.cpp:2740
KIO::directorySize
DirectorySizeJob * directorySize(const KUrl &directory)
Computes a directory size (by doing a recursive listing).
Definition: directorysizejob.cpp:202
KIO::HideProgressInfo
@ HideProgressInfo
Hide progress information dialog, i.e.
Definition: jobclasses.h:51
KIO::filesize_t
qulonglong filesize_t
64-bit file size
Definition: global.h:57
KRecentDirs::list
QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:60
name
const char * name(StandardAction id)
end
const KShortcut & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal