
/* Programmed  by Krishnaprasad Kamisetty */

#import "ArticleFile.h"

@implementation ArticleFile

/* Initialization Methods */

- setPlace:anObject
{
    place = anObject;
    return self;
}

- setPages:anObject
{
    pages = anObject;
    return self;
}

- setDate:anObject
{
    date = anObject;
    return self;
}

- setTitle:anObject
{
    title = anObject;
    return self;
}

- setKeyWord:anObject
{
    keyWord = anObject;
    return self;
}

- setDueOn:anObject
{
    dueOn = anObject;
    return self;
}

- setCountry:anObject
{
    country = anObject;
    return self;
}

- setNum:anObject
{
    num = anObject;
    return self;
}

- setComments:anObject
{
    comments = anObject;
    return self;
}

- setVol:anObject
{
    vol = anObject;
    return self;
}

- setLname:anObject
{
    lname = anObject;
    return self;
}

- setJournal:anObject
{
    journal = anObject;
    return self;
}

- setChkout:anObject
{
    chkout = anObject;
    return self;
}

- setFname:anObject
{
    fname = anObject;
    return self;
}

- setDirectory:anObject
{
    directory = anObject;
    [directory setStringValue:"Articles" at:0];
    [directory selectTextAt:0];
    return self;
}

- setWordField:anObject
{
   wordField = anObject;
   return self;
}

- setAbsWindow:anObject
{
    absWindow = anObject;
    [absWindow setFreeWhenClosed:NO];
    [absWindow close];
    return self;
}

- setAuthWindow:anObject
{
   authWindow = anObject;
  /*  [authWindow setFreeWhenClosed:NO];
   [authWindow close];  */
   return self;
}

- setArticleWindow:anObject
{
   articleWindow = anObject;
   [articleWindow setFreeWhenClosed:NO];
   [articleWindow close];
   return self;
}

- setKeyWindow:anObject
{
   keyWindow = anObject;
   [keyWindow setFreeWhenClosed:NO];
   [keyWindow close];
   return self;
}

- setNumberWindow:anObject
{
    numberWindow = anObject;
    [numberWindow setFreeWhenClosed:NO];
    [numberWindow close];
    return self;
}

- setDirectoryWindow:anObject
{
   directoryWindow = anObject;
   [directory setFreeWhenClosed:NO];
   [directory close];
   return self;
}

- setSearchWindow:anObject
{
    searchWindow = anObject;
    [searchWindow setFreeWhenClosed:NO];
    [searchWindow close];
    return self;
}

- setArticleNumber:anObject
{
    articleNumber = anObject;
    return self;
}
  
/* The actual Instance methods */

- addKeyWordAndClose:sender
{
     const char *temp1;
     char   temp[20];
         
    temp1 = [keyWord stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output,"%s.",temp);
    [NXApp stopModal];
    [keyWindow close];
    return self;
}

- addMoreKeyWords:sender
{
     const char *temp1;
     char temp[20];
     
    temp1 = [keyWord stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output," %s,",temp);
    [keyWord selectTextAt:0];
    return self;
}

- addArticleInfoAndClose:sender
{
     const char *temp1;
     char temp[75];
        
    temp1 = [title stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output, "\"%s\",",temp);
    temp1 = [journal stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output," %s,",temp);
    temp1 = [vol stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," Vol. %s,",temp);
    temp1 = [num stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," No. %s,",temp);
    temp1 = [date stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," %s,",temp);
    temp1 = [pages stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," pp %s,",temp);
    temp1 = [place stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," %s,",temp);
    temp1 = [country stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output," %s.\n\n",temp);
    temp1 = [comments stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output,"Comments: %s\n\n",temp);
    temp1 = [chkout stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output,"Checked Out By: %s\n",temp);
    temp1 = [dueOn stringValueAt:0];
    sprintf(temp, "%s",temp1);
    fprintf(output,"Due On: %s",temp);

    [NXApp stopModal];
    [articleWindow close];
    return self;
}

- addAbstractAndClose:sender
{
    [NXApp stopModal];
    [absWindow close];
    return self;
}

- addAuthorAndClose:sender
{
     const char *temp1;
     char   temp[40];
         
    temp1 = [lname stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output," %s,",temp);
    temp1 = [fname stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output,"%s.,",temp);
    [NXApp stopModal];
    [authWindow close];
    return self;
}

- addMoreAuthors:sender
{
     const char *temp1;
     char temp[20];
     
    temp1 = [lname stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output," %s,",temp);
    temp1 = [fname stringValueAt:0];
    sprintf(temp,"%s",temp1);
    fprintf(output," %s,",temp);
    [lname selectTextAt:0];
    return self;
}

- warnUser:sender
{
    NXRunAlertPanel("WARNING","The feature that you selected has not yet been implemented","FINE!", NULL,NULL);
    return self;
}

- skipAbstract:sender
{
    [NXApp stopModal];
    [absWindow close];
    NXRunAlertPanel("Message","Abstract is Skipped","OK!!", NULL,NULL);
    return self;
}

- fileInfoOpen:(int)item
{
   switch (item)
   {
        case AUTHORS:
  		[authWindow makeKeyAndOrderFront:self];
		break;
        case ARTICLE:
   		[articleWindow makeKeyAndOrderFront:self];
		break;
        case KEYWORDS:
   		[keyWindow makeKeyAndOrderFront:self];
		break;
        case ABSTRACT:
      		[absWindow makeKeyAndOrderFront:self];
		break;
	default:
	        [authWindow makeKeyAndOrderFront:self];
		break;
    }
   return self;
}

