forked from shundhammer/qdirstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanupCollection.cpp
597 lines (457 loc) · 15.3 KB
/
CleanupCollection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* File name: CleanupCollection.cpp
* Summary: QDirStat classes to reclaim disk space
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <[email protected]>
*/
#include <QMenu>
#include <QToolBar>
#include <QMessageBox>
#include "CleanupCollection.h"
#include "Cleanup.h"
#include "StdCleanup.h"
#include "Settings.h"
#include "SettingsHelpers.h"
#include "SelectionModel.h"
#include "OutputWindow.h"
#include "Refresher.h"
#include "Logger.h"
#include "Exception.h"
#define MAX_URLS_IN_CONFIRMATION_POPUP 7
using namespace QDirStat;
CleanupCollection::CleanupCollection( SelectionModel * selectionModel,
QObject * parent ):
QObject( parent ),
_selectionModel( selectionModel ),
_listMover( _cleanupList )
{
readSettings();
// Just initialize to show the current status in the log;
// the contents are cached anyway.
(void) Cleanup::desktopSpecificApps();
if ( _cleanupList.isEmpty() )
addStdCleanups();
updateActions();
connect( selectionModel, SIGNAL( selectionChanged() ),
this, SLOT ( updateActions() ) );
}
CleanupCollection::~CleanupCollection()
{
writeSettings();
clear();
}
void CleanupCollection::add( Cleanup * cleanup )
{
CHECK_PTR( cleanup );
_cleanupList << cleanup;
connect( cleanup, SIGNAL( triggered() ),
this, SLOT ( execute() ) );
updateMenusAndToolBars();
}
void CleanupCollection::remove( Cleanup * cleanup )
{
int index = indexOf( cleanup );
if ( index == -1 )
{
logError() << "No such cleanup: " << cleanup << endl;
return;
}
_cleanupList.removeAt( index );
delete cleanup;
// No need for updateMenusAndToolBars() since QObject/QWidget will take care of
// deleted actions all by itself.
}
void CleanupCollection::addStdCleanups()
{
foreach ( Cleanup * cleanup, StdCleanup::stdCleanups( this ) )
{
add( cleanup );
}
}
int CleanupCollection::indexOf( Cleanup * cleanup ) const
{
int index = _cleanupList.indexOf( cleanup );
if ( index == -1 )
logError() << "Cleanup " << cleanup << " is not in this collection" << endl;
return index;
}
Cleanup * CleanupCollection::at( int index ) const
{
if ( index >= 0 && index < _cleanupList.size() )
return _cleanupList.at( index );
else
return 0;
}
void CleanupCollection::clear()
{
qDeleteAll( _cleanupList );
_cleanupList.clear();
// No need for updateMenusAndToolBars() since QObject/QWidget will take
// care of deleted actions all by itself.
}
void CleanupCollection::updateMenusAndToolBars()
{
updateMenus();
updateToolBars();
}
void CleanupCollection::updateActions()
{
FileInfoSet sel = _selectionModel->selectedItems();
bool dirSelected = sel.containsDir();
bool fileSelected = sel.containsFile();
bool dotEntrySelected = sel.containsDotEntry();
bool busy = sel.containsBusyItem();
bool treeBusy = sel.treeIsBusy();
foreach ( Cleanup * cleanup, _cleanupList )
{
if ( ! cleanup->active() || sel.isEmpty() )
cleanup->setEnabled( false );
else
{
bool enabled = ! busy;
if ( treeBusy && cleanup->refreshPolicy() != Cleanup::NoRefresh )
enabled = false;
if ( dirSelected && ! cleanup->worksForDir() )
enabled = false;
if ( dotEntrySelected && ! cleanup->worksForDotEntry() )
enabled = false;
if ( fileSelected && ! cleanup->worksForFile() )
enabled = false;
cleanup->setEnabled( enabled );
}
}
}
void CleanupCollection::updateMenus()
{
_menus.removeAll( 0 ); // Remove QPointers that have become invalid
foreach ( QMenu * menu, _menus )
{
if ( menu )
{
// Remove all Cleanups from this menu
foreach ( QAction * action, menu->actions() )
{
Cleanup * cleanup = dynamic_cast<Cleanup *>( action );
if ( cleanup )
menu->removeAction( cleanup );
}
// Add the current cleanups in the current order
addToMenu( menu );
}
}
}
void CleanupCollection::updateToolBars()
{
_toolBars.removeAll( 0 ); // Remove QPointers that have become invalid
foreach ( QToolBar * toolBar, _toolBars )
{
if ( toolBar )
{
// Remove all Cleanups from this tool bar
foreach ( QAction * action, toolBar->actions() )
{
Cleanup * cleanup = dynamic_cast<Cleanup *>( action );
if ( cleanup )
toolBar->removeAction( cleanup );
}
// Add the current cleanups in the current order
addToToolBar( toolBar );
}
}
}
void CleanupCollection::execute()
{
Cleanup * cleanup = qobject_cast<Cleanup *>( sender() );
if ( ! cleanup )
{
logError() << "Wrong sender type: "
<< sender()->metaObject()->className() << endl;
return;
}
FileInfoSet selection = _selectionModel->selectedItems();
if ( selection.isEmpty() )
{
logWarning() << "Nothing selected" << endl;
return;
}
if ( cleanup->askForConfirmation() && ! confirmation( cleanup, selection ) )
{
logDebug() << "User declined confirmation" << endl;
return;
}
emit startingCleanup( cleanup->cleanTitle() );
OutputWindow * outputWindow = new OutputWindow( qApp->activeWindow() );
CHECK_NEW( outputWindow );
outputWindow->setAutoClose( cleanup->outputWindowAutoClose() );
switch ( cleanup->outputWindowPolicy() )
{
case Cleanup::ShowAlways:
outputWindow->show();
break;
case Cleanup::ShowAfterTimeout:
outputWindow->showAfterTimeout( cleanup->outputWindowTimeout() );
break;
case Cleanup::ShowIfErrorOutput: // showOnStderr is default
break;
case Cleanup::ShowNever:
outputWindow->setShowOnStderr( false );
break;
}
if ( cleanup->refreshPolicy() == Cleanup::RefreshThis ||
cleanup->refreshPolicy() == Cleanup::RefreshParent )
{
FileInfoSet refreshSet =
cleanup->refreshPolicy() == Cleanup::RefreshParent ?
Refresher::parents( selection ) : selection;
_selectionModel->prepareRefresh( refreshSet );
Refresher * refresher = new Refresher( refreshSet, this );
CHECK_NEW( refresher );
connect( outputWindow, SIGNAL( lastProcessFinished( int ) ),
refresher, SLOT ( refresh() ) );
}
connect( outputWindow, SIGNAL( lastProcessFinished( int ) ),
this, SIGNAL( cleanupFinished ( int ) ) );
foreach ( FileInfo * item, selection )
{
if ( cleanup->worksFor( item ) )
{
cleanup->execute( item, outputWindow );
}
else
{
logWarning() << "Cleanup " << cleanup
<< " does not work for " << item << endl;
}
}
outputWindow->noMoreProcesses();
}
bool CleanupCollection::confirmation( Cleanup * cleanup, const FileInfoSet & items )
{
QString msg = "<html>";
QString title = cleanup->cleanTitle();
if ( items.size() == 1 ) // The most common case
{
FileInfo * item = items.first();
if ( item->isDir() || item->isDotEntry() )
msg += tr( "<h3>%1</h3>for <b>directory</b> %2" ).arg( title ).arg( item->url() );
else
msg += tr( "<h3>%1</h3>for file %2" ).arg( title ).arg( item->url() );
msg += "<br>";
}
else // Multiple items selected
{
QStringList urls;
bool isMixed = items.containsDir() && items.containsFile();
if ( isMixed )
{
QStringList dirs = filteredUrls( items, true, false, // dibs, nonDirs
true ); // extraHighlight
QStringList nonDirs = filteredUrls( items, false, true ); // dirs, nonDirs
dirs = dirs.mid ( 0, MAX_URLS_IN_CONFIRMATION_POPUP );
nonDirs = nonDirs.mid( 0, MAX_URLS_IN_CONFIRMATION_POPUP );
urls << dirs << "" << nonDirs;
}
else // ! isMixed
{
// Build a list of the first couple of selected items (7 max)
urls = filteredUrls( items, true, true ); // dirs, nonDirs
urls = urls.mid( 0, MAX_URLS_IN_CONFIRMATION_POPUP );
}
if ( urls.size() < items.size() ) // Only displaying part of the items?
{
urls << "...";
urls << tr( "<i>(%1 items total)</i>" ).arg( items.size() );
}
msg += tr( "<h3>%1</h3> for:<br>\n%2<br>" ).arg( title ).arg( urls.join( "<br>" ) );
}
int ret = QMessageBox::question( qApp->activeWindow(),
tr( "Please Confirm" ), // title
msg, // text
QMessageBox::Yes | QMessageBox::No );
return ret == QMessageBox::Yes;
}
QStringList CleanupCollection::filteredUrls( const FileInfoSet & items,
bool dirs,
bool nonDirs,
bool extraHighlight ) const
{
QStringList urls;
for ( FileInfoSet::const_iterator it = items.begin(); it != items.end(); ++it )
{
if ( ( dirs && (*it)->isDir() ) ||
( nonDirs && ! (*it)->isDir() ) )
{
QString name = (*it)->url();
if ( (*it)->isDir() )
{
if ( extraHighlight )
urls << tr( "<b>Directory <font color=blue>%1</font></b>" ).arg( name );
else
urls << tr( "<b>Directory</b> %1" ).arg( name );
}
else
urls << name;
}
}
return urls;
}
void CleanupCollection::addToMenu( QMenu * menu, bool keepUpdated )
{
CHECK_PTR( menu );
foreach ( Cleanup * cleanup, _cleanupList )
{
if ( cleanup->active() )
menu->addAction( cleanup );
}
if ( keepUpdated && ! _menus.contains( menu ) )
_menus << menu;
}
void CleanupCollection::addToToolBar( QToolBar * toolBar, bool keepUpdated )
{
CHECK_PTR( toolBar );
foreach ( Cleanup * cleanup, _cleanupList )
{
if ( cleanup->active() &&
! cleanup->icon().isNull() )
{
// Add only cleanups that have an icon to avoid overcrowding the
// toolbar with actions that only have a text.
toolBar->addAction( cleanup );
}
}
if ( keepUpdated && ! _toolBars.contains( toolBar ) )
_toolBars << toolBar;
}
void CleanupCollection::moveUp( Cleanup * cleanup )
{
_listMover.moveUp( cleanup );
updateMenusAndToolBars();
}
void CleanupCollection::moveDown( Cleanup * cleanup )
{
_listMover.moveDown( cleanup );
updateMenusAndToolBars();
}
void CleanupCollection::moveToTop( Cleanup * cleanup )
{
_listMover.moveToTop( cleanup );
updateMenusAndToolBars();
}
void CleanupCollection::moveToBottom( Cleanup * cleanup )
{
_listMover.moveToBottom( cleanup );
updateMenusAndToolBars();
}
void CleanupCollection::readSettings()
{
CleanupSettings settings;
QStringList cleanupGroups = settings.findGroups( settings.groupPrefix() );
if ( ! cleanupGroups.isEmpty() ) // Keep defaults (StdCleanups) if settings empty
{
clear();
// Read all settings groups [Cleanup_xx] that were found
foreach ( const QString & groupName, cleanupGroups )
{
settings.beginGroup( groupName );
// Read one cleanup
QString command = settings.value( "Command" ).toString();
QString title = settings.value( "Title" ).toString();
QString iconName = settings.value( "Icon" ).toString();
QString hotkey = settings.value( "Hotkey" ).toString();
QString shell = settings.value( "Shell" ).toString();
bool active = settings.value( "Active" , true ).toBool();
bool worksForDir = settings.value( "WorksForDir" , true ).toBool();
bool worksForFile = settings.value( "WorksForFile" , true ).toBool();
bool worksForDotEntry = settings.value( "WorksForDotEntry" , true ).toBool();
bool recurse = settings.value( "Recurse" , false ).toBool();
bool askForConfirmation = settings.value( "AskForConfirmation" , false ).toBool();
bool outputWindowAutoClose = settings.value( "OutputWindowAutoClose", false ).toBool();
int outputWindowTimeout = settings.value( "OutputWindowTimeout" , 0 ).toInt();
int refreshPolicy = readEnumEntry( settings, "RefreshPolicy",
Cleanup::NoRefresh,
Cleanup::refreshPolicyMapping() );
int outputWindowPolicy = readEnumEntry( settings, "OutputWindowPolicy",
Cleanup::ShowAfterTimeout,
Cleanup::outputWindowPolicyMapping() );
if ( ! command.isEmpty() &&
! title.isEmpty() )
{
Cleanup * cleanup = new Cleanup( command, title, this );
CHECK_NEW( cleanup );
add( cleanup );
cleanup->setActive ( active );
cleanup->setWorksForDir ( worksForDir );
cleanup->setWorksForFile ( worksForFile );
cleanup->setWorksForDotEntry( worksForDotEntry );
cleanup->setRecurse ( recurse );
cleanup->setShell ( shell );
cleanup->setAskForConfirmation ( askForConfirmation );
cleanup->setOutputWindowAutoClose( outputWindowAutoClose );
cleanup->setOutputWindowTimeout ( outputWindowTimeout );
cleanup->setRefreshPolicy ( static_cast<Cleanup::RefreshPolicy>( refreshPolicy ) );
cleanup->setOutputWindowPolicy( static_cast<Cleanup::OutputWindowPolicy>( outputWindowPolicy ) );
if ( ! iconName.isEmpty() )
cleanup->setIcon( iconName );
if ( ! hotkey.isEmpty() )
cleanup->setShortcut( hotkey );
// if ( ! shell.isEmpty() )
// logDebug() << "Using custom shell " << shell << " for " << cleanup << endl;
}
else
{
logError() << "Need at least Command and Title for a cleanup" << endl;
}
settings.endGroup(); // [Cleanup_01], [Cleanup_02], ...
}
}
}
void CleanupCollection::writeSettings()
{
CleanupSettings settings;
// Remove all leftover cleanup descriptions
settings.removeGroups( settings.groupPrefix() );
// Using a separate group for each cleanup for better readability in the
// file.
//
// Settings arrays are hard to read and to edit if there are more than,
// say, 2-3 entries for each array index. Plus, a user editing the file
// would have to take care of the array count - which is very error prone.
//
// We are using [Cleanup_01], [Cleanup_02], ... here just because that's
// easiest to generate automatically; upon reading, the numbers are
// irrelevant. It's just important that each group name is
// unique. readSettings() will happily pick up any group that starts with
// "Cleanup_".
for ( int i=0; i < _cleanupList.size(); ++i )
{
QString groupName;
groupName.sprintf( "Cleanup_%02d", i+1 );
settings.beginGroup( groupName );
Cleanup * cleanup = _cleanupList.at(i);
settings.setValue( "Command" , cleanup->command() );
settings.setValue( "Title" , cleanup->title() );
settings.setValue( "Active" , cleanup->active() );
settings.setValue( "WorksForDir" , cleanup->worksForDir() );
settings.setValue( "WorksForFile" , cleanup->worksForFile() );
settings.setValue( "WorksForDotEntry" , cleanup->worksForDotEntry() );
settings.setValue( "Recurse" , cleanup->recurse() );
settings.setValue( "AskForConfirmation" , cleanup->askForConfirmation() );
settings.setValue( "OutputWindowAutoClose", cleanup->outputWindowAutoClose() );
if ( cleanup->outputWindowTimeout() > 0 )
settings.setValue( "OutputWindowTimeout" , cleanup->outputWindowTimeout() );
writeEnumEntry( settings, "RefreshPolicy",
cleanup->refreshPolicy(),
Cleanup::refreshPolicyMapping() );
writeEnumEntry( settings, "OutputWindowPolicy",
cleanup->outputWindowPolicy(),
Cleanup::outputWindowPolicyMapping() );
if ( ! cleanup->shell().isEmpty() )
settings.setValue( "Shell", cleanup->shell() );
if ( ! cleanup->iconName().isEmpty() )
settings.setValue( "Icon", cleanup->iconName() );
if ( ! cleanup->shortcut().isEmpty() )
settings.setValue( "Hotkey" , cleanup->shortcut().toString() );
settings.endGroup(); // [Cleanup_01], [Cleanup_02], ...
}
}