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: @""];
75 if( o_hashOfOurBinary )
76 [o_hashOfOurBinary release];
78 [o_urlOfBinary release];
85 /* translate strings to the user's language */
86 [o_update_window setTitle: _NS("Check for Update")];
87 [o_btn_DownloadNow setTitle: _NS("Download now")];
88 [o_btn_okay setTitle: _NS("OK")];
91 - (void)showUpdateWindow
93 /* show the window and check for a potential update */
94 [o_fld_status setStringValue: _NS("Checking for Update...")];
95 [o_fld_currentVersionAndSize setStringValue: @""];
96 [o_fld_releaseNote setString: @""];
98 [o_update_window center];
99 [o_update_window displayIfNeeded];
100 [o_update_window makeKeyAndOrderFront:nil];
102 [o_bar_checking startAnimation: self];
103 [self checkForUpdate];
104 [o_bar_checking stopAnimation: self];
107 - (IBAction)download:(id)sender
109 /* enable the following once full notification support is available
110 * provide a save dialogue *
111 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
112 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
114 [saveFilePanel setRequiredFileType: @"dmg"];
115 [saveFilePanel setCanSelectHiddenExtension: YES];
116 [saveFilePanel setCanCreateDirectories: YES];
117 [saveFilePanel beginSheetForDirectory:nil file:nil modalForWindow: \
118 o_update_window modalDelegate:self didEndSelector:sel contextInfo:nil];*/
120 /* delete this afterwards */
121 [self performDownload: @""];
124 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
125 (int)returnCode contextInfo: (void *)contextInfo
127 if (returnCode == NSOKButton)
129 /* perform download and pass the selected path */
130 [self performDownload: [sheet filename]];
135 - (IBAction)okay:(id)sender
137 /* just close the window */
138 [o_update_window close];
141 - (void)checkForUpdate
143 p_u = update_New( p_intf );
144 update_Check( p_u, VLC_FALSE );
145 update_iterator_t *p_uit = update_iterator_New( p_u );
146 BOOL releaseChecked = NO;
148 NSString * pathToReleaseNote;
149 pathToReleaseNote = [[NSString stringWithString: \
150 @"~/Library/Caches/vlc_releasenote_temp.txt"] \
151 stringByExpandingTildeInPath];
155 p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
156 p_uit->i_t = UPDATE_FILE_TYPE_ALL;
157 update_iterator_Action( p_uit, UPDATE_MIRROR );
159 while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
161 /* if the announced item is of the type "binary", keep it and display
162 * its details to the user. Do similar stuff on "info". Do both
163 * only if the file is announced as stable */
164 if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
166 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
168 [o_fld_releaseNote setString: \
169 [NSString stringWithUTF8String: \
170 (p_uit->file.psz_description)]];
171 /* download our release note
172 * We will read the temp file after this loop */
173 update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
175 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
177 msg_Dbg( p_intf, "binary found, version = %s" \
178 ", hash=%s, size=%i", p_uit->release.psz_version, \
179 p_uit->file.psz_md5, (int)((p_uit->file.l_size \
181 [o_fld_currentVersionAndSize setStringValue: \
182 [NSString stringWithFormat: \
183 @"The current release is %s (%i MB to download).", \
184 p_uit->release.psz_version, ((p_uit->file.l_size \
188 [o_urlOfBinary release];
189 o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
190 p_uit->file.psz_url];
192 /* save the hash of our file, if available */
193 if( p_uit->file.psz_md5 )
195 if( o_hashOfOurBinary )
196 [o_hashOfOurBinary release];
197 o_hashOfOurBinary = [[NSString alloc] \
198 initWithUTF8String: p_uit->file.psz_md5];
201 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
204 /* our version is outdated, let the user download the new
206 [o_fld_status setStringValue: _NS("Your version of VLC " \
208 [o_btn_DownloadNow setEnabled: YES];
209 msg_Dbg( p_intf, "this version of VLC is outdated" );
210 /* put the mirror information */
211 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
212 p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
213 p_uit->mirror.psz_type );
214 /* make sure that we perform this check only once */
215 releaseChecked = YES;
217 else if(! releaseChecked )
219 [o_fld_status setStringValue: _NS("Your version of VLC " \
221 [o_btn_DownloadNow setEnabled: NO];
222 msg_Dbg( p_intf, "current version is up-to-date" );
223 releaseChecked = YES;
229 update_iterator_Delete( p_uit );
231 /* wait for our download, since it is done by another thread
232 * this does usually take 300000 to 500000 ms */
234 while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
240 /* if this takes more than 1 min, exit */
241 msg_Warn( p_intf, "download took more than a minute, exiting" );
245 msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
248 /* let's open our cached release note and display it
249 * we can't use NSString stringWithContentsOfFile:encoding:error:
250 * since it is Tiger only */
251 NSString * releaseNote = [[NSString alloc] initWithData: \
252 [NSData dataWithContentsOfFile: pathToReleaseNote] \
253 encoding: NSISOLatin1StringEncoding];
255 [o_fld_releaseNote setString: releaseNote];
257 /* delete the file since it isn't needed anymore */
259 myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
260 pathToReleaseNote handler: nil];
264 - (void)performDownload:(NSString *)path
266 /* enable this once notifications are completely available on OSX
267 update_iterator_t *p_uit = update_iterator_New( p_u );
270 update_iterator_Action( p_uit, UPDATE_MIRROR );
273 while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
275 if( p_uit->file.psz_url == [o_hashOfOurBinary UTF8String] )
281 update_download( p_uit, (char *)[path UTF8String] );
283 update_iterator_Delete( p_uit );
286 /* delete the following afterwards */
287 msg_Dbg( p_intf, "url is %s, using default browser for download", \
288 [o_urlOfBinary UTF8String] );
290 NSURL * o_url = [NSURL URLWithString: o_urlOfBinary];
292 [[NSWorkspace sharedWorkspace] openURL: o_url];
294 [o_update_window close];