]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
* download the updated versions through VLC and not through your default browser
[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_hashOfOurBinary )
76         [o_hashOfOurBinary release];
77     if( o_urlOfBinary )
78         [o_urlOfBinary release];
79     if( o_indexOfOurBinary )
80         [o_indexOfOurBinary release];
81
82     [super dealloc];
83 }
84
85 - (void)initStrings
86 {
87     /* translate strings to the user's language */
88     [o_update_window setTitle: _NS("Check for Update")];
89     [o_btn_DownloadNow setTitle: _NS("Download now")];
90     [o_btn_okay setTitle: _NS("OK")];
91 }
92
93 - (void)showUpdateWindow
94 {
95     /* show the window and check for a potential update */
96     [o_fld_status setStringValue: _NS("Checking for Update...")];
97     [o_fld_currentVersionAndSize setStringValue: @""];
98     [o_fld_releaseNote setString: @""];
99
100     [o_update_window center];
101     [o_update_window displayIfNeeded];
102     [o_update_window makeKeyAndOrderFront:nil];
103
104     [o_bar_checking startAnimation: self];
105     [self checkForUpdate];
106     [o_bar_checking stopAnimation: self];
107 }
108
109 - (IBAction)download:(id)sender
110 {
111     /* provide a save dialogue */
112     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
113     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
114     
115     [saveFilePanel setRequiredFileType: @"dmg"];
116     [saveFilePanel setCanSelectHiddenExtension: YES];
117     [saveFilePanel setCanCreateDirectories: YES];
118     [saveFilePanel beginSheetForDirectory:nil file: \
119         [[o_urlOfBinary componentsSeparatedByString:@"/"] lastObject] \
120         modalForWindow: o_update_window modalDelegate:self didEndSelector:sel \
121         contextInfo:nil];
122 }
123
124 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
125     (int)returnCode contextInfo: (void *)contextInfo
126 {
127     if (returnCode == NSOKButton)
128     {
129         /* perform download and pass the selected path */
130         [self performDownload: [sheet filename]];
131     }
132     [sheet release];
133 }
134
135 - (IBAction)okay:(id)sender
136 {
137     /* just close the window */
138     [o_update_window close];
139 }
140
141 - (void)checkForUpdate
142 {
143     p_u = update_New( p_intf );
144     update_Check( p_u, VLC_FALSE );
145     update_iterator_t *p_uit = update_iterator_New( p_u );
146     BOOL releaseChecked = NO;
147     int x = 0;
148     NSString * pathToReleaseNote;
149     pathToReleaseNote = [[NSString stringWithString: \
150         @"~/Library/Caches/vlc_releasenote_temp.txt"] \
151         stringByExpandingTildeInPath];
152     
153     if( p_uit )
154     {
155         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
156         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
157         update_iterator_Action( p_uit, UPDATE_MIRROR );
158         
159         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
160         {
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                     [o_fld_releaseNote setString: \
169                         [NSString stringWithUTF8String: \
170                         (p_uit->file.psz_description)]];
171                     /* download our release note
172                      * We will read the temp file after this loop */
173                     update_download( p_uit, (char *)[pathToReleaseNote UTF8String] );
174                 }
175                 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
176                 {
177                     msg_Dbg( p_intf, "binary found, version = %s" \
178                         ", hash=%s, size=%i MB, position in release file list=%i",\
179                         p_uit->release.psz_version, p_uit->file.psz_md5, \
180                         (int)((p_uit->file.l_size / 1024) / 1024), \
181                         p_uit->i_f);
182                     [o_fld_currentVersionAndSize setStringValue: \
183                         [NSString stringWithFormat: \
184                         @"The current release is %s (%i MB to download).", \
185                         p_uit->release.psz_version, ((p_uit->file.l_size \
186                         / 1024) / 1024)]];
187                         
188                     if( o_urlOfBinary )
189                         [o_urlOfBinary release];
190                     o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
191                         p_uit->file.psz_url];
192
193                     /* save the hash of our file, if available */
194                     if( p_uit->file.psz_md5 )
195                     {
196                         if( o_hashOfOurBinary )
197                             [o_hashOfOurBinary release];
198                         o_hashOfOurBinary = [[NSString alloc] \
199                             initWithUTF8String: p_uit->file.psz_md5];
200                     }
201                     
202                     if( p_uit->i_f )
203                     {
204                         if( o_indexOfOurBinary )
205                             [o_indexOfOurBinary release];
206                         o_indexOfOurBinary = [[NSNumber alloc] \
207                             initWithInt: p_uit->i_f];
208                     }
209                 }
210                 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
211                     !releaseChecked )
212                 {
213                     /* our version is outdated, let the user download the new
214                      * release */
215                     [o_fld_status setStringValue: _NS("Your version of VLC " \
216                         "is outdated.")];
217                     [o_btn_DownloadNow setEnabled: YES];
218                     msg_Dbg( p_intf, "this version of VLC is outdated" );
219                     /* put the mirror information */
220                     msg_Dbg( p_intf, "used mirror: %s, %s [%s]", \
221                             p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
222                             p_uit->mirror.psz_type );
223                     /* make sure that we perform this check only once */
224                     releaseChecked = YES;
225                 }
226                 else if(! releaseChecked )
227                 {
228                     [o_fld_status setStringValue: _NS("Your version of VLC " \
229                         "is up-to-date.")];
230                     [o_btn_DownloadNow setEnabled: NO];
231                     msg_Dbg( p_intf, "current version is up-to-date" );
232                     releaseChecked = YES;
233                 }
234             }
235             x += 1;
236         }
237
238         update_iterator_Delete( p_uit );
239         
240         /* wait for our download, since it is done by another thread
241          * this does usually take 300000 to 500000 ms */
242         int i = 0;
243         while( [[NSFileManager defaultManager] fileExistsAtPath: pathToReleaseNote] == NO )
244         {
245             msleep( 100000 );
246             i += 1;
247             if( i == 600 )
248             {
249                 /* if this takes more than 1 min, exit */
250                 msg_Warn( p_intf, "download took more than a minute, exiting" );
251                 return;
252             }
253         }
254         msg_Dbg( p_intf, "waited %i ms for the release notes", (i * 100000) );
255         msleep( 500000 );
256
257         /* let's open our cached release note and display it
258          * we can't use NSString stringWithContentsOfFile:encoding:error: 
259          * since it is Tiger only */
260         NSString * releaseNote = [[NSString alloc] initWithData: \
261             [NSData dataWithContentsOfFile: pathToReleaseNote] \
262             encoding: NSISOLatin1StringEncoding];
263         if( releaseNote )
264             [o_fld_releaseNote setString: releaseNote];
265
266         /* delete the file since it isn't needed anymore */
267         BOOL myBOOL = NO;
268         myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
269             pathToReleaseNote handler: nil];
270     }
271 }
272
273 - (void)performDownload:(NSString *)path
274 {
275     update_iterator_t *p_uit = update_iterator_New( p_u );
276     if( p_uit )
277     {
278         update_iterator_Action( p_uit, UPDATE_MIRROR );
279
280         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
281         {
282             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE &&
283                 p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
284                 p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
285             {
286                 /* that's our binary */
287                 update_download( p_uit, (char *)[path UTF8String] );
288             }
289         }
290         
291         update_iterator_Delete( p_uit );
292     }
293
294     [o_update_window close];
295 }
296
297 @end