source: trunk/Cocoa/F-Script Anywhere/Source/DeVercruesseProcess.m@ 7

Last change on this file since 7 was 7, checked in by Nicholas Riley, 22 years ago

F-Script Anywhere 1.1.2a1

File size: 7.7 KB
Line 
1//
2// DeVercruesseProcess.m
3//
4// Created by Frank Vercruesse on Wed Apr 04 2001.
5// Copyright (c) 2001 Frank Vercruesse.
6//
7//
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation; either version 2
11// of the License, or (at your option) any later version.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21// 02111-1307, USA.
22
23// njr changes:
24
25#import "DeVercruesseProcess.h"
26#import "DeVercruesseProcessManager.h"
27
28
29@implementation DeVercruesseProcess
30
31+ (id)processWithPSN:(ProcessSerialNumber*)sn
32{
33 return [[[self alloc] initWithPSN:sn] autorelease];
34}
35
36- (id)init
37{
38 return [self initWithPSN:nil];
39}
40
41- (id)initWithPSN:(ProcessSerialNumber*)sn
42{
43 OSErr err;
44 CPSProcessInfoRec pInfo;
45 int dummy;
46
47
48 if( sn )
49 {
50 err = CPSGetProcessInfo( (CPSProcessSerNum*) sn, &pInfo, nil, 0, &dummy, nil, 0);
51
52 if( !err )
53 {
54 self = [super init];
55
56 if( self )
57 {
58 psn.highLongOfPSN = sn->highLongOfPSN;
59 psn.lowLongOfPSN = sn->lowLongOfPSN;
60
61 pid = pInfo.UnixPID;
62 flavour = pInfo.Flavour;
63 attributes = pInfo.Attributes;
64 type = pInfo.ExecFileType;
65 creator = pInfo.ExecFileCreator;
66
67 bundleIdentifier = nil;
68 identifier = nil;
69
70 name = nil;
71 loc = nil;
72 img = nil;
73 }
74 }
75 else
76 {
77 [self dealloc];
78 return nil;
79 }
80 }
81 else
82 {
83 [self dealloc];
84 return nil;
85 }
86
87 return self;
88}
89
90- (void)dealloc
91{
92 [bundleIdentifier release];
93 [identifier release];
94 [name release];
95 [img release];
96 [cachedImage release];
97 [loc release];
98
99 [super dealloc];
100}
101
102- (NSString *)executableLoc;
103{
104 CFURLRef bundleURL = CFURLCreateWithFileSystemPath(NULL, (CFStringRef) [self loc], kCFURLPOSIXPathStyle, NO);
105 CFBundleRef bundleRef = CFBundleCreate(NULL, bundleURL);
106
107 if (bundleRef == NULL) {
108 CFRelease(bundleURL);
109 return nil; // not a bundle
110 } else {
111 CFURLRef execURL = CFBundleCopyExecutableURL(bundleRef);
112 CFURLRef absURL = CFURLCopyAbsoluteURL(execURL);
113 CFStringRef execPath = CFURLCopyFileSystemPath(absURL, kCFURLPOSIXPathStyle);
114
115 if (absURL != NULL) CFRelease(absURL);
116 if (execURL != NULL) CFRelease(execURL);
117 CFRelease(bundleRef);
118 return [(NSString *)execPath autorelease];
119 }
120}
121
122
123- (NSString*)loc
124{
125 char buffer[1025];
126 FSRef location;
127
128
129 if( !loc )
130 {
131 if( !GetProcessBundleLocation( &psn, &location) )
132 {
133 FSRefMakePath( &location, buffer, 1024);
134 loc = [NSString stringWithUTF8String:buffer];
135 [loc retain];
136 }
137 }
138
139 return loc;
140}
141
142- (NSString*)name;
143{
144 char buffer[1025];
145 CFStringRef pName;
146 NSBundle *b;
147
148
149 if( !name )
150 {
151 if( [self loc] )
152 {
153 b = [NSBundle bundleWithPath:loc];
154 if( b )
155 {
156 name = [b localizedStringForKey:@"CFBundleName" value:@"_AsM_nO_nAmE_" table:@"InfoPlist"];
157
158 if( [name isEqualToString:@"_AsM_nO_nAmE_"] )
159 name = [[b infoDictionary] objectForKey:@"CFBundleName"];
160
161 if( name )
162 [name retain];
163 }
164 }
165
166 if( !name && (CopyProcessName( &psn, &pName) == noErr) ) // with thanks to Dylan Ashe
167 {
168 CFStringGetCString( pName, buffer, 256, CFStringConvertNSStringEncodingToEncoding( [NSString defaultCStringEncoding]));
169
170 name = [NSString stringWithCString:buffer];
171 [name retain];
172
173 CFRelease( pName);
174 }
175 }
176
177 return name;
178}
179
180- (NSImage*)img
181{
182 if( !img )
183 {
184 if( [self loc] )
185 {
186 img = [[NSWorkspace sharedWorkspace] iconForFile:loc];
187
188 if( img )
189 {
190 [img setDataRetained:YES];
191// [img setCachedSeparately:YES]; // added 8/26/01 by Frank
192 [img retain];
193 }
194 }
195 }
196
197 return img;
198}
199
200- (NSImage *)cachedImage;
201{
202 return cachedImage;
203}
204
205- (void)setCachedImage:(NSImage *)anImage;
206{
207 if (anImage != cachedImage) {
208 [anImage retain];
209 [cachedImage release];
210 cachedImage = anImage;
211 }
212}
213
214
215- (ProcessSerialNumber*)psn
216{
217 return &psn;
218}
219
220- (UInt32)pid
221{
222 return pid;
223}
224
225- (UInt32)flavour
226{
227 return flavour;
228}
229
230- (UInt32)attributes
231{
232 CPSProcessInfoRec pInfo;
233 int dummy;
234
235
236 if( CPSGetProcessInfo( (CPSProcessSerNum*) &psn, &pInfo, nil, 0, &dummy, nil, 0) == noErr )
237 attributes = pInfo.Attributes;
238
239 return attributes;
240}
241
242- (UInt32)type
243{
244 return type;
245}
246
247- (UInt32)creator
248{
249 return creator;
250}
251
252- (NSString*)identifier
253{
254 if( !identifier )
255 {
256 identifier = [[NSString alloc] initWithFormat:@"%@%.8X%.8X",
257 [self bundleIdentifier], [self type] ? type : 'APPL', [self creator] ? creator : '????'];
258 }
259
260 return identifier;
261}
262
263- (NSString*)bundleIdentifier
264{
265 NSBundle *b;
266
267
268 if( !bundleIdentifier && [self loc] && (b = [NSBundle bundleWithPath:loc]) )
269 bundleIdentifier = [[b bundleIdentifier] retain];
270
271 return bundleIdentifier ? bundleIdentifier : @"";
272}
273
274
275- (unsigned)hash
276{
277 return psn.lowLongOfPSN;
278// return pid;
279}
280
281- (BOOL)isEqual:(id)anObject
282{
283 if( anObject )
284 return CPSEqualProcess( (CPSProcessSerNum*) &psn, (CPSProcessSerNum*) [anObject psn]);
285 else
286 return NO;
287}
288
289- (BOOL)isEqualToCurrent
290{
291 return [self isEqual:[ProcessMgr currentProcess]];
292}
293
294- (BOOL)isEqualToFront
295{
296 return [self isEqual:[ProcessMgr frontProcess]];
297}
298
299- (BOOL)isStillRunning
300{
301 CPSProcessInfoRec pInfo;
302 int dummy;
303
304
305 if( CPSGetProcessInfo( (CPSProcessSerNum*) &psn, &pInfo, nil, 0, &dummy, nil, 0) == noErr )
306 return YES;
307 else
308 return NO;
309}
310
311- (BOOL)isBackgroundOnly
312{
313 return (((attributes & kCPSBGOnlyAttr) | (attributes & kCPSUIElementAttr)) != 0);
314}
315
316- (BOOL)isHidden
317{
318 return (([self attributes] & kCPSHiddenAttr) != 0);
319}
320
321
322- (BOOL)isClassic
323{
324 return (flavour == kCPSBlueApp);
325}
326
327- (BOOL)isCarbon
328{
329 return (flavour == kCPSCarbonApp);
330}
331
332- (BOOL)isCocoa
333{
334 return (flavour == kCPSYellowApp);
335}
336
337
338- (void)show
339{
340 if( [self isStillRunning] )
341 CPSPostShowReq( (CPSProcessSerNum*) &psn);
342}
343
344- (void)hide
345{
346 if( [self isStillRunning] )
347 CPSPostHideReq( (CPSProcessSerNum*) &psn);
348}
349
350- (void)makeFront
351{
352 if( [self isStillRunning] )
353 {
354 CPSSetFrontProcess( (CPSProcessSerNum*) &psn);
355 CPSPostShowReq( (CPSProcessSerNum*) &psn);
356 }
357}
358
359- (void)kill
360{
361 [self kill:NO];
362}
363
364- (void)kill:(BOOL)hard
365{
366 if( [self isStillRunning] )
367 CPSPostKillRequest( (CPSProcessSerNum*) &psn, hard ? bfCPSKillHard : 0 );
368}
369
370- (NSString *)description;
371{
372 return [NSString stringWithFormat: @"%@ pid %d: '%@' (%@)", [super description], [self pid], [self name], [self identifier]];
373}
374
375@end
Note: See TracBrowser for help on using the repository browser.