
/* sula_cb.c	-callbacks for main form
   distributed as part of _ s u l a _

	sula - an irc client for X

    Author: Tano Fotang, 1997

    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.

    see the file sula/COPYING for details.

*/



#include <forms.h>
#include <time.h>
#include <sys/types.h>
#include <sys/param.h>
/*#include <sys/wait.h>*/
#include "config.h"
#include "aux.h"
#include "sula.h"
#include "servers.h"
#include "chanwin.h"
#include "global.h"
#include "script.h"
#include "parser.h"
#include "ignore.h"
#include "dcc.h"
#include "setting.h"
#include "cmd.h"

FD_serverlist *server_tool;
static void server_dblclick_cb(FL_OBJECT *brow,long data){
	char *buf, servname[512];
	u_short port;
	int n;
	Server *s;

	#ifndef NO_BUG
       /* there's a bug which cases pointer/keybrd focus to differ->SIGSEV.
       see sula_cb.c for more
       */
	if(server_tool->dismiss->u_ldata){
		fl_set_object_label(server_tool->status,
			"Please close and reopen this window.. a bug");
		fl_ringbell(0);
		return;
	}
	#endif
	if( (n=fl_get_browser(brow))<1)
		return;
	buf=(char *)fl_get_browser_line(server_tool->browser,n);
	if(!buf) return;
	if(sscanf(buf,"%*s %s %hu", servname, &port)<2)
   		return;
	s=search_server_tree_byname(servname, port);
	assert(s);
	if(s->fd>-1){
		fl_set_object_label(server_tool->status,"Trying to disconnect..");
		close_server_connectionbyname(s->servname, s->port);
	}
	else {
		fl_set_object_label(server_tool->status,"Trying to connect..");
		connect_to_server(s, NULL, NULL,0);
	}
}

static int server_tool_atclose(FL_FORM * form, void *obj)
{
	
	server_tool->dismiss->u_ldata=0;
	server_tool=NULL;
	/**((FD_serverlist **) obj) = NULL;*/
	return FL_OK;
}

void main_cb(FL_OBJECT *ob, long data){
switch(data){


case 0: { /* server tool */
	if(server_tool){
		fl_raise_form(server_tool->serverlist);
		return;
	}
	server_tool=create_form_serverlist();
	fl_set_browser_fontsize(server_tool->browser, FL_NORMAL_SIZE);
	fl_set_browser_dblclick_callback(server_tool->browser,server_dblclick_cb,0);
	fl_set_form_atclose(server_tool->serverlist,server_tool_atclose,0);
	fl_show_form(server_tool->serverlist,FL_PLACE_MOUSE,
	 FL_FULLBORDER,sula_NAME sula_VERSION": Server list");
	fl_call_object_callback(server_tool->update);
	fl_do_forms();
	}
	break;
case 1:{	/* new channel window */
      	char * label;
	Winstruct *win=new_chan_win();
	if(!win) return;
      	set_object_attribs(win);
	fit_object_label(win->chanwin->status, sula_NAME sula_VERSION"    type /help for help");
      	label = (char *) malloc(sizeof(char) * (strlen(sula_NAME sula_VERSION) + 32));
	sprintf(label, "[%d] "sula_NAME sula_VERSION 
			": No server connection", (win-winstruct)+1);
	fl_show_form(win->chanwin->chanwin, FL_PLACE_FREE, FL_FULLBORDER,
			label );
	free(label);
	fl_do_forms();
	}
	break;
case 2: do_setup(); break;
case 3: do_help(); break;
case 4:{
	if(fl_mouse_button()== FL_MIDDLEMOUSE){
		fl_finish();
		exit(0);
	}
	fl_deactivate_all_forms();
	if(!confirm(servFdTree?"Quit IRC?":"Shut down client?",
	 sula_NAME sula_VERSION": Confirm exit", 7)){
		/* 7 seconds to comply */
		fl_activate_all_forms();
		return;
	}else{
		cleanup();
       		 exit(0);
		}
	}
case 5:do_script(); break;/* script */
default: break;

}/* swicth*/

}
extern void cleanup(void){
	char *path, *tmp;
	char buf[MAXPATHLEN+1];

	fl_finish();
	if(! (path=getenv("HOME")) ) path=".";
	if(gflags & SAVE_HISTORY){
		sprintf(buf,"%s/%s/%s", path , P_HOME, P_HISTORY);
		tmp=strdup(buf);
		sprintf(buf, "%s.bak", tmp);
		rename(tmp,buf);
	       	/*fprintf(stderr,"\nsaving cmd log: %s\n",tmp);*/
		save_history(tmp);	
	}
	buf[0]='\0';
	if(gflags & SAVE_BOOKMARKS){
		sprintf(buf,"%s/%s/%s", path , P_HOME, P_BOOKMARKS);
		tmp=strdup(buf);
		sprintf(buf, "%s.bak", tmp);
		rename(tmp,buf);
	       /*	fprintf(stderr,"saving bookmarks: %s\n",tmp);*/
		if(save_bookmarks(tmp, bmTree)) err_ret("save bookmarks:%s", tmp);
		
	}
	 
}

void History_cb(FL_OBJECT *ob, long data){

if(data){	/* HistRew */
	History *hist=(History *)main_form->FwdHist->u_ldata;
	if(hist==NULL) hist=mainhistCur;
	else	hist=hist->left;
	main_form->FwdHist->u_ldata=(long)hist;
	if(hist==mainhistStart && (hist=hist->left)==mainhistStart)
		 return; /*empty list */
	fl_set_input(main_form->main_input, hist->line);
}else{
	History *hist=((History *)ob->u_ldata);
	if(hist==NULL) hist=mainhistCur;
	else hist= hist->right;
	ob->u_ldata=(long)hist;
	if(hist==mainhistStart && (hist=hist->right)==mainhistStart)
	 	return;	/* empty list */
	fl_set_input(main_form->main_input, hist->line);
}
}


static int AddTomainHistory(const char *s);

void main_input_cb(FL_OBJECT *ob, long data){
	char *s=(char *)fl_get_input(ob);

	if(!(*s) || !clean(s)) return;
	if(!AddTomainHistory(s))
		main_form->FwdHist->u_ldata=(long)0L ;
	
	if(!strncasecmp(s,"/run ",5)){
		 script_start(s+5 );
	}
	else if(!strncasecmp(s,"/l",2))
	fl_trigger_object(main_form->script);
	 /*list_hooks(main_form->browser);*/
	else fl_addto_browser(main_form->browser, s);
	fl_set_input(ob,"");

}


/*-----------------------------------------------------------------*/
static int AddTomainHistory(const char *s){
	History *p;
	History *current;

	if(!s)  return -1;
	p=(History *)malloc(sizeof(History));
	if(p==NULL) return errno;
	p->line=strdup(s);
	p->left=mainhistCur;  p->right=mainhistCur->right;
	mainhistCur->right->left=p;   mainhistCur->right=p;
	 
	mainHistSize+=sizeof(s);
	current= mainhistStart->right;
	#if 0
	while(mainHistSize>MAX_HIST_SIZE && current!=mainhistStart){
		History *next;
		if(current==mainhistStart) return;	/*list is empty*/
		next=current->right;
		mainHistSize-=sizeof(current->line);
		free(current->line);
		current->left->right= next;
		next->left=current->left;
		free(current);
		current=next;
	}
	#endif
	return 0;
}

