summaryrefslogtreecommitdiffstats
path: root/kdirstat/kcleanup.cpp
blob: 146d7b95982d70400ce36aca9e7818aafa4df6d3 (plain)
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
/*
 *   File name:	kcleanup.cpp
 *   Summary:	Support classes for KDirStat
 *   License:	LGPL - See file COPYING.LIB for details.
 *   Author:	Stefan Hundhammer <sh@suse.de>
 *
 *   Updated:	2004-11-23
 */


#include <stdlib.h>
#include <tqapplication.h>
#include <tqregexp.h>

#include <kapp.h>
#include <kprocess.h>
#include <kdebug.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <tdeglobalsettings.h>

#include "kcleanup.h"
#include "kdirsaver.h"

#define VERBOSE_RUN_COMMAND	1
#define SIMULATE_COMMAND	0

using namespace KDirStat;


KCleanup::KCleanup( TQString		id,
		    TQString		command,
		    TQString		title,
		    TDEActionCollection *	parent	)
    
    : TDEAction( title,
	       0,	// accel
	       parent,
	       id )
    
    , _id	( id	  )
    , _command	( command )
    , _title	( title	  )
{
    _selection		= 0;
    _enabled		= true;
    _worksForDir	= true;
    _worksForFile	= false;
    _worksForDotEntry	= false;
    _worksLocalOnly	= true;
    _recurse		= false;
    _askForConfirmation	= false;
    _refreshPolicy	= noRefresh;
    
    TDEAction::setEnabled( false );
}


KCleanup::KCleanup( const KCleanup &src )
    : TDEAction()
{
    copy( src );
}


KCleanup &
KCleanup::operator= ( const KCleanup &src )
{
    copy( src );
    
    return *this;
}


void
KCleanup::copy( const KCleanup &src )
{
    setTitle( src.title() );
    _selection		= src.selection();
    _id			= src.id();
    _command		= src.command();
    _enabled		= src.enabled();
    _worksForDir	= src.worksForDir();
    _worksForFile	= src.worksForFile();
    _worksForDotEntry	= src.worksForDotEntry();
    _worksLocalOnly	= src.worksLocalOnly();
    _recurse		= src.recurse();
    _askForConfirmation	= src.askForConfirmation();
    _refreshPolicy	= src.refreshPolicy();
}


void
KCleanup::setTitle( const TQString &title )
{
    _title = title;
    TDEAction::setText( _title );
}


bool
KCleanup::worksFor( KFileInfo *item ) const
{
    if ( ! _enabled || ! item )
	return false;

    if ( worksLocalOnly() && ! item->tree()->isFileProtocol() )
	return false;

    if	( item->isDotEntry() )	return worksForDotEntry();
    if	( item->isDir() )	return worksForDir();
    
    return worksForFile();
}


void
KCleanup::selectionChanged( KFileInfo *selection )
{
    bool enabled = false;
    _selection = selection;

    if ( selection )
    {
	enabled = worksFor( selection );

	if ( ! selection->isFinished() )
	{
	    // This subtree isn't finished reading yet

	    switch ( _refreshPolicy )
	    {
		// Refresh policies that would cause this subtree to be deleted
		case refreshThis:
		case refreshParent:
		case assumeDeleted:

		    // Prevent premature deletion of this tree - this would
		    // cause a core dump for sure. 
		    enabled = false;
		    break;
			
		default:
		    break;
	    }
	    
	}
    }
    
    TDEAction::setEnabled( enabled );
}


void
KCleanup::executeWithSelection()
{
    if ( _selection )
	execute( _selection );
}


bool
KCleanup::confirmation( KFileInfo * item )
{
    TQString msg;

    if ( item->isDir() || item->isDotEntry() )
    {
	msg = i18n( "%1\nin directory %2" ).arg( cleanTitle() ).arg( item->url() );
    }
    else
    {
	msg = i18n( "%1\nfor file %2" ).arg( cleanTitle() ).arg( item->url() );
    }

    if ( KMessageBox::warningContinueCancel( 0,				// parentWidget
					     msg,			// message
					     i18n( "Please Confirm" ),	// caption
					     i18n( "Confirm" )		// confirmButtonLabel
					     ) == KMessageBox::Continue )
	return true;
    else
	return false;
}


void
KCleanup::execute( KFileInfo *item )
{
    if ( worksFor( item ) )
    {
	if ( _askForConfirmation && ! confirmation( item ) )
	    return;
	    
	KDirTree  * tree = item->tree();
	
	executeRecursive( item );
	
	switch ( _refreshPolicy )
	{
	    case noRefresh:
		// Do nothing.
		break;


	    case refreshThis:
		tree->refresh( item );
		break;


	    case refreshParent:
		tree->refresh( item->parent() );
		break;


	    case assumeDeleted:

		// Assume the cleanup action has deleted the item.
		// Modify the KDirTree accordingly.

		tree->deleteSubtree( item );

		// Don't try to figure out a reasonable next selection - the
		// views have to do that while handling the subtree
		// deletion. Only the views have any knowledge about a
		// reasonable strategy for choosing a next selection. Unlike
		// the view items, the KFileInfo items don't have an order that
		// makes any sense to the user.

		break;
	}
    }

    emit executed();
}


