#import "EnhancedText.h"
#import "EmacsText.h"
#import "StringStorage.h"
#import "MailController.h"
#import "MailMessage.h"
#import "FileShelfView.h"
#import "MasterController.h"
#import "Defaults.h"
#import "PopUpButton.h"
#import "ModuleMgr.h"
#import "version.h"

@implementation MailController

- init
{
   [super init];
   [self setDocType: "send"];
   MUA = [[StringStorage alloc] init: [NXApp appName]];
   [MUA appendStringValue: " ( %s )"];
   [MUA replaceSubstring: "%s" with: version + 9];
   MTA = [[StringStorage alloc] init: "/usr/lib/sendmail"];
   return self;
}

- free
{
   [MUA free];
   [MTA free];
   return [super free];
}
   
- awakeFromNib
{
   [self empty];
   [ccField setNextText: text];		     /* I have to do this here since setText broke connections */
   [self selectTypeByName: [[[NXApp delegate] defaults] get: "DefaultSendType" ]];
   return self;
}

- empty
{
   [toField setStringValue: ""];
   [ccField setStringValue: ""];
   [subjectField setStringValue: ""];
   [text empty: self];
   [shelf empty];
   [toField selectText: self];
   return self;
}

- deliver:sender
{
   id mailMessage = nil;
   const char *format = [typeList title];
   id bundle = [[[NXApp delegate] sendBundles] getModule: format];

   if(!bundle)
       return nil;

   if(!([toField stringValue]  && strlen([toField stringValue])))
   {
      NXRunAlertPanel([NXApp appName], "Mail requires at least a recipient.", (char *)0, (char *)0, (char *)0);
      return nil;
   }
   

   mailMessage = [[[bundle principalClass] alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];

   if(!mailMessage)
   {
      NXLogError("Unsupported type\n");
      return nil;
   }

   if([[mailMessage class] supportsAttachments])
       [mailMessage setAttachments: [shelf fileNames]];

   [mailMessage deliverTo: [toField stringValue]
                  subject: [subjectField stringValue]
                       cc: [ccField stringValue]
                     text: [text stringValue]];

   [panel orderOut: self];
   [self empty];
   [mailMessage free];
   return self;
}

- selectType: sender
{
   return [self selectTypeByName: [[sender selectedCell] title]];
}

- selectTypeByName: (const char *)str
{
   id bundle;

   bundle = [[[NXApp delegate] sendBundles] getModule: str];
   
   if(!bundle)
       return nil;
   
   [typeList setPopUpTitle: str];	     /* make sure UI reflects choice */

   if([[bundle principalClass] supportsAttachments])
       [shelf setEnabled: YES];
   else
       [shelf setEnabled: NO];

   return self;
}

- setTypeList: anObject
{
   id sendBundles = [[NXApp delegate] sendBundles];
   const   char  *key;

   id popUp = [anObject target];

   /* select first item in matrix and on button */
   typeList = anObject;

   [sendBundles initState]; 
   while (key = [sendBundles nextKey])
       [popUp addItem: (const char *)key];

   [popUp removeItemAt: 0];		     /* clear out the .nibs' item */

					     /* set up the target */
   [popUp setTarget: self];
   [popUp setAction: @selector(selectType:)];
   return self;
}

/* I need to place an EmacsText in a ScrollView. But Grouping in IB
 * creates one that can not be resized.  So I just swap out one
 * from a ScrollView/Text pulled right from the IB Palette.
 */
- setText: anObject
{
   id origText = [anObject docView];	     /* get the TeXT IB put in the scroller */
   id newText;
   NXRect theRect;

   [origText getFrame: &theRect];	     /* get its frame */
					     /* create an EmacsText using that Rect */
   newText = [[EmacsText alloc] initFrame: &theRect];

   [anObject setDocView: newText];	     /* set it as the scrollers docView */
   text = newText;			     /* get the pointer for later use */
   [origText free];			     /* free the one IB put in there. */
   return self;
}

- shelf
{
   return shelf;
}

@end

