]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
920a991d6a1bd1c9c20465f4d086ae12a5160656
[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 #ifdef UPDATE_CHECK
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #import "update.h"
31
32 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
33 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
34
35 /*****************************************************************************
36  * VLCExtended implementation
37  *****************************************************************************/
38
39 @implementation VLCUpdate
40
41 static VLCUpdate *_o_sharedInstance = nil;
42
43 + (VLCUpdate *)sharedInstance
44 {
45     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
46 }
47
48 - (id)init
49 {
50     if( _o_sharedInstance ) {
51         [self dealloc];
52     } else {
53         _o_sharedInstance = [super init];
54         b_checked = VLC_FALSE;
55
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")];
64     }
65
66     return _o_sharedInstance;
67 }
68
69 - (void)awakeFromNib
70 {
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]];
74 }
75
76 - (void)setShouldCheckUpdate: (BOOL)check
77 {
78     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
79     [o_chk_updateOnStartup setState: check];
80 }
81
82 - (BOOL)shouldCheckForUpdate
83 {
84     NSDate *o_last_update;
85     NSDate *o_next_update;
86  
87     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
88     {
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];
93     }
94
95     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
96         return NO;
97
98     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
99     if( !o_last_update )
100         return YES;
101
102     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
103     if( !o_next_update )
104         return YES;
105
106     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
107 }
108
109 - (void)showUpdateWindow
110 {
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];
115
116     if( !b_checked )
117     {
118         [o_bar_checking startAnimation: self];
119         [self checkForUpdate];
120         b_checked = VLC_TRUE;
121         [o_bar_checking stopAnimation: self];
122     }
123 }
124
125 - (IBAction)download:(id)sender
126 {
127     /* provide a save dialogue */
128     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
129     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
130
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 
137                             modalDelegate:self
138                            didEndSelector:sel
139                               contextInfo:nil];
140 }
141
142 - (void)getLocationForSaving: (NSSavePanel *)sheet 
143                   returnCode: (int)returnCode 
144                  contextInfo: (void *)contextInfo
145 {
146     if( returnCode == NSOKButton )
147     {
148         /* perform download and pass the selected path */
149         [self performDownload: [sheet filename]];
150     }
151     [sheet release];
152 }
153
154 - (IBAction)okay:(id)sender
155 {
156     /* just hides the window */
157     [o_update_window orderOut: self];
158 }
159
160 - (IBAction)changeCheckUpdateOnStartup:(id)sender
161 {
162     [self setShouldCheckUpdate: [sender state]];
163 }
164
165 - (void)setUpToDate:(BOOL)uptodate
166 {
167     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
168
169     if( uptodate )
170     {
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];
175     }
176     else
177     {
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];
188     }
189
190     [pool release];
191 }
192
193 static void updateCallback( void * p_data, vlc_bool_t b_success )
194 {
195     [(id)p_data setUpToDate: !b_success || update_CompareReleaseToCurrent( ((VLCUpdate*)p_data)->p_u ) == UpdateReleaseStatusNewer ];
196 }
197
198 - (void)checkForUpdate
199 {
200     p_u = update_New( VLCIntf );
201     if( !p_u )
202         return;
203     update_Check( p_u, updateCallback, self );
204
205     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
206 }
207
208 - (void)performDownload:(NSString *)path
209 {
210     update_Download( p_u, [path UTF8String] );
211     [o_btn_DownloadNow setEnabled: NO];
212     [o_update_window orderOut: self];
213     update_Delete( p_u );
214 }
215
216 @end
217
218 #endif