File: MoveToTrash.cpp

package info (click to toggle)
gcvs 1.0final-17
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,248 kB
  • ctags: 10,629
  • sloc: ansic: 71,711; cpp: 39,785; sh: 18,434; makefile: 1,917; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (397 lines) | stat: -rwxr-xr-x 9,852 bytes parent folder | download | duplicates (3)
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
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * MoveToTrash.cpp --- send the files to the trash
 * Author : Miro Jurisic <meeroh@MIT.EDU> --- December 1997
 * Modified : Alexandre Parenteau <aubonbeurre@hotmail.com> --- December 1997
 */

#include "stdafx.h"

#ifdef macintosh
#	include <Files.h>
#	include <Folders.h>
#	include <AERegistry.h>
#	include <AEPackObject.h>
#	include <AEObjects.h>
#endif /* macintosh */

#ifdef qUnix
#	include <stdio.h>
#endif /* qUnix */

#ifdef HAVE_UNISTD_H
#	include <unistd.h>
#endif

#include "MoveToTrash.h"
#include "CPStr.h"
#include "FileTraversal.h"
#include "AppConsole.h"

#ifdef WIN32
#	include <direct.h>
#endif /* WIN32 */

#ifdef WIN32
#	ifdef _DEBUG
#	define new DEBUG_NEW
#	undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
#	endif
#endif /* WIN32 */

