Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #import "NSFileManager+Trash.h"
  2.  
  3. #import <unistd.h>
  4.  
  5. @implementation NSFileManager (Trash)
  6.  
  7. - (BOOL)trashPath:(NSString *)source showAlerts:(BOOL)flag
  8. {
  9. BOOL isDir;
  10. NSString *destination = nil;
  11.  
  12. if ( [self isDeletableFileAtPath:source] == NO )
  13. {
  14. log_err("don't have the permissions to trash file: %@", source);
  15. return NO;
  16. }
  17.  
  18. if ( [source hasPrefix:@"/Volumes/"] )
  19. {
  20. log_debug("file is on another drive");
  21.  
  22. NSArray *components = [source pathComponents];
  23.  
  24. // Format: /Volumes/<drivename>/.Trashes/<UID>
  25. NSString *trashesFolder = [NSString stringWithFormat:@"/Volumes/%@/.Trashes/%d",
  26. [components objectAtIndex:2], getuid()];
  27.  
  28. // check if we can't trash it (e.g. it's over a network)
  29. if ( ![self fileExistsAtPath:trashesFolder isDirectory:&isDir] || !isDir )
  30. {
  31. log_debug("file is over the network");
  32.  
  33. if (flag == NO) return NO;
  34.  
  35. if (NSRunCriticalAlertPanel(@"Warning",
  36. @"The file \"%@\" will be deleted immediately.\n"
  37. "Are you sure you want to continue?",
  38. @"Yes", @"No", nil,
  39. [source lastPathComponent]) == NSAlertDefaultReturn)
  40. {
  41. return [self removeFileAtPath:source handler:nil];
  42. }
  43.  
  44. // User clicked cancel, they obviously do not want to delete the file.
  45. return NO;
  46. }
  47.  
  48. // get the home directory in a special way to make sure it's not mounted on an external drive
  49. NSString *home = [[[NSHomeDirectory() stringByResolvingSymlinksInPath]
  50. stringByStandardizingPath] stringByAppendingString:@"/"];
  51.  
  52. // make sure this is not the case, if it is we'll just use the normal .Trash folder (below)
  53. if ( ! [source hasPrefix:home] )
  54. {
  55. destination = [trashesFolder stringByAppendingPathComponent:[source lastPathComponent]];
  56. }
  57. }
  58.  
  59. // it's on our volume
  60. if ( destination == nil )
  61. {
  62. destination = [NSHomeDirectory() stringByAppendingPathComponent:@".Trash"];
  63. destination = [destination stringByAppendingPathComponent:[source lastPathComponent]];
  64. }
  65.  
  66. // handle duplicates like the Finder does it
  67. if ( [self fileExistsAtPath:destination] )
  68. {
  69. NSString *pathExtention = [destination pathExtension];
  70. NSString *pathWithoutExtention = [destination stringByDeletingPathExtension];
  71. NSString *now = [[NSCalendarDate calendarDate] descriptionWithCalendarFormat:@" %H-%M-%S"];
  72.  
  73. destination = [pathWithoutExtention stringByAppendingString:now];
  74.  
  75. if ( ! [pathExtention isEqualToString:@""] )
  76. destination = [destination stringByAppendingPathExtension:pathExtention];
  77. }
  78.  
  79. log_debug("trashing file to destination: %@", destination);
  80.  
  81. return [self movePath:source toPath:destination handler:nil];
  82. }
  83.  
  84. @end
Add Comment
Please, Sign In to add comment