KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threadedlister.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***************************************************************************
  2. threadedlister.cpp - description
  3. -------------------
  4. begin : Tue Feb 01 2005
  5. copyright : (C) 2005 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "threadedlister.h"
  17. #include "kmylistbox.h"
  18. #include "krecursivelister.h"
  19. #include <tdeapplication.h>
  20. #include <tqmutex.h>
  21. ThreadedLister::ThreadedLister( TQMutex* mutex, unsigned int* counter, KMyListBox* list )
  22. : TQObject(), TQThread(), m_mutex( mutex ), m_counter( counter ), m_list( list )
  23. {
  24. m_mutex->lock();
  25. ++(*m_counter);
  26. m_mutex->unlock();
  27. m_reclister = NULL;
  28. m_lister = NULL;
  29. m_internal = NULL;
  30. m_hidden = false;
  31. m_recursive = false;
  32. m_dirnames = false;
  33. m_dironly = false;
  34. }
  35. ThreadedLister::~ThreadedLister()
  36. {
  37. if( m_reclister )
  38. delete m_reclister;
  39. if( m_lister )
  40. delete m_lister;
  41. if( m_internal )
  42. delete m_internal;
  43. }
  44. void ThreadedLister::run()
  45. {
  46. m_internal = new TQMutex();
  47. m_internal->lock();
  48. if( m_recursive )
  49. {
  50. m_reclister = new KRecursiveLister();
  51. m_reclister->setShowingDotFiles( m_hidden );
  52. m_reclister->setNameFilter( m_filter );
  53. m_reclister->setDirOnlyMode( m_dironly );
  54. connect( m_reclister, TQT_SIGNAL( completed() ), this, TQT_SLOT( reclisterFinished() ) );
  55. m_reclister->openURL( m_dirname );
  56. } else {
  57. m_lister = new KDirLister();
  58. m_lister->setAutoUpdate( false );
  59. m_lister->setShowingDotFiles( m_hidden );
  60. m_lister->setNameFilter( m_filter );
  61. connect( m_lister, TQT_SIGNAL( completed() ), this, TQT_SLOT( listerFinished() ) );
  62. m_lister->openURL( m_dirname, false, false );
  63. }
  64. // try to lock the mutex.
  65. // This will block run as long as *listerFinished()
  66. // does not unlock the mutex.
  67. m_internal->lock();
  68. m_internal->unlock();
  69. TDEApplication::postEvent( m_list, new TQCustomEvent( (TQEvent::Type)ThreadedLister::TYPE(), (void*)this ) );
  70. }
  71. void ThreadedLister::reclisterFinished()
  72. {
  73. KFileItemList l = m_reclister->items();
  74. FileList list = l;
  75. if( m_dirnames )
  76. {
  77. FileList dirs = m_reclister->dirs();
  78. KFileItem* item;
  79. for( item = dirs.first(); item; item = dirs.next() )
  80. list.append( item );
  81. }
  82. list.sort();
  83. m_mutex->lock();
  84. m_list->setUpdatesEnabled( false );
  85. if( m_dirnames )
  86. {
  87. TQString name = m_dirname.fileName();
  88. if( !m_hidden && name.right( 1 ) != TQString::fromLatin1(".") )
  89. m_list->addDirName( m_dirname );
  90. }
  91. KFileItem* item;
  92. for( item = list.first(); item; item = list.next() )
  93. if( item->isFile() )
  94. m_list->addFile( item->url(), true );
  95. else if( item->isDir() )
  96. m_list->addDirName( item->url() );
  97. m_mutex->unlock();
  98. m_list->setUpdatesEnabled( true );
  99. m_internal->unlock();
  100. }
  101. void ThreadedLister::listerFinished()
  102. {
  103. if( m_lister->isFinished() ) {
  104. FileList list = m_lister->items( KDirLister::FilteredItems );
  105. list.sort();
  106. m_mutex->lock();
  107. m_list->setUpdatesEnabled( false );
  108. if( m_dirnames )
  109. {
  110. TQString name = m_dirname.fileName();
  111. if( !m_hidden && name.right( 1 ) != TQString::fromLatin1(".") )
  112. m_list->addDirName( m_dirname );
  113. }
  114. KFileItem* item;
  115. for( item = list.first(); item; item = list.next() )
  116. if( item->isFile() )
  117. m_list->addFile( item->url(), true );
  118. --(*m_counter);
  119. m_mutex->unlock();
  120. m_list->setUpdatesEnabled( true );
  121. m_internal->unlock();
  122. }
  123. }
  124. #include "threadedlister.moc"