]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
Mac OS X gui: checkForUpdate may not run on first thread. Hence alloc an autorelease...
[vlc] / modules / gui / macosx / update.m
1 /*****************************************************************************
2  * update.m: MacOS X Check-For-Update window
3  *****************************************************************************
4  * Copyright (C) 2005-2006 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
38 /*****************************************************************************
39  * VLCExtended implementation
40  *****************************************************************************/
41
42 @implementation VLCUpdate
43
44 static VLCUpdate *_o_sharedInstance = nil;
45
46 + (VLCUpdate *)sharedInstance
47 {
48     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
49 }
50
51 - (id)init
52 {
53     if (_o_sharedInstance) {
54         [self dealloc];
55     } else {
56         _o_sharedInstance = [super init];
57     }
58
59     return _o_sharedInstance;
60 }
61
62 - (void)awakeFromNib
63 {
64     /* get up */
65     p_intf = VLCIntf;
66
67     /* clean the interface */
68     [o_fld_releaseNote setString: @""];
69     
70     [self initStrings];
71 }
72
73 - (void)dealloc
74 {
75     if( o_urlOfBinary )
76         [o_urlOfBinary release];
77
78     [super dealloc];
79 }
80
81 - (void)initStrings
82 {
83     /* translate strings to the user's language */
84     [o_update_window setTitle: _NS("Check for Updates")];
85     [o_btn_DownloadNow setTitle: _NS("Download now")];
86     [o_btn_okay setTitle: _NS("OK")];
87 }
88
89 - (void)showUpdateWindow
90 {
91     /* show the window and check for a potential update */
92     [o_fld_status setStringValue: _NS("Checking for Updates...")];
93     [o_fld_currentVersionAndSize setStringValue: @""];
94     [o_fld_releaseNote setString: @""];
95
96     [o_update_window center];
97     [o_update_window displayIfNeeded];
98     [o_update_window makeKeyAndOrderFront:nil];
99
100     [o_bar_checking startAnimation: self];
101     [self checkForUpdate];
102     [o_bar_checking stopAnimation: self];
103 }
104
105 - (IBAction)download:(id)sender
106 {
107     /* provide a save dialogue */
108     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
109     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
110     
111     [saveFilePanel setRequiredFileType: @"dmg"];
112     [saveFilePanel setCanSelectHiddenExtension: YES];
113     [saveFilePanel setCanCreateDirectories: YES];
114     [saveFilePanel beginSheetForDirectory:nil file: \
115         [[o_urlOfBinary componentsSeparatedByString:@"/"] lastObject] \
116         modalForWindow: o_update_window modalDelegate:self didEndSelector:sel \
117         contextInfo:nil];
118 }
119
120 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
121     (int)returnCode contextInfo: (void *)contextInfo
122 {
123     if (returnCode == NSOKButton)
124     {
125         /* perform download and pass the selected path */
126         [self performDownload: [sheet filename]];
127     }
128     [sheet release];
129 }
130
131 - (IBAction)okay:(id)sender
132 {
133     /* just close the window */
134     [o_update_window close];
135 }
136
137 - (void)checkForUpdate
138 {
139     /* We may not run on first thread */
140     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
141
142     p_u = update_New( p_intf );
143     update_Check( p_u, VLC_FALSE );
144     update_iterator_t *p_uit = update_iterator_New( p_u );
145     BOOL releaseChecked = NO;
146     BOOL gettingReleaseNote = NO;
147     int x = 0;
148     NSString * pathToReleaseNote;
149     pathToReleaseNote = [NSString stringWithFormat: \
150         @"/tmp/vlc_releasenote_%d.tmp", mdate()];
151
152     if( p_uit )
153     {
154         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
155         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
156         update_iterator_Action( p_uit, UPDATE_MIRROR );
157         
158         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
159         {
160             msg_Dbg( p_intf, "parsing available updates, run %i", x );
161             /* if the announced item is of the type "binary", keep it and display
162              * its details to the user. Do similar stuff on "info". Do both 
163              * only if the file is announced as stable */
164             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
165             {
166                 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
167                 {
168                     msg_Dbg( p_intf, "release note found, desc = %s",
169                         p_uit->file.psz_description );
170                     [o_fld_releaseNote setString: \
171                         [NSString stringWithUTF8String: \
172                         (p_uit->file.psz_description)]];
173                     /* download our release note
174                      * We will read the temp file after this loop */
175                     update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
176                     gettingReleaseNote = YES;
177                 }
178                 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
179                 {
180                     msg_Dbg( p_intf, "binary found, version = %s, " \
181                         "url=%s, size=%i MB", p_uit->release.psz_version, \
182                         p_uit->file.psz_url, \
183                         (int)((p_uit->file.l_size / 1024) / 1024) );
184                     [o_fld_currentVersionAndSize setStringValue: [NSString \
185                         stringWithFormat: \
186                         _NS("The latest VLC media player release " \
187                             "is %s (%i MB to download)."), \
188                         p_uit->release.psz_version, ((p_uit->file.l_size \
189                         / 1024) / 1024)]];
190                         
191                     if( o_urlOfBinary )
192                         [o_urlOfBinary release];
193                     o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
194                         p_uit->file.psz_url];
195                 }
196                 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
197                     !releaseChecked )
198                 {
199                     /* our version is outdated, let the user download the new
200                      * release */
201                     [o_fld_status setStringValue: _NS("This version of VLC " \
202                         "is outdated.")];
203                     [o_btn_DownloadNow setEnabled: YES];
204                     msg_Dbg( p_intf, "this version of VLC is outdated" );
205                     /* put the mirror information */
206                     msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
207                             p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
208                             p_uit->mirror.psz_type );
209                     /* make sure that we perform this check only once */
210                     releaseChecked = YES;
211                     /* Make sure the update window is showed in case we have something */
212                     [o_update_window center];
213                     [o_update_window makeKeyAndOrderFront: self];
214
215                 }
216                 else if(! releaseChecked )
217                 {
218                     [o_fld_status setStringValue: _NS("This version of VLC " \
219                         "is latest available.")];
220                     [o_btn_DownloadNow setEnabled: NO];
221                     msg_Dbg( p_intf, "current version is up-to-date" );
222                     releaseChecked = YES;
223                 }
224             }
225             x += 1;
226         }
227
228         update_iterator_Delete( p_uit );
229
230         /* wait for our release notes if necessary, since the download is done
231          * by another thread -- this does usually take 300000 to 500000 ms */
232         if( gettingReleaseNote )
233         {
234             int i = 0;
235             while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
236             {
237                 msleep( 100000 );
238                 i += 1;
239                 if( i == 150 )
240                 {
241                     /* if this takes more than 15 sec, exit */
242                     msg_Warn( p_intf, "download took more than 15 sec, exiting" );
243                     break;
244                 }
245             }
246             msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
247             msleep( 500000 );
248
249             /* let's open our cached release note and display it
250              * we can't use NSString stringWithContentsOfFile:encoding:error: 
251              * since it is Tiger only */
252             NSString * releaseNote = [[NSString alloc] initWithData: \
253                 [NSData dataWithContentsOfFile: pathToReleaseNote] \
254                 encoding: NSISOLatin1StringEncoding];
255             if( releaseNote )
256                 [o_fld_releaseNote setString: releaseNote];
257         
258             /* delete the file since it isn't needed anymore */
259             BOOL myBOOL = NO;
260             myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
261                 pathToReleaseNote handler: nil];
262         }
263         else
264         {
265             /* don't confuse the user, but make her happy */
266             [o_fld_status setStringValue: _NS("This version of VLC " \
267                 "is latest available.")];
268             [o_btn_DownloadNow setEnabled: NO];
269             msg_Dbg( p_intf, "current version is up-to-date" );
270             msg_Warn( p_intf, "retrieving current release notes failed!" );
271         }
272     }
273     [pool release];
274 }
275
276 - (void)performDownload:(NSString *)path
277 {
278     update_iterator_t *p_uit = update_iterator_New( p_u );
279     if( p_uit )
280     {
281         update_iterator_Action( p_uit, UPDATE_MIRROR );
282
283         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
284         {
285             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
286                 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
287                 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
288             {
289                 /* put the mirror information */
290                 msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
291                     p_uit->mirror.psz_name, p_uit->mirror.psz_location, \
292                     p_uit->mirror.psz_type );
293
294                 /* that's our binary */
295                 update_download( p_uit, (char *)[path UTF8String] );
296             }
297         }
298         
299         update_iterator_Delete( p_uit );
300     }
301
302     [o_update_window close];
303 }
304
305 @end