QTfrontend/util/platform/M3InstallController.m
author sheepluva
Sun, 07 Apr 2013 17:35:57 +0200
branch0.9.19
changeset 8864 a79869951d08
parent 8449 2816230a107e
child 8931 ca678bee1a41
permissions -rw-r--r--
small engine translation updates. Thanks to community members 'Wuzzy' and 'Flandre Scarlet' for their suggestions :)
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]) {
8449
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    73
        if ([NSFileManager instancesRespondToSelector:@selector(removeItemAtPath:error:)])
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    74
            [[NSFileManager defaultManager] removeItemAtPath:appsPath error:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    75
        else
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    76
            //casting hides the deprecation warning
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    77
            [(id)[NSFileManager defaultManager] removeFileAtPath:appsPath handler:nil];
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    78
    }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    79
    //Delete the app that is installed
8449
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    80
    BOOL success = NO;
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    81
    if ([NSFileManager instancesRespondToSelector:@selector(copyItemAtPath:toPath:error:)])
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    82
        success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] bundlePath]
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    83
                                                          toPath:appsPath
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    84
                                                           error:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    85
    else
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    86
        success = [(id)[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath]
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    87
                                                        toPath:appsPath
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    88
                                                       handler:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    89
    if (success) {
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    90
        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
    91
                        [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
    92
                        NSLocalizedString(@"Quit", @"Quit"), nil, nil);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    93
    } else {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    94
        if ([[NSFileManager defaultManager] fileExistsAtPath:userAppsPath]) {
8449
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    95
            if ([NSFileManager instancesRespondToSelector:@selector(removeItemAtPath:error:)])
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    96
                [[NSFileManager defaultManager] removeItemAtPath:userAppsPath error:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    97
            else
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
    98
                [(id)[NSFileManager defaultManager] removeFileAtPath:userAppsPath handler:nil];
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    99
        }
8449
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   100
        if ([NSFileManager instancesRespondToSelector:@selector(copyItemAtPath:toPath:error:)])
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   101
            success = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] bundlePath]
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   102
                                                              toPath:userAppsPath
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   103
                                                               error:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   104
        else
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   105
            success = [(id)[NSFileManager defaultManager] copyPath:[[NSBundle mainBundle] bundlePath]
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   106
                                                            toPath:userAppsPath
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   107
                                                           handler:nil];
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   108
        if (success) {
2816230a107e shut a few frontend warnings out
koda
parents: 8440
diff changeset
   109
            NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"%@ installed successfully", @"AppName installed successfully"), appName],
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   110
                [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
   111
                        NSLocalizedString(@"Quit", @"Quit"), nil, nil);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   112
        } else {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   113
            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
   114
                            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
   115
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   116
    }
2400
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
   117
}
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
   118
2422ea85d100 added a utility that warns the user to install hedgewars when it's run from the diskimage
koda
parents:
diff changeset
   119
@end