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 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
33 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
34 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
36 /*****************************************************************************
37 * VLCExtended implementation
38 *****************************************************************************/
40 @implementation VLCUpdate
42 static VLCUpdate *_o_sharedInstance = nil;
44 + (VLCUpdate *)sharedInstance
46 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
51 if( _o_sharedInstance ) {
54 _o_sharedInstance = [super init];
57 /* clean the interface */
58 [o_fld_releaseNote setString: @""];
59 [o_fld_currentVersion setString: @""];
60 /* translate strings to the user's language */
61 [o_update_window setTitle: _NS("Check for Updates")];
62 [o_btn_DownloadNow setTitle: _NS("Download now")];
63 [o_btn_okay setTitle: _NS("OK")];
64 [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
67 return _o_sharedInstance;
72 /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
73 * the Alert panel to pop up at this time */
74 [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
77 - (void)setShouldCheckUpdate: (BOOL)check
79 [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
80 [o_chk_updateOnStartup setState: check];
83 - (BOOL)shouldCheckForUpdate
85 NSDate *o_last_update;
86 NSDate *o_next_update;
88 if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
90 /* We don't have any preferences stored, ask the user. */
91 int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
92 _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
93 [self setShouldCheckUpdate: res];
96 if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
99 o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
103 o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
107 return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
110 - (void)showUpdateWindow
112 /* show the window and check for a potential update */
113 [o_update_window center];
114 [o_update_window displayIfNeeded];
115 [o_update_window makeKeyAndOrderFront:nil];
119 [o_bar_checking startAnimation: self];
120 [self checkForUpdate];
122 [o_bar_checking stopAnimation: self];
126 - (IBAction)download:(id)sender
128 /* provide a save dialogue */
129 SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
130 NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
132 [saveFilePanel setRequiredFileType: @"dmg"];
133 [saveFilePanel setCanSelectHiddenExtension: YES];
134 [saveFilePanel setCanCreateDirectories: YES];
135 [saveFilePanel beginSheetForDirectory:nil file:
136 [[[NSString stringWithUTF8String: p_u->release.psz_url] componentsSeparatedByString:@"/"] lastObject]
137 modalForWindow: o_update_window
143 - (void)getLocationForSaving: (NSSavePanel *)sheet
144 returnCode: (int)returnCode
145 contextInfo: (void *)contextInfo
147 if( returnCode == NSOKButton )
149 /* perform download and pass the selected path */
150 [self performDownload: [sheet filename]];
155 - (IBAction)okay:(id)sender
157 /* just hides the window */
158 [o_update_window orderOut: self];
161 - (IBAction)changeCheckUpdateOnStartup:(id)sender
163 [self setShouldCheckUpdate: [sender state]];
166 - (void)setUpToDate:(BOOL)uptodate
168 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
172 [o_fld_releaseNote setString: @""];
173 [o_fld_currentVersion setStringValue: @""];
174 [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
175 [o_btn_DownloadNow setEnabled: NO];
179 [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_u->release.psz_desc ? p_u->release.psz_desc : "" )]];
180 [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
181 [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
182 _NS("The current release is %d.%d.%d%c."), p_u->release.i_major,
183 p_u->release.i_minor, p_u->release.i_revision, p_u->release.extra]];
184 [o_btn_DownloadNow setEnabled: YES];
185 /* Make sure the update window is showed in case we have something */
186 [o_update_window center];
187 [o_update_window displayIfNeeded];
188 [o_update_window makeKeyAndOrderFront: self];
194 static void updateCallback( void * p_data, bool b_success )
196 [(id)p_data setUpToDate: !b_success || update_CompareReleaseToCurrent( ((VLCUpdate*)p_data)->p_u ) == UpdateReleaseStatusNewer ];
199 - (void)checkForUpdate
201 p_u = update_New( VLCIntf );
204 update_Check( p_u, updateCallback, self );
206 [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
209 - (void)performDownload:(NSString *)path
211 update_Download( p_u, [path UTF8String] );
212 [o_btn_DownloadNow setEnabled: NO];
213 [o_update_window orderOut: self];
214 update_Delete( p_u );