1 |
masakih |
419 |
// |
2 |
|
|
// BSThreadListUpdateTask.m |
3 |
|
|
// BathyScaphe |
4 |
|
|
// |
5 |
|
|
// Created by Hori,Masaki on 06/03/29. |
6 |
tsawada2 |
965 |
// Copyright 2006-2008 BathyScaphe Project. All rights reserved. |
7 |
|
|
// encoding="UTF-8" |
8 |
masakih |
419 |
// |
9 |
|
|
|
10 |
|
|
#import "BSThreadListUpdateTask.h" |
11 |
|
|
#import "BSDBThreadList.h" |
12 |
|
|
#import "BoardListItem.h" |
13 |
tsawada2 |
965 |
#import "BoardBoardListItem.h" |
14 |
masakih |
419 |
#import "DatabaseManager.h" |
15 |
|
|
#import "AppDefaults.h" |
16 |
|
|
|
17 |
masakih |
586 |
NSString *BSThreadListUpdateTaskDidFinishNotification = @"BSThreadListUpdateTaskDidFinishNotification"; |
18 |
|
|
|
19 |
masakih |
419 |
@implementation BSThreadListUpdateTask |
20 |
|
|
+ (id)taskWithBSDBThreadList:(BSDBThreadList *)threadList |
21 |
|
|
{ |
22 |
|
|
return [[[[self class] alloc] initWithBSDBThreadList:threadList] autorelease]; |
23 |
|
|
} |
24 |
tsawada2 |
965 |
|
25 |
masakih |
419 |
- (id)initWithBSDBThreadList:(BSDBThreadList *)threadList |
26 |
|
|
{ |
27 |
|
|
if(self = [super init]) { |
28 |
masakih |
518 |
target = threadList; //[threadList retain]; |
29 |
masakih |
419 |
progress = YES; |
30 |
|
|
userCanceled = NO; |
31 |
masakih |
683 |
|
32 |
|
|
bbsName = [[[target boardListItem] representName] copy]; |
33 |
masakih |
419 |
} |
34 |
|
|
|
35 |
|
|
return self; |
36 |
|
|
} |
37 |
tsawada2 |
965 |
|
38 |
masakih |
419 |
- (void)dealloc |
39 |
|
|
{ |
40 |
masakih |
593 |
[cursor release]; |
41 |
masakih |
419 |
|
42 |
|
|
[super dealloc]; |
43 |
|
|
} |
44 |
|
|
|
45 |
tsawada2 |
965 |
- (id)identifier |
46 |
masakih |
419 |
{ |
47 |
|
|
return [NSValue valueWithPointer:self]; |
48 |
|
|
} |
49 |
|
|
|
50 |
tsawada2 |
965 |
- (NSString *)title |
51 |
masakih |
419 |
{ |
52 |
masakih |
683 |
return bbsName; |
53 |
masakih |
419 |
} |
54 |
tsawada2 |
965 |
|
55 |
|
|
- (NSString *)messageInProgress |
56 |
masakih |
419 |
{ |
57 |
masakih |
586 |
return [NSString stringWithFormat: |
58 |
tsawada2 |
753 |
NSLocalizedStringFromTable(@"Updating Thread(%@)", @"ThreadsList", @""), |
59 |
masakih |
683 |
bbsName]; |
60 |
masakih |
419 |
} |
61 |
|
|
|
62 |
tsawada2 |
965 |
- (IBAction)cancel:(id)sender |
63 |
masakih |
419 |
{ |
64 |
|
|
userCanceled = YES; |
65 |
masakih |
611 |
target = nil; |
66 |
masakih |
419 |
} |
67 |
|
|
|
68 |
tsawada2 |
965 |
#pragma mark - |
69 |
tsawada2 |
961 |
/* |
70 |
masakih |
419 |
static inline NSArray *componentsSeparatedByWhiteSpace(NSString *string) |
71 |
|
|
{ |
72 |
|
|
NSMutableArray *result = [NSMutableArray array]; |
73 |
|
|
NSScanner *s = [NSScanner scannerWithString : string]; |
74 |
|
|
NSCharacterSet *cs = [NSCharacterSet whitespaceCharacterSet]; |
75 |
|
|
NSString *str; |
76 |
|
|
|
77 |
|
|
while ([s scanUpToCharactersFromSet : cs intoString : &str]) { |
78 |
|
|
[result addObject : str]; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
if ([result count] == 0) { |
82 |
|
|
return nil; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
return result; |
86 |
|
|
} |
87 |
|
|
static inline NSString *whereClauseFromSearchString(NSString *searchString) |
88 |
|
|
{ |
89 |
|
|
NSMutableString *clause; |
90 |
|
|
NSArray *searchs; |
91 |
|
|
NSEnumerator *searchsEnum; |
92 |
|
|
NSString *token; |
93 |
|
|
|
94 |
|
|
NSString *p = @""; |
95 |
|
|
|
96 |
|
|
searchs = componentsSeparatedByWhiteSpace(searchString); |
97 |
|
|
|
98 |
|
|
if (!searchs || [searchs count] == 0) { |
99 |
|
|
return nil; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
clause = [NSMutableString stringWithFormat : @" WHERE "]; |
103 |
|
|
|
104 |
|
|
searchsEnum = [searchs objectEnumerator]; |
105 |
|
|
while (token = [searchsEnum nextObject]) { |
106 |
|
|
if ([token hasPrefix : @"!"]) { |
107 |
|
|
if ([token length] == 1) continue; |
108 |
|
|
|
109 |
|
|
[clause appendFormat : @"%@NOT %@ LIKE '%%%@%%' ", |
110 |
|
|
p, ThreadNameColumn, [token substringFromIndex : 1]]; |
111 |
|
|
} else { |
112 |
|
|
[clause appendFormat : @"%@%@ LIKE '%%%@%%' ", |
113 |
|
|
p, ThreadNameColumn, token]; |
114 |
|
|
} |
115 |
|
|
p = @"AND "; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
return clause; |
119 |
|
|
} |
120 |
tsawada2 |
961 |
*/ |
121 |
tsawada2 |
965 |
- (NSString *)sqlForList |
122 |
masakih |
419 |
{ |
123 |
|
|
NSMutableString *sql; |
124 |
tsawada2 |
965 |
BoardListItem *boardItem = [target boardListItem]; |
125 |
masakih |
419 |
|
126 |
tsawada2 |
965 |
if ([BoardListItem isBoardItem:boardItem] && [CMRPref threadsListViewMode] == BSThreadsListShowsStoredLogFiles) { |
127 |
masakih |
1165 |
sql = [NSMutableString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = %lu AND %@ > 0", |
128 |
|
|
BoardThreadInfoViewName, BoardIDColumn, (unsigned long)[(BoardBoardListItem *)boardItem boardID], NumberOfReadColumn]; |
129 |
tsawada2 |
965 |
} else { |
130 |
|
|
NSString *targetTable = [boardItem query]; |
131 |
masakih |
1182 |
sql = [NSMutableString stringWithFormat: @"SELECT * FROM (%@) ",targetTable]; |
132 |
masakih |
419 |
} |
133 |
|
|
return sql; |
134 |
|
|
} |
135 |
tsawada2 |
965 |
|
136 |
masakih |
419 |
- (id)cursor |
137 |
|
|
{ |
138 |
masakih |
586 |
return cursor; |
139 |
|
|
} |
140 |
tsawada2 |
965 |
|
141 |
|
|
- (void)setCursor:(id)new |
142 |
masakih |
586 |
{ |
143 |
|
|
id temp = cursor; |
144 |
|
|
cursor = [new retain]; |
145 |
|
|
[temp release]; |
146 |
|
|
} |
147 |
tsawada2 |
965 |
|
148 |
|
|
- (void)doExecuteWithLayout:(CMRThreadLayout *)layout |
149 |
masakih |
586 |
{ |
150 |
masakih |
880 |
id <SQLiteMutableCursor> result = nil; |
151 |
|
|
|
152 |
masakih |
419 |
SQLiteDB *db = [[DatabaseManager defaultManager] databaseForCurrentThread]; |
153 |
|
|
NSString *sql; |
154 |
|
|
|
155 |
|
|
UTILAssertNotNil(db); |
156 |
|
|
|
157 |
masakih |
917 |
sql = [self sqlForList]; |
158 |
masakih |
880 |
if(userCanceled) goto final; |
159 |
|
|
result = [db cursorForSQL : sql]; |
160 |
|
|
if ([db lastErrorID] != 0) { |
161 |
tsawada2 |
1068 |
NSLog(@"sql error on BSThreadListUpdateTask.m (-doExecuteWithLayout.) \n\tReason : %@", [db lastError]); |
162 |
masakih |
880 |
result = nil; |
163 |
tsawada2 |
965 |
} |
164 |
|
|
|
165 |
masakih |
419 |
final: |
166 |
masakih |
586 |
[self setCursor:result]; |
167 |
masakih |
419 |
[self postTaskDidFinishNotification]; |
168 |
|
|
} |
169 |
|
|
@end |
170 |
|
|
|
171 |
tsawada2 |
965 |
|
172 |
masakih |
586 |
@implementation BSThreadListUpdateTask(Notification) |
173 |
tsawada2 |
965 |
- (void)postTaskDidFinishNotification |
174 |
masakih |
419 |
{ |
175 |
|
|
NSNotificationCenter *nc_; |
176 |
masakih |
586 |
|
177 |
masakih |
419 |
nc_ = [NSNotificationCenter defaultCenter]; |
178 |
tsawada2 |
965 |
[nc_ postNotificationName:BSThreadListUpdateTaskDidFinishNotification object:self]; |
179 |
masakih |
419 |
} |
180 |
|
|
@end |
181 |
masakih |
518 |
|
182 |
tsawada2 |
965 |
|
183 |
masakih |
518 |
@implementation NSString(BSThreadListUpdateTaskAddition) |
184 |
|
|
- (NSComparisonResult)numericCompare:(NSString *)string |
185 |
|
|
{ |
186 |
|
|
return [self compare:string options:NSNumericSearch]; |
187 |
|
|
} |
188 |
|
|
@end |
189 |
tsawada2 |
961 |
|
190 |
tsawada2 |
965 |
|
191 |
masakih |
518 |
@implementation NSNumber(BSThreadListUpdateTaskAddition) |
192 |
|
|
- (NSComparisonResult)numericCompare:(id)obj |
193 |
|
|
{ |
194 |
|
|
return [self compare:obj]; |
195 |
|
|
} |
196 |
|
|
@end |
197 |
tsawada2 |
965 |
|
198 |
|
|
|
199 |
masakih |
880 |
@implementation NSDate(BSThreadListUpdateTaskAddition) |
200 |
|
|
- (NSComparisonResult)numericCompare:(id)obj |
201 |
|
|
{ |
202 |
|
|
return [self compare:obj]; |
203 |
|
|
} |
204 |
|
|
@end |