Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 272 lines (223 sloc) 9.421 kB
a327ea2 @andreyvit Imported official Platypus 4.0 sources.
authored
1 //
2 // PrefsController.m
3 // Platypus
4 //
5 // Created by Sveinbjorn Thordarson on 6/17/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "PrefsController.h"
10
11
12 @implementation PrefsController
13
14 /*****************************************
15 - Set controls according to data in NSUserDefaults
16 *****************************************/
17
18 - (IBAction)showPrefs:(id)sender
19 {
20 if ([prefsWindow isVisible])// if prefs are already visible we just bring the window front
21 {
22 [prefsWindow makeKeyAndOrderFront: sender];
23 return;
24 }
25
26 // set controls according to NSUserDefaults
27 [revealAppCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"RevealApplicationWhenCreated"]];
28 [defaultEditorMenu setTitle: [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultEditor"]];
29 [defaultAuthorTextField setStringValue: [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultAuthor"]];
30 [defaultBundleIdentifierTextField setStringValue: [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultBundleIdentifierPrefix"]];
31
32 //set icons for editor menu
33 [self setIconsForEditorMenu];
34 [self updateCLTStatus];
35
36 //center and show prefs window
37 [prefsWindow center];
38 [prefsWindow makeKeyAndOrderFront: sender];
39 }
40
41 /*****************************************
42 - Set the icons for the menu items in the Editors list
43 *****************************************/
44
45 - (void)setIconsForEditorMenu
46 {
47 int i;
48 NSSize smallIconSize = { 16, 16 };
49
50 for (i = 0; i < [defaultEditorMenu numberOfItems]; i++)
51 {
52 if ([[[defaultEditorMenu itemAtIndex: i] title] isEqualToString: @"Built-In"] == YES)
53 {
54 NSImage *icon = [NSImage imageNamed: @"Platypus"];
55 [icon setSize: smallIconSize];
56 [[defaultEditorMenu itemAtIndex: i] setImage: icon];
57 }
58 else if ([[[defaultEditorMenu itemAtIndex: i] title] isEqualToString: @"Select..."] == NO && [[[defaultEditorMenu itemAtIndex: i] title] length] > 0)
59 {
60 NSImage *icon = [NSImage imageNamed: @"NSDefaultApplicationIcon"];
61 NSString *appPath = [[NSWorkspace sharedWorkspace] fullPathForApplication: [[defaultEditorMenu itemAtIndex: i] title]];
62 if (appPath != NULL) // app found
63 icon = [[NSWorkspace sharedWorkspace] iconForFile: appPath];
64 [icon setSize: smallIconSize];
65 [[defaultEditorMenu itemAtIndex: i] setImage: icon];
66 }
67 }
68 }
69
70 /*****************************************
71 - Set NSUserDefaults according to control settings
72 *****************************************/
73
74 - (IBAction)applyPrefs:(id)sender
75 {
76 [[NSUserDefaults standardUserDefaults] setBool: [revealAppCheckbox state] forKey:@"RevealApplicationWhenCreated"];
77 [[NSUserDefaults standardUserDefaults] setObject: [defaultEditorMenu titleOfSelectedItem] forKey:@"DefaultEditor"];
78 //make sure bundle identifier ends with a '.'
79 if ([[defaultBundleIdentifierTextField stringValue] characterAtIndex: [[defaultBundleIdentifierTextField stringValue]length]-1] != '.')
80
81 [[NSUserDefaults standardUserDefaults] setObject: [[defaultBundleIdentifierTextField stringValue] stringByAppendingString: @"."] forKey:@"DefaultBundleIdentifierPrefix"];
82 else
83 [[NSUserDefaults standardUserDefaults] setObject: [defaultBundleIdentifierTextField stringValue] forKey:@"DefaultBundleIdentifierPrefix"];
84 [[NSUserDefaults standardUserDefaults] setObject: [defaultAuthorTextField stringValue] forKey:@"DefaultAuthor"];
85
86 [prefsWindow performClose: sender];
87 }
88
89
90 /*****************************************
91 - Restore prefs to their default value
92 *****************************************/
93
94 - (IBAction)restoreDefaultPrefs:(id)sender
95 {
96 [revealAppCheckbox setState: NO];
97 [defaultEditorMenu setTitle: @"Built-In"];
98 [defaultAuthorTextField setStringValue: NSFullUserName()];
99
100 // create default bundle identifier prefix string
101 NSString *bundleId = [NSString stringWithFormat: @"org.%@.", NSUserName()];
102 bundleId = [[bundleId componentsSeparatedByString:@" "] componentsJoinedByString:@""];//remove all spaces
103 [defaultBundleIdentifierTextField setStringValue: bundleId];
104 }
105
106
107 /*****************************************
108 - For selecting any application as the external editor for script
109 *****************************************/
110
111 - (IBAction) selectScriptEditor:(id)sender
112 {
113 int result;
114 NSString *editorName;
115
116 //create open panel
117 NSOpenPanel *oPanel = [NSOpenPanel openPanel];
118 [oPanel setTitle: @"Select Editor"];
119 [oPanel setAllowsMultipleSelection:NO];
120 [oPanel setCanChooseDirectories: NO];
121
122 //run open panel
123 result = [oPanel runModalForDirectory:nil file:nil types: [NSArray arrayWithObject:@"app"]];
124 if (result == NSOKButton)
125 {
126 //set app name minus .app suffix
127 editorName = [STUtil cutSuffix: [[oPanel filename] lastPathComponent]];
128 [defaultEditorMenu setTitle: editorName];
129 [self setIconsForEditorMenu];
130 }
131 else
132 [defaultEditorMenu setTitle: [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultEditor"]];
133 }
134
135 /*****************************************
136 - Update report on command line tool install status
137 -- both text field and button
138 *****************************************/
139
140 - (void)updateCLTStatus
141 {
142 //set status of clt install button and text field
143 if ([self isCommandLineToolInstalled])
144 {
145 NSString *versionString = [NSString stringWithContentsOfFile: CMDLINE_VERSION_PATH encoding: NSASCIIStringEncoding error: NULL];
146
147 if ([versionString isEqualToString: PROGRAM_VERSION]) // it's installed and current
148 {
149 [CLTStatusTextField setTextColor: [NSColor greenColor]];
150 [CLTStatusTextField setStringValue: @"Command line tool is installed"];
151 }
152 else // installed but not this version
153 {
154 [CLTStatusTextField setTextColor: [NSColor orangeColor]];
155
156 if ([versionString floatValue] < [PROGRAM_VERSION floatValue])
157 [CLTStatusTextField setStringValue: @"Old version of command line"]; //older
158 else
159 [CLTStatusTextField setStringValue: @"Newer version of command line"]; //newer
160 }
161 [installCLTButton setTitle: @"Uninstall"];
162 }
163 else // it's not installed at all
164 {
165 [CLTStatusTextField setStringValue: @"Command line tool is not installed"];
166 [CLTStatusTextField setTextColor: [NSColor redColor]];
167 [installCLTButton setTitle: @"Install"];
168 }
169 }
170
171 /*****************************************
172 - Install/uninstall CLT based on install status
173 *****************************************/
174
175 - (IBAction)installCLT:(id)sender;
176 {
177 if ([self isCommandLineToolInstalled] == NO)
178 [self installCommandLineTool];
179 else
180 [self uninstallCommandLineTool];
181 }
182
183 /*****************************************
184 - Run install script for CLT stuff
185 *****************************************/
186
187 -(void)installCommandLineTool
188 {
189 [installCLTProgressIndicator setUsesThreadedAnimation: YES];
190 [installCLTProgressIndicator startAnimation: self];
191
192 [self executeScriptWithPrivileges: [[NSBundle mainBundle] pathForResource: @"InstallCommandLineTool.sh" ofType: NULL]];
193
194 [self updateCLTStatus];
195 [installCLTProgressIndicator stopAnimation: self];
196 }
197
198 /*****************************************
199 - Run UNinstall script for CLT stuff
200 *****************************************/
201
202 -(void)uninstallCommandLineTool
203 {
204 [installCLTProgressIndicator setUsesThreadedAnimation: YES];
205 [installCLTProgressIndicator startAnimation: self];
206
207 [self executeScriptWithPrivileges: [[NSBundle mainBundle] pathForResource: @"UninstallCommandLineTool.sh" ofType: NULL]];
208
209 [self updateCLTStatus];
210 [installCLTProgressIndicator stopAnimation: self];
211 }
212
213 /*****************************************
214 - Determine whether command line tool is installed
215 *****************************************/
216
217 -(BOOL)isCommandLineToolInstalled
218 {
219 if ([[NSFileManager defaultManager] fileExistsAtPath: CMDLINE_VERSION_PATH] &&
220 [[NSFileManager defaultManager] fileExistsAtPath: CMDLINE_TOOL_PATH] &&
221 [[NSFileManager defaultManager] fileExistsAtPath: CMDLINE_MANPAGE_PATH] &&
222 [[NSFileManager defaultManager] fileExistsAtPath: CMDLINE_EXEC_PATH] &&
223 [[NSFileManager defaultManager] fileExistsAtPath: CMDLINE_ICON_PATH])
224 {
225 return YES;
226 }
227 return NO;
228 }
229
230 /*****************************************
231 - Run script with privileges using Authentication Manager
232 *****************************************/
233 - (void)executeScriptWithPrivileges: (NSString *)pathToScript
234 {
235 OSErr err = noErr;
236 AuthorizationRef authorizationRef;
237 char *args[2];
238 char resDirPath[4096];
239 char scriptPath[4096];
240
241 //get path to script in c string format
242 [pathToScript getCString: (char *)&scriptPath maxLength: 4096];
243
244 //create array of arguments - first argument is the Resource directory of the Platypus application
245 [[[NSBundle mainBundle] resourcePath] getCString: (char *)&resDirPath];
246 args[0] = resDirPath;
247 args[1] = NULL;
248
249 // Use Apple's Authentication Manager APIs to get an Authorization Reference
250 err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
251 if (err != errAuthorizationSuccess)
252 {
253 NSLog(@"Authorization for script execution failed - Error %d", err);
254 return;
255 }
256
257 //use Authorization Reference to execute the script with privileges
258 if (!(err = AuthorizationExecuteWithPrivileges(authorizationRef,(char *)&scriptPath, kAuthorizationFlagDefaults, args, NULL)) != noErr)
259 {
260 // wait for task to finish
261 int child;
262 wait(&child);
263
264 // destroy the auth ref
265 AuthorizationFree(authorizationRef, kAuthorizationFlagDefaults);
266 }
267 }
268
269 #pragma mark -
270
271 @end
Something went wrong with that request. Please try again.