]> git.sesse.net Git - vlc/blob - modules/gui/macosx/update.m
* new CheckForUpdate panel (closes #310)
[vlc] / modules / gui / macosx / update.m
1 /*****************************************************************************
2  * update.m: MacOS X Check-For-Update window
3  *****************************************************************************
4  * Copyright (C) 2005 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     /* clean the interface */
65     [o_fld_releaseNote setString: @""];
66     
67     [self initStrings];
68 }
69
70 - (void)dealloc
71 {
72     if( o_hashOfOurBinary )
73         [o_hashOfOurBinary release];
74     if( o_urlOfBinary )
75         [o_urlOfBinary release];
76     [super dealloc];
77 }
78
79 - (void)initStrings
80 {
81     /* translate strings to the user's language */
82     [o_update_window setTitle: _NS("Check for update")];
83     [o_btn_DownloadNow setTitle: _NS("Download now")];
84     [o_btn_okay setTitle: _NS("OK")];
85 }
86
87 - (void)showUpdateWindow
88 {
89     /* show the window and check for a potential update */
90     [o_fld_status setStringValue: _NS("Checking for Update...")];
91     [o_fld_currentVersionAndSize setStringValue: @""];
92     [o_fld_releaseNote setString: @""];
93
94     [o_update_window center];
95     [o_update_window displayIfNeeded];
96     [o_update_window makeKeyAndOrderFront:nil];
97
98     [o_bar_checking startAnimation: self];
99     [self checkForUpdate];
100     [o_bar_checking stopAnimation: self];
101     [o_lbl_checkForUpdate setHidden: YES];
102 }
103
104 - (IBAction)download:(id)sender
105 {
106     /* enable the following once full notification support is available
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:nil modalForWindow: \
115         o_update_window modalDelegate:self didEndSelector:sel contextInfo:nil];*/
116
117     /* delete this afterwards */
118     [self performDownload: @""];
119 }
120
121 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
122     (int)returnCode contextInfo: (void *)contextInfo
123 {
124     if (returnCode == NSOKButton)
125     {
126         /* perform download and pass the selected path */
127         [self performDownload: [sheet filename]];
128     }
129     [sheet release];
130 }
131
132 - (IBAction)okay:(id)sender
133 {
134     /* just close the window */
135     [o_update_window close];
136 }
137
138 - (void)checkForUpdate
139 {
140     p_u = update_New( VLCIntf );
141     update_Check( p_u, VLC_FALSE );
142     update_iterator_t *p_uit = update_iterator_New( p_u );
143     BOOL releaseChecked = NO;
144     int x = 0;
145     
146     if( p_uit )
147     {
148         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
149         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
150         update_iterator_Action( p_uit, UPDATE_MIRROR );
151         
152         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
153         {
154             /* if the announced item is of the type "binary", keep it and display
155              * its details to the user. Do similar stuff on "info". Do both 
156              * only if the file is announced as stable */
157             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
158             {
159                 if( p_uit->file.i_type == UPDATE_FILE_TYPE_INFO )
160                 {
161                     [o_fld_releaseNote setString: \
162                         [NSString stringWithUTF8String: \
163                         (p_uit->file.psz_description)]];
164                 }
165                 else if( p_uit->file.i_type == UPDATE_FILE_TYPE_BINARY )
166                 {
167                     msg_Dbg( VLCIntf, "binary found, version = %s" \
168                         ", hash=%s", p_uit->release.psz_version, \
169                         p_uit->file.psz_md5 );
170                     [o_fld_currentVersionAndSize setStringValue: \
171                         [NSString stringWithFormat: \
172                         @"The current release is %s (%i MB to download).", \
173                         p_uit->release.psz_version, p_uit->file.l_size]];
174                         
175                     if( o_urlOfBinary )
176                         [o_urlOfBinary release];
177                     o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
178                         p_uit->file.psz_url];
179
180                     /* save the hash of our file, if available */
181                     if( p_uit->file.psz_md5 )
182                     {
183                         if( o_hashOfOurBinary )
184                             [o_hashOfOurBinary release];
185                         o_hashOfOurBinary = [[NSString alloc] \
186                             initWithUTF8String: p_uit->file.psz_md5];
187                     }
188                 }
189                 if( p_uit->release.i_status == UPDATE_RELEASE_STATUS_NEWER &&
190                     !releaseChecked )
191                 {
192                     /* our version is outdated, let the user download the new
193                      * release */
194                     [o_fld_status setStringValue: _NS("Your version of VLC " \
195                         "is outdated.")];
196                     [o_btn_DownloadNow setEnabled: YES];
197                     msg_Dbg( VLCIntf, "this version of VLC is outdated" );
198                     /* put the mirror information */
199                     msg_Dbg( VLCIntf, "used mirror: %s, %s [%s]", \
200                             p_uit->mirror.psz_name, p_uit->mirror.psz_location,\
201                             p_uit->mirror.psz_type );
202                     /* make sure that we perform this check only once */
203                     releaseChecked = YES;
204                 }
205                 else if(! releaseChecked )
206                 {
207                     [o_fld_status setStringValue: _NS("Your version of VLC " \
208                         "is up-to-date.")];
209                     [o_btn_DownloadNow setEnabled: NO];
210                     msg_Dbg( VLCIntf, "current version is up-to-date" );
211                     releaseChecked = YES;
212                 }
213             }
214             x += 1;
215         }
216         update_iterator_Delete( p_uit );
217     }
218 }
219
220 - (void)performDownload:(NSString *)path
221 {
222     /* enable this once notifications are completely available on OSX 
223     update_iterator_t *p_uit = update_iterator_New( p_u );
224     if( p_uit )
225     {
226         update_iterator_Action( p_uit, UPDATE_MIRROR );
227         
228         int i_count = 0;
229         while( update_iterator_Action( p_uit, UPDATE_FILE ) != UPDATE_FAIL )
230         {
231             if( p_uit->file.psz_url == [o_hashOfOurBinary UTF8String] )
232                 break;
233             i_count += 1;
234         }
235         
236         
237         update_download( p_uit, (char *)[path UTF8String] );
238         
239         update_iterator_Delete( p_uit );
240     }*/
241     
242     /* delete the following afterwards */
243     msg_Dbg( VLCIntf, "url is %s, using default browser for download", \
244         [o_urlOfBinary UTF8String] );
245         
246     NSURL * o_url = [NSURL URLWithString: o_urlOfBinary];
247
248     [[NSWorkspace sharedWorkspace] openURL: o_url];
249     
250     [o_update_window close];
251 }
252
253 @end