3 |
// BathyScaphe |
// BathyScaphe |
4 |
// |
// |
5 |
// Created by Hori,Masaki on 06/08/06. |
// Created by Hori,Masaki on 06/08/06. |
6 |
// Copyright 2006-2011 BathyScaphe Project. All rights reserved. |
// Copyright 2006-2012 BathyScaphe Project. All rights reserved. |
7 |
// encoding="UTF-8" |
// encoding="UTF-8" |
8 |
// |
// |
9 |
|
|
14 |
#import "BSDownloadTask.h" |
#import "BSDownloadTask.h" |
15 |
#import "BSDBThreadsListDBUpdateTask2.h" |
#import "BSDBThreadsListDBUpdateTask2.h" |
16 |
|
|
|
#import "CMRTaskManager.h" |
|
|
#import "CMXWorkerContext.h" |
|
17 |
#import "AppDefaults.h" |
#import "AppDefaults.h" |
18 |
#import "BoardManager.h" |
#import "BoardManager.h" |
19 |
#import "CMRHostHandler.h" |
#import "CMRHostHandler.h" |
20 |
|
|
21 |
NSString *const ThreadsListDownloaderShouldRetryUpdateNotification = @"ThreadsListDownloaderShouldRetryUpdateNotification"; |
NSString *const ThreadsListDownloaderShouldRetryUpdateNotification = @"ThreadsListDownloaderShouldRetryUpdateNotification"; |
22 |
|
|
23 |
|
|
24 |
|
@interface BSThreadsListOPTask () |
25 |
|
@property (retain) NSURL *URL; |
26 |
|
@property (assign) BSDBThreadList *targetList; |
27 |
|
@end |
28 |
|
|
29 |
@implementation BSThreadsListOPTask |
@implementation BSThreadsListOPTask |
30 |
|
@synthesize URL = targetURL; |
31 |
|
@synthesize targetList = m_targetList; |
32 |
|
|
33 |
+ (id)taskWithThreadList:(BSDBThreadList *)list forceDownload:(BOOL)forceDownload |
+ (id)taskWithThreadList:(BSDBThreadList *)list forceDownload:(BOOL)forceDownload |
34 |
{ |
{ |
35 |
return [[[[self class] alloc] initWithThreadList:list forceDownload:forceDownload rebuild:NO] autorelease]; |
return [[[[self class] alloc] initWithThreadList:list forceDownload:forceDownload rebuild:NO] autorelease]; |
38 |
- (id)initWithThreadList:(BSDBThreadList *)list forceDownload:(BOOL)forceDownload rebuild:(BOOL)flag |
- (id)initWithThreadList:(BSDBThreadList *)list forceDownload:(BOOL)forceDownload rebuild:(BOOL)flag |
39 |
{ |
{ |
40 |
if (self = [super init]) { |
if (self = [super init]) { |
41 |
m_targetList = list; |
self.targetList = list; |
42 |
m_forceDL = forceDownload; |
m_forceDL = forceDownload; |
43 |
isRebuilding = flag; |
isRebuilding = flag; |
44 |
[self setBoardName:[list boardName]]; |
self.boardName = [list boardName]; |
45 |
|
|
46 |
if (![self boardName]) goto fail; |
if (!self.boardName) goto fail; |
47 |
} |
} |
48 |
|
|
49 |
return self; |
return self; |
67 |
} |
} |
68 |
|
|
69 |
#pragma mark- |
#pragma mark- |
|
- (void)setURL:(NSURL *)url |
|
|
{ |
|
|
id temp = targetURL; |
|
|
targetURL = [url retain]; |
|
|
[temp release]; |
|
|
} |
|
|
|
|
|
- (NSURL *)url |
|
|
{ |
|
|
return targetURL; |
|
|
} |
|
70 |
|
|
71 |
- (void)setBoardName:(NSString *)name |
- (void)setBoardName:(NSString *)name |
72 |
{ |
{ |
79 |
isLivedoor = host ? is_jbbs_livedoor(host) : NO; |
isLivedoor = host ? is_jbbs_livedoor(host) : NO; |
80 |
|
|
81 |
id temp = bbsName; |
id temp = bbsName; |
82 |
bbsName = [name retain]; |
bbsName = [name copy]; |
83 |
[temp release]; |
[temp release]; |
84 |
|
|
85 |
[self setURL:u]; |
self.URL = u; |
86 |
} |
} |
87 |
|
|
88 |
- (NSString *)boardName |
- (NSString *)boardName |
95 |
{ |
{ |
96 |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
97 |
|
|
98 |
dlTask = [[BSDownloadTask alloc] initWithURL:[self url]]; |
dlTask = [[BSDownloadTask alloc] initWithURL:self.URL]; |
99 |
|
|
100 |
[nc addObserver:self |
[nc addObserver:self |
101 |
selector:@selector(dlDidFinishDownloadNotification:) |
selector:@selector(dlDidFinishDownloadNotification:) |
125 |
{ |
{ |
126 |
BoardManager *bm = [BoardManager defaultManager]; |
BoardManager *bm = [BoardManager defaultManager]; |
127 |
NSError *error; |
NSError *error; |
128 |
if ([bm tryToDetectMovedBoard:[self boardName] error:&error]) { |
if ([bm tryToDetectMovedBoard:self.boardName error:&error]) { |
129 |
UTILNotifyName(ThreadsListDownloaderShouldRetryUpdateNotification); |
UTILNotifyName(ThreadsListDownloaderShouldRetryUpdateNotification); |
130 |
} else { |
} else { |
131 |
NSAlert *alert = [NSAlert alertWithError:error]; |
NSAlert *alert = [NSAlert alertWithError:error]; |
155 |
|
|
156 |
NSString *message = [NSString stringWithFormat: |
NSString *message = [NSString stringWithFormat: |
157 |
NSLocalizedStringFromTable(APP_TLIST_NOT_FOUND_MSG_FMT, @"ThreadsList", nil), |
NSLocalizedStringFromTable(APP_TLIST_NOT_FOUND_MSG_FMT, @"ThreadsList", nil), |
158 |
[targetURL absoluteString]]; |
[self.URL absoluteString]]; |
159 |
|
|
160 |
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
161 |
[alert setAlertStyle:NSWarningAlertStyle]; |
[alert setAlertStyle:NSWarningAlertStyle]; |
169 |
} |
} |
170 |
|
|
171 |
#pragma mark- |
#pragma mark- |
172 |
- (void)doExecuteWithLayout:(CMRThreadLayout *)layout |
- (void)excute |
173 |
{ |
{ |
174 |
[self checkIsInterrupted]; |
if(self.isInterrupted) goto abort; |
175 |
if ([CMRPref isOnlineMode] || m_forceDL) { |
if ([CMRPref isOnlineMode] || m_forceDL) { |
176 |
dlTask = [self makeDownloadTask]; |
dlTask = [self makeDownloadTask]; |
177 |
[dlTask run]; |
[dlTask run]; |
179 |
dlTask = nil; |
dlTask = nil; |
180 |
[temp release]; |
[temp release]; |
181 |
|
|
182 |
[self checkIsInterrupted]; |
if(self.isInterrupted) goto abort; |
183 |
if (m_downloadData && [m_downloadData length] != 0) { |
if (m_downloadData && [m_downloadData length] != 0) { |
184 |
dbupTask = [[BSDBThreadsListDBUpdateTask2 taskWithBBSName:bbsName data:m_downloadData livedoor:isLivedoor rebuilding:isRebuilding] retain]; |
dbupTask = [[BSDBThreadsListDBUpdateTask2 taskWithBBSName:bbsName data:m_downloadData livedoor:isLivedoor rebuilding:isRebuilding] retain]; |
185 |
[dbupTask run]; |
[dbupTask run]; |
186 |
if (isRebuilding && [dbupTask lastErrorWhileRebuilding]) { |
if (isRebuilding && [dbupTask lastErrorWhileRebuilding]) { |
187 |
m_targetList.rebuildError = [dbupTask lastErrorWhileRebuilding]; |
self.targetList.rebuildError = [dbupTask lastErrorWhileRebuilding]; |
188 |
} |
} |
189 |
|
|
190 |
id temp2 = dbupTask; |
id temp2 = dbupTask; |
191 |
dbupTask = nil; |
dbupTask = nil; |
192 |
[temp2 release]; |
[temp2 release]; |
193 |
|
|
194 |
[self checkIsInterrupted]; |
if(self.isInterrupted) goto abort; |
195 |
} else if (m_downloadError) { |
} else if (m_downloadError) { |
196 |
[self showDownloadErrorAlert]; |
[self showDownloadErrorAlert]; |
197 |
} else { |
} else { |
199 |
} |
} |
200 |
} |
} |
201 |
|
|
202 |
[m_targetList updateCursor]; |
abort: |
203 |
|
[self.targetList updateCursor]; |
204 |
|
|
205 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
206 |
} |
} |
207 |
|
|
|
- (void)finalizeWhenInterrupted |
|
|
{ |
|
|
[m_targetList updateCursor]; |
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
|
|
[super finalizeWhenInterrupted]; // 2008-03-11 |
|
|
} |
|
208 |
|
|
209 |
- (void)dlDidFinishDownloadNotification:(NSNotification *)notification |
- (void)dlDidFinishDownloadNotification:(NSNotification *)notification |
210 |
{ |
{ |
220 |
|
|
221 |
-(void)dlCancelDownloadNotification:(NSNotification *)notification |
-(void)dlCancelDownloadNotification:(NSNotification *)notification |
222 |
{ |
{ |
223 |
[self setIsInterrupted:YES]; |
self.isInterrupted = YES; |
224 |
} |
} |
225 |
-(void)dlAbortDownloadNotification:(NSNotification *)notification |
-(void)dlAbortDownloadNotification:(NSNotification *)notification |
226 |
{ |
{ |
228 |
} |
} |
229 |
|
|
230 |
#pragma mark - |
#pragma mark - |
|
- (id)identifier |
|
|
{ |
|
|
return [NSString stringWithFormat:@"%@-%p", self, self]; |
|
|
} |
|
231 |
- (NSString *)title |
- (NSString *)title |
232 |
{ |
{ |
233 |
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"Update threads list. %@", @"ThreadsList", @""), |
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"Update threads list. %@", @"ThreadsList", @""), |
234 |
bbsName]; |
self.boardName]; |
235 |
} |
} |
236 |
- (NSString *)messageInProgress |
- (NSString *)message |
237 |
{ |
{ |
238 |
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"Update threads list. %@", @"ThreadsList", @""), |
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"Update threads list. %@", @"ThreadsList", @""), |
239 |
bbsName]; |
self.boardName]; |
240 |
} |
} |
241 |
|
|
242 |
-(IBAction)cancel:(id)sender |
-(IBAction)cancel:(id)sender |
243 |
{ |
{ |
244 |
[dlTask cancel:self]; |
[dlTask cancel:self]; |
245 |
m_targetList = nil; |
self.targetList = nil; |
246 |
|
|
247 |
[super cancel:sender]; |
[super cancel:sender]; |
248 |
} |
} |