]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/update.m
Removes trailing spaces. Removes tabs.
[vlc] / modules / gui / macosx / update.m
index 39f974583906926b7b4d2e9f3c80c3eb462c7294..ef11e12224c5a494b35675b29b7a44c65faf6c88 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * update.m: MacOS X Check-For-Update window
  *****************************************************************************
- * Copyright (C) 2005-2006 the VideoLAN team
+ * Copyright (C) 2005-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Felix K\9fhne <fkuehne@users.sf.net>
@@ -23,8 +23,8 @@
 
 
 /*****************************************************************************
- * Note: the code used to communicate with VLC's core was inspired by 
- * ../wxwidgets/dialogs/updatevlc.cpp, written by Antoine Cellerier. 
+ * Note: the code used to communicate with VLC's core was inspired by
+ * ../wxwidgets/dialogs/updatevlc.cpp, written by Antoine Cellerier.
  *****************************************************************************/
 
 
@@ -34,6 +34,8 @@
 #import "update.h"
 #import "intf.h"
 
+static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
+static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
 
 /*****************************************************************************
  * VLCExtended implementation
@@ -50,7 +52,7 @@ static VLCUpdate *_o_sharedInstance = nil;
 
 - (id)init
 {
-    if (_o_sharedInstance) {
+    if( _o_sharedInstance ) {
         [self dealloc];
     } else {
         _o_sharedInstance = [super init];
@@ -66,8 +68,8 @@ static VLCUpdate *_o_sharedInstance = nil;
 
     /* clean the interface */
     [o_fld_releaseNote setString: @""];
-    
-    [self initStrings];
+    [self initInterface];
 }
 
 - (void)dealloc
@@ -78,12 +80,49 @@ static VLCUpdate *_o_sharedInstance = nil;
     [super dealloc];
 }
 
