1 /*****************************************************************************
2 * update.m: MacOS X Check-For-Update window
3 *****************************************************************************
4 * Copyright (C) 2005-2007 the VideoLAN team
7 * Authors: Felix K
\9fhne <fkuehne@users.sf.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
25 /*****************************************************************************
26 * Note: the code used to communicate with VLC's core was inspired by
27 * ../wxwidgets/dialogs/updatevlc.cpp, written by Antoine Cellerier.
28 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
37 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
38 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
40 /*****************************************************************************
41 * VLCExtended implementation
42 *****************************************************************************/
44 @implementation VLCUpdate
46 static VLCUpdate *_o_sharedInstance = nil;
48 + (VLCUpdate *)sharedInstance
50 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
55 if( _o_sharedInstance ) {
58 _o_sharedInstance = [super init];
61 return _o_sharedInstance;
69 /* clean the interface */
70 [o_fld_releaseNote setString: @""];
78 [o_urlOfBinary release];
85 /* translate strings to the user's language */
86 [o_update_window setTitle: _NS("Check for Updates")];
87 [o_btn_DownloadNow setTitle: _NS("Download now")];
88 [o_btn_okay setTitle: _NS("OK")];
89 [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
90 /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want the Alert
91 * panel to pop up at this time */
92 [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
95 - (void)setShouldCheckUpdate: (BOOL)check
97 [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
98 [o_chk_updateOnStartup setState: check];
101 - (BOOL)shouldCheckForUpdate
103 NSDate *o_last_update;
104 NSDate *o_next_update;
106 if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
108 /* We don't have any preferences stored, ask the user. */
109 int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
110 _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
111 [self setShouldCheckUpdate: res];
114 if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
117 o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
121 o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
125 return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
128 - (void)showUpdateWindow
130 /* show the window and check for a potential update */
131 [o_fld_status setStringValue: _NS("Checking for Updates...")];
132 [o_fld_currentVersionAndSize setStringValue: @""];
133 [o_fld_releaseNote setString: @""];
135 [o_update_window center];
136 [o_update_window displayIfNeeded];
137 [o_update_window makeKeyAndOrderFront:nil];
139 [o_bar_checking startAnimation: self];
140 [self checkForUpdate];
141 [o_bar_checking stopAnimation: self];
144 - (IBAction)download:(id)sender
146 /* provide a save dialogue */
147 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
148 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
150 [saveFilePanel setRequiredFileType: @"dmg"];
151 [saveFilePanel setCanSelectHiddenExtension: YES];
152 [saveFilePanel setCanCreateDirectories: YES];
153 [saveFilePanel beginSheetForDirectory:nil file: \
154 [[o_urlOfBinary componentsSeparatedByString:@"/"] lastObject] \
155 modalForWindow: o_update_window modalDelegate:self didEndSelector:sel \
159 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
160 (int)returnCode contextInfo: (void *)contextInfo
162 if( returnCode == NSOKButton )
164 /* perform download and pass the selected path */
165 [self performDownload: [sheet filename]];
170 - (IBAction)okay:(id)sender
172 /* just close the window */
173 [o_update_window close];
176 - (IBAction)changeCheckUpdateOnStartup:(id)sender
178 [self setShouldCheckUpdate: [sender state]];
181 - (void)checkForUpdate
183 /* We may not run on first thread */
184 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
185 p_u = update_New( p_intf );
186 update_Check( p_u, VLC_FALSE );
187 update_iterator_t *p_uit = update_iterator_New( p_u );
188 BOOL releaseChecked = NO;
189 BOOL gettingReleaseNote = NO;
191 NSString * pathToReleaseNote;
192 pathToReleaseNote = [NSString stringWithFormat: \
193 @"/tmp/vlc_releasenote_%d.tmp", mdate()];
195 [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
199 p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
200 p_uit->i_t = UPDATE_FILE_TYPE_ALL;
201 update_iterator_Action( p_uit, UPDATE_MIRROR );
203 while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
205 msg_Dbg( p_intf, "parsing available updates, run %i", x );
206 /* if the announced item is of the type "binary", keep it and display
207 * its details to the user. Do similar stuff on "info". Do both
208 * only if the file is announced as stable */
209 if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
211 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
213 msg_Dbg( p_intf, "release note found, desc = %s",
214 p_uit->file.psz_description );
215 [o_fld_releaseNote setString: \
216 [NSString stringWithUTF8String: \
217 (p_uit->file.psz_description)]];
218 /* download our release note
219 * We will read the temp file after this loop */
220 update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
221 gettingReleaseNote = YES;
223 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
225 msg_Dbg( p_intf, "binary found, version = %s, " \
226 "url=%s, size=%i MB", p_uit->release.psz_version, \
227 p_uit->file.psz_url, \
228 (int)((p_uit->file.l_size / 1024) / 1024) );
229 [o_fld_currentVersionAndSize setStringValue: [NSString \
231 _NS("The latest VLC media player release " \
232 "is %s (%i MB to download)."), \
233 p_uit->release.psz_version, ((p_uit->file.l_size \
237 [o_urlOfBinary release];
238 o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
239 p_uit->file.psz_url];
241 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
244 /* our version is outdated, let the user download the new
246 [o_fld_status setStringValue: _NS("This version of VLC " \
248 [o_btn_DownloadNow setEnabled: YES];
249 msg_Dbg( p_intf, "this version of VLC is outdated" );
250 /* put the mirror information */
251 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
252 p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
253 p_uit->mirror.psz_type );
254 /* make sure that we perform this check only once */
255 releaseChecked = YES;
256 /* Make sure the update window is showed in case we have something */
257 [o_update_window center];
258 [o_update_window displayIfNeeded];
259 [o_update_window makeKeyAndOrderFront: self];
262 else if(! releaseChecked )
264 [o_fld_status setStringValue: _NS("This version of VLC " \
265 "is the latest available.")];
266 [o_btn_DownloadNow setEnabled: NO];
267 msg_Dbg( p_intf, "current version is up-to-date" );
268 releaseChecked = YES;
274 update_iterator_Delete( p_uit );
276 /* wait for our release notes if necessary, since the download is done
277 * by another thread -- this does usually take 300000 to 500000 ms */
278 if( gettingReleaseNote )
281 while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
287 /* if this takes more than 15 sec, exit */
288 msg_Warn( p_intf, "download took more than 15 sec, exiting" );
292 msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
295 /* let's open our cached release note and display it
296 * we can't use NSString stringWithContentsOfFile:encoding:error:
297 * since it is Tiger only */
298 NSString * releaseNote = [[NSString alloc] initWithData: \
299 [NSData dataWithContentsOfFile: pathToReleaseNote] \
300 encoding: NSISOLatin1StringEncoding];
302 [o_fld_releaseNote setString: releaseNote];
304 /* delete the file since it isn't needed anymore */
306 myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
307 pathToReleaseNote handler: nil];
311 /* don't confuse the user, but make her happy */
312 [o_fld_status setStringValue: _NS("This version of VLC " \
313 "is the latest available.")];
314 [o_btn_DownloadNow setEnabled: NO];
315 msg_Dbg( p_intf, "current version is up-to-date" );
321 - (void)performDownload:(NSString *)path
323 update_iterator_t *p_uit = update_iterator_New( p_u );
326 update_iterator_Action( p_uit, UPDATE_MIRROR );
328 while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
330 if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
331 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
332 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
334 /* put the mirror information */
335 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
336 p_uit->mirror.psz_name, p_uit->mirror.psz_location, \
337 p_uit->mirror.psz_type );
339 /* that's our binary */
340 update_download( p_uit, (char *)[path UTF8String] );
344 update_iterator_Delete( p_uit );
347 [o_update_window close];