void
KCleanup::executeRecursive( KFileInfo *item )
{
    if ( worksFor( item ) )
    {
	if ( _recurse )
	{
	    // Recurse into all subdirectories.

	    KFileInfo * subdir = item->firstChild();

	    while ( subdir )
	    {
		if ( subdir->isDir() )
		{
		    /**
		     * Recursively execute in this subdirectory, but only if it
		     * really is a directory: File children might have been
		     * reparented to the directory (normally, they reside in
		     * the dot entry) if there are no real subdirectories on
		     * this directory level.
		     **/
		    executeRecursive( subdir );
		}
		subdir = subdir->next();
	    }
	}


	// Perform cleanup for this directory.

	runCommand( item, _command );
    }
}


const TQString
KCleanup::itemDir( const KFileInfo *item ) const
{
    TQString dir = item->url();

    if ( ! item->isDir() && ! item->isDotEntry() )
    {
	dir.replace ( TQRegExp ( "/[^/]*$" ), "" );
    }

    return dir;
}


TQString
KCleanup::cleanTitle() const
{
    // Use the cleanup action's title, if possible.
   
    TQString title = _title;

    if ( title.isEmpty() )
    {
	title = _id;
    }

    // Get rid of any "&" characters in the text that denote keyboard
    // shortcuts in menus.
    title.replace( TQRegExp( "&" ), "" );

    return title;
}


TQString
KCleanup::expandVariables( const KFileInfo *	item,
			   const TQString &	unexpanded ) const
{
    TQString expanded = unexpanded;

    expanded.replace( TQRegExp( "%p" ),
		      "\"" + TQString::fromLocal8Bit( item->url() )  + "\"" );
    expanded.replace( TQRegExp( "%n" ),
		      "\"" + TQString::fromLocal8Bit( item->name() ) + "\"" );

    if ( KDE::versionMajor() >= 3 && KDE::versionMinor() >= 4 )
	expanded.replace( TQRegExp( "%t" ), "trash:/" );
    else
	expanded.replace( TQRegExp( "%t" ), TDEGlobalSettings::trashPath() );

    return expanded;
}

#include <tqtextcodec.h>
void
KCleanup::runCommand ( const KFileInfo *	item,
		       const TQString &		command ) const
{
    TDEProcess	proc;
    KDirSaver	dir( itemDir( item ) );
    TQString	cmd( expandVariables( item, command ));

#if VERBOSE_RUN_COMMAND
    printf( "\ncd " );
    fflush( stdout );
    system( "pwd" );
    TQTextCodec * codec = TQTextCodec::codecForLocale();
    printf( "%s\n", (const char *) codec->fromUnicode( cmd ) );
    fflush( stdout );
#endif

#if ! SIMULATE_COMMAND
    proc << "sh";
    proc << "-c";
    proc << cmd;

    switch ( _refreshPolicy )
    {
	case noRefresh:
	case assumeDeleted:

	    // In either case it is no use waiting for the command to
	    // finish, so we are starting the command as a pure
	    // background process.

	    proc.start( TDEProcess::DontCare );
	    break;


	case refreshThis:
	case refreshParent:

	    // If a display refresh is due after the command, we need to
	    // wait for the command to be finished in order to avoid
	    // performing the update prematurely, so we are starting this
	    // process in blocking mode.

	    TQApplication::setOverrideCursor( waitCursor );
	    proc.start( TDEProcess::Block );
	    TQApplication::restoreOverrideCursor();
	    break;
    }

#endif
}


void
KCleanup::readConfig()
{
    TDEConfig *config = kapp->config();
    TDEConfigGroupSaver saver( config, _id );

    bool valid = config->readBoolEntry( "valid", false	);

    // If the config section requested exists, it should contain a
    // "valid" field with a true value. If not, there is no such
    // section within the config file. In this case, just leave this
    // cleanup action undisturbed - we'd rather have a good default
    // value (as provided - hopefully - by our application upon
    // startup) than a generic empty cleanup action.
   
    if ( valid )
    {
	_command		= config->readEntry	( "command"		);
	_enabled		= config->readBoolEntry ( "enabled"		);
	_worksForDir		= config->readBoolEntry ( "worksForDir"		);
	_worksForFile		= config->readBoolEntry ( "worksForFile"	);
	_worksForDotEntry	= config->readBoolEntry ( "worksForDotEntry"	);
	_worksLocalOnly		= config->readBoolEntry ( "worksLocalOnly"	);
	_recurse		= config->readBoolEntry ( "recurse"		, false	);
	_askForConfirmation	= config->readBoolEntry ( "askForConfirmation"	, false	);
	_refreshPolicy		= (KCleanup::RefreshPolicy) config->readNumEntry( "refreshPolicy" );
	setTitle( config->readEntry( "title" ) );
    }
}


void
KCleanup::saveConfig() const
{
    TDEConfig *config = kapp->config();
    TDEConfigGroupSaver saver( config, _id );

    config->writeEntry( "valid",		true			);
    config->writeEntry( "command",		_command		);
    config->writeEntry( "title",		_title			);
    config->writeEntry( "enabled",		_enabled		);
    config->writeEntry( "worksForDir",		_worksForDir		);
    config->writeEntry( "worksForFile",		_worksForFile		);
    config->writeEntry( "worksForDotEntry",	_worksForDotEntry	);
    config->writeEntry( "worksLocalOnly",	_worksLocalOnly		);
    config->writeEntry( "recurse",		_recurse		);
    config->writeEntry( "askForConfirmation",	_askForConfirmation	);
    config->writeEntry( "refreshPolicy",	(int) _refreshPolicy	);
}

#include "kcleanup.moc"
// EOF