Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/mac-app/PreferencePanelController.m
8814 views
1
//
2
// PreferencePanelController.m
3
//
4
// Created by Ivan Andrus on 26/6/10.
5
// Copyright 2010 __MyCompanyName__. All rights reserved.
6
//
7
8
#import "PreferencePanelController.h"
9
#import "MyDocument.h"
10
#import "AppController.h"
11
12
@implementation PreferencePanelController
13
14
15
- (void)windowDidBecomeKey:(NSNotification *)aNotification{
16
17
NSUserDefaults *defaults;
18
defaults = [NSUserDefaults standardUserDefaults];
19
NSString *terminalEmulator = [defaults stringForKey:@"TerminalEmulator"];
20
21
// Disable Showing in the Dock on Tiger
22
if ( [appController isTigerOrLess] ) {
23
[showInDock setEnabled:NO];
24
}
25
26
// Set up Terminal Emulation
27
28
// We start with the items bundled with the application and
29
// overwrite with those the user has saved. This way the user
30
// gets any items added in later versions.
31
// TODO: we may want a way to delete them, but then we would never
32
// be able to delete default ones which might be confusing and
33
// frustrating
34
// TODO: ideally we would only save those that the user has added or changed.
35
NSMutableDictionary *terminalEmulatorList =
36
[[NSMutableDictionary dictionaryWithContentsOfFile:
37
[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]
38
objectForKey:@"TerminalEmulatorList"];
39
40
NSDictionary *savedTermEmuList = [defaults dictionaryForKey:@"TerminalEmulatorList"];
41
NSEnumerator *enumerator = [savedTermEmuList keyEnumerator];
42
id key;
43
// extra parens to suppress warning about using = instead of ==
44
while( (key = [enumerator nextObject]) ) {
45
[terminalEmulatorList setObject:[savedTermEmuList objectForKey:key] forKey:key];
46
}
47
// Save to defaults since that's how we look it up later
48
[defaults setObject:terminalEmulatorList forKey:@"TerminalEmulatorList"];
49
// NSLog(@"TerminalEmulatorList:%@",terminalEmulatorList);
50
51
// Add terminal emulators to UI
52
[TerminalEmulator removeAllItems];
53
// 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 other
54
[TerminalEmulator addItemsWithObjectValues:[[terminalEmulatorList allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
55
[TerminalEmulator setStringValue:terminalEmulator];
56
[TerminalApplescript setStringValue:[terminalEmulatorList objectForKey:terminalEmulator]];
57
58
// Set up Default Arguments
59
NSDictionary *defaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];
60
[SessionType removeAllItems];
61
[SessionType addItemsWithObjectValues:[defaultArgList allKeys]];
62
}
63
64
- (IBAction)apply:(id)sender{
65
NSUserDefaults *defaults;
66
defaults = [NSUserDefaults standardUserDefaults];
67
68
NSString *terminalEmulator = [TerminalEmulator stringValue];
69
[defaults setObject:terminalEmulator forKey:@"TerminalEmulator"];
70
71
NSDictionary *terminalEmulatorList = [defaults dictionaryForKey:@"TerminalEmulatorList"];
72
NSMutableDictionary *newList = [[terminalEmulatorList mutableCopy] autorelease];
73
[newList setObject:[TerminalApplescript stringValue] forKey:terminalEmulator];
74
75
// NSLog(@"%@ is now %@", terminalEmulatorList, newList);
76
[defaults setObject:newList forKey:@"TerminalEmulatorList"];
77
78
NSString *sessionType = [SessionType stringValue];
79
if ( [sessionType length] > 0 ) {
80
81
[defaults setObject:terminalEmulator forKey:@"SessionType"];
82
83
NSDictionary *DefaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];
84
NSMutableDictionary *newList = [[DefaultArgList mutableCopy] autorelease];
85
[newList setObject:[DefaultArgs stringValue] forKey:sessionType];
86
87
// NSLog(@"%@ is now %@", DefaultArgList, newList);
88
[defaults setObject:newList forKey:@"DefaultArguments"];
89
}
90
}
91
92
- (IBAction)resetTerminalApplescript:(id)sender{
93
94
NSDictionary *defaultTerminalEmulatorList =
95
[[NSDictionary dictionaryWithContentsOfFile:
96
[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]
97
objectForKey:@"TerminalEmulatorList"];
98
99
NSString *script = [defaultTerminalEmulatorList objectForKey:[TerminalEmulator stringValue]];
100
if ( script != nil ) {
101
[TerminalApplescript setStringValue:script];
102
}
103
}
104
105
-(IBAction)addToPATH:(id)sender{
106
107
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
108
[alert addButtonWithTitle:@"~/bin"];
109
[alert addButtonWithTitle:@"/usr/local/bin"];
110
[alert addButtonWithTitle:@"Cancel"];
111
[alert setMessageText:@"Install link to Sage"];
112
[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"];
113
[alert setAlertStyle:NSWarningAlertStyle];
114
115
[alert beginSheetModalForWindow:prefWindow
116
modalDelegate:self
117
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
118
contextInfo:nil];
119
}
120
121
122
- (void)alertDidEnd:(NSAlert *)alert returnCode:(long)returnCode contextInfo:(void *)contextInfo {
123
124
if (returnCode == NSAlertFirstButtonReturn) {
125
// ~/bin
126
// Yes we could do all of this without a shell script, but all the Obj-C methods require 10.5
127
[appController terminalRun:
128
[NSString stringWithFormat: @"mkdir -p ~/bin; rm -f ~/bin/sage; ln -s '%@' ~/bin/sage",
129
[[NSUserDefaults standardUserDefaults] objectForKey:@"SageBinary"]]];
130
131
} else if (returnCode == NSAlertSecondButtonReturn) {
132
133
// take the easy way out with respect to administrator privileges
134
[appController terminalRun:
135
[NSString stringWithFormat: @"sudo rm -f /usr/local/bin/sage; sudo ln -s '%@' /usr/local/bin/sage",
136
[[NSUserDefaults standardUserDefaults] objectForKey:@"SageBinary"]]];
137
138
} else {
139
// Cancel
140
}
141
}
142
143
// This actually ensures the data will be correct
144
// http://www.cocoabuilder.com/archive/cocoa/221619-detecting-when-nscombobox-text-changed-by-list.html
145
- (void)controlTextDidEndEditing:(NSNotification *)aNotification{
146
[appController setupPaths];
147
[self updateForComboBoxChanges];
148
}
149
150
// This provides snappier feedback if selecting using the mouse
151
- (void)comboBoxWillDismiss:(NSNotification *)notification{
152
[self updateForComboBoxChanges];
153
}
154
155
-(void)updateForComboBoxChanges{
156
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
157
158
NSDictionary *terminalEmulatorList = [defaults dictionaryForKey:@"TerminalEmulatorList"];
159
NSString *terminalApplscript = [terminalEmulatorList objectForKey:[TerminalEmulator stringValue]];
160
if ( terminalApplscript != nil ) {
161
[TerminalApplescript setStringValue:[terminalEmulatorList objectForKey:[TerminalEmulator stringValue]]];
162
}
163
164
if ([[SessionType stringValue] length] > 0 ) {
165
NSDictionary *defaultArgList = [defaults dictionaryForKey:@"DefaultArguments"];
166
NSString *defArgstring = [defaultArgList objectForKey:[SessionType stringValue]];
167
if ( defArgstring != nil ) {
168
[DefaultArgs setStringValue:defArgstring];
169
}
170
}
171
}
172
173
@end
174
175