-- (void)initStrings
+- (void)initInterface
 {
     /* translate strings to the user's language */
     [o_update_window setTitle: _NS("Check for Updates")];
     [o_btn_DownloadNow setTitle: _NS("Download now")];
     [o_btn_okay setTitle: _NS("OK")];
+    [o_chk_updateOnStartup setTitle: _NS("Automatically check for updates")];
+    /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want the Alert
+     * panel to pop up at this time */
+    [o_chk_updateOnStartup setState: [[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup]];
+}
+
+- (void)setShouldCheckUpdate: (BOOL)check
+{
+    [[NSUserDefaults standardUserDefaults] setBool: check forKey: kPrefUpdateOnStartup];
+    [o_chk_updateOnStartup setState: check];
+}
+
+- (BOOL)shouldCheckForUpdate
+{
+    NSDate *o_last_update;
+    NSDate *o_next_update;
+    if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
+    {
+        /* We don't have any preferences stored, ask the user. */
+        int res = NSRunInformationalAlertPanel( _NS("Do you want VLC to check for updates automatically?"),
+              _NS("You can change this option in VLC's update window later on."), _NS("Yes"), _NS("No"), nil );
+        [self setShouldCheckUpdate: res];
+    }
+
+    if( ![[NSUserDefaults standardUserDefaults] boolForKey: kPrefUpdateOnStartup] )
+        return NO;
+
+    o_last_update = [[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateLastTimeChecked];
+    if( !o_last_update )
+        return YES;
+
+    o_next_update = [[[NSDate alloc] initWithTimeInterval: 60*60*24*2 /* every two days */ sinceDate: o_last_update] autorelease];
+    if( !o_next_update )
+        return YES;
+
+    return [o_next_update compare: [NSDate date]] == NSOrderedAscending;
 }
 
 - (void)showUpdateWindow
@@ -107,7 +146,7 @@ static VLCUpdate *_o_sharedInstance = nil;
     /* provide a save dialogue */
     SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
     NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
-    
     [saveFilePanel setRequiredFileType: @"dmg"];
     [saveFilePanel setCanSelectHiddenExtension: YES];
     [saveFilePanel setCanCreateDirectories: YES];
@@ -120,7 +159,7 @@ static VLCUpdate *_o_sharedInstance = nil;
 - (void)getLocationForSaving: (NSSavePanel *)sheet returnCode: \
     (int)returnCode contextInfo: (void *)contextInfo
 {
-    if (returnCode == NSOKButton)
+    if( returnCode == NSOKButton )
     {
         /* perform download and pass the selected path */
         [self performDownload: [sheet filename]];
@@ -134,8 +173,15 @@ static VLCUpdate *_o_sharedInstance = nil;
     [o_update_window close];
 }
 
+- (IBAction)changeCheckUpdateOnStartup:(id)sender
+{
+    [self setShouldCheckUpdate: [sender state]];
+}
+
 - (void)checkForUpdate
 {
+    /* We may not run on first thread */
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     p_u = update_New( p_intf );
     update_Check( p_u, VLC_FALSE );
     update_iterator_t *p_uit = update_iterator_New( p_u );
@@ -146,17 +192,19 @@ static VLCUpdate *_o_sharedInstance = nil;
     pathToReleaseNote = [NSString stringWithFormat: \
         @"/tmp/vlc_releasenote_%d.tmp", mdate()];
 
+    [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
+
     if( p_uit )
     {
         p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
         p_uit->i_t = UPDATE_FILE_TYPE_ALL;
         update_iterator_Action( p_uit, UPDATE_MIRROR );
-        
         while( update_iterator_Action( p_uit, UPDATE_FILE) != UPDATE_FAIL )
         {
             msg_Dbg( p_intf, "parsing available updates, run %i", x );
             /* if the announced item is of the type "binary", keep it and display
-             * its details to the user. Do similar stuff on "info". Do both 
+             * its details to the user. Do similar stuff on "info". Do both
              * only if the file is announced as stable */
             if( p_uit->release.i_type == UPDATE_RELEASE_TYPE_STABLE )
             {
@@ -184,7 +232,7 @@ static VLCUpdate *_o_sharedInstance = nil;
                             "is %s (%i MB to download)."), \
                         p_uit->release.psz_version, ((p_uit->file.l_size \
                         / 1024) / 1024)]];
-                        
                     if( o_urlOfBinary )
                         [o_urlOfBinary release];
                     o_urlOfBinary = [[NSString alloc] initWithUTF8String: \
@@ -207,13 +255,14 @@ static VLCUpdate *_o_sharedInstance = nil;
                     releaseChecked = YES;
                     /* Make sure the update window is showed in case we have something */
                     [o_update_window center];
+                    [o_update_window displayIfNeeded];
                     [o_update_window makeKeyAndOrderFront: self];
 
                 }
                 else if(! releaseChecked )
                 {
                     [o_fld_status setStringValue: _NS("This version of VLC " \
-                        "is latest available.")];
+                        "is the latest available.")];
                     [o_btn_DownloadNow setEnabled: NO];
                     msg_Dbg( p_intf, "current version is up-to-date" );
                     releaseChecked = YES;
@@ -244,14 +293,14 @@ static VLCUpdate *_o_sharedInstance = nil;
             msleep( 500000 );
 
             /* let's open our cached release note and display it
-             * we can't use NSString stringWithContentsOfFile:encoding:error: 
+             * we can't use NSString stringWithContentsOfFile:encoding:error:
              * since it is Tiger only */
             NSString * releaseNote = [[NSString alloc] initWithData: \
                 [NSData dataWithContentsOfFile: pathToReleaseNote] \
                 encoding: NSISOLatin1StringEncoding];
             if( releaseNote )
                 [o_fld_releaseNote setString: releaseNote];
-        
             /* delete the file since it isn't needed anymore */
             BOOL myBOOL = NO;
             myBOOL = [[NSFileManager defaultManager] removeFileAtPath: \
@@ -261,12 +310,12 @@ static VLCUpdate *_o_sharedInstance = nil;
         {
             /* don't confuse the user, but make her happy */
             [o_fld_status setStringValue: _NS("This version of VLC " \
-                "is latest available.")];
+                "is the latest available.")];
             [o_btn_DownloadNow setEnabled: NO];
             msg_Dbg( p_intf, "current version is up-to-date" );
-            msg_Warn( p_intf, "retrieving current release notes failed!" );
         }
     }
+    [pool release];
 }
 
 - (void)performDownload:(NSString *)path
@@ -291,7 +340,7 @@ static VLCUpdate *_o_sharedInstance = nil;
                 update_download( p_uit, (char *)[path UTF8String] );
             }
         }
-        
         update_iterator_Delete( p_uit );
     }