1 /*****************************************************************************
2 * update.m: MacOS X Check-For-Update window
3 *****************************************************************************
4 * Copyright © 2005-2008 the VideoLAN team
7 * Authors: Felix Kühne <fkuehne@users.sf.net>
8 * Rafaël Carré <funman@videolanorg>
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.
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.
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 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
35 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
36 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
38 /*****************************************************************************
39 * VLCUpdate implementation
40 *****************************************************************************/
42 @implementation VLCUpdate
44 static VLCUpdate *_o_sharedInstance = nil;
46 + (VLCUpdate *)sharedInstance
48 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
53 if( _o_sharedInstance ) {
56 _o_sharedInstance = [super init];
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")];
69 return _o_sharedInstance;
74 if( p_u ) update_Delete( p_u );
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]];
84 - (void)setShouldCheckUpdate: (BOOL)check
86 [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
87 [o_chk_updateOnStartup setState: check];
89 /* make sure we got this set, even if we crash later on */
90 [[NSUserDefaults standardUserDefaults] synchronize];
93 - (BOOL)shouldCheckForUpdate
95 NSDate *o_last_update;
96 NSDate *o_next_update;
98 if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
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];
106 if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
109 o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
113 o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*7 /* every seven days */ sinceDate: o_last_update] autorelease];
117 return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
120 - (void)showUpdateWindow
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];
129 [o_bar_checking startAnimation: self];
130 [self checkForUpdate];
132 [o_bar_checking stopAnimation: self];
136 - (IBAction)download:(id)sender
138 /* provide a save dialogue */
139 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
140 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
142 [saveFilePanel setRequiredFileType: @"dmg"];
143 [saveFilePanel setCanSelectHiddenExtension: YES];
144 [saveFilePanel setCanCreateDirectories: YES];
145 update_release_t *p_release = update_GetRelease( p_u );
147 [saveFilePanel beginSheetForDirectory:@"~/Downloads" file:
148 [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
149 modalForWindow: o_update_window
155 - (void)getLocationForSaving: (NSSavePanel *)sheet
156 returnCode: (int)returnCode
157 contextInfo: (void *)contextInfo
159 if( returnCode == NSOKButton )
161 /* perform download and pass the selected path */
162 [NSThread detachNewThreadSelector:@selector(performDownload:) toTarget:self withObject:[sheet filename]];
167 - (IBAction)okay:(id)sender
169 /* just hides the window */
170 [o_update_window orderOut: self];
173 - (IBAction)changeCheckUpdateOnStartup:(id)sender
175 [self setShouldCheckUpdate: [sender state]];
178 - (void)setUpToDate:(NSNumber *)uptodate
180 if( [uptodate boolValue] )
182 [o_fld_releaseNote setString: @""];
183 [o_fld_currentVersion setStringValue: @""];
184 [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
185 [o_btn_DownloadNow setEnabled: NO];
189 update_release_t *p_release = update_GetRelease( p_u );
190 [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_release->psz_desc ? p_release->psz_desc : "" )]];
191 [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
192 [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
193 _NS("The current release is %d.%d.%d%c."), p_release->i_major,
194 p_release->i_minor, p_release->i_revision, p_release->extra]];
195 [o_btn_DownloadNow setEnabled: YES];
196 /* Make sure the update window is showed in case we have something */
197 [o_update_window center];
198 [o_update_window displayIfNeeded];
199 [o_update_window makeKeyAndOrderFront: self];
203 static void updateCallback( void * p_data, bool b_success )
205 VLCUpdate * update = p_data;
206 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
207 NSNumber * state = [NSNumber numberWithBool:!b_success || !update_NeedUpgrade( update->p_u )];
208 [update performSelectorOnMainThread:@selector(setUpToDate:) withObject:state waitUntilDone:YES];
212 - (void)checkForUpdate
214 p_u = update_New( VLCIntf );
217 update_Check( p_u, updateCallback, self );
219 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
220 [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
224 - (void)performDownload:(NSString *)path
226 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
227 update_Download( p_u, [path UTF8String] );
228 [o_btn_DownloadNow setEnabled: NO];
229 [o_update_window orderOut: self];
230 update_WaitDownload( p_u );
231 update_Delete( p_u );