#ifdef macintosh
	static OSErr	GetAddressOfFinder (
		AEAddressDesc*	outFinderAddress);
	static OSErr	SendFilesToTrash (
		AEAddressDesc const*	inFinderAddress,
		short					inFileCount,
		const UFSSpec*			inFileList);
	static OSErr	PutFilesInAE (
		short			inFileCount,
		const UFSSpec*	inFileList,
		AppleEvent	*	ioAppleEvent);
	static OSErr ToolboxMoveToTrash(const UFSSpec* spec);

	const	OSType	kFinderSignature	=	'MACS';

	static std::vector<UFSSpec> sToTrash;
	static bool sTrashRetainer = false;
	
	static OSErr	MoveToTrash (const std::vector<UFSSpec> & inFiles)
	{
		OSErr	err = noErr;
		AEDesc	finderAddress = {typeNull, nil};
		
		/*	First unlock the file so that the Finder can rename it
			if there are already files of that name in the Trash */
		std::vector<UFSSpec>::const_iterator i;
		for(i = inFiles.begin(); i != inFiles.end(); ++i)
		{
#if TARGET_RT_MAC_CFM
			err = FSpRstFLock (&*i);
#else
			FSCatalogInfo info;
			if((err = FSGetCatalogInfo(&*i, kFSCatInfoNodeFlags, &info, NULL, NULL, NULL)) == noErr &&
				(info.nodeFlags & kFSNodeLockedMask) != 0)
			{
				info.nodeFlags = info.nodeFlags & ~kFSNodeLockedMask;
				err = FSSetCatalogInfo(&*i, kFSCatInfoNodeFlags, &info);
			}
#endif
			if (err != noErr)
				goto cleanup;
		}

		err = GetAddressOfFinder (&finderAddress);
		if (err != noErr)
			goto cleanup;
		
		err = SendFilesToTrash (&finderAddress, inFiles.size(), &inFiles[0]);
		if (err != noErr)
			goto cleanup;
			
	cleanup:
		if (finderAddress.dataHandle != nil) {
			AEDisposeDesc (&finderAddress);
		}
		if(err != noErr)
		{
			/* try the toolbox way */
			for(i = inFiles.begin(); i != inFiles.end(); ++i)
			{
				err = ToolboxMoveToTrash(&*i);
			}
		}
		
		return err;
	}

	static OSErr	MoveToTrash (const UFSSpec* inFile)
	{
		OSErr	err = noErr;
		AEDesc	finderAddress = {typeNull, nil};
		
		/*	First unlock the file so that the Finder can rename it
			if there are already files of that name in the Trash */
		
#if TARGET_RT_MAC_CFM
		err = FSpRstFLock (inFile);
#else
		FSCatalogInfo info;
		if((err = FSGetCatalogInfo(inFile, kFSCatInfoNodeFlags, &info, NULL, NULL, NULL)) == noErr &&
			(info.nodeFlags & kFSNodeLockedMask) != 0)
		{
			info.nodeFlags = info.nodeFlags & ~kFSNodeLockedMask;
			err = FSSetCatalogInfo(inFile, kFSCatInfoNodeFlags, &info);
		}
#endif
		if (err != noErr)
			goto cleanup;

		err = GetAddressOfFinder (&finderAddress);
		if (err != noErr)
			goto cleanup;
		
		err = SendFilesToTrash (&finderAddress, 1, inFile);
		if (err != noErr)
			goto cleanup;
			
	cleanup:
		if (finderAddress.dataHandle != nil) {
			AEDisposeDesc (&finderAddress);
		}
		if(err != noErr)
		{
			/* try the toolbox way */
			err = ToolboxMoveToTrash(inFile);
		}
		
		return err;
	}

	static OSErr
	ToolboxMoveToTrash(const UFSSpec* spec)
	{
		OSErr err;

#if TARGET_RT_MAC_CFM
		short foundVRefNum;
		long foundDirID;

		err = FindFolder(spec->vRefNum, kTrashFolderType, kDontCreateFolder,
			&foundVRefNum, &foundDirID);

		if(err != noErr)
			return err;
		
		err = CatMove(spec->vRefNum, spec->parID, spec->name, foundDirID, "\p:"/*spec.name*/);
#else
		FSRef folder;
		
		err = FSFindFolder(kOnAppropriateDisk, kTrashFolderType, kDontCreateFolder,
			&folder);

		if(err != noErr)
			return err;
		
		err = FSMoveObject(spec, &folder, NULL);
#endif

		return err;
	}

	/* get address of the Finder (to use as the destination of our appleevents) */

	static OSErr
	GetAddressOfFinder (
		AEAddressDesc*	outFinderAddress)
	{
		return (AECreateDesc (typeApplSignature, (Ptr) &kFinderSignature, sizeof (long), outFinderAddress));
	}

	/* Tell Finder to send a list of files to the trash */

	static OSErr
	SendFilesToTrash (
		AEAddressDesc const*	inFinderAddress,
		short					inFileCount,
		const UFSSpec*			inFileList)
	{
		OSErr		err = noErr;
		AppleEvent	theAppleEvent = {typeNull, nil};
		AppleEvent	theReply = {typeNull, nil};
		DescType	trashType = kTrashFolderType;
		AEDesc		keyData = {typeNull, nil};
		AEDesc		trashSpecifier = {typeNull, nil};
		AEDesc		nullDescriptor = {typeNull, nil};
		
		
		/* create the appleevent */
		err = AECreateAppleEvent (kAECoreSuite, kAEMove, inFinderAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
		if (err != noErr)
			goto cleanup;
		
		/* put the files into the direct object of the appleevent */
		err = PutFilesInAE (inFileCount, inFileList, &theAppleEvent);
		if (err != noErr)
			goto cleanup;
		
		/* create a descriptor for trash */
		err = AECreateDesc (typeType, (Ptr) &trashType, sizeof (DescType) , &keyData);
		if (err != noErr)
			goto cleanup;
		
		/* get specifier for trash object */
		err = CreateObjSpecifier (typeProperty, &nullDescriptor, formPropertyID, &keyData, true, &trashSpecifier);
		if (err != noErr)
			goto cleanup;
		
		/* put the trash specifier as the indirect object in the appleevent */
		err = AEPutParamDesc (&theAppleEvent, keyAEInsertHere, &trashSpecifier);
		if (err != noErr)
			goto cleanup;
		
		/* send the appleevent */
		err = AESend (&theAppleEvent, &theReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
		if (err != noErr)
			goto cleanup;
			
		/* ignore the reply */	

	cleanup:

		/* dispose of the stuff that we allocated */
		if (theAppleEvent.dataHandle != nil) {
			AEDisposeDesc (&theAppleEvent);
		}
		if (theReply.dataHandle != nil) {
			AEDisposeDesc (&theReply);
		}
		if (keyData.dataHandle != nil) {
			AEDisposeDesc (&keyData);
		}
		if (trashSpecifier.dataHandle != nil) {
			AEDisposeDesc (&trashSpecifier);
		}
		/* don't have to dispose of nullDescriptor because it's null */
		
		return err;
	}

	/* put a list of files into the direct object of an apple event */

	static OSErr
	PutFilesInAE (
		short				inFileCount,
		const UFSSpec*		inFileList,
		AppleEvent*			ioAppleEvent)
	{
		OSErr		err;
		AEDescList	fileList = {typeNull, nil};
		short		i;
#if TARGET_RT_MAC_MACHO
		AliasHandle alias;
#endif

		err = AECreateList(nil, 0, false, &fileList);
		if (err != noErr) return err;
		
		for (i = 1; i <= inFileCount; i++) {
#if TARGET_RT_MAC_CFM
			err = AEPutPtr(&fileList, i, typeFSS,
							(Ptr) (inFileList + i - 1), sizeof(UFSSpec));
#else
			err = ::FSNewAlias(NULL, inFileList + i - 1, &alias);
			if (err != noErr) goto cleanup;
			
			::HLock((Handle)alias);
			err = AEPutPtr(&fileList, i, typeAlias, *alias, ::GetHandleSize((Handle)alias));
			::HUnlock((Handle)alias);
			
			::DisposeHandle((Handle)alias);
#endif
			if (err != noErr) goto cleanup;
		}
		
		err = AEPutParamDesc(ioAppleEvent, keyDirectObject, &fileList);
		
	cleanup:
		if (fileList.dataHandle != nil) {
			AEDisposeDesc(&fileList);
		}
		
		return err;
	}
#endif /* macintosh */

void CompatBeginMoveToTrash(void)
{
#ifdef macintosh
	sToTrash.erase(sToTrash.begin(), sToTrash.end());
	sTrashRetainer = true;
#endif
}

void CompatEndMoveToTrash(void)
{
#ifdef macintosh
	if(sToTrash.size() > 0)
	{
		OSErr err;
		err = MoveToTrash(sToTrash);
		if(err != noErr)
			cvs_err("Unable to send files to the trash (Error %d)\n", err);
		else
			cvs_out("Files has been moved successfully to the trash...\n");
	}

	sToTrash.erase(sToTrash.begin(), sToTrash.end());
	sTrashRetainer = false;
#endif
}

bool CompatMoveToTrash(const char *file, const char *dir, const UFSSpec *spec)
{
#ifdef macintosh
	if(sTrashRetainer)
	{
		sToTrash.push_back(*spec);
		return true;
	}
	
	return spec != NULL ? MoveToTrash(spec) == noErr : false;
#elif defined(WIN32)
	SHFILEOPSTRUCT fileop;
	CStr fullpath(dir);
	if(!fullpath.empty() && !fullpath.endsWith(kPathDelimiter))
	{
		fullpath << kPathDelimiter;
	}
	fullpath << file;
	fullpath << ' ';
	fullpath[fullpath.length() - 1] = '\0';

    fileop.hwnd     = *AfxGetMainWnd(); 
    fileop.wFunc    = FO_DELETE; 
    fileop.pFrom    = fullpath; 
    fileop.pTo      = 0L; 
    fileop.fFlags   = FOF_ALLOWUNDO | FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;

	if(SHFileOperation(&fileop) != 0)
	{
		if(dir != 0L && chdir(dir) != 0)
			return false;

		cvs_err("Unable to send \'%s\' to the recycle bin (error %d)\n",
			file, GetLastError());
		cvs_err("Trying now to delete \'%s\' instead\n", file);
		return remove(file) == 0;
	}
	return true;
#else /* !macintosh && !WIN32 */
	CStr fullpath(dir);
	if(!fullpath.empty() && !fullpath.endsWith(kPathDelimiter))
	{
		fullpath << kPathDelimiter;
	}
	fullpath << file;

	return remove(fullpath) == 0;
#endif /* !macintosh && !WIN32 */
}