| 1 |
/* |
| 2 |
** CWFlags.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2004 Ludovic Marcotte |
| 5 |
** Copyright (C) 2018 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWFlags.h> |
| 25 |
|
| 26 |
#import <Pantomime/NSData+Extensions.h> |
| 27 |
|
| 28 |
#define CHECK_FLAG(c, value) \ |
| 29 |
theRange = [theData rangeOfCString: c]; \ |
| 30 |
if (theRange.length) { [self add: value]; } |
| 31 |
|
| 32 |
// |
| 33 |
// |
| 34 |
// |
| 35 |
@implementation CWFlags |
| 36 |
|
| 37 |
- (id) initWithFlags: (PantomimeFlag) theFlags |
| 38 |
{ |
| 39 |
self = [super init]; |
| 40 |
if (self) |
| 41 |
{ |
| 42 |
flags = theFlags; |
| 43 |
} |
| 44 |
return self; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
// |
| 49 |
// NSCoding protocol |
| 50 |
// |
| 51 |
- (void) encodeWithCoder: (NSCoder *) theCoder |
| 52 |
{ |
| 53 |
[theCoder encodeObject: [NSNumber numberWithInt: flags]]; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
- (id) initWithCoder: (NSCoder *) theCoder |
| 58 |
{ |
| 59 |
self = [super init]; |
| 60 |
if (self) |
| 61 |
{ |
| 62 |
flags = (PantomimeFlag)[[theCoder decodeObject] intValue]; |
| 63 |
} |
| 64 |
return self; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
// |
| 69 |
// NSCopying protocol |
| 70 |
// |
| 71 |
- (id) copyWithZone: (NSZone *) zone |
| 72 |
{ |
| 73 |
CWFlags *copy; |
| 74 |
|
| 75 |
copy = [[CWFlags allocWithZone: zone] initWithFlags: flags]; |
| 76 |
|
| 77 |
return copy; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
// |
| 82 |
// |
| 83 |
// |
| 84 |
- (void) add: (PantomimeFlag) theFlag |
| 85 |
{ |
| 86 |
flags = flags|theFlag; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
// |
| 91 |
// |
| 92 |
// |
| 93 |
- (void) addFlagsFromData: (NSData *) theData |
| 94 |
format: (PantomimeFolderFormat) theFormat |
| 95 |
{ |
| 96 |
NSRange theRange; |
| 97 |
|
| 98 |
if (theData) |
| 99 |
{ |
| 100 |
if (theFormat == PantomimeFormatMbox) |
| 101 |
{ |
| 102 |
CHECK_FLAG("R", PantomimeSeen); |
| 103 |
CHECK_FLAG("D", PantomimeDeleted); |
| 104 |
CHECK_FLAG("A", PantomimeAnswered); |
| 105 |
CHECK_FLAG("F", PantomimeFlagged); |
| 106 |
} |
| 107 |
else if (theFormat == PantomimeFormatMaildir) |
| 108 |
{ |
| 109 |
CHECK_FLAG("S", PantomimeSeen); |
| 110 |
CHECK_FLAG("R", PantomimeAnswered); |
| 111 |
CHECK_FLAG("F", PantomimeFlagged); |
| 112 |
CHECK_FLAG("D", PantomimeDraft); |
| 113 |
CHECK_FLAG("T", PantomimeDeleted); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
// |
| 120 |
// |
| 121 |
// |
| 122 |
- (BOOL) contain: (PantomimeFlag) theFlag |
| 123 |
{ |
| 124 |
if ((flags&theFlag) == theFlag) |
| 125 |
{ |
| 126 |
return YES; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
return NO; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// |
| 136 |
// |
| 137 |
// |
| 138 |
- (void) replaceWithFlags: (CWFlags *) theFlags |
| 139 |
{ |
| 140 |
flags = theFlags->flags; |
| 141 |
} |
| 142 |
|
| 143 |
// |
| 144 |
// |
| 145 |
// |
| 146 |
- (void) remove: (PantomimeFlag) theFlag |
| 147 |
{ |
| 148 |
flags = flags&(flags^theFlag); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
// |
| 153 |
// |
| 154 |
// |
| 155 |
- (void) removeAll |
| 156 |
{ |
| 157 |
flags = 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
// |
| 162 |
// |
| 163 |
// |
| 164 |
- (NSString *) statusString |
| 165 |
{ |
| 166 |
return [NSString stringWithFormat: @"%cO", ([self contain: PantomimeSeen] ? 'R' : ' ')]; |
| 167 |
} |
| 168 |
|
| 169 |
// |
| 170 |
|
| 171 |
// This is useful if we want to store the flags in the mbox file |
| 172 |
// when expunging messages from it. We might write in the headers: |
| 173 |
// |
| 174 |
// X-Status: FA |
| 175 |
// |
| 176 |
// If the message had the "Flagged" and "Answered" flags. |
| 177 |
// |
| 178 |
// Note: We store the same value as pine does in order to ease |
| 179 |
// using mbox files between the two MUAs. |
| 180 |
// |
| 181 |
- (NSString *) xstatusString |
| 182 |
{ |
| 183 |
NSMutableString *aMutableString; |
| 184 |
|
| 185 |
aMutableString = [[NSMutableString alloc] init]; |
| 186 |
|
| 187 |
if ([self contain: PantomimeDeleted]) |
| 188 |
{ |
| 189 |
[aMutableString appendFormat: @"%c", 'D']; |
| 190 |
} |
| 191 |
|
| 192 |
if ([self contain: PantomimeFlagged]) |
| 193 |
{ |
| 194 |
[aMutableString appendFormat: @"%c", 'F']; |
| 195 |
} |
| 196 |
|
| 197 |
if ([self contain: PantomimeAnswered]) |
| 198 |
{ |
| 199 |
[aMutableString appendFormat: @"%c", 'A']; |
| 200 |
} |
| 201 |
|
| 202 |
return AUTORELEASE(aMutableString); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
// |
| 207 |
// |
| 208 |
// |
| 209 |
- (NSString *) maildirString |
| 210 |
{ |
| 211 |
NSMutableString *aMutableString; |
| 212 |
|
| 213 |
aMutableString = [[NSMutableString alloc] initWithString: @"2,"]; |
| 214 |
|
| 215 |
if ([self contain: PantomimeDraft]) |
| 216 |
{ |
| 217 |
[aMutableString appendString: @"D"]; |
| 218 |
} |
| 219 |
|
| 220 |
if ([self contain: PantomimeFlagged]) |
| 221 |
{ |
| 222 |
[aMutableString appendString: @"F"]; |
| 223 |
} |
| 224 |
|
| 225 |
if ([self contain: PantomimeAnswered]) |
| 226 |
{ |
| 227 |
[aMutableString appendString: @"R"]; |
| 228 |
} |
| 229 |
|
| 230 |
if ([self contain: PantomimeSeen]) |
| 231 |
{ |
| 232 |
[aMutableString appendString: @"S"]; |
| 233 |
} |
| 234 |
|
| 235 |
if ([self contain: PantomimeDeleted]) |
| 236 |
{ |
| 237 |
[aMutableString appendString: @"T"]; |
| 238 |
} |
| 239 |
|
| 240 |
return AUTORELEASE(aMutableString); |
| 241 |
} |
| 242 |
|
| 243 |
@end |