| 1 |
#import "DUMMailBox.h" |
| 2 |
#import "DUMMail.h" |
| 3 |
|
| 4 |
|
| 5 |
@implementation DUMMailBox |
| 6 |
// Private Method |
| 7 |
-(void)readMail:(NSString *)path{ |
| 8 |
NSRange textRange = NSMakeRange(0, 0); |
| 9 |
NSRange lineRange = NSMakeRange(0, 0); |
| 10 |
NSData *data; |
| 11 |
NSString *mailFile; |
| 12 |
NSMutableString *buf; |
| 13 |
NSFileHandle *file; |
| 14 |
|
| 15 |
if(!path){ |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
file = [NSFileHandle fileHandleForReadingAtPath:path]; |
| 20 |
data = [file readDataToEndOfFile]; |
| 21 |
mailFile = [[NSString alloc] initWithData:data encoding:NSJapaneseEUCStringEncoding]; |
| 22 |
|
| 23 |
// バッファサイズの数字はいいかげん。 |
| 24 |
buf = [NSMutableString stringWithCapacity:500]; |
| 25 |
|
| 26 |
// メールファイルを、1メールずつに切り分ける |
| 27 |
textRange = NSMakeRange(0, [mailFile length]); |
| 28 |
while(textRange.length > 0){ |
| 29 |
NSString *line; |
| 30 |
lineRange = [mailFile lineRangeForRange: |
| 31 |
NSMakeRange(textRange.location, 0)]; |
| 32 |
line = [mailFile substringWithRange:lineRange]; |
| 33 |
|
| 34 |
if([buf length] != 0 && [line hasPrefix:@"From "]){ |
| 35 |
DUMMail *mail = [[DUMMail alloc] initWithMailData:buf]; |
| 36 |
[mails addObject:mail]; |
| 37 |
// NSLog(@"%@", mail); |
| 38 |
[mail release]; |
| 39 |
// バッファをクリア |
| 40 |
[buf deleteCharactersInRange:NSMakeRange(0, [buf length])]; |
| 41 |
} |
| 42 |
[buf appendString:line]; |
| 43 |
|
| 44 |
textRange.location = NSMaxRange(lineRange); |
| 45 |
textRange.length -= lineRange.length; |
| 46 |
} |
| 47 |
[data release]; |
| 48 |
[mailFile release]; |
| 49 |
NSLog(@"%i mail files\n", [mails count]); |
| 50 |
} |
| 51 |
|
| 52 |
// Public Method |
| 53 |
-(id)initWithName:(NSString *)name dataFilePath:path;{ |
| 54 |
self = [super init]; |
| 55 |
mailBoxName = name; |
| 56 |
[mailBoxName retain]; |
| 57 |
childMailBox = [[NSMutableDictionary alloc] init]; |
| 58 |
childMailBoxNameArray = [[NSMutableArray alloc] init]; |
| 59 |
mails = [[NSMutableArray alloc] init]; |
| 60 |
[self readMail:path]; |
| 61 |
return self; |
| 62 |
} |
| 63 |
-(void)dealloc{ |
| 64 |
[mailBoxName release]; |
| 65 |
[childMailBox release]; |
| 66 |
[mails release]; |
| 67 |
[childMailBoxNameArray release]; |
| 68 |
[super dealloc]; |
| 69 |
} |
| 70 |
-(NSString *)name{ |
| 71 |
return mailBoxName; |
| 72 |
} |
| 73 |
-(void)addMail:(DUMMail *)mail{ |
| 74 |
[mails addObject:mail]; |
| 75 |
} |
| 76 |
-(unsigned int)countMailBoxes{ |
| 77 |
return [childMailBoxNameArray count]; |
| 78 |
} |
| 79 |
-(unsigned int)countMail{ |
| 80 |
return [mails count]; |
| 81 |
} |
| 82 |
-(unsigned int)countUnreadMail{ |
| 83 |
unsigned int count = 0; |
| 84 |
unsigned int i; |
| 85 |
for(i = 0; i < [mails count]; i++){ |
| 86 |
if([[mails objectAtIndex:i] isUnread]){ |
| 87 |
count++; |
| 88 |
} |
| 89 |
} |
| 90 |
return count; |
| 91 |
} |
| 92 |
-(void)addMailBox:(DUMMailBox *)mailBox{ |
| 93 |
[childMailBox setObject:mailBox forKey:[mailBox name]]; |
| 94 |
[childMailBoxNameArray addObject:[mailBox name]]; |
| 95 |
} |
| 96 |
-(DUMMailBox *)mailBoxAtIndex:(unsigned int)index{ |
| 97 |
NSString *key = [childMailBoxNameArray objectAtIndex:index]; |
| 98 |
return [childMailBox objectForKey:key]; |
| 99 |
} |
| 100 |
-(DUMMail *)mailAtIndex:(unsigned int)index{ |
| 101 |
return [mails objectAtIndex:index]; |
| 102 |
} |
| 103 |
|
| 104 |
@end |