]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
macosx: The vlc update API can only handle a destination directory.
[vlc] / modules / gui / macosx / update.m
1 /*****************************************************************************
2  * update.m: MacOS X Check-For-Update window
3  *****************************************************************************
4  * Copyright © 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Kühne <fkuehne@users.sf.net>
8  *          Rafaël Carré <funman@videolanorg>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #import "update.h"
26
27 #ifdef UPDATE_CHECK
28
29 #include <assert.h>
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
36 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
37
38 /*****************************************************************************
39  * VLCUpdate implementation
40  *****************************************************************************/
41
42 @implementation VLCUpdate
43
44 static VLCUpdate *_o_sharedInstance = nil;
45
46 + (VLCUpdate *)sharedInstance
47 {
48     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
49 }
50
51 - (id)init
52 {
53     if( _o_sharedInstance ) {
54         [self dealloc];
55     } else {
56         _o_sharedInstance = [super init];
57         b_checked = false;
58
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")];
67     }
68
69     return _o_sharedInstance;
70 }
71
72 - (void)end
73 {
74     if( p_u ) update_Delete( p_u );
75 }
76
77 - (void)awakeFromNib
78 {
79     /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
80      * the Alert panel to pop up at this time */
81     [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
82 }
83
84 - (void)setShouldCheckUpdate: (BOOL)check
85 {
86     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
87     [o_chk_updateOnStartup setState: check];
88
89     /* make sure we got this set, even if we crash later on */
90     [[NSUserDefaults standardUserDefaults] synchronize];
91 }
92
93 - (BOOL)shouldCheckForUpdate
94 {
95     NSDate *o_last_update;
96     NSDate *o_next_update;
97  
98     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
99     {
100         /* We don't have any preferences stored, ask the user. */
101         int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
102               _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
103         [self setShouldCheckUpdate: res];
104     }
105
106     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
107         return NO;
108
109     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
110     if( !o_last_update )
111         return YES;
112
113     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*7 /* every seven days */ sinceDate: o_last_update] autorelease];
114     if( !o_next_update )
115         return YES;
116
117     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
118 }
119
120 - (void)showUpdateWindow
121 {
122     /* show the window and check for a potential update */
123     [o_update_window center];
124     [o_update_window displayIfNeeded];
125     [o_update_window makeKeyAndOrderFront:nil];
126
127     if( !b_checked )
128     {
129         [o_bar_checking startAnimation: self];
130         [self checkForUpdate];
131         b_checked = true;
132         [o_bar_checking stopAnimation: self];
133     }
134 }
135
136 - (IBAction)download:(id)sender
137 {
138     /* provide a save dialogue */
139     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
140     NSOpenPanel * saveFilePanel = [[NSOpenPanel alloc] init];
141
142     [saveFilePanel setCanChooseFiles: NO];
143     [saveFilePanel setCanChooseDirectories: YES];
144     [saveFilePanel setCanCreateDirectories: YES];
145     update_release_t *p_release = update_GetRelease( p_u );
146     assert( p_release );
147     [saveFilePanel beginSheetForDirectory:@"~/Downloads" file:
148         [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
149                            modalForWindow: o_update_window 
150                             modalDelegate:self
151                            didEndSelector:sel
152                               contextInfo:nil];
153 }
154
155 - (void)getLocationForSaving: (NSOpenPanel *)sheet 
156                   returnCode: (int)returnCode 
157                  contextInfo: (void *)contextInfo
158 {
159     if( returnCode == NSOKButton )
160     {
161         /* perform download and pass the selected path */
162         [NSThread detachNewThreadSelector:@selector(performDownload:) toTarget:self withObject:[sheet directory]];
163     }
164     [sheet release];
165 }
166
167 - (IBAction)okay:(id)sender
168 {
169     /* just hides the window */
170     [o_update_window orderOut: self];
171 }
172
173 - (IBAction)changeCheckUpdateOnStartup:(id)sender
174 {
175     [self setShouldCheckUpdate: [sender state]];
176 }
177
178 - (void)setUpToDate:(NSNumber *)uptodate
179 {
180     if( [uptodate boolValue] )
181     {
182         [o_fld_releaseNote setString: @""];
183         [o_fld_currentVersion setStringValue: @""];
184         [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
185         [o_btn_DownloadNow setEnabled: NO];
186     }
187     else
188     {
189         update_release_t *p_release = update_GetRelease( p_u );
190         [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_release->psz_desc ? p_release->psz_desc : "" )]];
191         [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
192         [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
193             _NS("The current release is %d.%d.%d%c."), p_release->i_major,
194             p_release->i_minor, p_release->i_revision, p_release->extra]];
195         [o_btn_DownloadNow setEnabled: YES];
196         /* Make sure the update window is showed in case we have something */
197         [o_update_window center];
198         [o_update_window displayIfNeeded];
199         [o_update_window makeKeyAndOrderFront: self];
200     }
201 }
202
203 static void updateCallback( void * p_data, bool b_success )
204 {
205     VLCUpdate * update = p_data;
206     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
207     NSNumber * state = [NSNumber numberWithBool:!b_success || !update_NeedUpgrade( update->p_u )];
208     [update performSelectorOnMainThread:@selector(setUpToDate:) withObject:state waitUntilDone:YES];
209     [pool release];
210 }
211
212 - (void)checkForUpdate
213 {
214     p_u = update_New( VLCIntf );
215     if( !p_u )
216         return;
217     update_Check( p_u, updateCallback, self );
218
219     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
220     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
221     [pool release];
222 }
223
224 - (void)performDownload:(NSString *)path
225 {
226     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
227     update_Download( p_u, [path UTF8String] );
228     [o_btn_DownloadNow setEnabled: NO];
229     [o_update_window orderOut: self];
230     update_WaitDownload( p_u );
231     update_Delete( p_u );
232     p_u = nil;
233     [pool release];
234 }
235
236 @end
237
238 #endif