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 * 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 /* 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 /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
75 * the Alert panel to pop up at this time */
76 [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
79 - (void)setShouldCheckUpdate: (BOOL)check
81 [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
82 [o_chk_updateOnStartup setState: check];
85 - (BOOL)shouldCheckForUpdate
87 NSDate *o_last_update;
88 NSDate *o_next_update;
90 if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
92 /* We don't have any preferences stored, ask the user. */
93 int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
94 _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
95 [self setShouldCheckUpdate: res];
98 if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
101 o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
105 o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
109 return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
112 - (void)showUpdateWindow
114 /* show the window and check for a potential update */
115 [o_update_window center];
116 [o_update_window displayIfNeeded];
117 [o_update_window makeKeyAndOrderFront:nil];
121 [o_bar_checking startAnimation: self];
122 [self checkForUpdate];
124 [o_bar_checking stopAnimation: self];
128 - (IBAction)download:(id)sender
130 /* provide a save dialogue */
131 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
132 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
134 [saveFilePanel setRequiredFileType: @"dmg"];
135 [saveFilePanel setCanSelectHiddenExtension: YES];
136 [saveFilePanel setCanCreateDirectories: YES];
137 update_release_t *p_release = update_GetRelease( p_u );
139 [saveFilePanel beginSheetForDirectory:nil file:
140 [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
141 modalForWindow: o_update_window
147 - (void)getLocationForSaving: (NSSavePanel *)sheet
148 returnCode: (int)returnCode
149 contextInfo: (void *)contextInfo
151 if( returnCode == NSOKButton )
153 /* perform download and pass the selected path */
154 [self performDownload: [sheet filename]];
159 - (IBAction)okay:(id)sender
161 /* just hides the window */
162 [o_update_window orderOut: self];
165 - (IBAction)changeCheckUpdateOnStartup:(id)sender
167 [self setShouldCheckUpdate: [sender state]];
170 - (void)setUpToDate:(BOOL)uptodate
174 [o_fld_releaseNote setString: @""];
175 [o_fld_currentVersion setStringValue: @""];
176 [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
177 [o_btn_DownloadNow setEnabled: NO];
181 update_release_t *p_release = update_GetRelease( p_u );
182 [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_release->psz_desc ? p_release->psz_desc : "" )]];
183 [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
184 [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
185 _NS("The current release is %d.%d.%d%c."), p_release->i_major,
186 p_release->i_minor, p_release->i_revision, p_release->extra]];
187 [o_btn_DownloadNow setEnabled: YES];
188 /* Make sure the update window is showed in case we have something */
189 [o_update_window center];
190 [o_update_window displayIfNeeded];
191 [o_update_window makeKeyAndOrderFront: self];
195 static void updateCallback( void * p_data, bool b_success )
197 VLCUpdate * update = p_data;
198 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
199 NSNumber * state = [NSNumber numberWithBool:!b_success || !update_NeedUpgrade( update->p_u )];
200 [update performSelectorOnMainThread:@selector(setUpToDate:) withObject:state waitUntilDone:YES];
204 - (void)checkForUpdate
206 p_u = update_New( VLCIntf );
209 update_Check( p_u, updateCallback, self );
211 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
212 [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
216 - (void)performDownload:(NSString *)path
218 update_Download( p_u, [path UTF8String] );
219 [o_btn_DownloadNow setEnabled: NO];
220 [o_update_window orderOut: self];
221 update_Delete( p_u );