]> git.sesse.net Git - vlc/blobdiff - src/misc/update.c
web intf: prettify js code.
[vlc] / src / misc / update.c
index 16c5ec660f32ee953e5abe04b1a68c4d83231b1a..a3b9538c12c8cb5b0de8d88c1ac21226e3bb5182 100644 (file)
 #include <vlc_pgpkey.h>
 #include <vlc_stream.h>
 #include <vlc_strings.h>
-#include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_dialog.h>
+#include <vlc_interface.h>
 
 #include <gcrypt.h>
 #include <vlc_gcrypt.h>
-
+#ifdef WIN32
+#include <shellapi.h>
+#endif
 #include "update.h"
 #include "../libvlc.h"
 
 
 /*
  * Here is the format of these "status files" :
- * First line is the last version: "X.Y.Ze" where:
+ * First line is the last version: "X.Y.Z.E" where:
  *      * X is the major number
  *      * Y is the minor number
  *      * Z is the revision number
- *      * e is an OPTIONAL extra letter
- *      * AKA "0.8.6d" or "0.9.0"
- * Second line is an url of the binary for this last version
+ *      * .E is an OPTIONAL extra number
+ *      * IE "1.2.0" or "1.1.10.1"
+ * Second line is a url of the binary for this last version
  * Remaining text is a required description of the update
  */
 
 #if defined( UNDER_CE )
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
 #elif defined( WIN32 )
-#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
-#elif defined( __APPLE__ )
-#   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
-#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
-#   else
-#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
-#   endif
-#elif defined( SYS_BEOS )
-#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
+#  ifndef NDEBUG
+#    define UPDATE_VLC_STATUS_URL "http://update-test.videolan.org/vlc/status-win-x86"
+#  else
+#    define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
+#  endif
 #else
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
 #endif
  * Update_t functions
  *****************************************************************************/
 
+#undef update_New
 /**
  * Create a new update VLC struct
  *
  * \param p_this the calling vlc_object
  * \return pointer to new update_t or NULL
  */
-update_t *__update_New( vlc_object_t *p_this )
+update_t *update_New( vlc_object_t *p_this )
 {
     update_t *p_update;
     assert( p_this );
@@ -134,15 +134,14 @@ void update_Delete( update_t *p_update )
 
     if( p_update->p_check )
     {
-        vlc_object_kill( p_update->p_check );
-        vlc_thread_join( p_update->p_check );
-        vlc_object_release( p_update->p_check );
+        vlc_join( p_update->p_check->thread, NULL );
+        free( p_update->p_check );
     }
 
     if( p_update->p_download )
     {
         vlc_object_kill( p_update->p_download );
-        vlc_thread_join( p_update->p_download );
+        vlc_join( p_update->p_download->thread, NULL );
         vlc_object_release( p_update->p_download );
     }
 
@@ -181,10 +180,6 @@ static void EmptyRelease( update_t *p_update )
 static bool GetUpdateFile( update_t *p_update )
 {
     stream_t *p_stream = NULL;
-    int i_major = 0;
-    int i_minor = 0;
-    int i_revision = 0;
-    unsigned char extra;
     char *psz_version_line = NULL;
     char *psz_update_data = NULL;
 
@@ -224,18 +219,12 @@ static bool GetUpdateFile( update_t *p_update )
     strncpy( psz_version_line, psz_update_data, i_len );
     psz_version_line[i_len] = '\0';
 
-    p_update->release.extra = 0;
-    switch( sscanf( psz_version_line, "%i.%i.%i%c",
-                    &i_major, &i_minor, &i_revision, &extra ) )
+    p_update->release.i_extra = 0;
+    int ret = sscanf( psz_version_line, "%i.%i.%i.%i",
+                    &p_update->release.i_major, &p_update->release.i_minor,
+                    &p_update->release.i_revision, &p_update->release.i_extra);
+    if( ret != 3 && ret != 4 )
     {
-        case 4:
-            p_update->release.extra = extra;
-        case 3:
-            p_update->release.i_major = i_major;
-            p_update->release.i_minor = i_minor;
-            p_update->release.i_revision = i_revision;
-            break;
-        default:
             msg_Err( p_update->p_libvlc, "Update version false formated" );
             goto error;
     }
@@ -274,8 +263,6 @@ static bool GetUpdateFile( update_t *p_update )
     strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
     p_update->release.psz_desc[i_len] = '\0';
 
-    printf("desc %s\n", p_update->release.psz_desc);
-
     /* Now that we know the status is valid, we must download its signature
      * to authenticate it */
     signature_packet_t sign;
@@ -339,7 +326,7 @@ static bool GetUpdateFile( update_t *p_update )
         else
         {
             free( p_hash );
-            msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
+            msg_Err( p_update->p_libvlc, "Key signature invalid !" );
             goto error;
         }
     }
@@ -379,7 +366,7 @@ error:
     return false;
 }
 
