File: CWFolder.m

package info (click to toggle)
pantomime 1.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: objc: 22,039; makefile: 11; sh: 4
file content (979 lines) | stat: -rw-r--r-- 21,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/*
**  CWFolder.m
**
**  Copyright (c) 2001-2007 Ludovic Marcotte
**                2013-2020 Riccardo Mottola
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Pantomime/CWFolder.h>

#import <Pantomime/CWConstants.h>
#import <Pantomime/CWContainer.h>
#import <Pantomime/CWFlags.h>
#import <Pantomime/CWMessage.h>
#import <Pantomime/NSString+Extensions.h>

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSMapTable.h>

//
//
//
@implementation CWFolder 

- (id) initWithName: (NSString *) theName
{
  self = [super init];

  if (self)
    {
      _properties = [[NSMutableDictionary alloc] init];
      _allVisibleMessages = nil;
  
      _allMessages = [[NSMutableArray alloc] init];
  
      //
      // By default, we don't do message threading so we don't
      // initialize this ivar for no reasons
      //
      _allContainers = nil;
      _cacheManager = nil;
      _mode = PantomimeUnknownMode;

      [self setName: theName];
      [self setShowDeleted: NO];
      [self setShowRead: YES];
    }
  return self;
}


//
//
//
- (void) dealloc
{
  //NSLog(@"Folder: -dealloc");
  RELEASE(_properties);
  RELEASE(_name);
  TEST_RELEASE(_allContainers);

  //
  // To be safe, we set the value of the _folder ivar of all CWMessage
  // instances to nil value in case something is retaining them.
  //
  [_allMessages makeObjectsPerformSelector: @selector(setFolder:) withObject: nil];
  RELEASE(_allMessages);

  TEST_RELEASE(_allVisibleMessages);
  TEST_RELEASE(_cacheManager);

  [super dealloc];
}


//
// NSCopying protocol (FIXME)
//
- (id) copyWithZone: (NSZone *) zone
{
  return RETAIN(self);
}


//
//
//
- (NSString *) name
{
  return _name;
}


//
//
//
- (void) setName: (NSString *) theName
{
  ASSIGN(_name, theName);
}


//
//
//
- (void) appendMessage: (CWMessage *) theMessage
{
  if (theMessage)
    {
      [_allMessages addObject: theMessage];
      
      if (_allVisibleMessages)
	{
	  [_allVisibleMessages addObject: theMessage];
	}

      // FIXME
      // If we've done message threading, we simply append the message
      // to the end of our containers array. We might want to place
      // it in the right thread in the future.
      if (_allContainers)
	{
	  CWContainer *aContainer;

	  aContainer = [[CWContainer alloc] init];
	  aContainer->message = theMessage;
	  [theMessage setProperty: aContainer  forKey: @"Container"];
	  [_allContainers addObject: aContainer];
	  RELEASE(aContainer);
	}
    }
}


//
//
//
- (void) appendMessageFromRawSource: (NSData *) theData
                              flags: (CWFlags *) theFlags
{
  [self subclassResponsibility: _cmd];
}


//
//
//
- (NSArray *) allContainers
{
  return _allContainers;
}

//
//
//
- (NSMutableArray *) messages
{
  return _allMessages;
}

//
//
//
- (NSArray *) visibleMessages
{ 
  if (_allVisibleMessages == nil)
    {
      NSUInteger i, count;

      count = [_allMessages count];
      _allVisibleMessages = [[NSMutableArray alloc] initWithCapacity: count];

      // quick
      if (_show_deleted && _show_read)
	{
	  [_allVisibleMessages addObjectsFromArray: _allMessages];
	  return _allVisibleMessages;
	}

      for (i = 0; i < count; i++)
	{
	  CWMessage *aMessage;
	  
	  aMessage = [_allMessages objectAtIndex: i];
      
	  // We show or hide deleted messages
	  if (_show_deleted)
	    {
	      [_allVisibleMessages addObject: aMessage];
	    }
	  else
	    {
	      if ([[aMessage flags] contain: PantomimeDeleted])
		{
		  // Do nothing
		  continue;
		}
	      else
		{
		  [_allVisibleMessages addObject: aMessage];
		}
	    }

	  // We show or hide read messages
	  if (_show_read)
	    {
	      if (![_allVisibleMessages containsObject: aMessage])
		{
		  [_allVisibleMessages addObject: aMessage];
		}
	    }
	  else
	    {
	      if ([[aMessage flags] contain: PantomimeSeen])
		{
		  if (![[aMessage flags] contain: PantomimeDeleted])
		    {
		      [_allVisibleMessages removeObject: aMessage];
		    }
		}
	      else if (![_allVisibleMessages containsObject: aMessage])
		{
		  [_allVisibleMessages addObject: aMessage];
		}
	    }
	}
    }

  return _allVisibleMessages;
}


//
//
//
- (void) setMessages: (NSArray *) theMessages
{
  if (theMessages)
    {
      RELEASE(_allMessages);
      _allMessages = [[NSMutableArray alloc] initWithArray: theMessages];

      if (_allContainers)
	{
	  [self thread];
	}
    }
  else
    {
      DESTROY(_allMessages);
    }

  DESTROY(_allVisibleMessages);
}


//
//
//
- (CWMessage *) messageAtIndex: (NSUInteger) theIndex
{
  if (theIndex >= [self countVisible])
    {
      return nil;
    }
  
  return [[self visibleMessages] objectAtIndex: theIndex];
}


//
//
//
- (NSUInteger) countVisible
{
  return [[self visibleMessages] count];
}


//
//
//
- (void) close
{
  [self subclassResponsibility: _cmd];
  return;
}


//
//
//
- (void) expunge
{
  [self subclassResponsibility: _cmd];
}


//
//
//
- (id) store
{
  return _store;
}


//
// No need to retain the store here since our store object
// retains our folder object.
//
- (void) setStore: (id) theStore
{
  _store = theStore;
}


//
//
//
- (void) removeMessage: (CWMessage *) theMessage
{
  if (theMessage)
    {
      [_allMessages removeObject: theMessage];
      
      if (_allVisibleMessages)
	{
	  [_allVisibleMessages removeObject: theMessage];
	}

      // FIXME - We must go through our _allContainers ivar in order
      //         to find the message that has just been removed from
      //         this folder. We must go through all levels.
      //         Right now, we simply do again our message threading algo
      if (_allContainers)
	{
	  [self thread];
	}
    }
}


//
//
//
- (BOOL) showDeleted
{
  return _show_deleted;
}


//
//
//
- (void) setShowDeleted: (BOOL) theBOOL
{
  if (theBOOL != _show_deleted)
    {
      _show_deleted = theBOOL;
      DESTROY(_allVisibleMessages);
    }
}


//
//
//
- (BOOL) showRead
{
  return _show_read;
}


//
//
//
- (void) setShowRead: (BOOL) theBOOL
{
  if (theBOOL != _show_read)
    {
      _show_read = theBOOL;
      DESTROY(_allVisibleMessages);
    }
}


//
//
//
- (NSUInteger) numberOfDeletedMessages
{
  NSUInteger c, i, count;
  
  c = [_allMessages count];
  count = 0;

  for (i = 0; i < c; i++)
    {
      if ([[[_allMessages objectAtIndex: i] flags] contain: PantomimeDeleted])
	{
	  count++;
	}
    }

  return count;
}


//
//
//
- (NSUInteger) numberOfUnreadMessages
{
  NSUInteger i, c, count;
  
  c = [_allMessages count];
  count = 0;
  
  for (i = 0; i < c; i++)
    {
      if (![[[_allMessages objectAtIndex: i] flags] contain: PantomimeSeen])
	{
	  count++;
	}
    }

  return count;
}


//
//
//
- (unsigned long) size;
{
  unsigned long size;
  NSUInteger c, i;

  c = [_allMessages count];
  size = 0;
  
  for (i = 0; i < c; i++)
    {
      size += [(CWMessage *)[_allMessages objectAtIndex: i] size];
    }

  return size;
  
}


//
//
//
- (void) updateCache
{
  DESTROY(_allVisibleMessages);
}


//
//
//
- (void) thread
{
  NSMapTable *id_table, *subject_table;
  NSAutoreleasePool *pool;
  NSUInteger i, count;

  // We clean up ...
  TEST_RELEASE(_allContainers);

  // We create our local autorelease pool
  pool = [[NSAutoreleasePool alloc] init];

  // Build id_table and our containers mutable array
  id_table = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
  _allContainers = [[NSMutableArray alloc] init];

  //
  // 1. A., B. and C.
  //
  count = [_allMessages count];
  for (i = 0; i < count; i++)
    {
      CWContainer *aContainer;
      CWMessage *aMessage;
      
      NSString *aReference;
      NSUInteger j;

      // So that gcc shutup
      aMessage = nil;
      aReference = nil;

      aMessage = [_allMessages objectAtIndex: i];
      
      // We skip messages that don't have a valid Message-ID
      if (![aMessage messageID])
      	{
	  aContainer = [[CWContainer alloc] init];
	  aContainer->message = aMessage;
	  [aMessage setProperty: aContainer  forKey: @"Container"];
	  [_allContainers addObject: aContainer];
	  RELEASE(aContainer);
      	  continue;
      	}
      
      //
      // A.
      //
      aContainer = NSMapGet(id_table, [aMessage messageID]);
      
      if (aContainer)
	{
	  //aContainer->message = aMessage;
	  
	  if (aContainer->message != aMessage)
	    {
	      aContainer = [[CWContainer alloc] init];
	      aContainer->message = aMessage;
	      [aMessage setProperty: aContainer  forKey: @"Container"];
	      NSMapInsert(id_table, [aMessage messageID], aContainer);
	      DESTROY(aContainer);
	    }
	}
      else
	{
	  aContainer = [[CWContainer alloc] init];
	  aContainer->message = aMessage;
	  [aMessage setProperty: aContainer  forKey: @"Container"];
	  NSMapInsert(id_table, [aMessage messageID], aContainer);
	  DESTROY(aContainer);
	}
      
      //
      // B. For each element in the message's References field:
      //
      for (j = 0; j < [[aMessage allReferences] count]; j++)
	{
	  // We get a Message-ID
	  aReference = [[aMessage allReferences] objectAtIndex: j];

	  // Find a container object for the given Message-ID
	  aContainer = NSMapGet(id_table, aReference);
	  
	  if (aContainer)
	    {
	      // We found it. We use that.
	    }
	  // Otherwise, make (and index) one (new Container) with a null Message
	  else 
	    {
	      aContainer = [[CWContainer alloc] init];
	      NSMapInsert(id_table, aReference, aContainer);
	      RELEASE(aContainer);
	    }
	  
	  // NOTE:
	  // aContainer is valid here. It points to the message (could be a nil message)
	  // that has a Message-ID equals to the current aReference value.
	  
	  // If we are currently using the last References's entry of our list,
	  // we simply break the loop since we are gonna set it in C.
	  //if ( j == ([[aMessage allReferences] count] - 1) )
	  //  {
	  //    break;
	  // }

	  // Link the References field's Containers together in the order implied by the References header.
	  // The last references
	  if (j == ([[aMessage allReferences] count] - 1) &&
	      aContainer->parent == nil)
	    {
	      // We grab the container of our current message
	      [((CWContainer *)NSMapGet(id_table, [aMessage messageID])) setParent: aContainer];
	    }
	  
	  // We set the child
	  //if ( aContainer->message != aMessage &&
	  //     aContainer->child == nil )
	  //  {
	  //    [aContainer setChild: NSMapGet(id_table, [aMessage messageID])];
	  //  }	      

	} // for (j = 0; ...
      
      // NOTE: The loop is over here. It was an ascending loop so
      //       aReference points to the LAST reference in our References list

      //
      // C. Set the parent of this message to be the last element in References. 
      //
      // NOTE: Again, aReference points to the last Message-ID in the References list
      
      // We get the container for the CURRENT message
      aContainer = (CWContainer *)NSMapGet(id_table, [aMessage messageID]);
      
      // If we have no References and no In-Reply-To fields, we simply set a
      // the parent to nil since it can be the message that started the thread.
      if ([[aMessage allReferences] count] == 0 &&
	  [aMessage headerValueForName: @"In-Reply-To"] == nil)
	{
	  [aContainer setParent: nil];
	}
      // If we have no References but an In-Reply-To field, that becomes our parent.
      else if ([[aMessage allReferences] count] == 0 &&
	       [aMessage headerValueForName: @"In-Reply-To"])
	{
	  [aContainer setParent: (CWContainer *)NSMapGet(id_table, [aMessage headerValueForName: @"In-Reply-To"])];
	  // FIXME, should we really do that? or should we do it in B?
	  [(CWContainer *)NSMapGet(id_table, [aMessage headerValueForName: @"In-Reply-To"]) setChild: aContainer];
	}
      else
	{
	  [aContainer setParent: (CWContainer *)NSMapGet(id_table, aReference)];
	  [(CWContainer *)NSMapGet(id_table, aReference) setChild: aContainer];
	}
      
    } // for (i = 0; ...

  //
  // 2. Find the root set.
  //
  [_allContainers addObjectsFromArray: NSAllMapTableValues(id_table)];

  //while (NO)
  for (i = ([_allContainers count]); i > 0; i--)
    {
      CWContainer *aContainer;
      
      aContainer = [_allContainers objectAtIndex: i-1];
      
      if (aContainer->parent != nil)
	{
	  [_allContainers removeObjectAtIndex: i-1];
	}
    }

  //
  // 3. Discard id_table.
  //
  NSFreeMapTable(id_table);

  
  //
  // 4. Prune empty containers.
  //
  //while (NO)
  for (i = ([_allContainers count]); i > 0; i--)
    {
      CWContainer *aContainer;

      aContainer = [_allContainers objectAtIndex: i-1];
      
      // Recursively walk all containers under the root set.
      while (aContainer)
	{
	  // A. If it is an empty container with no children, nuke it
	  if (aContainer->message == nil &&
	      aContainer->child == nil)
	    {
	      // We nuke it
	      // FIXME: Won't work for non-root containers.
	      [_allContainers removeObject: aContainer];
	    }
	  
	  // B. If the Container has no Message, but does have children, remove this container but 
	  //    promote its children to this level (that is, splice them in to the current child list.)
	  //    Do not promote the children if doing so would promote them to the root set 
	  //    -- unless there is only one child, in which case, do. 
	  // FIXME: We promote to the root no matter what :)
	  if (aContainer->message == nil && aContainer->child)
	    {
	      CWContainer *c;
	      
	      c = aContainer;
	      RETAIN(c);
	      [c->child setParent: nil];
	      [_allContainers removeObject: c];
	      [_allContainers addObject: c->child]; // We promote the the root for now
	     
	      // We go to our child and we continue to loop
	      //aContainer = aContainer->child;
	      aContainer = [aContainer childAtIndex: ([aContainer count]-1)];
	      RELEASE(c);
	      continue;
	    }
	  
	  //aContainer = aContainer->child;
	  aContainer = [aContainer childAtIndex: ([aContainer count]-1)];
	}

    }
  
  //
  // 5. Group root set by subject.
  //
  // A. Construct a new hash table, subject_table, which associates subject 
  //    strings with Container objects.
  subject_table = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);

  //
  // B. For each Container in the root set:
  //
    
  //while (NO)
  for (i = 0; i < [_allContainers count]; i++)
    {
      CWContainer *aContainer;
      CWMessage *aMessage;
      NSString *aString;

      aContainer = [_allContainers objectAtIndex: i];
      aMessage = aContainer->message;
      aString = [aMessage subject];
      
      if (aString)
	{
	  aString = [aMessage baseSubject];

	  // If the subject is now "", give up on this Container.
	  if ([aString length] == 0)
	    {
	      //aContainer = aContainer->child;
	      continue;
	    }
	  
	  // We set the new subject
	  //[aMessage setSubject: aString];
	  
	  // Add this Container to the subject_table if:
	  // o There is no container in the table with this subject, or
	  // o This one is an empty container and the old one is not: 
	  //   the empty one is more interesting as a root, so put it in the table instead.
	  // o The container in the table has a ``Re:'' version of this subject, 
	  //   and this container has a non-``Re:'' version of this subject. 
	  //   The non-re version is the more interesting of the two.
	  if (!NSMapGet(subject_table, aString))
	    {
	      NSMapInsert(subject_table, aString, aContainer);
	    }
	  else
	    {
	      NSString *aSubject;
	      
	      // We obtain the subject of the message of our container.
	      aSubject = [((CWContainer *)NSMapGet(subject_table, aString))->message subject];

	      if ([aSubject hasREPrefix] && ![[aMessage subject] hasREPrefix])
		{
		  // We replace the container
		  NSMapRemove(subject_table, aString);
		  NSMapInsert(subject_table, [aMessage subject], aContainer);
		}
	    }
	  
	} // if ( aString )
    }
  
  //
  // C. Now the subject_table is populated with one entry for each subject which occurs in 
  //    the root set. Now iterate over the root set, and gather together the difference.
  //
  for (i = ([_allContainers count]); i > 0; i--)
    {
      CWContainer *aContainer, *containerFromTable;
      NSString *aSubject, *aString;

      aContainer = [_allContainers objectAtIndex: i-1];
      
      // Find the subject of this Container (as above.)
      aSubject = [aContainer->message subject];
      aString = [aContainer->message baseSubject];
      
      // Look up the Container of that subject in the table.
      // If it is null, or if it is this container, continue.
      containerFromTable = NSMapGet(subject_table, aString);
      if (!containerFromTable || containerFromTable == aContainer) 
	{
	  continue; 
	}
      
      // If that container is a non-empty, and that message's subject does 
      // not begin with ``Re:'', but this message's subject does, then make this be a child of the other.
      if (![[containerFromTable->message subject] hasREPrefix] &&
	  [aSubject hasREPrefix])
	{
	  [aContainer setParent: containerFromTable];
	  [containerFromTable setChild: aContainer]; 
	  [_allContainers removeObject: aContainer];
	}
      // If that container is a non-empty, and that message's subject begins with ``Re:'', 
      // but this  message's subject does not, then make that be a child of this one -- 
      // they were misordered. (This happens somewhat implicitly, since if there are two
      // messages, one with Re: and one without, the one without will be in the hash table,
      // regardless of the order in which they were seen.)
      else if ([[containerFromTable->message subject] hasREPrefix] &&
	       ![aSubject hasREPrefix])
	{
	  [containerFromTable setParent: aContainer];
	  [aContainer setChild: containerFromTable]; 
	  [_allContainers removeObject: containerFromTable];
	}
      // Otherwise, make a new empty container and make both msgs be a child of it. 
      // This catches the both-are-replies and neither-are-replies cases, and makes them 
      // be siblings instead of asserting a hierarchical relationship which might not be true.
      else
	{
#if 0
	  // FIXME - not so sure about that step.
	  CWContainer *aNewContainer;
	  
	  aNewContainer = [[CWContainer alloc] init];
	  
	  [aContainer setParent: aNewContainer];
	  [containerFromTable setParent: aNewContainer];
	  
	  [aNewContainer setChild: aContainer]; 
	  [aNewContainer setChild: containerFromTable];
	  [_allContainers addObject: aNewContainer];
	  RELEASE(aNewContainer);
	  
	  // We remove ..
	  [_allContainers removeObject: aContainer];
	  [_allContainers removeObject: containerFromTable];
#endif
	}
    }
  
  NSFreeMapTable(subject_table);

  //
  // 6.  Now you're done threading!
  //
  //     Specifically, you no longer need the ``parent'' slot of the Container object, 
  //     so if you wanted to flush the data out into a smaller, longer-lived structure, you 
  //     could reclaim some storage as a result. 
  //
  // GNUMail.app DOES USE the parent slot so we keep it.

  //
  // 7.  Now, sort the siblings.
  //     
  //     At this point, the parent-child relationships are set. However, the sibling ordering 
  //     has not been adjusted, so now is the time to walk the tree one last time and order the siblings 
  //     by date, sender, subject, or whatever. This step could also be merged in to the end of step 4, 
  //     above, but it's probably clearer to make it be a final pass. If you were careful, you could 
  //     also sort the messages first and take care in the above algorithm to not perturb the ordering,
  //      but that doesn't really save anything. 
  //
  // By default we at least sort everything by number.
  //[_allContainers sortUsingSelector: @selector(compareAccordingToNumber:)];

  RELEASE(pool);
}


//
//
//
- (void) unthread
{
  NSUInteger count;

  count = [_allMessages count];
  
  while (count--)
    {
      [[_allMessages objectAtIndex: count] setProperty: nil  forKey: @"Container"];
    }

  DESTROY(_allContainers);
}

//
//
//
- (void) search: (NSString *) theString
	   mask: (PantomimeSearchMask) theMask
	options: (PantomimeSearchOption) theOptions
{
  [self subclassResponsibility: _cmd];
}


//
//
//
- (CWCacheManager *) cacheManager
{
  return _cacheManager;
}

- (void) setCacheManager: (id) theCacheManager
{
  ASSIGN(_cacheManager, theCacheManager);
}


//
//
//
- (PantomimeFolderMode) mode
{
  return _mode;
}


//
//
//
- (void) setMode: (PantomimeFolderMode) theMode
{
  _mode = theMode;
}


//
//
//
- (void) setFlags: (CWFlags *) theFlags
         messages: (NSArray *) theMessages
{
  NSUInteger c, i;

  c = [theMessages count];
  for (i = 0; i < c; i++)
    {
      [[theMessages objectAtIndex: i] setFlags: theFlags];
    }
}


//
//
//
- (id) propertyForKey: (id) theKey
{
  return [_properties objectForKey: theKey];
}


//
//
//
- (void) setProperty: (id) theProperty
	      forKey: (id) theKey
{
  if (theProperty)
    {
      [_properties setObject: theProperty  forKey: theKey];
    }
  else
    {
      [_properties removeObjectForKey: theKey];
    }
}

@end