]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
0cdb26bb13ab05679f270ef01bfd9a0174a94efc
[vlc] / modules / gui / macosx / update.m
1 /*****************************************************************************
2  * update.m: MacOS X Check-For-Update window
3  *****************************************************************************
4  * Copyright (C) 2005-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix K\9fhne <fkuehne@users.sf.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Note: the code used to communicate with VLC's core was inspired by 
27  * ../wxwidgets/dialogs/updatevlc.cpp, written by Antoine Cellerier. 
28  *****************************************************************************/
29
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #import "update.h"
35 #import "intf.h"
36
37 static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
38 static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
39
40 /*****************************************************************************
41  * VLCExtended implementation
42  *****************************************************************************/
43
44 @implementation VLCUpdate
45
46 static VLCUpdate *_o_sharedInstance = nil;
47
48 + (VLCUpdate *)sharedInstance
49 {
50     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
51 }
52
53 - (id)init
54 {
55     if( _o_sharedInstance ) {
56         [self dealloc];
57     } else {
58         _o_sharedInstance = [super init];
59     }
60
61     return _o_sharedInstance;
62 }
63
64 - (void)awakeFromNib
65 {
66     /* get up */
67     p_intf = VLCIntf;
68
69     /* clean the interface */
70     [o_fld_releaseNote setString: @""];
71     
72     [self initInterface];
73 }
74
75 - (void)dealloc
76 {
77     if( o_urlOfBinary )
78         [o_urlOfBinary release];
79
80     [super dealloc];
81 }
82
83 - (void)initInterface
84 {
85     /* translate strings to the user's language */
86     [o_update_window setTitle: _NS("Check for Updates")];
87     [o_btn_DownloadNow setTitle: _NS("Download now")];
88     [o_btn_okay setTitle: _NS("OK")];
89     [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
90     /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want the Alert
91      * panel to pop up at this time */
92     [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
93 }
94
95 - (void)setShouldCheckUpdate: (BOOL)check
96 {
97     [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
98     [o_chk_updateOnStartup setState: check];
99 }
100
101 - (BOOL)shouldCheckForUpdate
102 {
103     NSDate *o_last_update;
104     NSDate *o_next_update;
105     
106     if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
107     {
108         /* We don't have any preferences stored, ask the user. */
109         int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
110               _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
111         [self setShouldCheckUpdate: res];
112     }
113
114     if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
115         return NO;
116
117     o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
118     if( !o_last_update )
119         return YES;
120
121     o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
122     if( !o_next_update )
123         return YES;
124
125     return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
126 }
127
128 - (void)showUpdateWindow
129 {
130     /* show the window and check for a potential update */
131     [o_fld_status setStringValue: _NS("Checking for Updates...")];
132     [o_fld_currentVersionAndSize setStringValue: @""];
133     [o_fld_releaseNote setString: @""];
134
135     [o_update_window center];
136     [o_update_window displayIfNeeded];
137     [o_update_window makeKeyAndOrderFront:nil];
138
139     [o_bar_checking startAnimation: self];
140     [self checkForUpdate];
141     [o_bar_checking stopAnimation: self];
142 }
143
144 - (IBAction)download:(id)sender
145 {
146     /* provide a save dialogue */
147     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
148     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
149     
150     [saveFilePanel setRequiredFileType: @"dmg"];
151     [saveFilePanel setCanSelectHiddenExtension: YES];
152     [saveFilePanel setCanCreateDirectories: YES];
153     [saveFilePanel beginSheetForDirectory:nil file: \
154         [[o_urlOfBinary componentsSeparatedByString:@"/"] lastObject] \
155         modalForWindow: o_update_window modalDelegate:self didEndSelector:sel \
156         contextInfo:nil];
157 }
158
159 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
160     (int)returnCode contextInfo: (void *)contextInfo
161 {
162     if( returnCode == NSOKButton )
163     {
164         /* perform download and pass the selected path */
165         [self performDownload: [sheet filename]];
166     }
167     [sheet release];
168 }
169
170 - (IBAction)okay:(id)sender
171 {
172     /* just close the window */
173     [o_update_window close];
174 }
175
176 - (IBAction)changeCheckUpdateOnStartup:(id)sender
177 {
178     [self setShouldCheckUpdate: [sender state]];
179 }
180
181 - (void)checkForUpdate
182 {
183     /* We may not run on first thread */
184     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
185     p_u = update_New( p_intf );
186     update_Check( p_u, VLC_FALSE );
187     update_iterator_t *p_uit = update_iterator_New( p_u );
188     BOOL releaseChecked = NO;
189     BOOL gettingReleaseNote = NO;
190     int x = 0;
191     NSString * pathToReleaseNote;
192     pathToReleaseNote = [NSString stringWithFormat: \
193         @"/tmp/vlc_releasenote_%d.tmp", mdate()];
194
195     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
196
197     if( p_uit )
198     {
199         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
200         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
201         update_iterator_Action( p_uit, UPDATE_MIRROR );
202         
203         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
204         {
205             msg_Dbg( p_intf, "parsing available updates, run %i", x );
206             /* if the announced item is of the type "binary", keep it and display
207              * its details to the user. Do similar stuff on "info". Do both 
208              * only if the file is announced as stable */
209             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
210             {
211                 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
212                 {
213                     msg_Dbg( p_intf, "release note found, desc = %s",
214                         p_uit->file.psz_description );
215                     [o_fld_releaseNote setString: \
216                         [NSString stringWithUTF8String: \
217                         (p_uit->file.psz_description)]];
218                     /* download our release note
219                      * We will read the temp file after this loop */
220                     update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
221                     gettingReleaseNote = YES;
222                 }
223                 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
224                 {
225                     msg_Dbg( p_intf, "binary found, version = %s, " \
226                         "url=%s, size=%i MB", p_uit->release.psz_version, \
227                         p_uit->file.psz_url, \
228                         (int)((p_uit->file.l_size / 1024) / 1024) );
229                     [o_fld_currentVersionAndSize setStringValue: [NSString \
230                         stringWithFormat: \
231                         _NS("The latest VLC media player release " \
232                             "is %s (%i MB to download)."), \
233                         p_uit->release.psz_version, ((p_uit->file.l_size \
234                         / 1024) / 1024)]];
235                         
236                     if( o_urlOfBinary )
237                         [o_urlOfBinary release];
238                     o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
239                         p_uit->file.psz_url];
240                 }
241                 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
242                     !releaseChecked )
243                 {
244                     /* our version is outdated, let the user download the new
245                      * release */
246                     [o_fld_status setStringValue: _NS("This version of VLC " \
247                         "is outdated.")];
248                     [o_btn_DownloadNow setEnabled: YES];
249                     msg_Dbg( p_intf, "this version of VLC is outdated" );
250                     /* put the mirror information */
251                     msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
252                             p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
253                             p_uit->mirror.psz_type );
254                     /* make sure that we perform this check only once */
255                     releaseChecked = YES;
256                     /* Make sure the update window is showed in case we have something */
257                     [o_update_window center];
258                     [o_update_window displayIfNeeded];
259                     [o_update_window makeKeyAndOrderFront: self];
260
261                 }
262                 else if(! releaseChecked )
263                 {
264                     [o_fld_status setStringValue: _NS("This version of VLC " \
265                         "is the latest available.")];
266                     [o_btn_DownloadNow setEnabled: NO];
267                     msg_Dbg( p_intf, "current version is up-to-date" );
268                     releaseChecked = YES;
269                 }
270             }
271             x += 1;
272         }
273
274         update_iterator_Delete( p_uit );
275
276         /* wait for our release notes if necessary, since the download is done
277          * by another thread -- this does usually take 300000 to 500000 ms */
278         if( gettingReleaseNote )
279         {
280             int i = 0;
281             while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
282             {
283                 msleep( 100000 );
284                 i += 1;
285                 if( i == 150 )
286                 {
287                     /* if this takes more than 15 sec, exit */
288                     msg_Warn( p_intf, "download took more than 15 sec, exiting" );
289                     break;
290                 }
291             }
292             msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
293             msleep( 500000 );
294
295             /* let's open our cached release note and display it
296              * we can't use NSString stringWithContentsOfFile:encoding:error: 
297              * since it is Tiger only */
298             NSString * releaseNote = [[NSString alloc] initWithData: \
299                 [NSData dataWithContentsOfFile: pathToReleaseNote] \
300                 encoding: NSISOLatin1StringEncoding];
301             if( releaseNote )
302                 [o_fld_releaseNote setString: releaseNote];
303         
304             /* delete the file since it isn't needed anymore */
305             BOOL myBOOL = NO;
306             myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
307                 pathToReleaseNote handler: nil];
308         }
309         else
310         {
311             /* don't confuse the user, but make her happy */
312             [o_fld_status setStringValue: _NS("This version of VLC " \
313                 "is the latest available.")];
314             [o_btn_DownloadNow setEnabled: NO];
315             msg_Dbg( p_intf, "current version is up-to-date" );
316         }
317     }
318     [pool release];
319 }
320
321 - (void)performDownload:(NSString *)path
322 {
323     update_iterator_t *p_uit = update_iterator_New( p_u );
324     if( p_uit )
325     {
326         update_iterator_Action( p_uit, UPDATE_MIRROR );
327
328         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
329         {
330             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
331                 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
332                 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
333             {
334                 /* put the mirror information */
335                 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
336                     p_uit->mirror.psz_name, p_uit->mirror.psz_location, \
337                     p_uit->mirror.psz_type );
338
339                 /* that's our binary */
340                 update_download( p_uit, (char *)[path UTF8String] );
341             }
342         }
343         
344         update_iterator_Delete( p_uit );
345     }
346
347     [o_update_window close];
348 }
349
350 @end