]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
3d296d2ec4453c2fbabed2ce9c091363f6f81749
[vlc] / modules / gui / macosx / update.m
1 /*****************************************************************************
2  * update.m: MacOS X Check-For-Update window
3  *****************************************************************************
4  * Copyright © 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Kühne <fkuehne@users.sf.net>
8  *          Rafaël Carré <funman@videolanorg>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #import "update.h"
26
27 #ifdef UPDATE_CHECK
28
29 #include <assert.h>
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
36 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
37
38 /*****************************************************************************
39  * VLCUpdate implementation
40  *****************************************************************************/
41
42 @implementation VLCUpdate
43
44 static VLCUpdate *_o_sharedInstance = nil;
45
46 + (VLCUpdate *)sharedInstance
47 {
48     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
49 }
50
51 - (id)init
52 {
53     if( _o_sharedInstance ) {
54         [self dealloc];
55     } else {
56         _o_sharedInstance = [super init];
57         b_checked = false;
58
59         /* clean the interface */
60         [o_fld_releaseNote setString: @""];
61         [o_fld_currentVersion setString: @""];
62         /* translate strings to the user's language */
63         [o_update_window setTitle: _NS("Check for Updates")];
64         [o_btn_DownloadNow setTitle: _NS("Download now")];
65         [o_btn_okay setTitle: _NS("OK")];
66         [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
67     }
68
69     return _o_sharedInstance;
70 }
71
72 - (void)end
73 {
74     if( p_u ) update_Delete( p_u );
75 }
76
77 - (void)awakeFromNib
78 {
79     /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
80      * the Alert panel to pop up at this time */
81     [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
82 }
83
84 - (void)setShouldCheckUpdate: (BOOL)check
85 {
86     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
87     [o_chk_updateOnStartup setState: check];
88
89     /* make sure we got this set, even if we crash later on */
90     [[NSUserDefaults standardUserDefaults] synchronize];
91 }
92
93 - (BOOL)shouldCheckForUpdate
94 {
95     NSDate *o_last_update;
96     NSDate *o_next_update;
97  
98     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
99     {
100         /* We don't have any preferences stored, ask the user. */
101         int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
102               _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
103         [self setShouldCheckUpdate: res];
104     }
105
106     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
107         return NO;
108
109     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
110     if( !o_last_update )
111         return YES;
112
113     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*7 /* every seven days */ sinceDate: o_last_update] autorelease];
114     if( !o_next_update )
115         return YES;
116
117     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
118 }
119
120 - (void)showUpdateWindow
121 {
122     /* show the window and check for a potential update */
123     [o_update_window center];
124     [o_update_window displayIfNeeded];
125     [o_update_window makeKeyAndOrderFront:nil];
126
127     if( !b_checked )
128     {
129         [o_bar_checking startAnimation: self];
130         [self checkForUpdate];
131         b_checked = true;
132         [o_bar_checking stopAnimation: self];
133     }
134 }
135
136 - (IBAction)download:(id)sender
137 {
138     /* provide a save dialogue */
139     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
140     NSOpenPanel * saveFilePanel = [[NSOpenPanel alloc] init];
141
142     [saveFilePanel setCanChooseFiles: NO];
143     [saveFilePanel setCanChooseDirectories: YES];
144     [saveFilePanel setCanCreateDirectories: YES];
145     [saveFilePanel setPrompt: _NS("Save" )];
146     [saveFilePanel setNameFieldLabel: _NS("Save As:" )];
147     update_release_t *p_release = update_GetRelease( p_u );
148     assert( p_release );
149     [saveFilePanel beginSheetForDirectory:@"~/Downloads" file:
150         [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
151                            modalForWindow: o_update_window 
152                             modalDelegate:self
153                            didEndSelector:sel
154                               contextInfo:nil];
155 }
156
157 - (void)getLocationForSaving: (NSOpenPanel *)sheet 
158                   returnCode: (int)returnCode 
159                  contextInfo: (void *)contextInfo
160 {
161     if( returnCode == NSOKButton )
162     {
163         /* perform download and pass the selected path */
164         [NSThread detachNewThreadSelector:@selector(performDownload:) toTarget:self withObject:[sheet directory]];
165     }
166     [sheet release];
167 }
168
169 - (IBAction)okay:(id)sender
170 {
171     /* just hides the window */
172     [o_update_window orderOut: self];
173 }
174
175 - (IBAction)changeCheckUpdateOnStartup:(id)sender
176 {
177     [self setShouldCheckUpdate: [sender state]];
178 }
179
180 - (void)setUpToDate:(NSNumber *)uptodate
181 {
182     if( [uptodate boolValue] )
183     {
184         [o_fld_releaseNote setString: @""];
185         [o_fld_currentVersion setStringValue: @""];
186         [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
187         [o_btn_DownloadNow setEnabled: NO];
188     }
189     else
190     {
191         update_release_t *p_release = update_GetRelease( p_u );
192         [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_release->psz_desc ? p_release->psz_desc : "" )]];
193         [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
194         [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
195             _NS("The current release is %d.%d.%d%c."), p_release->i_major,
196             p_release->i_minor, p_release->i_revision, p_release->extra]];
197         [o_btn_DownloadNow setEnabled: YES];
198         /* Make sure the update window is showed in case we have something */
199         [o_update_window center];
200         [o_update_window displayIfNeeded];
201         [o_update_window makeKeyAndOrderFront: self];
202     }
203 }
204
205 static void updateCallback( void * p_data, bool b_success )
206 {
207     VLCUpdate * update = p_data;
208     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
209     NSNumber * state = [NSNumber numberWithBool:!b_success || !update_NeedUpgrade( update->p_u )];
210     [update performSelectorOnMainThread:@selector(setUpToDate:) withObject:state waitUntilDone:YES];
211     [pool release];
212 }
213
214 - (void)checkForUpdate
215 {
216     p_u = update_New( VLCIntf );
217     if( !p_u )
218         return;
219     update_Check( p_u, updateCallback, self );
220
221     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
222     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
223     [pool release];
224 }
225
226 - (void)performDownload:(NSString *)path
227 {
228     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
229     update_Download( p_u, [path UTF8String] );
230     [o_btn_DownloadNow setEnabled: NO];
231     [o_update_window orderOut: self];
232     update_WaitDownload( p_u );
233     update_Delete( p_u );
234     p_u = nil;
235     [pool release];
236 }
237
238 @end
239
240 #endif