QTfrontend/util/platform/M3InstallController.m
branch0.9.19
changeset 8931 ca678bee1a41
parent 8449 2816230a107e
equal deleted inserted replaced
8930:a71599790f7a 8931:ca678bee1a41
    33 
    33 
    34 #import <Foundation/Foundation.h>
    34 #import <Foundation/Foundation.h>
    35 
    35 
    36 @implementation M3InstallController
    36 @implementation M3InstallController
    37 
    37 
    38 - (id) init {
    38 -(id) init {
    39         if ((self = [super init])) {
    39     if ((self = [super init])) {
    40         NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    40         NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    41         NSString *title = [NSString stringWithFormat:NSLocalizedString(@"%@ is currently running from a disk image", @"AppName is currently running from a disk image"), appName];
    41         NSString *title = [NSString stringWithFormat:NSLocalizedString(@"%@ is currently running from a disk image", @"AppName is currently running from a disk image"), appName];
    42         NSString *body = [NSString stringWithFormat:NSLocalizedString(@"Would you like to install %@ in your applications folder before quitting?", @"Would you like to install App Name in your applications folder before quitting?"), appName];
    42         NSString *body = [NSString stringWithFormat:NSLocalizedString(@"Would you like to install %@ in your applications folder before quitting?", @"Would you like to install App Name in your applications folder before quitting?"), appName];
    43         alert = [[NSAlert alertWithMessageText:title
    43         alert = [[NSAlert alertWithMessageText:title
    44                                  defaultButton:NSLocalizedString(@"Install", @"Install")
    44                                  defaultButton:NSLocalizedString(@"Install", @"Install")
    48         //[alert setShowsSuppressionButton:YES];
    48         //[alert setShowsSuppressionButton:YES];
    49     }
    49     }
    50     return self;
    50     return self;
    51 }
    51 }
    52 
    52 
    53 - (void)displayInstaller {
    53 -(void) displayInstaller {
    54     NSString *imageFilePath = [[[NSWorkspace sharedWorkspace] propertiesForPath:[[NSBundle mainBundle] bundlePath]] objectForKey:NSWorkspace_RBimagefilepath];
    54     NSString *imageFilePath = [[[NSWorkspace sharedWorkspace] propertiesForPath:[[NSBundle mainBundle] bundlePath]] objectForKey:NSWorkspace_RBimagefilepath];
    55     if (imageFilePath && ![imageFilePath isEqualToString:[NSString stringWithFormat:@"/Users/.%@/%@.sparseimage", NSUserName(), NSUserName()]] && ![[NSUserDefaults standardUserDefaults] boolForKey:@"M3DontAskInstallAgain"]) {
    55     if (imageFilePath && ![imageFilePath isEqualToString:[NSString stringWithFormat:@"/Users/.%@/%@.sparseimage", NSUserName(), NSUserName()]] && ![[NSUserDefaults standardUserDefaults] boolForKey:@"M3DontAskInstallAgain"]) {
    56         NSInteger returnValue = [alert runModal];
    56         NSInteger returnValue = [alert runModal];
    57         if (returnValue == NSAlertDefaultReturn) {
    57         if (returnValue == NSAlertDefaultReturn) {
    58             [self installApp];
    58             [self installApp];
    59         }
    59         }
    60         if ([[alert suppressionButton] state] == NSOnState) {
    60         if ([NSAlert instancesRespondToSelector:@selector(suppressionButton)])
    61             [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"M3DontAskInstallAgain"];
    61             if ([[alert performSelector:@selector(suppressionButton)] state] == NSOnState)
       
    62                 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"M3DontAskInstallAgain"];
    62         }
    63         }
    63     }
       
    64 }
    64 }
    65 
    65 
    66 - (void)installApp {
    66 -(void) installApp {
    67     NSString *appsPath = [[NSString stringWithString:@"/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]];
    67     NSString *appsPath = [[NSString stringWithString:@"/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]];
    68     NSString *userAppsPath = [[[NSString stringWithString:@"~/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] stringByExpandingTildeInPath];
    68     NSString *userAppsPath = [[[NSString stringWithString:@"~/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] stringByExpandingTildeInPath];
    69     NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    69     NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
       
    70     NSString *currentPath = [[NSBundle mainBundle] bundlePath];
       
    71     NSString *finalPath;
       
    72     NSError *error = nil;
       
    73     BOOL success;
    70 
    74 
    71     //Delete the app that is installed
    75     // Prepare the remove invocation
       
    76     SEL removeSelector;
       
    77     if ([NSFileManager instancesRespondToSelector:@selector(removeItemAtPath:error:)])
       
    78         removeSelector = @selector(removeItemAtPath:error:);
       
    79     else
       
    80         removeSelector = @selector(removeFileAtPath:handler:);
       
    81 
       
    82     NSMethodSignature *removeSignature = [NSFileManager instanceMethodSignatureForSelector:removeSelector];
       
    83     NSInvocation *removeInvocation = [NSInvocation invocationWithMethodSignature:removeSignature];
       
    84     [removeInvocation setTarget:[NSFileManager defaultManager]];
       
    85     [removeInvocation setSelector:removeSelector];
       
    86 
       
    87     // Delete the app if already installed
    72     if ([[NSFileManager defaultManager] fileExistsAtPath:appsPath]) {
    88     if ([[NSFileManager defaultManager] fileExistsAtPath:appsPath]) {
    73         if ([NSFileManager instancesRespondToSelector:@selector(removeItemAtPath:error:)])
    89         [removeInvocation setArgument:&appsPath atIndex:2];
    74             [[NSFileManager defaultManager] removeItemAtPath:appsPath error:nil];
    90         [removeInvocation setArgument:&error atIndex:3];
    75         else
    91         [removeInvocation invoke];
    76             //casting hides the deprecation warning
       
    77             [(id)[NSFileManager defaultManager] removeFileAtPath:appsPath handler:nil];
       
    78     }
    92     }
    79     //Delete the app that is installed
    93 
    80     BOOL success = NO;
    94     // Prepare the copy invocation
       
    95     SEL copySelector;
    81     if ([NSFileManager instancesRespondToSelector:@selector(copyItemAtPath:toPath:error:)])
    96     if ([NSFileManager instancesRespondToSelector:@selector(copyItemAtPath:toPath:error:)])
    82         success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] bundlePath]
    97         copySelector = @selector(copyItemAtPath:toPath:error:);
    83                                                           toPath:appsPath
       
    84                                                            error:nil];
       
    85     else
    98     else
    86         success = [(id)[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath]
    99         copySelector = @selector(copyPath:toPath:handler:);
    87                                                         toPath:appsPath
   100 
    88                                                        handler:nil];
   101     NSMethodSignature *copySignature = [NSFileManager instanceMethodSignatureForSelector:copySelector];
    89     if (success) {
   102     NSInvocation *copyInvocation = [NSInvocation invocationWithMethodSignature:copySignature];
    90         NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"App Name installed successfully"), appName],
   103 
    91                         [NSString stringWithFormat:NSLocalizedString(@"%@ was installed in /Applications", @"App Name was installed in /Applications"), appName],
   104     [copyInvocation setTarget:[NSFileManager defaultManager]];
    92                         NSLocalizedString(@"Quit", @"Quit"), nil, nil);
   105     [copyInvocation setSelector:copySelector];
    93     } else {
   106 
       
   107     // Copy the app in /Applications
       
   108     [copyInvocation setArgument:&currentPath atIndex:2];
       
   109     [copyInvocation setArgument:&appsPath atIndex:3];
       
   110     [copyInvocation setArgument:&error atIndex:4];
       
   111     [copyInvocation invoke];
       
   112     [copyInvocation getReturnValue:&success];
       
   113     finalPath = @"/Applications";
       
   114 
       
   115     // In case something went wrong, let's try again somewhere else
       
   116     if (success == NO) {
       
   117         // Delete the app if already installed
    94         if ([[NSFileManager defaultManager] fileExistsAtPath:userAppsPath]) {
   118         if ([[NSFileManager defaultManager] fileExistsAtPath:userAppsPath]) {
    95             if ([NSFileManager instancesRespondToSelector:@selector(removeItemAtPath:error:)])
   119             [removeInvocation setArgument:&userAppsPath atIndex:2];
    96                 [[NSFileManager defaultManager] removeItemAtPath:userAppsPath error:nil];
   120             [removeInvocation invoke];
    97             else
       
    98                 [(id)[NSFileManager defaultManager] removeFileAtPath:userAppsPath handler:nil];
       
    99         }
   121         }
   100         if ([NSFileManager instancesRespondToSelector:@selector(copyItemAtPath:toPath:error:)])
   122 
   101             success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] bundlePath]
   123         // Copy the app in ~/Applications
   102                                                               toPath:userAppsPath
   124         [copyInvocation setArgument:&userAppsPath atIndex:3];
   103                                                                error:nil];
   125         [copyInvocation invoke];
   104         else
   126         [copyInvocation getReturnValue:&success];
   105             success = [(id)[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath]
   127         finalPath = [[NSString stringWithString:@"~/Applications"] stringByExpandingTildeInPath];
   106                                                             toPath:userAppsPath
       
   107                                                            handler:nil];
       
   108         if (success) {
       
   109             NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"AppName installed successfully"), appName],
       
   110                 [NSString stringWithFormat:NSLocalizedString(@"%@ was installed in %@", @"App Name was installed in %@"), appName, [[NSString stringWithString:@"~/Applications"] stringByExpandingTildeInPath]],
       
   111                         NSLocalizedString(@"Quit", @"Quit"), nil, nil);
       
   112         } else {
       
   113             NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"Could not install %@", @"Could not install App Name"), appName],
       
   114                             NSLocalizedString(@"An error occurred when installing", @"An error occurred when installing"), NSLocalizedString(@"Quit", @"Quit"), nil, nil);
       
   115         }
       
   116     }
   128     }
       
   129 
       
   130     if (success)
       
   131         NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"successful installation title"), appName],
       
   132               [NSString stringWithFormat:NSLocalizedString(@"%@ was installed in %@", @"successfull installation text"), appName, finalPath],
       
   133               NSLocalizedString(@"Ok", @"ok message"), nil, nil);
       
   134     else
       
   135         NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"Could not install %@", @"installation failure title"), appName],
       
   136               NSLocalizedString(@"An error occurred when installing", @"installation failure text"),
       
   137               NSLocalizedString(@"Quit", @"exit message"), nil, nil);
   117 }
   138 }
   118 
   139 
   119 @end
   140 @end