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