• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
kdirlister.cpp
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  2001-2005 Michael Brade <brade@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kdirlister.h"
23 
24 #include <tqregexp.h>
25 #include <tqptrlist.h>
26 #include <tqtimer.h>
27 #include <tqeventloop.h>
28 
29 #include <tdeapplication.h>
30 #include <kdebug.h>
31 #include <tdelocale.h>
32 #include <tdeio/job.h>
33 #include <tdemessagebox.h>
34 #include <tdeglobal.h>
35 #include <tdeglobalsettings.h>
36 #include <kstaticdeleter.h>
37 #include <kprotocolinfo.h>
38 
39 #include "kdirlister_p.h"
40 
41 #include <assert.h>
42 #include <unistd.h>
43 
44 KDirListerCache* KDirListerCache::s_pSelf = 0;
45 static KStaticDeleter<KDirListerCache> sd_KDirListerCache;
46 
47 // Enable this to get printDebug() called often, to see the contents of the cache
48 //#define DEBUG_CACHE
49 
50 // Make really sure it doesn't get activated in the final build
51 #ifdef NDEBUG
52 #undef DEBUG_CACHE
53 #endif
54 
55 KDirListerCache::KDirListerCache( int maxCount )
56  : itemsCached( maxCount )
57 {
58  kdDebug(7004) << "+KDirListerCache" << endl;
59 
60  itemsInUse.setAutoDelete( false );
61  itemsCached.setAutoDelete( true );
62  urlsCurrentlyListed.setAutoDelete( true );
63  urlsCurrentlyHeld.setAutoDelete( true );
64  pendingUpdates.setAutoDelete( true );
65 
66  connect( kdirwatch, TQT_SIGNAL( dirty( const KURL& ) ),
67  this, TQT_SLOT( slotFileDirty( const KURL& ) ) );
68  connect( kdirwatch, TQT_SIGNAL( created( const TQString& ) ),
69  this, TQT_SLOT( slotFileCreated( const TQString& ) ) );
70  connect( kdirwatch, TQT_SIGNAL( deleted( const TQString& ) ),
71  this, TQT_SLOT( slotFileDeleted( const TQString& ) ) );
72 }
73 
74 KDirListerCache::~KDirListerCache()
75 {
76  kdDebug(7004) << "-KDirListerCache" << endl;
77 
78  itemsInUse.setAutoDelete( true );
79  itemsInUse.clear();
80  itemsCached.clear();
81  urlsCurrentlyListed.clear();
82  urlsCurrentlyHeld.clear();
83 
84  if ( KDirWatch::exists() )
85  kdirwatch->disconnect( this );
86 }
87 
88 // setting _reload to true will emit the old files and
89 // call updateDirectory
90 bool KDirListerCache::listDir( KDirLister *lister, const KURL& _u,
91  bool _keep, bool _reload )
92 {
93  // HACK
94  // The media:/ tdeioslave has massive problems related to not properly updating its root directory
95  // Therefore, force a reload every time the media:/ tdeioslave root is accessed!
96  // FIXME
97  // For anyone wanting to tackle this problem, it was traced into the KDirListerCache::updateDirectory TDEIO::listDir TDEIO job
98  // Specifically, slotUpdateResult is never called for the root directory *iff* the user descends into an unmounted media device
99  // Strangely, slotUpdateResult *is* called if the user instead right-clicks on the unmounted media device and selects Mount from the context menu
100  if ((_u.protocol() == "media") && (_u.path() == "/")) {
101  _reload = true;
102  }
103 
104  // like this we don't have to worry about trailing slashes any further
105  KURL _url = _u;
106  _url.cleanPath(); // kill consecutive slashes
107  _url.adjustPath(-1);
108  TQString urlStr = _url.url();
109  TQString urlReferenceStr = _url.internalReferenceURL();
110 
111  if ( !lister->validURL( _url ) ) {
112  return false;
113  }
114 
115 #ifdef DEBUG_CACHE
116  printDebug();
117 #endif
118  kdDebug(7004) << k_funcinfo << lister << " url=" << _url
119  << " keep=" << _keep << " reload=" << _reload << endl;
120 
121  if ( !_keep )
122  {
123  // stop any running jobs for lister
124  stop( lister );
125 
126  // clear our internal list for lister
127  forgetDirs( lister );
128 
129  lister->d->rootFileItem = 0;
130  }
131  else if ( lister->d->lstDirs.find( _url ) != lister->d->lstDirs.end() )
132  {
133  // stop the job listing _url for this lister
134  stop( lister, _url );
135 
136  // clear _url for lister
137  forgetDirs( lister, _url, true );
138 
139  if ( lister->d->url == _url )
140  lister->d->rootFileItem = 0;
141  }
142 
143  lister->d->lstDirs.append( _url );
144 
145  if ( lister->d->url.isEmpty() || !_keep ) // set toplevel URL only if not set yet
146  lister->d->url = _url;
147 
148  DirItem *itemU = itemsInUse[urlStr + ":" + urlReferenceStr];
149  DirItem *itemC;
150 
151  if ( !urlsCurrentlyListed[urlStr + ":" + urlReferenceStr] )
152  {
153  // if there is an update running for _url already we get into
154  // the following case - it will just be restarted by updateDirectory().
155 
156  if ( itemU )
157  {
158  kdDebug(7004) << "listDir: Entry already in use: " << _url << endl;
159 
160  bool oldState = lister->d->complete;
161  lister->d->complete = false;
162 
163  emit lister->started( _url );
164 
165  if ( !lister->d->rootFileItem && lister->d->url == _url ) {
166  lister->d->rootFileItem = itemU->rootItem;
167  }
168 
169  lister->addNewItems( *(itemU->lstItems) );
170  lister->emitItems();
171 
172  // _url is already in use, so there is already an entry in urlsCurrentlyHeld
173  assert( urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr] );
174  urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr]->append( lister );
175 
176  lister->d->complete = oldState;
177 
178  lister->emitCompleted( _url );
179  if ( lister->d->complete ) {
180  emit lister->completed();
181  }
182 
183  if ( _reload || !itemU->complete ) {
184  updateDirectory( _url );
185  }
186  }
187  else if ( !_reload && (itemC = itemsCached.take( urlStr )) )
188  {
189  kdDebug(7004) << "listDir: Entry in cache: " << _url << endl;
190 
191  itemC->decAutoUpdate();
192  itemsInUse.insert( urlStr + ":" + urlReferenceStr, itemC );
193  itemU = itemC;
194 
195  bool oldState = lister->d->complete;
196  lister->d->complete = false;
197 
198  emit lister->started( _url );
199 
200  if ( !lister->d->rootFileItem && lister->d->url == _url ) {
201  lister->d->rootFileItem = itemC->rootItem;
202  }
203 
204  lister->addNewItems( *(itemC->lstItems) );
205  lister->emitItems();
206 
207  Q_ASSERT( !urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr] );
208  TQPtrList<KDirLister> *list = new TQPtrList<KDirLister>;
209  list->append( lister );
210  urlsCurrentlyHeld.insert( urlStr + ":" + urlReferenceStr, list );
211 
212  lister->d->complete = oldState;
213 
214  lister->emitCompleted( _url );
215  if ( lister->d->complete ) {
216  emit lister->completed();
217  }
218 
219  if ( !itemC->complete ) {
220  updateDirectory( _url );
221  }
222  }
223  else // dir not in cache or _reload is true
224  {
225  kdDebug(7004) << "listDir: Entry not in cache or reloaded: " << _url << endl;
226 
227  TQPtrList<KDirLister> *list = new TQPtrList<KDirLister>;
228  list->append( lister );
229  urlsCurrentlyListed.insert( urlStr + ":" + urlReferenceStr, list );
230 
231  itemsCached.remove( urlStr );
232  itemU = new DirItem( _url );
233  itemsInUse.insert( urlStr + ":" + urlReferenceStr, itemU );
234 
235 // // we have a limit of MAX_JOBS_PER_LISTER concurrently running jobs
236 // if ( lister->numJobs() >= MAX_JOBS_PER_LISTER )
237 // {
238 // lstPendingUpdates.append( _url );
239 // }
240 // else
241 // {
242 
243  if ( lister->d->url == _url ) {
244  lister->d->rootFileItem = 0;
245  }
246 
247  TDEIO::ListJob* job = TDEIO::listDir( _url, false /* no default GUI */ );
248  jobs.insert( job, TQValueList<TDEIO::UDSEntry>() );
249 
250  lister->jobStarted( job );
251  lister->connectJob( job );
252 
253  if ( lister->d->window ) {
254  job->setWindow( lister->d->window );
255  }
256 
257  connect( job, TQT_SIGNAL( entries( TDEIO::Job *, const TDEIO::UDSEntryList & ) ),
258  this, TQT_SLOT( slotEntries( TDEIO::Job *, const TDEIO::UDSEntryList & ) ) );
259  connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
260  this, TQT_SLOT( slotResult( TDEIO::Job * ) ) );
261  connect( job, TQT_SIGNAL( redirection( TDEIO::Job *, const KURL & ) ),
262  this, TQT_SLOT( slotRedirection( TDEIO::Job *, const KURL & ) ) );
263 
264  emit lister->started( _url );
265 
266 // }
267  }
268  }
269  else
270  {
271  kdDebug(7004) << "listDir: Entry currently being listed: " << _url << endl;
272 
273  emit lister->started( _url );
274 
275  urlsCurrentlyListed[urlStr + ":" + urlReferenceStr]->append( lister );
276 
277  TDEIO::ListJob *job = jobForUrl( urlStr + ":" + urlReferenceStr );
278  Q_ASSERT( job );
279 
280  lister->jobStarted( job );
281  lister->connectJob( job );
282 
283  Q_ASSERT( itemU );
284 
285  if ( !lister->d->rootFileItem && lister->d->url == _url ) {
286  lister->d->rootFileItem = itemU->rootItem;
287  }
288 
289  lister->addNewItems( *(itemU->lstItems) );
290  lister->emitItems();
291  }
292 
293  // automatic updating of directories
294  if ( lister->d->autoUpdate ) {
295  itemU->incAutoUpdate();
296  }
297 
298  return true;
299 }
300 
301 bool KDirListerCache::validURL( const KDirLister *lister, const KURL& url ) const
302 {
303  if ( !url.isValid() )
304  {
305  if ( lister->d->autoErrorHandling )
306  {
307  TQString tmp = i18n("Malformed URL\n%1").arg( url.prettyURL() );
308  KMessageBox::error( lister->d->errorParent, tmp );
309  }
310  return false;
311  }
312 
313  if ( !KProtocolInfo::supportsListing( url ) )
314  {
315  if ( lister->d->autoErrorHandling )
316  {
317  // TODO: this message should be changed during next string unfreeze!
318  TQString tmp = i18n("Malformed URL\n%1").arg( url.prettyURL() );
319  KMessageBox::error( lister->d->errorParent, tmp );
320  }
321  return false;
322  }
323 
324  return true;
325 }
326 
327 void KDirListerCache::stop( KDirLister *lister )
328 {
329 #ifdef DEBUG_CACHE
330  printDebug();
331 #endif
332  kdDebug(7004) << k_funcinfo << "lister: " << lister << endl;
333  bool stopped = false;
334 
335  TQDictIterator< TQPtrList<KDirLister> > it( urlsCurrentlyListed );
336  TQPtrList<KDirLister> *listers;
337  int curIndex;
338  while ( (listers = it.current()) )
339  {
340  curIndex = listers->findRef( lister );
341  if ( curIndex > -1 )
342  {
343  // lister is listing url
344  TQString url = it.currentKey();
345  KDirLister* curLister = listers->at( curIndex );
346 
347  //kdDebug(7004) << k_funcinfo << " found lister in list - for " << url << endl;
348  bool ret = listers->removeRef( lister );
349  Q_ASSERT( ret );
350 
351  TDEIO::ListJob *job = jobForUrl( url );
352  if ( job ) {
353  lister->jobDone( job );
354  }
355 
356  // move lister to urlsCurrentlyHeld
357  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld[url];
358  if ( !holders )
359  {
360  holders = new TQPtrList<KDirLister>;
361  urlsCurrentlyHeld.insert( url, holders );
362  }
363 
364  holders->append( lister );
365 
366  emit lister->canceled( curLister->d->url );
367 
368  //kdDebug(7004) << k_funcinfo << "remaining list: " << listers->count() << " listers" << endl;
369 
370  if ( listers->isEmpty() )
371  {
372  // kill the job since it isn't used any more
373  if ( job ) {
374  killJob( job );
375  }
376 
377  urlsCurrentlyListed.remove( url );
378  }
379 
380  stopped = true;
381  }
382  else
383  ++it;
384  }
385 
386  if ( stopped )
387  {
388  emit lister->canceled();
389  lister->d->complete = true;
390  }
391 
392  // this is wrong if there is still an update running!
393  //Q_ASSERT( lister->d->complete );
394 }
395 
396 void KDirListerCache::stop( KDirLister *lister, const KURL& _u )
397 {
398  TQString urlStr( _u.url(-1) );
399  TQString urlReferenceStr = _u.internalReferenceURL();
400  KURL _url( urlStr );
401 
402  // TODO: consider to stop all the "child jobs" of _url as well
403  kdDebug(7004) << k_funcinfo << lister << " url=" << _url << endl;
404 
405  TQPtrList<KDirLister> *listers = urlsCurrentlyListed[urlStr + ":" + urlReferenceStr];
406  if ( !listers || !listers->removeRef( lister ) )
407  return;
408 
409  // move lister to urlsCurrentlyHeld
410  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr];
411  if ( !holders )
412  {
413  holders = new TQPtrList<KDirLister>;
414  urlsCurrentlyHeld.insert( urlStr + ":" + urlReferenceStr, holders );
415  }
416 
417  holders->append( lister );
418 
419 
420  TDEIO::ListJob *job = jobForUrl( urlStr + ":" + urlReferenceStr );
421  if ( job ) {
422  lister->jobDone( job );
423  }
424 
425  emit lister->canceled( _url );
426 
427  if ( listers->isEmpty() )
428  {
429  // kill the job since it isn't used any more
430  if ( job )
431  killJob( job );
432 
433  urlsCurrentlyListed.remove( urlStr + ":" + urlReferenceStr );
434  }
435 
436  if ( lister->numJobs() == 0 )
437  {
438  lister->d->complete = true;
439 
440  // we killed the last job for lister
441  emit lister->canceled();
442  }
443 }
444 
445 void KDirListerCache::setAutoUpdate( KDirLister *lister, bool enable )
446 {
447  // IMPORTANT: this method does not check for the current autoUpdate state!
448 
449  for ( KURL::List::Iterator it = lister->d->lstDirs.begin();
450  it != lister->d->lstDirs.end(); ++it )
451  {
452  if ( enable ) {
453  itemsInUse[(*it).url() + ":" + (*it).internalReferenceURL()]->incAutoUpdate();
454  }
455  else {
456  itemsInUse[(*it).url() + ":" + (*it).internalReferenceURL()]->decAutoUpdate();
457  }
458  }
459 }
460 
461 void KDirListerCache::forgetDirs( KDirLister *lister )
462 {
463  kdDebug(7004) << k_funcinfo << lister << endl;
464 
465  emit lister->clear();
466 
467  // forgetDirs() will modify lstDirs, make a copy first
468  KURL::List lstDirsCopy = lister->d->lstDirs;
469  for ( KURL::List::Iterator it = lstDirsCopy.begin();
470  it != lstDirsCopy.end(); ++it )
471  {
472  forgetDirs( lister, *it, false );
473  }
474 }
475 
476 void KDirListerCache::forgetDirs( KDirLister *lister, const KURL& _url, bool notify )
477 {
478  kdDebug(7004) << k_funcinfo << lister << " _url: " << _url << endl;
479 
480  KURL url( _url );
481  url.adjustPath( -1 );
482  TQString urlStr = url.url();
483  TQString urlReferenceStr = url.internalReferenceURL();
484  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr];
485  //Q_ASSERT( holders );
486  if ( holders )
487  {
488  holders->removeRef( lister );
489  }
490 
491  // remove the dir from lister->d->lstDirs so that it doesn't contain things
492  // that itemsInUse doesn't. When emitting the canceled signals lstDirs must
493  // not contain anything that itemsInUse does not contain. (otherwise it
494  // might crash in findByName()).
495  lister->d->lstDirs.remove( lister->d->lstDirs.find( url ) );
496 
497  DirItem *item = itemsInUse[urlStr + ":" + urlReferenceStr];
498 
499  if ( holders && holders->isEmpty() )
500  {
501  urlsCurrentlyHeld.remove( urlStr + ":" + urlReferenceStr ); // this deletes the (empty) holders list
502  if ( !urlsCurrentlyListed[urlStr + ":" + urlReferenceStr] )
503  {
504  // item not in use anymore -> move into cache if complete
505  itemsInUse.remove( urlStr + ":" + urlReferenceStr );
506 
507  // this job is a running update
508  TDEIO::ListJob *job = jobForUrl( urlStr + ":" + urlReferenceStr );
509  if ( job )
510  {
511  lister->jobDone( job );
512  killJob( job );
513  kdDebug(7004) << k_funcinfo << "Killing update job for " << urlStr << endl;
514 
515  emit lister->canceled( url );
516  if ( lister->numJobs() == 0 )
517  {
518  lister->d->complete = true;
519  emit lister->canceled();
520  }
521  }
522 
523  if ( notify )
524  emit lister->clear( url );
525 
526  if ( item && item->complete )
527  {
528  kdDebug(7004) << k_funcinfo << lister << " item moved into cache: " << url << endl;
529  itemsCached.insert( urlStr, item ); // TODO: may return false!!
530 
531  // Should we forget the dir for good, or keep a watch on it?
532  // Generally keep a watch, except when it would prevent
533  // unmounting a removable device (#37780)
534  const bool isLocal = item->url.isLocalFile();
535  const bool isManuallyMounted = isLocal && TDEIO::manually_mounted( item->url.path() );
536  bool containsManuallyMounted = false;
537  if ( !isManuallyMounted && item->lstItems && isLocal )
538  {
539  // Look for a manually-mounted directory inside
540  // If there's one, we can't keep a watch either, FAM would prevent unmounting the CDROM
541  // I hope this isn't too slow (manually_mounted caches the last device so most
542  // of the time this is just a stat per subdir)
543  KFileItemListIterator kit( *item->lstItems );
544  for ( ; kit.current() && !containsManuallyMounted; ++kit )
545  if ( (*kit)->isDir() && TDEIO::manually_mounted( (*kit)->url().path() ) )
546  containsManuallyMounted = true;
547  }
548 
549  if ( isManuallyMounted || containsManuallyMounted )
550  {
551  kdDebug(7004) << "Not adding a watch on " << item->url << " because it " <<
552  ( isManuallyMounted ? "is manually mounted" : "contains a manually mounted subdir" ) << endl;
553  item->complete = false; // set to "dirty"
554  }
555  else
556  item->incAutoUpdate(); // keep watch
557  }
558  else
559  {
560  delete item;
561  item = 0;
562  }
563  }
564  }
565 
566  if ( item && lister->d->autoUpdate )
567  item->decAutoUpdate();
568 }
569 
570 void KDirListerCache::updateDirectory( const KURL& _dir )
571 {
572  kdDebug(7004) << k_funcinfo << _dir << endl;
573 
574  TQString urlStr = _dir.url(-1);
575  TQString urlReferenceStr = _dir.internalReferenceURL();
576  if ( !checkUpdate( _dir, -1 ) ) {
577  return;
578  }
579 
580  // A job can be running to
581  // - only list a new directory: the listers are in urlsCurrentlyListed
582  // - only update a directory: the listers are in urlsCurrentlyHeld
583  // - update a currently running listing: the listers are in urlsCurrentlyListed
584  // and urlsCurrentlyHeld
585 
586  TQPtrList<KDirLister> *listers = urlsCurrentlyListed[urlStr + ":" + urlReferenceStr];
587  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld[urlStr + ":" + urlReferenceStr];
588 
589  // restart the job for _dir if it is running already
590  bool killed = false;
591  TQWidget *window = 0;
592  TDEIO::ListJob *job = jobForUrl( urlStr + ":" + urlReferenceStr );
593  if ( job )
594  {
595  window = job->window();
596 
597  killJob( job );
598  killed = true;
599 
600  if ( listers ) {
601  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
602  kdl->jobDone( job );
603  }
604  }
605 
606  if ( holders ) {
607  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() ) {
608  kdl->jobDone( job );
609  }
610  }
611  }
612  kdDebug(7004) << k_funcinfo << "Killed = " << killed << endl;
613 
614  // we don't need to emit canceled signals since we only replaced the job,
615  // the listing is continuing.
616 
617  Q_ASSERT( !listers || (listers && killed) );
618 
619  job = TDEIO::listDir( _dir, false /* no default GUI */ );
620  jobs.insert( job, TQValueList<TDEIO::UDSEntry>() );
621 
622  connect( job, TQT_SIGNAL(entries( TDEIO::Job *, const TDEIO::UDSEntryList & )),
623  this, TQT_SLOT(slotUpdateEntries( TDEIO::Job *, const TDEIO::UDSEntryList & )) );
624  connect( job, TQT_SIGNAL(result( TDEIO::Job * )),
625  this, TQT_SLOT(slotUpdateResult( TDEIO::Job * )) );
626 
627  kdDebug(7004) << k_funcinfo << "update started in " << _dir << endl;
628 
629  if ( listers ) {
630  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
631  kdl->jobStarted( job );
632  }
633  }
634 
635  if ( holders )
636  {
637  if ( !killed )
638  {
639  bool first = true;
640  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
641  {
642  kdl->jobStarted( job );
643  if ( first && kdl->d->window )
644  {
645  first = false;
646  job->setWindow( kdl->d->window );
647  }
648  emit kdl->started( _dir );
649  }
650  }
651  else
652  {
653  job->setWindow( window );
654 
655  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() ) {
656  kdl->jobStarted( job );
657  }
658  }
659  }
660 }
661 
662 bool KDirListerCache::checkUpdate( const KURL& _dir, int truncationMode )
663 {
664  if ( !itemsInUse[_dir.url(truncationMode) + ":" + _dir.internalReferenceURL()] )
665  {
666  DirItem *item = itemsCached[_dir.url(truncationMode)];
667  if ( item && item->complete )
668  {
669  item->complete = false;
670  item->decAutoUpdate();
671  // Hmm, this debug output might include login/password from the _dir URL.
672  //kdDebug(7004) << k_funcinfo << "directory " << _dir << " not in use, marked dirty." << endl;
673  }
674  //else
675  //kdDebug(7004) << k_funcinfo << "aborted, directory " << _dir << " not in cache." << endl;
676 
677  return false;
678  }
679  else
680  return true;
681 }
682 
683 KFileItemList *KDirListerCache::itemsForDir( const KURL &_dir ) const
684 {
685  TQString urlStr = _dir.url(-1);
686  TQString urlReferenceStr = _dir.internalReferenceURL();
687  DirItem *item = itemsInUse[ urlStr + ":" + urlReferenceStr ];
688  if ( !item ) {
689  item = itemsCached[ urlStr ];
690  }
691  return item ? item->lstItems : 0;
692 }
693 
694 KFileItem *KDirListerCache::findByName( const KDirLister *lister, const TQString& _name ) const
695 {
696  Q_ASSERT( lister );
697 
698  for ( KURL::List::Iterator it = lister->d->lstDirs.begin();
699  it != lister->d->lstDirs.end(); ++it )
700  {
701  KFileItemListIterator kit( *itemsInUse[(*it).url() + ":" + (*it).internalReferenceURL()]->lstItems );
702  for ( ; kit.current(); ++kit )
703  if ( (*kit)->name() == _name )
704  return (*kit);
705  }
706 
707  return 0L;
708 }
709 
710 KFileItem *KDirListerCache::findByURL( const KDirLister *lister, const KURL& _u ) const
711 {
712  KURL _url = _u;
713  _url.adjustPath(-1);
714 
715  KURL parentDir( _url );
716  parentDir.setPath( parentDir.directory() );
717 
718  // If lister is set, check that it contains this dir
719  if ( lister && !lister->d->lstDirs.contains( parentDir ) )
720  return 0L;
721 
722  KFileItemList *itemList = itemsForDir( parentDir );
723  if ( itemList )
724  {
725  KFileItemListIterator kit( *itemList );
726  for ( ; kit.current(); ++kit )
727  if ( (*kit)->url() == _url )
728  return (*kit);
729  }
730  return 0L;
731 }
732 
733 void KDirListerCache::FilesAdded( const KURL &dir )
734 {
735  kdDebug(7004) << k_funcinfo << dir << endl;
736  updateDirectory( dir );
737 }
738 
739 void KDirListerCache::FilesRemoved( const KURL::List &fileList )
740 {
741  kdDebug(7004) << k_funcinfo << endl;
742  KURL::List::ConstIterator it = fileList.begin();
743  for ( ; it != fileList.end() ; ++it )
744  {
745  // emit the deleteItem signal if this file was shown in any view
746  KFileItem *fileitem = 0L;
747  KURL parentDir( *it );
748  parentDir.setPath( parentDir.directory() );
749  KFileItemList *lstItems = itemsForDir( parentDir );
750  if ( lstItems )
751  {
752  KFileItem *fit = lstItems->first();
753  for ( ; fit; fit = lstItems->next() )
754  if ( fit->url() == *it ) {
755  fileitem = fit;
756  lstItems->take(); // remove fileitem from list
757  break;
758  }
759  }
760 
761  // Tell the views about it before deleting the KFileItems. They might need the subdirs'
762  // file items (see the dirtree).
763  if ( fileitem )
764  {
765  TQPtrList<KDirLister> *listers = urlsCurrentlyHeld[parentDir.url() + ":" + parentDir.internalReferenceURL()];
766  if ( listers ) {
767  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
768  kdl->emitDeleteItem( fileitem );
769  }
770  }
771  }
772 
773  // If we found a fileitem, we can test if it's a dir. If not, we'll go to deleteDir just in case.
774  if ( !fileitem || fileitem->isDir() )
775  {
776  // in case of a dir, check if we have any known children, there's much to do in that case
777  // (stopping jobs, removing dirs from cache etc.)
778  deleteDir( *it );
779  }
780 
781  // now remove the item itself
782  delete fileitem;
783  }
784 }
785 
786 void KDirListerCache::FilesChanged( const KURL::List &fileList )
787 {
788  KURL::List dirsToUpdate;
789  kdDebug(7004) << k_funcinfo << "only half implemented" << endl;
790  KURL::List::ConstIterator it = fileList.begin();
791  for ( ; it != fileList.end() ; ++it )
792  {
793  if ( ( *it ).isLocalFile() )
794  {
795  kdDebug(7004) << "KDirListerCache::FilesChanged " << *it << endl;
796  KFileItem *fileitem = findByURL( 0, *it );
797  if ( fileitem )
798  {
799  // we need to refresh the item, because e.g. the permissions can have changed.
800  aboutToRefreshItem( fileitem );
801  fileitem->refresh();
802  emitRefreshItem( fileitem );
803  }
804  else {
805  kdDebug(7004) << "item not found" << endl;
806  }
807  } else {
808  // For remote files, refresh() won't be able to figure out the new information.
809  // Let's update the dir.
810  KURL dir( *it );
811  dir.setPath( dir.directory( true ) );
812  if ( dirsToUpdate.find( dir ) == dirsToUpdate.end() ) {
813  dirsToUpdate.prepend( dir );
814  }
815  }
816  }
817 
818  KURL::List::ConstIterator itdir = dirsToUpdate.begin();
819  for ( ; itdir != dirsToUpdate.end() ; ++itdir ) {
820  updateDirectory( *itdir );
821  }
822  // ## TODO problems with current jobs listing/updating that dir
823  // ( see kde-2.2.2's kdirlister )
824 }
825 
826 void KDirListerCache::FileRenamed( const KURL &src, const KURL &dst )
827 {
828  kdDebug(7004) << k_funcinfo << src.prettyURL() << " -> " << dst.prettyURL() << endl;
829 #ifdef DEBUG_CACHE
830  printDebug();
831 #endif
832 
833  // Somehow this should only be called if src is a dir. But how could we know if it is?
834  // (Note that looking into itemsInUse isn't good enough. One could rename a subdir in a view.)
835  renameDir( src, dst );
836 
837  // Now update the KFileItem representing that file or dir (not exclusive with the above!)
838  KURL oldurl( src );
839  oldurl.adjustPath( -1 );
840  KFileItem *fileitem = findByURL( 0, oldurl );
841  if ( fileitem )
842  {
843  if ( !fileitem->isLocalFile() && !fileitem->localPath().isEmpty() ) // it uses UDS_LOCAL_PATH? ouch, needs an update then
844  FilesChanged( src );
845  else
846  {
847  aboutToRefreshItem( fileitem );
848  fileitem->setURL( dst );
849  fileitem->refreshMimeType();
850  emitRefreshItem( fileitem );
851  }
852  }
853 #ifdef DEBUG_CACHE
854  printDebug();
855 #endif
856 }
857 
858 void KDirListerCache::aboutToRefreshItem( KFileItem *fileitem )
859 {
860  // Look whether this item was shown in any view, i.e. held by any dirlister
861  KURL parentDir( fileitem->url() );
862  parentDir.setPath( parentDir.directory() );
863  TQString parentDirURL = parentDir.url();
864  TQString parentDirReferenceURL = parentDir.internalReferenceURL();
865  TQPtrList<KDirLister> *listers = urlsCurrentlyHeld[parentDirURL + ":" + parentDirReferenceURL];
866  if ( listers )
867  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
868  kdl->aboutToRefreshItem( fileitem );
869 
870  // Also look in urlsCurrentlyListed, in case the user manages to rename during a listing
871  listers = urlsCurrentlyListed[parentDirURL + ":" + parentDirReferenceURL];
872  if ( listers )
873  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
874  kdl->aboutToRefreshItem( fileitem );
875 }
876 
877 void KDirListerCache::emitRefreshItem( KFileItem *fileitem )
878 {
879  // Look whether this item was shown in any view, i.e. held by any dirlister
880  KURL parentDir( fileitem->url() );
881  parentDir.setPath( parentDir.directory() );
882  TQString parentDirURL = parentDir.url();
883  TQString parentDirReferenceURL = parentDir.internalReferenceURL();
884  TQPtrList<KDirLister> *listers = urlsCurrentlyHeld[parentDirURL + ":" + parentDirReferenceURL];
885  if ( listers )
886  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
887  {
888  kdl->addRefreshItem( fileitem );
889  kdl->emitItems();
890  }
891 
892  // Also look in urlsCurrentlyListed, in case the user manages to rename during a listing
893  listers = urlsCurrentlyListed[parentDirURL + ":" + parentDirReferenceURL];
894  if ( listers )
895  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
896  {
897  kdl->addRefreshItem( fileitem );
898  kdl->emitItems();
899  }
900 }
901 
902 KDirListerCache* KDirListerCache::self()
903 {
904  if ( !s_pSelf )
905  s_pSelf = sd_KDirListerCache.setObject( s_pSelf, new KDirListerCache );
906 
907  return s_pSelf;
908 }
909 
910 bool KDirListerCache::exists()
911 {
912  return s_pSelf != 0;
913 }
914 
915 
916 // private slots
917 
918 // _file can also be a directory being currently held!
919 void KDirListerCache::slotFileDirty( const KURL& _url )
920 {
921  kdDebug(7004) << k_funcinfo << _url << endl;
922 
923  if ( !pendingUpdates[_url.path()] )
924  {
925  KURL dir;
926  dir.setPath( _url.path() );
927  dir.setInternalReferenceURL(_url.internalReferenceURL());
928  if ( checkUpdate( dir, -1 ) ) {
929  updateDirectory( _url );
930  }
931 
932  // the parent directory of _url.path()
933  dir.setPath( dir.directory() );
934  dir.setInternalReferenceURL(_url.internalReferenceURL());
935  if ( checkUpdate( dir ) )
936  {
937  // Nice hack to save memory: use the qt object name to store the filename
938  TQTimer *timer = new TQTimer( this, _url.path().utf8() );
939  connect( timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotFileDirtyDelayed()) );
940  pendingUpdates.insert( _url.path(), timer );
941  timer->start( 500, true );
942  }
943  }
944 }
945 
946 // delayed updating of files, FAM is flooding us with events
947 void KDirListerCache::slotFileDirtyDelayed()
948 {
949  TQString file = TQString::fromUtf8( TQT_TQOBJECT_CONST(sender())->name() );
950 
951  kdDebug(7004) << k_funcinfo << file << endl;
952 
953  // TODO: do it better: don't always create/delete the TQTimer but reuse it.
954  // Delete the timer after the parent directory is removed from the cache.
955  pendingUpdates.remove( file );
956 
957  KURL u;
958  u.setPath( file );
959  KFileItem *item = findByURL( 0, u ); // search all items
960  if ( item )
961  {
962  // we need to refresh the item, because e.g. the permissions can have changed.
963  aboutToRefreshItem( item );
964  item->refresh();
965  emitRefreshItem( item );
966  }
967 }
968 
969 void KDirListerCache::slotFileCreated( const TQString& _file )
970 {
971  kdDebug(7004) << k_funcinfo << _file << endl;
972  // XXX: how to avoid a complete rescan here?
973  KURL u;
974  u.setPath( _file );
975  u.setPath( u.directory() );
976  FilesAdded( u );
977 }
978 
979 void KDirListerCache::slotFileDeleted( const TQString& _file )
980 {
981  kdDebug(7004) << k_funcinfo << _file << endl;
982  KURL u;
983  u.setPath( _file );
984  FilesRemoved( u );
985 }
986 
987 void KDirListerCache::slotEntries( TDEIO::Job *job, const TDEIO::UDSEntryList &entries )
988 {
989  KURL url = joburl( static_cast<TDEIO::ListJob *>(job) );
990  url.adjustPath(-1);
991  TQString urlStr = url.url();
992  TQString urlReferenceStr = url.internalReferenceURL();
993 
994  kdDebug(7004) << k_funcinfo << "new entries for " << url << endl;
995 
996  DirItem *dir = itemsInUse[urlStr + ":" + urlReferenceStr];
997  Q_ASSERT( dir );
998 
999  TQPtrList<KDirLister> *listers = urlsCurrentlyListed[urlStr + ":" + urlReferenceStr];
1000  Q_ASSERT( listers );
1001  Q_ASSERT( !listers->isEmpty() );
1002 
1003  // check if anyone wants the mimetypes immediately
1004  bool delayedMimeTypes = true;
1005  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1006  delayedMimeTypes = delayedMimeTypes && kdl->d->delayedMimeTypes;
1007  }
1008 
1009  // avoid creating these QStrings again and again
1010  static const TQString& dot = TDEGlobal::staticQString(".");
1011  static const TQString& dotdot = TDEGlobal::staticQString("..");
1012 
1013  TDEIO::UDSEntryListConstIterator it = entries.begin();
1014  TDEIO::UDSEntryListConstIterator end = entries.end();
1015 
1016  for ( ; it != end; ++it )
1017  {
1018  TQString name;
1019 
1020  // find out about the name
1021  TDEIO::UDSEntry::ConstIterator entit = (*it).begin();
1022  for( ; entit != (*it).end(); ++entit ) {
1023  if ( (*entit).m_uds == TDEIO::UDS_NAME ) {
1024  name = (*entit).m_str;
1025  break;
1026  }
1027  }
1028 
1029  Q_ASSERT( !name.isEmpty() );
1030  if ( name.isEmpty() ) {
1031  continue;
1032  }
1033 
1034  if ( name == dot )
1035  {
1036  Q_ASSERT( !dir->rootItem );
1037  dir->rootItem = new KFileItem( *it, url, delayedMimeTypes, true );
1038 
1039  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1040  if ( !kdl->d->rootFileItem && kdl->d->url == url ) {
1041  kdl->d->rootFileItem = dir->rootItem;
1042  }
1043  }
1044  }
1045  else if ( name != dotdot )
1046  {
1047  KFileItem* item = new KFileItem( *it, url, delayedMimeTypes, true );
1048  Q_ASSERT( item );
1049 
1050  //kdDebug(7004)<< "Adding item: " << item->url() << endl;
1051  dir->lstItems->append( item );
1052 
1053  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1054  kdl->addNewItem( item );
1055  }
1056  }
1057  }
1058 
1059  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1060  kdl->emitItems();
1061  }
1062 }
1063 
1064 void KDirListerCache::slotResult( TDEIO::Job *j )
1065 {
1066  Q_ASSERT( j );
1067  TDEIO::ListJob *job = static_cast<TDEIO::ListJob *>( j );
1068  jobs.remove( job );
1069 
1070  KURL jobUrl = joburl( job );
1071  jobUrl.adjustPath(-1); // need remove trailing slashes again, in case of redirections
1072  TQString jobUrlStr = jobUrl.url();
1073  TQString jobReferenceUrlStr = jobUrl.internalReferenceURL();
1074 
1075  kdDebug(7004) << k_funcinfo << "finished listing " << jobUrl << endl;
1076 #ifdef DEBUG_CACHE
1077  printDebug();
1078 #endif
1079 
1080  TQPtrList<KDirLister> *listers = urlsCurrentlyListed.take( jobUrlStr + ":" + jobReferenceUrlStr );
1081  Q_ASSERT( listers );
1082 
1083  // move the directory to the held directories, do it before emitting
1084  // the signals to make sure it exists in KDirListerCache in case someone
1085  // calls listDir during the signal emission
1086  Q_ASSERT( !urlsCurrentlyHeld[jobUrlStr + ":" + jobReferenceUrlStr] );
1087  urlsCurrentlyHeld.insert( jobUrlStr + ":" + jobReferenceUrlStr, listers );
1088 
1089  KDirLister *kdl;
1090 
1091  if ( job->error() )
1092  {
1093  for ( kdl = listers->first(); kdl; kdl = listers->next() )
1094  {
1095  kdl->jobDone( job );
1096  kdl->handleError( job );
1097  emit kdl->canceled( jobUrl );
1098  if ( kdl->numJobs() == 0 )
1099  {
1100  kdl->d->complete = true;
1101  emit kdl->canceled();
1102  }
1103  }
1104  }
1105  else
1106  {
1107  DirItem *dir = itemsInUse[jobUrlStr + ":" + jobReferenceUrlStr];
1108  Q_ASSERT( dir );
1109  dir->complete = true;
1110 
1111  for ( kdl = listers->first(); kdl; kdl = listers->next() )
1112  {
1113  kdl->jobDone( job );
1114  kdl->emitCompleted( jobUrl );
1115  if ( kdl->numJobs() == 0 )
1116  {
1117  kdl->d->complete = true;
1118  emit kdl->completed();
1119  }
1120  }
1121  }
1122 
1123  // TODO: hmm, if there was an error and job is a parent of one or more
1124  // of the pending urls we should cancel it/them as well
1125  processPendingUpdates();
1126 
1127 #ifdef DEBUG_CACHE
1128  printDebug();
1129 #endif
1130 }
1131 
1132 void KDirListerCache::slotRedirection( TDEIO::Job *j, const KURL& url )
1133 {
1134  Q_ASSERT( j );
1135  TDEIO::ListJob *job = static_cast<TDEIO::ListJob *>( j );
1136 
1137  KURL oldUrl = job->url(); // here we really need the old url!
1138  KURL newUrl = url;
1139 
1140  // strip trailing slashes
1141  oldUrl.adjustPath(-1);
1142  newUrl.adjustPath(-1);
1143 
1144  if ( oldUrl == newUrl )
1145  {
1146  kdDebug(7004) << k_funcinfo << "New redirection url same as old, giving up." << endl;
1147  return;
1148  }
1149 
1150  kdDebug(7004) << k_funcinfo << oldUrl.prettyURL() << " -> " << newUrl.prettyURL() << endl;
1151 
1152 #ifdef DEBUG_CACHE
1153  printDebug();
1154 #endif
1155 
1156  // I don't think there can be dirItems that are childs of oldUrl.
1157  // Am I wrong here? And even if so, we don't need to delete them, right?
1158  // DF: redirection happens before listDir emits any item. Makes little sense otherwise.
1159 
1160  // oldUrl cannot be in itemsCached because only completed items are moved there
1161  DirItem *dir = itemsInUse.take( oldUrl.url() + ":" + oldUrl.internalReferenceURL() );
1162  Q_ASSERT( dir );
1163 
1164  TQPtrList<KDirLister> *listers = urlsCurrentlyListed.take( oldUrl.url() + ":" + oldUrl.internalReferenceURL() );
1165  Q_ASSERT( listers );
1166  Q_ASSERT( !listers->isEmpty() );
1167 
1168  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1169  {
1170  // TODO: put in own method?
1171  if ( kdl->d->url.equals( oldUrl, true ) )
1172  {
1173  kdl->d->rootFileItem = 0;
1174  kdl->d->url = newUrl;
1175  }
1176 
1177  *kdl->d->lstDirs.find( oldUrl ) = newUrl;
1178 
1179  if ( kdl->d->lstDirs.count() == 1 )
1180  {
1181  emit kdl->clear();
1182  emit kdl->redirection( newUrl );
1183  emit kdl->redirection( oldUrl, newUrl );
1184  }
1185  else
1186  {
1187  emit kdl->clear( oldUrl );
1188  emit kdl->redirection( oldUrl, newUrl );
1189  }
1190  }
1191 
1192  // when a lister was stopped before the job emits the redirection signal, the old url will
1193  // also be in urlsCurrentlyHeld
1194  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld.take( oldUrl.url() + ":" + oldUrl.internalReferenceURL() );
1195  if ( holders )
1196  {
1197  Q_ASSERT( !holders->isEmpty() );
1198 
1199  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1200  {
1201  kdl->jobStarted( job );
1202 
1203  // do it like when starting a new list-job that will redirect later
1204  emit kdl->started( oldUrl );
1205 
1206  // TODO: maybe don't emit started if there's an update running for newUrl already?
1207 
1208  if ( kdl->d->url.equals( oldUrl, true ) )
1209  {
1210  kdl->d->rootFileItem = 0;
1211  kdl->d->url = newUrl;
1212  }
1213 
1214  *kdl->d->lstDirs.find( oldUrl ) = newUrl;
1215 
1216  if ( kdl->d->lstDirs.count() == 1 )
1217  {
1218  emit kdl->clear();
1219  emit kdl->redirection( newUrl );
1220  emit kdl->redirection( oldUrl, newUrl );
1221  }
1222  else
1223  {
1224  emit kdl->clear( oldUrl );
1225  emit kdl->redirection( oldUrl, newUrl );
1226  }
1227  }
1228  }
1229 
1230  DirItem *newDir = itemsInUse[newUrl.url() + ":" + newUrl.internalReferenceURL()];
1231  if ( newDir )
1232  {
1233  kdDebug(7004) << "slotRedirection: " << newUrl.url() << " already in use" << endl;
1234 
1235  // only in this case there can newUrl already be in urlsCurrentlyListed or urlsCurrentlyHeld
1236  delete dir;
1237 
1238  // get the job if one's running for newUrl already (can be a list-job or an update-job), but
1239  // do not return this 'job', which would happen because of the use of redirectionURL()
1240  TDEIO::ListJob *oldJob = jobForUrl( newUrl.url() + ":" + newUrl.internalReferenceURL(), job );
1241 
1242  // listers of newUrl with oldJob: forget about the oldJob and use the already running one
1243  // which will be converted to an updateJob
1244  TQPtrList<KDirLister> *curListers = urlsCurrentlyListed[newUrl.url() + ":" + newUrl.internalReferenceURL()];
1245  if ( curListers )
1246  {
1247  kdDebug(7004) << "slotRedirection: and it is currently listed" << endl;
1248 
1249  Q_ASSERT( oldJob ); // ?!
1250 
1251  for ( KDirLister *kdl = curListers->first(); kdl; kdl = curListers->next() ) // listers of newUrl
1252  {
1253  kdl->jobDone( oldJob );
1254 
1255  kdl->jobStarted( job );
1256  kdl->connectJob( job );
1257  }
1258 
1259  // append listers of oldUrl with newJob to listers of newUrl with oldJob
1260  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1261  curListers->append( kdl );
1262  }
1263  else {
1264  urlsCurrentlyListed.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), listers );
1265  }
1266 
1267  if ( oldJob ) // kill the old job, be it a list-job or an update-job
1268  killJob( oldJob );
1269 
1270  // holders of newUrl: use the already running job which will be converted to an updateJob
1271  TQPtrList<KDirLister> *curHolders = urlsCurrentlyHeld[newUrl.url() + ":" + newUrl.internalReferenceURL()];
1272  if ( curHolders )
1273  {
1274  kdDebug(7004) << "slotRedirection: and it is currently held." << endl;
1275 
1276  for ( KDirLister *kdl = curHolders->first(); kdl; kdl = curHolders->next() ) // holders of newUrl
1277  {
1278  kdl->jobStarted( job );
1279  emit kdl->started( newUrl );
1280  }
1281 
1282  // append holders of oldUrl to holders of newUrl
1283  if ( holders )
1284  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1285  curHolders->append( kdl );
1286  }
1287  else if ( holders )
1288  urlsCurrentlyHeld.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), holders );
1289 
1290 
1291  // emit old items: listers, holders. NOT: newUrlListers/newUrlHolders, they already have them listed
1292  // TODO: make this a separate method?
1293  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1294  {
1295  if ( !kdl->d->rootFileItem && kdl->d->url == newUrl )
1296  kdl->d->rootFileItem = newDir->rootItem;
1297 
1298  kdl->addNewItems( *(newDir->lstItems) );
1299  kdl->emitItems();
1300  }
1301 
1302  if ( holders )
1303  {
1304  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1305  {
1306  if ( !kdl->d->rootFileItem && kdl->d->url == newUrl )
1307  kdl->d->rootFileItem = newDir->rootItem;
1308 
1309  kdl->addNewItems( *(newDir->lstItems) );
1310  kdl->emitItems();
1311  }
1312  }
1313  }
1314  else if ( (newDir = itemsCached.take( newUrl.url() )) )
1315  {
1316  kdDebug(7004) << "slotRedirection: " << newUrl.url() << " is unused, but already in the cache." << endl;
1317 
1318  delete dir;
1319  itemsInUse.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), newDir );
1320  urlsCurrentlyListed.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), listers );
1321  if ( holders )
1322  urlsCurrentlyHeld.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), holders );
1323 
1324  // emit old items: listers, holders
1325  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1326  {
1327  if ( !kdl->d->rootFileItem && kdl->d->url == newUrl )
1328  kdl->d->rootFileItem = newDir->rootItem;
1329 
1330  kdl->addNewItems( *(newDir->lstItems) );
1331  kdl->emitItems();
1332  }
1333 
1334  if ( holders )
1335  {
1336  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1337  {
1338  if ( !kdl->d->rootFileItem && kdl->d->url == newUrl )
1339  kdl->d->rootFileItem = newDir->rootItem;
1340 
1341  kdl->addNewItems( *(newDir->lstItems) );
1342  kdl->emitItems();
1343  }
1344  }
1345  }
1346  else
1347  {
1348  kdDebug(7004) << "slotRedirection: " << newUrl.url() << " has not been listed yet." << endl;
1349 
1350  delete dir->rootItem;
1351  dir->rootItem = 0;
1352  dir->lstItems->clear();
1353  dir->redirect( newUrl );
1354  itemsInUse.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), dir );
1355  urlsCurrentlyListed.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), listers );
1356 
1357  if ( holders )
1358  urlsCurrentlyHeld.insert( newUrl.url() + ":" + newUrl.internalReferenceURL(), holders );
1359  else
1360  {
1361 #ifdef DEBUG_CACHE
1362  printDebug();
1363 #endif
1364  return; // only in this case the job doesn't need to be converted,
1365  }
1366  }
1367 
1368  // make the job an update job
1369  job->disconnect( this );
1370 
1371  connect( job, TQT_SIGNAL(entries( TDEIO::Job *, const TDEIO::UDSEntryList & )),
1372  this, TQT_SLOT(slotUpdateEntries( TDEIO::Job *, const TDEIO::UDSEntryList & )) );
1373  connect( job, TQT_SIGNAL(result( TDEIO::Job * )),
1374  this, TQT_SLOT(slotUpdateResult( TDEIO::Job * )) );
1375 
1376  // FIXME: autoUpdate-Counts!!
1377 
1378 #ifdef DEBUG_CACHE
1379  printDebug();
1380 #endif
1381 }
1382 
1383 void KDirListerCache::renameDir( const KURL &oldUrl, const KURL &newUrl )
1384 {
1385  kdDebug(7004) << k_funcinfo << oldUrl.prettyURL() << " -> " << newUrl.prettyURL() << endl;
1386  TQString oldUrlStr = oldUrl.url(-1);
1387  TQString newUrlStr = newUrl.url(-1);
1388 
1389  // Not enough. Also need to look at any child dir, even sub-sub-sub-dir.
1390  //DirItem *dir = itemsInUse.take( oldUrlStr );
1391  //emitRedirections( oldUrl, url );
1392 
1393  // Look at all dirs being listed/shown
1394  TQDictIterator<DirItem> itu( itemsInUse );
1395  bool goNext;
1396  while ( itu.current() )
1397  {
1398  goNext = true;
1399  DirItem *dir = itu.current();
1400  TQString oldDirURLIndep = itu.currentKey();
1401  oldDirURLIndep.truncate(oldDirURLIndep.length() - (dir->url.internalReferenceURL().length()+strlen(":")));
1402  KURL oldDirUrl ( oldDirURLIndep );
1403  //kdDebug(7004) << "itemInUse: " << oldDirUrl.prettyURL() << endl;
1404  // Check if this dir is oldUrl, or a subfolder of it
1405  if ( oldUrl.isParentOf( oldDirUrl ) )
1406  {
1407  // TODO should use KURL::cleanpath like isParentOf does
1408  TQString relPath = oldDirUrl.path().mid( oldUrl.path().length() );
1409 
1410  KURL newDirUrl( newUrl ); // take new base
1411  if ( !relPath.isEmpty() ) {
1412  newDirUrl.addPath( relPath ); // add unchanged relative path
1413  }
1414  //kdDebug(7004) << "KDirListerCache::renameDir new url=" << newDirUrl.prettyURL() << endl;
1415 
1416  // Update URL in dir item and in itemsInUse
1417  dir->redirect( newDirUrl );
1418  itemsInUse.remove( itu.currentKey() ); // implies ++itu
1419  itemsInUse.insert( newDirUrl.url(-1), dir );
1420  goNext = false; // because of the implied ++itu above
1421  if ( dir->lstItems )
1422  {
1423  // Rename all items under that dir
1424  KFileItemListIterator kit( *dir->lstItems );
1425  for ( ; kit.current(); ++kit )
1426  {
1427  KURL oldItemUrl = (*kit)->url();
1428  TQString oldItemUrlStr( oldItemUrl.url(-1) );
1429  KURL newItemUrl( oldItemUrl );
1430  newItemUrl.setPath( newDirUrl.path() );
1431  newItemUrl.addPath( oldItemUrl.fileName() );
1432  kdDebug(7004) << "KDirListerCache::renameDir renaming " << oldItemUrlStr << " to " << newItemUrl.url() << endl;
1433  (*kit)->setURL( newItemUrl );
1434  }
1435  }
1436  emitRedirections( oldDirUrl, newDirUrl );
1437  }
1438  if ( goNext )
1439  ++itu;
1440  }
1441 
1442  // Is oldUrl a directory in the cache?
1443  // Remove any child of oldUrl from the cache - even if the renamed dir itself isn't in it!
1444  removeDirFromCache( oldUrl );
1445  // TODO rename, instead.
1446 }
1447 
1448 void KDirListerCache::emitRedirections( const KURL &oldUrl, const KURL &url )
1449 {
1450  kdDebug(7004) << k_funcinfo << oldUrl.prettyURL() << " -> " << url.prettyURL() << endl;
1451  TQString oldUrlStr = oldUrl.url(-1);
1452  TQString urlStr = url.url(-1);
1453  TQString oldReferenceUrlStr = oldUrl.internalReferenceURL();
1454  TQString urlReferenceStr = url.internalReferenceURL();
1455 
1456  TDEIO::ListJob *job = jobForUrl( oldUrlStr + ":" + oldReferenceUrlStr );
1457  if ( job )
1458  killJob( job );
1459 
1460  // Check if we were listing this dir. Need to abort and restart with new name in that case.
1461  TQPtrList<KDirLister> *listers = urlsCurrentlyListed.take( oldUrlStr + ":" + oldReferenceUrlStr );
1462  if ( listers )
1463  {
1464  // Tell the world that the job listing the old url is dead.
1465  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1466  {
1467  if ( job )
1468  kdl->jobDone( job );
1469 
1470  emit kdl->canceled( oldUrl );
1471  }
1472 
1473  urlsCurrentlyListed.insert( urlStr + ":" + urlReferenceStr, listers );
1474  }
1475 
1476  // Check if we are currently displaying this directory (odds opposite wrt above)
1477  // Update urlsCurrentlyHeld dict with new URL
1478  TQPtrList<KDirLister> *holders = urlsCurrentlyHeld.take( oldUrlStr + ":" + oldReferenceUrlStr );
1479  if ( holders )
1480  {
1481  if ( job )
1482  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1483  kdl->jobDone( job );
1484 
1485  urlsCurrentlyHeld.insert( urlStr + ":" + urlReferenceStr, holders );
1486  }
1487 
1488  if ( listers )
1489  {
1490  updateDirectory( url );
1491 
1492  // Tell the world about the new url
1493  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() )
1494  emit kdl->started( url );
1495  }
1496 
1497  if ( holders )
1498  {
1499  // And notify the dirlisters of the redirection
1500  for ( KDirLister *kdl = holders->first(); kdl; kdl = holders->next() )
1501  {
1502  *kdl->d->lstDirs.find( oldUrl ) = url;
1503 
1504  if ( kdl->d->lstDirs.count() == 1 )
1505  emit kdl->redirection( url );
1506 
1507  emit kdl->redirection( oldUrl, url );
1508  }
1509  }
1510 }
1511 
1512 void KDirListerCache::removeDirFromCache( const KURL& dir )
1513 {
1514  kdDebug(7004) << "KDirListerCache::removeDirFromCache " << dir.prettyURL() << endl;
1515  TQCacheIterator<DirItem> itc( itemsCached );
1516  while ( itc.current() )
1517  {
1518  if ( dir.isParentOf( KURL( itc.currentKey() ) ) )
1519  itemsCached.remove( itc.currentKey() );
1520  else
1521  ++itc;
1522  }
1523 }
1524 
1525 void KDirListerCache::slotUpdateEntries( TDEIO::Job* job, const TDEIO::UDSEntryList& list )
1526 {
1527  jobs[static_cast<TDEIO::ListJob*>(job)] += list;
1528 }
1529 
1530 void KDirListerCache::slotUpdateResult( TDEIO::Job * j )
1531 {
1532  Q_ASSERT( j );
1533  TDEIO::ListJob *job = static_cast<TDEIO::ListJob *>( j );
1534 
1535  KURL jobUrl = joburl( job );
1536  jobUrl.adjustPath(-1); // need remove trailing slashes again, in case of redirections
1537  TQString jobUrlStr = jobUrl.url();
1538  TQString jobReferenceUrlStr = jobUrl.internalReferenceURL();
1539 
1540  kdDebug(7004) << k_funcinfo << "finished update " << jobUrl << endl;
1541 
1542  KDirLister *kdl;
1543 
1544  TQPtrList<KDirLister> *listers = urlsCurrentlyHeld[jobUrlStr + ":" + jobReferenceUrlStr];
1545  TQPtrList<KDirLister> *tmpLst = urlsCurrentlyListed.take( jobUrlStr + ":" + jobReferenceUrlStr );
1546 
1547  if ( tmpLst )
1548  {
1549  if ( listers )
1550  for ( kdl = tmpLst->first(); kdl; kdl = tmpLst->next() )
1551  {
1552  Q_ASSERT( listers->containsRef( kdl ) == 0 );
1553  listers->append( kdl );
1554  }
1555  else
1556  {
1557  listers = tmpLst;
1558  urlsCurrentlyHeld.insert( jobUrlStr + ":" + jobReferenceUrlStr, listers );
1559  }
1560  }
1561 
1562  // once we are updating dirs that are only in the cache this will fail!
1563  Q_ASSERT( listers );
1564 
1565  if ( job->error() )
1566  {
1567  for ( kdl = listers->first(); kdl; kdl = listers->next() )
1568  {
1569  kdl->jobDone( job );
1570 
1571  //don't bother the user
1572  //kdl->handleError( job );
1573 
1574  emit kdl->canceled( jobUrl );
1575  if ( kdl->numJobs() == 0 )
1576  {
1577  kdl->d->complete = true;
1578  emit kdl->canceled();
1579  }
1580  }
1581 
1582  jobs.remove( job );
1583 
1584  // TODO: if job is a parent of one or more
1585  // of the pending urls we should cancel them
1586  processPendingUpdates();
1587  return;
1588  }
1589 
1590  DirItem *dir = itemsInUse[jobUrlStr + ":" + jobReferenceUrlStr];
1591  dir->complete = true;
1592 
1593 
1594  // check if anyone wants the mimetypes immediately
1595  bool delayedMimeTypes = true;
1596  for ( kdl = listers->first(); kdl; kdl = listers->next() ) {
1597  delayedMimeTypes = delayedMimeTypes && kdl->d->delayedMimeTypes;
1598  }
1599 
1600  // should be enough to get reasonable speed in most cases
1601  TQDict<KFileItem> fileItems( 9973 );
1602 
1603  KFileItemListIterator kit ( *(dir->lstItems) );
1604 
1605  // Unmark all items in url
1606  for ( ; kit.current(); ++kit )
1607  {
1608  (*kit)->unmark();
1609  if (!((*kit)->listerURL().isEmpty())) {
1610  fileItems.insert( (*kit)->listerURL().url(), *kit );
1611  }
1612  else {
1613  fileItems.insert( (*kit)->url().url(), *kit );
1614  }
1615  }
1616 
1617  static const TQString& dot = TDEGlobal::staticQString(".");
1618  static const TQString& dotdot = TDEGlobal::staticQString("..");
1619 
1620  KFileItem *item = 0, *tmp;
1621 
1622  TQValueList<TDEIO::UDSEntry> buf = jobs[job];
1623  TQValueListIterator<TDEIO::UDSEntry> it = buf.begin();
1624  for ( ; it != buf.end(); ++it )
1625  {
1626  // Form the complete url
1627  if ( !item ) {
1628  item = new KFileItem( *it, jobUrl, delayedMimeTypes, true );
1629  }
1630  else {
1631  item->setUDSEntry( *it, jobUrl, delayedMimeTypes, true );
1632  }
1633 
1634  // Find out about the name
1635  TQString name = item->name();
1636  Q_ASSERT( !name.isEmpty() );
1637 
1638  // we duplicate the check for dotdot here, to avoid iterating over
1639  // all items again and checking in matchesFilter() that way.
1640  if ( name.isEmpty() || name == dotdot ) {
1641  continue;
1642  }
1643 
1644  if ( name == dot )
1645  {
1646  // if the update was started before finishing the original listing
1647  // there is no root item yet
1648  if ( !dir->rootItem )
1649  {
1650  dir->rootItem = item;
1651  item = 0;
1652 
1653  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1654  if ( !kdl->d->rootFileItem && kdl->d->url == jobUrl ) {
1655  kdl->d->rootFileItem = dir->rootItem;
1656  }
1657  }
1658  }
1659 
1660  continue;
1661  }
1662 
1663  // Find this item
1664  if ( (tmp = fileItems[item->url().url()]) )
1665  {
1666  tmp->mark();
1667 
1668  // check if something changed for this file
1669  if ( !tmp->cmp( *item ) )
1670  {
1671  for ( kdl = listers->first(); kdl; kdl = listers->next() ) {
1672  kdl->aboutToRefreshItem( tmp );
1673  }
1674 
1675  //kdDebug(7004) << "slotUpdateResult: file changed: " << tmp->name() << endl;
1676  tmp->assign( *item );
1677 
1678  for ( kdl = listers->first(); kdl; kdl = listers->next() ) {
1679  kdl->addRefreshItem( tmp );
1680  }
1681  }
1682  }
1683  else // this is a new file
1684  {
1685  //kdDebug(7004) << "slotUpdateResult: new file: " << name << endl;
1686 
1687  item->mark();
1688  dir->lstItems->append( item );
1689 
1690  for ( kdl = listers->first(); kdl; kdl = listers->next() ) {
1691  kdl->addNewItem( item );
1692  }
1693 
1694  // item used, we need a new one for the next iteration
1695  item = 0;
1696  }
1697  }
1698 
1699  if ( item ) {
1700  delete item;
1701  }
1702 
1703  jobs.remove( job );
1704 
1705  deleteUnmarkedItems( listers, dir->lstItems );
1706 
1707  for ( kdl = listers->first(); kdl; kdl = listers->next() )
1708  {
1709  kdl->emitItems();
1710 
1711  kdl->jobDone( job );
1712 
1713  kdl->emitCompleted( jobUrl );
1714  if ( kdl->numJobs() == 0 )
1715  {
1716  kdl->d->complete = true;
1717  emit kdl->completed();
1718  }
1719  }
1720 
1721  // TODO: hmm, if there was an error and job is a parent of one or more
1722  // of the pending urls we should cancel it/them as well
1723  processPendingUpdates();
1724 }
1725 
1726 // private
1727 
1728 TDEIO::ListJob *KDirListerCache::jobForUrl( const TQString& url, TDEIO::ListJob *not_job )
1729 {
1730  TDEIO::ListJob *job;
1731  TQMap< TDEIO::ListJob *, TQValueList<TDEIO::UDSEntry> >::Iterator it = jobs.begin();
1732  while ( it != jobs.end() )
1733  {
1734  job = it.key();
1735  KURL itjoburl = joburl( job );
1736  if ( ((itjoburl.url(-1) + ":" + itjoburl.internalReferenceURL()) == url) && (job != not_job) ) {
1737  return job;
1738  }
1739  ++it;
1740  }
1741  return 0;
1742 }
1743 
1744 const KURL& KDirListerCache::joburl( TDEIO::ListJob *job )
1745 {
1746  if ( job->redirectionURL().isValid() ) {
1747  return job->redirectionURL();
1748  }
1749  else {
1750  return job->url();
1751  }
1752 }
1753 
1754 void KDirListerCache::killJob( TDEIO::ListJob *job )
1755 {
1756  jobs.remove( job );
1757  job->disconnect( this );
1758  job->kill();
1759 }
1760 
1761 void KDirListerCache::deleteUnmarkedItems( TQPtrList<KDirLister> *listers, KFileItemList *lstItems )
1762 {
1763  // Find all unmarked items and delete them
1764  KFileItem* item;
1765  lstItems->first();
1766  while ( (item = lstItems->current()) )
1767  if ( !item->isMarked() )
1768  {
1769  //kdDebug() << k_funcinfo << item->name() << endl;
1770  for ( KDirLister *kdl = listers->first(); kdl; kdl = listers->next() ) {
1771  kdl->emitDeleteItem( item );
1772  }
1773 
1774  if ( item->isDir() ) {
1775  deleteDir( item->url() );
1776  }
1777 
1778  // finally actually delete the item
1779  lstItems->take();
1780  delete item;
1781  }
1782  else
1783  lstItems->next();
1784 }
1785 
1786 void KDirListerCache::deleteDir( const KURL& dirUrl )
1787 {
1788  //kdDebug() << k_funcinfo << dirUrl.prettyURL() << endl;
1789  // unregister and remove the childs of the deleted item.
1790  // Idea: tell all the KDirListers that they should forget the dir
1791  // and then remove it from the cache.
1792 
1793  TQDictIterator<DirItem> itu( itemsInUse );
1794  while ( itu.current() )
1795  {
1796  TQString deletedUrlIndep = itu.currentKey();
1797  deletedUrlIndep.truncate(deletedUrlIndep.length() - ((*itu)->url.internalReferenceURL().length()+strlen(":")));
1798  KURL deletedUrl( deletedUrlIndep );
1799  if ( dirUrl.isParentOf( deletedUrl ) )
1800  {
1801  // stop all jobs for deletedUrl
1802 
1803  TQPtrList<KDirLister> *kdls = urlsCurrentlyListed[deletedUrl.url() + ":" + deletedUrl.internalReferenceURL()];
1804  if ( kdls ) // yeah, I lack good names
1805  {
1806  // we need a copy because stop modifies the list
1807  kdls = new TQPtrList<KDirLister>( *kdls );
1808  for ( KDirLister *kdl = kdls->first(); kdl; kdl = kdls->next() )
1809  stop( kdl, deletedUrl );
1810 
1811  delete kdls;
1812  }
1813 
1814  // tell listers holding deletedUrl to forget about it
1815  // this will stop running updates for deletedUrl as well
1816 
1817  kdls = urlsCurrentlyHeld[deletedUrl.url() + ":" + deletedUrl.internalReferenceURL()];
1818  if ( kdls )
1819  {
1820  // we need a copy because forgetDirs modifies the list
1821  kdls = new TQPtrList<KDirLister>( *kdls );
1822 
1823  for ( KDirLister *kdl = kdls->first(); kdl; kdl = kdls->next() )
1824  {
1825  // lister's root is the deleted item
1826  if ( kdl->d->url == deletedUrl )
1827  {
1828  // tell the view first. It might need the subdirs' items (which forgetDirs will delete)
1829  if ( kdl->d->rootFileItem )
1830  emit kdl->deleteItem( kdl->d->rootFileItem );
1831  forgetDirs( kdl );
1832  kdl->d->rootFileItem = 0;
1833  }
1834  else
1835  {
1836  bool treeview = kdl->d->lstDirs.count() > 1;
1837  if ( !treeview )
1838  emit kdl->clear();
1839 
1840  forgetDirs( kdl, deletedUrl, treeview );
1841  }
1842  }
1843 
1844  delete kdls;
1845  }
1846 
1847  // delete the entry for deletedUrl - should not be needed, it's in
1848  // items cached now
1849 
1850  DirItem *dir = itemsInUse.take( deletedUrl.url() + ":" + deletedUrl.internalReferenceURL() );
1851  Q_ASSERT( !dir );
1852  if ( !dir ) // take didn't find it - move on
1853  ++itu;
1854  }
1855  else
1856  ++itu;
1857  }
1858 
1859  // remove the children from the cache
1860  removeDirFromCache( dirUrl );
1861 }
1862 
1863 void KDirListerCache::processPendingUpdates()
1864 {
1865  // TODO
1866 }
1867 
1868 #ifndef NDEBUG
1869 void KDirListerCache::printDebug()
1870 {
1871  kdDebug(7004) << "Items in use: " << endl;
1872  TQDictIterator<DirItem> itu( itemsInUse );
1873  for ( ; itu.current() ; ++itu ) {
1874  kdDebug(7004) << " " << itu.currentKey() << " URL: " << itu.current()->url
1875  << " rootItem: " << ( itu.current()->rootItem ? itu.current()->rootItem->url() : KURL() )
1876  << " autoUpdates refcount: " << itu.current()->autoUpdates
1877  << " complete: " << itu.current()->complete
1878  << ( itu.current()->lstItems ? TQString(" with %1 items.").arg(itu.current()->lstItems->count()) : TQString(" lstItems=NULL") ) << endl;
1879  }
1880 
1881  kdDebug(7004) << "urlsCurrentlyHeld: " << endl;
1882  TQDictIterator< TQPtrList<KDirLister> > it( urlsCurrentlyHeld );
1883  for ( ; it.current() ; ++it )
1884  {
1885  TQString list;
1886  for ( TQPtrListIterator<KDirLister> listit( *it.current() ); listit.current(); ++listit )
1887  list += " 0x" + TQString::number( (long)listit.current(), 16 );
1888  kdDebug(7004) << " " << it.currentKey() << " " << it.current()->count() << " listers: " << list << endl;
1889  }
1890 
1891  kdDebug(7004) << "urlsCurrentlyListed: " << endl;
1892  TQDictIterator< TQPtrList<KDirLister> > it2( urlsCurrentlyListed );
1893  for ( ; it2.current() ; ++it2 )
1894  {
1895  TQString list;
1896  for ( TQPtrListIterator<KDirLister> listit( *it2.current() ); listit.current(); ++listit )
1897  list += " 0x" + TQString::number( (long)listit.current(), 16 );
1898  kdDebug(7004) << " " << it2.currentKey() << " " << it2.current()->count() << " listers: " << list << endl;
1899  }
1900 
1901  TQMap< TDEIO::ListJob *, TQValueList<TDEIO::UDSEntry> >::Iterator jit = jobs.begin();
1902  kdDebug(7004) << "Jobs: " << endl;
1903  for ( ; jit != jobs.end() ; ++jit )
1904  kdDebug(7004) << " " << jit.key() << " listing " << joburl( jit.key() ).prettyURL() << ": " << (*jit).count() << " entries." << endl;
1905 
1906  kdDebug(7004) << "Items in cache: " << endl;
1907  TQCacheIterator<DirItem> itc( itemsCached );
1908  for ( ; itc.current() ; ++itc )
1909  kdDebug(7004) << " " << itc.currentKey() << " rootItem: "
1910  << ( itc.current()->rootItem ? itc.current()->rootItem->url().prettyURL() : TQString("NULL") )
1911  << ( itc.current()->lstItems ? TQString(" with %1 items.").arg(itc.current()->lstItems->count()) : TQString(" lstItems=NULL") ) << endl;
1912 }
1913 #endif
1914 
1915 /*********************** -- The new KDirLister -- ************************/
1916 
1917 
1918 KDirLister::KDirLister( bool _delayedMimeTypes )
1919 {
1920  kdDebug(7003) << "+KDirLister" << endl;
1921 
1922  d = new KDirListerPrivate;
1923 
1924  d->complete = true;
1925  d->delayedMimeTypes = _delayedMimeTypes;
1926 
1927  setAutoUpdate( true );
1928  setDirOnlyMode( false );
1929  setShowingDotFiles( false );
1930 
1931  setAutoErrorHandlingEnabled( true, 0 );
1932 }
1933 
1934 KDirLister::~KDirLister()
1935 {
1936  kdDebug(7003) << "-KDirLister" << endl;
1937 
1938  if ( KDirListerCache::exists() )
1939  {
1940  // Stop all running jobs
1941  stop();
1942  s_pCache->forgetDirs( this );
1943  }
1944 
1945  delete d;
1946 }
1947 
1948 bool KDirLister::openURL( const KURL& _url, bool _keep, bool _reload )
1949 {
1950  kdDebug(7003) << k_funcinfo << _url.prettyURL()
1951  << " keep=" << _keep << " reload=" << _reload << endl;
1952 
1953  // emit the current changes made to avoid an inconsistent treeview
1954  if ( d->changes != NONE && _keep ) {
1955  emitChanges();
1956  }
1957 
1958  d->changes = NONE;
1959 
1960  // If a local path is available, monitor that instead of the given remote URL...
1961  if (!_url.isLocalFile()) {
1962  TDEIO::LocalURLJob* localURLJob = TDEIO::localURL(_url);
1963  if (localURLJob) {
1964  d->openURL_url[localURLJob] = _url;
1965  d->openURL_keep[localURLJob] = _keep;
1966  d->openURL_reload[localURLJob] = _reload;
1967  connect(localURLJob, TQT_SIGNAL(localURL(TDEIO::Job*, const KURL&, bool)), this, TQT_SLOT(slotOpenURLGotLocalURL(TDEIO::Job*, const KURL&, bool)));
1968  connect(localURLJob, TQT_SIGNAL(destroyed()), this, TQT_SLOT(slotLocalURLKIODestroyed()));
1969  }
1970  return true;
1971  }
1972  else {
1973  return s_pCache->listDir( this, _url, _keep, _reload );
1974  }
1975 }
1976 
1977 void KDirLister::slotOpenURLGotLocalURL(TDEIO::Job *job, const KURL& url, bool isLocal) {
1978  KURL realURL = d->openURL_url[job];
1979  if (isLocal) {
1980  realURL = url;
1981  realURL.setInternalReferenceURL(d->openURL_url[job].url());
1982  d->m_referenceURLMap[d->openURL_url[job].url()] = url.path();
1983  }
1984  s_pCache->listDir( this, realURL, d->openURL_keep[job], d->openURL_reload[job] );
1985  d->openURL_url.remove(job);
1986  d->openURL_keep.remove(job);
1987  d->openURL_reload.remove(job);
1988 }
1989 
1990 void KDirLister::slotLocalURLKIODestroyed() {
1991  TDEIO::LocalURLJob* terminatedJob = const_cast<TDEIO::LocalURLJob*>(static_cast<const TDEIO::LocalURLJob*>(sender()));
1992 
1993  if (d->openURL_url.contains(terminatedJob)) {
1994  s_pCache->listDir( this, d->openURL_url[terminatedJob], d->openURL_keep[terminatedJob], d->openURL_reload[terminatedJob] );
1995  d->openURL_url.remove(terminatedJob);
1996  d->openURL_keep.remove(terminatedJob);
1997  d->openURL_reload.remove(terminatedJob);
1998  }
1999 }
2000 
2001 void KDirLister::stop()
2002 {
2003  kdDebug(7003) << k_funcinfo << endl;
2004  s_pCache->stop( this );
2005  d->m_referenceURLMap.clear();
2006 }
2007 
2008 void KDirLister::stop( const KURL& _url )
2009 {
2010  kdDebug(7003) << k_funcinfo << _url.prettyURL() << endl;
2011  s_pCache->stop( this, _url );
2012  d->m_referenceURLMap.remove(_url.url());
2013 }
2014 
2015 bool KDirLister::autoUpdate() const
2016 {
2017  return d->autoUpdate;
2018 }
2019 
2020 void KDirLister::setAutoUpdate( bool _enable )
2021 {
2022  if ( d->autoUpdate == _enable )
2023  return;
2024 
2025  d->autoUpdate = _enable;
2026  s_pCache->setAutoUpdate( this, _enable );
2027 }
2028 
2029 bool KDirLister::showingDotFiles() const
2030 {
2031  return d->isShowingDotFiles;
2032 }
2033 
2034 void KDirLister::setShowingDotFiles( bool _showDotFiles )
2035 {
2036  if ( d->isShowingDotFiles == _showDotFiles )
2037  return;
2038 
2039  d->isShowingDotFiles = _showDotFiles;
2040  d->changes ^= DOT_FILES;
2041 }
2042 
2043 bool KDirLister::dirOnlyMode() const
2044 {
2045  return d->dirOnlyMode;
2046 }
2047 
2048 void KDirLister::setDirOnlyMode( bool _dirsOnly )
2049 {
2050  if ( d->dirOnlyMode == _dirsOnly )
2051  return;
2052 
2053  d->dirOnlyMode = _dirsOnly;
2054  d->changes ^= DIR_ONLY_MODE;
2055 }
2056 
2057 bool KDirLister::autoErrorHandlingEnabled() const
2058 {
2059  return d->autoErrorHandling;
2060 }
2061 
2062 void KDirLister::setAutoErrorHandlingEnabled( bool enable, TQWidget* parent )
2063 {
2064  d->autoErrorHandling = enable;
2065  d->errorParent = parent;
2066 }
2067 
2068 const KURL& KDirLister::url() const
2069 {
2070  return d->url;
2071 }
2072 
2073 const KURL::List& KDirLister::directories() const
2074 {
2075  return d->lstDirs;
2076 }
2077 
2078 void KDirLister::emitChanges()
2079 {
2080  if ( d->changes == NONE )
2081  return;
2082 
2083  static const TQString& dot = TDEGlobal::staticQString(".");
2084  static const TQString& dotdot = TDEGlobal::staticQString("..");
2085 
2086  for ( KURL::List::Iterator it = d->lstDirs.begin();
2087  it != d->lstDirs.end(); ++it )
2088  {
2089  KFileItemListIterator kit( *s_pCache->itemsForDir( *it ) );
2090  for ( ; kit.current(); ++kit )
2091  {
2092  if ( (*kit)->text() == dot || (*kit)->text() == dotdot )
2093  continue;
2094 
2095  bool oldMime = true, newMime = true;
2096 
2097  if ( d->changes & MIME_FILTER )
2098  {
2099  oldMime = doMimeFilter( (*kit)->mimetype(), d->oldMimeFilter )
2100  && doMimeExcludeFilter( (*kit)->mimetype(), d->oldMimeExcludeFilter );
2101  newMime = doMimeFilter( (*kit)->mimetype(), d->mimeFilter )
2102  && doMimeExcludeFilter( (*kit)->mimetype(), d->mimeExcludeFilter );
2103 
2104  if ( oldMime && !newMime )
2105  {
2106  emit deleteItem( *kit );
2107  continue;
2108  }
2109  }
2110 
2111  if ( d->changes & DIR_ONLY_MODE )
2112  {
2113  // the lister switched to dirOnlyMode
2114  if ( d->dirOnlyMode )
2115  {
2116  if ( !(*kit)->isDir() )
2117  emit deleteItem( *kit );
2118  }
2119  else if ( !(*kit)->isDir() ) {
2120  addNewItem( *kit );
2121  }
2122 
2123  continue;
2124  }
2125 
2126  if ( (*kit)->isHidden() )
2127  {
2128  if ( d->changes & DOT_FILES )
2129  {
2130  // the lister switched to dot files mode
2131  if ( d->isShowingDotFiles ) {
2132  addNewItem( *kit );
2133  }
2134  else {
2135  emit deleteItem( *kit );
2136  }
2137 
2138  continue;
2139  }
2140  }
2141  else if ( d->changes & NAME_FILTER )
2142  {
2143  bool oldName = (*kit)->isDir() ||
2144  d->oldFilters.isEmpty() ||
2145  doNameFilter( (*kit)->text(), d->oldFilters );
2146 
2147  bool newName = (*kit)->isDir() ||
2148  d->lstFilters.isEmpty() ||
2149  doNameFilter( (*kit)->text(), d->lstFilters );
2150 
2151  if ( oldName && !newName )
2152  {
2153  emit deleteItem( *kit );
2154  continue;
2155  }
2156  else if ( !oldName && newName ) {
2157  addNewItem( *kit );
2158  }
2159  }
2160 
2161  if ( (d->changes & MIME_FILTER) && !oldMime && newMime ) {
2162  addNewItem( *kit );
2163  }
2164  }
2165 
2166  emitItems();
2167  }
2168 
2169  d->changes = NONE;
2170 }
2171 
2172 void KDirLister::updateDirectory( const KURL& _u )
2173 {
2174  s_pCache->updateDirectory( _u );
2175 }
2176 
2177 bool KDirLister::isFinished() const
2178 {
2179  return d->complete;
2180 }
2181 
2182 KFileItem *KDirLister::rootItem() const
2183 {
2184  return d->rootFileItem;
2185 }
2186 
2187 KFileItem *KDirLister::findByURL( const KURL& _url ) const
2188 {
2189  return s_pCache->findByURL( this, _url );
2190 }
2191 
2192 KFileItem *KDirLister::findByName( const TQString& _name ) const
2193 {
2194  return s_pCache->findByName( this, _name );
2195 }
2196 
2197 #ifndef KDE_NO_COMPAT
2198 KFileItem *KDirLister::find( const KURL& _url ) const
2199 {
2200  return findByURL( _url );
2201 }
2202 #endif
2203 
2204 
2205 // ================ public filter methods ================ //
2206 
2207 void KDirLister::setNameFilter( const TQString& nameFilter )
2208 {
2209  if ( !(d->changes & NAME_FILTER) )
2210  {
2211  d->oldFilters = d->lstFilters;
2212  d->lstFilters.setAutoDelete( false );
2213  }
2214 
2215  d->lstFilters.clear();
2216  d->lstFilters.setAutoDelete( true );
2217 
2218  d->nameFilter = nameFilter;
2219 
2220  // Split on white space
2221  TQStringList list = TQStringList::split( ' ', nameFilter );
2222  for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it )
2223  d->lstFilters.append( new TQRegExp(*it, false, true ) );
2224 
2225  d->changes |= NAME_FILTER;
2226 }
2227 
2228 const TQString& KDirLister::nameFilter() const
2229 {
2230  return d->nameFilter;
2231 }
2232 
2233 void KDirLister::setMimeFilter( const TQStringList& mimeFilter )
2234 {
2235  if ( !(d->changes & MIME_FILTER) )
2236  d->oldMimeFilter = d->mimeFilter;
2237 
2238  if ( mimeFilter.find("all/allfiles") != mimeFilter.end() ||
2239  mimeFilter.find("all/all") != mimeFilter.end() )
2240  d->mimeFilter.clear();
2241  else
2242  d->mimeFilter = mimeFilter;
2243 
2244  d->changes |= MIME_FILTER;
2245 }
2246 
2247 void KDirLister::setMimeExcludeFilter( const TQStringList& mimeExcludeFilter )
2248 {
2249  if ( !(d->changes & MIME_FILTER) )
2250  d->oldMimeExcludeFilter = d->mimeExcludeFilter;
2251 
2252  d->mimeExcludeFilter = mimeExcludeFilter;
2253  d->changes |= MIME_FILTER;
2254 }
2255 
2256 
2257 void KDirLister::clearMimeFilter()
2258 {
2259  if ( !(d->changes & MIME_FILTER) )
2260  {
2261  d->oldMimeFilter = d->mimeFilter;
2262  d->oldMimeExcludeFilter = d->mimeExcludeFilter;
2263  }
2264  d->mimeFilter.clear();
2265  d->mimeExcludeFilter.clear();
2266  d->changes |= MIME_FILTER;
2267 }
2268 
2269 const TQStringList& KDirLister::mimeFilters() const
2270 {
2271  return d->mimeFilter;
2272 }
2273 
2274 bool KDirLister::matchesFilter( const TQString& name ) const
2275 {
2276  return doNameFilter( name, d->lstFilters );
2277 }
2278 
2279 bool KDirLister::matchesMimeFilter( const TQString& mime ) const
2280 {
2281  return doMimeFilter( mime, d->mimeFilter ) && doMimeExcludeFilter(mime,d->mimeExcludeFilter);
2282 }
2283 
2284 // ================ protected methods ================ //
2285 
2286 bool KDirLister::matchesFilter( const KFileItem *item ) const
2287 {
2288  Q_ASSERT( item );
2289  static const TQString& dotdot = TDEGlobal::staticQString("..");
2290 
2291  if ( item->text() == dotdot )
2292  return false;
2293 
2294  if ( !d->isShowingDotFiles && item->isHidden() )
2295  return false;
2296 
2297  if ( item->isDir() || d->lstFilters.isEmpty() )
2298  return true;
2299 
2300  return matchesFilter( item->text() );
2301 }
2302 
2303 bool KDirLister::matchesMimeFilter( const KFileItem *item ) const
2304 {
2305  Q_ASSERT( item );
2306  // Don't lose time determining the mimetype if there is no filter
2307  if ( d->mimeFilter.isEmpty() && d->mimeExcludeFilter.isEmpty() )
2308  return true;
2309  return matchesMimeFilter( item->mimetype() );
2310 }
2311 
2312 bool KDirLister::doNameFilter( const TQString& name, const TQPtrList<TQRegExp>& filters ) const
2313 {
2314  for ( TQPtrListIterator<TQRegExp> it( filters ); it.current(); ++it )
2315  if ( it.current()->exactMatch( name ) )
2316  return true;
2317 
2318  return false;
2319 }
2320 
2321 bool KDirLister::doMimeFilter( const TQString& mime, const TQStringList& filters ) const
2322 {
2323  if ( filters.isEmpty() )
2324  return true;
2325 
2326  KMimeType::Ptr mimeptr = KMimeType::mimeType(mime);
2327  //kdDebug(7004) << "doMimeFilter: investigating: "<<mimeptr->name()<<endl;
2328  TQStringList::ConstIterator it = filters.begin();
2329  for ( ; it != filters.end(); ++it )
2330  if ( mimeptr->is(*it) )
2331  return true;
2332  //else kdDebug(7004) << "doMimeFilter: compared without result to "<<*it<<endl;
2333 
2334 
2335  return false;
2336 }
2337 
2338 bool KDirLister::doMimeExcludeFilter( const TQString& mime, const TQStringList& filters ) const
2339 {
2340  if ( filters.isEmpty() )
2341  return true;
2342 
2343  TQStringList::ConstIterator it = filters.begin();
2344  for ( ; it != filters.end(); ++it )
2345  if ( (*it) == mime )
2346  return false;
2347 
2348  return true;
2349 }
2350 
2351 
2352 bool KDirLister::validURL( const KURL& _url ) const
2353 {
2354  return s_pCache->validURL( this, _url );
2355 }
2356 
2357 void KDirLister::handleError( TDEIO::Job *job )
2358 {
2359  if ( d->autoErrorHandling ) {
2360  job->showErrorDialog( d->errorParent );
2361  }
2362 }
2363 
2364 
2365 // ================= private methods ================= //
2366 
2367 void KDirLister::addNewItem( const KFileItem *item )
2368 {
2369  if ( ( d->dirOnlyMode && !item->isDir() ) || !matchesFilter( item ) ) {
2370  return; // No reason to continue... bailing out here prevents a mimetype scan.
2371  }
2372 
2373  if ((item->url().internalReferenceURL() != "")
2374  && (d->m_referenceURLMap.contains(item->url().internalReferenceURL()))) {
2375  // Likely a media:/ tdeioslave URL or similar
2376  // Rewrite the URL to ensure that the user remains within the media:/ tree!
2377  TQString itemPath = item->url().path();
2378  if (itemPath.startsWith(d->m_referenceURLMap[item->url().internalReferenceURL()])) {
2379  itemPath = itemPath.remove(0, d->m_referenceURLMap[item->url().internalReferenceURL()].length());
2380  TQString newPath = item->url().internalReferenceURL();
2381  if (!newPath.endsWith("/")) newPath = newPath + "/";
2382  while (itemPath.startsWith("/")) itemPath = itemPath.remove(0,1);
2383  while (itemPath.endsWith("/")) itemPath.truncate(itemPath.length()-1);
2384  newPath = newPath + itemPath;
2385  const_cast<KFileItem*>(item)->setListerURL(item->url());
2386  const_cast<KFileItem*>(item)->setURL(newPath);
2387  }
2388  }
2389 
2390  if ( matchesMimeFilter( item ) )
2391  {
2392  if ( !d->lstNewItems ) {
2393  d->lstNewItems = new KFileItemList;
2394  }
2395 
2396  d->lstNewItems->append( item ); // items not filtered
2397  }
2398  else
2399  {
2400  if ( !d->lstMimeFilteredItems ) {
2401  d->lstMimeFilteredItems = new KFileItemList;
2402  }
2403 
2404  d->lstMimeFilteredItems->append( item ); // only filtered by mime
2405  }
2406 }
2407 
2408 void KDirLister::addNewItems( const KFileItemList& items )
2409 {
2410  // TODO: make this faster - test if we have a filter at all first
2411  // DF: was this profiled? The matchesFoo() functions should be fast, w/o filters...
2412  // Of course if there is no filter and we can do a range-insertion instead of a loop, that might be good.
2413  // But that's for Qt4, not possible with TQPtrList.
2414  for ( KFileItemListIterator kit( items ); kit.current(); ++kit ) {
2415  addNewItem( *kit );
2416  }
2417 }
2418 
2419 void KDirLister::aboutToRefreshItem( const KFileItem *item )
2420 {
2421  // The code here follows the logic in addNewItem
2422  if ( ( d->dirOnlyMode && !item->isDir() ) || !matchesFilter( item ) )
2423  d->refreshItemWasFiltered = true;
2424  else if ( !matchesMimeFilter( item ) )
2425  d->refreshItemWasFiltered = true;
2426  else
2427  d->refreshItemWasFiltered = false;
2428 }
2429 
2430 void KDirLister::addRefreshItem( const KFileItem *item )
2431 {
2432  bool isExcluded = (d->dirOnlyMode && !item->isDir()) || !matchesFilter( item );
2433 
2434  if ((item->url().internalReferenceURL() != "")
2435  && (d->m_referenceURLMap.contains(item->url().internalReferenceURL()))) {
2436  // Likely a media:/ tdeioslave URL or similar
2437  // Rewrite the URL to ensure that the user remains within the media:/ tree!
2438  TQString itemPath = item->url().path();
2439  if (itemPath.startsWith(d->m_referenceURLMap[item->url().internalReferenceURL()])) {
2440  itemPath = itemPath.remove(0, d->m_referenceURLMap[item->url().internalReferenceURL()].length());
2441  TQString newPath = item->url().internalReferenceURL();
2442  if (!newPath.endsWith("/")) newPath = newPath + "/";
2443  while (itemPath.startsWith("/")) itemPath = itemPath.remove(0,1);
2444  while (itemPath.endsWith("/")) itemPath.truncate(itemPath.length()-1);
2445  newPath = newPath + itemPath;
2446  const_cast<KFileItem*>(item)->setListerURL(item->url());
2447  const_cast<KFileItem*>(item)->setURL(newPath);
2448  }
2449  }
2450 
2451  if ( !isExcluded && matchesMimeFilter( item ) )
2452  {
2453  if ( d->refreshItemWasFiltered )
2454  {
2455  if ( !d->lstNewItems ) {
2456  d->lstNewItems = new KFileItemList;
2457  }
2458 
2459  d->lstNewItems->append( item );
2460  }
2461  else
2462  {
2463  if ( !d->lstRefreshItems ) {
2464  d->lstRefreshItems = new KFileItemList;
2465  }
2466 
2467  d->lstRefreshItems->append( item );
2468  }
2469  }
2470  else if ( !d->refreshItemWasFiltered )
2471  {
2472  if ( !d->lstRemoveItems ) {
2473  d->lstRemoveItems = new KFileItemList;
2474  }
2475 
2476  // notify the user that the mimetype of a file changed that doesn't match
2477  // a filter or does match an exclude filter
2478  d->lstRemoveItems->append( item );
2479  }
2480 }
2481 
2482 void KDirLister::emitItems()
2483 {
2484  KFileItemList *tmpNew = d->lstNewItems;
2485  d->lstNewItems = 0;
2486 
2487  KFileItemList *tmpMime = d->lstMimeFilteredItems;
2488  d->lstMimeFilteredItems = 0;
2489 
2490  KFileItemList *tmpRefresh = d->lstRefreshItems;
2491  d->lstRefreshItems = 0;
2492 
2493  KFileItemList *tmpRemove = d->lstRemoveItems;
2494  d->lstRemoveItems = 0;
2495 
2496  if ( tmpNew )
2497  {
2498  // For historical reasons, items with different protocols and/or prefixes must be emitted separately
2499  TQString protocol;
2500  TQString prefix;
2501  TQString prevProtocol;
2502  TQString prevPrefix;
2503  KFileItemList emitList;
2504  for ( KFileItemListIterator kit( *tmpNew ); kit.current(); ++kit )
2505  {
2506  KFileItem *item = *kit;
2507  protocol = item->url().protocol();
2508  prefix = TQStringList::split("/", item->url().path())[0];
2509  if ((protocol != prevProtocol) || (prefix != prevPrefix)) {
2510  if (emitList.count() > 0) {
2511  emit newItems( emitList );
2512  emitList.clear();
2513  }
2514  }
2515  emitList.append(item);
2516  prevProtocol = protocol;
2517  prevPrefix = prefix;
2518  }
2519 
2520  if (emitList.count() > 0) {
2521  emit newItems( emitList );
2522  }
2523  delete tmpNew;
2524  }
2525 
2526  if ( tmpMime )
2527  {
2528  emit itemsFilteredByMime( *tmpMime );
2529  delete tmpMime;
2530  }
2531 
2532  if ( tmpRefresh )
2533  {
2534  emit refreshItems( *tmpRefresh );
2535  delete tmpRefresh;
2536  }
2537 
2538  if ( tmpRemove )
2539  {
2540  for ( KFileItem *tmp = tmpRemove->first(); tmp; tmp = tmpRemove->next() ) {
2541  emit deleteItem( tmp );
2542  }
2543  delete tmpRemove;
2544  }
2545 }
2546 
2547 void KDirLister::emitDeleteItem( KFileItem *item )
2548 {
2549  if ( ( d->dirOnlyMode && !item->isDir() ) || !matchesFilter( item ) ) {
2550  return; // No reason to continue... bailing out here prevents a mimetype scan.
2551  }
2552 
2553  if ( matchesMimeFilter( item ) ) {
2554  emit deleteItem( item );
2555  }
2556 }
2557 
2558 
2559 // ================ private slots ================ //
2560 
2561 void KDirLister::slotInfoMessage( TDEIO::Job *, const TQString& message )
2562 {
2563  emit infoMessage( message );
2564 }
2565 
2566 void KDirLister::slotPercent( TDEIO::Job *job, unsigned long pcnt )
2567 {
2568  d->jobData[static_cast<TDEIO::ListJob *>(job)].percent = pcnt;
2569 
2570  int result = 0;
2571 
2572  TDEIO::filesize_t size = 0;
2573 
2574  TQMap< TDEIO::ListJob *, KDirListerPrivate::JobData >::Iterator dataIt = d->jobData.begin();
2575  while ( dataIt != d->jobData.end() )
2576  {
2577  result += (*dataIt).percent * (*dataIt).totalSize;
2578  size += (*dataIt).totalSize;
2579  ++dataIt;
2580  }
2581 
2582  if ( size != 0 )
2583  result /= size;
2584  else
2585  result = 100;
2586  emit percent( result );
2587 }
2588 
2589 void KDirLister::slotTotalSize( TDEIO::Job *job, TDEIO::filesize_t size )
2590 {
2591  d->jobData[static_cast<TDEIO::ListJob *>(job)].totalSize = size;
2592 
2593  TDEIO::filesize_t result = 0;
2594  TQMap< TDEIO::ListJob *, KDirListerPrivate::JobData >::Iterator dataIt = d->jobData.begin();
2595  while ( dataIt != d->jobData.end() )
2596  {
2597  result += (*dataIt).totalSize;
2598  ++dataIt;
2599  }
2600 
2601  emit totalSize( result );
2602 }
2603 
2604 void KDirLister::slotProcessedSize( TDEIO::Job *job, TDEIO::filesize_t size )
2605 {
2606  d->jobData[static_cast<TDEIO::ListJob *>(job)].processedSize = size;
2607 
2608  TDEIO::filesize_t result = 0;
2609  TQMap< TDEIO::ListJob *, KDirListerPrivate::JobData >::Iterator dataIt = d->jobData.begin();
2610  while ( dataIt != d->jobData.end() )
2611  {
2612  result += (*dataIt).processedSize;
2613  ++dataIt;
2614  }
2615 
2616  emit processedSize( result );
2617 }
2618 
2619 void KDirLister::slotSpeed( TDEIO::Job *job, unsigned long spd )
2620 {
2621  d->jobData[static_cast<TDEIO::ListJob *>(job)].speed = spd;
2622 
2623  int result = 0;
2624  TQMap< TDEIO::ListJob *, KDirListerPrivate::JobData >::Iterator dataIt = d->jobData.begin();
2625  while ( dataIt != d->jobData.end() )
2626  {
2627  result += (*dataIt).speed;
2628  ++dataIt;
2629  }
2630 
2631  emit speed( result );
2632 }
2633 
2634 uint KDirLister::numJobs()
2635 {
2636  return d->jobData.count();
2637 }
2638 
2639 void KDirLister::jobDone( TDEIO::ListJob *job )
2640 {
2641  d->jobData.remove( job );
2642 }
2643 
2644 void KDirLister::jobStarted( TDEIO::ListJob *job )
2645 {
2646  KDirListerPrivate::JobData jobData;
2647  jobData.speed = 0;
2648  jobData.percent = 0;
2649  jobData.processedSize = 0;
2650  jobData.totalSize = 0;
2651 
2652  d->jobData.insert( job, jobData );
2653  d->complete = false;
2654 }
2655 
2656 void KDirLister::connectJob( TDEIO::ListJob *job )
2657 {
2658  connect( job, TQT_SIGNAL(infoMessage( TDEIO::Job *, const TQString& )),
2659  this, TQT_SLOT(slotInfoMessage( TDEIO::Job *, const TQString& )) );
2660  connect( job, TQT_SIGNAL(percent( TDEIO::Job *, unsigned long )),
2661  this, TQT_SLOT(slotPercent( TDEIO::Job *, unsigned long )) );
2662  connect( job, TQT_SIGNAL(totalSize( TDEIO::Job *, TDEIO::filesize_t )),
2663  this, TQT_SLOT(slotTotalSize( TDEIO::Job *, TDEIO::filesize_t )) );
2664  connect( job, TQT_SIGNAL(processedSize( TDEIO::Job *, TDEIO::filesize_t )),
2665  this, TQT_SLOT(slotProcessedSize( TDEIO::Job *, TDEIO::filesize_t )) );
2666  connect( job, TQT_SIGNAL(speed( TDEIO::Job *, unsigned long )),
2667  this, TQT_SLOT(slotSpeed( TDEIO::Job *, unsigned long )) );
2668 }
2669 
2670 void KDirLister::emitCompleted( const KURL& _url )
2671 {
2672  KURL emitURL = _url;
2673 
2674  if ((_url.internalReferenceURL() != "")
2675  && (d->m_referenceURLMap.contains(_url.internalReferenceURL()))) {
2676  // Likely a media:/ tdeioslave URL or similar
2677  // Rewrite the URL to ensure that the user remains within the media:/ tree!
2678  TQString itemPath = _url.path();
2679  if (itemPath.startsWith(d->m_referenceURLMap[_url.internalReferenceURL()])) {
2680  itemPath = itemPath.remove(0, d->m_referenceURLMap[_url.internalReferenceURL()].length());
2681  TQString newPath = _url.internalReferenceURL();
2682  if (!newPath.endsWith("/")) newPath = newPath + "/";
2683  while (itemPath.startsWith("/")) itemPath = itemPath.remove(0,1);
2684  while (itemPath.endsWith("/")) itemPath.truncate(itemPath.length()-1);
2685  newPath = newPath + itemPath;
2686  emitURL = newPath;
2687  }
2688  }
2689 
2690  emit completed( emitURL );
2691 }
2692 
2693 void KDirLister::setMainWindow( TQWidget *window )
2694 {
2695  d->window = window;
2696 }
2697 
2698 TQWidget *KDirLister::mainWindow()
2699 {
2700  return d->window;
2701 }
2702 
2703 KFileItemList KDirLister::items( WhichItems which ) const
2704 {
2705  return itemsForDir( url(), which );
2706 }
2707 
2708 KFileItemList KDirLister::itemsForDir( const KURL& dir, WhichItems which ) const
2709 {
2710  KFileItemList result;
2711  KFileItemList *allItems = s_pCache->itemsForDir( dir );
2712  if ( !allItems ) {
2713  return result;
2714  }
2715 
2716  if ( which == AllItems ) {
2717  result = *allItems; // shallow copy
2718  }
2719  else // only items passing the filters
2720  {
2721  for ( KFileItemListIterator kit( *allItems ); kit.current(); ++kit )
2722  {
2723  KFileItem *item = *kit;
2724  bool isExcluded = (d->dirOnlyMode && !item->isDir()) || !matchesFilter( item );
2725  if ( !isExcluded && matchesMimeFilter( item ) ) {
2726  result.append( item );
2727  }
2728  }
2729  }
2730 
2731  return result;
2732 }
2733 
2734 // to keep BC changes
2735 
2736 void KDirLister::virtual_hook( int, void * )
2737 { /*BASE::virtual_hook( id, data );*/ }
2738 
2739 #include "kdirlister.moc"
2740 #include "kdirlister_p.moc"

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.