1 /*****************************************************************************
2 * update.m: MacOS X Check-For-Update window
3 *****************************************************************************
4 * Copyright (C) 2005-2006 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 *****************************************************************************/
38 /*****************************************************************************
39 * VLCExtended 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 return _o_sharedInstance;
67 /* clean the interface */
68 [o_fld_releaseNote setString: @""];
76 [o_urlOfBinary release];
83 /* translate strings to the user's language */
84 [o_update_window setTitle: _NS("Check for Updates")];
85 [o_btn_DownloadNow setTitle: _NS("Download now")];
86 [o_btn_okay setTitle: _NS("OK")];
89 - (void)showUpdateWindow
91 /* show the window and check for a potential update */
92 [o_fld_status setStringValue: _NS("Checking for Updates...")];
93 [o_fld_currentVersionAndSize setStringValue: @""];
94 [o_fld_releaseNote setString: @""];
96 [o_update_window center];
97 [o_update_window displayIfNeeded];
98 [o_update_window makeKeyAndOrderFront:nil];
100 [o_bar_checking startAnimation: self];
101 [self checkForUpdate];
102 [o_bar_checking stopAnimation: self];
105 - (IBAction)download:(id)sender
107 /* provide a save dialogue */
108 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
109 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
111 [saveFilePanel setRequiredFileType: @"dmg"];
112 [saveFilePanel setCanSelectHiddenExtension: YES];
113 [saveFilePanel setCanCreateDirectories: YES];
114 [saveFilePanel beginSheetForDirectory:nil file: \
115 [[o_urlOfBinary componentsSeparatedByString:@"/"] lastObject] \
116 modalForWindow: o_update_window modalDelegate:self didEndSelector:sel \
120 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
121 (int)returnCode contextInfo: (void *)contextInfo
123 if (returnCode == NSOKButton)
125 /* perform download and pass the selected path */
126 [self performDownload: [sheet filename]];
131 - (IBAction)okay:(id)sender
133 /* just close the window */
134 [o_update_window close];
137 - (void)checkForUpdate
139 p_u = update_New( p_intf );
140 update_Check( p_u, VLC_FALSE );
141 update_iterator_t *p_uit = update_iterator_New( p_u );
142 BOOL releaseChecked = NO;
143 BOOL gettingReleaseNote = NO;
145 NSString * pathToReleaseNote;
146 pathToReleaseNote = [NSString stringWithFormat: \
147 @"/tmp/vlc_releasenote_%d.tmp", mdate()];
151 p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
152 p_uit->i_t = UPDATE_FILE_TYPE_ALL;
153 update_iterator_Action( p_uit, UPDATE_MIRROR );
155 while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
157 msg_Dbg( p_intf, "parsing available updates, run %i", x );
158 /* if the announced item is of the type "binary", keep it and display
159 * its details to the user. Do similar stuff on "info". Do both
160 * only if the file is announced as stable */
161 if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
163 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
165 msg_Dbg( p_intf, "release note found, desc = %s",
166 p_uit->file.psz_description );
167 [o_fld_releaseNote setString: \
168 [NSString stringWithUTF8String: \
169 (p_uit->file.psz_description)]];
170 /* download our release note
171 * We will read the temp file after this loop */
172 update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
173 gettingReleaseNote = YES;
175 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
177 msg_Dbg( p_intf, "binary found, version = %s, " \
178 "url=%s, size=%i MB", p_uit->release.psz_version, \
179 p_uit->file.psz_url, \
180 (int)((p_uit->file.l_size / 1024) / 1024) );
181 [o_fld_currentVersionAndSize setStringValue: [NSString \
183 _NS("The latest VLC media player release " \
184 "is %s (%i MB to download)."), \
185 p_uit->release.psz_version, ((p_uit->file.l_size \
189 [o_urlOfBinary release];
190 o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
191 p_uit->file.psz_url];
193 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
196 /* our version is outdated, let the user download the new
198 [o_fld_status setStringValue: _NS("This version of VLC " \
200 [o_btn_DownloadNow setEnabled: YES];
201 msg_Dbg( p_intf, "this version of VLC is outdated" );
202 /* put the mirror information */
203 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
204 p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
205 p_uit->mirror.psz_type );
206 /* make sure that we perform this check only once */
207 releaseChecked = YES;
209 else if(! releaseChecked )
211 [o_fld_status setStringValue: _NS("This version of VLC " \
212 "is latest available.")];
213 [o_btn_DownloadNow setEnabled: NO];
214 msg_Dbg( p_intf, "current version is up-to-date" );
215 releaseChecked = YES;
221 update_iterator_Delete( p_uit );
223 /* wait for our release notes if necessary, since the download is done
224 * by another thread -- this does usually take 300000 to 500000 ms */
225 if( gettingReleaseNote )
228 while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
234 /* if this takes more than 15 sec, exit */
235 msg_Warn( p_intf, "download took more than 15 sec, exiting" );
239 msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
242 /* let's open our cached release note and display it
243 * we can't use NSString stringWithContentsOfFile:encoding:error:
244 * since it is Tiger only */
245 NSString * releaseNote = [[NSString alloc] initWithData: \
246 [NSData dataWithContentsOfFile: pathToReleaseNote] \
247 encoding: NSISOLatin1StringEncoding];
249 [o_fld_releaseNote setString: releaseNote];
251 /* delete the file since it isn't needed anymore */
253 myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
254 pathToReleaseNote handler: nil];
258 /* don't confuse the user, but make her happy */
259 [o_fld_status setStringValue: _NS("This version of VLC " \
260 "is latest available.")];
261 [o_btn_DownloadNow setEnabled: NO];
262 msg_Dbg( p_intf, "current version is up-to-date" );
263 msg_Warn( p_intf, "retrieving current release notes failed!" );
268 - (void)performDownload:(NSString *)path
270 update_iterator_t *p_uit = update_iterator_New( p_u );
273 update_iterator_Action( p_uit, UPDATE_MIRROR );
275 while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
277 if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
278 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
279 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
281 /* put the mirror information */
282 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
283 p_uit->mirror.psz_name, p_uit->mirror.psz_location, \
284 p_uit->mirror.psz_type );
286 /* that's our binary */
287 update_download( p_uit, (char *)[path UTF8String] );
291 update_iterator_Delete( p_uit );
294 [o_update_window close];