ProfileController.m (2580B)
1 /* Copyright (c) 2003, see file COPYING for details. */ 2 3 #import "ProfileController.h" 4 #import "Bridge.h" 5 6 @implementation ProfileController 7 8 NSString *unisonDirectory() 9 { 10 return (NSString *)ocamlCall("S", "unisonDirectory"); 11 } 12 13 - (void)initProfiles 14 { 15 NSString *directory = unisonDirectory(); 16 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 17 NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath:directory]; 18 #else 19 NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil]; 20 #endif 21 NSUInteger count = [files count]; 22 NSUInteger i,j; 23 24 [profiles release]; 25 profiles = [[NSMutableArray alloc] init]; 26 defaultIndex = -1; 27 28 if (files) { 29 for (i = j = 0; i < count; i++) { 30 NSString *file = [files objectAtIndex:i]; 31 if ([[file pathExtension] isEqualTo:@"prf"]) { 32 NSString *withoutExtension = [file stringByDeletingPathExtension]; 33 [profiles insertObject:withoutExtension atIndex:j]; 34 if ([@"default" isEqualTo:withoutExtension]) defaultIndex = j; 35 j++; 36 } 37 } 38 NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]; 39 [profiles sortUsingDescriptors:[NSArray arrayWithObject:sort]]; 40 41 if (j > 0) 42 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO]; 43 44 } 45 } 46 47 - (void)awakeFromNib 48 { 49 // start with the default profile selected 50 [self initProfiles]; 51 if (defaultIndex >= 0) 52 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:defaultIndex] byExtendingSelection:NO]; 53 // on awake the scroll bar is inactive, but after adding profiles we might need it; 54 // reloadData makes it happen. Q: is setNeedsDisplay more efficient? 55 [tableView reloadData]; 56 } 57 58 - (NSUInteger)numberOfRowsInTableView:(NSTableView *)aTableView 59 { 60 if (!profiles) return 0; 61 else return [profiles count]; 62 } 63 64 - (id)tableView:(NSTableView *)aTableView 65 objectValueForTableColumn:(NSTableColumn *)aTableColumn 66 row:(int)rowIndex 67 { 68 if (rowIndex >= 0 && rowIndex < [profiles count]) 69 return [profiles objectAtIndex:rowIndex]; 70 else return nil; 71 } 72 73 - (NSString *)selected 74 { 75 NSInteger rowIndex = [tableView selectedRow]; 76 if (rowIndex >= 0 && rowIndex < [profiles count]) 77 return [profiles objectAtIndex:rowIndex]; 78 else return nil; 79 } 80 81 - (NSTableView *)tableView 82 { 83 return tableView; 84 } 85 86 - (NSMutableArray*)getProfiles { 87 return profiles; 88 } 89 90 @end