]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
macosx: Use a toolbar to display controls. (Doesn't look like a toolbar though)
[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  * VLCExtended 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)awakeFromNib
73 {
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]];
77 }
78
79 - (void)setShouldCheckUpdate: (BOOL)check
80 {
81     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
82     [o_chk_updateOnStartup setState: check];
83 }
84
85 - (BOOL)shouldCheckForUpdate
86 {
87     NSDate *o_last_update;
88     NSDate *o_next_update;
89  
90     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
91     {
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];
96     }
97
98     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
99         return NO;
100
101     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
102     if( !o_last_update )
103         return YES;
104
105     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
106     if( !o_next_update )
107         return YES;
108
109     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
110 }
111
112 - (void)showUpdateWindow
113 {
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];
118
119     if( !b_checked )
120     {
121         [o_bar_checking startAnimation: self];
122         [self checkForUpdate];
123         b_checked = true;
124         [o_bar_checking stopAnimation: self];
125     }
126 }
127
128 - (IBAction)download:(id)sender
129 {
130     /* provide a save dialogue */
131     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
132     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
133
134     [saveFilePanel setRequiredFileType: @"dmg"];
135     [saveFilePanel setCanSelectHiddenExtension: YES];
136     [saveFilePanel setCanCreateDirectories: YES];
137     update_release_t *p_release = update_GetRelease( p_u );
138     assert( p_release );
139     [saveFilePanel beginSheetForDirectory:nil file:
140         [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
141                            modalForWindow: o_update_window 
142                             modalDelegate:self
143                            didEndSelector:sel
144                               contextInfo:nil];
145 }
146
147 - (void)getLocationForSaving: (NSSavePanel *)sheet 
148                   returnCode: (int)returnCode 
149                  contextInfo: (void *)contextInfo
150 {
151     if( returnCode == NSOKButton )
152     {
153         /* perform download and pass the selected path */
154         [self performDownload: [sheet filename]];
155     }
156     [sheet release];
157 }
158
159 - (IBAction)okay:(id)sender
160 {
161     /* just hides the window */
162     [o_update_window orderOut: self];
163 }
164
165 - (IBAction)changeCheckUpdateOnStartup:(id)sender
166 {
167     [self setShouldCheckUpdate: [sender state]];
168 }
169
170 - (void)setUpToDate:(BOOL)uptodate
171 {
172     if( uptodate )
173     {
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];
178     }
179     else
180     {
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];
192     }
193 }
194
195 static void updateCallback( void * p_data, bool b_success )
196 {
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];
201     [pool release];
202 }
203
204 - (void)checkForUpdate
205 {
206     p_u = update_New( VLCIntf );
207     if( !p_u )
208         return;
209     update_Check( p_u, updateCallback, self );
210
211     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
212     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
213     [pool release];
214 }
215
216 - (void)performDownload:(NSString *)path
217 {
218     update_Download( p_u, [path UTF8String] );
219     [o_btn_DownloadNow setEnabled: NO];
220     [o_update_window orderOut: self];
221     update_Delete( p_u );
222 }
223
224 @end
225
226 #endif