Ignore:
Timestamp:
05/25/06 17:46:45 (18 years ago)
Author:
rchin
Message:

Auto installation of F-Script as requested by Philippe Mougin. We'll optimize the FSA framework package later (not sure if it would be a good idea to remove headers in framework, etc.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/F-Script Anywhere/Source/FSAApp.mm

    r229 r230  
    3030#import <CoreFoundation/CoreFoundation.h>
    3131#import <ApplicationServices/ApplicationServices.h>
    32 #import <Security/Authorization.h>
    3332#import <Security/AuthorizationTags.h>
    3433
     
    121120}
    122121
     122-(void)installFScript
     123{
     124    OSStatus myStatus;
     125
     126    NSString *installPath = nil;
     127    if([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/Frameworks/FScript.framework"])
     128        installPath = @"/Library/Frameworks/FScript.framework";
     129    if([[NSFileManager defaultManager] fileExistsAtPath:[@"~/Library/Frameworks/FScript.framework" stringByExpandingTildeInPath]])
     130        installPath = [@"~/Library/Frameworks/FScript.framework" stringByExpandingTildeInPath];
     131   
     132    if(installPath){
     133        [[NSFileManager defaultManager] removeFileAtPath:installPath handler:nil];
     134    }
     135   
     136    if(!installPath){
     137        switch([[NSAlert alertWithMessageText:@"Install F-Script"
     138                                defaultButton:@"Current User"
     139                              alternateButton:@"All Users"
     140                                  otherButton:nil
     141                    informativeTextWithFormat:@"No previous version of F-Script found. You can choose to install for just your current user (~/Library/Frameworks) or for all users (/Library/Frameworks)."] runModal]){
     142            case NSAlertDefaultReturn:  // Current user
     143                installPath = [@"~/Library/Frameworks/FScript.framework" stringByExpandingTildeInPath];
     144                break;
     145            case NSAlertAlternateReturn: // All users
     146                installPath = @"/Library/Frameworks/FScript.framework";
     147                break;
     148            default:
     149                break;
     150        }       
     151    }
     152   
     153    NSString *fromPath = [[NSBundle mainBundle] pathForResource:@"FScript" ofType:@"framework"];
     154    if(![[NSFileManager defaultManager] fileExistsAtPath:[installPath stringByDeletingLastPathComponent]]){
     155        if ([[NSFileManager defaultManager] isWritableFileAtPath:installPath]){
     156            [[NSFileManager defaultManager] createDirectoryAtPath:[installPath stringByDeletingLastPathComponent]
     157                                                       attributes:nil];
     158        } else {
     159            [self createAuthorization];
     160            NSString *myToolPath = @"/bin/mkdir";
     161            char *myArguments[] = { (char *)[[installPath stringByDeletingLastPathComponent] UTF8String], NULL };
     162            myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, [myToolPath UTF8String],
     163                                                          myFlags, myArguments,
     164                                                          NULL);
     165        }
     166    }
     167    if([[NSFileManager defaultManager] isWritableFileAtPath:[installPath stringByDeletingLastPathComponent]]){
     168        [[NSFileManager defaultManager] copyPath:fromPath toPath:installPath handler:nil];
     169    } else {
     170        [self createAuthorization];
     171        NSString *myToolPath = @"/bin/cp";
     172        char *myArguments[] = { "-r", (char *)[fromPath UTF8String], (char *)[installPath UTF8String], NULL };
     173        myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, [myToolPath UTF8String],
     174                                                      myFlags, myArguments,
     175                                                      NULL);
     176    }   
     177}
     178
    123179- (BOOL)_shouldContinueAfterFrameworkErrorPanelReturnedWithResult:(int)result;
    124180{
     
    126182        case NSAlertOtherReturn:
    127183            return true;
    128         case NSAlertAlternateReturn:
    129             [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FSA_FScriptURL]];
     184        case NSAlertAlternateReturn: /* Install from resources */
     185            [self installFScript];
     186            return true;
    130187        case NSAlertDefaultReturn:
    131188        default:
     
    135192}
    136193
    137 -(tDirStatus)authorizeAndAddToProcMod:(NSString *)username
    138 {
     194-(BOOL)createAuthorization
     195{
     196    if(myAuthorizationRef)
     197        return YES;
     198   
    139199    OSStatus myStatus;
    140     AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
    141     AuthorizationRef myAuthorizationRef;
     200    myFlags = kAuthorizationFlagDefaults;
    142201   
    143202    myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
    144203                                   myFlags, &myAuthorizationRef);
    145204    if(myStatus != errAuthorizationSuccess)
    146         return eDSAuthFailed;
     205        return NO;
    147206   
    148207    AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
     
    157216   
    158217    if(myStatus != errAuthorizationSuccess)
     218        return NO;
     219   
     220    myFlags = kAuthorizationFlagDefaults;
     221    return YES;
     222}
     223
     224-(tDirStatus)authorizeAndAddToProcMod:(NSString *)username
     225{
     226    OSStatus myStatus;
     227   
     228    if(![self createAuthorization])
    159229        return eDSAuthFailed;
    160    
    161     myFlags = kAuthorizationFlagDefaults;
    162230   
    163231    NSString *myToolPath = [[NSBundle mainBundle] pathForResource:@"AddToProcMod" ofType:@""];
     
    176244    myReadBuffer[didRead - 1] = 0;
    177245   
    178     AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
    179 
    180246    return (tDirStatus)strtol(myReadBuffer, NULL, 10);
    181247}
     
    274340        int result = NSRunInformationalAlertPanel(
    275341            NSLocalizedString(@"F-Script Framework Not Found", "Framework not found alert title"),
    276             NSLocalizedString(@"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%@.\n\nIf you believe this message is in error, click '%@'.",
     342            NSLocalizedString(@"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%@.\n\nThis package also includes a copy of F-Script for your convenience that can be installed easily by clicking below. Note that there may be a more recent version of F-Script available from the F-Script web site.\n\nIf you believe this message is in error, click '%@'.",
    277343                              "Framework not found alert message, F-Script Web site URL parameter, 'Continue' button parameter"),
    278344            NSLocalizedString(@"Quit", "Quit button title"),
    279             NSLocalizedString(@"Get F-Script", "'Get F-Script' button title"),
     345            NSLocalizedString(@"Install F-Script", "'Get F-Script' button title"),
    280346            NSLocalizedString(@"Continue", "Continue button title"),
    281347            FSA_FScriptURL, NSLocalizedString(@"Continue", ""));
     
    290356            int result = NSRunInformationalAlertPanel(
    291357                NSLocalizedString(@"F-Script Framework Too Old", "Framework too old alert title"),
    292                 NSLocalizedString(@"F-Script Anywhere %s requires version %@ or later of the F-Script framework be installed in a Frameworks directory, such as ~/Library/Frameworks or /Library/Frameworks. The installed version is %@.\n\nTo upgrade F-Script, please visit its Web site\n%@.\n\nTo use this version of F-Script regardless of the problems you may experience, click '%@'.",
     358                NSLocalizedString(@"F-Script Anywhere %s requires version %@ or later of the F-Script framework be installed in a Frameworks directory, such as ~/Library/Frameworks or /Library/Frameworks. The installed version is %@.\n\nTo upgrade F-Script, please visit its Web site\n%@.\n\nThis package also includes a copy of F-Script for your convenience that can be installed easily by clicking below. Note that there may be a more recent version of F-Script available from the F-Script web site.\n\nTo use this version of F-Script regardless of the problems you may experience, click '%@'.",
    293359                                  "Framework too old alert message, FSA version parameter, minimum F-Script version parameter, current F-Script version parameter, F-Script Web site URL parameter, 'Continue' button parameter"),
    294360                NSLocalizedString(@"Quit", "Quit button title"),
    295                 NSLocalizedString(@"Get F-Script", "'Get F-Script' button title"),
     361                NSLocalizedString(@"Install F-Script", "'Get F-Script' button title"),
    296362                NSLocalizedString(@"Continue", "Continue button title"),
    297363                FSA_VERSION, FSA_FScriptMinimumVersion, frameworkVersion,
    298                 FSA_FScriptURL, NSLocalizedString(@"Continue", ""));
     364                FSA_FScriptURL, NSLocalizedString(@"Install F-Script", ""));
    299365            if (![self _shouldContinueAfterFrameworkErrorPanelReturnedWithResult: result]) return;
    300366        }
    301367    }
    302368
     369    if(myAuthorizationRef){
     370        AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
     371        myAuthorizationRef = nil;
     372    }
     373   
    303374    NSNotificationCenter *workspaceNotificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
    304375    [workspaceNotificationCenter
Note: See TracChangeset for help on using the changeset viewer.