- clearAllFields
{
     [articleNumber setStringValue:"" at:0];
     [lname setStringValue:"" at:0];
     [fname setStringValue:"" at:0];
     [title setStringValue:"" at:0];
     [journal setStringValue:"" at:0];
     [vol setStringValue:"" at:0];
     [num setStringValue:"" at:0];
     [date setStringValue:"" at:0];
     [pages setStringValue:"" at:0];
     [place setStringValue:"" at:0];
     [country setStringValue:"" at:0];
     [comments setStringValue:"" at:0];
     [chkout setStringValue:"" at:0];
     [dueOn setStringValue:"" at:0];
     [keyWord setStringValue:"" at:0];
     return self;
}
     
- collectInfo:sender
{
      char cpCommand[35], rmCommand[35];
      char dir[15];
      
     sprintf(dir,"~/%s",[directory stringValueAt:0]);
     [directory selectTextAt:0];
     [directoryWindow close];
     sprintf(cpCommand,"cp ~/%s %s/.",[articleNumber stringValueAt:0],dir);  
     sprintf(rmCommand,"rm ~/%s",[articleNumber stringValueAt:0]);
     output = fopen([articleNumber stringValueAt:0], "w");
     [numberWindow close];
     [self  fileInfoOpen:AUTHORS]; 
     [NXApp runModalFor:authWindow];
     [self fileInfoOpen:ARTICLE];
     [NXApp runModalFor:articleWindow];
     [self fileInfoOpen:KEYWORDS];
     fprintf(output,"\n\nKEYWORD(S):\n\n");
     [NXApp runModalFor:keyWindow];
     [self fileInfoOpen:ABSTRACT];
     [NXApp runModalFor:absWindow];
     fclose(output);
     system(cpCommand);
     system(rmCommand);
     NXRunAlertPanel("Add Article","Your article %s has been added to %s directory","OK",
     NULL,NULL,[articleNumber stringValueAt:0],[directory stringValueAt:0]);
     [self clearAllFields];
     return self;
}

- deleteInfo:sender
{
     char deleteCommand[20];
     const char  *dir, *artnum;
      
     dir = [directory stringValueAt:0];
     artnum = [articleNumber stringValueAt:0];
     sprintf(deleteCommand,"rm ~/%s/%s",dir, artnum);
     [directoryWindow close];
     [numberWindow close];
     system(deleteCommand);
     NXRunAlertPanel("Delete Article","Your article %s has been deleted from %s directory","OK",NULL,NULL,artnum,dir);
          return self;
}

- indexInfo:sender
{
    char indexCommand[25];
    id info;
    NXModalSession *nextModal;
    
    sprintf(indexCommand,"ixBuild %s",[directory stringValueAt:0]);
    [directory selectTextAt:0];
    [directoryWindow close];
    info = NXGetAlertPanel("Indexing Status","Indexing %s directory...Patience!",NULL,NULL,NULL,[directory stringValueAt:0]);
    nextModal = [NXApp beginModalSession:NULL for:info];
    system(indexCommand);
    [NXApp endModalSession:nextModal];
    NXFreeAlertPanel(info);
    NXRunAlertPanel("Index Completion Message","Indexing %s directory completed","Good!",NULL,NULL,[directory stringValueAt:0]);
    return self;
}

- updateInfo:sender
{ 
   char updateCommand[30];
   id info;
   NXModalSession *nextModal;
      
   sprintf(updateCommand,"Edit ~/%s/%s",[directory stringValueAt:0],[articleNumber stringValueAt:0]);
   info = NXGetAlertPanel("Update"," Edit will open to let you update article %s in %s directory",NULL,NULL,NULL,[articleNumber stringValueAt:0],[directory stringValueAt:0]);
   [articleNumber setStringValue:"" at:0];
   [numberWindow close];
   [directory selectTextAt:0];
   [directoryWindow close];
   nextModal = [NXApp beginModalSession:NULL for:info];
   [NXApp endModalSession:nextModal];   
   NXFreeAlertPanel(info);
   system(updateCommand);
   return self;
}

- searchWords:sender
{
   int speakerResult, ignored;
   port_t  appPort;
   
   [directory selectTextAt:0];
   [directoryWindow close];
   [searchWindow close];
   
   /* look up the public port for app's Listener on local host */
   appPort  = NXPortFromName("Librarian",NULL);
   if (appPort == PORT_NULL)
   NXRunAlertPanel("Failure","Couldn't find the Public port for Librarian","Too Bad!",NULL,NULL);
   
   /* connect  the port to the Application's Speaker */
   
  [ [NXApp appSpeaker] setSendPort:appPort];
   
   /* use the openFile:ok:method (Librarian uses this for lookup requests. The others use 
   it for file-opening requests */
   
   speakerResult = [[NXApp appSpeaker] openFile:[wordField stringValueAt:0] ok:&ignored];
   printf ("speakerResult is %d\n",speakerResult);
   if (speakerResult != 0 )
   NXRunAlertPanel("Fiasco","Couldn't pass the message to the Librarian","Hummm",NULL,NULL);
   return self;
}

@end
