home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Kits / MiscTableScroll-138.1 / Examples / LazyScrollDir / DirArray.m < prev    next >
Encoding:
Text File  |  1998-03-31  |  8.5 KB  |  238 lines

  1. //=============================================================================
  2. //
  3. //  Copyright (C) 1995,1996,1997,1998 by Paul S. McCarthy and Eric Sunshine.
  4. //        Written by Paul S. McCarthy and Eric Sunshine.
  5. //                All Rights Reserved.
  6. //
  7. //    This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the authors
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "License.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //
  14. //=============================================================================
  15. //-----------------------------------------------------------------------------
  16. // DirArray.m
  17. //
  18. //    An extensible array of directory entries.
  19. //
  20. //-----------------------------------------------------------------------------
  21. //-----------------------------------------------------------------------------
  22. // $Id: DirArray.m,v 1.4 98/03/30 09:12:26 sunshine Exp $
  23. // $Log:    DirArray.m,v $
  24. // Revision 1.4  98/03/30  09:12:26  sunshine
  25. // v18.1: Now correctly types totalBytes as unsigned long long; not size_t.
  26. // 
  27. // Revision 1.3  97/06/24  07:58:02  sunshine
  28. // v15.1: Removed unused dictionary keys.  Fixed bug: Wasn't taking sticky bit
  29. // into account when computing canToggleLock.
  30. // 
  31. // Revision 1.2  97/04/25  19:53:21  sunshine
  32. // v14.2: Ported to OPENSTEP 4.2 (prerelease) for Mach & NT.
  33. // Completely rewrote to use NSFileManager rather than Unix directory-scanning
  34. // functions so that it works on Windows NT.
  35. //-----------------------------------------------------------------------------
  36. #import    "DirArray.h"
  37. #import    <AppKit/NSApplication.h>
  38. #import <AppKit/NSImage.h>
  39. #import <AppKit/NSWorkspace.h>
  40. #import <Foundation/NSFileManager.h>
  41.  
  42. NSString* const DA_SHORT_NAME = @"ShortName";
  43. NSString* const DA_LONG_NAME = @"LongName";
  44. NSString* const DA_SOFT_LINK = @"SoftLink";
  45. NSString* const DA_IS_DIRECTORY = @"IsDirectory";
  46. NSString* const DA_IS_LOCKED = @"IsLocked";
  47. NSString* const DA_CAN_TOGGLE_LOCK = @"CanToggleLock";
  48. static NSString* const DA_SCALED_ICON = @"ScaledIcon";
  49. static NSString* const DA_UNSCALED_ICON = @"UnscaledIcon";
  50.  
  51.  
  52. //=============================================================================
  53. // DirArray IMPELEMENTATION
  54. //=============================================================================
  55. @implementation DirArray
  56.  
  57. - (unsigned int)count            { return [files count]; }
  58. - (id)objectAtIndex:(unsigned int)n    { return [files objectAtIndex:n]; }
  59. - (unsigned long long)totalBytes    { return totalBytes; }
  60. - (BOOL)writable            { return writable; }
  61. - (BOOL)sticky                { return sticky; }
  62. - (NSString*)username            { return username; }
  63.  
  64. //-----------------------------------------------------------------------------
  65. // init
  66. //-----------------------------------------------------------------------------
  67. - (id)init
  68.     {
  69.     [super init];
  70.     name = [[NSString allocWithZone:[self zone]] init];
  71.     files = [[NSMutableArray allocWithZone:[self zone]] init];
  72.     totalBytes = 0;
  73.     writable = NO;
  74.     sticky = NO;
  75.     username = [NSUserName() retain];
  76.     return self;
  77.     }
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // dealloc
  82. //-----------------------------------------------------------------------------
  83. - (void)dealloc
  84.     {
  85.     [files release];
  86.     [name release];
  87.     [username release];
  88.     [super dealloc];
  89.     }
  90.  
  91.  
  92. //-----------------------------------------------------------------------------
  93. // dirWritable:
  94. //-----------------------------------------------------------------------------
  95. - (BOOL)dirWritable:(NSString*)path
  96.     {
  97.     return [[NSFileManager defaultManager] isWritableFileAtPath:path];
  98.     }
  99.  
  100.  
  101. //-----------------------------------------------------------------------------
  102. // dirSticky:
  103. //-----------------------------------------------------------------------------
  104. - (BOOL)dirSticky:(NSDirectoryEnumerator*)enumerator
  105.     {
  106.     int const STICKY_BIT = 01000;
  107.     unsigned long n = [[enumerator directoryAttributes] filePosixPermissions];
  108.     return ((n & STICKY_BIT) != 0);
  109.     }
  110.  
  111.  
  112. //-----------------------------------------------------------------------------
  113. // addFile:attributes:
  114. //-----------------------------------------------------------------------------
  115. - (void)addFile:(NSString*)file attributes:(NSDictionary*)attributes
  116.     {
  117.     NSFileManager* const manager = [NSFileManager defaultManager];
  118.     NSString* const longName = [file isEqualToString:@".."] ?
  119.     name : [name stringByAppendingPathComponent:file];
  120.     BOOL const canToggle = writable && ![file isEqualToString:@".."] &&
  121.     (!sticky || [[attributes fileOwnerAccountName]
  122.     isEqualToString:username]);
  123.     BOOL const isLink = [[attributes fileType]
  124.     isEqualToString:NSFileTypeSymbolicLink];
  125.     NSDictionary* const deepAttributes = !isLink ? attributes :
  126.     [manager fileAttributesAtPath:longName traverseLink:YES];
  127.     BOOL const isDir = [[deepAttributes fileType]
  128.     isEqualToString:NSFileTypeDirectory];
  129.     NSMutableDictionary* const dict = [[attributes mutableCopy] autorelease];
  130.     [dict setObject:file forKey:DA_SHORT_NAME];
  131.     [dict setObject:longName forKey:DA_LONG_NAME];
  132.     [dict setObject:isLink ? 
  133.     [manager pathContentOfSymbolicLinkAtPath:longName] : @""
  134.     forKey:DA_SOFT_LINK];
  135.     [dict setObject:[NSNumber numberWithBool:isDir] forKey:DA_IS_DIRECTORY];
  136.     [dict setObject:[NSNumber numberWithBool:canToggle]
  137.     forKey:DA_CAN_TOGGLE_LOCK];
  138.     [dict setObject:[NSNumber numberWithBool:!canToggle] forKey:DA_IS_LOCKED];
  139.     [files addObject:dict];
  140.     }
  141.  
  142.  
  143. //-----------------------------------------------------------------------------
  144. // loadPath:showHidden:
  145. //-----------------------------------------------------------------------------
  146. - (BOOL)loadPath:(NSString*)path showHidden:(BOOL)showHidden
  147.     {
  148.     NSFileManager* manager = [NSFileManager defaultManager];
  149.     NSDirectoryEnumerator* enumerator = [manager enumeratorAtPath:path];
  150.  
  151.     [files removeAllObjects];
  152.     [name autorelease];
  153.     name = [path copyWithZone:[self zone]];
  154.     writable = [self dirWritable:path];
  155.     sticky = [self dirSticky:enumerator];
  156.     totalBytes = 0;
  157.  
  158.     if (enumerator != 0)
  159.     {
  160.     NSString* file;
  161.     [self addFile:@".." attributes:
  162.         [manager fileAttributesAtPath:path traverseLink:NO]];
  163.  
  164.     while ((file = [enumerator nextObject]) != 0)
  165.         {
  166.         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  167.         NSDictionary* attributes = [enumerator fileAttributes];
  168.         if (showHidden || [file characterAtIndex:0] != '.')
  169.         {
  170.         totalBytes += [attributes fileSize];
  171.         [self addFile:file attributes:attributes];
  172.         }
  173.         if ([[attributes fileType] isEqualToString:NSFileTypeDirectory])
  174.         [enumerator skipDescendents];
  175.         [pool release];
  176.         }
  177.     }
  178.     return (enumerator != 0);
  179.     }
  180.  
  181. @end
  182.  
  183.  
  184. //=============================================================================
  185. // NSMutableDictionary(DirArray) IMPLEMENTATION
  186. //=============================================================================
  187. @interface NSMutableDictionary(DirArray)
  188. - (void)loadImages;
  189. @end
  190.  
  191. @implementation NSMutableDictionary(DirArray)
  192.  
  193. //-----------------------------------------------------------------------------
  194. // loadImages
  195. //-----------------------------------------------------------------------------
  196. - (void)loadImages
  197.     {
  198.     NSImage* image;
  199.     NSString* name = [self objectForKey:DA_LONG_NAME];
  200.     NSParameterAssert( name != 0 );
  201.     image = [[NSWorkspace sharedWorkspace] iconForFile:name];
  202.     [self setObject:[[image copy] autorelease] forKey:DA_UNSCALED_ICON];
  203.     [image setScalesWhenResized:YES];
  204.     [self setObject:image forKey:DA_SCALED_ICON];
  205.     }
  206.  
  207. @end
  208.  
  209.  
  210. //=============================================================================
  211. // NSDictionary(DirArray) IMPLEMENTATION
  212. //=============================================================================
  213. @implementation NSDictionary(DirArray)
  214.  
  215. //-----------------------------------------------------------------------------
  216. // getImage:
  217. //-----------------------------------------------------------------------------
  218. - (id)getImage:(NSString*)key
  219.     {
  220.     id image = 0;
  221.     if ([self isKindOfClass:[NSMutableDictionary class]])
  222.     {
  223.     if ([self objectForKey:key] == 0)
  224.         [(NSMutableDictionary*)self loadImages];
  225.     image = [self objectForKey:key];
  226.     }
  227.     return image;
  228.     }
  229.  
  230.  
  231. //-----------------------------------------------------------------------------
  232. // [un]scaledImage
  233. //-----------------------------------------------------------------------------
  234. - (id)scaledImage   { return [self getImage:DA_SCALED_ICON  ]; }
  235. - (id)unscaledImage { return [self getImage:DA_UNSCALED_ICON]; }
  236.  
  237. @end
  238.