From 750ede4f2c3f2cf8fb3ee6982b9a9778c09d3eed Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 4 Nov 2011 17:43:15 -0700 Subject: [PATCH] handle storage on the objective-c side. --- Atom/Classes/AtomApp.h | 3 + Atom/Classes/AtomApp.m | 98 ++++++++++++++++++++++---------- Atom/Classes/AtomController.m | 7 ++- Atom/JSCocoa/JSCocoaController.m | 2 +- extensions/tree/tree-pane.coffee | 1 - src/editor.coffee | 20 +++++++ src/storage.coffee | 30 ++++++---- src/window.coffee | 30 +--------- 8 files changed, 119 insertions(+), 72 deletions(-) diff --git a/Atom/Classes/AtomApp.h b/Atom/Classes/AtomApp.h index fff27b881..80d2fe5bb 100644 --- a/Atom/Classes/AtomApp.h +++ b/Atom/Classes/AtomApp.h @@ -9,4 +9,7 @@ - (AtomController *)createController:(NSString *)path; - (void)removeController:(AtomController *)controller; +- (id)storageGet:(NSString *)keyPath defaultValue:(id)defaultValue; +- (id)storageSet:(NSString *)keyPath value:(id)value; + @end diff --git a/Atom/Classes/AtomApp.m b/Atom/Classes/AtomApp.m index 333a42d07..523de45d5 100644 --- a/Atom/Classes/AtomApp.m +++ b/Atom/Classes/AtomApp.m @@ -13,13 +13,11 @@ @synthesize controllers; - (AtomController *)createController:(NSString *)path { - // Don't like this storage code in here. if (path) { - NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH]; - if (!storage) storage = [NSMutableArray array]; - if (![storage containsObject:path]) { - [storage addObject:path]; - [storage writeToFile:ATOM_STORAGE_PATH atomically:YES]; + NSMutableArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]]; + if (![openedPaths containsObject:path]) { + [openedPaths addObject:path]; + [self storageSet:@"app.openedPaths" value:openedPaths]; } } @@ -32,9 +30,9 @@ - (void)removeController:(AtomController *)controller { [controllers removeObject:controller]; - NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH]; - [storage removeObject:controller.path]; - [storage writeToFile:ATOM_STORAGE_PATH atomically:YES]; + NSMutableArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]]; + [openedPaths removeObject:controller.path]; + [self storageSet:@"app.openedPaths" value:openedPaths]; } - (void)open:(NSString *)path { @@ -65,23 +63,23 @@ } // Overridden -- (void)sendEvent:(NSEvent *)event { - if ([event type] == NSKeyDown) { - BOOL handeled = NO; - AtomController *controller = [[self keyWindow] windowController]; - - // The keyWindow could be a Cocoa Dialog or something, ignore that. - if ([controller isKindOfClass:[AtomController class]]) { - JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil]; - handeled = [controller.jscocoa toBool:value]; - } - - if (!handeled) [super sendEvent:event]; - } - else { - [super sendEvent:event]; - } -} +//- (void)sendEvent:(NSEvent *)event { +// if ([event type] == NSKeyDown) { +// BOOL handeled = NO; +// AtomController *controller = [[self keyWindow] windowController]; +// +// // The keyWindow could be a Cocoa Dialog or something, ignore that. +// if ([controller isKindOfClass:[AtomController class]]) { +// JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil]; +// handeled = [controller.jscocoa toBool:value]; +// } +// +// if (!handeled) [super sendEvent:event]; +// } +// else { +// [super sendEvent:event]; +// } +//} // AppDelegate - (void)applicationWillFinishLaunching:(NSNotification *)aNotification { @@ -106,16 +104,56 @@ [NSException raise:@"Atom: Failed to open storage path at '%@'. %@" format:ATOM_USER_PATH, [error localizedDescription]]; } - // Don't like this storage code in here. - NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH]; - if (storage.count == 0) { + NSArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]]; + if (openedPaths.count == 0) { [self createController:NULL]; } else { - for (NSString *path in storage) { + for (NSString *path in openedPaths) { [self createController:path]; } } } +// Helper Methods that should probably go elsewhere +- (id)storage { + id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH]; + if (!storage) storage = [NSMutableDictionary dictionary]; + + return storage; +} + +- (id)storageGet:(NSString *)keyPath defaultValue:(id)defaultValue { + id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH]; + if (!storage) storage = [NSMutableDictionary dictionary]; + + id value = [storage valueForKeyPath:keyPath]; + if (!value) value = defaultValue; + + return value; +} + +- (id)storageSet:(NSString *)keyPath value:(id)value { + id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH]; + if (!storage) storage = [NSMutableDictionary dictionary]; + + NSArray *keys = [keyPath componentsSeparatedByString:@"."]; + id parent = storage; + for (int i = 0; i < keys.count - 1; i++) { + NSString *key = [keys objectAtIndex:i]; + id newParent = [parent valueForKey:key]; + if (!newParent) { + newParent = [NSMutableDictionary dictionary]; + [parent setValue:newParent forKey:key]; + } + parent = newParent; + } + + [storage setValue:value forKeyPath:keyPath]; + + [storage writeToFile:ATOM_STORAGE_PATH atomically:YES]; + + return value; +} + @end diff --git a/Atom/Classes/AtomController.m b/Atom/Classes/AtomController.m index d2d6c435e..7784a075b 100644 --- a/Atom/Classes/AtomController.m +++ b/Atom/Classes/AtomController.m @@ -22,8 +22,8 @@ } - (id)initWithPath:(NSString *)aPath { - aPath = aPath ? aPath : @"/tmp"; - + aPath = aPath ? aPath : @"/tmp"; + self = [super initWithWindowNibName:@"AtomWindow"]; path = [[aPath stringByStandardizingPath] retain]; @@ -33,6 +33,8 @@ - (void)windowDidLoad { [super windowDidLoad]; + [[webView inspector] showConsole:self]; + [self.window setDelegate:self]; [webView setUIDelegate:self]; @@ -49,6 +51,7 @@ [[webView mainFrame] loadRequest:request]; } +// Helper methods that should go elsewhere - (NSString *)tempfile { char *directory = "/tmp"; char *prefix = "temp-file"; diff --git a/Atom/JSCocoa/JSCocoaController.m b/Atom/JSCocoa/JSCocoaController.m index 12ece3494..a0f5e167c 100644 --- a/Atom/JSCocoa/JSCocoaController.m +++ b/Atom/JSCocoa/JSCocoaController.m @@ -1,4 +1,4 @@ - // + // // JSCocoa.m // JSCocoa // diff --git a/extensions/tree/tree-pane.coffee b/extensions/tree/tree-pane.coffee index 08705b389..39a2c17ec 100644 --- a/extensions/tree/tree-pane.coffee +++ b/extensions/tree/tree-pane.coffee @@ -56,7 +56,6 @@ class TreePane extends Pane paths = fs.list root list = $('