Open-Source-Software-Entwicklung und Downloads

Browse Subversion Repository

Annotation of /bathyscaphe/trunk/application/source/browser/BSThreadListUpdateTask.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 593 - (hide annotations) (download)
Sun Nov 19 13:23:48 2006 UTC (17 years, 6 months ago) by masakih
File size: 10086 byte(s)
remove memory leak. and any change.

1 masakih 419 //
2     // BSThreadListUpdateTask.m
3     // BathyScaphe
4     //
5     // Created by Hori,Masaki on 06/03/29.
6     // Copyright 2006 __MyCompanyName__. All rights reserved.
7     //
8    
9     #import "BSThreadListUpdateTask.h"
10     #import "BSDBThreadList.h"
11     #import "BoardListItem.h"
12     #import "DatabaseManager.h"
13     #import "AppDefaults.h"
14    
15 masakih 586 NSString *BSThreadListUpdateTaskDidFinishNotification = @"BSThreadListUpdateTaskDidFinishNotification";
16    
17 masakih 419 @implementation BSThreadListUpdateTask
18    
19     + (id)taskWithBSDBThreadList:(BSDBThreadList *)threadList
20     {
21     return [[[[self class] alloc] initWithBSDBThreadList:threadList] autorelease];
22     }
23     - (id)initWithBSDBThreadList:(BSDBThreadList *)threadList
24     {
25     if(self = [super init]) {
26 masakih 518 target = threadList; //[threadList retain];
27 masakih 419 progress = YES;
28     userCanceled = NO;
29     }
30    
31     return self;
32     }
33     - (void)dealloc
34     {
35 masakih 518 // [target release];
36 masakih 593 [cursor release];
37 masakih 419
38     [super dealloc];
39     }
40    
41     - (id) identifier
42     {
43     return [NSValue valueWithPointer:self];
44     }
45    
46     - (NSString *) title
47     {
48     return [[target boardListItem] representName];
49     }
50     - (NSString *) message
51     {
52     return [NSString stringWithFormat:@"Updating -- %@", [[target boardListItem] representName]];
53     }
54 masakih 586 - (NSString *) messageInProgress
55 masakih 419 {
56 masakih 586 return [NSString stringWithFormat:
57     NSLocalizedString(@"Updating Thread(%@)", @"Updating Thread(%@)"),
58     [target boardName]];
59 masakih 419 }
60    
61     - (IBAction) cancel : (id) sender
62     {
63     userCanceled = YES;
64     }
65    
66    
67     //- (id)copyWithZone:(NSZone *)zone
68     //{
69     // BSThreadListUpdateTask *result = [[self class] taskWithBSDBThreadList:target];
70     // if(result) {
71     // result->progress = progress;
72     // result->userCanceled = userCanceled;
73     // }
74     //
75     // return [result retain];
76     //}
77    
78     #pragma mark-
79    
80     static inline NSArray *componentsSeparatedByWhiteSpace(NSString *string)
81     {
82     NSMutableArray *result = [NSMutableArray array];
83     NSScanner *s = [NSScanner scannerWithString : string];
84     NSCharacterSet *cs = [NSCharacterSet whitespaceCharacterSet];
85     NSString *str;
86    
87     while ([s scanUpToCharactersFromSet : cs intoString : &str]) {
88     [result addObject : str];
89     }
90    
91     if ([result count] == 0) {
92     return nil;
93     }
94    
95     return result;
96     }
97     static inline NSString *whereClauseFromSearchString(NSString *searchString)
98     {
99     NSMutableString *clause;
100     NSArray *searchs;
101     NSEnumerator *searchsEnum;
102     NSString *token;
103    
104     NSString *p = @"";
105    
106     searchs = componentsSeparatedByWhiteSpace(searchString);
107    
108     if (!searchs || [searchs count] == 0) {
109     return nil;
110     }
111    
112     clause = [NSMutableString stringWithFormat : @" WHERE "];
113    
114     searchsEnum = [searchs objectEnumerator];
115     while (token = [searchsEnum nextObject]) {
116     if ([token hasPrefix : @"!"]) {
117     if ([token length] == 1) continue;
118    
119     [clause appendFormat : @"%@NOT %@ LIKE '%%%@%%' ",
120     p, ThreadNameColumn, [token substringFromIndex : 1]];
121     } else {
122     [clause appendFormat : @"%@%@ LIKE '%%%@%%' ",
123     p, ThreadNameColumn, token];
124     }
125     p = @"AND ";
126     }
127    
128     return clause;
129     }
130    
131     enum {
132     kNewerThreadType, // ������������
133     kOlderThreadType, // ���������������
134     kAllThreadType, // ���������
135     };
136    
137     // filter ���������
138     // ������������������������������������������������������������������������������DB���������������������������
139     // WHERE���������������
140     static inline NSString *conditionFromStatusAndType( int status, int type )
141     {
142     NSMutableString *result = [NSMutableString string];
143     NSString *brankOrAnd = @"";
144    
145     if(status & ThreadLogCachedStatus &&
146     (type == kOlderThreadType || !(status & ThreadNewCreatedStatus))) {
147     // ������/������������������������������������������ ���������������������������������
148     [result appendFormat : @"NOT %@ IS NULL\n", NumberOfReadColumn];
149     brankOrAnd = @" AND ";
150     } else if(status & ThreadNoCacheStatus) {
151     // ���������������������
152     [result appendFormat : @"%@ IS NULL\n", NumberOfReadColumn];
153     brankOrAnd = @" AND ";
154     } else if(status & ThreadNewCreatedStatus && type == kOlderThreadType) {
155     // ������������������������������������������������������������ boardID ���������������������������0���������
156     [result appendFormat : @"%@ < 0\n",BoardIDColumn];
157     brankOrAnd = @" AND ";
158     }
159    
160     switch(type) {
161     case kNewerThreadType:
162     [result appendFormat : @"%@%@ = %u\n",
163     brankOrAnd, ThreadStatusColumn, ThreadNewCreatedStatus];
164     break;
165     case kOlderThreadType:
166     [result appendFormat : @"%@%@ != %u\n",
167     brankOrAnd, ThreadStatusColumn, ThreadNewCreatedStatus];
168     break;
169     case kAllThreadType:
170     // Do nothing.
171     break;
172     default:
173     UTILUnknownCSwitchCase(type);
174     break;
175     }
176    
177     return result;
178     }
179     static inline NSString *orderBy( NSString *sortKey, BOOL isAscending )
180     {
181 masakih 566 // NSString *result = nil;
182 masakih 419 NSString *sortCol = nil;
183     NSString *ascending = @"";
184    
185     if (!isAscending) ascending = @"DESC";
186    
187     if ([sortKey isEqualTo : CMRThreadTitleKey]) {
188     sortCol = ThreadNameColumn;
189     } else if ([sortKey isEqualTo : CMRThreadLastLoadedNumberKey]) {
190     sortCol = NumberOfReadColumn;
191     } else if ([sortKey isEqualTo : CMRThreadNumberOfMessagesKey]) {
192     sortCol = NumberOfAllColumn;
193     } else if ([sortKey isEqualTo : CMRThreadNumberOfUpdatedKey]) {
194     sortCol = NumberOfDifferenceColumn;
195     } else if ([sortKey isEqualTo : CMRThreadSubjectIndexKey]) {
196     sortCol = TempThreadThreadNumberColumn;
197     } else if ([sortKey isEqualTo : CMRThreadStatusKey]) {
198     sortCol = ThreadStatusColumn;
199     } else if ([sortKey isEqualTo : CMRThreadModifiedDateKey]) {
200     sortCol = ModifiedDateColumn;
201     } else if ([sortKey isEqualTo : ThreadPlistIdentifierKey]) {
202     sortCol = ThreadIDColumn;
203     } else if ([sortKey isEqualTo : ThreadPlistBoardNameKey]) {
204     sortCol = BoardNameColumn;
205     }
206    
207 masakih 518 // if(sortCol) {
208     // result = [NSString stringWithFormat : @"ORDER BY %@ %@",sortCol, ascending];
209     // }
210     //
211     // return result;
212     return [sortCol lowercaseString];
213 masakih 419 }
214     - (NSString *) sqlForListForType : (int) type
215     {
216     NSString *targetTable = [[target boardListItem] query];
217     NSMutableString *sql;
218     NSString *whereOrAnd = @" WHERE ";
219     NSString *searchCondition;
220     NSString *filterCondition;
221 masakih 566 // NSString *order;
222 masakih 419
223     sql = [NSMutableString stringWithFormat : @"SELECT * FROM (%@) ",targetTable];
224    
225     if ([target searchString] && ![[target searchString] isEmpty]) {
226     searchCondition = whereClauseFromSearchString([target searchString]);
227     if (searchCondition) {
228     [sql appendString : searchCondition];
229     whereOrAnd = @" AND ";
230     }
231     }
232    
233     filterCondition = conditionFromStatusAndType( [target status], type);
234 masakih 518 if(filterCondition && [filterCondition length] != 0) {
235 masakih 419 [sql appendFormat : @"%@ %@\n", whereOrAnd, filterCondition];
236     // whereOrAnd = @" AND ";
237     }
238    
239 masakih 518 // order = orderBy( [target sortKey], [target isAscending]);
240     // if(order) {
241     // [sql appendString : order];
242     // }
243 masakih 419
244     return sql;
245     }
246     - (id)cursor
247     {
248 masakih 586 return cursor;
249     }
250     - (void) setCursor:(id)new
251     {
252     id temp = cursor;
253     cursor = [new retain];
254     [temp release];
255     }
256     - (void) doExecuteWithLayout : (CMRThreadLayout *) layout
257     {
258 masakih 419 id result = nil;
259    
260 masakih 586 // [self postTaskWillStartNotification];
261 masakih 419
262     SQLiteDB *db = [[DatabaseManager defaultManager] databaseForCurrentThread];
263     NSString *newersSQL = nil;
264     NSString *sql;
265     id <SQLiteMutableCursor> newerCursor = nil;
266     id <SQLiteMutableCursor> olderCursor = nil;
267    
268     UTILAssertNotNil(db);
269    
270     if( [CMRPref collectByNew] ) {
271     if(userCanceled) goto final;
272     newersSQL = [self sqlForListForType : kNewerThreadType];
273     if(userCanceled) goto final;
274     sql = [self sqlForListForType : kOlderThreadType];
275     } else {
276     sql = [self sqlForListForType : kAllThreadType];
277     }
278    
279 masakih 518 // if(userCanceled) goto final;
280     // sql = [sql stringByAppendingString:@"\nLIMIT 5000"];
281     // newersSQL = [newersSQL stringByAppendingString:@"\nLIMIT 5000"];
282 masakih 419
283     do {
284     if(userCanceled) goto final;
285     olderCursor = [db cursorForSQL : sql];
286     if ([db lastErrorID] != 0) {
287     NSLog(@"sql error on %s line %d.\n\tReason : %@", __FILE__, __LINE__, [db lastError]);
288     olderCursor = nil;
289     break;
290     }
291     if(userCanceled) goto final;
292     if(newersSQL) {
293     newerCursor = [db cursorForSQL : newersSQL];
294     if([db lastErrorID] != 0) {
295     NSLog(@"sql error on %s line %d.\n\tReason : %@", __FILE__, __LINE__, [db lastError]);
296     newerCursor = nil;
297     break;
298     }
299     }
300 masakih 518
301 masakih 419 if(userCanceled) goto final;
302 masakih 566
303     /*
304 masakih 518 id sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:orderBy([target sortKey], 0)
305     ascending:[target isAscending]
306     selector:@selector(numericCompare:)] autorelease];
307     id sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
308 masakih 566 */
309     id sortDescriptors = [target sortDescriptors];
310 masakih 518 if(newerCursor) {
311     NSArray *data = [newerCursor arrayForTableView];
312     NSArray *col = [newerCursor columnNames];
313     NSArrayController *acon = [[[NSArrayController alloc] initWithContent:data] autorelease];
314    
315     [acon setSortDescriptors:sortDescriptors];
316     data = [acon arrangeObjects:data];
317    
318     newerCursor = [NSDictionary dictionaryWithObjectsAndKeys:data, @"Values", col, @"ColumnNames", nil];
319     }
320     if(olderCursor) {
321     NSArray *data = [olderCursor arrayForTableView];
322     NSArray *col = [olderCursor columnNames];
323     NSArrayController *acon = [[[NSArrayController alloc] initWithContent:data] autorelease];
324    
325     [acon setSortDescriptors:sortDescriptors];
326     data = [acon arrangeObjects:data];
327    
328     olderCursor = [NSDictionary dictionaryWithObjectsAndKeys:data, @"Values", col, @"ColumnNames", nil];
329     }
330    
331     if(userCanceled) goto final;
332 masakih 419 if(newerCursor && [newerCursor rowCount]) {
333     [newerCursor appendCursor : olderCursor];
334     olderCursor = nil;
335     }
336     } while( NO );
337    
338     if(olderCursor || newerCursor) {
339     if(olderCursor) {
340     result = olderCursor;
341     } else {
342     result = newerCursor;
343     }
344     }
345    
346     final:
347 masakih 586 [self setCursor:result];
348 masakih 419 [self postTaskDidFinishNotification];
349     }
350    
351     @end
352    
353 masakih 586 @implementation BSThreadListUpdateTask(Notification)
354 masakih 419
355     - (void) postTaskDidFinishNotification
356     {
357     NSNotificationCenter *nc_;
358 masakih 586
359 masakih 419 nc_ = [NSNotificationCenter defaultCenter];
360 masakih 586 [nc_ postNotificationName : BSThreadListUpdateTaskDidFinishNotification
361 masakih 419 object : self];
362     }
363     @end
364 masakih 518
365     @implementation NSString(BSThreadListUpdateTaskAddition)
366     - (NSComparisonResult)numericCompare:(NSString *)string
367     {
368     return [self compare:string options:NSNumericSearch];
369     }
370     @end
371     @implementation NSNumber(BSThreadListUpdateTaskAddition)
372     - (NSComparisonResult)numericCompare:(id)obj
373     {
374     return [self compare:obj];
375     }
376     @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26