/* etc_cb.c -misc. 
 * 
 * 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 COPYING for more info.
 * 
 */

#include <forms.h>
#include "sula.h"
/*#include "servers.h"*/
#if 1
#include <sys/utsname.h>	/* UNAME(2)            Linux Programmer's Manual */
#endif
#include "aux.h"
#include "etc.h"
#include "chanwin.h"
#include "global.h"
#include "setting.h"
#include "cmd.h"
#include "config.h"

#include "sula.xpm"
#include "warning2.xpm"

void    about_cb(FL_OBJECT * ob, long data)
{
	switch (data) {
	    case 1:{
			    FD_copying *form = (FD_copying *) ob->form->fdui;

			    fl_hide_form(form->copying);
			    fl_free_form(form->copying);
			    *((int *) ob->u_ldata) = 0;
			    break;
		    }
	    case 0:{
			    FD_about *form = (FD_about *) ob->form->fdui;

			    fl_hide_form(form->about);
			    fl_free_form(form->about);
			    *((int *) ob->u_vdata) = 0;
			    break;
		    }
	    default:
		    break;
	}
}

void    copying_cb(FL_OBJECT * ob, long data)
{
	FD_copying *form;
	static int already_open = 0;

	if (already_open)
		return;
	already_open = 1;
	if (access(copyright_file, R_OK) < 0) {
		char   *buf = (char *) malloc(sizeof(char) * (strlen(copyright_file) + 256));

		if (!buf) {
			err_ret("%s %s", __FILE__, __LINE__);
			return;
		}
		sprintf(buf, "%s:\n%s", copyright_file, strerror(errno));

		confirm(buf, sula_NAME ": Error", 60);
		already_open = 0;
		free(buf);
		return;
	}
	else {
		char   *label[] =
		{"I see!", "Good!", "A ha!", "So so!", "Got it!", "Cool..", "Oh well!",};

		form = create_form_copying();
		fl_set_form_atclose(form->copying, atclose_dec_count, (void *) &already_open);
		form->ok->u_ldata = (long) &already_open;
		srandom(time(NULL));
		fl_set_browser_fontsize(form->browser, FL_NORMAL_SIZE);
		fl_set_object_label(form->ok, label[random() % (sizeof(label) / sizeof(char *))]);

		fl_fit_object_label(form->ok, 2, 2);
		fl_set_form_minsize(form->copying, 530, 380);
		fl_show_form(form->copying, FL_PLACE_GEOMETRY, FL_FULLBORDER, sula_NAME sula_VERSION ": Copyright & Warranty");
		fl_load_browser(form->browser, copyright_file);
		fl_do_forms();
	}
}

void    do_help(void)
{
	FD_guide *form;
	static void browse_cmds(Cmd * p, FL_OBJECT * brow);
	static int already_open = 0;

	if (already_open)
		return;
	already_open = 1;
	form = create_form_guide();
	fl_set_browser_fontsize(form->brow1, FL_NORMAL_SIZE);
	fl_set_browser_fontstyle(form->brow1, FL_TIMES_STYLE);
	fl_set_form_atclose(form->guide, atclose_dec_count, (void *) &already_open);
	form->done->u_ldata = (long) &already_open;
	/*fl_set_form_minsize(form->guide,  680, 365); */
	fl_show_form(form->guide, FL_PLACE_GEOMETRY, FL_FULLBORDER, sula_NAME sula_VERSION ": User Guide");
	fl_freeze_form(form->guide);
	browse_cmds(cmdTree, form->brow1);
	fl_unfreeze_form(form->guide);
	fl_do_forms();
}

