Path: blob/master/src/mac-app/PreferencePanelController.m
8814 views
//1// PreferencePanelController.m2//3// Created by Ivan Andrus on 26/6/10.4// Copyright 2010 __MyCompanyName__. All rights reserved.5//67#import "PreferencePanelController.h"8#import "MyDocument.h"9#import "AppController.h"1011@implementation PreferencePanelController121314- (void)windowDidBecomeKey:(NSNotification *)aNotification{1516NSUserDefaults *defaults;17defaults = [NSUserDefaults standardUserDefaults];18NSString *terminalEmulator = [defaults stringForKey:@"TerminalEmulator"];1920// Disable Showing in the Dock on Tiger21if ( [appController isTigerOrLess] ) {22[showInDock setEnabled:NO];23}2425// Set up Terminal Emulation2627// We start with the items bundled with the application and28// overwrite with those the user has saved. This way the user29// gets any items added in later versions.30// TODO: we may want a way to delete them, but then we would never31// be able to delete default ones which might be confusing and32// frustrating33// TODO: ideally we would only save those that the user has added or changed.34NSMutableDictionary *terminalEmulatorList =35[[NSMutableDictionary dictionaryWithContentsOfFile:36[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]37objectForKey:@"TerminalEmulatorList"];3839NSDictionary *savedTermEmuList = [defaults dictionaryForKey:@"TerminalEmulatorList"];40NSEnumerator *enumerator = [savedTermEmuList keyEnumerator];41id key;42// extra parens to suppress warning about using = instead of ==43while( (key = [enumerator nextObject]) ) {44[terminalEmulatorList setObject:[savedTermEmuList objectForKey:key] forKey:key];45}46// Save to defaults since that's how we look it up later47[defaults setObject:terminalEmulatorList forKey:@"TerminalEmulatorList"];48// NSLog(@"TerminalEmulatorList:%@",terminalEmulatorList);4950// Add terminal emulators to UI51[TerminalEmulator removeAllItems];52// This isn't a great sorting method, but it doesn't matter that much. I just want xterm and xterm -- don't exit next to each other53[TerminalEmulator addItemsWithObjectValues:[[terminalEmulatorList allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];54[TerminalEmulator setStringValue:terminalEmulator];55[TerminalApplescript setStringValue:[terminalEmulatorList objectForKey:terminalEmulator]];5657// Set up Default Arguments58NSDictionary *defaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];59[SessionType removeAllItems];60[SessionType addItemsWithObjectValues:[defaultArgList allKeys]];61}6263- (IBAction)apply:(id)sender{64NSUserDefaults *defaults;65defaults = [NSUserDefaults standardUserDefaults];6667NSString *terminalEmulator = [TerminalEmulator stringValue];68[defaults setObject:terminalEmulator forKey:@"TerminalEmulator"];6970NSDictionary *terminalEmulatorList = [defaults dictionaryForKey:@"TerminalEmulatorList"];71NSMutableDictionary *newList = [[terminalEmulatorList mutableCopy] autorelease];72[newList setObject:[TerminalApplescript stringValue] forKey:terminalEmulator];7374// NSLog(@"%@ is now %@", terminalEmulatorList, newList);75[defaults setObject:newList forKey:@"TerminalEmulatorList"];7677NSString *sessionType = [SessionType stringValue];78if ( [sessionType length] > 0 ) {7980[defaults setObject:terminalEmulator forKey:@"SessionType"];8182NSDictionary *DefaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];83NSMutableDictionary *newList = [[DefaultArgList mutableCopy] autorelease];84[newList setObject:[DefaultArgs stringValue] forKey:sessionType];8586// NSLog(@"%@ is now %@", DefaultArgList, newList);87[defaults setObject:newList forKey:@"DefaultArguments"];88}89}9091- (IBAction)resetTerminalApplescript:(id)sender{9293NSDictionary *defaultTerminalEmulatorList =94[[NSDictionary dictionaryWithContentsOfFile:95[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]96objectForKey:@"TerminalEmulatorList"];9798NSString *script = [defaultTerminalEmulatorList objectForKey:[TerminalEmulator stringValue]];99if ( script != nil ) {100[TerminalApplescript setStringValue:script];101}102}103104-(IBAction)addToPATH:(id)sender{105106NSAlert *alert = [[[NSAlert alloc] init] autorelease];107[alert addButtonWithTitle:@"~/bin"];108[alert addButtonWithTitle:@"/usr/local/bin"];109[alert addButtonWithTitle:@"Cancel"];110[alert setMessageText:@"Install link to Sage"];111[alert setInformativeText:@"You are about to install a link to sage in one of two directories which are often in PATH. Any prior version of sage there will be overwritten. If you add it to /usr/local/bin it will require administrator privileges and will be available for all users of this computer.\nIf you install in ~/bin it will be available only for you, and you should make sure that you add it to PATH yourself. You can do this by adding to your ~/.profile (creating if it doesn't exists):\nPATH=$PATH:~/bin"];112[alert setAlertStyle:NSWarningAlertStyle];113114[alert beginSheetModalForWindow:prefWindow115modalDelegate:self116didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)117contextInfo:nil];118}119120121- (void)alertDidEnd:(NSAlert *)alert returnCode:(long)returnCode contextInfo:(void *)contextInfo {122123if (returnCode == NSAlertFirstButtonReturn) {124// ~/bin125// Yes we could do all of this without a shell script, but all the Obj-C methods require 10.5126[appController terminalRun:127[NSString stringWithFormat: @"mkdir -p ~/bin; rm -f ~/bin/sage; ln -s '%@' ~/bin/sage",128[[NSUserDefaults standardUserDefaults] objectForKey:@"SageBinary"]]];129130} else if (returnCode == NSAlertSecondButtonReturn) {131132// take the easy way out with respect to administrator privileges133[appController terminalRun:134[NSString stringWithFormat: @"sudo rm -f /usr/local/bin/sage; sudo ln -s '%@' /usr/local/bin/sage",135[[NSUserDefaults standardUserDefaults] objectForKey:@"SageBinary"]]];136137} else {138// Cancel139}140}141142// This actually ensures the data will be correct143// http://www.cocoabuilder.com/archive/cocoa/221619-detecting-when-nscombobox-text-changed-by-list.html144- (void)controlTextDidEndEditing:(NSNotification *)aNotification{145[appController setupPaths];146[self updateForComboBoxChanges];147}148149// This provides snappier feedback if selecting using the mouse150- (void)comboBoxWillDismiss:(NSNotification *)notification{151[self updateForComboBoxChanges];152}153154-(void)updateForComboBoxChanges{155NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];156157NSDictionary *terminalEmulatorList = [defaults dictionaryForKey:@"TerminalEmulatorList"];158NSString *terminalApplscript = [terminalEmulatorList objectForKey:[TerminalEmulator stringValue]];159if ( terminalApplscript != nil ) {160[TerminalApplescript setStringValue:[terminalEmulatorList objectForKey:[TerminalEmulator stringValue]]];161}162163if ([[SessionType stringValue] length] > 0 ) {164NSDictionary *defaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];165NSString *defArgstring = [defaultArgList objectForKey:[SessionType stringValue]];166if ( defArgstring != nil ) {167[DefaultArgs setStringValue:defArgstring];168}169}170}171172@end173174175