/* -*-C-*-
*******************************************************************************
*
* File:         EnhanceMail.m
* RCS:          $Header: /usr/local/lib/cvs/EnhanceMail/EnhanceMail.m,v 1.1.1.16 1996/06/25 13:44:09 cedman Exp $
* Description:  
* Author:       Carl Edman
* Created:      Fri Oct 13 11:48:05 1995
* Modified:     Tue Jun 25 09:20:02 1996 (Carl Edman) cedman@capitalist.princeton.edu
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
* (C) Copyright 1995, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import "EnhanceMail.h"
#import "Preferences.h"
#import <ctype.h>
#import <sys/wait.h>

NXBundle *EnhanceBundle=0;
char *EnhanceVersion="X";
char *regerrval=0;

static const char *CVSName="$Name: release2_0b4 $";
static BOOL windowsOnUnhide=NO;

BOOL EnhanceControlP(void)
   {
   NXEvent *ev=[NXApp currentEvent];
   return ev && (ev->flags & NX_CONTROLMASK);
   }

BOOL EnhanceShiftP(void)
   {
   NXEvent *ev=[NXApp currentEvent];
   return ev && (ev->flags & NX_SHIFTMASK);
   }

BOOL EnhanceAlternateP(void)
   {
   NXEvent *ev=[NXApp currentEvent];
   return ev && (ev->flags & NX_ALTERNATEMASK);
   }

BOOL EnhancePaths(const char *paths[])
   {
   int i;
   char path[MAXPATHLEN+1];
   
   if (EnhanceBundle==0)
      EnhanceBundle=[NXBundle bundleForClass:[EnhanceMail class]];
   
   for(i=0;paths[i];i++)
      {
      if ((paths[i][0]=='~') && (paths[i][1]=='/'))
         {
         sprintf(path,"%s%s",NXHomeDirectory(),paths[i]+1);
         paths[i]=NXUniqueString(path);
         }
      else if ((paths[i][0]=='#') && (paths[i][1]=='/'))
         {
         sprintf(path,"%s%s",[EnhanceBundle directory],paths[i]+1);
         paths[i]=NXUniqueString(path);
         }
      }

   return YES;
   }

void regerror(char *s)
   {
   [NXApp logError:"regexp(3): %s",s];
   }

@implementation EnhanceMail
+ finishLoading:(struct mach_header *)header
   {
   const char *beg=CVSName,*end=CVSName+strlen(CVSName);
   
   [self poseAs:[self superclass]];
   [NXApp changeClassTo:[self class]];

   while((beg<=end) && !isdigit(*beg)) beg++;
   while((beg<=end) && !isdigit(*end)) end--;
   
   if (beg<end)
      {
      char *c;

      end++;
      EnhanceVersion=strncpy(malloc(end-beg+1),beg,end-beg);
      EnhanceVersion[end-beg]='\0';
      for(c=EnhanceVersion;*c;c++) if (*c=='_') *c='.';
      }
   
   return self;
   }

- initialCheck:sender
   {
   if ([self isHidden])
      {
      windowsOnUnhide=YES;
      }
   else
      {
      if (EnhanceLaunchMailboxes) [(MailDriver *)NXApp mailboxes:self];
      if (EnhanceLaunchAddresses) [(MailDriver *)NXApp address:self];
      }
   return [super initialCheck:sender];
   }

- appDidUnhide:sender
   {
   if (windowsOnUnhide)
      {
      if (EnhanceLaunchMailboxes) [(MailDriver *)NXApp mailboxes:self];
      if (EnhanceLaunchAddresses) [(MailDriver *)NXApp address:self];
      windowsOnUnhide=NO;
      }
   return [super appDidUnhide:sender];
   }

- (BOOL)updateCell:cell
   {
   return [super updateCell:cell];
   }
@end

@implementation Menu(EnhanceMail)
- findCellWithTitle:(const char *)title
   {
   id cell=nil;
   int x, y, rows, cols;
   const char *c,*end;

   if (title==0) return nil;
   for(end=title;(*end) && (*end!='/');end++);
   
   [matrix getNumRows:&rows numCols:&cols];
   for(y=0;(y<rows) && (cell==nil);y++) for(x=0;(x<cols) && (cell==nil);x++)
      {
      if ((cell=[matrix cellAt:y:x])==nil) continue;
      
      if (((c=[cell title])==0)||(strncmp(title,c,end-title)!=0)||(c[end-title]!='\0'))
         cell=nil;
      }

   if (cell==nil)
      return nil;
   else if ((*end!='\0') && [cell hasSubmenu])
      return [[cell target] findCellWithTitle:end+1];
   else if (*end=='\0')
      return cell;
   else
      return nil;
   }

- addSlashItem:(const char *)title action:(SEL)sel keyEquivalent:(unsigned short)code
   {
   id cell=nil;
   int x, y, rows, cols;
   const char *c,*end;

   if (title==0) return nil;
   for(end=title;(*end) && (*end!='/');end++);

   [matrix getNumRows:&rows numCols:&cols];
   for(y=0;(y<rows) && (cell==nil);y++) for(x=0;(x<cols) && (cell==nil);x++)
      {
      if ((cell=[matrix cellAt:y:x])==nil) continue;
      
      if (((c=[cell title])==0)||(strncmp(title,c,end-title)!=0)||(c[end-title]!='\0'))
         cell=nil;
      }

   if ((cell==nil) && (*end=='\0'))
      {
      cell=[self addItem:title action:sel keyEquivalent:code];
      [self display];
      return cell;
      }
   else if (cell==nil)
      {
      char *d=alloca(end-title+1);
      strncpy(d,title,end-title);
      d[end-title]='\0';
      cell=[self addItem:d action:0 keyEquivalent:0];
      [self setSubmenu:[[Menu alloc] initTitle:d] forItem:cell];
      [self display];
      return [[cell target] addSlashItem:end+1 action:sel keyEquivalent:code];
      }
   else if ((*end!='\0') && [cell hasSubmenu])
      return [[cell target] addSlashItem:end+1 action:sel keyEquivalent:code];
   else if (*end=='\0')
      return cell;
   else
      return nil;
   }
@end

@implementation Object(EnhanceMail)
- changeClassTo:class
   {
   /* Don't try this at home, kids */
   isa=class;
   return self;
   }
@end

@implementation ButtonCell(EnhanceMail)
- (int)state
   {
   return [self intValue];
   }

- setState:(int)anInt
   {
   return [self setIntValue:anInt];
   }
@end
