Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "NSFileManager+Trash.h"
- #import <unistd.h>
- @implementation NSFileManager (Trash)
- - (BOOL)trashPath:(NSString *)source showAlerts:(BOOL)flag
- {
- BOOL isDir;
- NSString *destination = nil;
- if ( [self isDeletableFileAtPath:source] == NO )
- {
- log_err("don't have the permissions to trash file: %@", source);
- return NO;
- }
- if ( [source hasPrefix:@"/Volumes/"] )
- {
- log_debug("file is on another drive");
- NSArray *components = [source pathComponents];
- // Format: /Volumes/<drivename>/.Trashes/<UID>
- NSString *trashesFolder = [NSString stringWithFormat:@"/Volumes/%@/.Trashes/%d",
- [components objectAtIndex:2], getuid()];
- // check if we can't trash it (e.g. it's over a network)
- if ( ![self fileExistsAtPath:trashesFolder isDirectory:&isDir] || !isDir )
- {
- log_debug("file is over the network");
- if (flag == NO) return NO;
- if (NSRunCriticalAlertPanel(@"Warning",
- @"The file \"%@\" will be deleted immediately.\n"
- "Are you sure you want to continue?",
- @"Yes", @"No", nil,
- [source lastPathComponent]) == NSAlertDefaultReturn)
- {
- return [self removeFileAtPath:source handler:nil];
- }
- // User clicked cancel, they obviously do not want to delete the file.
- return NO;
- }
- // get the home directory in a special way to make sure it's not mounted on an external drive
- NSString *home = [[[NSHomeDirectory() stringByResolvingSymlinksInPath]
- stringByStandardizingPath] stringByAppendingString:@"/"];
- // make sure this is not the case, if it is we'll just use the normal .Trash folder (below)
- if ( ! [source hasPrefix:home] )
- {
- destination = [trashesFolder stringByAppendingPathComponent:[source lastPathComponent]];
- }
- }
- // it's on our volume
- if ( destination == nil )
- {
- destination = [NSHomeDirectory() stringByAppendingPathComponent:@".Trash"];
- destination = [destination stringByAppendingPathComponent:[source lastPathComponent]];
- }
- // handle duplicates like the Finder does it
- if ( [self fileExistsAtPath:destination] )
- {
- NSString *pathExtention = [destination pathExtension];
- NSString *pathWithoutExtention = [destination stringByDeletingPathExtension];
- NSString *now = [[NSCalendarDate calendarDate] descriptionWithCalendarFormat:@" %H-%M-%S"];
- destination = [pathWithoutExtention stringByAppendingString:now];
- if ( ! [pathExtention isEqualToString:@""] )
- destination = [destination stringByAppendingPathExtension:pathExtention];
- }
- log_debug("trashing file to destination: %@", destination);
- return [self movePath:source toPath:destination handler:nil];
- }
- @end
Add Comment
Please, Sign In to add comment