diff --git a/Ample/AppDelegate.m b/Ample/AppDelegate.m index 7be75cd..3a2344c 100644 --- a/Ample/AppDelegate.m +++ b/Ample/AppDelegate.m @@ -50,6 +50,8 @@ [self displayLaunchWindow]; } + _diskImages = [DiskImagesWindowController new]; + } -(void)displayLaunchWindow { diff --git a/Ample/DiskImagesWindowController.m b/Ample/DiskImagesWindowController.m index c5f68e6..f727d6f 100644 --- a/Ample/DiskImagesWindowController.m +++ b/Ample/DiskImagesWindowController.m @@ -8,31 +8,38 @@ #import "DiskImagesWindowController.h" #import "TableCellView.h" +#import "Ample.h" @interface DiskImagesWindowController () @property (weak) IBOutlet NSTableView *tableView; @property (strong) IBOutlet NSArrayController *arrayController; -@property NSMutableArray *content; +@property (strong) NSMutableArray *content; + @end @implementation DiskImagesWindowController { - //NSArray *_data; + BOOL _dirty; } + +-(instancetype)init { + + if ((self = [super init])) { + + [self loadRecentDiskImages]; + } + return self; +} + -(NSString *)windowNibName { return @"DiskImages"; } - (void)windowDidLoad { - //_data = [NSMutableArray new]; - [self setContent: - [NSMutableArray arrayWithObject: - [NSMutableDictionary dictionaryWithObjectsAndKeys: - @"/path/to/a/file.2mg", @"path", - @(12345), @"size", - nil] - ]]; + + if (!_content) + [self setContent: [NSMutableArray new]]; [super windowDidLoad]; @@ -41,6 +48,96 @@ // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. } +-(void)loadRecentDiskImages { +// NSError *error; + + NSURL *sd = SupportDirectory(); + NSURL *url = [sd URLByAppendingPathComponent: @"RecentDiskImages.plist"]; + + NSData *data = [NSData dataWithContentsOfURL: url]; + if (data) { + _content = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:nil error: nil]; + + } + if (!_content) + _content = [NSMutableArray new]; + + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + + [nc addObserver: self selector: @selector(diskImageAdded:) name: @"DiskImageAdded" object: nil]; + + [nc addObserver: self selector: @selector(willTerminate:) name: NSApplicationWillTerminateNotification object: nil]; +} + +-(void)diskImageAdded: (NSNotification *)notification { + + NSURL *url = [notification object]; + if (!url) return; + + [self addFile: url]; +} + +-(void)willTerminate: (NSNotification *)notification { + // if dirty, write data.... + + if (!_dirty) return; + + NSURL *sd = SupportDirectory(); + NSURL *url = [sd URLByAppendingPathComponent: @"RecentDiskImages.plist"]; + + if (_content && url) { + [_content writeToURL: url atomically: YES]; + } + + +} + + +-(BOOL)addFile: (NSObject *)pathOrURL { + + NSString *path = nil; + NSURL *url = nil; + if ([pathOrURL isKindOfClass: [NSString class]]) { + path = (NSString *)pathOrURL; + } else if ([pathOrURL isKindOfClass: [NSURL class]]){ + url = (NSURL *)pathOrURL; + + path = [NSString stringWithCString: [url fileSystemRepresentation] encoding: NSUTF8StringEncoding]; + } + if (!path) return NO; + + // todo -- check if file is in the list already... + + + NSFileManager *fm = [NSFileManager defaultManager]; + NSError *error; + + NSDictionary *attr = [fm attributesOfItemAtPath: path error: &error]; + if (error) { + NSLog(@"%@ : %@", path, error); + return NO; + } + + + NSNumber *size = [attr objectForKey: NSFileSize]; + + NSMutableDictionary *d = [NSMutableDictionary dictionaryWithObjectsAndKeys: + path, @"path", + size, @"size", + [NSDate new], @"date", + nil]; + + @synchronized (self) { + if (_arrayController) + [_arrayController addObject: d]; + else + [_content addObject: d]; + } + + _dirty = YES; + return YES; +} + @end @implementation DiskImagesWindowController (TableView) @@ -91,27 +188,8 @@ NSURL *url = [NSURL URLFromPasteboard: pb]; if (!url) return NO; - NSFileManager *fm = [NSFileManager defaultManager]; - - NSString *path = [NSString stringWithCString: [url fileSystemRepresentation] encoding: NSUTF8StringEncoding]; - if (!path) return NO; + return [self addFile: url]; - NSError *error = nil; - NSDictionary *attr = [fm attributesOfItemAtPath: path error: &error]; - if (error) { - NSLog(@"%@ : %@", path, error); - return NO; - } - - NSNumber *size = [attr objectForKey: NSFileSize]; - - NSMutableDictionary *d = [NSMutableDictionary dictionaryWithObjectsAndKeys: - path, @"path", - size, @"size" - , nil]; - - [_arrayController addObject: d]; - return YES; } @end diff --git a/Ample/MediaViewController.m b/Ample/MediaViewController.m index b8df390..5e1f2c1 100644 --- a/Ample/MediaViewController.m +++ b/Ample/MediaViewController.m @@ -611,6 +611,14 @@ static NSString *kDragType = @"private.ample.media"; - (IBAction)pathAction:(id)sender { // need to update the eject button... + + NSURL *url = [(NSPathControl *)sender URL]; + + if (url) { + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + [nc postNotificationName: @"DiskImageAdded" object: url]; + } + [self rebuildArgs]; } @end