2008-03-29 23:38:38 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2009-01-10 23:37:37 +00:00
|
|
|
* Copyright (c) 2008-2009 Transmission authors and contributors
|
2008-03-29 23:38:38 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2008-08-31 19:47:11 +00:00
|
|
|
#import "BlocklistDownloaderViewController.h"
|
2008-09-01 15:46:00 +00:00
|
|
|
#import "BlocklistScheduler.h"
|
2008-08-31 20:48:10 +00:00
|
|
|
#import "PrefsController.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)
|
|
|
|
|
|
|
|
- (void) startDownload;
|
2008-05-16 00:25:09 +00:00
|
|
|
- (void) finishDownloadSuccess;
|
2008-03-29 23:38:38 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloader
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
BlocklistDownloader * fDownloader = nil;
|
2008-08-31 20:48:10 +00:00
|
|
|
+ (BlocklistDownloader *) downloader
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
if (!fDownloader)
|
|
|
|
{
|
2008-08-31 20:48:10 +00:00
|
|
|
fDownloader = [[BlocklistDownloader alloc] init];
|
2008-08-31 19:47:11 +00:00
|
|
|
[fDownloader startDownload];
|
|
|
|
}
|
|
|
|
|
|
|
|
return fDownloader;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 20:26:41 +00:00
|
|
|
+ (BOOL) isRunning
|
|
|
|
{
|
|
|
|
return fDownloader != nil;
|
|
|
|
}
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
- (void) setViewController: (BlocklistDownloaderViewController *) viewController
|
2008-04-06 04:49:39 +00:00
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
fViewController = viewController;
|
|
|
|
if (fViewController)
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
switch (fState)
|
|
|
|
{
|
|
|
|
case BLOCKLIST_DL_START:
|
|
|
|
[fViewController setStatusStarting];
|
|
|
|
break;
|
|
|
|
case BLOCKLIST_DL_DOWNLOADING:
|
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
|
|
|
break;
|
|
|
|
case BLOCKLIST_DL_PROCESSING:
|
|
|
|
[fViewController setStatusProcessing];
|
|
|
|
break;
|
|
|
|
}
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
2008-04-06 04:49:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[fDownload release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
- (void) cancelDownload
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setFinished];
|
|
|
|
|
2008-04-06 03:47:03 +00:00
|
|
|
[fDownload cancel];
|
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
fDownloader = nil;
|
2008-03-29 23:38:38 +00:00
|
|
|
[self release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_DOWNLOADING;
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
fCurrentSize = 0;
|
|
|
|
fExpectedSize = [response expectedContentLength];
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
|
|
|
|
{
|
2008-03-30 01:51:48 +00:00
|
|
|
fCurrentSize += length;
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
|
|
|
|
{
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setFailed: [error localizedDescription]];
|
2008-03-29 23:38:38 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
fDownloader = nil;
|
|
|
|
[self release];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) downloadDidFinish: (NSURLDownload *) download
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_PROCESSING;
|
2008-12-26 05:57:51 +00:00
|
|
|
[self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloader (Private)
|
|
|
|
|
|
|
|
- (void) startDownload
|
|
|
|
{
|
2008-08-31 21:10:45 +00:00
|
|
|
fState = BLOCKLIST_DL_START;
|
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] cancelSchedule];
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: LIST_URL]];
|
|
|
|
|
|
|
|
fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
|
|
|
|
[fDownload setDestination: DESTINATION allowOverwrite: YES];
|
|
|
|
}
|
|
|
|
|
2008-05-16 00:25:09 +00:00
|
|
|
- (void) finishDownloadSuccess
|
|
|
|
{
|
2008-08-22 22:59:20 +00:00
|
|
|
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setStatusProcessing];
|
2008-05-16 00:25:09 +00:00
|
|
|
|
|
|
|
//process data
|
2008-08-31 20:48:10 +00:00
|
|
|
tr_blocklistSetContent([PrefsController handle], [DESTINATION UTF8String]);
|
2008-05-16 00:25:09 +00:00
|
|
|
|
|
|
|
//delete downloaded file
|
2008-12-26 05:57:51 +00:00
|
|
|
[[NSFileManager defaultManager] removeItemAtPath: DESTINATION error: NULL];
|
2008-05-16 00:25:09 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
[fViewController setFinished];
|
2008-05-16 00:25:09 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
//update last updated date for schedule
|
2008-08-31 20:05:46 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistLastUpdate"];
|
2008-09-01 15:46:00 +00:00
|
|
|
[[BlocklistScheduler scheduler] updateSchedule];
|
2008-08-31 20:05:46 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
|
2008-08-22 22:59:20 +00:00
|
|
|
|
|
|
|
[pool release];
|
2008-03-30 01:51:48 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
fDownloader = nil;
|
2008-03-29 23:38:38 +00:00
|
|
|
[self release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|