KDirStat – a graphical disk usage utility
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.

kcleanupcollection.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * File name: kcleanupcollection.cpp
  3. * Summary: Support classes for KDirStat
  4. * License: LGPL - See file COPYING.LIB for details.
  5. * Author: Stefan Hundhammer <sh@suse.de>
  6. *
  7. * Updated: 2004-11-23
  8. */
  9. #include <tdelocale.h>
  10. #include "kcleanup.h"
  11. #include "kstdcleanup.h"
  12. #include "kcleanupcollection.h"
  13. using namespace KDirStat;
  14. KCleanupCollection::KCleanupCollection( TDEActionCollection * actionCollection )
  15. : TQObject()
  16. , _actionCollection( actionCollection )
  17. {
  18. /**
  19. * All cleanups beloningt to this collection are stored in two separate TQt
  20. * collections, a TQPtrList and a TQDict. Make _one_ of them manage the cleanup
  21. * objects, i.e. have them clear the KCleanup objects upon deleting. The
  22. * TQPtrList is the master collection, the TQDict the slave.
  23. **/
  24. _cleanupList.setAutoDelete( true );
  25. _cleanupDict.setAutoDelete( false );
  26. _nextUserCleanupNo = 0;
  27. }
  28. KCleanupCollection::KCleanupCollection( const KCleanupCollection &src )
  29. : TQObject()
  30. {
  31. deepCopy( src );
  32. // Keep consistent with the KCleanup copy constructor: It explicitly uses a
  33. // zero TDEActionCollecton to make sure no duplicates of cleanups get into
  34. // the action collection.
  35. _actionCollection = 0;
  36. }
  37. KCleanupCollection::~KCleanupCollection()
  38. {
  39. // No need to delete the cleanups: _cleanupList takes care of that
  40. // (autoDelete!).
  41. }
  42. KCleanupCollection &
  43. KCleanupCollection::operator= ( const KCleanupCollection &src )
  44. {
  45. if ( size() != src.size() )
  46. {
  47. /**
  48. * If the sizes are different, we really need to make a deep copy -
  49. * i.e. discard all the existing cleanups in this collection and create
  50. * new ones with the KCleanup copy constructor.
  51. **/
  52. // kdDebug() << k_funcinfo << "Sizes different - deep copy" << endl;
  53. deepCopy( src );
  54. }
  55. else
  56. {
  57. /**
  58. * If the sizes are the same, we'd rather just use the KCleanup
  59. * assignment operator to individually assign each cleanup in the
  60. * source collection to the corresponding one in this collection.
  61. *
  62. * The background of this seemingly awkward solution are (again) the
  63. * limitations of the KCleanup copy constructor: It doesn't make a
  64. * truly identical copy of the entire KCleanup object. Rather, it
  65. * copies only the KCleanup members and leaves most of the TDEAction
  66. * members (the parent class) untouched.
  67. *
  68. * The behaviour implemented here comes handy in the most common
  69. * situation where this assignment operator is used:
  70. *
  71. * KCleanupCollection tmpCollection( origCollection );
  72. * ...
  73. * ... // let use change settings in settings dialog
  74. * ...
  75. * origCollection = tmpCollection;
  76. *
  77. * 'tmpCollection' here is an incomplete copy of 'origCollection' -
  78. * which represents what the user really can see in the menus, i.e. all
  79. * the TDEAction stuff in there really needs to work.
  80. *
  81. * During changing preferences in the 'settings' dialog, the user only
  82. * changes 'tmpCollection' - if he chooses to abandon his changes
  83. * (e.g., he clicks on the 'cancel' button), no harm is done -
  84. * 'tmpCollection' is simply not copied back to
  85. * 'origCollection'. Anyway, since 'tmpCollection' is merely a
  86. * container for the true KCleanup members, the TDEAction members don't
  87. * matter here: There is no representation of 'tmpCollection' in any
  88. * menu or tool bar.
  89. *
  90. * As soon as the user clicks on 'apply' or 'ok' in the 'settings'
  91. * dialog, however, 'tmpCollection' is copied back to 'origCollection'
  92. * - that is, its KCleanup members. Most of the TDEAction members (other
  93. * than 'text()' which is explicitly copied back) remain untouched,
  94. * thus maintaining consistency with the user interface is guaranteed.
  95. **/
  96. // kdDebug() << k_funcinfo << "Same sizes - individual assignment" << endl;
  97. KCleanupList srcList = src.cleanupList();
  98. KCleanupListIterator srcIt( srcList );
  99. KCleanupListIterator destIt( _cleanupList );
  100. while ( *srcIt && *destIt )
  101. {
  102. // kdDebug() << "Assigning " << *srcIt << endl;
  103. **destIt = **srcIt;
  104. ++srcIt;
  105. ++destIt;
  106. }
  107. }
  108. // Intentionally leaving '_actionCollection' untouched!
  109. return *this;
  110. }
  111. void
  112. KCleanupCollection::deepCopy( const KCleanupCollection &src )
  113. {
  114. // Copy simple values
  115. _nextUserCleanupNo = src.nextUserCleanupNo();
  116. // Just to make sure - clear the internal collections
  117. _cleanupList.clear();
  118. _cleanupDict.clear();
  119. // Make a deep copy of all the cleanups in the source collection
  120. KCleanupList srcList = src.cleanupList();
  121. KCleanupListIterator it( srcList );
  122. while ( *it )
  123. {
  124. // kdDebug() << k_funcinfo << "Creating new " << *it << endl;
  125. add( new KCleanup( **it ) );
  126. ++it;
  127. }
  128. }
  129. void
  130. KCleanupCollection::add( KCleanup *newCleanup )
  131. {
  132. TQ_CHECK_PTR( newCleanup );
  133. if ( _cleanupDict[ newCleanup->id() ] ) // Already there?
  134. {
  135. // Delete any old instance in the list.
  136. //
  137. // The instance in the dict will be deleted automatically by inserting
  138. // the new one.
  139. _cleanupList.first(); // Moves _cleanupList.current() to beginning
  140. while ( _cleanupList.current() )
  141. {
  142. if ( _cleanupList.current()->id() == newCleanup->id() )
  143. {
  144. // Found a cleanup with the same ID -
  145. // remove the current list item, delete it (autoDelete!) and
  146. // move _cleanupList.current() to the next item.
  147. _cleanupList.remove();
  148. }
  149. else
  150. _cleanupList.next();
  151. }
  152. }
  153. _cleanupList.append( newCleanup );
  154. _cleanupDict.insert( newCleanup->id(), newCleanup );
  155. connect( this, TQT_SIGNAL( selectionChanged( KFileInfo * ) ),
  156. newCleanup, TQT_SLOT ( selectionChanged( KFileInfo * ) ) );
  157. connect( this, TQT_SIGNAL( readConfig() ),
  158. newCleanup, TQT_SLOT ( readConfig() ) );
  159. connect( this, TQT_SIGNAL( saveConfig() ),
  160. newCleanup, TQT_SLOT ( saveConfig() ) );
  161. connect( newCleanup, TQT_SIGNAL( executed() ),
  162. this, TQT_SLOT ( cleanupExecuted() ) );
  163. }
  164. void
  165. KCleanupCollection::addStdCleanups()
  166. {
  167. add( KStdCleanup::openInKonqueror ( _actionCollection ) );
  168. add( KStdCleanup::openInTerminal ( _actionCollection ) );
  169. add( KStdCleanup::compressSubtree ( _actionCollection ) );
  170. add( KStdCleanup::makeClean ( _actionCollection ) );
  171. add( KStdCleanup::deleteTrash ( _actionCollection ) );
  172. add( KStdCleanup::moveToTrashBin ( _actionCollection ) );
  173. add( KStdCleanup::hardDelete ( _actionCollection ) );
  174. }
  175. void
  176. KCleanupCollection::addUserCleanups( int number )
  177. {
  178. for ( int i=0; i < number; i++ )
  179. {
  180. TQString id;
  181. id.sprintf( "cleanup_user_defined_%d", _nextUserCleanupNo );
  182. TQString title;
  183. if ( _nextUserCleanupNo <= 9 )
  184. // Provide a keyboard shortcut for cleanup #0..#9
  185. title=i18n( "User Defined Cleanup #&%1" ).arg(_nextUserCleanupNo);
  186. else
  187. // No keyboard shortcuts for cleanups #10.. - they would be duplicates
  188. title=i18n( "User Defined Cleanup #%1" ).arg(_nextUserCleanupNo);
  189. _nextUserCleanupNo++;
  190. KCleanup *cleanup = new KCleanup( id, "", title, _actionCollection );
  191. TQ_CHECK_PTR( cleanup );
  192. cleanup->setEnabled( false );
  193. if ( i <= 9 )
  194. {
  195. // Provide an application-wide keyboard accelerator for cleanup #0..#9
  196. cleanup->setShortcut( TQt::CTRL + TQt::Key_0 + i );
  197. }
  198. add( cleanup );
  199. }
  200. }
  201. KCleanup *
  202. KCleanupCollection::cleanup( const TQString & id )
  203. {
  204. return _cleanupDict[ id ];
  205. }
  206. void
  207. KCleanupCollection::clear()
  208. {
  209. _cleanupList.clear();
  210. _cleanupDict.clear();
  211. _nextUserCleanupNo = 0;
  212. }
  213. void
  214. KCleanupCollection::slotReadConfig()
  215. {
  216. emit readConfig();
  217. }
  218. void
  219. KCleanupCollection::cleanupExecuted()
  220. {
  221. emit userActivity( 10 );
  222. }
  223. #include "kcleanupcollection.moc"
  224. // EOF