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

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • kio
kdirlister.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3  2000 Carsten Pfeiffer <pfeiffer@kde.org>
4  2003-2005 David Faure <faure@kde.org>
5  2001-2006 Michael Brade <brade@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "kdirlister.h"
24 #include "kdirlister_p.h"
25 
26 #include <QtCore/QRegExp>
27 
28 #include <kdebug.h>
29 #include <kde_file.h>
30 #include <klocale.h>
31 #include <kio/job.h>
32 #include <kio/jobuidelegate.h>
33 #include <kmessagebox.h>
34 #include "kprotocolmanager.h"
35 #include "kmountpoint.h"
36 
37 #include <QFile>
38 
39 // Enable this to get printDebug() called often, to see the contents of the cache
40 //#define DEBUG_CACHE
41 
42 // Make really sure it doesn't get activated in the final build
43 #ifdef NDEBUG
44 #undef DEBUG_CACHE
45 #endif
46 
47 K_GLOBAL_STATIC(KDirListerCache, kDirListerCache)
48 
49 KDirListerCache::KDirListerCache()
50  : itemsCached( 10 ) // keep the last 10 directories around
51 {
52  //kDebug(7004);
53 
54  connect( &pendingUpdateTimer, SIGNAL(timeout()), this, SLOT(processPendingUpdates()) );
55  pendingUpdateTimer.setSingleShot( true );
56 
57  connect( KDirWatch::self(), SIGNAL(dirty(QString)),
58  this, SLOT(slotFileDirty(QString)) );
59  connect( KDirWatch::self(), SIGNAL(created(QString)),
60  this, SLOT(slotFileCreated(QString)) );
61  connect( KDirWatch::self(), SIGNAL(deleted(QString)),
62  this, SLOT(slotFileDeleted(QString)) );
63 
64  kdirnotify = new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
65  connect(kdirnotify, SIGNAL(FileRenamed(QString,QString)), SLOT(slotFileRenamed(QString,QString)));
66  connect(kdirnotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
67  connect(kdirnotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
68  connect(kdirnotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
69 
70  // The use of KUrl::url() in ~DirItem (sendSignal) crashes if the static for QRegExpEngine got deleted already,
71  // so we need to destroy the KDirListerCache before that.
72  qAddPostRoutine(kDirListerCache.destroy);
73 }
74 
75 KDirListerCache::~KDirListerCache()
76 {
77  //kDebug(7004);
78 
79  qDeleteAll(itemsInUse);
80  itemsInUse.clear();
81 
82  itemsCached.clear();
83  directoryData.clear();
84 
85  if ( KDirWatch::exists() )
86  KDirWatch::self()->disconnect( this );
87 }
88 
89 // setting _reload to true will emit the old files and
90 // call updateDirectory
91 bool KDirListerCache::listDir( KDirLister *lister, const KUrl& _u,
92  bool _keep, bool _reload )
93 {
94  KUrl _url(_u);
95  _url.cleanPath(); // kill consecutive slashes
96 
97  if (!_url.host().isEmpty() && KProtocolInfo::protocolClass(_url.protocol()) == ":local"
98  && _url.protocol() != "file") {
99  // ":local" protocols ignore the hostname, so strip it out preventively - #160057
100  // kio_file is special cased since it does honor the hostname (by redirecting to e.g. smb)
101  _url.setHost(QString());
102  if (_keep == false)
103  emit lister->redirection(_url);
104  }
105 
106  // like this we don't have to worry about trailing slashes any further
107  _url.adjustPath(KUrl::RemoveTrailingSlash);
108 
109  const QString urlStr = _url.url();
110 
111  QString resolved;
112  if (_url.isLocalFile()) {
113  // Resolve symlinks (#213799)
114  const QString local = _url.toLocalFile();
115  resolved = QFileInfo(local).canonicalFilePath();
116  if (local != resolved)
117  canonicalUrls[resolved].append(urlStr);
118  // TODO: remove entry from canonicalUrls again in forgetDirs
119  // Note: this is why we use a QStringList value in there rather than a QSet:
120  // we can just remove one entry and not have to worry about other dirlisters
121  // (the non-unicity of the stringlist gives us the refcounting, basically).
122  }
123 
124  if (!validUrl(lister, _url)) {
125  kDebug(7004) << lister << "url=" << _url << "not a valid url";
126  return false;
127  }
128 
129  //kDebug(7004) << lister << "url=" << _url << "keep=" << _keep << "reload=" << _reload;
130 #ifdef DEBUG_CACHE
131  printDebug();
132 #endif
133 
134  if (!_keep) {
135  // stop any running jobs for lister
136  stop(lister, true /*silent*/);
137 
138  // clear our internal list for lister
139  forgetDirs(lister);
140 
141  lister->d->rootFileItem = KFileItem();
142  } else if (lister->d->lstDirs.contains(_url)) {
143  // stop the job listing _url for this lister
144  stopListingUrl(lister, _url, true /*silent*/);
145 
146  // remove the _url as well, it will be added in a couple of lines again!
147  // forgetDirs with three args does not do this
148  // TODO: think about moving this into forgetDirs
149  lister->d->lstDirs.removeAll(_url);
150 
151  // clear _url for lister
152  forgetDirs(lister, _url, true);
153 
154  if (lister->d->url == _url)
155  lister->d->rootFileItem = KFileItem();
156  }
157 
158  lister->d->complete = false;
159 
160  lister->d->lstDirs.append(_url);
161 
162  if (lister->d->url.isEmpty() || !_keep) // set toplevel URL only if not set yet
163  lister->d->url = _url;
164 
165  DirItem *itemU = itemsInUse.value(urlStr);
166 
167  KDirListerCacheDirectoryData& dirData = directoryData[urlStr]; // find or insert
168 
169  if (dirData.listersCurrentlyListing.isEmpty()) {
170  // if there is an update running for _url already we get into
171  // the following case - it will just be restarted by updateDirectory().
172 
173  dirData.listersCurrentlyListing.append(lister);
174 
175  DirItem *itemFromCache = 0;
176  if (itemU || (!_reload && (itemFromCache = itemsCached.take(urlStr)) ) ) {
177  if (itemU) {
178  kDebug(7004) << "Entry already in use:" << _url;
179  // if _reload is set, then we'll emit cached items and then updateDirectory.
180  } else {
181  kDebug(7004) << "Entry in cache:" << _url;
182  itemsInUse.insert(urlStr, itemFromCache);
183  itemU = itemFromCache;
184  }
185  if (lister->d->autoUpdate) {
186  itemU->incAutoUpdate();
187  }
188  if (itemFromCache && itemFromCache->watchedWhileInCache) {
189  itemFromCache->watchedWhileInCache = false;;
190  itemFromCache->decAutoUpdate();
191  }
192 
193  emit lister->started(_url);
194 
195  // List items from the cache in a delayed manner, just like things would happen
196  // if we were not using the cache.
197  new KDirLister::Private::CachedItemsJob(lister, _url, _reload);
198 
199  } else {
200  // dir not in cache or _reload is true
201  if (_reload) {
202  kDebug(7004) << "Reloading directory:" << _url;
203  itemsCached.remove(urlStr);
204  } else {
205  kDebug(7004) << "Listing directory:" << _url;
206  }
207 
208  itemU = new DirItem(_url, resolved);
209  itemsInUse.insert(urlStr, itemU);
210  if (lister->d->autoUpdate)
211  itemU->incAutoUpdate();
212 
213 // // we have a limit of MAX_JOBS_PER_LISTER concurrently running jobs
214 // if ( lister->d->numJobs() >= MAX_JOBS_PER_LISTER )
215 // {
216 // pendingUpdates.insert( _url );
217 // }
218 // else
219  {
220  KIO::ListJob* job = KIO::listDir(_url, KIO::HideProgressInfo);
221  runningListJobs.insert(job, KIO::UDSEntryList());
222 
223  lister->d->jobStarted(job);
224  lister->d->connectJob(job);
225 
226  if (lister->d->window)
227  job->ui()->setWindow(lister->d->window);
228 
229  connect(job, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
230  this, SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList)));
231  connect(job, SIGNAL(result(KJob*)),
232  this, SLOT(slotResult(KJob*)));
233  connect(job, SIGNAL(redirection(KIO::Job*,KUrl)),
234  this, SLOT(slotRedirection(KIO::Job*,KUrl)));
235 
236  emit lister->started(_url);
237  }
238  //kDebug(7004) << "Entry now being listed by" << dirData.listersCurrentlyListing;
239  }
240  } else {
241 
242  kDebug(7004) << "Entry currently being listed:" << _url << "by" << dirData.listersCurrentlyListing;
243 #ifdef DEBUG_CACHE
244  printDebug();
245 #endif
246 
247  emit lister->started( _url );
248 
249  // Maybe listersCurrentlyListing/listersCurrentlyHolding should be QSets?
250  Q_ASSERT(!dirData.listersCurrentlyListing.contains(lister));
251  dirData.listersCurrentlyListing.append( lister );
252 
253  KIO::ListJob *job = jobForUrl( urlStr );
254  // job will be 0 if we were listing from cache rather than listing from a kio job.
255  if( job ) {
256  lister->d->jobStarted( job );
257  lister->d->connectJob( job );
258  }
259  Q_ASSERT( itemU );
260 
261  // List existing items in a delayed manner, just like things would happen
262  // if we were not using the cache.
263  if (!itemU->lstItems.isEmpty()) {
264  kDebug() << "Listing" << itemU->lstItems.count() << "cached items soon";
265  new KDirLister::Private::CachedItemsJob(lister, _url, _reload);
266  } else {
267  // The other lister hasn't emitted anything yet. Good, we'll just listen to it.
268  // One problem could be if we have _reload=true and the existing job doesn't, though.
269  }
270 
271 #ifdef DEBUG_CACHE
272  printDebug();
273 #endif
274  }
275 
276  return true;
277 }
278 
279 KDirLister::Private::CachedItemsJob* KDirLister::Private::cachedItemsJobForUrl(const KUrl& url) const
280 {
281  Q_FOREACH(CachedItemsJob* job, m_cachedItemsJobs) {
282  if (job->url() == url)
283  return job;
284  }
285  return 0;
286 }
287 
288 KDirLister::Private::CachedItemsJob::CachedItemsJob(KDirLister* lister, const KUrl& url, bool reload)
289  : KJob(lister),
290  m_lister(lister), m_url(url),
291  m_reload(reload), m_emitCompleted(true)
292 {
293  //kDebug() << "Creating CachedItemsJob" << this << "for lister" << lister << url;
294  if (lister->d->cachedItemsJobForUrl(url)) {
295  kWarning(7004) << "Lister" << lister << "has a cached items job already for" << url;
296  }
297  lister->d->m_cachedItemsJobs.append(this);
298  setAutoDelete(true);
299  start();
300 }
301 
302 // Called by start() via QueuedConnection
303 void KDirLister::Private::CachedItemsJob::done()
304 {
305  if (!m_lister) // job was already killed, but waiting deletion due to deleteLater
306  return;
307  kDirListerCache->emitItemsFromCache(this, m_lister, m_url, m_reload, m_emitCompleted);
308  emitResult();
309 }
310 
311 bool KDirLister::Private::CachedItemsJob::doKill()
312 {
313  //kDebug(7004) << this;
314  kDirListerCache->forgetCachedItemsJob(this, m_lister, m_url);
315  if (!property("_kdlc_silent").toBool()) {
316  emit m_lister->canceled(m_url);
317  emit m_lister->canceled();
318  }
319  m_lister = 0;
320  return true;
321 }
322 
323 void KDirListerCache::emitItemsFromCache(KDirLister::Private::CachedItemsJob* cachedItemsJob, KDirLister* lister, const KUrl& _url, bool _reload, bool _emitCompleted)
324 {
325  const QString urlStr = _url.url();
326  KDirLister::Private* kdl = lister->d;
327  kdl->complete = false;
328 
329  DirItem *itemU = kDirListerCache->itemsInUse.value(urlStr);
330  if (!itemU) {
331  kWarning(7004) << "Can't find item for directory" << urlStr << "anymore";
332  } else {
333  const KFileItemList items = itemU->lstItems;
334  const KFileItem rootItem = itemU->rootItem;
335  _reload = _reload || !itemU->complete;
336 
337  if (kdl->rootFileItem.isNull() && !rootItem.isNull() && kdl->url == _url) {
338  kdl->rootFileItem = rootItem;
339  }
340  if (!items.isEmpty()) {
341  //kDebug(7004) << "emitting" << items.count() << "for lister" << lister;
342  kdl->addNewItems(_url, items);
343  kdl->emitItems();
344  }
345  }
346 
347  forgetCachedItemsJob(cachedItemsJob, lister, _url);
348 
349  // Emit completed, unless we were told not to,
350  // or if listDir() was called while another directory listing for this dir was happening,
351  // so we "joined" it. We detect that using jobForUrl to ensure it's a real ListJob,
352  // not just a lister-specific CachedItemsJob (which wouldn't emit completed for us).
353  if (_emitCompleted) {
354 
355  kdl->complete = true;
356  emit lister->completed( _url );
357  emit lister->completed();
358 
359  if ( _reload ) {
360  updateDirectory( _url );
361  }
362  }
363 }
364 
365 void KDirListerCache::forgetCachedItemsJob(KDirLister::Private::CachedItemsJob* cachedItemsJob, KDirLister* lister, const KUrl& _url)
366 {
367  // Modifications to data structures only below this point;
368  // so that addNewItems is called with a consistent state
369 
370  const QString urlStr = _url.url();
371  lister->d->m_cachedItemsJobs.removeAll(cachedItemsJob);
372 
373  KDirListerCacheDirectoryData& dirData = directoryData[urlStr];
374  Q_ASSERT(dirData.listersCurrentlyListing.contains(lister));
375 
376  KIO::ListJob *listJob = jobForUrl(urlStr);
377  if (!listJob) {
378  Q_ASSERT(!dirData.listersCurrentlyHolding.contains(lister));
379  //kDebug(7004) << "Moving from listing to holding, because no more job" << lister << urlStr;
380  dirData.listersCurrentlyHolding.append( lister );
381  dirData.listersCurrentlyListing.removeAll( lister );
382  } else {
383  //kDebug(7004) << "Still having a listjob" << listJob << ", so not moving to currently-holding.";
384  }
385 }
386 
387 bool KDirListerCache::validUrl( const KDirLister *lister, const KUrl& url ) const
388 {
389  if ( !url.isValid() )
390  {
391  if ( lister->d->autoErrorHandling )
392  {
393  QString tmp = i18n("Malformed URL\n%1", url.prettyUrl() );
394  KMessageBox::error( lister->d->errorParent, tmp );
395  }
396  return false;
397  }
398 
399  if ( !KProtocolManager::supportsListing( url ) )
400  {
401  if ( lister->d->autoErrorHandling )
402  {
403  QString tmp = i18n("URL cannot be listed\n%1", url.prettyUrl() );
404  KMessageBox::error( lister->d->errorParent, tmp );
405  }
406  return false;
407  }
408 
409  return true;
410 }
411 
412 void KDirListerCache::stop( KDirLister *lister, bool silent )
413 {
414 #ifdef DEBUG_CACHE
415  //printDebug();
416 #endif
417  //kDebug(7004) << "lister:" << lister << "silent=" << silent;
418 
419  const KUrl::List urls = lister->d->lstDirs;
420  Q_FOREACH(const KUrl& url, urls) {
421  stopListingUrl(lister, url, silent);
422  }
423 
424 #if 0 // test code
425  QHash<QString,KDirListerCacheDirectoryData>::iterator dirit = directoryData.begin();
426  const QHash<QString,KDirListerCacheDirectoryData>::iterator dirend = directoryData.end();
427  for( ; dirit != dirend ; ++dirit ) {
428  KDirListerCacheDirectoryData& dirData = dirit.value();
429  if (dirData.listersCurrentlyListing.contains(lister)) {
430  kDebug(7004) << "ERROR: found lister" << lister << "in list - for" << dirit.key();
431  Q_ASSERT(false);
432  }
433  }
434 #endif
435 }
436 
437 void KDirListerCache::stopListingUrl(KDirLister *lister, const KUrl& _u, bool silent)
438 {
439  KUrl url(_u);
440  url.adjustPath( KUrl::RemoveTrailingSlash );
441  const QString urlStr = url.url();
442 
443  KDirLister::Private::CachedItemsJob* cachedItemsJob = lister->d->cachedItemsJobForUrl(url);
444  if (cachedItemsJob) {
445  if (silent) {
446  cachedItemsJob->setProperty("_kdlc_silent", true);
447  }
448  cachedItemsJob->kill(); // removes job from list, too
449  }
450 
451  // TODO: consider to stop all the "child jobs" of url as well
452  kDebug(7004) << lister << " url=" << url;
453 
454  QHash<QString,KDirListerCacheDirectoryData>::iterator dirit = directoryData.find(urlStr);
455  if (dirit == directoryData.end())
456  return;
457  KDirListerCacheDirectoryData& dirData = dirit.value();
458  if (dirData.listersCurrentlyListing.contains(lister)) {
459  //kDebug(7004) << " found lister" << lister << "in list - for" << urlStr;
460  if (dirData.listersCurrentlyListing.count() == 1) {
461  // This was the only dirlister interested in the list job -> kill the job
462  stopListJob(urlStr, silent);
463  } else {
464  // Leave the job running for the other dirlisters, just unsubscribe us.
465  dirData.listersCurrentlyListing.removeAll(lister);
466  if (!silent) {
467  emit lister->canceled();
468  emit lister->canceled(url);
469  }
470  }
471  }
472 }
473 
474 // Helper for stop() and stopListingUrl()
475 void KDirListerCache::stopListJob(const QString& url, bool silent)
476 {
477  // Old idea: if it's an update job, let's just leave the job running.
478  // After all, update jobs do run for "listersCurrentlyHolding",
479  // so there's no reason to kill them just because @p lister is now a holder.
480 
481  // However it could be a long-running non-local job (e.g. filenamesearch), which
482  // the user wants to abort, and which will never be used for updating...
483  // And in any case slotEntries/slotResult is not meant to be called by update jobs.
484  // So, change of plan, let's kill it after all, in a way that triggers slotResult/slotUpdateResult.
485 
486  KIO::ListJob *job = jobForUrl(url);
487  if (job) {
488  //kDebug() << "Killing list job" << job << "for" << url;
489  if (silent) {
490  job->setProperty("_kdlc_silent", true);
491  }
492  job->kill(KJob::EmitResult);
493  }
494 }
495 
496 void KDirListerCache::setAutoUpdate( KDirLister *lister, bool enable )
497 {
498  // IMPORTANT: this method does not check for the current autoUpdate state!
499 
500  for ( KUrl::List::const_iterator it = lister->d->lstDirs.constBegin();
501  it != lister->d->lstDirs.constEnd(); ++it ) {
502  DirItem* dirItem = itemsInUse.value((*it).url());
503  Q_ASSERT(dirItem);
504  if ( enable )
505  dirItem->incAutoUpdate();
506  else
507  dirItem->decAutoUpdate();
508  }
509 }
510 
511 void KDirListerCache::forgetDirs( KDirLister *lister )
512 {
513  //kDebug(7004) << lister;
514 
515  emit lister->clear();
516  // clear lister->d->lstDirs before calling forgetDirs(), so that
517  // it doesn't contain things that itemsInUse doesn't. When emitting
518  // the canceled signals, lstDirs must not contain anything that
519  // itemsInUse does not contain. (otherwise it might crash in findByName()).
520  const KUrl::List lstDirsCopy = lister->d->lstDirs;
521  lister->d->lstDirs.clear();
522 
523  //kDebug() << "Iterating over dirs" << lstDirsCopy;
524  for ( KUrl::List::const_iterator it = lstDirsCopy.begin();
525  it != lstDirsCopy.end(); ++it ) {
526  forgetDirs( lister, *it, false );
527  }
528 }
529 
530 static bool manually_mounted(const QString& path, const KMountPoint::List& possibleMountPoints)
531 {
532  KMountPoint::Ptr mp = possibleMountPoints.findByPath(path);
533  if (!mp) { // not listed in fstab -> yes, manually mounted
534  if (possibleMountPoints.isEmpty()) // no fstab at all -> don't assume anything
535  return false;
536  return true;
537  }
538  const bool supermount = mp->mountType() == "supermount";
539  if (supermount) {
540  return true;
541  }
542  // noauto -> manually mounted. Otherwise, mounted at boot time, won't be unmounted any time soon hopefully.
543  return mp->mountOptions().contains("noauto");
544 }
545 
546 
547 void KDirListerCache::forgetDirs( KDirLister *lister, const KUrl& _url, bool notify )
548 {
549  //kDebug(7004) << lister << " _url: " << _url;
550 
551  KUrl url( _url );
552  url.adjustPath( KUrl::RemoveTrailingSlash );
553  const QString urlStr = url.url();
554 
555  DirectoryDataHash::iterator dit = directoryData.find(urlStr);
556  if (dit == directoryData.end())
557  return;
558  KDirListerCacheDirectoryData& dirData = *dit;
559  dirData.listersCurrentlyHolding.removeAll(lister);
560 
561  // This lister doesn't care for updates running in <url> anymore
562  KIO::ListJob *job = jobForUrl(urlStr);
563  if (job)
564  lister->d->jobDone(job);
565 
566  DirItem *item = itemsInUse.value(urlStr);
567  Q_ASSERT(item);
568  bool insertIntoCache = false;
569 
570  if ( dirData.listersCurrentlyHolding.isEmpty() && dirData.listersCurrentlyListing.isEmpty() ) {
571  // item not in use anymore -> move into cache if complete
572  directoryData.erase(dit);
573  itemsInUse.remove( urlStr );
574 
575  // this job is a running update which nobody cares about anymore
576  if ( job ) {
577  killJob( job );
578  kDebug(7004) << "Killing update job for " << urlStr;
579 
580  // Well, the user of KDirLister doesn't really care that we're stopping
581  // a background-running job from a previous URL (in listDir) -> commented out.
582  // stop() already emitted canceled.
583  //emit lister->canceled( url );
584  if ( lister->d->numJobs() == 0 ) {
585  lister->d->complete = true;
586  //emit lister->canceled();
587  }
588  }
589 
590  if ( notify ) {
591  lister->d->lstDirs.removeAll( url );
592  emit lister->clear( url );
593  }
594 
595  insertIntoCache = item->complete;
596  if (insertIntoCache) {
597  // TODO(afiestas): remove use of KMountPoint+manually_mounted and port to Solid:
598  // 1) find Volume for the local path "item->url.toLocalFile()" (which could be anywhere
599  // under the mount point) -- probably needs a new operator in libsolid query parser
600  // 2) [**] becomes: if (Drive is hotpluggable or Volume is removable) "set to dirty" else "keep watch"
601  const KMountPoint::List possibleMountPoints = KMountPoint::possibleMountPoints(KMountPoint::NeedMountOptions);
602 
603  // Should we forget the dir for good, or keep a watch on it?
604  // Generally keep a watch, except when it would prevent
605  // unmounting a removable device (#37780)
606  const bool isLocal = item->url.isLocalFile();
607  bool isManuallyMounted = false;
608  bool containsManuallyMounted = false;
609  if (isLocal) {
610  isManuallyMounted = manually_mounted( item->url.toLocalFile(), possibleMountPoints );
611  if ( !isManuallyMounted ) {
612  // Look for a manually-mounted directory inside
613  // If there's one, we can't keep a watch either, FAM would prevent unmounting the CDROM
614  // I hope this isn't too slow
615  KFileItemList::const_iterator kit = item->lstItems.constBegin();
616  KFileItemList::const_iterator kend = item->lstItems.constEnd();
617  for ( ; kit != kend && !containsManuallyMounted; ++kit )
618  if ( (*kit).isDir() && manually_mounted((*kit).url().toLocalFile(), possibleMountPoints) )
619  containsManuallyMounted = true;
620  }
621  }
622 
623  if ( isManuallyMounted || containsManuallyMounted ) // [**]
624  {
625  kDebug(7004) << "Not adding a watch on " << item->url << " because it " <<
626  ( isManuallyMounted ? "is manually mounted" : "contains a manually mounted subdir" );
627  item->complete = false; // set to "dirty"
628  } else {
629  item->incAutoUpdate(); // keep watch
630  item->watchedWhileInCache = true;
631  }
632  }
633  else
634  {
635  delete item;
636  item = 0;
637  }
638  }
639 
640  if ( item && lister->d->autoUpdate )
641  item->decAutoUpdate();
642 
643  // Inserting into QCache must be done last, since it might delete the item
644  if (item && insertIntoCache) {
645  kDebug(7004) << lister << "item moved into cache:" << url;
646  itemsCached.insert(urlStr, item);
647  }
648 }
649 
650 void KDirListerCache::updateDirectory( const KUrl& _dir )
651 {
652  kDebug(7004) << _dir;
653 
654  QString urlStr = _dir.url(KUrl::RemoveTrailingSlash);
655  if (!checkUpdate(urlStr)) {
656  if (_dir.isLocalFile() && findByUrl(0, _dir)) {
657  pendingUpdates.insert(_dir.toLocalFile());
658  if (!pendingUpdateTimer.isActive())
659  pendingUpdateTimer.start(500);
660  }
661  return;
662  }
663 
664  // A job can be running to
665  // - only list a new directory: the listers are in listersCurrentlyListing
666  // - only update a directory: the listers are in listersCurrentlyHolding
667  // - update a currently running listing: the listers are in both
668 
669  KDirListerCacheDirectoryData& dirData = directoryData[urlStr];
670  QList<KDirLister *> listers = dirData.listersCurrentlyListing;
671  QList<KDirLister *> holders = dirData.listersCurrentlyHolding;
672 
673  //kDebug(7004) << urlStr << "listers=" << listers << "holders=" << holders;
674 
675  // restart the job for _dir if it is running already
676  bool killed = false;
677  QWidget *window = 0;
678  KIO::ListJob *job = jobForUrl( urlStr );
679  if (job) {
680  window = job->ui()->window();
681 
682  killJob( job );
683  killed = true;
684 
685  foreach ( KDirLister *kdl, listers )
686  kdl->d->jobDone( job );
687 
688  foreach ( KDirLister *kdl, holders )
689  kdl->d->jobDone( job );
690  } else {
691  // Emit any cached items.
692  // updateDirectory() is about the diff compared to the cached items...
693  Q_FOREACH(KDirLister *kdl, listers) {
694  KDirLister::Private::CachedItemsJob* cachedItemsJob = kdl->d->cachedItemsJobForUrl(_dir);
695  if (cachedItemsJob) {
696  cachedItemsJob->setEmitCompleted(false);
697  cachedItemsJob->done(); // removes from cachedItemsJobs list
698  delete cachedItemsJob;
699  killed = true;
700  }
701  }
702  }
703  //kDebug(7004) << "Killed=" << killed;
704 
705  // we don't need to emit canceled signals since we only replaced the job,
706  // the listing is continuing.
707 
708  if (!(listers.isEmpty() || killed)) {
709  kWarning() << "The unexpected happened.";
710  kWarning() << "listers for" << _dir << "=" << listers;
711  kWarning() << "job=" << job;
712  Q_FOREACH(KDirLister *kdl, listers) {
713  kDebug() << "lister" << kdl << "m_cachedItemsJobs=" << kdl->d->m_cachedItemsJobs;
714  }
715 #ifndef NDEBUG
716  printDebug();
717 #endif
718  }
719  Q_ASSERT( listers.isEmpty() || killed );
720 
721  job = KIO::listDir( _dir, KIO::HideProgressInfo );
722  runningListJobs.insert( job, KIO::UDSEntryList() );
723 
724  connect( job, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
725  this, SLOT(slotUpdateEntries(KIO::Job*,KIO::UDSEntryList)) );
726  connect( job, SIGNAL(result(KJob*)),
727  this, SLOT(slotUpdateResult(KJob*)) );
728 
729  kDebug(7004) << "update started in" << _dir;
730 
731  foreach ( KDirLister *kdl, listers ) {
732  kdl->d->jobStarted( job );
733  }
734 
735  if ( !holders.isEmpty() ) {
736  if ( !killed ) {
737  bool first = true;
738  foreach ( KDirLister *kdl, holders ) {
739  kdl->d->jobStarted( job );
740  if ( first && kdl->d->window ) {
741  first = false;
742  job->ui()->setWindow( kdl->d->window );
743  }
744  emit kdl->started( _dir );
745  }
746  } else {
747  job->ui()->setWindow( window );
748 
749  foreach ( KDirLister *kdl, holders ) {
750  kdl->d->jobStarted( job );
751  }
752  }
753  }
754 }
755 
756 bool KDirListerCache::checkUpdate( const QString& _dir )
757 {
758  if ( !itemsInUse.contains(_dir) )
759  {
760  DirItem *item = itemsCached[_dir];
761  if ( item && item->complete )
762  {
763  // Stop watching items once they are only in the cache and not used anymore.
764  // We'll trigger an update when listing that dir again later.
765  item->complete = false;
766  item->watchedWhileInCache = false;
767  item->decAutoUpdate();
768  // Hmm, this debug output might include login/password from the _dir URL.
769  //kDebug(7004) << "directory " << _dir << " not in use, marked dirty.";
770  }
771  //else
772  //kDebug(7004) << "aborted, directory " << _dir << " not in cache.";
773 
774  return false;
775  }
776  else
777  return true;
778 }
779 
780 KFileItem KDirListerCache::itemForUrl( const KUrl& url ) const
781 {
782  KFileItem *item = findByUrl( 0, url );
783  if (item) {
784  return *item;
785  } else {
786  return KFileItem();
787  }
788 }
789 
790 KDirListerCache::DirItem *KDirListerCache::dirItemForUrl(const KUrl& dir) const
791 {
792  const QString urlStr = dir.url(KUrl::RemoveTrailingSlash);
793  DirItem *item = itemsInUse.value(urlStr);
794  if ( !item )
795  item = itemsCached[urlStr];
796  return item;
797 }
798 
799 KFileItemList *KDirListerCache::itemsForDir(const KUrl& dir) const
800 {
801  DirItem *item = dirItemForUrl(dir);
802  return item ? &item->lstItems : 0;
803 }
804 
805 KFileItem KDirListerCache::findByName( const KDirLister *lister, const QString& _name ) const
806 {
807  Q_ASSERT(lister);
808 
809  for (KUrl::List::const_iterator it = lister->d->lstDirs.constBegin();
810  it != lister->d->lstDirs.constEnd(); ++it) {
811  DirItem* dirItem = itemsInUse.value((*it).url());
812  Q_ASSERT(dirItem);
813  const KFileItem item = dirItem->lstItems.findByName(_name);
814  if (!item.isNull())
815  return item;
816  }
817 
818  return KFileItem();
819 }
820 
821 KFileItem *KDirListerCache::findByUrl( const KDirLister *lister, const KUrl& _u ) const
822 {
823  KUrl url(_u);
824  url.adjustPath(KUrl::RemoveTrailingSlash);
825 
826  KUrl parentDir(url);
827  parentDir.setPath( parentDir.directory() );
828 
829  DirItem* dirItem = dirItemForUrl(parentDir);
830  if (dirItem) {
831  // If lister is set, check that it contains this dir
832  if (!lister || lister->d->lstDirs.contains(parentDir)) {
833  KFileItemList::iterator it = dirItem->lstItems.begin();
834  const KFileItemList::iterator end = dirItem->lstItems.end();
835  for (; it != end ; ++it) {
836  if ((*it).url() == url) {
837  return &*it;
838  }
839  }
840  }
841  }
842 
843  // Maybe _u is a directory itself? (see KDirModelTest::testChmodDirectory)
844  // We check this last, though, we prefer returning a kfileitem with an actual
845  // name if possible (and we make it '.' for root items later).
846  dirItem = dirItemForUrl(url);
847  if (dirItem && !dirItem->rootItem.isNull() && dirItem->rootItem.url() == url) {
848  // If lister is set, check that it contains this dir
849  if (!lister || lister->d->lstDirs.contains(url)) {
850  return &dirItem->rootItem;
851  }
852  }
853 
854  return 0;
855 }
856 
857 void KDirListerCache::slotFilesAdded( const QString &dir /*url*/ ) // from KDirNotify signals
858 {
859  KUrl urlDir(dir);
860  kDebug(7004) << urlDir; // output urls, not qstrings, since they might contain a password
861  if (urlDir.isLocalFile()) {
862  Q_FOREACH(const QString& u, directoriesForCanonicalPath(urlDir.toLocalFile())) {
863  updateDirectory(KUrl(u));
864  }
865  } else {
866  updateDirectory(urlDir);
867  }
868 }
869 
870 void KDirListerCache::slotFilesRemoved( const QStringList &fileList ) // from KDirNotify signals
871 {
872  // TODO: handling of symlinks-to-directories isn't done here,
873  // because I'm not sure how to do it and keep the performance ok...
874  slotFilesRemoved(KUrl::List(fileList));
875 }
876 
877 void KDirListerCache::slotFilesRemoved(const KUrl::List& fileList)
878 {
879  //kDebug(7004) << fileList.count();
880  // Group notifications by parent dirs (usually there would be only one parent dir)
881  QMap<QString, KFileItemList> removedItemsByDir;
882  KUrl::List deletedSubdirs;
883 
884  for (KUrl::List::const_iterator it = fileList.begin(); it != fileList.end() ; ++it) {
885  const KUrl url(*it);
886  DirItem* dirItem = dirItemForUrl(url); // is it a listed directory?
887  if (dirItem) {
888  deletedSubdirs.append(url);
889  if (!dirItem->rootItem.isNull()) {
890  removedItemsByDir[url.url()].append(dirItem->rootItem);
891  }
892  }
893 
894  KUrl parentDir(url);
895  parentDir.setPath(parentDir.directory());
896  dirItem = dirItemForUrl(parentDir);
897  if (!dirItem)
898  continue;
899  for (KFileItemList::iterator fit = dirItem->lstItems.begin(), fend = dirItem->lstItems.end(); fit != fend ; ++fit) {
900  if ((*fit).url() == url) {
901  const KFileItem fileitem = *fit;
902  removedItemsByDir[parentDir.url()].append(fileitem);
903  // If we found a fileitem, we can test if it's a dir. If not, we'll go to deleteDir just in case.
904  if (fileitem.isNull() || fileitem.isDir()) {
905  deletedSubdirs.append(url);
906  }
907  dirItem->lstItems.erase(fit); // remove fileitem from list
908  break;
909  }
910  }
911  }
912 
913  QMap<QString, KFileItemList>::const_iterator rit = removedItemsByDir.constBegin();
914  for(; rit != removedItemsByDir.constEnd(); ++rit) {
915  // Tell the views about it before calling deleteDir.
916  // They might need the subdirs' file items (see the dirtree).
917  DirectoryDataHash::const_iterator dit = directoryData.constFind(rit.key());
918  if (dit != directoryData.constEnd()) {
919  itemsDeleted((*dit).listersCurrentlyHolding, rit.value());
920  }
921  }
922 
923  Q_FOREACH(const KUrl& url, deletedSubdirs) {
924  // in case of a dir, check if we have any known children, there's much to do in that case
925  // (stopping jobs, removing dirs from cache etc.)
926  deleteDir(url);
927  }
928 }
929 
930 void KDirListerCache::slotFilesChanged( const QStringList &fileList ) // from KDirNotify signals
931 {
932  //kDebug(7004) << fileList;
933  KUrl::List dirsToUpdate;
934  QStringList::const_iterator it = fileList.begin();
935  for (; it != fileList.end() ; ++it) {
936  KUrl url( *it );
937  KFileItem *fileitem = findByUrl(0, url);
938  if (!fileitem) {
939  kDebug(7004) << "item not found for" << url;
940  continue;
941  }
942  if (url.isLocalFile()) {
943  pendingUpdates.insert(url.toLocalFile()); // delegate the work to processPendingUpdates
944  } else {
945  pendingRemoteUpdates.insert(fileitem);
946  // For remote files, we won't be able to figure out the new information,
947  // we have to do a update (directory listing)
948  KUrl dir(url);
949  dir.setPath(dir.directory());
950  if (!dirsToUpdate.contains(dir))
951  dirsToUpdate.prepend(dir);
952  }
953  }
954 
955  KUrl::List::const_iterator itdir = dirsToUpdate.constBegin();
956  for (; itdir != dirsToUpdate.constEnd() ; ++itdir)
957  updateDirectory( *itdir );
958  // ## TODO problems with current jobs listing/updating that dir
959  // ( see kde-2.2.2's kdirlister )
960 
961  processPendingUpdates();
962 }
963 
964 void KDirListerCache::slotFileRenamed( const QString &_src, const QString &_dst ) // from KDirNotify signals
965 {
966  KUrl src( _src );
967  KUrl dst( _dst );
968  kDebug(7004) << src << "->" << dst;
969 #ifdef DEBUG_CACHE
970  printDebug();
971 #endif
972 
973  KUrl oldurl(src);
974  oldurl.adjustPath( KUrl::RemoveTrailingSlash );
975  KFileItem *fileitem = findByUrl(0, oldurl);
976  if (!fileitem) {
977  kDebug(7004) << "Item not found:" << oldurl;
978  return;
979  }
980 
981  const KFileItem oldItem = *fileitem;
982 
983  // Dest already exists? Was overwritten then (testcase: #151851)
984  // We better emit it as deleted -before- doing the renaming, otherwise
985  // the "update" mechanism will emit the old one as deleted and
986  // kdirmodel will delete the new (renamed) one!
987  KFileItem* existingDestItem = findByUrl(0, dst);
988  if (existingDestItem) {
989  //kDebug() << dst << "already existed, let's delete it";
990  slotFilesRemoved(dst);
991  }
992 
993  // If the item had a UDS_URL as well as UDS_NAME set, the user probably wants
994  // to be updating the name only (since they can't see the URL).
995  // Check to see if a URL exists, and if so, if only the file part has changed,
996  // only update the name and not the underlying URL.
997  bool nameOnly = !fileitem->entry().stringValue( KIO::UDSEntry::UDS_URL ).isEmpty();
998  nameOnly &= src.directory( KUrl::IgnoreTrailingSlash | KUrl::AppendTrailingSlash ) ==
999  dst.directory( KUrl::IgnoreTrailingSlash | KUrl::AppendTrailingSlash );
1000 
1001  if (!nameOnly && fileitem->isDir()) {
1002  renameDir( src, dst );
1003  // #172945 - if the fileitem was the root item of a DirItem that was just removed from the cache,
1004  // then it's a dangling pointer now...
1005  fileitem = findByUrl(0, oldurl);
1006  if (!fileitem) //deleted from cache altogether, #188807
1007  return;
1008  }
1009 
1010  // Now update the KFileItem representing that file or dir (not exclusive with the above!)
1011  if (!oldItem.isLocalFile() && !oldItem.localPath().isEmpty()) { // it uses UDS_LOCAL_PATH? ouch, needs an update then
1012  slotFilesChanged( QStringList() << src.url() );
1013  } else {
1014  if( nameOnly )
1015  fileitem->setName( dst.fileName() );
1016  else
1017  fileitem->setUrl( dst );
1018  fileitem->refreshMimeType();
1019  fileitem->determineMimeType();
1020  QSet<KDirLister*> listers = emitRefreshItem( oldItem, *fileitem );
1021  Q_FOREACH(KDirLister * kdl, listers) {
1022  kdl->d->emitItems();
1023  }
1024  }
1025 
1026 #ifdef DEBUG_CACHE
1027  printDebug();
1028 #endif
1029 }
1030 
1031 QSet<KDirLister*> KDirListerCache::emitRefreshItem(const KFileItem& oldItem, const KFileItem& fileitem)
1032 {
1033  //kDebug(7004) << "old:" << oldItem.name() << oldItem.url()
1034  // << "new:" << fileitem.name() << fileitem.url();
1035  // Look whether this item was shown in any view, i.e. held by any dirlister
1036  KUrl parentDir( oldItem.url() );
1037  parentDir.setPath( parentDir.directory() );
1038  const QString parentDirURL = parentDir.url();
1039  DirectoryDataHash::iterator dit = directoryData.find(parentDirURL);
1040  QList<KDirLister *> listers;
1041  // Also look in listersCurrentlyListing, in case the user manages to rename during a listing
1042  if (dit != directoryData.end())
1043  listers += (*dit).listersCurrentlyHolding + (*dit).listersCurrentlyListing;
1044  if (oldItem.isDir()) {
1045  // For a directory, look for dirlisters where it's the root item.
1046  dit = directoryData.find(oldItem.url().url());
1047  if (dit != directoryData.end())
1048  listers += (*dit).listersCurrentlyHolding + (*dit).listersCurrentlyListing;
1049  }
1050  QSet<KDirLister*> listersToRefresh;
1051  Q_FOREACH(KDirLister *kdl, listers) {
1052  // For a directory, look for dirlisters where it's the root item.
1053  KUrl directoryUrl(oldItem.url());
1054  if (oldItem.isDir() && kdl->d->rootFileItem == oldItem) {
1055  const KFileItem oldRootItem = kdl->d->rootFileItem;
1056  kdl->d->rootFileItem = fileitem;
1057  kdl->d->addRefreshItem(directoryUrl, oldRootItem, fileitem);
1058  } else {
1059  directoryUrl.setPath(directoryUrl.directory());
1060  kdl->d->addRefreshItem(directoryUrl, oldItem, fileitem);
1061  }
1062  listersToRefresh.insert(kdl);
1063  }
1064  return listersToRefresh;
1065 }
1066 
1067 QStringList KDirListerCache::directoriesForCanonicalPath(const QString& dir) const
1068 {
1069  QStringList dirs;
1070  dirs << dir;
1071  dirs << canonicalUrls.value(dir).toSet().toList(); /* make unique; there are faster ways, but this is really small anyway */
1072 
1073  if (dirs.count() > 1)
1074  kDebug() << dir << "known as" << dirs;
1075 
1076  return dirs;
1077 }
1078 
1079 // private slots
1080 
1081 // Called by KDirWatch - usually when a dir we're watching has been modified,
1082 // but it can also be called for a file.
1083 void KDirListerCache::slotFileDirty( const QString& path )
1084 {
1085  // kDebug(7004) << path;
1086 
1087  KUrl url(path);
1088  url.adjustPath(KUrl::RemoveTrailingSlash);
1089 
1090  bool isDir;
1091  const KFileItem item = itemForUrl(url);
1092 
1093  if (!item.isNull()) {
1094  isDir = item.isDir();
1095  } else {
1096  KDE_struct_stat buff;
1097  kDebug(7004) << "Doing stat on:" << path;
1098  if ( KDE::stat( path, &buff ) != 0 )
1099  return; // error
1100  isDir = S_ISDIR(buff.st_mode);
1101  }
1102 
1103  if (isDir) {
1104  Q_FOREACH(const QString& dir, directoriesForCanonicalPath(url.toLocalFile())) {
1105  handleDirDirty(dir);
1106  }
1107  } else {
1108  Q_FOREACH(const QString& dir, directoriesForCanonicalPath(url.directory())) {
1109  KUrl aliasUrl(dir);
1110  aliasUrl.addPath(url.fileName());
1111  handleFileDirty(aliasUrl);
1112  }
1113  }
1114 }
1115 
1116 // Called by slotFileDirty
1117 void KDirListerCache::handleDirDirty(const KUrl& url)
1118 {
1119  // A dir: launch an update job if anyone cares about it
1120 
1121  // This also means we can forget about pending updates to individual files in that dir
1122  const QString dirPath = url.toLocalFile(KUrl::AddTrailingSlash);
1123  QMutableSetIterator<QString> pendingIt(pendingUpdates);
1124  while (pendingIt.hasNext()) {
1125  const QString updPath = pendingIt.next();
1126  //kDebug(7004) << "had pending update" << updPath;
1127  if (updPath.startsWith(dirPath) &&
1128  updPath.indexOf('/', dirPath.length()) == -1) { // direct child item
1129  kDebug(7004) << "forgetting about individual update to" << updPath;
1130  pendingIt.remove();
1131  }
1132  }
1133 
1134  updateDirectory(url);
1135 }
1136 
1137 // Called by slotFileDirty
1138 void KDirListerCache::handleFileDirty(const KUrl& url)
1139 {
1140  // A file: do we know about it already?
1141  KFileItem* existingItem = findByUrl(0, url);
1142  if (!existingItem) {
1143  // No - update the parent dir then
1144  KUrl dir(url);
1145  dir.setPath(url.directory());
1146  updateDirectory(dir);
1147  } else {
1148  // A known file: delay updating it, FAM is flooding us with events
1149  const QString filePath = url.toLocalFile();
1150  if (!pendingUpdates.contains(filePath)) {
1151  KUrl dir(url);
1152  dir.setPath(dir.directory());
1153  if (checkUpdate(dir.url())) {
1154  pendingUpdates.insert(filePath);
1155  if (!pendingUpdateTimer.isActive())
1156  pendingUpdateTimer.start(500);
1157  }
1158  }
1159  }
1160 }
1161 
1162 void KDirListerCache::slotFileCreated( const QString& path ) // from KDirWatch
1163 {
1164  kDebug(7004) << path;
1165  // XXX: how to avoid a complete rescan here?
1166  // We'd need to stat that one file separately and refresh the item(s) for it.
1167  KUrl fileUrl(path);
1168  slotFilesAdded(fileUrl.directory());
1169 }
1170 
1171 void KDirListerCache::slotFileDeleted( const QString& path ) // from KDirWatch
1172 {
1173  kDebug(7004) << path;
1174  KUrl u( path );
1175  QStringList fileUrls;
1176  Q_FOREACH(KUrl url, directoriesForCanonicalPath(u.directory())) {
1177  url.addPath(u.fileName());
1178  fileUrls << url.url();
1179  }
1180  slotFilesRemoved(fileUrls);
1181 }
1182 
1183 void KDirListerCache::slotEntries( KIO::Job *job, const KIO::UDSEntryList &entries )
1184 {
1185  KUrl url(joburl( static_cast<KIO::ListJob *>(job) ));
1186  url.adjustPath(KUrl::RemoveTrailingSlash);
1187  QString urlStr = url.url();
1188 
1189  //kDebug(7004) << "new entries for " << url;
1190 
1191  DirItem *dir = itemsInUse.value(urlStr);
1192  if (!dir) {
1193  kError(7004) << "Internal error: job is listing" << url << "but itemsInUse only knows about" << itemsInUse.keys();
1194  Q_ASSERT( dir );
1195  return;
1196  }
1197 
1198  DirectoryDataHash::iterator dit = directoryData.find(urlStr);
1199  if (dit == directoryData.end()) {
1200  kError(7004) << "Internal error: job is listing" << url << "but directoryData doesn't know about that url, only about:" << directoryData.keys();
1201  Q_ASSERT(dit != directoryData.end());
1202  return;
1203  }
1204  KDirListerCacheDirectoryData& dirData = *dit;
1205  if (dirData.listersCurrentlyListing.isEmpty()) {
1206  kError(7004) << "Internal error: job is listing" << url << "but directoryData says no listers are currently listing " << urlStr;
1207 #ifndef NDEBUG
1208  printDebug();
1209 #endif
1210  Q_ASSERT( !dirData.listersCurrentlyListing.isEmpty() );
1211  return;
1212  }
1213 
1214  // check if anyone wants the mimetypes immediately
1215  bool delayedMimeTypes = true;
1216  foreach ( KDirLister *kdl, dirData.listersCurrentlyListing )
1217  delayedMimeTypes &= kdl->d->delayedMimeTypes;
1218 
1219  KIO::UDSEntryList::const_iterator it = entries.begin();
1220  const KIO::UDSEntryList::const_iterator end = entries.end();
1221  for ( ; it != end; ++it )
1222  {
1223  const QString name = (*it).stringValue( KIO::UDSEntry::UDS_NAME );
1224 
1225  Q_ASSERT( !name.isEmpty() );
1226  if ( name.isEmpty() )
1227  continue;
1228 
1229  if ( name == "." )
1230  {
1231  Q_ASSERT( dir->rootItem.isNull() );
1232  // Try to reuse an existing KFileItem (if we listed the parent dir)
1233  // rather than creating a new one. There are many reasons:
1234  // 1) renames and permission changes to the item would have to emit the signals
1235  // twice, otherwise, so that both views manage to recognize the item.
1236  // 2) with kio_ftp we can only know that something is a symlink when
1237  // listing the parent, so prefer that item, which has more info.
1238  // Note that it gives a funky name() to the root item, rather than "." ;)
1239  dir->rootItem = itemForUrl(url);
1240  if (dir->rootItem.isNull())
1241  dir->rootItem = KFileItem( *it, url, delayedMimeTypes, true );
1242 
1243  foreach ( KDirLister *kdl, dirData.listersCurrentlyListing )
1244  if ( kdl->d->rootFileItem.isNull() && kdl->d->url == url )
1245  kdl->d->rootFileItem = dir->rootItem;
1246  }
1247  else if ( name != ".." )
1248  {
1249  KFileItem item( *it, url, delayedMimeTypes, true );
1250 
1251  //kDebug(7004)<< "Adding item: " << item.url();
1252  dir->lstItems.append( item );
1253 
1254  foreach ( KDirLister *kdl, dirData.listersCurrentlyListing )
1255  kdl->d->addNewItem(url, item);
1256  }
1257  }
1258 
1259  foreach ( KDirLister *kdl, dirData.listersCurrentlyListing )
1260  kdl->d->emitItems();
1261 }
1262 
1263 void KDirListerCache::slotResult( KJob *j )
1264 {
1265 #ifdef DEBUG_CACHE
1266  //printDebug();
1267 #endif
1268 
1269  Q_ASSERT( j );
1270  KIO::ListJob *job = static_cast<KIO::ListJob *>( j );
1271  runningListJobs.remove( job );
1272 
1273  KUrl jobUrl(joburl( job ));
1274  jobUrl.adjustPath(KUrl::RemoveTrailingSlash); // need remove trailing slashes again, in case of redirections
1275  QString jobUrlStr = jobUrl.url();
1276 
1277  kDebug(7004) << "finished listing" << jobUrl;
1278 
1279  DirectoryDataHash::iterator dit = directoryData.find(jobUrlStr);
1280  if (dit == directoryData.end()) {
1281  kError() << "Nothing found in directoryData for URL" << jobUrlStr;
1282 #ifndef NDEBUG
1283  printDebug();
1284 #endif
1285  Q_ASSERT(dit != directoryData.end());
1286  return;
1287  }
1288  KDirListerCacheDirectoryData& dirData = *dit;
1289  if ( dirData.listersCurrentlyListing.isEmpty() ) {
1290  kError() << "OOOOPS, nothing in directoryData.listersCurrentlyListing for" << jobUrlStr;
1291  // We're about to assert; dump the current state...
1292 #ifndef NDEBUG
1293  printDebug();
1294 #endif
1295  Q_ASSERT( !dirData.listersCurrentlyListing.isEmpty() );
1296  }
1297  QList<KDirLister *> listers = dirData.listersCurrentlyListing;
1298 
1299  // move all listers to the holding list, do it before emitting
1300  // the signals to make sure it exists in KDirListerCache in case someone
1301  // calls listDir during the signal emission
1302  Q_ASSERT( dirData.listersCurrentlyHolding.isEmpty() );
1303  dirData.moveListersWithoutCachedItemsJob(jobUrl);
1304 
1305  if ( job->error() )
1306  {
1307  foreach ( KDirLister *kdl, listers )
1308  {
1309  kdl->d->jobDone( job );
1310  if (job->error() != KJob::KilledJobError) {
1311  kdl->handleError( job );
1312  }
1313  const bool silent = job->property("_kdlc_silent").toBool();
1314  if (!silent) {
1315  emit kdl->canceled( jobUrl );
1316  }
1317 
1318  if (kdl->d->numJobs() == 0) {
1319  kdl->d->complete = true;
1320  if (!silent) {
1321  emit kdl->canceled();
1322  }
1323  }
1324  }
1325  }
1326  else
1327  {
1328  DirItem *dir = itemsInUse.value(jobUrlStr);
1329  Q_ASSERT( dir );
1330  dir->complete = true;
1331 
1332  foreach ( KDirLister* kdl, listers )
1333  {
1334  kdl->d->jobDone( job );
1335  emit kdl->completed( jobUrl );
1336  if ( kdl->d->numJobs() == 0 )
1337  {
1338  kdl->d->complete = true;
1339  emit kdl->completed();
1340  }
1341  }
1342  }
1343 
1344  // TODO: hmm, if there was an error and job is a parent of one or more
1345  // of the pending urls we should cancel it/them as well
1346  processPendingUpdates();
1347 
1348 #ifdef DEBUG_CACHE
1349  printDebug();
1350 #endif
1351 }
1352 
1353 void KDirListerCache::slotRedirection( KIO::Job *j, const KUrl& url )
1354 {
1355  Q_ASSERT( j );
1356  KIO::ListJob *job = static_cast<KIO::ListJob *>( j );
1357 
1358  KUrl oldUrl(job->url()); // here we really need the old url!
1359  KUrl newUrl(url);
1360 
1361  // strip trailing slashes
1362  oldUrl.adjustPath(KUrl::RemoveTrailingSlash);
1363  newUrl.adjustPath(KUrl::RemoveTrailingSlash);
1364 
1365  if ( oldUrl == newUrl ) {
1366  kDebug(7004) << "New redirection url same as old, giving up.";
1367  return;
1368  } else if (newUrl.isEmpty()) {
1369  kDebug(7004) << "New redirection url is empty, giving up.";
1370  return;
1371  }
1372 
1373  const QString oldUrlStr = oldUrl.url();
1374  const QString newUrlStr = newUrl.url();
1375 
1376  kDebug(7004) << oldUrl << "->" << newUrl;
1377 
1378 #ifdef DEBUG_CACHE
1379  // Can't do that here. KDirListerCache::joburl() will use the new url already,
1380  // while our data structures haven't been updated yet -> assert fail.
1381  //printDebug();
1382 #endif
1383 
1384  // I don't think there can be dirItems that are children of oldUrl.
1385  // Am I wrong here? And even if so, we don't need to delete them, right?
1386  // DF: redirection happens before listDir emits any item. Makes little sense otherwise.
1387 
1388  // oldUrl cannot be in itemsCached because only completed items are moved there
1389  DirItem *dir = itemsInUse.take(oldUrlStr);
1390  Q_ASSERT( dir );
1391 
1392  DirectoryDataHash::iterator dit = directoryData.find(oldUrlStr);
1393  Q_ASSERT(dit != directoryData.end());
1394  KDirListerCacheDirectoryData oldDirData = *dit;
1395  directoryData.erase(dit);
1396  Q_ASSERT( !oldDirData.listersCurrentlyListing.isEmpty() );
1397  const QList<KDirLister *> listers = oldDirData.listersCurrentlyListing;
1398  Q_ASSERT( !listers.isEmpty() );
1399 
1400  foreach ( KDirLister *kdl, listers ) {
1401  kdl->d->redirect(oldUrlStr, newUrl, false /*clear items*/);
1402  }
1403 
1404  // when a lister was stopped before the job emits the redirection signal, the old url will
1405  // also be in listersCurrentlyHolding
1406  const QList<KDirLister *> holders = oldDirData.listersCurrentlyHolding;
1407  foreach ( KDirLister *kdl, holders ) {
1408  kdl->d->jobStarted( job );
1409  // do it like when starting a new list-job that will redirect later
1410  // TODO: maybe don't emit started if there's an update running for newUrl already?
1411  emit kdl->started( oldUrl );
1412 
1413  kdl->d->redirect(oldUrl, newUrl, false /*clear items*/);
1414  }
1415 
1416  DirItem *newDir = itemsInUse.value(newUrlStr);
1417  if ( newDir ) {
1418  kDebug(7004) << newUrl << "already in use";
1419 
1420  // only in this case there can newUrl already be in listersCurrentlyListing or listersCurrentlyHolding
1421  delete dir;
1422 
1423  // get the job if one's running for newUrl already (can be a list-job or an update-job), but
1424  // do not return this 'job', which would happen because of the use of redirectionURL()
1425  KIO::ListJob *oldJob = jobForUrl( newUrlStr, job );
1426 
1427  // listers of newUrl with oldJob: forget about the oldJob and use the already running one
1428  // which will be converted to an updateJob
1429  KDirListerCacheDirectoryData& newDirData = directoryData[newUrlStr];
1430 
1431  QList<KDirLister *>& curListers = newDirData.listersCurrentlyListing;
1432  if ( !curListers.isEmpty() ) {
1433  kDebug(7004) << "and it is currently listed";
1434 
1435  Q_ASSERT( oldJob ); // ?!
1436 
1437  foreach ( KDirLister *kdl, curListers ) { // listers of newUrl
1438  kdl->d->jobDone( oldJob );
1439 
1440  kdl->d->jobStarted( job );
1441  kdl->d->connectJob( job );
1442  }
1443 
1444  // append listers of oldUrl with newJob to listers of newUrl with oldJob
1445  foreach ( KDirLister *kdl, listers )
1446  curListers.append( kdl );
1447  } else {
1448  curListers = listers;
1449  }
1450 
1451  if ( oldJob ) // kill the old job, be it a list-job or an update-job
1452  killJob( oldJob );
1453 
1454  // holders of newUrl: use the already running job which will be converted to an updateJob
1455  QList<KDirLister *>& curHolders = newDirData.listersCurrentlyHolding;
1456  if ( !curHolders.isEmpty() ) {
1457  kDebug(7004) << "and it is currently held.";
1458 
1459  foreach ( KDirLister *kdl, curHolders ) { // holders of newUrl
1460  kdl->d->jobStarted( job );
1461  emit kdl->started( newUrl );
1462  }
1463 
1464  // append holders of oldUrl to holders of newUrl
1465  foreach ( KDirLister *kdl, holders )
1466  curHolders.append( kdl );
1467  } else {
1468  curHolders = holders;
1469  }
1470 
1471 
1472  // emit old items: listers, holders. NOT: newUrlListers/newUrlHolders, they already have them listed
1473  // TODO: make this a separate method?
1474  foreach ( KDirLister *kdl, listers + holders ) {
1475  if ( kdl->d->rootFileItem.isNull() && kdl->d->url == newUrl )
1476  kdl->d->rootFileItem = newDir->rootItem;
1477 
1478  kdl->d->addNewItems(newUrl, newDir->lstItems);
1479  kdl->d->emitItems();
1480  }
1481  } else if ( (newDir = itemsCached.take( newUrlStr )) ) {
1482  kDebug(7004) << newUrl << "is unused, but already in the cache.";
1483 
1484  delete dir;
1485  itemsInUse.insert( newUrlStr, newDir );
1486  KDirListerCacheDirectoryData& newDirData = directoryData[newUrlStr];
1487  newDirData.listersCurrentlyListing = listers;
1488  newDirData.listersCurrentlyHolding = holders;
1489 
1490  // emit old items: listers, holders
1491  foreach ( KDirLister *kdl, listers + holders ) {
1492  if ( kdl->d->rootFileItem.isNull() && kdl->d->url == newUrl )
1493  kdl->d->rootFileItem = newDir->rootItem;
1494 
1495  kdl->d->addNewItems(newUrl, newDir->lstItems);
1496  kdl->d->emitItems();
1497  }
1498  } else {
1499  kDebug(7004) << newUrl << "has not been listed yet.";
1500 
1501  dir->rootItem = KFileItem();
1502  dir->lstItems.clear();
1503  dir->redirect( newUrl );
1504  itemsInUse.insert( newUrlStr, dir );
1505  KDirListerCacheDirectoryData& newDirData = directoryData[newUrlStr];
1506  newDirData.listersCurrentlyListing = listers;
1507  newDirData.listersCurrentlyHolding = holders;
1508 
1509  if ( holders.isEmpty() ) {
1510 #ifdef DEBUG_CACHE
1511  printDebug();
1512 #endif
1513  return; // only in this case the job doesn't need to be converted,
1514  }
1515  }
1516 
1517  // make the job an update job
1518  job->disconnect( this );
1519 
1520  connect( job, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
1521  this, SLOT(slotUpdateEntries(KIO::Job*,KIO::UDSEntryList)) );
1522  connect( job, SIGNAL(result(KJob*)),
1523  this, SLOT(slotUpdateResult(KJob*)) );
1524 
1525  // FIXME: autoUpdate-Counts!!
1526 
1527 #ifdef DEBUG_CACHE
1528  printDebug();
1529 #endif
1530 }
1531 
1532 struct KDirListerCache::ItemInUseChange
1533 {
1534  ItemInUseChange(const QString& old, const QString& newU, DirItem* di)
1535  : oldUrl(old), newUrl(newU), dirItem(di) {}
1536  QString oldUrl;
1537  QString newUrl;
1538  DirItem* dirItem;
1539 };
1540 
1541 void KDirListerCache::renameDir( const KUrl &oldUrl, const KUrl &newUrl )
1542 {
1543  kDebug(7004) << oldUrl << "->" << newUrl;
1544  const QString oldUrlStr = oldUrl.url(KUrl::RemoveTrailingSlash);
1545  const QString newUrlStr = newUrl.url(KUrl::RemoveTrailingSlash);
1546 
1547  // Not enough. Also need to look at any child dir, even sub-sub-sub-dir.
1548  //DirItem *dir = itemsInUse.take( oldUrlStr );
1549  //emitRedirections( oldUrl, url );
1550 
1551  QLinkedList<ItemInUseChange> itemsToChange;
1552  QSet<KDirLister *> listers;
1553 
1554  // Look at all dirs being listed/shown
1555  QHash<QString, DirItem *>::iterator itu = itemsInUse.begin();
1556  const QHash<QString, DirItem *>::iterator ituend = itemsInUse.end();
1557  for (; itu != ituend ; ++itu) {
1558  DirItem *dir = itu.value();
1559  KUrl oldDirUrl ( itu.key() );
1560  //kDebug(7004) << "itemInUse:" << oldDirUrl;
1561  // Check if this dir is oldUrl, or a subfolder of it
1562  if ( oldUrl.isParentOf( oldDirUrl ) ) {
1563  // TODO should use KUrl::cleanpath like isParentOf does
1564  QString relPath = oldDirUrl.path().mid( oldUrl.path().length() );
1565 
1566  KUrl newDirUrl( newUrl ); // take new base
1567  if ( !relPath.isEmpty() )
1568  newDirUrl.addPath( relPath ); // add unchanged relative path
1569  //kDebug(7004) << "new url=" << newDirUrl;
1570 
1571  // Update URL in dir item and in itemsInUse
1572  dir->redirect( newDirUrl );
1573 
1574  itemsToChange.append(ItemInUseChange(oldDirUrl.url(KUrl::RemoveTrailingSlash),
1575  newDirUrl.url(KUrl::RemoveTrailingSlash),
1576  dir));
1577  // Rename all items under that dir
1578 
1579  for ( KFileItemList::iterator kit = dir->lstItems.begin(), kend = dir->lstItems.end();
1580  kit != kend ; ++kit )
1581  {
1582  const KFileItem oldItem = *kit;
1583 
1584  const KUrl oldItemUrl ((*kit).url());
1585  const QString oldItemUrlStr( oldItemUrl.url(KUrl::RemoveTrailingSlash) );
1586  KUrl newItemUrl( oldItemUrl );
1587  newItemUrl.setPath( newDirUrl.path() );
1588  newItemUrl.addPath( oldItemUrl.fileName() );
1589  kDebug(7004) << "renaming" << oldItemUrl << "to" << newItemUrl;
1590  (*kit).setUrl(newItemUrl);
1591 
1592  listers |= emitRefreshItem(oldItem, *kit);
1593  }
1594  emitRedirections( oldDirUrl, newDirUrl );
1595  }
1596  }
1597 
1598  Q_FOREACH(KDirLister * kdl, listers) {
1599  kdl->d->emitItems();
1600  }
1601 
1602  // Do the changes to itemsInUse out of the loop to avoid messing up iterators,
1603  // and so that emitRefreshItem can find the stuff in the hash.
1604  foreach(const ItemInUseChange& i, itemsToChange) {
1605  itemsInUse.remove(i.oldUrl);
1606  itemsInUse.insert(i.newUrl, i.dirItem);
1607  }
1608 
1609  // Is oldUrl a directory in the cache?
1610  // Remove any child of oldUrl from the cache - even if the renamed dir itself isn't in it!
1611  removeDirFromCache( oldUrl );
1612  // TODO rename, instead.
1613 }
1614 
1615 // helper for renameDir, not used for redirections from KIO::listDir().
1616 void KDirListerCache::emitRedirections( const KUrl &oldUrl, const KUrl &newUrl )
1617 {
1618  kDebug(7004) << oldUrl << "->" << newUrl;
1619  const QString oldUrlStr = oldUrl.url(KUrl::RemoveTrailingSlash);
1620  const QString newUrlStr = newUrl.url(KUrl::RemoveTrailingSlash);
1621 
1622  KIO::ListJob *job = jobForUrl( oldUrlStr );
1623  if ( job )
1624  killJob( job );
1625 
1626  // Check if we were listing this dir. Need to abort and restart with new name in that case.
1627  DirectoryDataHash::iterator dit = directoryData.find(oldUrlStr);
1628  if ( dit == directoryData.end() )
1629  return;
1630  const QList<KDirLister *> listers = (*dit).listersCurrentlyListing;
1631  const QList<KDirLister *> holders = (*dit).listersCurrentlyHolding;
1632 
1633  KDirListerCacheDirectoryData& newDirData = directoryData[newUrlStr];
1634 
1635  // Tell the world that the job listing the old url is dead.
1636  foreach ( KDirLister *kdl, listers ) {
1637  if ( job )
1638  kdl->d->jobDone( job );
1639 
1640  emit kdl->canceled( oldUrl );
1641  }
1642  newDirData.listersCurrentlyListing += listers;
1643 
1644  // Check if we are currently displaying this directory (odds opposite wrt above)
1645  foreach ( KDirLister *kdl, holders ) {
1646  if ( job )
1647  kdl->d->jobDone( job );
1648  }
1649  newDirData.listersCurrentlyHolding += holders;
1650  directoryData.erase(dit);
1651 
1652  if ( !listers.isEmpty() ) {
1653  updateDirectory( newUrl );
1654 
1655  // Tell the world about the new url
1656  foreach ( KDirLister *kdl, listers )
1657  emit kdl->started( newUrl );
1658  }
1659 
1660  // And notify the dirlisters of the redirection
1661  foreach ( KDirLister *kdl, holders ) {
1662  kdl->d->redirect(oldUrl, newUrl, true /*keep items*/);
1663  }
1664 }
1665 
1666 void KDirListerCache::removeDirFromCache( const KUrl& dir )
1667 {
1668  kDebug(7004) << dir;
1669  const QList<QString> cachedDirs = itemsCached.keys(); // seems slow, but there's no qcache iterator...
1670  foreach(const QString& cachedDir, cachedDirs) {
1671  if ( dir.isParentOf( KUrl( cachedDir ) ) )
1672  itemsCached.remove( cachedDir );
1673  }
1674 }
1675 
1676 void KDirListerCache::slotUpdateEntries( KIO::Job* job, const KIO::UDSEntryList& list )
1677 {
1678  runningListJobs[static_cast<KIO::ListJob*>(job)] += list;
1679 }
1680 
1681 void KDirListerCache::slotUpdateResult( KJob * j )
1682 {
1683  Q_ASSERT( j );
1684  KIO::ListJob *job = static_cast<KIO::ListJob *>( j );
1685 
1686  KUrl jobUrl (joburl( job ));
1687  jobUrl.adjustPath(KUrl::RemoveTrailingSlash); // need remove trailing slashes again, in case of redirections
1688  QString jobUrlStr (jobUrl.url());
1689 
1690  kDebug(7004) << "finished update" << jobUrl;
1691 
1692  KDirListerCacheDirectoryData& dirData = directoryData[jobUrlStr];
1693  // Collect the dirlisters which were listing the URL using that ListJob
1694  // plus those that were already holding that URL - they all get updated.
1695  dirData.moveListersWithoutCachedItemsJob(jobUrl);
1696  QList<KDirLister *> listers = dirData.listersCurrentlyHolding;
1697  listers += dirData.listersCurrentlyListing;
1698 
1699  // once we are updating dirs that are only in the cache this will fail!
1700  Q_ASSERT( !listers.isEmpty() );
1701 
1702  if ( job->error() ) {
1703  foreach ( KDirLister* kdl, listers ) {
1704  kdl->d->jobDone( job );
1705 
1706  //don't bother the user
1707  //kdl->handleError( job );
1708 
1709  const bool silent = job->property("_kdlc_silent").toBool();
1710  if (!silent) {
1711  emit kdl->canceled( jobUrl );
1712  }
1713  if ( kdl->d->numJobs() == 0 ) {
1714  kdl->d->complete = true;
1715  if (!silent) {
1716  emit kdl->canceled();
1717  }
1718  }
1719  }
1720 
1721  runningListJobs.remove( job );
1722 
1723  // TODO: if job is a parent of one or more
1724  // of the pending urls we should cancel them
1725  processPendingUpdates();
1726  return;
1727  }
1728 
1729  DirItem *dir = itemsInUse.value(jobUrlStr, 0);
1730  if (!dir) {
1731  kError(7004) << "Internal error: itemsInUse did not contain" << jobUrlStr;
1732 #ifndef NDEBUG
1733  printDebug();
1734 #endif
1735  Q_ASSERT(dir);
1736  } else {
1737  dir->complete = true;
1738  }
1739 
1740  // check if anyone wants the mimetypes immediately
1741  bool delayedMimeTypes = true;
1742  foreach ( KDirLister *kdl, listers )
1743  delayedMimeTypes &= kdl->d->delayedMimeTypes;
1744 
1745  QHash<QString, KFileItem*> fileItems; // fileName -> KFileItem*
1746 
1747  // Unmark all items in url
1748  for ( KFileItemList::iterator kit = dir->lstItems.begin(), kend = dir->lstItems.end() ; kit != kend ; ++kit )
1749  {
1750  (*kit).unmark();
1751  fileItems.insert( (*kit).name(), &*kit );
1752  }
1753 
1754  const KIO::UDSEntryList& buf = runningListJobs.value( job );
1755  KIO::UDSEntryList::const_iterator it = buf.constBegin();
1756  const KIO::UDSEntryList::const_iterator end = buf.constEnd();
1757  for ( ; it != end; ++it )
1758  {
1759  // Form the complete url
1760  KFileItem item( *it, jobUrl, delayedMimeTypes, true );
1761 
1762  const QString name = item.name();
1763  Q_ASSERT( !name.isEmpty() );
1764 
1765  // we duplicate the check for dotdot here, to avoid iterating over
1766  // all items again and checking in matchesFilter() that way.
1767  if ( name.isEmpty() || name == ".." )
1768  continue;
1769 
1770  if ( name == "." )
1771  {
1772  // if the update was started before finishing the original listing
1773  // there is no root item yet
1774  if ( dir->rootItem.isNull() )
1775  {
1776  dir->rootItem = item;
1777 
1778  foreach ( KDirLister *kdl, listers )
1779  if ( kdl->d->rootFileItem.isNull() && kdl->d->url == jobUrl )
1780  kdl->d->rootFileItem = dir->rootItem;
1781  }
1782  continue;
1783  }
1784 
1785  // Find this item
1786  if (KFileItem* tmp = fileItems.value(item.name()))
1787  {
1788  QSet<KFileItem*>::iterator pru_it = pendingRemoteUpdates.find(tmp);
1789  const bool inPendingRemoteUpdates = (pru_it != pendingRemoteUpdates.end());
1790 
1791  // check if something changed for this file, using KFileItem::cmp()
1792  if (!tmp->cmp( item ) || inPendingRemoteUpdates) {
1793 
1794  if (inPendingRemoteUpdates) {
1795  pendingRemoteUpdates.erase(pru_it);
1796  }
1797 
1798  //kDebug(7004) << "file changed:" << tmp->name();
1799 
1800  const KFileItem oldItem = *tmp;
1801  *tmp = item;
1802  foreach ( KDirLister *kdl, listers )
1803  kdl->d->addRefreshItem(jobUrl, oldItem, *tmp);
1804  }
1805  //kDebug(7004) << "marking" << tmp;
1806  tmp->mark();
1807  }
1808  else // this is a new file
1809  {
1810  //kDebug(7004) << "new file:" << name;
1811 
1812  KFileItem pitem(item);
1813  pitem.mark();
1814  dir->lstItems.append( pitem );
1815 
1816  foreach ( KDirLister *kdl, listers )
1817  kdl->d->addNewItem(jobUrl, pitem);
1818  }
1819  }
1820 
1821  runningListJobs.remove( job );
1822 
1823  deleteUnmarkedItems( listers, dir->lstItems );
1824 
1825  foreach ( KDirLister *kdl, listers ) {
1826  kdl->d->emitItems();
1827 
1828  kdl->d->jobDone( job );
1829 
1830  emit kdl->completed( jobUrl );
1831  if ( kdl->d->numJobs() == 0 )
1832  {
1833  kdl->d->complete = true;
1834  emit kdl->completed();
1835  }
1836  }
1837 
1838  // TODO: hmm, if there was an error and job is a parent of one or more
1839  // of the pending urls we should cancel it/them as well
1840  processPendingUpdates();
1841 }
1842 
1843 // private
1844 
1845 KIO::ListJob *KDirListerCache::jobForUrl( const QString& url, KIO::ListJob *not_job )
1846 {
1847  QMap< KIO::ListJob *, KIO::UDSEntryList >::const_iterator it = runningListJobs.constBegin();
1848  while ( it != runningListJobs.constEnd() )
1849  {
1850  KIO::ListJob *job = it.key();
1851  if ( joburl( job ).url(KUrl::RemoveTrailingSlash) == url && job != not_job )
1852  return job;
1853  ++it;
1854  }
1855  return 0;
1856 }
1857 
1858 const KUrl& KDirListerCache::joburl( KIO::ListJob *job )
1859 {
1860  if ( job->redirectionUrl().isValid() )
1861  return job->redirectionUrl();
1862  else
1863  return job->url();
1864 }
1865 
1866 void KDirListerCache::killJob( KIO::ListJob *job )
1867 {
1868  runningListJobs.remove( job );
1869  job->disconnect( this );
1870  job->kill();
1871 }
1872 
1873 void KDirListerCache::deleteUnmarkedItems( const QList<KDirLister *>& listers, KFileItemList &lstItems )
1874 {
1875  KFileItemList deletedItems;
1876  // Find all unmarked items and delete them
1877  QMutableListIterator<KFileItem> kit(lstItems);
1878  while (kit.hasNext()) {
1879  const KFileItem& item = kit.next();
1880  if (!item.isMarked()) {
1881  //kDebug(7004) << "deleted:" << item.name() << &item;
1882  deletedItems.append(item);
1883  kit.remove();
1884  }
1885  }
1886  if (!deletedItems.isEmpty())
1887  itemsDeleted(listers, deletedItems);
1888 }
1889 
1890 void KDirListerCache::itemsDeleted(const QList<KDirLister *>& listers, const KFileItemList& deletedItems)
1891 {
1892  Q_FOREACH(KDirLister *kdl, listers) {
1893  kdl->d->emitItemsDeleted(deletedItems);
1894  }
1895 
1896  Q_FOREACH(const KFileItem& item, deletedItems) {
1897  if (item.isDir())
1898  deleteDir(item.url());
1899  }
1900 }
1901 
1902 void KDirListerCache::deleteDir( const KUrl& dirUrl )
1903 {
1904  //kDebug() << dirUrl;
1905  // unregister and remove the children of the deleted item.
1906  // Idea: tell all the KDirListers that they should forget the dir
1907  // and then remove it from the cache.
1908 
1909  // Separate itemsInUse iteration and calls to forgetDirs (which modify itemsInUse)
1910  KUrl::List affectedItems;
1911 
1912  QHash<QString, DirItem *>::iterator itu = itemsInUse.begin();
1913  const QHash<QString, DirItem *>::iterator ituend = itemsInUse.end();
1914  for ( ; itu != ituend; ++itu ) {
1915  const KUrl deletedUrl( itu.key() );
1916  if ( dirUrl.isParentOf( deletedUrl ) ) {
1917  affectedItems.append(deletedUrl);
1918  }
1919  }
1920 
1921  foreach(const KUrl& deletedUrl, affectedItems) {
1922  const QString deletedUrlStr = deletedUrl.url();
1923  // stop all jobs for deletedUrlStr
1924  DirectoryDataHash::iterator dit = directoryData.find(deletedUrlStr);
1925  if (dit != directoryData.end()) {
1926  // we need a copy because stop modifies the list
1927  QList<KDirLister *> listers = (*dit).listersCurrentlyListing;
1928  foreach ( KDirLister *kdl, listers )
1929  stopListingUrl( kdl, deletedUrl );
1930  // tell listers holding deletedUrl to forget about it
1931  // this will stop running updates for deletedUrl as well
1932 
1933  // we need a copy because forgetDirs modifies the list
1934  QList<KDirLister *> holders = (*dit).listersCurrentlyHolding;
1935  foreach ( KDirLister *kdl, holders ) {
1936  // lister's root is the deleted item
1937  if ( kdl->d->url == deletedUrl )
1938  {
1939  // tell the view first. It might need the subdirs' items (which forgetDirs will delete)
1940  if ( !kdl->d->rootFileItem.isNull() ) {
1941  emit kdl->deleteItem( kdl->d->rootFileItem );
1942  emit kdl->itemsDeleted(KFileItemList() << kdl->d->rootFileItem);
1943  }
1944  forgetDirs( kdl );
1945  kdl->d->rootFileItem = KFileItem();
1946  }
1947  else
1948  {
1949  const bool treeview = kdl->d->lstDirs.count() > 1;
1950  if ( !treeview )
1951  {
1952  emit kdl->clear();
1953  kdl->d->lstDirs.clear();
1954  }
1955  else
1956  kdl->d->lstDirs.removeAll( deletedUrl );
1957 
1958  forgetDirs( kdl, deletedUrl, treeview );
1959  }
1960  }
1961  }
1962 
1963  // delete the entry for deletedUrl - should not be needed, it's in
1964  // items cached now
1965  int count = itemsInUse.remove( deletedUrlStr );
1966  Q_ASSERT( count == 0 );
1967  Q_UNUSED( count ); //keep gcc "unused variable" complaining quiet when in release mode
1968  }
1969 
1970  // remove the children from the cache
1971  removeDirFromCache( dirUrl );
1972 }
1973 
1974 // delayed updating of files, FAM is flooding us with events
1975 void KDirListerCache::processPendingUpdates()
1976 {
1977  QSet<KDirLister *> listers;
1978  foreach(const QString& file, pendingUpdates) { // always a local path
1979  kDebug(7004) << file;
1980  KUrl u(file);
1981  KFileItem *item = findByUrl( 0, u ); // search all items
1982  if ( item ) {
1983  // we need to refresh the item, because e.g. the permissions can have changed.
1984  KFileItem oldItem = *item;
1985  item->refresh();
1986  listers |= emitRefreshItem( oldItem, *item );
1987  }
1988  }
1989  pendingUpdates.clear();
1990  Q_FOREACH(KDirLister * kdl, listers) {
1991  kdl->d->emitItems();
1992  }
1993 }
1994 
1995 #ifndef NDEBUG
1996 void KDirListerCache::printDebug()
1997 {
1998  kDebug(7004) << "Items in use:";
1999  QHash<QString, DirItem *>::const_iterator itu = itemsInUse.constBegin();
2000  const QHash<QString, DirItem *>::const_iterator ituend = itemsInUse.constEnd();
2001  for ( ; itu != ituend ; ++itu ) {
2002  kDebug(7004) << " " << itu.key() << "URL:" << itu.value()->url
2003  << "rootItem:" << ( !itu.value()->rootItem.isNull() ? itu.value()->rootItem.url() : KUrl() )
2004  << "autoUpdates refcount:" << itu.value()->autoUpdates
2005  << "complete:" << itu.value()->complete
2006  << QString("with %1 items.").arg(itu.value()->lstItems.count());
2007  }
2008 
2009  QList<KDirLister*> listersWithoutJob;
2010  kDebug(7004) << "Directory data:";
2011  DirectoryDataHash::const_iterator dit = directoryData.constBegin();
2012  for ( ; dit != directoryData.constEnd(); ++dit )
2013  {
2014  QString list;
2015  foreach ( KDirLister* listit, (*dit).listersCurrentlyListing )
2016  list += " 0x" + QString::number( (qlonglong)listit, 16 );
2017  kDebug(7004) << " " << dit.key() << (*dit).listersCurrentlyListing.count() << "listers:" << list;
2018  foreach ( KDirLister* listit, (*dit).listersCurrentlyListing ) {
2019  if (!listit->d->m_cachedItemsJobs.isEmpty()) {
2020  kDebug(7004) << " Lister" << listit << "has CachedItemsJobs" << listit->d->m_cachedItemsJobs;
2021  } else if (KIO::ListJob* listJob = jobForUrl(dit.key())) {
2022  kDebug(7004) << " Lister" << listit << "has ListJob" << listJob;
2023  } else {
2024  listersWithoutJob.append(listit);
2025  }
2026  }
2027 
2028  list.clear();
2029  foreach ( KDirLister* listit, (*dit).listersCurrentlyHolding )
2030  list += " 0x" + QString::number( (qlonglong)listit, 16 );
2031  kDebug(7004) << " " << dit.key() << (*dit).listersCurrentlyHolding.count() << "holders:" << list;
2032  }
2033 
2034  QMap< KIO::ListJob *, KIO::UDSEntryList >::Iterator jit = runningListJobs.begin();
2035  kDebug(7004) << "Jobs:";
2036  for ( ; jit != runningListJobs.end() ; ++jit )
2037  kDebug(7004) << " " << jit.key() << "listing" << joburl( jit.key() ) << ":" << (*jit).count() << "entries.";
2038 
2039  kDebug(7004) << "Items in cache:";
2040  const QList<QString> cachedDirs = itemsCached.keys();
2041  foreach(const QString& cachedDir, cachedDirs) {
2042  DirItem* dirItem = itemsCached.object(cachedDir);
2043  kDebug(7004) << " " << cachedDir << "rootItem:"
2044  << (!dirItem->rootItem.isNull() ? dirItem->rootItem.url().prettyUrl() : QString("NULL") )
2045  << "with" << dirItem->lstItems.count() << "items.";
2046  }
2047 
2048  // Abort on listers without jobs -after- showing the full dump. Easier debugging.
2049  Q_FOREACH(KDirLister* listit, listersWithoutJob) {
2050  kFatal() << "HUH? Lister" << listit << "is supposed to be listing, but has no job!";
2051  }
2052 }
2053 #endif
2054 
2055 
2056 KDirLister::KDirLister( QObject* parent )
2057  : QObject(parent), d(new Private(this))
2058 {
2059  //kDebug(7003) << "+KDirLister";
2060 
2061  d->complete = true;
2062 
2063  setAutoUpdate( true );
2064  setDirOnlyMode( false );
2065  setShowingDotFiles( false );
2066 
2067  setAutoErrorHandlingEnabled( true, 0 );
2068 }
2069 
2070 KDirLister::~KDirLister()
2071 {
2072  //kDebug(7003) << "~KDirLister" << this;
2073 
2074  // Stop all running jobs, remove lister from lists
2075  if (!kDirListerCache.isDestroyed()) {
2076  stop();
2077  kDirListerCache->forgetDirs( this );
2078  }
2079 
2080  delete d;
2081 }
2082 
2083 bool KDirLister::openUrl( const KUrl& _url, OpenUrlFlags _flags )
2084 {
2085  // emit the current changes made to avoid an inconsistent treeview
2086  if (d->hasPendingChanges && (_flags & Keep))
2087  emitChanges();
2088 
2089  d->hasPendingChanges = false;
2090 
2091  return kDirListerCache->listDir( this, _url, _flags & Keep, _flags & Reload );
2092 }
2093 
2094 void KDirLister::stop()
2095 {
2096  kDirListerCache->stop( this );
2097 }
2098 
2099 void KDirLister::stop( const KUrl& _url )
2100 {
2101  kDirListerCache->stopListingUrl( this, _url );
2102 }
2103 
2104 bool KDirLister::autoUpdate() const
2105 {
2106  return d->autoUpdate;
2107 }
2108 
2109 void KDirLister::setAutoUpdate( bool _enable )
2110 {
2111  if ( d->autoUpdate == _enable )
2112  return;
2113 
2114  d->autoUpdate = _enable;
2115  kDirListerCache->setAutoUpdate( this, _enable );
2116 }
2117 
2118 bool KDirLister::showingDotFiles() const
2119 {
2120  return d->settings.isShowingDotFiles;
2121 }
2122 
2123 void KDirLister::setShowingDotFiles( bool _showDotFiles )
2124 {
2125  if ( d->settings.isShowingDotFiles == _showDotFiles )
2126  return;
2127 
2128  d->prepareForSettingsChange();
2129  d->settings.isShowingDotFiles = _showDotFiles;
2130 }
2131 
2132 bool KDirLister::dirOnlyMode() const
2133 {
2134  return d->settings.dirOnlyMode;
2135 }
2136 
2137 void KDirLister::setDirOnlyMode( bool _dirsOnly )
2138 {
2139  if ( d->settings.dirOnlyMode == _dirsOnly )
2140  return;
2141 
2142  d->prepareForSettingsChange();
2143  d->settings.dirOnlyMode = _dirsOnly;
2144 }
2145 
2146 bool KDirLister::autoErrorHandlingEnabled() const
2147 {
2148  return d->autoErrorHandling;
2149 }
2150 
2151 void KDirLister::setAutoErrorHandlingEnabled( bool enable, QWidget* parent )
2152 {
2153  d->autoErrorHandling = enable;
2154  d->errorParent = parent;
2155 }
2156 
2157 KUrl KDirLister::url() const
2158 {
2159  return d->url;
2160 }
2161 
2162 KUrl::List KDirLister::directories() const
2163 {
2164  return d->lstDirs;
2165 }
2166 
2167 void KDirLister::emitChanges()
2168 {
2169  d->emitChanges();
2170 }
2171 
2172 void KDirLister::Private::emitChanges()
2173 {
2174  if (!hasPendingChanges)
2175  return;
2176 
2177  // reset 'hasPendingChanges' now, in case of recursion
2178  // (testcase: enabling recursive scan in ktorrent, #174920)
2179  hasPendingChanges = false;
2180 
2181  const Private::FilterSettings newSettings = settings;
2182  settings = oldSettings; // temporarily
2183 
2184  // Mark all items that are currently visible
2185  Q_FOREACH(const KUrl& dir, lstDirs) {
2186  KFileItemList* itemList = kDirListerCache->itemsForDir(dir);
2187  if (!itemList) {
2188  continue;
2189  }
2190 
2191  KFileItemList::iterator kit = itemList->begin();
2192  const KFileItemList::iterator kend = itemList->end();
2193  for (; kit != kend; ++kit) {
2194  if (isItemVisible(*kit) && m_parent->matchesMimeFilter(*kit))
2195  (*kit).mark();
2196  else
2197  (*kit).unmark();
2198  }
2199  }
2200 
2201  settings = newSettings;
2202 
2203  Q_FOREACH(const KUrl& dir, lstDirs) {
2204  KFileItemList deletedItems;
2205 
2206  KFileItemList* itemList = kDirListerCache->itemsForDir(dir);
2207  if (!itemList) {
2208  continue;
2209  }
2210 
2211  KFileItemList::iterator kit = itemList->begin();
2212  const KFileItemList::iterator kend = itemList->end();
2213  for (; kit != kend; ++kit) {
2214  KFileItem& item = *kit;
2215  const QString text = item.text();
2216  if (text == "." || text == "..")
2217  continue;
2218  const bool nowVisible = isItemVisible(item) && m_parent->matchesMimeFilter(item);
2219  if (nowVisible && !item.isMarked())
2220  addNewItem(dir, item); // takes care of emitting newItem or itemsFilteredByMime
2221  else if (!nowVisible && item.isMarked())
2222  deletedItems.append(*kit);
2223  }
2224  if (!deletedItems.isEmpty()) {
2225  emit m_parent->itemsDeleted(deletedItems);
2226  // for compat
2227  Q_FOREACH(const KFileItem& item, deletedItems)
2228  emit m_parent->deleteItem(item);
2229  }
2230  emitItems();
2231  }
2232  oldSettings = settings;
2233 }
2234 
2235 void KDirLister::updateDirectory( const KUrl& _u )
2236 {
2237  kDirListerCache->updateDirectory( _u );
2238 }
2239 
2240 bool KDirLister::isFinished() const
2241 {
2242  return d->complete;
2243 }
2244 
2245 KFileItem KDirLister::rootItem() const
2246 {
2247  return d->rootFileItem;
2248 }
2249 
2250 KFileItem KDirLister::findByUrl( const KUrl& _url ) const
2251 {
2252  KFileItem *item = kDirListerCache->findByUrl( this, _url );
2253  if (item) {
2254  return *item;
2255  } else {
2256  return KFileItem();
2257  }
2258 }
2259 
2260 KFileItem KDirLister::findByName( const QString& _name ) const
2261 {
2262  return kDirListerCache->findByName( this, _name );
2263 }
2264 
2265 
2266 // ================ public filter methods ================ //
2267 
2268 void KDirLister::setNameFilter( const QString& nameFilter )
2269 {
2270  if (d->nameFilter == nameFilter)
2271  return;
2272 
2273  d->prepareForSettingsChange();
2274 
2275  d->settings.lstFilters.clear();
2276  d->nameFilter = nameFilter;
2277  // Split on white space
2278  const QStringList list = nameFilter.split( ' ', QString::SkipEmptyParts );
2279  for (QStringList::const_iterator it = list.begin(); it != list.end(); ++it)
2280  d->settings.lstFilters.append(QRegExp(*it, Qt::CaseInsensitive, QRegExp::Wildcard));
2281 }
2282 
2283 QString KDirLister::nameFilter() const
2284 {
2285  return d->nameFilter;
2286 }
2287 
2288 void KDirLister::setMimeFilter( const QStringList& mimeFilter )
2289 {
2290  if (d->settings.mimeFilter == mimeFilter)
2291  return;
2292 
2293  d->prepareForSettingsChange();
2294  if (mimeFilter.contains(QLatin1String("application/octet-stream")) || mimeFilter.contains(QLatin1String("all/allfiles"))) // all files
2295  d->settings.mimeFilter.clear();
2296  else
2297  d->settings.mimeFilter = mimeFilter;
2298 }
2299 
2300 void KDirLister::setMimeExcludeFilter( const QStringList& mimeExcludeFilter )
2301 {
2302  if (d->settings.mimeExcludeFilter == mimeExcludeFilter)
2303  return;
2304 
2305  d->prepareForSettingsChange();
2306  d->settings.mimeExcludeFilter = mimeExcludeFilter;
2307 }
2308 
2309 
2310 void KDirLister::clearMimeFilter()
2311 {
2312  d->prepareForSettingsChange();
2313  d->settings.mimeFilter.clear();
2314  d->settings.mimeExcludeFilter.clear();
2315 }
2316 
2317 QStringList KDirLister::mimeFilters() const
2318 {
2319  return d->settings.mimeFilter;
2320 }
2321 
2322 bool KDirLister::matchesFilter( const QString& name ) const
2323 {
2324  return doNameFilter(name, d->settings.lstFilters);
2325 }
2326 
2327 bool KDirLister::matchesMimeFilter( const QString& mime ) const
2328 {
2329  return doMimeFilter(mime, d->settings.mimeFilter) &&
2330  d->doMimeExcludeFilter(mime, d->settings.mimeExcludeFilter);
2331 }
2332 
2333 // ================ protected methods ================ //
2334 
2335 bool KDirLister::matchesFilter( const KFileItem& item ) const
2336 {
2337  Q_ASSERT( !item.isNull() );
2338 
2339  if ( item.text() == ".." )
2340  return false;
2341 
2342  if ( !d->settings.isShowingDotFiles && item.isHidden() )
2343  return false;
2344 
2345  if ( item.isDir() || d->settings.lstFilters.isEmpty() )
2346  return true;
2347 
2348  return matchesFilter( item.text() );
2349 }
2350 
2351 bool KDirLister::matchesMimeFilter( const KFileItem& item ) const
2352 {
2353  Q_ASSERT(!item.isNull());
2354  // Don't lose time determining the mimetype if there is no filter
2355  if (d->settings.mimeFilter.isEmpty() && d->settings.mimeExcludeFilter.isEmpty())
2356  return true;
2357  return matchesMimeFilter(item.mimetype());
2358 }
2359 
2360 bool KDirLister::doNameFilter( const QString& name, const QList<QRegExp>& filters ) const
2361 {
2362  for ( QList<QRegExp>::const_iterator it = filters.begin(); it != filters.end(); ++it )
2363  if ( (*it).exactMatch( name ) )
2364  return true;
2365 
2366  return false;
2367 }
2368 
2369 bool KDirLister::doMimeFilter( const QString& mime, const QStringList& filters ) const
2370 {
2371  if ( filters.isEmpty() )
2372  return true;
2373 
2374  const KMimeType::Ptr mimeptr = KMimeType::mimeType(mime);
2375  if ( !mimeptr )
2376  return false;
2377 
2378  //kDebug(7004) << "doMimeFilter: investigating: "<<mimeptr->name();
2379  QStringList::const_iterator it = filters.begin();
2380  for ( ; it != filters.end(); ++it )
2381  if ( mimeptr->is(*it) )
2382  return true;
2383  //else kDebug(7004) << "doMimeFilter: compared without result to "<<*it;
2384 
2385  return false;
2386 }
2387 
2388 bool KDirLister::Private::doMimeExcludeFilter( const QString& mime, const QStringList& filters ) const
2389 {
2390  if ( filters.isEmpty() )
2391  return true;
2392 
2393  QStringList::const_iterator it = filters.begin();
2394  for ( ; it != filters.end(); ++it )
2395  if ( (*it) == mime )
2396  return false;
2397 
2398  return true;
2399 }
2400 
2401 void KDirLister::handleError( KIO::Job *job )
2402 {
2403  if ( d->autoErrorHandling )
2404  job->uiDelegate()->showErrorMessage();
2405 }
2406 
2407 
2408 // ================= private methods ================= //
2409 
2410 void KDirLister::Private::addNewItem(const KUrl& directoryUrl, const KFileItem &item)
2411 {
2412  if (!isItemVisible(item))
2413  return; // No reason to continue... bailing out here prevents a mimetype scan.
2414 
2415  //kDebug(7004) << "in" << directoryUrl << "item:" << item.url();
2416 
2417  if ( m_parent->matchesMimeFilter( item ) )
2418  {
2419  if ( !lstNewItems )
2420  {
2421  lstNewItems = new NewItemsHash;
2422  }
2423 
2424  Q_ASSERT( !item.isNull() );
2425  (*lstNewItems)[directoryUrl].append( item ); // items not filtered
2426  }
2427  else
2428  {
2429  if ( !lstMimeFilteredItems ) {
2430  lstMimeFilteredItems = new KFileItemList;
2431  }
2432 
2433  Q_ASSERT( !item.isNull() );
2434  lstMimeFilteredItems->append( item ); // only filtered by mime
2435  }
2436 }
2437 
2438 void KDirLister::Private::addNewItems(const KUrl& directoryUrl, const KFileItemList& items)
2439 {
2440  // TODO: make this faster - test if we have a filter at all first
2441  // DF: was this profiled? The matchesFoo() functions should be fast, w/o filters...
2442  // Of course if there is no filter and we can do a range-insertion instead of a loop, that might be good.
2443  KFileItemList::const_iterator kit = items.begin();
2444  const KFileItemList::const_iterator kend = items.end();
2445  for ( ; kit != kend; ++kit )
2446  addNewItem(directoryUrl, *kit);
2447 }
2448 
2449 void KDirLister::Private::addRefreshItem(const KUrl& directoryUrl, const KFileItem& oldItem, const KFileItem& item)
2450 {
2451  const bool refreshItemWasFiltered = !isItemVisible(oldItem) ||
2452  !m_parent->matchesMimeFilter(oldItem);
2453  if (isItemVisible(item) && m_parent->matchesMimeFilter(item)) {
2454  if ( refreshItemWasFiltered )
2455  {
2456  if ( !lstNewItems ) {
2457  lstNewItems = new NewItemsHash;
2458  }
2459 
2460  Q_ASSERT( !item.isNull() );
2461  (*lstNewItems)[directoryUrl].append( item );
2462  }
2463  else
2464  {
2465  if ( !lstRefreshItems ) {
2466  lstRefreshItems = new QList<QPair<KFileItem,KFileItem> >;
2467  }
2468 
2469  Q_ASSERT( !item.isNull() );
2470  lstRefreshItems->append( qMakePair(oldItem, item) );
2471  }
2472  }
2473  else if ( !refreshItemWasFiltered )
2474  {
2475  if ( !lstRemoveItems ) {
2476  lstRemoveItems = new KFileItemList;
2477  }
2478 
2479  // notify the user that the mimetype of a file changed that doesn't match
2480  // a filter or does match an exclude filter
2481  // This also happens when renaming foo to .foo and dot files are hidden (#174721)
2482  Q_ASSERT(!oldItem.isNull());
2483  lstRemoveItems->append(oldItem);
2484  }
2485 }
2486 
2487 void KDirLister::Private::emitItems()
2488 {
2489  NewItemsHash *tmpNew = lstNewItems;
2490  lstNewItems = 0;
2491 
2492  KFileItemList *tmpMime = lstMimeFilteredItems;
2493  lstMimeFilteredItems = 0;
2494 
2495  QList<QPair<KFileItem, KFileItem> > *tmpRefresh = lstRefreshItems;
2496  lstRefreshItems = 0;
2497 
2498  KFileItemList *tmpRemove = lstRemoveItems;
2499  lstRemoveItems = 0;
2500 
2501  if (tmpNew) {
2502  QHashIterator<KUrl, KFileItemList> it(*tmpNew);
2503  while (it.hasNext()) {
2504  it.next();
2505  emit m_parent->itemsAdded(it.key(), it.value());
2506  emit m_parent->newItems(it.value()); // compat
2507  }
2508  delete tmpNew;
2509  }
2510 
2511  if ( tmpMime )
2512  {
2513  emit m_parent->itemsFilteredByMime( *tmpMime );
2514  delete tmpMime;
2515  }
2516 
2517  if ( tmpRefresh )
2518  {
2519  emit m_parent->refreshItems( *tmpRefresh );
2520  delete tmpRefresh;
2521  }
2522 
2523  if ( tmpRemove )
2524  {
2525  emit m_parent->itemsDeleted( *tmpRemove );
2526  delete tmpRemove;
2527  }
2528 }
2529 
2530 bool KDirLister::Private::isItemVisible(const KFileItem& item) const
2531 {
2532  // Note that this doesn't include mime filters, because
2533  // of the itemsFilteredByMime signal. Filtered-by-mime items are
2534  // considered "visible", they are just visible via a different signal...
2535  return (!settings.dirOnlyMode || item.isDir())
2536  && m_parent->matchesFilter(item);
2537 }
2538 
2539 void KDirLister::Private::emitItemsDeleted(const KFileItemList &_items)
2540 {
2541  KFileItemList items = _items;
2542  QMutableListIterator<KFileItem> it(items);
2543  while (it.hasNext()) {
2544  const KFileItem& item = it.next();
2545  if (isItemVisible(item) && m_parent->matchesMimeFilter(item)) {
2546  // for compat
2547  emit m_parent->deleteItem(item);
2548  } else {
2549  it.remove();
2550  }
2551  }
2552  if (!items.isEmpty())
2553  emit m_parent->itemsDeleted(items);
2554 }
2555 
2556 // ================ private slots ================ //
2557 
2558 void KDirLister::Private::_k_slotInfoMessage( KJob *, const QString& message )
2559 {
2560  emit m_parent->infoMessage( message );
2561 }
2562 
2563 void KDirLister::Private::_k_slotPercent( KJob *job, unsigned long pcnt )
2564 {
2565  jobData[static_cast<KIO::ListJob *>(job)].percent = pcnt;
2566 
2567  int result = 0;
2568 
2569  KIO::filesize_t size = 0;
2570 
2571  QMap< KIO::ListJob *, Private::JobData >::Iterator dataIt = jobData.begin();
2572  while ( dataIt != jobData.end() )
2573  {
2574  result += (*dataIt).percent * (*dataIt).totalSize;
2575  size += (*dataIt).totalSize;
2576  ++dataIt;
2577  }
2578 
2579  if ( size != 0 )
2580  result /= size;
2581  else
2582  result = 100;
2583  emit m_parent->percent( result );
2584 }
2585 
2586 void KDirLister::Private::_k_slotTotalSize( KJob *job, qulonglong size )
2587 {
2588  jobData[static_cast<KIO::ListJob *>(job)].totalSize = size;
2589 
2590  KIO::filesize_t result = 0;
2591  QMap< KIO::ListJob *, Private::JobData >::Iterator dataIt = jobData.begin();
2592  while ( dataIt != jobData.end() )
2593  {
2594  result += (*dataIt).totalSize;
2595  ++dataIt;
2596  }
2597 
2598  emit m_parent->totalSize( result );
2599 }
2600 
2601 void KDirLister::Private::_k_slotProcessedSize( KJob *job, qulonglong size )
2602 {
2603  jobData[static_cast<KIO::ListJob *>(job)].processedSize = size;
2604 
2605  KIO::filesize_t result = 0;
2606  QMap< KIO::ListJob *, Private::JobData >::Iterator dataIt = jobData.begin();
2607  while ( dataIt != jobData.end() )
2608  {
2609  result += (*dataIt).processedSize;
2610  ++dataIt;
2611  }
2612 
2613  emit m_parent->processedSize( result );
2614 }
2615 
2616 void KDirLister::Private::_k_slotSpeed( KJob *job, unsigned long spd )
2617 {
2618  jobData[static_cast<KIO::ListJob *>(job)].speed = spd;
2619 
2620  int result = 0;
2621  QMap< KIO::ListJob *, Private::JobData >::Iterator dataIt = jobData.begin();
2622  while ( dataIt != jobData.end() )
2623  {
2624  result += (*dataIt).speed;
2625  ++dataIt;
2626  }
2627 
2628  emit m_parent->speed( result );
2629 }
2630 
2631 uint KDirLister::Private::numJobs()
2632 {
2633 #ifdef DEBUG_CACHE
2634  // This code helps detecting stale entries in the jobData map.
2635  qDebug() << m_parent << "numJobs:" << jobData.count();
2636  QMapIterator<KIO::ListJob *, JobData> it(jobData);
2637  while (it.hasNext()) {
2638  it.next();
2639  qDebug() << (void*)it.key();
2640  qDebug() << it.key();
2641  }
2642 #endif
2643 
2644  return jobData.count();
2645 }
2646 
2647 void KDirLister::Private::jobDone( KIO::ListJob *job )
2648 {
2649  jobData.remove( job );
2650 }
2651 
2652 void KDirLister::Private::jobStarted( KIO::ListJob *job )
2653 {
2654  Private::JobData data;
2655  data.speed = 0;
2656  data.percent = 0;
2657  data.processedSize = 0;
2658  data.totalSize = 0;
2659 
2660  jobData.insert( job, data );
2661  complete = false;
2662 }
2663 
2664 void KDirLister::Private::connectJob( KIO::ListJob *job )
2665 {
2666  m_parent->connect( job, SIGNAL(infoMessage(KJob*,QString,QString)),
2667  m_parent, SLOT(_k_slotInfoMessage(KJob*,QString)) );
2668  m_parent->connect( job, SIGNAL(percent(KJob*,ulong)),
2669  m_parent, SLOT(_k_slotPercent(KJob*,ulong)) );
2670  m_parent->connect( job, SIGNAL(totalSize(KJob*,qulonglong)),
2671  m_parent, SLOT(_k_slotTotalSize(KJob*,qulonglong)) );
2672  m_parent->connect( job, SIGNAL(processedSize(KJob*,qulonglong)),
2673  m_parent, SLOT(_k_slotProcessedSize(KJob*,qulonglong)) );
2674  m_parent->connect( job, SIGNAL(speed(KJob*,ulong)),
2675  m_parent, SLOT(_k_slotSpeed(KJob*,ulong)) );
2676 }
2677 
2678 void KDirLister::setMainWindow( QWidget *window )
2679 {
2680  d->window = window;
2681 }
2682 
2683 QWidget *KDirLister::mainWindow()
2684 {
2685  return d->window;
2686 }
2687 
2688 KFileItemList KDirLister::items( WhichItems which ) const
2689 {
2690  return itemsForDir( url(), which );
2691 }
2692 
2693 KFileItemList KDirLister::itemsForDir( const KUrl& dir, WhichItems which ) const
2694 {
2695  KFileItemList *allItems = kDirListerCache->itemsForDir( dir );
2696  if ( !allItems )
2697  return KFileItemList();
2698 
2699  if ( which == AllItems )
2700  return *allItems;
2701  else // only items passing the filters
2702  {
2703  KFileItemList result;
2704  KFileItemList::const_iterator kit = allItems->constBegin();
2705  const KFileItemList::const_iterator kend = allItems->constEnd();
2706  for ( ; kit != kend; ++kit )
2707  {
2708  const KFileItem& item = *kit;
2709  if (d->isItemVisible(item) && matchesMimeFilter(item)) {
2710  result.append(item);
2711  }
2712  }
2713  return result;
2714  }
2715 }
2716 
2717 bool KDirLister::delayedMimeTypes() const
2718 {
2719  return d->delayedMimeTypes;
2720 }
2721 
2722 void KDirLister::setDelayedMimeTypes( bool delayedMimeTypes )
2723 {
2724  d->delayedMimeTypes = delayedMimeTypes;
2725 }
2726 
2727 // called by KDirListerCache::slotRedirection
2728 void KDirLister::Private::redirect(const KUrl& oldUrl, const KUrl& newUrl, bool keepItems)
2729 {
2730  if ( url.equals( oldUrl, KUrl::CompareWithoutTrailingSlash ) ) {
2731  if (!keepItems) {
2732  rootFileItem = KFileItem();
2733  } else {
2734  rootFileItem.setUrl(newUrl);
2735  }
2736  url = newUrl;
2737  }
2738 
2739  const int idx = lstDirs.indexOf( oldUrl );
2740  if (idx == -1) {
2741  kWarning(7004) << "Unexpected redirection from" << oldUrl << "to" << newUrl
2742  << "but this dirlister is currently listing/holding" << lstDirs;
2743  } else {
2744  lstDirs[ idx ] = newUrl;
2745  }
2746 
2747  if ( lstDirs.count() == 1 ) {
2748  if (!keepItems)
2749  emit m_parent->clear();
2750  emit m_parent->redirection( newUrl );
2751  } else {
2752  if (!keepItems)
2753  emit m_parent->clear( oldUrl );
2754  }
2755  emit m_parent->redirection( oldUrl, newUrl );
2756 }
2757 
2758 void KDirListerCacheDirectoryData::moveListersWithoutCachedItemsJob(const KUrl& url)
2759 {
2760  // Move dirlisters from listersCurrentlyListing to listersCurrentlyHolding,
2761  // but not those that are still waiting on a CachedItemsJob...
2762  // Unit-testing note:
2763  // Run kdirmodeltest in valgrind to hit the case where an update
2764  // is triggered while a lister has a CachedItemsJob (different timing...)
2765  QMutableListIterator<KDirLister *> lister_it(listersCurrentlyListing);
2766  while (lister_it.hasNext()) {
2767  KDirLister* kdl = lister_it.next();
2768  if (!kdl->d->cachedItemsJobForUrl(url)) {
2769  // OK, move this lister from "currently listing" to "currently holding".
2770 
2771  // Huh? The KDirLister was present twice in listersCurrentlyListing, or was in both lists?
2772  Q_ASSERT(!listersCurrentlyHolding.contains(kdl));
2773  if (!listersCurrentlyHolding.contains(kdl)) {
2774  listersCurrentlyHolding.append(kdl);
2775  }
2776  lister_it.remove();
2777  } else {
2778  //kDebug(7004) << "Not moving" << kdl << "to listersCurrentlyHolding because it still has job" << kdl->d->m_cachedItemsJobs;
2779  }
2780  }
2781 }
2782 
2783 KFileItem KDirLister::cachedItemForUrl(const KUrl& url)
2784 {
2785  return kDirListerCache->itemForUrl(url);
2786 }
2787 
2788 #include "kdirlister.moc"
2789 #include "kdirlister_p.moc"
KIO::JobUiDelegate::setWindow
virtual void setWindow(QWidget *window)
Associate this job with a window given by window.
Definition: jobuidelegate.cpp:58
KIO::UDSEntry::UDS_URL
An alternative URL (If different from the caption).
Definition: udsentry.h:190
KDirLister::Private::CachedItemsJob::doKill
virtual bool doKill()
Definition: kdirlister.cpp:311
kdirlister.h
KDirListerCache::slotFilesRemoved
void slotFilesRemoved(const QStringList &fileList)
Notify that files have been deleted.
Definition: kdirlister.cpp:870
KDirLister::Private::JobData::speed
long unsigned int speed
Definition: kdirlister_p.h:135
i18n
QString i18n(const char *text)
KCompositeJob::kill
bool kill(KillVerbosity verbosity=Quietly)
KDirLister::percent
void percent(int percent)
Progress signal showing the overall progress of the KDirLister.
KDirLister::~KDirLister
virtual ~KDirLister()
Destroy the directory lister.
Definition: kdirlister.cpp:2070
KDirLister::Private::rootFileItem
KFileItem rootFileItem
Definition: kdirlister_p.h:142
KSharedPtr
Definition: kprotocolmanager.h:31
KUrl::adjustPath
void adjustPath(AdjustPathOption trailing)
KDirLister::deleteItem
QT_MOC_COMPAT void deleteItem(const KFileItem &_fileItem)
Signals that an item has been deleted.
KDirListerCacheDirectoryData
Definition: kdirlister_p.h:448
KIO::filesize_t
qulonglong filesize_t
64-bit file size
Definition: global.h:57
KUrl::directory
QString directory(const DirectoryOptions &options=IgnoreTrailingSlash) const
KUrl::RemoveTrailingSlash
KFileItem::determineMimeType
KMimeType::Ptr determineMimeType() const
Returns the mimetype of the file item.
Definition: kfileitem.cpp:779
KDirWatch::self
static KDirWatch * self()
KDirLister::setShowingDotFiles
virtual void setShowingDotFiles(bool _showDotFiles)
Changes the "is viewing dot files" setting.
Definition: kdirlister.cpp:2123
KDirLister::findByName
virtual KFileItem findByName(const QString &name) const
Find an item by its name.
Definition: kdirlister.cpp:2260
KDirLister::Private::autoErrorHandling
bool autoErrorHandling
Definition: kdirlister_p.h:131
KFileItem::isDir
bool isDir() const
Returns true if this item represents a directory.
Definition: kfileitem.cpp:1147
KDirLister::matchesMimeFilter
bool matchesMimeFilter(const QString &mime) const
Checks whether mime matches a filter in the list of mime types.
Definition: kdirlister.cpp:2327
kdebug.h
KDirLister::Private::_k_slotInfoMessage
void _k_slotInfoMessage(KJob *, const QString &)
Definition: kdirlister.cpp:2558
KUrl::AddTrailingSlash
KDirLister::Private::m_parent
KDirLister * m_parent
Definition: kdirlister_p.h:112
KDirLister::setAutoUpdate
virtual void setAutoUpdate(bool enable)
Enable/disable automatic directory updating, when a directory changes (using KDirWatch).
Definition: kdirlister.cpp:2109
KDirLister::directories
KUrl::List directories() const
Returns all URLs that are listed by this KDirLister.
Definition: kdirlister.cpp:2162
KFileItem::mimetype
QString mimetype() const
Returns the mimetype of the file item.
Definition: kfileitem.cpp:770
kmountpoint.h
KDirLister::Private::oldSettings
FilterSettings oldSettings
Definition: kdirlister_p.h:163
timeout
int timeout
KIO::ListJob
A ListJob is allows you to get the get the content of a directory.
Definition: jobclasses.h:936
KFileItem::isNull
bool isNull() const
Return true if default-constructed.
Definition: kfileitem.cpp:1720
KDirLister::delayedMimeTypes
bool delayedMimeTypes() const
KDirLister::Private::emitChanges
void emitChanges()
Definition: kdirlister.cpp:2172
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
KDirLister::handleError
virtual void handleError(KIO::Job *)
Reimplement to customize error handling.
Definition: kdirlister.cpp:2401
KIO::HideProgressInfo
Hide progress information dialog, i.e.
Definition: jobclasses.h:51
QWidget
KDirLister::Keep
Previous directories aren't forgotten (they are still watched by kdirwatch and their items are kept f...
Definition: kdirlister.h:76
KFileItem::refresh
void refresh()
Throw away and re-read (for local files) all information about the file.
Definition: kfileitem.cpp:512
KUrl::cleanPath
void cleanPath(const CleanPathOption &options=SimplifyDirSeparators)
dirs
KStandardDirs * dirs()
KDE::stat
int stat(const QString &path, KDE_struct_stat *buf)
name
const char * name(StandardAction id)
KDirLister::openUrl
virtual bool openUrl(const KUrl &_url, OpenUrlFlags _flags=NoFlags)
Run the directory lister on the given url.
Definition: kdirlister.cpp:2083
KIO::SimpleJob::url
const KUrl & url() const
Returns the SimpleJob's URL.
Definition: job.cpp:341
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDirLister::clearMimeFilter
virtual void clearMimeFilter()
Clears the mime based filter.
Definition: kdirlister.cpp:2310
KDirLister::findByUrl
virtual KFileItem findByUrl(const KUrl &_url) const
Find an item by its URL.
Definition: kdirlister.cpp:2250
KFileItem::setUrl
void setUrl(const KUrl &url)
Sets the item's URL.
Definition: kfileitem.cpp:543
KDirLister::Private::CachedItemsJob::done
void done()
Definition: kdirlister.cpp:303
KDirLister::cachedItemForUrl
static KFileItem cachedItemForUrl(const KUrl &url)
Return the KFileItem for the given URL, if we listed it recently and it's still in the cache - which ...
Definition: kdirlister.cpp:2783
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
KFileItem::setName
void setName(const QString &name)
Sets the item's name (i.e.
Definition: kfileitem.cpp:554
KDirLister::Private::nameFilter
QString nameFilter
Definition: kdirlister_p.h:152
KDirLister::Private::FilterSettings::mimeExcludeFilter
QStringList mimeExcludeFilter
Definition: kdirlister_p.h:160
KDirListerCacheDirectoryData::listersCurrentlyListing
QList< KDirLister * > listersCurrentlyListing
Definition: kdirlister_p.h:457
KDirLister::rootItem
KFileItem rootItem() const
Returns the file item of the URL.
Definition: kdirlister.cpp:2245
KDirListerCache::forgetCachedItemsJob
void forgetCachedItemsJob(KDirLister::Private::CachedItemsJob *job, KDirLister *lister, const KUrl &url)
Definition: kdirlister.cpp:365
QString
KUrl::CompareWithoutTrailingSlash
KDirLister::setMainWindow
void setMainWindow(QWidget *window)
Pass the main window this object is associated with this is used for caching authentication data...
Definition: kdirlister.cpp:2678
QHash
KDirLister::nameFilter
QString nameFilter() const
Returns the current name filter, as set via setNameFilter()
KDirLister::dirOnlyMode
bool dirOnlyMode() const
Checks whether the KDirLister only lists directories or all files.
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDirListerCache::stopListingUrl
void stopListingUrl(KDirLister *lister, const KUrl &_url, bool silent=false)
Definition: kdirlister.cpp:437
KUrl::isParentOf
bool isParentOf(const KUrl &u) const
KDirLister::Private::addNewItem
void addNewItem(const KUrl &directoryUrl, const KFileItem &item)
Definition: kdirlister.cpp:2410
klocale.h
manually_mounted
static bool manually_mounted(const QString &path, const KMountPoint::List &possibleMountPoints)
Definition: kdirlister.cpp:530
KDirLister::Private::_k_slotPercent
void _k_slotPercent(KJob *, unsigned long)
Definition: kdirlister.cpp:2563
KMountPoint::NeedMountOptions
KDirLister::Private::connectJob
void connectJob(KIO::ListJob *)
Definition: kdirlister.cpp:2664
KDirListerCache::slotFileRenamed
void slotFileRenamed(const QString &srcUrl, const QString &dstUrl)
Definition: kdirlister.cpp:964
KUrl
KDirLister::Private::complete
bool complete
Definition: kdirlister_p.h:123
KDirLister::totalSize
void totalSize(KIO::filesize_t size)
Emitted when we know the size of the jobs.
kprotocolmanager.h
KUrl::setPath
void setPath(const QString &path)
KDirListerCache::itemForUrl
KFileItem itemForUrl(const KUrl &url) const
Definition: kdirlister.cpp:780
KDirLister::Private::autoUpdate
bool autoUpdate
Definition: kdirlister_p.h:125
KIO::Job::ui
JobUiDelegate * ui() const
Retrieves the UI delegate of this job.
Definition: job.cpp:90
KDirLister::Private::FilterSettings::dirOnlyMode
bool dirOnlyMode
Definition: kdirlister_p.h:157
KIO::listDir
ListJob * listDir(const KUrl &url, JobFlags flags=DefaultFlags, bool includeHidden=true)
List the contents of url, which is assumed to be a directory.
Definition: job.cpp:2739
KFileItem::isLocalFile
bool isLocalFile() const
Returns true if the file is a local file.
Definition: kfileitem.cpp:1581
KDirLister::autoErrorHandlingEnabled
bool autoErrorHandlingEnabled() const
Check whether auto error handling is enabled.
KDirListerCache::stop
void stop(KDirLister *lister, bool silent=false)
Definition: kdirlister.cpp:412
KDirListerCacheDirectoryData::listersCurrentlyHolding
QList< KDirLister * > listersCurrentlyHolding
Definition: kdirlister_p.h:459
KDirLister::Private::isItemVisible
bool isItemVisible(const KFileItem &item) const
Should this item be visible according to the current filter settings?
Definition: kdirlister.cpp:2530
KDirLister::stop
virtual void stop()
Stop listing all directories currently being listed.
Definition: kdirlister.cpp:2094
KDirLister::Private::FilterSettings::isShowingDotFiles
bool isShowingDotFiles
Definition: kdirlister_p.h:156
KDirLister::setNameFilter
virtual void setNameFilter(const QString &filter)
Set a name filter to only list items matching this name, e.g.
Definition: kdirlister.cpp:2268
KDirListerCache::updateDirectory
void updateDirectory(const KUrl &dir)
Definition: kdirlister.cpp:650
KUrl::addPath
void addPath(const QString &txt)
KDirLister::Private::_k_slotProcessedSize
void _k_slotProcessedSize(KJob *, qulonglong)
Definition: kdirlister.cpp:2601
KDirLister::doNameFilter
virtual bool doNameFilter(const QString &name, const QList< QRegExp > &filters) const
Called by the public matchesFilter() to do the actual filtering.
Definition: kdirlister.cpp:2360
KDirLister::Reload
Indicates whether to use the cache or to reread the directory from the disk.
Definition: kdirlister.h:81
KDirLister::Private::JobData::percent
long unsigned int percent
Definition: kdirlister_p.h:135
KDirLister::setDirOnlyMode
virtual void setDirOnlyMode(bool dirsOnly)
Call this to list only directories.
Definition: kdirlister.cpp:2137
KDirLister::Private::prepareForSettingsChange
void prepareForSettingsChange()
Definition: kdirlister_p.h:99
KDirLister::Private::hasPendingChanges
bool hasPendingChanges
Definition: kdirlister_p.h:129
KDirLister::Private::FilterSettings
Definition: kdirlister_p.h:154
KDirLister::Private::CachedItemsJob::start
void start()
Definition: kdirlister_p.h:475
KDirLister::Private::window
QWidget * window
Definition: kdirlister_p.h:149
KDirLister::Private::emitItemsDeleted
void emitItemsDeleted(const KFileItemList &items)
Definition: kdirlister.cpp:2539
KDirLister::Private::CachedItemsJob::CachedItemsJob
CachedItemsJob(KDirLister *lister, const KUrl &url, bool reload)
Definition: kdirlister.cpp:288
KDirLister::items
KFileItemList items(WhichItems which=FilteredItems) const
Returns the items listed for the current url().
Definition: kdirlister.cpp:2688
KDirLister::speed
void speed(int bytes_per_second)
Emitted to display information about the speed of the jobs.
KUrl::protocol
QString protocol() const
KDirLister::mainWindow
QWidget * mainWindow()
Returns the main window associated with this object.
Definition: kdirlister.cpp:2683
KDirLister::Private::errorParent
QWidget * errorParent
Definition: kdirlister_p.h:132
QStringList
kdirlister_p.h
KDirLister::infoMessage
void infoMessage(const QString &msg)
Emitted to display information about running jobs.
KFileItem::localPath
QString localPath() const
Returns the local path if isLocalFile() == true or the KIO item has a UDS_LOCAL_PATH atom...
Definition: kfileitem.cpp:602
KDirLister::setAutoErrorHandlingEnabled
void setAutoErrorHandlingEnabled(bool enable, QWidget *parent)
Enable or disable auto error handling is enabled.
Definition: kdirlister.cpp:2151
KDirLister::started
void started(const KUrl &_url)
Tell the view that we started to list _url.
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
KDirLister::isFinished
bool isFinished() const
Returns true if no io operation is currently in progress.
Definition: kdirlister.cpp:2240
KDirLister::canceled
void canceled()
Tell the view that the user canceled the listing.
KDirLister::completed
void completed()
Tell the view that listing is finished.
KDirListerCache::slotFilesChanged
void slotFilesChanged(const QStringList &fileList)
Notify that files have been changed.
Definition: kdirlister.cpp:930
KDirLister::Private::FilterSettings::lstFilters
QList< QRegExp > lstFilters
Definition: kdirlister_p.h:158
KDirLister::setDelayedMimeTypes
void setDelayedMimeTypes(bool delayedMimeTypes)
Delayed mimetypes feature: If enabled, mime types will be fetched on demand, which leads to a faster ...
Definition: kdirlister.cpp:2722
KDirLister::setMimeFilter
virtual void setMimeFilter(const QStringList &mimeList)
Set mime-based filter to only list items matching the given mimetypes.
Definition: kdirlister.cpp:2288
KDirLister::Private::jobStarted
void jobStarted(KIO::ListJob *)
Definition: kdirlister.cpp:2652
KProtocolInfo::protocolClass
static QString protocolClass(const QString &protocol)
KUrl::path
QString path(AdjustPathOption trailing=LeaveTrailingSlash) const
KDirLister::Private::FilterSettings::mimeFilter
QStringList mimeFilter
Definition: kdirlister_p.h:159
KDirLister::Private::jobDone
void jobDone(KIO::ListJob *)
Definition: kdirlister.cpp:2647
KDirLister::AllItems
Definition: kdirlister.h:393
KCompositeJob::uiDelegate
KJobUiDelegate * uiDelegate() const
KFileItem::refreshMimeType
void refreshMimeType()
Re-reads mimetype information.
Definition: kfileitem.cpp:533
KDirListerCache::listDir
bool listDir(KDirLister *lister, const KUrl &_url, bool _keep, bool _reload)
Definition: kdirlister.cpp:91
KDirLister::showingDotFiles
bool showingDotFiles() const
Checks whether hidden files (files beginning with a dot) will be shown.
KDirLister::Private::m_cachedItemsJobs
QList< CachedItemsJob * > m_cachedItemsJobs
Definition: kdirlister_p.h:150
KDirListerCache::findByUrl
KFileItem * findByUrl(const KDirLister *lister, const KUrl &url) const
Definition: kdirlister.cpp:821
KDirLister::Private
Definition: kdirlister_p.h:42
KFileItem::text
QString text() const
Returns the text of the file item.
Definition: kfileitem.cpp:1589
jobuidelegate.h
KDirLister::url
KUrl url() const
Returns the top level URL that is listed by this KDirLister.
Definition: kdirlister.cpp:2157
QSet
KDirLister::Private::JobData::totalSize
KIO::filesize_t totalSize
Definition: kdirlister_p.h:136
KDirLister::clear
void clear()
Signal to clear all items.
KDirLister::WhichItems
WhichItems
Used by items() and itemsForDir() to specify whether you want all items for a directory or just the f...
Definition: kdirlister.h:391
KDirLister::Private::cachedItemsJobForUrl
CachedItemsJob * cachedItemsJobForUrl(const KUrl &url) const
Definition: kdirlister.cpp:279
KDirLister::doMimeFilter
virtual bool doMimeFilter(const QString &mime, const QStringList &filters) const
Called by the public matchesMimeFilter() to do the actual filtering.
Definition: kdirlister.cpp:2369
KDirLister::Private::emitItems
void emitItems()
Definition: kdirlister.cpp:2487
KDirListerCache::forgetDirs
void forgetDirs(KDirLister *lister)
Definition: kdirlister.cpp:511
KDirLister::processedSize
void processedSize(KIO::filesize_t size)
Regularly emitted to show the progress of this KDirLister.
job.h
KDirLister::Private::delayedMimeTypes
bool delayedMimeTypes
Definition: kdirlister_p.h:127
KUrl::List
KDirLister::Private::redirect
void redirect(const KUrl &oldUrl, const KUrl &newUrl, bool keepItems)
Redirect this dirlister from oldUrl to newUrl.
Definition: kdirlister.cpp:2728
KDirListerCache
Design of the cache: There is a single KDirListerCache for the whole process.
Definition: kdirlister_p.h:181
KDirListerCache::setAutoUpdate
void setAutoUpdate(KDirLister *lister, bool enable)
Definition: kdirlister.cpp:496
KDirListerCache::findByName
KFileItem findByName(const KDirLister *lister, const QString &_name) const
Definition: kdirlister.cpp:805
OrgKdeKDirNotifyInterface
Proxy class for interface org.kde.KDirNotify.
Definition: kdirnotify.h:47
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
KJobUiDelegate::showErrorMessage
virtual void showErrorMessage()
KDirLister::Private::CachedItemsJob::setEmitCompleted
void setEmitCompleted(bool b)
Definition: kdirlister_p.h:478
KDirLister::redirection
void redirection(const KUrl &_url)
Signal a redirection.
KMountPoint::possibleMountPoints
static List possibleMountPoints(DetailsNeededFlags infoNeeded=BasicInfoNeeded)
KDirLister::autoUpdate
bool autoUpdate() const
Checks whether KDirWatch will automatically update directories.
KDirLister::Private::lstDirs
KUrl::List lstDirs
List of dirs handled by this dirlister.
Definition: kdirlister_p.h:118
KMountPoint::List::findByPath
Ptr findByPath(const QString &path) const
KFileItem::isMarked
bool isMarked() const
Used when updating a directory.
Definition: kfileitem.cpp:1686
KFileItem::name
QString name(bool lowerCase=false) const
Return the name of the file item (without a path).
Definition: kfileitem.cpp:1597
KIO::UDSEntry::UDS_NAME
Filename - as displayed in directory listings etc.
Definition: udsentry.h:163
KDirLister::Private::_k_slotTotalSize
void _k_slotTotalSize(KJob *, qulonglong)
Definition: kdirlister.cpp:2586
KDirLister::Private::JobData::processedSize
KIO::filesize_t processedSize
Definition: kdirlister_p.h:136
KUrl::IgnoreTrailingSlash
KDirListerCache::emitItemsFromCache
void emitItemsFromCache(KDirLister::Private::CachedItemsJob *job, KDirLister *lister, const KUrl &_url, bool _reload, bool _emitCompleted)
Definition: kdirlister.cpp:323
KDirListerCache::slotFilesAdded
void slotFilesAdded(const QString &urlDirectory)
Notify that files have been added in directory The receiver will list that directory again to find th...
Definition: kdirlister.cpp:857
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:94
KDirLister::Private::JobData
Definition: kdirlister_p.h:134
KFileItem::isHidden
bool isHidden() const
Checks whether the file is hidden.
Definition: kfileitem.cpp:1131
KUrl::AppendTrailingSlash
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDirLister::Private::numJobs
uint numJobs()
Definition: kdirlister.cpp:2631
KMountPoint::List
KDirLister::Private::_k_slotSpeed
void _k_slotSpeed(KJob *, unsigned long)
Definition: kdirlister.cpp:2616
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KDirLister::Private::doMimeExcludeFilter
bool doMimeExcludeFilter(const QString &mimeExclude, const QStringList &filters) const
Definition: kdirlister.cpp:2388
KDirWatch::exists
static bool exists()
KProtocolManager::supportsListing
static bool supportsListing(const KUrl &url)
Returns whether the protocol can list files/objects.
Definition: kprotocolmanager.cpp:1032
KDirLister
Helper class for the kiojob used to list and update a directory.
Definition: kdirlister.h:57
KIO::ListJob::redirectionUrl
const KUrl & redirectionUrl() const
Returns the ListJob's redirection URL.
Definition: job.cpp:2779
KDirListerCacheDirectoryData::moveListersWithoutCachedItemsJob
void moveListersWithoutCachedItemsJob(const KUrl &url)
Definition: kdirlister.cpp:2758
KDirLister::Private::settings
FilterSettings settings
Definition: kdirlister_p.h:162
KFileItem::entry
KIO::UDSEntry entry() const
Returns the UDS entry.
Definition: kfileitem.cpp:1678
KDirLister::Private::CachedItemsJob::url
KUrl url() const
Definition: kdirlister_p.h:480
KUrl::isLocalFile
bool isLocalFile() const
kmessagebox.h
KDirLister::mimeFilter
QStringList mimeFilter
Definition: kdirlister.h:69
KDirLister::Private::CachedItemsJob
Definition: kdirlister_p.h:470
end
const KShortcut & end()
KDirLister::setMimeExcludeFilter
void setMimeExcludeFilter(const QStringList &mimeList)
Filtering should be done with KFileFilter.
Definition: kdirlister.cpp:2300
KDirLister::Private::addRefreshItem
void addRefreshItem(const KUrl &directoryUrl, const KFileItem &oldItem, const KFileItem &item)
Definition: kdirlister.cpp:2449
KDirListerCache::~KDirListerCache
~KDirListerCache()
Definition: kdirlister.cpp:75
KDirLister::mimeFilters
QStringList mimeFilters() const
Returns the list of mime based filters, as set via setMimeFilter().
Definition: kdirlister.cpp:2317
KJob
kFatal
static QDebug kFatal(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDirLister::itemsDeleted
void itemsDeleted(const KFileItemList &items)
Signal that items have been deleted.
KDirListerCache::itemsForDir
KFileItemList * itemsForDir(const KUrl &dir) const
Definition: kdirlister.cpp:799
KUrl::prettyUrl
QString prettyUrl(AdjustPathOption trailing=LeaveTrailingSlash) const
KDirLister::Private::addNewItems
void addNewItems(const KUrl &directoryUrl, const KFileItemList &items)
Definition: kdirlister.cpp:2438
KFileItem::url
KUrl url() const
Returns the url of the file.
Definition: kfileitem.cpp:1549
KDirLister::emitChanges
virtual void emitChanges()
Actually emit the changes made with setShowingDotFiles, setDirOnlyMode, setNameFilter and setMimeFilt...
Definition: kdirlister.cpp:2167
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QMap
Definition: netaccess.h:36
KDirLister::matchesFilter
bool matchesFilter(const QString &name) const
Checks whether name matches a filter in the list of name filters.
Definition: kdirlister.cpp:2322
KFileItem
A KFileItem is a generic class to handle a file, local or remote.
Definition: kfileitem.h:45
KDialogJobUiDelegate::window
QWidget * window() const
QList< UDSEntry >
KIO::number
QString number(KIO::filesize_t size)
Converts a size to a string representation Not unlike QString::number(...)
Definition: global.cpp:63
KDirLister::KDirLister
KDirLister(QObject *parent=0)
Create a directory lister.
Definition: kdirlister.cpp:2056
KCompositeJob::error
int error() const
KRecentDirs::list
QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:60
KDirLister::Private::url
KUrl url
Definition: kdirlister_p.h:121
KDirLister::itemsForDir
KFileItemList itemsForDir(const KUrl &dir, WhichItems which=FilteredItems) const
Returns the items listed for the given dir.
Definition: kdirlister.cpp:2693
KDirLister::updateDirectory
virtual void updateDirectory(const KUrl &_dir)
Update the directory _dir.
Definition: kdirlister.cpp:2235
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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