-static void* update_CheckReal( vlc_object_t *p_this );
+static void* update_CheckReal( void * );
 
 /**
  * Check for updates
@@ -396,14 +383,11 @@ void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void
     // If the object already exist, destroy it
     if( p_update->p_check )
     {
-        vlc_object_kill( p_update->p_check );
-        vlc_thread_join( p_update->p_check );
-        vlc_object_release( p_update->p_check );
+        vlc_join( p_update->p_check->thread, NULL );
+        free( p_update->p_check );
     }
 
-    update_check_thread_t *p_uct =
-        vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
-                           VLC_OBJECT_GENERIC, "update check" );
+    update_check_thread_t *p_uct = calloc( 1, sizeof( *p_uct ) );
     if( !p_uct ) return;
 
     p_uct->p_update = p_update;
@@ -411,13 +395,12 @@ void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void
     p_uct->pf_callback = pf_callback;
     p_uct->p_data = p_data;
 
-    vlc_thread_create( p_uct, "check for update", update_CheckReal,
-                       VLC_THREAD_PRIORITY_LOW );
+    vlc_clone( &p_uct->thread, update_CheckReal, p_uct, VLC_THREAD_PRIORITY_LOW );
 }
 
-void* update_CheckReal( vlc_object_t* p_this )
+void* update_CheckReal( void *obj )
 {
-    update_check_thread_t *p_uct = (update_check_thread_t *)p_this;
+    update_check_thread_t *p_uct = (update_check_thread_t *)obj;
     bool b_ret;
     int canc;
 
@@ -435,40 +418,39 @@ void* update_CheckReal( vlc_object_t* p_this )
     return NULL;
 }
 
-/**
- * Compare a given release's version number to the current VLC's one
- *
- * \param p_update structure
- * \return true if we have to upgrade to the given version to be up to date
- */
-static bool is_strictly_greater( int * a, int * b, int n)
-{
-    if( n <= 0 ) return false;
-    if(a[0] > b[0] ) return true;
-    if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );
-    /* a[0] < b[0] */ return false;
-}
-
 bool update_NeedUpgrade( update_t *p_update )
 {
     assert( p_update );
 
-    int current_version[] = {
-        *PACKAGE_VERSION_MAJOR - '0',
-        *PACKAGE_VERSION_MINOR - '0',
-        *PACKAGE_VERSION_REVISION - '0',
-        /* extra string of development versions is "-git", "-rc" ..
-         * so make sure version a.b.c is newer than a.b.c-XXX */
-        (*PACKAGE_VERSION_EXTRA == '-') ? -1 : *PACKAGE_VERSION_EXTRA
+    static const int current[4] = {
+        PACKAGE_VERSION_MAJOR,
+        PACKAGE_VERSION_MINOR,
+        PACKAGE_VERSION_REVISION,
+        PACKAGE_VERSION_EXTRA
     };
-    int latest_version[] = {
+    const int latest[4] = {
         p_update->release.i_major,
         p_update->release.i_minor,
         p_update->release.i_revision,
-        p_update->release.extra
+        p_update->release.i_extra
     };
 
-    return is_strictly_greater( latest_version, current_version, 4 );
+    for (unsigned i = 0; i < sizeof latest / sizeof *latest; i++) {
+        /* there is a new version available */
+        if (latest[i] > current[i])
+            return true;
+
+        /* current version is more recent than the latest version ?! */
+        if (latest[i] < current[i])
+            return false;
+    }
+
+    /* current version is not a release, it's a -git or -rc version */
+    if (*PACKAGE_VERSION_DEV)
+        return true;
+
+    /* current version is latest version */
+    return false;
 }
 
 /**
@@ -482,18 +464,18 @@ static char *size_str( long int l_size )
     char *psz_tmp = NULL;
     int i_retval = 0;
     if( l_size >> 30 )
-        i_retval = asprintf( &psz_tmp, _("%.1f GB"), (float)l_size/(1<<30) );
+        i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
     else if( l_size >> 20 )
-        i_retval = asprintf( &psz_tmp, _("%.1f MB"), (float)l_size/(1<<20) );
+        i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
     else if( l_size >> 10 )
-        i_retval = asprintf( &psz_tmp, _("%.1f kB"), (float)l_size/(1<<10) );
+        i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
     else
         i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
 
     return i_retval == -1 ? NULL : psz_tmp;
 }
 
-static void* update_DownloadReal( vlc_object_t *p_this );
+static void* update_DownloadReal( void * );
 
 /**
  * Download the file given in the update_t
@@ -510,13 +492,13 @@ void update_Download( update_t *p_update, const char *psz_destdir )
     if( p_update->p_download )
     {
         vlc_object_kill( p_update->p_download );
-        vlc_thread_join( p_update->p_download );
+        vlc_join( p_update->p_download->thread, NULL );
         vlc_object_release( p_update->p_download );
     }
 
     update_download_thread_t *p_udt =
         vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
-                           VLC_OBJECT_GENERIC, "update download" );
+                           "update download" );
     if( !p_udt )
         return;
 
@@ -524,18 +506,17 @@ void update_Download( update_t *p_update, const char *psz_destdir )
     p_update->p_download = p_udt;
     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
 
-    vlc_thread_create( p_udt, "download update", update_DownloadReal,
-                       VLC_THREAD_PRIORITY_LOW );
+    vlc_clone( &p_udt->thread, update_DownloadReal, p_udt, VLC_THREAD_PRIORITY_LOW );
 }
 
-static void* update_DownloadReal( vlc_object_t *p_this )
+static void* update_DownloadReal( void *obj )
 {
-    update_download_thread_t *p_udt = (update_download_thread_t *)p_this;
+    update_download_thread_t *p_udt = (update_download_thread_t *)obj;
     dialog_progress_bar_t *p_progress = NULL;
     long int l_size;
     long int l_downloaded = 0;
     float f_progress;
-    char *psz_status = NULL;
+    char *psz_status;
     char *psz_downloaded = NULL;
     char *psz_size = NULL;
     char *psz_destfile = NULL;
@@ -576,7 +557,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
         goto end;
 
-    p_file = utf8_fopen( psz_destfile, "w" );
+    p_file = vlc_fopen( psz_destfile, "w" );
     if( !p_file )
     {
         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
@@ -636,17 +617,12 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( vlc_object_alive( p_udt ) &&
         !dialog_ProgressCancelled( p_progress ) )
     {
-        if( asprintf( &psz_status, _("%s\nDone %s (100.0%%)"),
-            p_update->release.psz_url, psz_size ) != -1 )
-        {
-            dialog_ProgressDestroy( p_progress );
-            p_progress = NULL;
-            free( psz_status );
-        }
+        dialog_ProgressDestroy( p_progress );
+        p_progress = NULL;
     }
     else
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         goto end;
     }
 
@@ -654,7 +630,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( download_signature( VLC_OBJECT( p_udt ), &sign,
             p_update->release.psz_url ) != VLC_SUCCESS )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
 
         dialog_FatalWait( p_udt, _("File could not be verified"),
             _("It was not possible to download a cryptographic signature for "
@@ -666,7 +642,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
 
     if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature issuer" );
         dialog_FatalWait( p_udt, _("Invalid signature"),
             _("The cryptographic signature for the downloaded file \"%s\" was "
@@ -678,7 +654,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
 
     if( sign.type != BINARY_SIGNATURE )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature type" );
         dialog_FatalWait( p_udt, _("Invalid signature"),
             _("The cryptographic signature for the downloaded file \"%s\" was "
@@ -692,7 +668,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( !p_hash )
     {
         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File not verifiable"),
             _("It was not possible to securely verify the downloaded file"
               " \"%s\". Thus, it was deleted."),
@@ -704,7 +680,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( p_hash[0] != sign.hash_verification[0] ||
         p_hash[1] != sign.hash_verification[1] )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
@@ -716,7 +692,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
             != VLC_SUCCESS )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
@@ -728,6 +704,20 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     msg_Info( p_udt, "%s authenticated", psz_destfile );
     free( p_hash );
 
+#ifdef WIN32
+    int answer = dialog_Question( p_udt, _("Update VLC media player"),
+    _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
+    _("Install"), _("Cancel"), NULL);
+
+    if(answer == 1)
+    {
+        wchar_t psz_wdestfile[MAX_PATH];
+        MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
+        answer = (int)ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
+        if(answer > 32)
+            libvlc_Quit(p_udt->p_libvlc);
+    }
+#endif
 end:
     if( p_progress )
         dialog_ProgressDestroy( p_progress );
@@ -750,7 +740,8 @@ update_release_t *update_GetRelease( update_t *p_update )
 }
 
 #else
-update_t *__update_New( vlc_object_t *p_this )
+#undef update_New
+update_t *update_New( vlc_object_t *p_this )
 {
     (void)p_this;
     return NULL;