| 1 |
/* |
| 2 |
** CWFolder.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** 2013-2020 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/CWFolder.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWConstants.h> |
| 27 |
#import <Pantomime/CWContainer.h> |
| 28 |
#import <Pantomime/CWFlags.h> |
| 29 |
#import <Pantomime/CWMessage.h> |
| 30 |
#import <Pantomime/NSString+Extensions.h> |
| 31 |
|
| 32 |
#import <Foundation/NSAutoreleasePool.h> |
| 33 |
#import <Foundation/NSMapTable.h> |
| 34 |
|
| 35 |
// |
| 36 |
// |
| 37 |
// |
| 38 |
@implementation CWFolder |
| 39 |
|
| 40 |
- (id) initWithName: (NSString *) theName |
| 41 |
{ |
| 42 |
self = [super init]; |
| 43 |
|
| 44 |
if (self) |
| 45 |
{ |
| 46 |
_properties = [[NSMutableDictionary alloc] init]; |
| 47 |
_allVisibleMessages = nil; |
| 48 |
|
| 49 |
_allMessages = [[NSMutableArray alloc] init]; |
| 50 |
|
| 51 |
// |
| 52 |
// By default, we don't do message threading so we don't |
| 53 |
// initialize this ivar for no reasons |
| 54 |
// |
| 55 |
_allContainers = nil; |
| 56 |
_cacheManager = nil; |
| 57 |
_mode = PantomimeUnknownMode; |
| 58 |
|
| 59 |
[self setName: theName]; |
| 60 |
[self setShowDeleted: NO]; |
| 61 |
[self setShowRead: YES]; |
| 62 |
} |
| 63 |
return self; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// |
| 68 |
// |
| 69 |
// |
| 70 |
- (void) dealloc |
| 71 |
{ |
| 72 |
//NSLog(@"Folder: -dealloc"); |
| 73 |
RELEASE(_properties); |
| 74 |
RELEASE(_name); |
| 75 |
TEST_RELEASE(_allContainers); |
| 76 |
|
| 77 |
// |
| 78 |
// To be safe, we set the value of the _folder ivar of all CWMessage |
| 79 |
// instances to nil value in case something is retaining them. |
| 80 |
// |
| 81 |
[_allMessages makeObjectsPerformSelector: @selector(setFolder:) withObject: nil]; |
| 82 |
RELEASE(_allMessages); |
| 83 |
|
| 84 |
TEST_RELEASE(_allVisibleMessages); |
| 85 |
TEST_RELEASE(_cacheManager); |
| 86 |
|
| 87 |
[super dealloc]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
// |
| 92 |
// NSCopying protocol (FIXME) |
| 93 |
// |
| 94 |
- (id) copyWithZone: (NSZone *) zone |
| 95 |
{ |
| 96 |
return RETAIN(self); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
// |
| 101 |
// |
| 102 |
// |
| 103 |
- (NSString *) name |
| 104 |
{ |
| 105 |
return _name; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// |
| 110 |
// |
| 111 |
// |
| 112 |
- (void) setName: (NSString *) theName |
| 113 |
{ |
| 114 |
ASSIGN(_name, theName); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
// |
| 119 |
// |
| 120 |
// |
| 121 |
- (void) appendMessage: (CWMessage *) theMessage |
| 122 |
{ |
| 123 |
if (theMessage) |
| 124 |
{ |
| 125 |
[_allMessages addObject: theMessage]; |
| 126 |
|
| 127 |
if (_allVisibleMessages) |
| 128 |
{ |
| 129 |
[_allVisibleMessages addObject: theMessage]; |
| 130 |
} |
| 131 |
|
| 132 |
// FIXME |
| 133 |
// If we've done message threading, we simply append the message |
| 134 |
// to the end of our containers array. We might want to place |
| 135 |
// it in the right thread in the future. |
| 136 |
if (_allContainers) |
| 137 |
{ |
| 138 |
CWContainer *aContainer; |
| 139 |
|
| 140 |
aContainer = [[CWContainer alloc] init]; |
| 141 |
aContainer->message = theMessage; |
| 142 |
[theMessage setProperty: aContainer forKey: @"Container"]; |
| 143 |
[_allContainers addObject: aContainer]; |
| 144 |
RELEASE(aContainer); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// |
| 151 |
// |
| 152 |
// |
| 153 |
- (void) appendMessageFromRawSource: (NSData *) theData |
| 154 |
flags: (CWFlags *) theFlags |
| 155 |
{ |
| 156 |
[self subclassResponsibility: _cmd]; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
// |
| 161 |
// |
| 162 |
// |
| 163 |
- (NSArray *) allContainers |
| 164 |
{ |
| 165 |
return _allContainers; |
| 166 |
} |
| 167 |
|
| 168 |
// |
| 169 |
// |
| 170 |
// |
| 171 |
- (NSMutableArray *) messages |
| 172 |
{ |
| 173 |
return _allMessages; |
| 174 |
} |
| 175 |
|
| 176 |
// |
| 177 |
// |
| 178 |
// |
| 179 |
- (NSArray *) visibleMessages |
| 180 |
{ |
| 181 |
if (_allVisibleMessages == nil) |
| 182 |
{ |
| 183 |
NSUInteger i, count; |
| 184 |
|
| 185 |
count = [_allMessages count]; |
| 186 |
_allVisibleMessages = [[NSMutableArray alloc] initWithCapacity: count]; |
| 187 |
|
| 188 |
// quick |
| 189 |
if (_show_deleted && _show_read) |
| 190 |
{ |
| 191 |
[_allVisibleMessages addObjectsFromArray: _allMessages]; |
| 192 |
return _allVisibleMessages; |
| 193 |
} |
| 194 |
|
| 195 |
for (i = 0; i < count; i++) |
| 196 |
{ |
| 197 |
CWMessage *aMessage; |
| 198 |
|
| 199 |
aMessage = [_allMessages objectAtIndex: i]; |
| 200 |
|
| 201 |
// We show or hide deleted messages |
| 202 |
if (_show_deleted) |
| 203 |
{ |
| 204 |
[_allVisibleMessages addObject: aMessage]; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
if ([[aMessage flags] contain: PantomimeDeleted]) |
| 209 |
{ |
| 210 |
// Do nothing |
| 211 |
continue; |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
[_allVisibleMessages addObject: aMessage]; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// We show or hide read messages |
| 220 |
if (_show_read) |
| 221 |
{ |
| 222 |
if (![_allVisibleMessages containsObject: aMessage]) |
| 223 |
{ |
| 224 |
[_allVisibleMessages addObject: aMessage]; |
| 225 |
} |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
if ([[aMessage flags] contain: PantomimeSeen]) |
| 230 |
{ |
| 231 |
if (![[aMessage flags] contain: PantomimeDeleted]) |
| 232 |
{ |
| 233 |
[_allVisibleMessages removeObject: aMessage]; |
| 234 |
} |
| 235 |
} |
| 236 |
else if (![_allVisibleMessages containsObject: aMessage]) |
| 237 |
{ |
| 238 |
[_allVisibleMessages addObject: aMessage]; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return _allVisibleMessages; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
// |
| 249 |
// |
| 250 |
// |
| 251 |
- (void) setMessages: (NSArray *) theMessages |
| 252 |
{ |
| 253 |
if (theMessages) |
| 254 |
{ |
| 255 |
RELEASE(_allMessages); |
| 256 |
_allMessages = [[NSMutableArray alloc] initWithArray: theMessages]; |
| 257 |
|
| 258 |
if (_allContainers) |
| 259 |
{ |
| 260 |
[self thread]; |
| 261 |
} |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
DESTROY(_allMessages); |
| 266 |
} |
| 267 |
|
| 268 |
DESTROY(_allVisibleMessages); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
// |
| 273 |
// |
| 274 |
// |
| 275 |
- (CWMessage *) messageAtIndex: (NSUInteger) theIndex |
| 276 |
{ |
| 277 |
if (theIndex >= [self countVisible]) |
| 278 |
{ |
| 279 |
return nil; |
| 280 |
} |
| 281 |
|
| 282 |
return [[self visibleMessages] objectAtIndex: theIndex]; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
// |
| 287 |
// |
| 288 |
// |
| 289 |
- (NSUInteger) countVisible |
| 290 |
{ |
| 291 |
return [[self visibleMessages] count]; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
// |
| 296 |
// |
| 297 |
// |
| 298 |
- (void) close |
| 299 |
{ |
| 300 |
[self subclassResponsibility: _cmd]; |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
// |
| 306 |
// |
| 307 |
// |
| 308 |
- (void) expunge |
| 309 |
{ |
| 310 |
[self subclassResponsibility: _cmd]; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
// |
| 315 |
// |
| 316 |
// |
| 317 |
- (id) store |
| 318 |
{ |
| 319 |
return _store; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
// |
| 324 |
// No need to retain the store here since our store object |
| 325 |
// retains our folder object. |
| 326 |
// |
| 327 |
- (void) setStore: (id) theStore |
| 328 |
{ |
| 329 |
_store = theStore; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
// |
| 334 |
// |
| 335 |
// |
| 336 |
- (void) removeMessage: (CWMessage *) theMessage |
| 337 |
{ |
| 338 |
if (theMessage) |
| 339 |
{ |
| 340 |
[_allMessages removeObject: theMessage]; |
| 341 |
|
| 342 |
if (_allVisibleMessages) |
| 343 |
{ |
| 344 |
[_allVisibleMessages removeObject: theMessage]; |
| 345 |
} |
| 346 |
|
| 347 |
// FIXME - We must go through our _allContainers ivar in order |
| 348 |
// to find the message that has just been removed from |
| 349 |
// this folder. We must go through all levels. |
| 350 |
// Right now, we simply do again our message threading algo |
| 351 |
if (_allContainers) |
| 352 |
{ |
| 353 |
[self thread]; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
// |
| 360 |
// |
| 361 |
// |
| 362 |
- (BOOL) showDeleted |
| 363 |
{ |
| 364 |
return _show_deleted; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
// |
| 369 |
// |
| 370 |
// |
| 371 |
- (void) setShowDeleted: (BOOL) theBOOL |
| 372 |
{ |
| 373 |
if (theBOOL != _show_deleted) |
| 374 |
{ |
| 375 |
_show_deleted = theBOOL; |
| 376 |
DESTROY(_allVisibleMessages); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
// |
| 382 |
// |
| 383 |
// |
| 384 |
- (BOOL) showRead |
| 385 |
{ |
| 386 |
return _show_read; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
// |
| 391 |
// |
| 392 |
// |
| 393 |
- (void) setShowRead: (BOOL) theBOOL |
| 394 |
{ |
| 395 |
if (theBOOL != _show_read) |
| 396 |
{ |
| 397 |
_show_read = theBOOL; |
| 398 |
DESTROY(_allVisibleMessages); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
// |
| 404 |
// |
| 405 |
// |
| 406 |
- (NSUInteger) numberOfDeletedMessages |
| 407 |
{ |
| 408 |
NSUInteger c, i, count; |
| 409 |
|
| 410 |
c = [_allMessages count]; |
| 411 |
count = 0; |
| 412 |
|
| 413 |
for (i = 0; i < c; i++) |
| 414 |
{ |
| 415 |
if ([[[_allMessages objectAtIndex: i] flags] contain: PantomimeDeleted]) |
| 416 |
{ |
| 417 |
count++; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
return count; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
// |
| 426 |
// |
| 427 |
// |
| 428 |
- (NSUInteger) numberOfUnreadMessages |
| 429 |
{ |
| 430 |
NSUInteger i, c, count; |
| 431 |
|
| 432 |
c = [_allMessages count]; |
| 433 |
count = 0; |
| 434 |
|
| 435 |
for (i = 0; i < c; i++) |
| 436 |
{ |
| 437 |
if (![[[_allMessages objectAtIndex: i] flags] contain: PantomimeSeen]) |
| 438 |
{ |
| 439 |
count++; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
return count; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
// |
| 448 |
// |
| 449 |
// |
| 450 |
- (unsigned long) size; |
| 451 |
{ |
| 452 |
unsigned long size; |
| 453 |
NSUInteger c, i; |
| 454 |
|
| 455 |
c = [_allMessages count]; |
| 456 |
size = 0; |
| 457 |
|
| 458 |
for (i = 0; i < c; i++) |
| 459 |
{ |
| 460 |
size += [(CWMessage *)[_allMessages objectAtIndex: i] size]; |
| 461 |
} |
| 462 |
|
| 463 |
return size; |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
// |
| 469 |
// |
| 470 |
// |
| 471 |
- (void) updateCache |
| 472 |
{ |
| 473 |
DESTROY(_allVisibleMessages); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
// |
| 478 |
// |
| 479 |
// |
| 480 |
- (void) thread |
| 481 |
{ |
| 482 |
NSMapTable *id_table, *subject_table; |
| 483 |
NSAutoreleasePool *pool; |
| 484 |
NSUInteger i, count; |
| 485 |
|
| 486 |
// We clean up ... |
| 487 |
TEST_RELEASE(_allContainers); |
| 488 |
|
| 489 |
// We create our local autorelease pool |
| 490 |
pool = [[NSAutoreleasePool alloc] init]; |
| 491 |
|
| 492 |
// Build id_table and our containers mutable array |
| 493 |
id_table = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16); |
| 494 |
_allContainers = [[NSMutableArray alloc] init]; |
| 495 |
|
| 496 |
// |
| 497 |
// 1. A., B. and C. |
| 498 |
// |
| 499 |
count = [_allMessages count]; |
| 500 |
for (i = 0; i < count; i++) |
| 501 |
{ |
| 502 |
CWContainer *aContainer; |
| 503 |
CWMessage *aMessage; |
| 504 |
|
| 505 |
NSString *aReference; |
| 506 |
NSUInteger j; |
| 507 |
|
| 508 |
// So that gcc shutup |
| 509 |
aMessage = nil; |
| 510 |
aReference = nil; |
| 511 |
|
| 512 |
aMessage = [_allMessages objectAtIndex: i]; |
| 513 |
|
| 514 |
// We skip messages that don't have a valid Message-ID |
| 515 |
if (![aMessage messageID]) |
| 516 |
{ |
| 517 |
aContainer = [[CWContainer alloc] init]; |
| 518 |
aContainer->message = aMessage; |
| 519 |
[aMessage setProperty: aContainer forKey: @"Container"]; |
| 520 |
[_allContainers addObject: aContainer]; |
| 521 |
RELEASE(aContainer); |
| 522 |
continue; |
| 523 |
} |
| 524 |
|
| 525 |
// |
| 526 |
// A. |
| 527 |
// |
| 528 |
aContainer = NSMapGet(id_table, [aMessage messageID]); |
| 529 |
|
| 530 |
if (aContainer) |
| 531 |
{ |
| 532 |
//aContainer->message = aMessage; |
| 533 |
|
| 534 |
if (aContainer->message != aMessage) |
| 535 |
{ |
| 536 |
aContainer = [[CWContainer alloc] init]; |
| 537 |
aContainer->message = aMessage; |
| 538 |
[aMessage setProperty: aContainer forKey: @"Container"]; |
| 539 |
NSMapInsert(id_table, [aMessage messageID], aContainer); |
| 540 |
DESTROY(aContainer); |
| 541 |
} |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
aContainer = [[CWContainer alloc] init]; |
| 546 |
aContainer->message = aMessage; |
| 547 |
[aMessage setProperty: aContainer forKey: @"Container"]; |
| 548 |
NSMapInsert(id_table, [aMessage messageID], aContainer); |
| 549 |
DESTROY(aContainer); |
| 550 |
} |
| 551 |
|
| 552 |
// |
| 553 |
// B. For each element in the message's References field: |
| 554 |
// |
| 555 |
for (j = 0; j < [[aMessage allReferences] count]; j++) |
| 556 |
{ |
| 557 |
// We get a Message-ID |
| 558 |
aReference = [[aMessage allReferences] objectAtIndex: j]; |
| 559 |
|
| 560 |
// Find a container object for the given Message-ID |
| 561 |
aContainer = NSMapGet(id_table, aReference); |
| 562 |
|
| 563 |
if (aContainer) |
| 564 |
{ |
| 565 |
// We found it. We use that. |
| 566 |
} |
| 567 |
// Otherwise, make (and index) one (new Container) with a null Message |
| 568 |
else |
| 569 |
{ |
| 570 |
aContainer = [[CWContainer alloc] init]; |
| 571 |
NSMapInsert(id_table, aReference, aContainer); |
| 572 |
RELEASE(aContainer); |
| 573 |
} |
| 574 |
|
| 575 |
// NOTE: |
| 576 |
// aContainer is valid here. It points to the message (could be a nil message) |
| 577 |
// that has a Message-ID equals to the current aReference value. |
| 578 |
|
| 579 |
// If we are currently using the last References's entry of our list, |
| 580 |
// we simply break the loop since we are gonna set it in C. |
| 581 |
//if ( j == ([[aMessage allReferences] count] - 1) ) |
| 582 |
// { |
| 583 |
// break; |
| 584 |
// } |
| 585 |
|
| 586 |
// Link the References field's Containers together in the order implied by the References header. |
| 587 |
// The last references |
| 588 |
if (j == ([[aMessage allReferences] count] - 1) && |
| 589 |
aContainer->parent == nil) |
| 590 |
{ |
| 591 |
// We grab the container of our current message |
| 592 |
[((CWContainer *)NSMapGet(id_table, [aMessage messageID])) setParent: aContainer]; |
| 593 |
} |
| 594 |
|
| 595 |
// We set the child |
| 596 |
//if ( aContainer->message != aMessage && |
| 597 |
// aContainer->child == nil ) |
| 598 |
// { |
| 599 |
// [aContainer setChild: NSMapGet(id_table, [aMessage messageID])]; |
| 600 |
// } |
| 601 |
|
| 602 |
} // for (j = 0; ... |
| 603 |
|
| 604 |
// NOTE: The loop is over here. It was an ascending loop so |
| 605 |
// aReference points to the LAST reference in our References list |
| 606 |
|
| 607 |
// |
| 608 |
// C. Set the parent of this message to be the last element in References. |
| 609 |
// |
| 610 |
// NOTE: Again, aReference points to the last Message-ID in the References list |
| 611 |
|
| 612 |
// We get the container for the CURRENT message |
| 613 |
aContainer = (CWContainer *)NSMapGet(id_table, [aMessage messageID]); |
| 614 |
|
| 615 |
// If we have no References and no In-Reply-To fields, we simply set a |
| 616 |
// the parent to nil since it can be the message that started the thread. |
| 617 |
if ([[aMessage allReferences] count] == 0 && |
| 618 |
[aMessage headerValueForName: @"In-Reply-To"] == nil) |
| 619 |
{ |
| 620 |
[aContainer setParent: nil]; |
| 621 |
} |
| 622 |
// If we have no References but an In-Reply-To field, that becomes our parent. |
| 623 |
else if ([[aMessage allReferences] count] == 0 && |
| 624 |
[aMessage headerValueForName: @"In-Reply-To"]) |
| 625 |
{ |
| 626 |
[aContainer setParent: (CWContainer *)NSMapGet(id_table, [aMessage headerValueForName: @"In-Reply-To"])]; |
| 627 |
// FIXME, should we really do that? or should we do it in B? |
| 628 |
[(CWContainer *)NSMapGet(id_table, [aMessage headerValueForName: @"In-Reply-To"]) setChild: aContainer]; |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
[aContainer setParent: (CWContainer *)NSMapGet(id_table, aReference)]; |
| 633 |
[(CWContainer *)NSMapGet(id_table, aReference) setChild: aContainer]; |
| 634 |
} |
| 635 |
|
| 636 |
} // for (i = 0; ... |
| 637 |
|
| 638 |
// |
| 639 |
// 2. Find the root set. |
| 640 |
// |
| 641 |
[_allContainers addObjectsFromArray: NSAllMapTableValues(id_table)]; |
| 642 |
|
| 643 |
//while (NO) |
| 644 |
for (i = ([_allContainers count]); i > 0; i--) |
| 645 |
{ |
| 646 |
CWContainer *aContainer; |
| 647 |
|
| 648 |
aContainer = [_allContainers objectAtIndex: i-1]; |
| 649 |
|
| 650 |
if (aContainer->parent != nil) |
| 651 |
{ |
| 652 |
[_allContainers removeObjectAtIndex: i-1]; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
// |
| 657 |
// 3. Discard id_table. |
| 658 |
// |
| 659 |
NSFreeMapTable(id_table); |
| 660 |
|
| 661 |
|
| 662 |
// |
| 663 |
// 4. Prune empty containers. |
| 664 |
// |
| 665 |
//while (NO) |
| 666 |
for (i = ([_allContainers count]); i > 0; i--) |
| 667 |
{ |
| 668 |
CWContainer *aContainer; |
| 669 |
|
| 670 |
aContainer = [_allContainers objectAtIndex: i-1]; |
| 671 |
|
| 672 |
// Recursively walk all containers under the root set. |
| 673 |
while (aContainer) |
| 674 |
{ |
| 675 |
// A. If it is an empty container with no children, nuke it |
| 676 |
if (aContainer->message == nil && |
| 677 |
aContainer->child == nil) |
| 678 |
{ |
| 679 |
// We nuke it |
| 680 |
// FIXME: Won't work for non-root containers. |
| 681 |
[_allContainers removeObject: aContainer]; |
| 682 |
} |
| 683 |
|
| 684 |
// B. If the Container has no Message, but does have children, remove this container but |
| 685 |
// promote its children to this level (that is, splice them in to the current child list.) |
| 686 |
// Do not promote the children if doing so would promote them to the root set |
| 687 |
// -- unless there is only one child, in which case, do. |
| 688 |
// FIXME: We promote to the root no matter what :) |
| 689 |
if (aContainer->message == nil && aContainer->child) |
| 690 |
{ |
| 691 |
CWContainer *c; |
| 692 |
|
| 693 |
c = aContainer; |
| 694 |
RETAIN(c); |
| 695 |
[c->child setParent: nil]; |
| 696 |
[_allContainers removeObject: c]; |
| 697 |
[_allContainers addObject: c->child]; // We promote the the root for now |
| 698 |
|
| 699 |
// We go to our child and we continue to loop |
| 700 |
//aContainer = aContainer->child; |
| 701 |
aContainer = [aContainer childAtIndex: ([aContainer count]-1)]; |
| 702 |
RELEASE(c); |
| 703 |
continue; |
| 704 |
} |
| 705 |
|
| 706 |
//aContainer = aContainer->child; |
| 707 |
aContainer = [aContainer childAtIndex: ([aContainer count]-1)]; |
| 708 |
} |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
// |
| 713 |
// 5. Group root set by subject. |
| 714 |
// |
| 715 |
// A. Construct a new hash table, subject_table, which associates subject |
| 716 |
// strings with Container objects. |
| 717 |
subject_table = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16); |
| 718 |
|
| 719 |
// |
| 720 |
// B. For each Container in the root set: |
| 721 |
// |
| 722 |
|
| 723 |
//while (NO) |
| 724 |
for (i = 0; i < [_allContainers count]; i++) |
| 725 |
{ |
| 726 |
CWContainer *aContainer; |
| 727 |
CWMessage *aMessage; |
| 728 |
NSString *aString; |
| 729 |
|
| 730 |
aContainer = [_allContainers objectAtIndex: i]; |
| 731 |
aMessage = aContainer->message; |
| 732 |
aString = [aMessage subject]; |
| 733 |
|
| 734 |
if (aString) |
| 735 |
{ |
| 736 |
aString = [aMessage baseSubject]; |
| 737 |
|
| 738 |
// If the subject is now "", give up on this Container. |
| 739 |
if ([aString length] == 0) |
| 740 |
{ |
| 741 |
//aContainer = aContainer->child; |
| 742 |
continue; |
| 743 |
} |
| 744 |
|
| 745 |
// We set the new subject |
| 746 |
//[aMessage setSubject: aString]; |
| 747 |
|
| 748 |
// Add this Container to the subject_table if: |
| 749 |
// o There is no container in the table with this subject, or |
| 750 |
// o This one is an empty container and the old one is not: |
| 751 |
// the empty one is more interesting as a root, so put it in the table instead. |
| 752 |
// o The container in the table has a ``Re:'' version of this subject, |
| 753 |
// and this container has a non-``Re:'' version of this subject. |
| 754 |
// The non-re version is the more interesting of the two. |
| 755 |
if (!NSMapGet(subject_table, aString)) |
| 756 |
{ |
| 757 |
NSMapInsert(subject_table, aString, aContainer); |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
NSString *aSubject; |
| 762 |
|
| 763 |
// We obtain the subject of the message of our container. |
| 764 |
aSubject = [((CWContainer *)NSMapGet(subject_table, aString))->message subject]; |
| 765 |
|
| 766 |
if ([aSubject hasREPrefix] && ![[aMessage subject] hasREPrefix]) |
| 767 |
{ |
| 768 |
// We replace the container |
| 769 |
NSMapRemove(subject_table, aString); |
| 770 |
NSMapInsert(subject_table, [aMessage subject], aContainer); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
} // if ( aString ) |
| 775 |
} |
| 776 |
|
| 777 |
// |
| 778 |
// C. Now the subject_table is populated with one entry for each subject which occurs in |
| 779 |
// the root set. Now iterate over the root set, and gather together the difference. |
| 780 |
// |
| 781 |
for (i = ([_allContainers count]); i > 0; i--) |
| 782 |
{ |
| 783 |
CWContainer *aContainer, *containerFromTable; |
| 784 |
NSString *aSubject, *aString; |
| 785 |
|
| 786 |
aContainer = [_allContainers objectAtIndex: i-1]; |
| 787 |
|
| 788 |
// Find the subject of this Container (as above.) |
| 789 |
aSubject = [aContainer->message subject]; |
| 790 |
aString = [aContainer->message baseSubject]; |
| 791 |
|
| 792 |
// Look up the Container of that subject in the table. |
| 793 |
// If it is null, or if it is this container, continue. |
| 794 |
containerFromTable = NSMapGet(subject_table, aString); |
| 795 |
if (!containerFromTable || containerFromTable == aContainer) |
| 796 |
{ |
| 797 |
continue; |
| 798 |
} |
| 799 |
|
| 800 |
// If that container is a non-empty, and that message's subject does |
| 801 |
// not begin with ``Re:'', but this message's subject does, then make this be a child of the other. |
| 802 |
if (![[containerFromTable->message subject] hasREPrefix] && |
| 803 |
[aSubject hasREPrefix]) |
| 804 |
{ |
| 805 |
[aContainer setParent: containerFromTable]; |
| 806 |
[containerFromTable setChild: aContainer]; |
| 807 |
[_allContainers removeObject: aContainer]; |
| 808 |
} |
| 809 |
// If that container is a non-empty, and that message's subject begins with ``Re:'', |
| 810 |
// but this message's subject does not, then make that be a child of this one -- |
| 811 |
// they were misordered. (This happens somewhat implicitly, since if there are two |
| 812 |
// messages, one with Re: and one without, the one without will be in the hash table, |
| 813 |
// regardless of the order in which they were seen.) |
| 814 |
else if ([[containerFromTable->message subject] hasREPrefix] && |
| 815 |
![aSubject hasREPrefix]) |
| 816 |
{ |
| 817 |
[containerFromTable setParent: aContainer]; |
| 818 |
[aContainer setChild: containerFromTable]; |
| 819 |
[_allContainers removeObject: containerFromTable]; |
| 820 |
} |
| 821 |
// Otherwise, make a new empty container and make both msgs be a child of it. |
| 822 |
// This catches the both-are-replies and neither-are-replies cases, and makes them |
| 823 |
// be siblings instead of asserting a hierarchical relationship which might not be true. |
| 824 |
else |
| 825 |
{ |
| 826 |
#if 0 |
| 827 |
// FIXME - not so sure about that step. |
| 828 |
CWContainer *aNewContainer; |
| 829 |
|
| 830 |
aNewContainer = [[CWContainer alloc] init]; |
| 831 |
|
| 832 |
[aContainer setParent: aNewContainer]; |
| 833 |
[containerFromTable setParent: aNewContainer]; |
| 834 |
|
| 835 |
[aNewContainer setChild: aContainer]; |
| 836 |
[aNewContainer setChild: containerFromTable]; |
| 837 |
[_allContainers addObject: aNewContainer]; |
| 838 |
RELEASE(aNewContainer); |
| 839 |
|
| 840 |
// We remove .. |
| 841 |
[_allContainers removeObject: aContainer]; |
| 842 |
[_allContainers removeObject: containerFromTable]; |
| 843 |
#endif |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
NSFreeMapTable(subject_table); |
| 848 |
|
| 849 |
// |
| 850 |
// 6. Now you're done threading! |
| 851 |
// |
| 852 |
// Specifically, you no longer need the ``parent'' slot of the Container object, |
| 853 |
// so if you wanted to flush the data out into a smaller, longer-lived structure, you |
| 854 |
// could reclaim some storage as a result. |
| 855 |
// |
| 856 |
// GNUMail.app DOES USE the parent slot so we keep it. |
| 857 |
|
| 858 |
// |
| 859 |
// 7. Now, sort the siblings. |
| 860 |
// |
| 861 |
// At this point, the parent-child relationships are set. However, the sibling ordering |
| 862 |
// has not been adjusted, so now is the time to walk the tree one last time and order the siblings |
| 863 |
// by date, sender, subject, or whatever. This step could also be merged in to the end of step 4, |
| 864 |
// above, but it's probably clearer to make it be a final pass. If you were careful, you could |
| 865 |
// also sort the messages first and take care in the above algorithm to not perturb the ordering, |
| 866 |
// but that doesn't really save anything. |
| 867 |
// |
| 868 |
// By default we at least sort everything by number. |
| 869 |
//[_allContainers sortUsingSelector: @selector(compareAccordingToNumber:)]; |
| 870 |
|
| 871 |
RELEASE(pool); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
// |
| 876 |
// |
| 877 |
// |
| 878 |
- (void) unthread |
| 879 |
{ |
| 880 |
NSUInteger count; |
| 881 |
|
| 882 |
count = [_allMessages count]; |
| 883 |
|
| 884 |
while (count--) |
| 885 |
{ |
| 886 |
[[_allMessages objectAtIndex: count] setProperty: nil forKey: @"Container"]; |
| 887 |
} |
| 888 |
|
| 889 |
DESTROY(_allContainers); |
| 890 |
} |
| 891 |
|
| 892 |
// |
| 893 |
// |
| 894 |
// |
| 895 |
- (void) search: (NSString *) theString |
| 896 |
mask: (PantomimeSearchMask) theMask |
| 897 |
options: (PantomimeSearchOption) theOptions |
| 898 |
{ |
| 899 |
[self subclassResponsibility: _cmd]; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
// |
| 904 |
// |
| 905 |
// |
| 906 |
- (CWCacheManager *) cacheManager |
| 907 |
{ |
| 908 |
return _cacheManager; |
| 909 |
} |
| 910 |
|
| 911 |
- (void) setCacheManager: (id) theCacheManager |
| 912 |
{ |
| 913 |
ASSIGN(_cacheManager, theCacheManager); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
// |
| 918 |
// |
| 919 |
// |
| 920 |
- (PantomimeFolderMode) mode |
| 921 |
{ |
| 922 |
return _mode; |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
// |
| 927 |
// |
| 928 |
// |
| 929 |
- (void) setMode: (PantomimeFolderMode) theMode |
| 930 |
{ |
| 931 |
_mode = theMode; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
// |
| 936 |
// |
| 937 |
// |
| 938 |
- (void) setFlags: (CWFlags *) theFlags |
| 939 |
messages: (NSArray *) theMessages |
| 940 |
{ |
| 941 |
NSUInteger c, i; |
| 942 |
|
| 943 |
c = [theMessages count]; |
| 944 |
for (i = 0; i < c; i++) |
| 945 |
{ |
| 946 |
[[theMessages objectAtIndex: i] setFlags: theFlags]; |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
// |
| 952 |
// |
| 953 |
// |
| 954 |
- (id) propertyForKey: (id) theKey |
| 955 |
{ |
| 956 |
return [_properties objectForKey: theKey]; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
// |
| 961 |
// |
| 962 |
// |
| 963 |
- (void) setProperty: (id) theProperty |
| 964 |
forKey: (id) theKey |
| 965 |
{ |
| 966 |
if (theProperty) |
| 967 |
{ |
| 968 |
[_properties setObject: theProperty forKey: theKey]; |
| 969 |
} |
| 970 |
else |
| 971 |
{ |
| 972 |
[_properties removeObjectForKey: theKey]; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
@end |
| 977 |
|
| 978 |
|
| 979 |
|