transmission/macosx/BlocklistDownloader.m

160 lines
4.6 KiB
Mathematica
Raw Normal View History

2008-03-29 23:38:38 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2008 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "BlocklistDownloader.h"
#import "BlocklistDownloaderViewController.h"
#import "NSApplicationAdditions.h"
2008-03-29 23:38:38 +00:00
#define LIST_URL @"http://download.m0k.org/transmission/files/level1.gz"
#define DESTINATION [NSTemporaryDirectory() stringByAppendingPathComponent: @"level1.gz"]
@interface BlocklistDownloader (Private)
- (id) initWithHandle: (tr_handle *) handle;
2008-03-29 23:38:38 +00:00
- (void) startDownload;
- (void) finishDownloadSuccess;
2008-03-29 23:38:38 +00:00
@end
@implementation BlocklistDownloader
BlocklistDownloader * fDownloader = nil;
+ (BlocklistDownloader *) downloader: (tr_handle *) handle
2008-03-29 23:38:38 +00:00
{
if (!fDownloader)
{
fDownloader = [[BlocklistDownloader alloc] initWithHandle: handle];
[fDownloader startDownload];
}
return fDownloader;
2008-03-29 23:38:38 +00:00
}
- (void) setViewController: (BlocklistDownloaderViewController *) viewController
{
fViewController = viewController;
if (fViewController)
{
#warning set actual status
[fViewController setStatusStarting];
}
}
2008-03-29 23:38:38 +00:00
- (void) dealloc
{
[fDownload release];
[super dealloc];
}
- (void) cancelDownload
2008-03-29 23:38:38 +00:00
{
[fViewController setFinished];
[fDownload cancel];
fDownloader = nil;
2008-03-29 23:38:38 +00:00
[self release];
}
- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
{
fCurrentSize = 0;
fExpectedSize = [response expectedContentLength];
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
2008-03-29 23:38:38 +00:00
}
- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
{
fCurrentSize += length;
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
2008-03-29 23:38:38 +00:00
}
- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
{
[fViewController setFailed: [error localizedDescription]];
2008-03-29 23:38:38 +00:00
fDownloader = nil;
[self release];
2008-03-29 23:38:38 +00:00
}
- (void) downloadDidFinish: (NSURLDownload *) download
{
if ([NSApp isOnLeopardOrBetter])
[self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
else
[self finishDownloadSuccess];
2008-03-29 23:38:38 +00:00
}
@end
@implementation BlocklistDownloader (Private)
- (id) initWithHandle: (tr_handle *) handle
2008-03-29 23:38:38 +00:00
{
if ((self = [super init]))
{
fHandle = handle;
2008-03-29 23:38:38 +00:00
}
return self;
}
- (void) startDownload
{
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: LIST_URL]];
fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
[fDownload setDestination: DESTINATION allowOverwrite: YES];
}
- (void) finishDownloadSuccess
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[fViewController setStatusProcessing];
//process data
tr_blocklistSetContent(fHandle, [DESTINATION UTF8String]);
//delete downloaded file
if ([NSApp isOnLeopardOrBetter])
[[NSFileManager defaultManager] removeItemAtPath: DESTINATION error: NULL];
else
[[NSFileManager defaultManager] removeFileAtPath: DESTINATION handler: nil];
[fViewController setFinished];
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistLastUpdate"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
[pool release];
fDownloader = nil;
2008-03-29 23:38:38 +00:00
[self release];
}
@end