void    help_cb(FL_OBJECT * ob, long data)
{
	FD_guide *form = (FD_guide *) ob->form->fdui;
	static void show_help(const char *, const char *, FD_guide *);

	switch (data) {
	    case 0:
		    fl_clear_browser(form->brow2);
		    break;
	    case 1:{
			    char    cmd[MAXCMDLEN + 1];
			    int     n = fl_get_browser(form->brow1);

			    if (n < 1)
				    return;
			    if (sscanf(fl_get_browser_line(form->brow1, n), "%s*", cmd) < 1)
				    return;
			    fl_freeze_form(form->guide);
			    show_help(uppercase(cmd), NULL, form);
			    fl_unfreeze_form(form->guide);
			    break;
		    }
	    case 2:
		    fl_hide_form(form->guide);
		    fl_free_form(form->guide);
		    *((int *) ob->u_ldata) = 0;
		    break;
	    default:
		    break;
	}
}
static void show_help(const char *s1, const char *s2, FD_guide * form)
{
	char   *hfname=NULL;
	char    *hdir = sula_lib;
	size_t  ll;

	ll = strlen(hdir);
	#if 0
	if (*(hdir + ll - 1) == '/')
		*(hdir + --ll) = '\0';
	if (ll == 0)
		ll = 1;
	#endif
	if (!s2) {
		hfname = (char *) malloc(sizeof(char) * (ll + strlen(s1) + 16));
		sprintf(hfname, "%s/help/%s.hlp", *hdir ? hdir : ".", s1);
	}
	else {
		hfname = (char *) malloc(sizeof(char) * (ll + strlen(s1) + strlen(s2) + 16));
		sprintf(hfname, "%s/help/%s/%s.hlp", *hdir ? hdir : ".", s1, s2);
	}
	if (access(hfname, R_OK) < 0)
	      out:
	{
		char   *buf = (char *) malloc(sizeof(char) * (strlen(hfname) + 256));

		sprintf(buf, "%s: %s", hfname, strerror(errno));
		fit_object_label(form->status, buf);
		if (gflags & BEEP_ERR)  fl_ringbell(0);
		free(buf); free(hfname); return;
	}
	else {
		FILE   *fp = fopen(hfname, "r");

#define BUFS	1024
		char    buf[BUFS + 1];

		if (fp == NULL)
			goto out;
		fl_freeze_form(form->guide);
		while (fgets(buf, BUFS, fp)) {
			buf[strlen(buf) - 1] = ' ';	/* browser ignores empty lines */
			fl_addto_browser(form->brow2, buf);
		}
		fl_unfreeze_form(form->guide);
		fclose(fp);
	}
	free(hfname);
}

static void browse_cmds(Cmd * p, FL_OBJECT * brow)
{
	if (p) {
		char    str[50];

		browse_cmds(p->left, brow);
		sprintf(str, "%s%s%s", p->name,
			p->type & CMD_IN ? "" : "*",
			p->type & CMD_CTCP ? " (CTCP)" : "");
		fl_add_browser_line(brow, str);
		browse_cmds(p->right, brow);
	}
}
void    do_about(void)
{
	FD_about *form;
	struct utsname ut;	/* unix type */
	char    s[30];
	static int already_open = 0;

	if (already_open)
		return;
	already_open = 1;
	form = create_form_about();
	fl_set_form_atclose(form->about, atclose_dec_count, (void *) &already_open);
	fl_set_pixmap_data(form->ok, sula_xpm);
	form->ok->u_ldata = (long) XC_hand2;
	fl_set_object_prehandler(form->ok, set_cursor);
	form->ok->u_vdata = (void *) &already_open;
	form->copying->u_ldata = (long) XC_hand2;
	fl_set_object_prehandler(form->copying, set_cursor);
	fl_set_object_label(form->label, sula_DESCR);
	fl_fit_object_label(form->label, 2, 2);
	fit_object_label(form->author, "T" "a" "n" "o " "F" "ot" "a" "n" "g");
	fit_object_label(form->version, sula_VERSION);
	fit_object_label(form->release, sula_RELEASE);
	if (uname(&ut) != 0)
		fl_set_object_label(form->machine, "unknown");
	else {
		char    t[132]; /* 65*2 */

		sprintf(t, "%s %s", ut.sysname, ut.release);
		fit_object_label(form->machine, t);
	}
	sprintf(s, "%s (%s)", __DATE__, __TIME__);
	fit_object_label(form->date, s);
	fit_object_label(form->by, sula_COMPILED);
	fit_object_label(form->host, sula_HOST " ru" "nni" "ng " sula_OS);
	fl_show_form(form->about, FL_PLACE_MOUSE, FL_FULLBORDER, "A" "bo" "ut " sula_NAME sula_VERSION);
	fl_do_forms();
}


