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

KIO

  • sources
  • kde-4.14
  • kdelibs
  • 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 
27 namespace 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 
82 using namespace KIO;
83 
84 DirectorySizeJob::DirectorySizeJob(DirectorySizeJobPrivate &dd)
85  : KIO::Job(dd)
86 {
87 }
88 
89 DirectorySizeJob::~DirectorySizeJob()
90 {
91 }
92 
93 KIO::filesize_t DirectorySizeJob::totalSize() const
94 {
95  return d_func()->m_totalSize;
96 }
97 
98 KIO::filesize_t DirectorySizeJob::totalFiles() const
99 {
100  return d_func()->m_totalFiles;
101 }
102 
103 KIO::filesize_t DirectorySizeJob::totalSubdirs() const
104 {
105  return d_func()->m_totalSubdirs;
106 }
107 
108 void 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 
137 void 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 
148 void 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 
182 void 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
202 DirectorySizeJob * KIO::directorySize( const KUrl & directory )
203 {
204  return DirectorySizeJobPrivate::newJob(directory); // useless - but consistent with other jobs
205 }
206 
207 //static
208 DirectorySizeJob * KIO::directorySize( const KFileItemList & lstItems )
209 {
210  return DirectorySizeJobPrivate::newJob(lstItems);
211 }
212 
213 #include "directorysizejob.moc"
KIO::DirectorySizeJob::~DirectorySizeJob
~DirectorySizeJob()
Definition: directorysizejob.cpp:89
KIO::filesize_t
qulonglong filesize_t
64-bit file size
Definition: global.h:57
KIO::UDSEntry::isLink
bool isLink() const
Definition: udsentry.cpp:89
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
KFileItem::isDir
bool isDir() const
Returns true if this item represents a directory.
Definition: kfileitem.cpp:1141
kdebug.h
KCompositeJob::emitResult
void emitResult()
KIO::DirectorySizeJob
Computes a directory size (similar to "du", but doesn't give the same results since we simply sum up ...
Definition: directorysizejob.h:35
KCompositeJob::setUiDelegate
void setUiDelegate(KJobUiDelegate *delegate)
KIO::UDSEntry::UDS_DEVICE_ID
Device number for this file, used to detect hardlinks.
Definition: udsentry.h:258
KIO::UDSEntry
Universal Directory Service.
Definition: udsentry.h:58
KIO::ListJob
A ListJob is allows you to get the get the content of a directory.
Definition: jobclasses.h:936
KIO::HideProgressInfo
Hide progress information dialog, i.e.
Definition: jobclasses.h:51
KCompositeJob::setError
void setError(int errorCode)
directorysizejob.h
name
const char * name(StandardAction id)
KIO::DirectorySizeJob::totalSize
KIO::filesize_t totalSize() const
Definition: directorysizejob.cpp:93
KIO::UDSEntry::isDir
bool isDir() const
Definition: udsentry.cpp:84
KIO::Job::removeSubjob
virtual bool removeSubjob(KJob *job)
Mark a sub job as being done.
Definition: job.cpp:118
QSet::insert
const_iterator insert(const T &value)
KUrl
KCompositeJob::setErrorText
void setErrorText(const QString &errorText)
KIO::UDSEntry::numberValue
long long numberValue(uint field, long long defaultValue=0) const
Definition: udsentry.cpp:78
QHash
KIO::JobUiDelegate
A UI delegate tuned to be used with KIO Jobs.
Definition: jobuidelegate.h:39
KFileItemList
List of KFileItems, which adds a few helper methods to QList.
Definition: kfileitem.h:674
KIO::UDSEntry::stringValue
QString stringValue(uint field) const
Definition: udsentry.cpp:73
QSet
QString
QList< UDSEntry >
KFileItem::size
KIO::filesize_t size() const
Returns the size of the file, if known.
Definition: kfileitem.cpp:610
KIO::DirectorySizeJob::totalSubdirs
KIO::filesize_t totalSubdirs() const
Definition: directorysizejob.cpp:103
KIO::UDSEntry::UDS_INODE
Inode number for this file, used to detect hardlinks.
Definition: udsentry.h:261
jobuidelegate.h
QList::end
iterator end()
KFileItem::isLink
bool isLink() const
Returns true if this item represents a link in the UNIX sense of a link.
Definition: kfileitem.cpp:1567
QSet::contains
bool contains(const T &value) const
job_p.h
KIO::DirectorySizeJob::slotResult
virtual void slotResult(KJob *job)
Definition: directorysizejob.cpp:182
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::UDSEntry::UDS_NAME
Filename - as displayed in directory listings etc.
Definition: udsentry.h:163
QList< UDSEntry >::ConstIterator
typedef ConstIterator
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:94
KIO::DirectorySizeJob::totalFiles
KIO::filesize_t totalFiles() const
Definition: directorysizejob.cpp:98
KIO::directorySize
DirectorySizeJob * directorySize(const KUrl &directory)
Computes a directory size (by doing a recursive listing).
Definition: directorysizejob.cpp:202
KIO::JobPrivate
Definition: job_p.h:39
end
const KShortcut & end()
KJob
KIO::UDSEntry::UDS_SIZE
Size of the file.
Definition: udsentry.h:144
QList::begin
iterator begin()
KFileItem::url
KUrl url() const
Returns the url of the file.
Definition: kfileitem.cpp:1543
KIO::DirectorySizeJob::DirectorySizeJob
DirectorySizeJob(DirectorySizeJobPrivate &dd)
Definition: directorysizejob.cpp:84
KFileItem
A KFileItem is a generic class to handle a file, local or remote.
Definition: kfileitem.h:45
KRecentDirs::list
QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:60
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:52 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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