1#import "MyAppDelegate.h"
2#import "PreferencesWindowController.h"
3#import <Carbon/Carbon.h>
4#import <ApplicationServices/ApplicationServices.h>
5
6@implementation MyAppDelegate
7
8- (id)init
9{
10    self = [super init];
11    initial_action_done = NO;
12    should_terminate = NO;
13    return self;
14}
15
16- (IBAction)showPreferences:(id)sender
17{
18    [PreferencesWindowController getPreferencesWindow];
19}
20
21- (void)applicationDidFinishLaunching:(NSNotification *)notification
22{
23    // Test that the file mappings are correct
24    [self testFileTypeBinding];
25    // If we were opened because of a file drag or doubleclick
26    // we've set initial_action_done in shouldShowUI
27    // Otherwise we open a preferences dialog.
28    if (!initial_action_done) {
29        initial_action_done = YES;
30        [self showPreferences: self];
31    }
32}
33
34- (BOOL)shouldShowUI
35{
36    // if this call comes before applicationDidFinishLaunching: we
37    // should terminate immediately after starting the script.
38    if (!initial_action_done)
39        should_terminate = YES;
40    initial_action_done = YES;
41    if( GetCurrentKeyModifiers() & optionKey )
42        return YES;
43    return NO;
44}
45
46- (BOOL)shouldTerminate
47{
48    return should_terminate;
49}
50
51- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
52{
53    return NO;
54}
55
56- (void)testFileTypeBinding
57{
58    NSURL *ourUrl;
59    OSStatus err;
60    FSRef appRef;
61    NSURL *appUrl;
62    static NSString *extensions[] = { @"py", @"pyw", @"pyc", NULL};
63    NSString **ext_p;
64    int i;
65
66    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SkipFileBindingTest"])
67        return;
68    ourUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
69    for( ext_p = extensions; *ext_p; ext_p++ ) {
70        err = LSGetApplicationForInfo(
71            kLSUnknownType,
72            kLSUnknownCreator,
73            (CFStringRef)*ext_p,
74            kLSRolesViewer,
75            &appRef,
76            (CFURLRef *)&appUrl);
77        if (err || ![appUrl isEqual: ourUrl] ) {
78            i = NSRunAlertPanel(@"File type binding",
79                @"PythonLauncher is not the default application for all " \
80                  @"Python script types. You should fix this with the " \
81                  @"Finder's \"Get Info\" command.\n\n" \
82                  @"See \"Changing the application that opens a file\" in " \
83                  @"Mac Help for details.",
84                @"OK",
85                @"Don't show this warning again",
86                NULL);
87            if ( i == 0 ) { // Don't show again
88                [[NSUserDefaults standardUserDefaults]
89                    setObject:@"YES" forKey:@"SkipFileBindingTest"];
90            }
91            return;
92        }
93    }
94}
95
96@end
97