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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
33 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
35 /*****************************************************************************
36 * VLCExtended implementation
37 *****************************************************************************/
39 @implementation VLCUpdate
41 static VLCUpdate *_o_sharedInstance = nil;
43 + (VLCUpdate *)sharedInstance
45 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
50 if( _o_sharedInstance ) {
53 _o_sharedInstance = [super init];
54 b_checked = VLC_FALSE;
56 /* clean the interface */
57 [o_fld_releaseNote setString: @""];
58 [o_fld_currentVersion setString: @""];
59 /* translate strings to the user's language */
60 [o_update_window setTitle: _NS("Check for Updates")];
61 [o_btn_DownloadNow setTitle: _NS("Download now")];
62 [o_btn_okay setTitle: _NS("OK")];
63 [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
66 return _o_sharedInstance;
71 /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
72 * the Alert panel to pop up at this time */
73 [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
76 - (void)setShouldCheckUpdate: (BOOL)check
78 [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
79 [o_chk_updateOnStartup setState: check];
82 - (BOOL)shouldCheckForUpdate
84 NSDate *o_last_update;
85 NSDate *o_next_update;
87 if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
89 /* We don't have any preferences stored, ask the user. */
90 int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
91 _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
92 [self setShouldCheckUpdate: res];
95 if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
98 o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
102 o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
106 return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
109 - (void)showUpdateWindow
111 /* show the window and check for a potential update */
112 [o_update_window center];
113 [o_update_window displayIfNeeded];
114 [o_update_window makeKeyAndOrderFront:nil];
118 [o_bar_checking startAnimation: self];
119 [self checkForUpdate];
120 b_checked = VLC_TRUE;
121 [o_bar_checking stopAnimation: self];
125 - (IBAction)download:(id)sender
127 /* provide a save dialogue */
128 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
129 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
131 [saveFilePanel setRequiredFileType: @"dmg"];
132 [saveFilePanel setCanSelectHiddenExtension: YES];
133 [saveFilePanel setCanCreateDirectories: YES];
134 [saveFilePanel beginSheetForDirectory:nil file:
135 [[[NSString stringWithUTF8String: p_u->release.psz_url] componentsSeparatedByString:@"/"] lastObject]
136 modalForWindow: o_update_window
142 - (void)getLocationForSaving: (NSSavePanel *)sheet
143 returnCode: (int)returnCode
144 contextInfo: (void *)contextInfo
146 if( returnCode == NSOKButton )
148 /* perform download and pass the selected path */
149 [self performDownload: [sheet filename]];
154 - (IBAction)okay:(id)sender
156 /* just hides the window */
157 [o_update_window orderOut: self];
160 - (IBAction)changeCheckUpdateOnStartup:(id)sender
162 [self setShouldCheckUpdate: [sender state]];
165 - (void)setUpToDate:(BOOL)uptodate
167 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
171 [o_fld_releaseNote setString: @""];
172 [o_fld_currentVersion setStringValue: @""];
173 [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
174 [o_btn_DownloadNow setEnabled: NO];
178 [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_u->release.psz_desc ? p_u->release.psz_desc : "" )]];
179 [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
180 [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
181 _NS("The current release is %d.%d.%d%c."), p_u->release.i_major,
182 p_u->release.i_minor, p_u->release.i_revision, p_u->release.extra]];
183 [o_btn_DownloadNow setEnabled: YES];
184 /* Make sure the update window is showed in case we have something */
185 [o_update_window center];
186 [o_update_window displayIfNeeded];
187 [o_update_window makeKeyAndOrderFront: self];
193 static void updateCallback( void * p_data, vlc_bool_t b_success )
195 [(id)p_data setUpToDate: !b_success || update_CompareReleaseToCurrent( ((VLCUpdate*)p_data)->p_u ) == UpdateReleaseStatusNewer ];
198 - (void)checkForUpdate
200 p_u = update_New( VLCIntf );
203 update_Check( p_u, updateCallback, self );
205 [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
208 - (void)performDownload:(NSString *)path
210 update_Download( p_u, [path UTF8String] );
211 [o_btn_DownloadNow setEnabled: NO];
212 [o_update_window orderOut: self];
213 update_Delete( p_u );