Clean up manual (previously 'neutral') vs. automatic (i.e. process-driven) configurat...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 01:06:53 +0000 (02:06 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 01:06:53 +0000 (02:06 +0100)
ApplicationController.m
ConfigsController.h
ConfigsController.m
TargetConfig.m

index e87421c..09be1c0 100644 (file)
 
 - (void)didSwitchApplication:(NSNotification *)notification {
     NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
 
 - (void)didSwitchApplication:(NSNotification *)notification {
     NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
-    ProcessSerialNumber psn;
-    OSStatus err;
-    if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) {
-        [self.configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn];
-    } else {
-        NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
-        NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error);
-    }
+    [self.configsController activateConfigForProcess:currentApp.localizedName];
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
@@ -94,6 +87,6 @@
 - (void)chooseConfig:(id)sender {
     int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
     Config *chosen = self.configsController.configs[idx];
 - (void)chooseConfig:(id)sender {
     int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
     Config *chosen = self.configsController.configs[idx];
-    [configsController activateConfig:chosen forApplication:NULL];
+    [configsController activateConfig:chosen];
 }
 @end
 }
 @end
index 5d2b4d1..32e0c61 100644 (file)
 
 - (IBAction)addPressed:(id)sender;
 - (IBAction)removePressed:(id)sender;
 
 - (IBAction)addPressed:(id)sender;
 - (IBAction)removePressed:(id)sender;
-- (void)activateConfig:(Config *)config forApplication:(ProcessSerialNumber *)psn;
+- (void)activateConfig:(Config *)config;
+- (void)activateConfigForProcess:(NSString *)processName;
 
 - (NSDictionary *)dumpAll;
 - (void)loadAllFrom:(NSDictionary*) dict;
 
 @property (readonly) Config *currentConfig;
 
 - (NSDictionary *)dumpAll;
 - (void)loadAllFrom:(NSDictionary*) dict;
 
 @property (readonly) Config *currentConfig;
-@property (readonly) Config *currentNeutralConfig;
 @property (readonly) NSArray *configs;
 
 - (void)save;
 - (void)load;
 
 @property (readonly) NSArray *configs;
 
 - (void)save;
 - (void)load;
 
-- (void)applicationSwitchedTo:(NSString *)name withPsn:(ProcessSerialNumber)psn;
-
 @end
 @end
index 10adcee..50861cf 100644 (file)
@@ -7,7 +7,7 @@
 
 @implementation ConfigsController {
     NSMutableArray *configs;
 
 @implementation ConfigsController {
     NSMutableArray *configs;
-       Config *neutralConfig;
+       Config *manualConfig;
 }
 
 @synthesize currentConfig;
 }
 
 @synthesize currentConfig;
 - (id)init {
        if ((self = [super init])) {
                configs = [[NSMutableArray alloc] init];
 - (id)init {
        if ((self = [super init])) {
                configs = [[NSMutableArray alloc] init];
-               currentConfig = [[Config alloc] init];
-               [currentConfig setName: @"(default)"];
+        currentConfig = [[Config alloc] init];
+        currentConfig.name = @"(default)";
+        manualConfig = currentConfig;
                [configs addObject:currentConfig];
        }
        return self;
 }
 
                [configs addObject:currentConfig];
        }
        return self;
 }
 
-// TODO: Neutral config stuff is a mess.
+- (Config *)objectForKeyedSubscript:(NSString *)name {
+    for (Config *config in configs)
+        if ([name isEqualToString:config.name])
+            return config;
+    return nil;
+}
 
 
--(void) restoreNeutralConfig {
-       if(!neutralConfig)
-               return;
-       [self activateConfig: neutralConfig forApplication: NULL];
+- (void)activateConfigForProcess:(NSString *)processName {
+    Config *oldConfig = manualConfig;
+    [self activateConfig:self[processName]];
+    manualConfig = oldConfig;
 }
 
 }
 
--(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
-       if(currentConfig == config)
+- (void)activateConfig:(Config *)config {
+    if (!config)
+        config = manualConfig;
+       if (currentConfig == config)
                return;
                return;
-
-       if(psn) {
-               if(!neutralConfig)
-                       neutralConfig = currentConfig;
-       } else {
-               neutralConfig = NULL;
-       }
-       
-       if(currentConfig != NULL) {
-               [targetController reset];
-       }
+    manualConfig = config;
        currentConfig = config;
        currentConfig = config;
+    [targetController reset];
        [removeButton setEnabled:configs[0] != config];
        [targetController load];
        [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
        [removeButton setEnabled:configs[0] != config];
        [targetController load];
        [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
     if (tableView.selectedRow == 0)
         return;
 
     if (tableView.selectedRow == 0)
         return;
 
-       Config *current_config = configs[tableView.selectedRow];
+       Config *toRemove = configs[tableView.selectedRow];
        [configs removeObjectAtIndex:tableView.selectedRow];
        [configs removeObjectAtIndex:tableView.selectedRow];
+    
+    if (toRemove == currentConfig)
+        currentConfig = configs[0];
+    if (toRemove == manualConfig)
+        manualConfig = configs[0];
        
        // remove all "switch to configuration" actions
     for (Config *config in configs) {
        
        // remove all "switch to configuration" actions
     for (Config *config in configs) {
@@ -75,7 +79,7 @@
                for (id key in entries) {
                        Target *target = entries[key];
                        if ([target isKindOfClass:[TargetConfig class]]
                for (id key in entries) {
                        Target *target = entries[key];
                        if ([target isKindOfClass:[TargetConfig class]]
-                && [(TargetConfig *)target config] == current_config)
+                && [(TargetConfig *)target config] == toRemove)
                                [entries removeObjectForKey: key];
                }
        }
                                [entries removeObjectForKey: key];
                }
        }
@@ -85,7 +89,7 @@
 
 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
     if (tableView.selectedRow >= 0)
 
 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
     if (tableView.selectedRow >= 0)
-        [self activateConfig:configs[tableView.selectedRow] forApplication: NULL];
+        [self activateConfig:configs[tableView.selectedRow]];
 }
        
 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
 }
        
 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
 
 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
        return index > 0;
 
 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
        return index > 0;
-}      
-
-- (Config *)currentConfig {
-       return currentConfig;
-}
-
-- (Config *)currentNeutralConfig {
-       if (neutralConfig)
-               return neutralConfig;
-       return currentConfig;
 }
 
 -(void) save {
 }
 
 -(void) save {
                [ary addObject: cfgInfo];
        }
        envelope[@"configurationList"] = ary;
                [ary addObject: cfgInfo];
        }
        envelope[@"configurationList"] = ary;
-       envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]);
        return envelope;
 }
        return envelope;
 }
