Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 282 lines (214 sloc) 8.173 kb
dc5be1c Initial check-in.
uli authored
1 //
2 // UKApplicationListController.m
3 // TalkingMoose (XC2)
4 //
5 // Created by Uli Kusterer on 2005-12-07.
6 // Copyright 2005 M. Uli Kusterer. All rights reserved.
7 //
8
9 #import "UKApplicationListController.h"
10 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
11 #import "UKSystemInfo.h"
12 #endif
13
14
15 @implementation UKApplicationListController
16
17 -(id) init
18 {
19 if( (self = [super init]) )
20 {
21 listOfApplications = [[NSMutableArray alloc] init];
22
23 // Instead of on dealloc (which isn't called when we're in MainMenu.nib), we listen for app will quit notifications to save our prefs:
24 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillQuit:) name: NSApplicationWillTerminateNotification object: NSApp];
25 }
26
27 return self;
28 }
29
30 -(void) dealloc
31 {
32 [listOfApplications release];
33 listOfApplications = nil;
34
35 [[NSNotificationCenter defaultCenter] removeObserver: self];
36
37 [super dealloc];
38 }
39
40
41 -(void) awakeFromNib
42 {
43 // Load list of apps from prefs here!
44 NSDictionary* dict = [[NSUserDefaults standardUserDefaults] objectForKey: [self userDefaultsKey]];
45 if( dict ) // Have a list in prefs?
46 {
47 // Get rid of old one, add the one from prefs:
48 [listOfApplications autorelease];
49 listOfApplications = [dict mutableCopy];
50
51 // Make all entries in the array mutable:
273f12c Moose fixes: Don't modify the array during enumeration, enumerate the or...
uli authored
52 NSEnumerator* appEntryEnny = [dict objectEnumerator];
dc5be1c Initial check-in.
uli authored
53 NSDictionary* currAppEntry = nil;
54 int x = 0;
55
56 while( (currAppEntry = [appEntryEnny nextObject]) )
57 [listOfApplications replaceObjectAtIndex: x++ withObject: [[currAppEntry mutableCopy] autorelease] ];
58 }
59
60 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
61 #define NSSmallSquareBezelStyle 10
62 // Allow setting up as Square button for pre-10.4 and automatically fix
63 // so it shows Small Square on OS versions that have that new style:
64 if( UKSystemVersion() >= MAC_OS_X_VERSION_10_4 )
65 {
66 [addAppButton setBezelStyle: NSSmallSquareBezelStyle];
67 [removeAppButton setBezelStyle: NSSmallSquareBezelStyle];
68 }
69 #endif
70
71 [applicationListView reloadData];
72 [removeAppButton setEnabled: ([applicationListView selectedRow] >= 0) ];
73 }
74
75
76 -(void) applicationWillQuit: (NSNotification*)notif
77 {
78 // Save list of app dictionaries to prefs:
79 [[NSUserDefaults standardUserDefaults] setObject: listOfApplications forKey: [self userDefaultsKey]];
80 }
81
82
83 -(NSString*) userDefaultsKey
84 {
85 if( autosaveName )
86 return autosaveName;
87 else
88 return @"UKApplicationListController-Apps";
89 }
90
91
92 -(int) numberOfRowsInTableView: (NSTableView*)tableView
93 {
94 return [listOfApplications count];
95 }
96
97
98 -(id) tableView: (NSTableView*)tableView objectValueForTableColumn: (NSTableColumn*)tableColumn row: (int)row
99 {
100 NSMutableDictionary* dict = [listOfApplications objectAtIndex: row];
101
102 if( [[tableColumn identifier] isEqualToString: @"mustBeRunning"] )
103 return [NSNumber numberWithBool: ![[dict objectForKey: @"mustBeFrontmost"] boolValue]];
104 else
105 return [dict objectForKey: [tableColumn identifier]];
106 }
107
108
109 -(void) tableView: (NSTableView*)tableView setObjectValue: (id)object forTableColumn: (NSTableColumn*)tableColumn row: (int)row
110 {
111 NSMutableDictionary* dict = [listOfApplications objectAtIndex: row];
112
113 if( [[tableColumn identifier] isEqualToString: @"mustBeRunning"] )
114 [dict setObject: [NSNumber numberWithBool: ![object boolValue]] forKey: @"mustBeFrontmost"];
115 else
116 [dict setObject: object forKey: [tableColumn identifier]];
117 [tableView setNeedsDisplay: YES];
118 }
119
120
121 -(void) tableViewSelectionDidChange: (NSNotification*)notification
122 {
123 [removeAppButton setEnabled: ([applicationListView selectedRow] >= 0) ];
124 }
125
126
127 -(void) removeSelectedApp: (id)sender
128 {
129 int selRow = [applicationListView selectedRow];
130
131 if( selRow < 0 )
132 return;
133
134 [listOfApplications removeObjectAtIndex: selRow];
135 [applicationListView noteNumberOfRowsChanged];
136 }
137
138
139 -(BOOL) appInListIsRunning
140 {
141 NSEnumerator* appEnny = [listOfApplications objectEnumerator];
142 NSDictionary* currApp = nil;
143 NSArray* runningApps = [[[[NSWorkspace sharedWorkspace] launchedApplications] retain] autorelease];
144
145 while( (currApp = [appEnny nextObject]) )
146 {
147 NSEnumerator* runningAppsEnny = [runningApps objectEnumerator];
148 NSDictionary* currRunningApp = nil;
149
150 while( (currRunningApp = [runningAppsEnny nextObject]) )
151 {
152 if( [[currApp objectForKey: @"applicationPath"] isEqualToString: [currRunningApp objectForKey: @"NSApplicationPath"]] )
153 return YES;
154 }
155 }
156
157 return NO;
158 }
159
160
161 -(BOOL) appInListIsFrontmost
162 {
163 NSEnumerator* appEnny = [listOfApplications objectEnumerator];
164 NSDictionary* currApp = nil;
165 NSDictionary* currRunningApp = [[[[NSWorkspace sharedWorkspace] activeApplication] retain] autorelease];
166
624363c Uli Kusterer Turn off unneeded logging.
authored
167 //UKLog(@"currRunningApp: %@", currRunningApp);
dc5be1c Initial check-in.
uli authored
168
169 while( (currApp = [appEnny nextObject]) )
170 {
171 if( [[currApp objectForKey: @"applicationPath"] isEqualToString: [currRunningApp objectForKey: @"NSApplicationPath"]] )
172 return YES;
173 }
174
175 return NO;
176 }
177
178
179 -(BOOL) screenSaverRunning
180 {
181 NSDictionary* activeApp = [[NSWorkspace sharedWorkspace] activeApplication];
182 BOOL saverOrFullScreen = activeApp == nil;
183 if( activeApp && !saverOrFullScreen )
184 saverOrFullScreen = [[activeApp objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: @"com.apple.ScreenSaver.Engine"];
624363c Uli Kusterer Turn off unneeded logging.
authored
185 //UKLog(@"Screen Saver: %s, %@",(saverOrFullScreen?"YES":"NO"),activeApp);
dc5be1c Initial check-in.
uli authored
186 return( saverOrFullScreen );
187 }
188
189
190 -(BOOL) appInListMatches
191 {
192 NSEnumerator* appEnny = [listOfApplications objectEnumerator];
193 NSDictionary* currApp = nil;
194 NSDictionary* frontmostApp = [[[[NSWorkspace sharedWorkspace] activeApplication] retain] autorelease];
195 NSArray* runningApps = [[[[NSWorkspace sharedWorkspace] launchedApplications] retain] autorelease];
196 NSString* frontmostAppPath = [frontmostApp objectForKey: @"NSApplicationPath"];
197
198 while( (currApp = [appEnny nextObject]) )
199 {
200 NSString* currAppPath = [currApp objectForKey: @"applicationPath"];
201
202 if( [[currApp objectForKey: @"mustBeFrontmost"] boolValue] ) // Must be frontmost? Just compare against frontmost app:
203 {
204 if( [currAppPath isEqualToString: frontmostAppPath] )
205 return YES;
206 }
207 else // Matches when running, even if in back? Check all running apps whether they're this one:
208 {
209 NSEnumerator* runningAppsEnny = [runningApps objectEnumerator];
210 NSDictionary* currRunningApp = nil;
211
212 while( (currRunningApp = [runningAppsEnny nextObject]) )
213 {
214 NSString* currRunningAppPath = [currRunningApp objectForKey: @"NSApplicationPath"];
215
216 if( [currAppPath isEqualToString: currRunningAppPath] )
217 return YES;
218 }
219 }
220 }
221
222 return NO;
223 }
224
225
226
227
228
229 -(void) addApp: (id)sender
230 {
231 NSOpenPanel* appPicker = [NSOpenPanel openPanel];
232
233 [appPicker setCanChooseDirectories: NO];
234 [appPicker setAllowsMultipleSelection: YES];
235 [appPicker setCanChooseFiles: YES];
236 [appPicker setTreatsFilePackagesAsDirectories: NO];
237
238 [appPicker beginSheetForDirectory: @"/Applications" file: @"" types: [NSArray arrayWithObjects: @"app", nil]
239 modalForWindow: [applicationListView window] modalDelegate: self
240 didEndSelector: @selector(appPickerPanelEnded:returnCode:contextInfo:) contextInfo: nil];
241 }
242
243 -(void) appPickerPanelEnded: (NSOpenPanel*)sheet returnCode: (int)returnCode contextInfo: (void*)contextInf
244 {
245 if( returnCode == NSOKButton )
246 {
247 NSEnumerator* filenameEnny = [[sheet filenames] objectEnumerator];
248 NSString* currFilename = nil;
249
250 while( (currFilename = [filenameEnny nextObject]) )
251 {
252 NSBundle* currAppBundle = [NSBundle bundleWithPath: currFilename];
253
254 [listOfApplications addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:
255 [NSNumber numberWithBool: YES], @"mustBeFrontmost",
256 currFilename, @"applicationPath",
257 [[NSFileManager defaultManager] displayNameAtPath: currFilename], @"applicationDisplayName",
258 [currAppBundle bundleIdentifier], @"bundleIdentifier",
259 nil]];
260 }
261 [applicationListView noteNumberOfRowsChanged];
262 }
263 }
264
265
266 -(NSString*) autosaveName
267 {
268 return [[autosaveName retain] autorelease];
269 }
270
271 -(void) setAutosaveName: (NSString*)anAutosaveName
272 {
273 if( autosaveName != anAutosaveName )
274 {
275 [autosaveName release];
276 autosaveName = [anAutosaveName copy];
277 }
278 }
279
280
281 @end
Something went wrong with that request. Please try again.