QTfrontend/util/platform/M3InstallController.m
author koda
Sat, 26 Jan 2013 23:56:10 +0100
changeset 8440 ea4d6a7a2937
parent 8381 588a8e6e2041
child 8449 2816230a107e
permissions -rw-r--r--
complete tabs and trailing whitespace formatting on frontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     1
/*****************************************************************
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     2
 M3InstallController.m
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
     3
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     4
 Created by Martin Pilkington on 02/06/2007.
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
     5
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     6
 Copyright (c) 2006-2009 M Cubed Software
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
     7
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     8
 Permission is hereby granted, free of charge, to any person
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
     9
 obtaining a copy of this software and associated documentation
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    10
 files (the "Software"), to deal in the Software without
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    11
 restriction, including without limitation the rights to use,
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    12
 copy, modify, merge, publish, distribute, sublicense, and/or sell
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    13
 copies of the Software, and to permit persons to whom the
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    14
 Software is furnished to do so, subject to the following
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    15
 conditions:
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
    16
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    17
 The above copyright notice and this permission notice shall be
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    18
 included in all copies or substantial portions of the Software.
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
    19
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    20
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    21
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    22
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    23
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    24
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    25
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    26
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    27
 OTHER DEALINGS IN THE SOFTWARE.
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
    28
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    29
 *****************************************************************/
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    30
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    31
#import "M3InstallController.h"
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    32
#import "NSWorkspace_RBAdditions.h"
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    33
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    34
#import <Foundation/Foundation.h>
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    35
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    36
@implementation M3InstallController
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    37
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    38
- (id) init {
6479
4f08821cbff5 minor cleanup
koda
parents: 3697
diff changeset
    39
        if ((self = [super init])) {
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    40
        NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    41
        NSString *title = [NSString stringWithFormat:NSLocalizedString(@"%@ is currently running from a disk image", @"AppName is currently running from a disk image"), appName];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    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];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    43
        alert = [[NSAlert alertWithMessageText:title
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    44
                                 defaultButton:NSLocalizedString(@"Install", @"Install")
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    45
                               alternateButton:NSLocalizedString(@"Don't Install", @"Don't Install")
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    46
                                   otherButton:nil
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    47
                     informativeTextWithFormat:body] retain];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    48
        //[alert setShowsSuppressionButton:YES];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    49
    }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    50
    return self;
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    51
}
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    52
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    53
- (void)displayInstaller {
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    54
    NSString *imageFilePath = [[[NSWorkspace sharedWorkspace] propertiesForPath:[[NSBundle mainBundle] bundlePath]] objectForKey:NSWorkspace_RBimagefilepath];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    55
    if (imageFilePath && ![imageFilePath isEqualToString:[NSString stringWithFormat:@"/Users/.%@/%@.sparseimage", NSUserName(), NSUserName()]] && ![[NSUserDefaults standardUserDefaults] boolForKey:@"M3DontAskInstallAgain"]) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    56
        NSInteger returnValue = [alert runModal];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    57
        if (returnValue == NSAlertDefaultReturn) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    58
            [self installApp];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    59
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    60
        if ([[alert suppressionButton] state] == NSOnState) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    61
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"M3DontAskInstallAgain"];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    62
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    63
    }
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    64
}
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    65
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    66
- (void)installApp {
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    67
    NSString *appsPath = [[NSString stringWithString:@"/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    68
    NSString *userAppsPath = [[[NSString stringWithString:@"~/Applications"] stringByAppendingPathComponent:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] stringByExpandingTildeInPath];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    69
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2606
diff changeset
    70
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    71
    //Delete the app that is installed
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    72
    if ([[NSFileManager defaultManager] fileExistsAtPath:appsPath]) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    73
        [[NSFileManager defaultManager] removeFileAtPath:appsPath handler:nil];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    74
    }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    75
    //Delete the app that is installed
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    76
    if ([[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath] toPath:appsPath
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    77
                                          handler:nil]) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    78
        NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"App Name installed successfully"), appName],
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    79
                        [NSString stringWithFormat:NSLocalizedString(@"%@ was installed in /Applications", @"App Name was installed in /Applications"), appName],
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    80
                        NSLocalizedString(@"Quit", @"Quit"), nil, nil);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    81
    } else {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    82
        if ([[NSFileManager defaultManager] fileExistsAtPath:userAppsPath]) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    83
            [[NSFileManager defaultManager] removeFileAtPath:userAppsPath handler:nil];
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    84
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    85
        if ([[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath] toPath:userAppsPath
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    86
                                                handler:nil]) {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    87
        NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"AppName installed successfully"), appName],
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    88
                [NSString stringWithFormat:NSLocalizedString(@"%@ was installed in %@", @"App Name was installed in %@"), appName, [[NSString stringWithString:@"~/Applications"] stringByExpandingTildeInPath]],
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    89
                        NSLocalizedString(@"Quit", @"Quit"), nil, nil);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    90
        } else {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    91
            NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"Could not install %@", @"Could not install App Name"), appName],
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    92
                            NSLocalizedString(@"An error occurred when installing", @"An error occurred when installing"), NSLocalizedString(@"Quit", @"Quit"), nil, nil);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    93
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    94
    }
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    95
}
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    96
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
    97
@end