update.m 7.72 KB
Newer Older
1 2 3
/*****************************************************************************
 * update.m: MacOS X Check-For-Update window
 *****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
4
 * Copyright © 2005-2008 the VideoLAN team
5 6
 * $Id$
 *
7
 * Authors: Felix Kühne <fkuehne@users.sf.net>
Rafaël Carré's avatar
Rafaël Carré committed
8
 *          Rafaël Carré <funman@videolanorg>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

25 26
#import "update.h"

27
#ifdef UPDATE_CHECK
28

29 30
#include <assert.h>

31 32 33 34
/*****************************************************************************
 * Preamble
 *****************************************************************************/

35 36
static NSString * kPrefUpdateOnStartup = @"UpdateOnStartup";
static NSString * kPrefUpdateLastTimeChecked = @"UpdateLastTimeChecked";
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/*****************************************************************************
 * VLCExtended implementation
 *****************************************************************************/

@implementation VLCUpdate

static VLCUpdate *_o_sharedInstance = nil;

+ (VLCUpdate *)sharedInstance
{
    return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
}

- (id)init
{
53
    if( _o_sharedInstance ) {
54 55 56
        [self dealloc];
    } else {
        _o_sharedInstance = [super init];
57
        b_checked = false;
Rafaël Carré's avatar
Rafaël Carré committed
58 59 60 61 62 63 64 65 66

        /* clean the interface */
        [o_fld_releaseNote setString: @""];
        [o_fld_currentVersion setString: @""];
        /* 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")];
67 68 69 70 71 72 73
    }

    return _o_sharedInstance;
}

- (void)awakeFromNib
{
Rafaël Carré's avatar
Rafaël Carré committed
74 75
    /* we don't use - (BOOL)shouldCheckUpdateOnStartup because we don't want
     * the Alert panel to pop up at this time */
76 77 78 79 80 81 82 83 84 85 86 87 88
    [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;
89
 
90
    if( ![[NSUserDefaults standardUserDefaults] objectForKey: kPrefUpdateOnStartup] )
91 92
    {
        /* We don't have any preferences stored, ask the user. */
93 94
        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 );
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        [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;
110 111 112 113 114 115 116 117 118
}

- (void)showUpdateWindow
{
    /* show the window and check for a potential update */
    [o_update_window center];
    [o_update_window displayIfNeeded];
    [o_update_window makeKeyAndOrderFront:nil];

Rafaël Carré's avatar
Rafaël Carré committed
119 120 121 122
    if( !b_checked )
    {
        [o_bar_checking startAnimation: self];
        [self checkForUpdate];
123
        b_checked = true;
Rafaël Carré's avatar
Rafaël Carré committed
124 125
        [o_bar_checking stopAnimation: self];
    }
126 127 128 129
}

- (IBAction)download:(id)sender
{
130
    /* provide a save dialogue */
131 132
    SEL sel = @selector(getLocationForSaving:returnCode:contextInfo:);
    NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
Rafaël Carré's avatar
Rafaël Carré committed
133

134 135 136
    [saveFilePanel setRequiredFileType: @"dmg"];
    [saveFilePanel setCanSelectHiddenExtension: YES];
    [saveFilePanel setCanCreateDirectories: YES];
137 138
    update_release_t *p_release = update_GetRelease( p_u );
    assert( p_release );
139
    [saveFilePanel beginSheetForDirectory:nil file:
140
        [[[NSString stringWithUTF8String: p_release->psz_url] componentsSeparatedByString:@"/"] lastObject]
141 142 143 144
                           modalForWindow: o_update_window 
                            modalDelegate:self
                           didEndSelector:sel
                              contextInfo:nil];
145 146
}

147 148 149
- (void)getLocationForSaving: (NSSavePanel *)sheet 
                  returnCode: (int)returnCode 
                 contextInfo: (void *)contextInfo
150
{
151
    if( returnCode == NSOKButton )
152 153
    {
        /* perform download and pass the selected path */
154
        [NSThread detachNewThreadSelector:@selector(performDownload:) toTarget:self withObject:[sheet filename]];
155 156 157 158 159 160
    }
    [sheet release];
}

- (IBAction)okay:(id)sender
{
Rafaël Carré's avatar
Rafaël Carré committed
161 162
    /* just hides the window */
    [o_update_window orderOut: self];
163 164
}

165 166 167 168 169
- (IBAction)changeCheckUpdateOnStartup:(id)sender
{
    [self setShouldCheckUpdate: [sender state]];
}

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
170
- (void)setUpToDate:(NSNumber *)uptodate
171
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
172
    if( [uptodate boolValue] )
173
    {
174 175
        [o_fld_releaseNote setString: @""];
        [o_fld_currentVersion setStringValue: @""];
Rafaël Carré's avatar
Rafaël Carré committed
176 177 178 179 180
        [o_fld_status setStringValue: _NS("This version of VLC is the latest available.")];
        [o_btn_DownloadNow setEnabled: NO];
    }
    else
    {
181 182
        update_release_t *p_release = update_GetRelease( p_u );
        [o_fld_releaseNote setString: [NSString stringWithUTF8String: (p_release->psz_desc ? p_release->psz_desc : "" )]];
Rafaël Carré's avatar
Rafaël Carré committed
183 184
        [o_fld_status setStringValue: _NS("This version of VLC is outdated.")];
        [o_fld_currentVersion setStringValue: [NSString stringWithFormat:
185 186
            _NS("The current release is %d.%d.%d%c."), p_release->i_major,
            p_release->i_minor, p_release->i_revision, p_release->extra]];
Rafaël Carré's avatar
Rafaël Carré committed
187 188 189 190 191
        [o_btn_DownloadNow setEnabled: 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];
192 193 194
    }
}

195
static void updateCallback( void * p_data, bool b_success )
196
{
197 198 199 200 201
    VLCUpdate * update = p_data;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSNumber * state = [NSNumber numberWithBool:!b_success || !update_NeedUpgrade( update->p_u )];
    [update performSelectorOnMainThread:@selector(setUpToDate:) withObject:state waitUntilDone:YES];
    [pool release];
Rafaël Carré's avatar
Rafaël Carré committed
202 203 204 205 206 207 208 209
}

- (void)checkForUpdate
{
    p_u = update_New( VLCIntf );
    if( !p_u )
        return;
    update_Check( p_u, updateCallback, self );
210

211
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rafaël Carré's avatar
Rafaël Carré committed
212
    [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: kPrefUpdateLastTimeChecked];
213
    [pool release];
Rafaël Carré's avatar
Rafaël Carré committed
214 215 216 217
}

- (void)performDownload:(NSString *)path
{
218
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
219
    update_Download( p_u, [path UTF8String] );
Rafaël Carré's avatar
Rafaël Carré committed
220 221
    [o_btn_DownloadNow setEnabled: NO];
    [o_update_window orderOut: self];
222
    update_WaitDownload( p_u );
Rafaël Carré's avatar
Rafaël Carré committed
223
    update_Delete( p_u );
224
    p_u = nil;
225
    [pool release];
226 227 228
}

@end
229 230

#endif