+
 -(void) loadAllFrom: (NSDictionary*) envelope{
        if(envelope == NULL)
                return;
 -(void) loadAllFrom: (NSDictionary*) envelope{
        if(envelope == NULL)
                return;
                }
        }
        
                }
        }
        
-       configs = newConfigs;
-       [tableView reloadData];
-       currentConfig = NULL;
-       [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
-       
-       int index = [envelope[@"selectedIndex"] intValue];
-    if (index < configs.count)
-        [self activateConfig: configs[index] forApplication: NULL];
-}
-
--(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
-       for(int i=0; i<[configs count]; i++) {
-               Config* cfg = configs[i];
-               if([[cfg name] isEqualToString: name]) {
-                       [self activateConfig: cfg forApplication: &psn];
-                       return;
-               }
-       }
-       [self restoreNeutralConfig];
+    if (newConfigs.count) {
+        configs = newConfigs;
+        [tableView reloadData];
+        currentConfig = configs[0];
+        manualConfig = configs[0];
+        [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
+    }
 }
 
 @end
 }
 
 @end
index f62755d..8e4fc40 100644 (file)
@@ -30,7 +30,7 @@
 }
 
 -(void) trigger {
 }
 
 -(void) trigger {
-       [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:config forApplication: NULL];
+       [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:config];
 }
 
 @end
 }
 
 @end