~andymatuschak/sparkle/main

« back to all changes in this revision

Viewing changes to SUUpdateAlert.m

  • Committer: andym
  • Date: 2007-07-10 04:55:25 UTC
  • Revision ID: andym@localhost-20070710045525-3awjris3of5zx0s0
Holy restructuring, batman! Watch out for falling folders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  SUUpdateAlert.m
 
3
//  Sparkle
 
4
//
 
5
//  Created by Andy Matuschak on 3/12/06.
 
6
//  Copyright 2006 Andy Matuschak. All rights reserved.
 
7
//
 
8
 
 
9
#import "SUUpdateAlert.h"
 
10
#import "SUAppcastItem.h"
 
11
#import "SUUtilities.h"
 
12
#import <WebKit/WebKit.h>
 
13
 
 
14
@implementation SUUpdateAlert
 
15
 
 
16
- initWithAppcastItem:(SUAppcastItem *)item
 
17
{
 
18
        NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"SUUpdateAlert" ofType:@"nib"];
 
19
        if (!path) // slight hack to resolve issues with running with in configurations
 
20
        {
 
21
                NSBundle *current = [NSBundle bundleForClass:[self class]];
 
22
                NSString *frameworkPath = [[[NSBundle mainBundle] sharedFrameworksPath] stringByAppendingFormat:@"/Sparkle.framework", [current bundleIdentifier]];
 
23
                NSBundle *framework = [NSBundle bundleWithPath:frameworkPath];
 
24
                path = [framework pathForResource:@"SUUpdateAlert" ofType:@"nib"];
 
25
        }
 
26
        
 
27
        [super initWithWindowNibPath:path owner:self];
 
28
        
 
29
        updateItem = [item retain];
 
30
        [self setShouldCascadeWindows:NO];
 
31
        
 
32
        return self;
 
33
}
 
34
 
 
35
- (void)dealloc
 
36
{
 
37
        [updateItem release];
 
38
        [super dealloc];
 
39
}
 
40
 
 
41
- (void)endWithSelection:(SUUpdateAlertChoice)choice
 
42
{
 
43
        [releaseNotesView stopLoading:self];
 
44
        [releaseNotesView setFrameLoadDelegate:nil];
 
45
        [releaseNotesView setPolicyDelegate:nil];
 
46
        [self close];
 
47
        if ([delegate respondsToSelector:@selector(updateAlert:finishedWithChoice:)])
 
48
                [delegate updateAlert:self finishedWithChoice:choice];
 
49
}
 
50
 
 
51
- (IBAction)installUpdate:sender
 
52
{
 
53
        [self endWithSelection:SUInstallUpdateChoice];
 
54
}
 
55
 
 
56
- (IBAction)skipThisVersion:sender
 
57
{
 
58
        [self endWithSelection:SUSkipThisVersionChoice];
 
59
}
 
60
 
 
61
- (IBAction)remindMeLater:sender
 
62
{
 
63
        [self endWithSelection:SURemindMeLaterChoice];
 
64
}
 
65
 
 
66
- (void)displayReleaseNotes
 
