fix macosx update gui
[vlc.git] / modules / gui / macosx / update.m
blob679784307f306cb6aaf5685abe3d20de961cd13e
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  *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #   include "config.h"
27 #endif
29 #ifdef UPDATE_CHECK
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #import "update.h"
35 #import "intf.h"
37 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
38 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
40 /*****************************************************************************
41  * VLCExtended implementation
42  *****************************************************************************/
44 @implementation VLCUpdate
46 static VLCUpdate *_o_sharedInstance = nil;
48 + (VLCUpdate *)sharedInstance
50     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
53 - (id)init
55     if( _o_sharedInstance ) {
56         [self dealloc];
57     } else {
58         _o_sharedInstance = [super init];
59         b_checked = VLC_FALSE;
61         /* clean the interface */
62         [o_fld_releaseNote setString: @""];
63         [o_fld_currentVersion setString: @""];
64         /* translate strings to the user's language */
65         [o_update_window setTitle: _NS("Check for Updates")];
66         [o_btn_DownloadNow setTitle: _NS("Download now")];
67         [o_btn_okay setTitle: _NS("OK")];
68         [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
69     }
71     return _o_sharedInstance;
74 - (void)awakeFromNib
76     /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
77      * the Alert panel to pop up at this time */
78     [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
81 - (void)setShouldCheckUpdate: (BOOL)check
83     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
84     [o_chk_updateOnStartup setState: check];
87 - (BOOL)shouldCheckForUpdate
89     NSDate *o_last_update;
90     NSDate *o_next_update;
92     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
93     {
94         /* We don't have any preferences stored, ask the user. */
95         int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
96               _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
97         [self setShouldCheckUpdate: res];
98     }
100     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
101         return NO;
103     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
104     if( !o_last_update )
105         return YES;
107     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
108     if( !o_next_update )
109         return YES;
111     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
114 - (void)showUpdateWindow
116     /* show the window and check for a potential update */
117     [o_update_window center];
118     [o_update_window displayIfNeeded];
119     [o_update_window makeKeyAndOrderFront:nil];
121     if( !b_checked )
122     {
123         [o_bar_checking startAnimation: self];
124         [self checkForUpdate];
125         b_checked = VLC_TRUE;
126         [o_bar_checking stopAnimation: self];
127     }
130 - (IBAction)download:(id)sender
132     /* provide a save dialogue */
133     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
134     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
136     [saveFilePanel setRequiredFileType: @"dmg"];
137     [saveFilePanel setCanSelectHiddenExtension: YES];
138     [saveFilePanel setCanCreateDirectories: YES];
139     [saveFilePanel beginSheetForDirectory:nil file:
140         [[[NSString stringWithUTF8String: p_u->release.psz_url] componentsSeparatedByString:@"/"] lastObject]
141                            modalForWindow: o_update_window 
142                             modalDelegate:self
143                            didEndSelector:sel
144                               contextInfo:nil];
147 - (void)getLocationForSaving: (NSSavePanel *)sheet 
148                   returnCode: (int)returnCode 
149                  contextInfo: (void *)contextInfo
151     if( returnCode == NSOKButton )
152     {
153         /* perform download and pass the selected path */
154         [self performDownload: [sheet filename]];
155     }
156     [sheet release];
159 - (IBAction)okay:(id)sender
161     /* just hides the window */
162     [o_update_window orderOut: self];
165 - (IBAction)changeCheckUpdateOnStartup:(id)sender
167     [self setShouldCheckUpdate: [sender state]];
170 - (void)setUpToDate:(BOOL)uptodate
172     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
174     if( uptodate )
175     {
176         [o_fld_releaseNote setString: [NSString string]];
177         [o_fld_currentVersion setStringValue: [NSString string]];
178         [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
179         [o_btn_DownloadNow setEnabled: NO];
180     }
181     else
182     {
183         [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_u->release.psz_desc ? p_u->release.psz_desc : "" )]];
184         [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
185         [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
186             _NS("The current release is %d.%d.%d%c."), p_u->release.i_major,
187             p_u->release.i_minor, p_u->release.i_revision, p_u->release.extra]];
188         [o_btn_DownloadNow setEnabled: YES];
189         /* Make sure the update window is showed in case we have something */
190         [o_update_window center];
191         [o_update_window displayIfNeeded];
192         [o_update_window makeKeyAndOrderFront: self];
193     }
195     [pool release];
198 static void updateCallback( void * p_data, vlc_bool_t b_success )
200     [(id)p_data setUpToDate: !b_success || update_CompareReleaseToCurrent( ((VLCUpdate*)p_data)->p_u ) == UpdateReleaseStatusNewer ];
203 - (void)checkForUpdate
205     p_u = update_New( VLCIntf );
206     if( !p_u )
207         return;
208     update_Check( p_u, updateCallback, self );
210     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
213 - (void)performDownload:(NSString *)path
215     update_Download( p_u, (char*)[path UTF8String] );
216     [o_btn_DownloadNow setEnabled: NO];
217     [o_update_window orderOut: self];
218     update_Delete( p_u );
221 @end
223 #endif