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.

kcleanup.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * File name: kcleanup.h
  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: 2003-01-07
  8. */
  9. #ifndef KCleanup_h
  10. #define KCleanup_h
  11. #ifdef HAVE_CONFIG_H
  12. # include <config.h>
  13. #endif
  14. #include <tqdict.h>
  15. #include <tqptrlist.h>
  16. #include <tqintdict.h>
  17. #include <tdeaction.h>
  18. #include <kdebug.h>
  19. #include "kdirtree.h"
  20. namespace KDirStat
  21. {
  22. /**
  23. * Cleanup action to be performed for @ref KDirTree items.
  24. *
  25. * @short KDirStat cleanup action
  26. **/
  27. class KCleanup: public TDEAction
  28. {
  29. Q_OBJECT
  30. public:
  31. enum RefreshPolicy { noRefresh, refreshThis, refreshParent, assumeDeleted };
  32. /**
  33. * Constructor.
  34. *
  35. * 'id' is the name of this cleanup action as used in the XML UI file
  36. * and config files, 'title' is the human readable menu title.
  37. * 'command' is the shell command to execute.
  38. *
  39. * Most applications will want to pass TDEMainWindow::actionCollection()
  40. * for 'parent' so the menus and toolbars can be created using the XML
  41. * UI description ('kdirstatui.rc' for KDirStat).
  42. **/
  43. KCleanup( TQString id = "",
  44. TQString command = "",
  45. TQString title = "",
  46. TDEActionCollection * parent = 0 );
  47. /**
  48. * Copy Constructor.
  49. *
  50. * Notice that this is a not quite complete copy constructor: Since
  51. * there is no TDEAction copy constructor, the inherited TDEAction members
  52. * will be constructed with the TDEAction default constructor. Thus, an
  53. * object created with this copy constructor can rely only on its
  54. * KCleanup members. This is intended for save/restore operations only,
  55. * not for general use. In particular, DO NOT connect an object thus
  56. * constructed with signals. The results will be undefined (at best).
  57. **/
  58. KCleanup( const KCleanup &src );
  59. /**
  60. * Assignment operator.
  61. *
  62. * This will not modify the TDEAction members, just the KCleanup
  63. * members. Just like the copy constructor, this is intended for
  64. * save/restore operations, not for general use.
  65. **/
  66. KCleanup & operator= ( const KCleanup &src );
  67. /**
  68. * Return the ID (name) of this cleanup action as used for setup files
  69. * and the XML UI description. This ID should be unique within the
  70. * application.
  71. **/
  72. const TQString & id() const { return _id; }
  73. /**
  74. * Return the command line that will be executed upon calling @ref
  75. * KCleanup::execute(). This command line may contain %p for the
  76. * complete path of the directory or file concerned or %n for the pure
  77. * file or directory name without path.
  78. **/
  79. const TQString & command() const { return _command; }
  80. /**
  81. * Return the user title of this command as displayed in menus.
  82. * This may include '&' characters for keyboard shortcuts.
  83. * See also @ref cleanTitle() .
  84. **/
  85. const TQString & title() const { return _title; }
  86. /**
  87. * Returns the cleanup action's title without '&' keyboard shortcuts.
  88. * Uses the ID as fallback if the name is empty.
  89. **/
  90. TQString cleanTitle() const;
  91. /**
  92. * Return whether or not this cleanup action is generally enabled.
  93. **/
  94. bool enabled() const { return _enabled; }
  95. /**
  96. * Return this cleanup's internally stored @ref KDirTree
  97. * selection. Important only for copy constructor etc.
  98. **/
  99. KFileInfo * selection() const { return _selection; }
  100. /**
  101. * Return whether or not this cleanup action works for this particular
  102. * KFileInfo. Checks all the other conditions (enabled(),
  103. * worksForDir(), worksForFile(), ...) accordingly.
  104. **/
  105. bool worksFor( KFileInfo *item ) const;
  106. /**
  107. * Return whether or not this cleanup action works for directories,
  108. * i.e. whether or not @ref KCleanup::execute() will be successful if
  109. * the object passed is a directory.
  110. **/
  111. bool worksForDir() const { return _worksForDir; }
  112. /**
  113. * Return whether or not this cleanup action works for plain files.
  114. **/
  115. bool worksForFile() const { return _worksForFile; }
  116. /**
  117. * Return whether or not this cleanup action works for KDirStat's
  118. * special 'Dot Entry' items, i.e. the pseudo nodes created in most
  119. * directories that hold the plain files.
  120. **/
  121. bool worksForDotEntry() const { return _worksForDotEntry; }
  122. /**
  123. * Return whether or not this cleanup action works for simple local
  124. * files and directories only ('file:/' protocol) or network
  125. * transparent, i.e. all protocols KDE supports ('ftp', 'smb' - but
  126. * even 'tar', even though it is - strictly spoken - local).
  127. **/
  128. bool worksLocalOnly() const { return _worksLocalOnly; }
  129. /**
  130. * Return whether or not the cleanup action should be performed
  131. * recursively in subdirectories of the initial KFileInfo.
  132. **/
  133. bool recurse() const { return _recurse; }
  134. /**
  135. * Return whether or not this cleanup should ask the user for
  136. * confirmation when it is executed.
  137. *
  138. * The default is 'false'. Use with caution - not only can this become
  139. * very annoying, people also tend to automatically click on 'OK' when
  140. * too many confirmation dialogs pop up!
  141. **/
  142. bool askForConfirmation() const { return _askForConfirmation; }
  143. /**
  144. * Return the refresh policy of this cleanup action - i.e. the action
  145. * to perform after each call to KCleanup::execute(). This is supposed
  146. * to bring the corresponding KDirTree back into sync after the cleanup
  147. * action - the underlying file tree might have changed due to that
  148. * cleanup action.
  149. *
  150. * noRefresh: Don't refresh anything. Assume nothing has changed.
  151. * This is the default.
  152. *
  153. * refreshThis: Refresh the KDirTree from the item on that was passed
  154. * to KCleanup::execute().
  155. *
  156. * refreshParent: Refresh the KDirTree from the parent of the item on
  157. * that was passed to KCleanup::execute(). If there is no such parent,
  158. * refresh the entire tree.
  159. *
  160. * assumeDeleted: Do not actually refresh the KDirTree. Instead,
  161. * blindly assume the cleanup action has deleted the item that was
  162. * passed to KCleanup::execute() and delete the corresponding subtree
  163. * in the KDirTree accordingly. This will work well for most deleting
  164. * actions as long as they can be performed without problems. If there
  165. * are any problems, however, the KDirTree might easily run out of sync
  166. * with the directory tree: The KDirTree will show the subtree as
  167. * deleted (i.e. it will not show it any more), but it still exists on
  168. * disk. This is the tradeoff to a very quick response. On the other
  169. * hand, the user can easily at any time hit one of the explicit
  170. * refresh buttons and everything will be back into sync again.
  171. **/
  172. enum RefreshPolicy refreshPolicy() const { return _refreshPolicy; }
  173. void setTitle ( const TQString &title );
  174. void setId ( const TQString &id ) { _id = id; }
  175. void setCommand ( const TQString &command) { _command = command; }
  176. void setEnabled ( bool enabled ) { _enabled = enabled; }
  177. void setWorksForDir ( bool canDo ) { _worksForDir = canDo; }
  178. void setWorksForFile ( bool canDo ) { _worksForFile = canDo; }
  179. void setWorksForDotEntry ( bool canDo ) { _worksForDotEntry = canDo; }
  180. void setWorksLocalOnly ( bool canDo ) { _worksLocalOnly = canDo; }
  181. void setRecurse ( bool recurse ) { _recurse = recurse; }
  182. void setAskForConfirmation ( bool ask ) { _askForConfirmation = ask; }
  183. void setRefreshPolicy ( enum RefreshPolicy refreshPolicy ) { _refreshPolicy = refreshPolicy; }
  184. public slots:
  185. /**
  186. * The heart of the matter: Perform the cleanup with the KFileInfo
  187. * specified.
  188. **/
  189. void execute( KFileInfo *item );
  190. /**
  191. * Perform the cleanup with the current KDirTree selection if there is
  192. * any.
  193. **/
  194. void executeWithSelection();
  195. /**
  196. * Set enabled/disabled status according to 'selection' and internally
  197. * store 'selection' - this will also be used upon calling
  198. * @ref executeWithSelection() . '0' means "nothing selected".
  199. **/
  200. void selectionChanged( KFileInfo *selection );
  201. /**
  202. * Read configuration.
  203. **/
  204. void readConfig();
  205. /**
  206. * Save configuration.
  207. **/
  208. void saveConfig() const;
  209. signals:
  210. /**
  211. * Emitted after the action is executed.
  212. *
  213. * Please note that there intentionally is no reference as to which
  214. * object the action was executed upon since this object very likely
  215. * doesn't exist any more.
  216. **/
  217. void executed();
  218. protected slots:
  219. /**
  220. * Inherited from @ref TDEAction : Perform the action.
  221. * In this case, execute the cleanup with the current selection.
  222. **/
  223. virtual void slotActivated() { executeWithSelection(); }
  224. protected:
  225. /**
  226. * Recursively perform the cleanup.
  227. **/
  228. void executeRecursive( KFileInfo *item );
  229. /**
  230. * Ask user for confirmation to execute this cleanup action for
  231. * 'item'. Returns 'true' if user accepts, 'false' otherwise.
  232. **/
  233. bool confirmation( KFileInfo *item );
  234. /**
  235. * Retrieve the directory part of a KFileInfo's path.
  236. **/
  237. const TQString itemDir( const KFileInfo *item ) const;
  238. /**
  239. * Expand some variables in string 'unexpanded' to information from
  240. * within 'item'. Multiple expansion is performed as needed, i.e. the
  241. * string may contain more than one variable to expand. The resulting
  242. * string is returned.
  243. *
  244. * %p expands to item->path(), i.e. the item's full path name.
  245. *
  246. * /usr/local/bin for that directory
  247. * /usr/local/bin/doit for a file within it
  248. *
  249. * %n expands to item->name(), i.e. the last component of the pathname.
  250. * The examples above would expand to:
  251. *
  252. * bin
  253. * doit
  254. *
  255. * For commands that are to be executed from within the 'Clean up'
  256. * menu, you might specify something like:
  257. *
  258. * "kfmclient openURL %p"
  259. * "tar czvf %{name}.tgz && rm -rf %{name}"
  260. **/
  261. TQString expandVariables ( const KFileInfo * item,
  262. const TQString & unexpanded ) const;
  263. /**
  264. * Run a command with 'item' as base to expand variables.
  265. **/
  266. void runCommand ( const KFileInfo * item,
  267. const TQString & command ) const;
  268. /**
  269. * Internal implementation of the copy constructor and assignment
  270. * operator: Copy all data members from 'src'.
  271. **/
  272. void copy ( const KCleanup &src );
  273. //
  274. // Data members
  275. //
  276. KFileInfo * _selection;
  277. TQString _id;
  278. TQString _command;
  279. TQString _title;
  280. bool _enabled;
  281. bool _worksForDir;
  282. bool _worksForFile;
  283. bool _worksForDotEntry;
  284. bool _worksLocalOnly;
  285. bool _recurse;
  286. bool _askForConfirmation;
  287. enum RefreshPolicy _refreshPolicy;
  288. };
  289. inline kdbgstream & operator<< ( kdbgstream & stream, const KCleanup * cleanup )
  290. {
  291. if ( cleanup )
  292. stream << cleanup->id();
  293. else
  294. stream << "<NULL>";
  295. return stream;
  296. }
  297. } // namespace KDirStat
  298. #endif // ifndef KCleanup_h
  299. // EOF