]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
04fc5743a224ad9591a7b3143ba83926b0ab74e7
[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ühne <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 
156                             modalDelegate:self
157                            didEndSelector:sel
158                               contextInfo:nil];
159 }
160
161 - (void)getLocationForSaving: (NSSavePanel *)sheet 
162                   returnCode: (int)returnCode 
163                  contextInfo: (void *)contextInfo
164 {
165     if( returnCode == NSOKButton )
166     {
167         /* perform download and pass the selected path */
168         [self performDownload: [sheet filename]];
169     }
170     [sheet release];
171 }
172
173 - (IBAction)okay:(id)sender
174 {
175     /* just close the window */
176     [o_update_window close];
177 }
178
179 - (IBAction)changeCheckUpdateOnStartup:(id)sender
180 {
181     [self setShouldCheckUpdate: [sender state]];
182 }
183
184 - (void)checkForUpdate
185 {
186     /* We may not run on first thread */
187     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
188     p_u = update_New( p_intf );
189     update_Check( p_u, VLC_FALSE );
190     update_iterator_t *p_uit = update_iterator_New( p_u );
191     BOOL releaseChecked = NO;
192     BOOL gettingReleaseNote = NO;
193     int x = 0;
194     NSString * pathToReleaseNote;
195     pathToReleaseNote = [NSString stringWithFormat:
196         @"/tmp/vlc_releasenote_%d.tmp", mdate()];
197
198     [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
199
200     if( p_uit )
201     {
202         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
203         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
204         update_iterator_Action( p_uit, UPDATE_MIRROR );
205  
206         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
207         {
208             msg_Dbg( p_intf, "parsing available updates, run %i", x );
209             /* if the announced item is of the type "binary", keep it and display
210              * its details to the user. Do similar stuff on "info". Do both
211              * only if the file is announced as stable */
212             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
213             {
214                 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
215                 {
216                     msg_Dbg( p_intf, "release note found, desc = %s",
217                         p_uit->file.psz_description );
218                     [o_fld_releaseNote setString:
219                         [NSString stringWithUTF8String:
220                         (p_uit->file.psz_description)]];
221                     /* download our release note
222                      * We will read the temp file after this loop */
223                     update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
224                     gettingReleaseNote = YES;
225                 }
226                 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
227                 {
228                     msg_Dbg( p_intf, "binary found, version = %s, "
229                         "url=%s, size=%i MB", p_uit->release.psz_version,
230                         p_uit->file.psz_url,
231                         (int)((p_uit->file.l_size / 1024) / 1024) );
232                     [o_fld_currentVersionAndSize setStringValue: 
233                         [NSString stringWithFormat:
234                         _NS("The latest VLC media player release "
235                             "is %s (%i MB to download)."),
236                         p_uit->release.psz_version, ((p_uit->file.l_size
237                         / 1024) / 1024)]];
238  
239                     if( o_urlOfBinary )
240                         [o_urlOfBinary release];
241                     o_urlOfBinary = [[NSString alloc] initWithUTF8String:
242                         p_uit->file.psz_url];
243                 }
244                 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
245                     !releaseChecked )
246                 {
247                     /* our version is outdated, let the user download the new
248                      * release */
249                     [o_fld_status setStringValue: _NS("This version of VLC "
250                         "is outdated.")];
251                     [o_btn_DownloadNow setEnabled: YES];
252                     msg_Dbg( p_intf, "this version of VLC is outdated" );
253                     /* put the mirror information */
254                     msg_Dbg( p_intf, "used mirror: %s, %s [%s]",
255                             p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
256                             p_uit->mirror.psz_type );
257                     /* make sure that we perform this check only once */
258                     releaseChecked = YES;
259                     /* Make sure the update window is showed in case we have something */
260                     [o_update_window center];
261                     [o_update_window displayIfNeeded];
262                     [o_update_window makeKeyAndOrderFront: self];
263
264                 }
265                 else if(! releaseChecked )
266                 {
267                     [o_fld_status setStringValue: _NS("This version of VLC "
268                         "is the latest available.")];
269                     [o_btn_DownloadNow setEnabled: NO];
270                     msg_Dbg( p_intf, "current version is up-to-date" );
271                     releaseChecked = YES;
272                 }
273             }
274             x += 1;
275         }
276
277         update_iterator_Delete( p_uit );
278
279         /* wait for our release notes if necessary, since the download is done
280          * by another thread -- this does usually take 300000 to 500000 ms */
281         if( gettingReleaseNote )
282         {
283             int i = 0;
284             while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
285             {
286                 msleep( 100000 );
287                 i += 1;
288                 if( i == 150 )
289                 {
290                     /* if this takes more than 15 sec, exit */
291                     msg_Warn( p_intf, "download took more than 15 sec, exiting" );
292                     break;
293                 }
294             }
295             msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
296             msleep( 500000 );
297
298             /* let's open our cached release note and display it
299              * we can't use NSString stringWithContentsOfFile:encoding:error:
300              * since it is Tiger only */
301             NSString * releaseNote = [[NSString alloc] initWithData:
302                 [NSData dataWithContentsOfFile: pathToReleaseNote]
303                 encoding: NSISOLatin1StringEncoding];
304             if( releaseNote )
305                 [o_fld_releaseNote setString: releaseNote];
306  
307             /* delete the file since it isn't needed anymore */
308             BOOL myBOOL = NO;
309             myBOOL = [[NSFileManager defaultManager] removeFileAtPath:
310                 pathToReleaseNote handler: nil];
311         }
312         else
313         {
314             /* don't confuse the user, but make her happy */
315             [o_fld_status setStringValue: _NS("This version of VLC "
316                 "is the latest available.")];
317             [o_btn_DownloadNow setEnabled: NO];
318             msg_Dbg( p_intf, "current version is up-to-date" );
319         }
320     }
321     [pool release];
322 }
323
324 - (void)performDownload:(NSString *)path
325 {
326     update_iterator_t *p_uit = update_iterator_New( p_u );
327     if( p_uit )
328     {
329         update_iterator_Action( p_uit, UPDATE_MIRROR );
330
331         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
332         {
333             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
334                 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
335                 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
336             {
337                 /* put the mirror information */
338                 msg_Dbg( p_intf, "used mirror: %s, %s [%s]",
339                     p_uit->mirror.psz_name, p_uit->mirror.psz_location,
340                     p_uit->mirror.psz_type );
341
342                 /* that's our binary */
343                 update_download( p_uit, (char *)[path UTF8String] );
344             }
345         }
346  
347         update_iterator_Delete( p_uit );
348     }
349
350     [o_update_window close];
351 }
352
353 @end