/*
 *	IRC.app -- IRC client program for NEXTSTEP
 *	Copyright (C)1995 Norihiro Itoh
 *
 *	このソースはIRC.appの一部です。
 *	This file is part of IRC.app.
 *
 *	本プログラムはフリー・ソフトウェアです。あなたは、Free Software 
 *	Foundationが公表したGNU一般公有使用許諾の「バージョン２」或いは
 *	それ以降の各バージョンの中からいずれかを選択し、そのバージョンが
 *	定める条項に従って本プログラムを再頒布または変更することができま
 *	す。
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	本プログラムは有用とは思いますが、頒布にあたっては、市場性及び特
 *	定目的適合性についての暗黙の保証を含めて、いかなる保証も行ないま
 *	せん。詳細についてはGNU一般公有使用許諾書をお読みください。
 *	This program 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 General Public License for more details.
 *
 *	あなたは、本プログラムと一緒にGNU一般公有使用許諾の写しを受け取っ
 *	ているはずです。そうでない場合は、Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USAへ手紙を書いてください。
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *	作者のメールアドレス: nito@scorpio.bekkoame.or.jp
 *	Author's e-mail address: nito@scorpio.bekkoame.or.jp
 */	

#import "ChannelManager.h"

@implementation ChannelManager

- showChannel:(const char *)titleName
{
    if (dialogueWindow == nil) {
	if (![NXApp loadNibSection:"channel.nib" owner:self]) {
	    return nil;
	}
    }
    namesStorage = [[Storage alloc] initCount:30 elementSize:20
    			description:"*"];
    [namesStorage empty];
    namesFlag = 0;
    recvFlag = 0;
    [userBrowser loadColumnZero];
    strcpy(channelName, titleName);
    [dialogueWindow setTitle:titleName];
    [[NXApp chatManager] sendTOPIC:channelName];
    [dialogueWindow makeKeyAndOrderFront:nil];
    return self;
}

- printDialogue:sender
{
    id			pinfo;
    
    pinfo = [NXApp printInfo];
    [pinfo setHorizPagination:NX_FITPAGINATION];
    [pinfo setHorizCentered:NO];
    [pinfo setVertCentered:NO];
    [pinfo setMarginLeft:40 right:35 top:45 bottom:35];
    [dialogueArea printPSCode:nil];
    return self;
}

- saveDialogue:sender
{
    NXStream		*stream;
    const char		*path;
    id			savepanel;
    
    savepanel = [SavePanel new];
    
    [savepanel setRequiredFileType:"rtf"];
    if (![savepanel runModalForDirectory:NXHomeDirectory() file:"IRC"])
    	return self;

    path = NXCopyStringBuffer([savepanel filename]);
    stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
    if (stream != NULL) {
    	[dialogueArea writeRichText:stream];
	NXSaveToFile(stream, path);
	NXCloseMemory(stream, NX_FREEBUFFER);
    }
    return self;
}

- closeDialogue:sender
{
    [[NXApp chatManager] sendPART:channelName];
    [dialogueWindow orderOut:nil];
    [NXApp closeChannel:channelName];
    return self;
}

- clearDialogue:sender
{
    NXColor		selcolor;
    
    selcolor = [dialogueArea selColor];
    [dialogueArea setEditable:YES];
    [dialogueArea setSelColor:[dialogueArea backgroundColor]];
    [[dialogueArea selectAll:nil] delete:nil];
    [dialogueArea setSelColor:selcolor];
    [dialogueArea setEditable:NO];
    return self;
}

- writeMessage:(char *)message from:(char *)name
{
    int			length;
    static char		buff[BUFSIZ+1];
    
    if (recvFlag == 0) {
    	[self clearDialogue:nil];
	recvFlag = 1;
    }
    length = [dialogueArea textLength];
    [dialogueArea setSel:length :length];
    [dialogueArea setSelFontStyle:NX_BOLD];
    sprintf(buff, "(%s) ", name);
    [dialogueArea replaceSel:buff];
    [dialogueArea setSelFontStyle:NX_UNBOLD];
    sprintf(buff, "%s\n", message);
    [dialogueArea replaceSel:buff];
    [dialogueArea scrollSelToVisible];
    return self;
}

- join:(char *)name;
{
    int			length;
    static char		buff[BUFSIZ+1];
    
    length = [dialogueArea textLength];
    [dialogueArea setSel:length :length];
    [dialogueArea setSelFontStyle:NX_UNBOLD];
    sprintf(buff, "(%s has joined this channel)\n", name);
    [dialogueArea replaceSel:buff];
    [dialogueArea scrollSelToVisible];
    [[NXApp chatManager] sendNAMES:channelName];
    return self;
}

- part:(char *)name;
{
    int			length;
    static char		buff[BUFSIZ+1];
    
    length = [dialogueArea textLength];
    [dialogueArea setSel:length :length];
    [dialogueArea setSelFontStyle:NX_UNBOLD];
    sprintf(buff, "(%s has parted from this channel)\n", name);
    [dialogueArea replaceSel:buff];
    [dialogueArea scrollSelToVisible];
    [[NXApp chatManager] sendNAMES:channelName];
    return self;
}

- setTopic:(const char *)message
{    
    [title setStringValue:message];
    return self;
}

- setNames:(char *)list
{
    char		*item;

    if (namesFlag == 0) {
    	[namesStorage empty];
	namesFlag = 1;
    }
    item = strtok(list, " \r\n");
    
    while (item != NULL) {
	[namesStorage addElement:item];
	item = strtok(NULL, " \r\n");
    }
    return self;
}

- endNames
{
    [userBrowser loadColumnZero];
    namesFlag = 0;
    return self;
}

- (char *)channelName
{
    return channelName;
}
- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
    int			row = 0;
    int			i;
    int			numOfRecords;
    NXBrowserCell	*cell;

    numOfRecords = [namesStorage count];
    for(i = 0; i < numOfRecords; i++) {
	[matrix addRow];
	cell = [matrix cellAt:row++ :column];
	[cell setStringValue:(const char *)[namesStorage elementAt:i]];
	[cell setLoaded:YES];
	[cell setLeaf:YES];
    }
    return row;
}

@end