67
{
 
68
        [releaseNotesView setFrameLoadDelegate:self];
 
69
        [releaseNotesView setPolicyDelegate:self];
 
70
        
 
71
        // Stick a nice big spinner in the middle of the web view until the page is loaded.
 
72
        NSRect frame = [[releaseNotesView superview] frame];
 
73
        releaseNotesSpinner = [[[NSProgressIndicator alloc] initWithFrame:NSMakeRect(NSMidX(frame)-16, NSMidY(frame)-16, 32, 32)] autorelease];
 
74
        [releaseNotesSpinner setStyle:NSProgressIndicatorSpinningStyle];
 
75
        [releaseNotesSpinner startAnimation:self];
 
76
        webViewFinishedLoading = NO;
 
77
        [[releaseNotesView superview] addSubview:releaseNotesSpinner];
 
78
        
 
79
        // If there's a release notes URL, load it; otherwise, just stick the contents of the description into the web view.
 
80
        if ([updateItem releaseNotesURL])
 
81
        {
 
82
                [[releaseNotesView mainFrame] loadRequest:[NSURLRequest requestWithURL:[updateItem releaseNotesURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]];
 
83
        }
 
84
        else
 
85
        {
 
86
                [[releaseNotesView mainFrame] loadHTMLString:[updateItem description] baseURL:nil];
 
87
        }       
 
88
}
 
89
 
 
90
- (BOOL)showsReleaseNotes
 
91
{
 
92
        if (!SUInfoValueForKey(SUShowReleaseNotesKey)) { return YES; } // defaults to YES
 
93
        return [SUInfoValueForKey(SUShowReleaseNotesKey) boolValue];
 
94
}
 
95
 
 
96
- (BOOL)allowsAutomaticUpdates
 
97
{
 
98
        if (!SUInfoValueForKey(SUExpectsDSASignatureKey)) { return NO; } // automatic updating requires DSA-signed updates
 
99
        if (!SUInfoValueForKey(SUAllowsAutomaticUpdatesKey)) { return YES; } // defaults to YES
 
100
        return [SUInfoValueForKey(SUAllowsAutomaticUpdatesKey) boolValue];
 
101
}
 
102
 
 
103
- (void)awakeFromNib
 
104
{       
 
105
        [[self window] setLevel:NSFloatingWindowLevel];
 
106
        [[self window] setFrameAutosaveName:@"SUUpdateAlertFrame"];
 
107
                
 
108
        // We're gonna do some frame magic to match the window's size to the description field and the presence of the release notes view.
 
109
        NSRect frame = [[self window] frame];
 
110
        
 
111
        if (![self showsReleaseNotes])
 
112
        {
 
113
                // Resize the window to be appropriate for not having a huge release notes view.
 
114
                frame.size.height -= [releaseNotesView frame].size.height;
 
115
                // No resizing!
 
116
                [[self window] setShowsResizeIndicator:NO];
 
117
                [[self window] setMinSize:frame.size];
 
118
                [[self window] setMaxSize:frame.size];
 
119
        }
 
120
        
 
121
        if (![self allowsAutomaticUpdates])
 
122
        {
 
123
                NSRect boxFrame = [[[releaseNotesView superview] superview] frame];
 
124
                boxFrame.origin.y -= 20;
 
125
                boxFrame.size.height += 20;
 
126
                [[[releaseNotesView superview] superview] setFrame:boxFrame];
 
127
        }
 
128
        
 
129
        [[self window] setFrame:frame display:NO];
 
130
        [[self window] center];
 
131
        
 
132
        if ([self showsReleaseNotes])
 
133
        {
 
134
                [self displayReleaseNotes];
 
135
        }
 
136
}
 
137
 
 
138
- (BOOL)windowShouldClose:note
 
139
{
 
140
        [self endWithSelection:SURemindMeLaterChoice];
 
141
        return YES;
 
142
}
 
143
 
 
144
- (NSImage *)applicationIcon
 
145
{
 
146
        return [NSImage imageNamed:@"NSApplicationIcon"];
 
147
}
 
148
 
 
149
- (NSString *)titleText
 
150
{
 
151
        return [NSString stringWithFormat:SULocalizedString(@"A new version of %@ is available!", nil), SUHostAppDisplayName()];
 
152
}
 
153
 
 
154
- (NSString *)descriptionText
 
155
{
 
156
        return [NSString stringWithFormat:SULocalizedString(@"%@ %@ is now available (you have %@). Would you like to download it now?", nil), SUHostAppDisplayName(), [updateItem versionString], SUHostAppVersionString()];   
 
157
}
 
158
 
 
159
- (void)webView:(WebView *)sender didFinishLoadForFrame:frame
 
160
{
 
161
    if ([frame parentFrame] == nil) {
 
162
        webViewFinishedLoading = YES;
 
163
                [releaseNotesSpinner setHidden:YES];
 
164
                [sender display]; // necessary to prevent weird scroll bar artifacting
 
165
    }
 
166
}
 
167
 
 
168
- (void)webView:sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:frame decisionListener:listener
 
169
{
 
170
    if (webViewFinishedLoading == YES) {
 
171
        [[NSWorkspace sharedWorkspace] openURL:[request URL]];
 
172
                
 
173
        [listener ignore];
 
174
    }    
 
175
    else {
 
176
        [listener use];
 
177
    }
 
178
}
 
179
 
 
180
- (void)setDelegate:del
 
181
{
 
182
        delegate = del;
 
183
}
 
184
 
 
185
@end