/*---------------confirm, getstring-----------------------------------------*/

int     confirm(const char *msg, const char *title, double tmout)
{
	FD_confirm *form = create_form_confirm();
	FL_OBJECT *answer;
	fl_set_form_atclose(form->confirm, dont_close_win, 0);
	fl_set_pixmap_data(form->icon, warning2_xpm);
	fl_set_object_label(form->message, msg);
	fl_show_form(form->confirm, FL_PLACE_MOUSE, FL_TRANSIENT, title);
	if (tmout)
		fl_set_timer(form->timer, tmout);
	while ((answer = fl_do_forms()) != form->yes && answer != form->no);
	fl_hide_form(form->confirm);
	fl_free_form(form->confirm);
	return answer == form->yes ? 1 : 0;
}
void    timer2_cb(FL_OBJECT * ob, long data)
{
	fl_trigger_object(((FD_confirm *) ob->form->fdui)->no);
}

char   *getstr2(const char *label, const char *title, const char *deflt_val,
		int use_browser)
{
	FD_getstring *form;
	FL_OBJECT *answer;
	char   *val;
	double  tmout = (double) timeout;
	

	form = create_form_getstring();
	fl_set_form_atclose(form->getstring, dont_close_win, (void *) 0);
	fl_set_input_return(form->input, FL_RETURN_ALWAYS);
	form->input->u_vdata = (void *) &tmout;
	form->input->u_ldata = (long) XC_xterm;
	fl_set_object_prehandler(form->input, set_cursor);

	if (deflt_val)
		fl_set_input(form->input, deflt_val);
	fl_set_object_label(form->label, label);
	fl_set_form_minsize(form->getstring, 400, 160);
	fl_set_form_maxsize(form->getstring, 600, 160);
	fl_set_timer(form->timer, timeout);
	if (!use_browser)
		fl_hide_object(form->fname);
	else {
		form->fname->u_vdata = (void *) deflt_val;
		/*form->fname->u_ldata=(long)&browser_open; */
	}
	fl_show_form(form->getstring, FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT, title);
	/*fl_reset_focus_object(form->input);*/
	
	while (((answer = fl_do_forms()) != form->ready) && (answer != form->cancel));
	val = (answer == form->ready) ? strdup2(fl_get_input(form->input)) : NULL;
	fl_hide_form(form->getstring);
	fl_free_form(form->getstring);
	return val;

}
static void zap_fselector(int id, void *junk)
{
	FD_FSELECTOR *fselector = fl_get_fselector_fdstruct();

	fl_trigger_object(fselector->cancel);

}

void    getstring_cb(FL_OBJECT * ob, long data)
{
	FD_getstring *form = (FD_getstring *) ob->form->fdui;

	switch (data) {
	    case 0:		/* input */
		    fl_set_timer(form->timer, *((double *) ob->u_vdata));
		    break;
	    case 1:{		/* browse */
			    char   *fname;
			    char   *deflt, *ext=NULL;
			    int     timeout = fl_add_timeout((*((double *) form->input->u_vdata)) * 1000, zap_fselector, NULL);

			    fl_suspend_timer(form->timer);
			    fl_set_cursor(form->getstring->window, XC_watch);
			    deflt = (char *) ob->u_vdata;
			    if(deflt){
			    	ext= strrchr(deflt,'/');
				if(ext){
					ext++;
					ext=strrchr(ext,'.');
					if(ext) ext++;
				}
			    }
			    ext=ext?ext:"*";
			    fname = (char *) fl_show_fselector("Select a file", "", ext, deflt ? deflt : "");
			    if (fname) {
				    fl_set_input(form->input, fname);
				    fl_remove_timeout(timeout);
			    }
			    fl_reset_cursor(form->getstring->window);
			    fl_resume_timer(form->timer);
			    break;
		    }
	    case 2:{		/* timer */
			    fl_trigger_object(form->ready);
		    }
		    break;

	    default:
		    break;
	}			/* swotch */
}

