/* -*-ObjC-*-
*******************************************************************************
*
* File:         MailboxSummary+Archiving.m
* RCS:          MailboxSummary+Archiving.m,v 1.1 1998/05/24 20:26:44 tom Exp
* Description:  Archiving category for MailboxSummary class
* Author:       Tom Hageman <tom@basil.icce.rug.nl>
* Created:      May 1998
* Modified:     
* Language:     Objective-C
* Package:      EnhanceMail
* Status:       Experimental
*
* Copyright (C) 1998 Tom Hageman, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/
#import <libc.h>
#import "MailboxSummary.h"


@implementation EnhanceMailboxSummary (Archiving)

// XXX This only reads/writes stat fields we are interested in.

static void readStat(NXTypedStream *stream, struct stat *st)
{
   NXReadType(stream, @encode(typeof(st->st_ino)), &st->st_ino);
   // NO! This is not portable in light of NFS mounts.
   ///NXReadType(stream, @encode(typeof(st->st_dev)), &st->st_dev);
   NXReadType(stream, @encode(typeof(st->st_mtime)), &st->st_mtime);
   NXReadType(stream, @encode(typeof(st->st_size)), &st->st_size);
}

static void writeStat(NXTypedStream *stream, struct stat *st)
{
   NXWriteType(stream, @encode(typeof(st->st_ino)), &st->st_ino);
   // NO! This is not portable in light of NFS mounts.
   ///NXWriteType(stream, @encode(typeof(st->st_dev)), &st->st_dev);
   NXWriteType(stream, @encode(typeof(st->st_mtime)), &st->st_mtime);
   NXWriteType(stream, @encode(typeof(st->st_size)), &st->st_size);
}


- read:(NXTypedStream *)stream
{
   char *shortName;
   int i;
   BOOL inSummaryHash;

   [super read:stream];

   NXReadType(stream, @encode(typeof(shortName)), &shortName);
   [self setName:shortName];
   free(shortName);

   NXReadType(stream, @encode(typeof(loadStatus)), &loadStatus);
   NXReadType(stream, @encode(typeof(accurate)), &accurate);
   NXReadType(stream, @encode(typeof(collectAll)), &collectAll);
#if 0
   NXReadType(stream, @encode(typeof(hasPendingAppnmail)), &hasPendingAppnmail);
   NXReadType(stream, @encode(typeof(hasNewMail)), &hasNewMail);
#endif
   NXReadArray(stream, @encode(typeof(counts[0])), sizeof(counts)/sizeof(counts[0]), counts);
   if (collectAll)
   {
      NXReadArray(stream, @encode(typeof(sizes[0])), sizeof(sizes)/sizeof(sizes[0]), sizes);
      NXReadArray(stream, @encode(typeof(attachCounts[0])), sizeof(attachCounts)/sizeof(attachCounts[0]), attachCounts);
      NXReadArray(stream, @encode(typeof(attachSizes[0])), sizeof(attachSizes)/sizeof(attachSizes[0]), attachSizes);
   }
   for (i = 0;  i < sizeof(stats)/sizeof(stats[0]);  i++)
   {
      readStat(stream, &stats[i]);
   }

   NXReadType(stream, @encode(typeof(inSummaryHash)), &inSummaryHash);
   if (inSummaryHash)
   {
      [[self class] setSummary:self forName:[self name]];
   }
   if (!fullname)
   {
      self = [self free];
   }
   return self;
}

- write:(NXTypedStream *)stream
{
   const char *shortName = [self shortName];
   int i;
   BOOL inSummaryHash;

   [super write:stream];

   NXWriteType(stream, @encode(typeof(shortName)), &shortName);

   NXWriteType(stream, @encode(typeof(loadStatus)), &loadStatus);
   NXWriteType(stream, @encode(typeof(accurate)), &accurate);
   NXWriteType(stream, @encode(typeof(collectAll)), &collectAll);
#if 0
   NXWriteType(stream, @encode(typeof(hasPendingAppnmail)), &hasPendingAppnmail);
   NXWriteType(stream, @encode(typeof(hasNewMail)), &hasNewMail);
#endif
   NXWriteArray(stream, @encode(typeof(counts[0])), sizeof(counts)/sizeof(counts[0]), counts);
   if (collectAll)
   {
      NXWriteArray(stream, @encode(typeof(sizes[0])), sizeof(sizes)/sizeof(sizes[0]), sizes);
      NXWriteArray(stream, @encode(typeof(attachCounts[0])), sizeof(attachCounts)/sizeof(attachCounts[0]), attachCounts);
      NXWriteArray(stream, @encode(typeof(attachSizes[0])), sizeof(attachSizes)/sizeof(attachSizes[0]), attachSizes);
   }
   for (i = 0;  i < sizeof(stats)/sizeof(stats[0]);  i++)
   {
      writeStat(stream, &stats[i]);
   }

   inSummaryHash = ([[self class] summaryForName:[self name] create:NO] != nil);
   NXWriteType(stream, @encode(typeof(inSummaryHash)), &inSummaryHash);

   return self;
}

@end // EnhanceMailboxSummary (Archiving)
