- (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)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
@implementation ConfigsController {
NSMutableArray *configs;
- Config *neutralConfig;
+ Config *manualConfig;
}
@synthesize currentConfig;
- (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;
}
-// 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;
-
- if(psn) {
- if(!neutralConfig)
- neutralConfig = currentConfig;
- } else {
- neutralConfig = NULL;
- }
-
- if(currentConfig != NULL) {
- [targetController reset];
- }
+ manualConfig = config;
currentConfig = config;
+ [targetController reset];
[removeButton setEnabled:configs[0] != config];
[targetController load];
[(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
if (tableView.selectedRow == 0)
return;
- Config *current_config = configs[tableView.selectedRow];
+ Config *toRemove = configs[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) {
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];
}
}
-(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 {
- (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 {
[ary addObject: cfgInfo];
}
envelope[@"configurationList"] = ary;
- envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]);
return envelope;
}
+
-(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