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