1 /*****************************************************************************
2 * update.c: VLC update and plugins download
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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 release 2 of the License, or
12 * (at your option) any later release.
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.
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 *****************************************************************************/
26 * This file contains functions related to VLC and plugins update management
29 /*****************************************************************************
31 *****************************************************************************/
35 #include <ctype.h> /* tolower() */
39 #include <vlc_update.h>
41 #include <vlc_block.h>
42 #include <vlc_stream.h>
43 #include <vlc_interface.h>
44 #include <vlc_charset.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 //#define UPDATE_VLC_STATUS_URL "http://zen.via.ecp.fr/~ivoire/videolan/update"
52 #if defined( UNDER_CE )
53 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
54 #elif defined( WIN32 )
55 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
56 #elif defined( __APPLE__ )
57 # define UPDATE_VLC_OS "macosx"
58 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
59 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
61 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
63 #elif defined( SYS_BEOS )
64 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
66 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
69 #define STRDUP( a ) ( a ? strdup( a ) : NULL )
72 /*****************************************************************************
74 *****************************************************************************/
75 static void EmptyRelease( update_t *p_update );
76 static void GetUpdateFile( update_t *p_update );
77 static int cmp( int i1, int i2 );
78 static int CompareReleases( const struct update_release_t *p1,
79 const struct update_release_t *p2 );
82 /*****************************************************************************
84 *****************************************************************************/
87 * Create a new update VLC struct
89 * \param p_this the calling vlc_object
90 * \return pointer to new update_t or NULL
92 update_t *__update_New( vlc_object_t *p_this )
96 if( p_this == NULL ) return NULL;
98 p_update = (update_t *)malloc( sizeof( update_t ) );
99 if( !p_update ) return NULL;
101 vlc_mutex_init( p_this, &p_update->lock );
103 p_update->p_libvlc = p_this->p_libvlc;
105 p_update->release.psz_svnrev = NULL;
106 p_update->release.psz_extra = NULL;
107 p_update->release.psz_url = NULL;
108 p_update->release.psz_desc = NULL;
114 * Delete an update_t struct
116 * \param p_update update_t* pointer
119 void update_Delete( update_t *p_update )
123 vlc_mutex_destroy( &p_update->lock );
125 if( p_update->release.psz_svnrev )
127 free( p_update->release.psz_svnrev );
128 p_update->release.psz_svnrev = NULL;
131 if( p_update->release.psz_extra )
133 free( p_update->release.psz_extra );
134 p_update->release.psz_extra = NULL;
137 if( p_update->release.psz_url )
139 free( p_update->release.psz_url );
140 p_update->release.psz_url = NULL;
143 if( p_update->release.psz_desc )
145 free( p_update->release.psz_desc );
146 p_update->release.psz_desc = NULL;
153 * Empty the release struct
155 * \param p_update update_t* pointer
158 static void EmptyRelease( update_t *p_update )
160 p_update->release.i_major = 0;
161 p_update->release.i_minor = 0;
162 p_update->release.i_revision = 0;
164 if( p_update->release.psz_svnrev )
166 free( p_update->release.psz_svnrev );
167 p_update->release.psz_svnrev = NULL;
170 if( p_update->release.psz_extra )
172 free( p_update->release.psz_extra );
173 p_update->release.psz_extra = NULL;
176 if( p_update->release.psz_url )
178 free( p_update->release.psz_url );
179 p_update->release.psz_url = NULL;
182 if( p_update->release.psz_desc )
184 free( p_update->release.psz_desc );
185 p_update->release.psz_desc = NULL;
190 * Get the update file and parse it
191 * *p_update has to be unlocked when calling this function
193 * \param p_update pointer to update struct
196 static void GetUpdateFile( update_t *p_update )
198 stream_t *p_stream = NULL;
202 char *psz_extra = NULL;
203 char *psz_svnrev = NULL;
204 char *psz_line = NULL;
206 vlc_mutex_lock( &p_update->lock );
208 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
211 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
212 UPDATE_VLC_STATUS_URL );
216 /* Try to read three lines */
217 if( !( psz_line = stream_ReadLine( p_stream ) ) )
219 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
220 UPDATE_VLC_STATUS_URL );
224 /* first line : version number */
225 if( sscanf( psz_line, "%i.%i.%i%as %as", &i_major, &i_minor, &i_revision, &psz_extra, &psz_svnrev ) )
227 p_update->release.i_major = i_major;
228 p_update->release.i_minor = i_minor;
229 p_update->release.i_revision = i_revision;
232 p_update->release.psz_svnrev = psz_svnrev;
234 p_update->release.psz_svnrev = STRDUP( "" );
237 p_update->release.psz_extra = psz_extra;
239 p_update->release.psz_extra = STRDUP( "" );
243 msg_Err( p_update->p_libvlc, "Update version false formated" );
248 /* Second line : URL */
249 if( !( psz_line = stream_ReadLine( p_stream ) ) )
251 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
252 UPDATE_VLC_STATUS_URL );
255 p_update->release.psz_url = psz_line;
258 /* Third line : description */
259 if( !( psz_line = stream_ReadLine( p_stream ) ) )
261 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
262 UPDATE_VLC_STATUS_URL );
265 p_update->release.psz_desc = psz_line;
268 vlc_mutex_unlock( &p_update->lock );
271 stream_Delete( p_stream );
277 * \param p_update pointer to update struct
280 void update_Check( update_t *p_update )
284 EmptyRelease( p_update );
286 GetUpdateFile( p_update );
290 * Compare two integers
292 * \param p1 first integer
293 * \param p2 second integer
294 * \return like strcmp
296 static int cmp( int i1, int i2 )
307 * Compare two release numbers
309 * \param p1 first release
310 * \param p2 second release
311 * \return UpdateReleaseStatus(Older|Equal|Newer)
313 static int CompareReleases( const struct update_release_t *p1,
314 const struct update_release_t *p2 )
317 if( ( d = cmp( p1->i_major, p2->i_major ) ) ) ;
318 else if( ( d = cmp( p1->i_minor, p2->i_minor ) ) ) ;
319 else if( ( d = cmp( p1->i_revision, p2->i_revision ) ) ) ;
322 if( p1->psz_extra[0] == '-' )
324 if( p2->psz_extra[0] == '-' )
325 d = strcmp( p1->psz_extra, p2->psz_extra );
331 if( p2->psz_extra[0] == '-' )
334 d = strcmp(p1->psz_extra, p2->psz_extra );
337 d = strcmp( p1->psz_svnrev, p2->psz_svnrev );
341 return UpdateReleaseStatusOlder;
343 return UpdateReleaseStatusEqual;
345 return UpdateReleaseStatusNewer;
349 * Compare a given release's version number to the current VLC's one
352 * \return UpdateReleaseStatus(Older|Equal|Newer)
354 int update_CompareReleaseToCurrent( update_t *p_update )
358 struct update_release_t c;
363 int i_result = UpdateReleaseStatusOlder;
365 /* get the current version number */
366 if( sscanf( PACKAGE_VERSION, "%i.%i.%i%as", &i_major, &i_minor, &i_revision, &psz_extra ) )
370 c.i_revision = i_revision;
372 c.psz_extra = psz_extra;
374 c.psz_extra = STRDUP( "" );
375 c.psz_svnrev = STRDUP( VLC_Changeset() );
377 i_result = CompareReleases( &p_update->release, &c );
380 free( c.psz_svnrev );