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

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

F-Script Anywhere 1.1.2a1

File size: 7.2 KB
Line 
1//
2//  FSAApp.m
3//  F-Script Anywhere
4//
5//  Created by Nicholas Riley on Fri Feb 01 2002.
6//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9/*
10
11 F-Script Anywhere is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 F-Script Anywhere is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with F-Script Anywhere; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25*/
26
27#import <PatchCocoa.h>
28#import "FSAApp.h"
29#import "FSAnywhere.h"
30
31NSString * const PatchBundleIdentifier = @"net.sabi.FScriptAnywhere";
32
33typedef struct {
34    OSStatus status;
35    NSString * const desc;
36} errRec, errList[];
37
38// These are just educated guesses based on the errors I've seen; I haven't seen these documented anywhere!
39static errList ERRS = {
40    { 5, @"F-Script Anywhere must be installed in a Cocoa application running as the current user.\n\nYou may be attempting to install in a setuid application, which is not supported" },
41    { 11, @"F-Script Anywhere cannot be installed in itself.\n\nIf you wish to install F-Script Anywhere in itself, make a copy of the F-Script Anywhere application and install one copy into the other" },
42    { smUnExBusErr, @"a bus error occurred.\n\nTry switching to the application first then using F-Script AnywhereÕs dock menu to install" },
43    { fnfErr, @"F-Script Anywhere was unable to locate its component to install in the application.  Please try reinstalling F-Script Anywhere" },
44    { cfragDupRegistrationErr, @"another running copy of F-Script Anywhere is already installed in the application"},
45    { 0, nil }
46};
47
48NSMutableDictionary *FSA_errors;
49
50NSString * FSA_descriptionForOSStatus(OSStatus status)
51{
52    NSString *desc = nil;
53   
54    if (FSA_errors != nil)
55        desc = [FSA_errors objectForKey: [NSNumber numberWithLong: status]];
56   
57    if (desc != nil)
58        return desc;
59   
60    return [NSString stringWithFormat: @"an error of type %ld occurred", status];
61}
62
63
64@implementation FSAApp
65
66+ (void)initialize;
67{
68    errRec *rec;
69
70    FSA_errors = [[NSMutableDictionary alloc] init];
71    for (rec = &(ERRS[0]) ; rec->status != 0 ; rec++)
72        [FSA_errors setObject: rec->desc forKey: [NSNumber numberWithLong: rec->status]];
73}
74
75- (void)finishLaunching
76{
77    // yes, someone could move the framework while the app is running, but they deserve what they get.
78    NSFileManager *fileMgr = [NSFileManager defaultManager];
79    NSArray *libraryDirectories =
80        NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, TRUE);
81    NSEnumerator *e = [libraryDirectories objectEnumerator];
82    NSString *libPath;
83    NSString *frameworkPath;
84    BOOL isDirectory;
85    BOOL found = NO;
86
87    while ( (libPath = [e nextObject]) != nil) {
88        frameworkPath = [libPath stringByAppendingPathComponent: @"Frameworks"];
89        if ([fileMgr fileExistsAtPath: [frameworkPath stringByAppendingPathComponent: @"FScript.framework"]
90                          isDirectory: &isDirectory] && isDirectory) {
91            found = YES;
92        }
93    }
94    [super finishLaunching];
95    if (!found) {
96        int result = NSRunInformationalAlertPanel(@"F-Script Framework Not Found", @"F-Script Anywhere requires the F-Script framework be installed in a Frameworks directory, such as ~/Library/Frameworks or /Library/Frameworks.\n\nTo download F-Script, please visit its Web site %@.\n\nIf you believe this message is in error, click ÒContinueÓ.", @"Quit", @"Get F-Script", @"Continue", FSA_FScriptURL);
97        switch (result) {
98            case NSAlertOtherReturn:
99                break;
100            case NSAlertAlternateReturn:
101                [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FSA_FScriptURL]];
102            case NSAlertDefaultReturn:
103            default:
104                [self terminate: self];
105                return;
106        }
107    }
108    [[PatchController sharedPatchController] setDelegate:self];
109}
110
111// This method causes the selected applications to quit, because the Objective-C runtime in Mac OS X does not support unloading of classes.
112- (void)unloadBundles:(id)sender
113{
114    UInt32      index,count;
115    NSArray     *pids;
116
117   
118    pids = [[PatchController sharedPatchController] processPIDsForPatchWithIdentifier:PatchBundleIdentifier];
119    count = [pids count];
120    for (index = 0 ; index < count ; index ++)
121    {
122        [[PatchController sharedPatchController] messagePatchForProcessWithPID:[[pids objectAtIndex:index] longValue]
123                                                                    identifier:PatchBundleIdentifier message:'bye ' data:NULL length:0];
124
125        [[PatchController sharedPatchController] unloadPatchForProcessWithPID:[[pids objectAtIndex:index] longValue]
126                                                                   identifier:PatchBundleIdentifier];
127    }
128
129}
130
131- (void)installBundleInAppWithPID:(pid_t)pid;
132{
133    OSStatus err;
134
135    if (pid == -1) return;
136
137    // XXX Switch to application first, resolves crash in Project Builder.
138    // XXX Fix window closing problem.
139   
140    err = [[PatchController sharedPatchController] loadPatchForProcessWithPID:pid identifier:PatchBundleIdentifier
141                                                                         path:[NSString stringWithFormat:@"%@/F-Script Anywhere",
142                                                                             [[NSBundle mainBundle] resourcePath]]
143                                                                     detached:NO];
144    if (err == noErr) {
145        [[PatchController sharedPatchController] messagePatchForProcessWithPID:pid identifier:PatchBundleIdentifier
146                                                                       message:'helo' data:NULL length:0];
147        [appList didPatchProcessID: pid];
148    }
149    else {
150        NSBeginAlertSheet(@"Installation failed", @"OK", nil, nil, appListPanel, self, nil, nil, nil,
151                          @"F-Script Anywhere was unable to install itself in the selected application (process ID %d), because %@.\n\nThe application may have crashed; restart it if needed.", pid, FSA_descriptionForOSStatus(err));
152    }
153}
154
155- (IBAction)installBundleInSelectedApp:(id)sender;
156{
157    [self installBundleInAppWithPID: [appList selectedProcessID]];
158}
159
160// from dock menu only!
161- (IBAction)installBundleInFrontmostApp:(id)sender;
162{
163    NSAssert1([sender tag] > 0, @"Unable to determine frontmost application from %@", sender);
164    [self installBundleInAppWithPID: (pid_t)[sender tag]];
165}
166
167@end
168
169@implementation FSAApp (PatchControllerDelegate)
170
171- (void) notifyProcessLaunch:(pid_t)pid info:(NSDictionary*)info
172{
173    [appList applicationLaunchedWithProcessID: pid];
174}
175
176- (void) notifyProcessDeath:(pid_t)pid info:(NSDictionary*)info
177{
178    FSALog(@"Process exited (pid %6d): %@\n", pid, [info objectForKey: PatchControllerNameKey]);
179    [appList applicationQuitWithProcessID: pid];
180}
181
182@end
Note: See TracBrowser for help on using the repository browser.