/* Code style: -*- linux-c -*- */
/* File : xfw/xfw_cb.c */

#include "forms.h"
#include "xfw.h"

#define TRUE 1

/* Global variables that couldn't be avoided (argh I *must* be a bad programmer!) */

int delete_click=0;


/* callbacks for form FirewallTool */
void type_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_type);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_type));

}

void source_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_source);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_source));

}

void dest_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_dest);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_dest));

}

void src_input(FL_OBJECT *ob, long data)
{
}

void dest_input(FL_OBJECT *ob, long data)
{
}

void prot_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_prot);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_prot));

}

void add_entry(FL_OBJECT *ob, long data)
{
 char temp[64]; /* for getting values from the system */

 /* this set are choices and will always have values */

 fl_addto_browser(disp_type, fl_get_choice_text(type_in));
 fl_addto_browser(disp_prot, fl_get_choice_text(prot_in));
 fl_addto_browser(disp_opt, fl_get_choice_text(opt_in));
 fl_addto_browser(disp_ifname, fl_get_choice_text(ifname_in));
 fl_addto_browser(disp_ifaddr, fl_get_choice_text(ifaddr_in));

 /* err ok, time to add the rest */

 strcpy(temp, fl_get_input(tosa_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_addto_browser(disp_tosa, temp);

 strcpy(temp, fl_get_input(tosx_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_addto_browser(disp_tosx, temp);

 strcpy(temp, fl_get_input(src_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_addto_browser(disp_source, temp);

 strcpy(temp, fl_get_input(dest_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_addto_browser(disp_dest, temp);


 /* this is a temporary thing */

 fl_addto_browser(disp_pkts, "400");
 fl_addto_browser(disp_bytes, "27656");

 /* and set our select bar on the last entry */
 set_browsers(fl_get_browser_maxline(disp_dest), fl_get_browser_topline(disp_dest));

}

void replace_entry(FL_OBJECT *ob, long data)
{
 int curr;
 char temp[64]; /* for getting values from the system */


 curr = fl_get_browser(disp_dest); /* any display would have done */

 /* this set are choices and will always have values */

 fl_replace_browser_line(disp_type, curr, fl_get_choice_text(type_in));
 fl_replace_browser_line(disp_prot, curr, fl_get_choice_text(prot_in));
 fl_replace_browser_line(disp_opt, curr, fl_get_choice_text(opt_in));
 fl_replace_browser_line(disp_ifname, curr, fl_get_choice_text(ifname_in));
 fl_replace_browser_line(disp_ifaddr, curr, fl_get_choice_text(ifaddr_in));

 /* err ok, time to add the rest */

 strcpy(temp, fl_get_input(tosa_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_replace_browser_line(disp_tosa, curr, temp);

 strcpy(temp, fl_get_input(tosx_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_replace_browser_line(disp_tosx, curr, temp);

 strcpy(temp, fl_get_input(src_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_replace_browser_line(disp_source, curr, temp);

 strcpy(temp, fl_get_input(dest_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_replace_browser_line(disp_dest, curr, temp);


 /* this is a temporary thing */

 fl_replace_browser_line(disp_pkts, curr, "123");
 fl_replace_browser_line(disp_bytes, curr, "78932");
}

void delete_entry(FL_OBJECT *ob, long data)
{
 int current_line, max;

 /* do we have authority to delete? ie is this the second click? */
 /* (This should be read as a xevent (FL_DBLCLICK) but I don't see */
 /*  how to get it, so I've implemented this nasty code here ) */

 delete_click++;

 if (delete_click == 2) {

   /* Delete the currently selected line only */
   /* first up, where the hell am I? */

   current_line = fl_get_browser(disp_dest); /* any display would do */
   max = fl_get_browser_maxline(disp_dest);

   /* beware of zero entries! or we'll segv */

   /* right, are we on the end of the list? */
   if ((max != 0)) {

     fl_delete_browser_line(disp_type, current_line);
     fl_delete_browser_line(disp_source, current_line);
     fl_delete_browser_line(disp_dest, current_line);
     fl_delete_browser_line(disp_prot, current_line);
     fl_delete_browser_line(disp_opt, current_line);
     fl_delete_browser_line(disp_tosa, current_line);
     fl_delete_browser_line(disp_tosx, current_line);
     fl_delete_browser_line(disp_ifname, current_line);
     fl_delete_browser_line(disp_ifaddr, current_line);
     fl_delete_browser_line(disp_pkts, current_line);
     fl_delete_browser_line(disp_bytes, current_line);

     /* humm maxline will have been decreased by one */
     max--;

     /* and now reselect the line (being careful of the end of the list) */
     if (max - current_line < 0)
       current_line--;

     if (current_line != 0)
       set_browsers(current_line, fl_get_browser_topline(disp_dest));
   }

   delete_click = 0;
 }

}

void append_entry(FL_OBJECT *ob, long data)
{
 int curr;
 char temp[64]; /* for getting values from the system */

 curr = fl_get_browser(disp_dest); /* any display would have done */
 curr++; /* so that we insert UNDER the current selected entry */

 /* argh! specialist behaviour from the forms library!,
    means I'll have to watch out for cases where we are
    appending to the end of a list (segv) in which case we
    call add_entry (same action) */

 if (curr > fl_get_browser_maxline(disp_dest)) {
       add_entry(ob, data);
       return; /* run away! */
 }

 /* this set are choices and will always have values */

 fl_insert_browser_line(disp_type, curr, fl_get_choice_text(type_in));
 fl_insert_browser_line(disp_prot, curr, fl_get_choice_text(prot_in));
 fl_insert_browser_line(disp_opt, curr, fl_get_choice_text(opt_in));
 fl_insert_browser_line(disp_ifname, curr, fl_get_choice_text(ifname_in));
 fl_insert_browser_line(disp_ifaddr, curr, fl_get_choice_text(ifaddr_in));

 /* err ok, time to add the rest */

 strcpy(temp, fl_get_input(tosa_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_insert_browser_line(disp_tosa, curr, temp);

 strcpy(temp, fl_get_input(tosx_in));
 if (strlen(temp) < 1) strcpy(temp, " ");
 fl_insert_browser_line(disp_tosx, curr, temp);

 strcpy(temp, fl_get_input(src_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_insert_browser_line(disp_source, curr, temp);

 strcpy(temp, fl_get_input(dest_in));
 if (strlen(temp) < 1) strcpy(temp, "---.---.---.---/-- ----");
 fl_insert_browser_line(disp_dest, curr, temp);


 /* this is a temporary thing */

 fl_insert_browser_line(disp_pkts, curr, "555");
 fl_insert_browser_line(disp_bytes, curr, "66666");

 /* and finally, move our select to the new line */
 set_browsers(curr, fl_get_browser_topline(disp_dest));
}

void quit_program(FL_OBJECT *ob, long data)
{
/*  if (fl_show_question("Argh! you don't really want to kill me do you?","","") == TRUE) */
   exit(1);

}

void opt_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_opt);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_opt));

}

void tosa_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_tosa);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_tosa));

}

void tosx_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_tosx);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_tosx));

}

void ifname_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_ifname);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_ifname));

}

void ifaddr_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_ifaddr);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_ifaddr));

}

void pkts_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_pkts);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_pkts));

}

void bytes_cb(FL_OBJECT *ob, long data)
{
 int line_num=0;

 line_num = fl_get_browser(disp_bytes);

 /* do it smoothly with a freeze/unfreeze */
 set_browsers(line_num, fl_get_browser_topline(disp_bytes));

}

void ifaddr_input(FL_OBJECT *ob, long data)
{
}

void ifname_input(FL_OBJECT *ob, long data)
{
}

void type_input(FL_OBJECT *ob, long data)
{
}

void prot_input(FL_OBJECT *ob, long data)
{
}

void opt_input(FL_OBJECT *ob, long data)
{
}

void tosa_input(FL_OBJECT *ob, long data)
{
}

void tosx_input(FL_OBJECT *ob, long data)
{
}

void forwarding_setup(FL_OBJECT *ob, long data)
{
}

void accounting_setup(FL_OBJECT *ob, long data)
{
}

void input_setup(FL_OBJECT *ob, long data)
{
}

void output_setup(FL_OBJECT *ob, long data)
{
}

void save_entrys(FL_OBJECT *ob, long data)
{
}

void load_entrys(FL_OBJECT *ob, long data)
{
}


void set_browsers(int line_num, int top_line)
{

 /* this s a cosmetic touch for delete_entry
    I just couldn't think of an elegant solution */
 delete_click = 0;

 /* this is a pretty type thing which moves all the other
    browsers in line with a selected member
    as such it's a long nasty thing with sets, deselects
    and reselects (hatefull really) */


 fl_set_browser_topline(disp_pkts, top_line);
 fl_deselect_browser(disp_pkts);
 fl_select_browser_line(disp_pkts, line_num);

 fl_set_browser_topline(disp_bytes, top_line);
 fl_deselect_browser(disp_bytes);
 fl_select_browser_line(disp_bytes, line_num);

 fl_set_browser_topline(disp_type, top_line);
 fl_deselect_browser(disp_type);
 fl_select_browser_line(disp_type, line_num);

 fl_set_browser_topline(disp_prot, top_line);
 fl_deselect_browser(disp_prot);
 fl_select_browser_line(disp_prot, line_num);

 fl_set_browser_topline(disp_opt, top_line);
 fl_deselect_browser(disp_opt);
 fl_select_browser_line(disp_opt, line_num);

 fl_set_browser_topline(disp_tosa, top_line);
 fl_deselect_browser(disp_tosa);
 fl_select_browser_line(disp_tosa, line_num);

 fl_set_browser_topline(disp_tosx, top_line);
 fl_deselect_browser(disp_tosx);
 fl_select_browser_line(disp_tosx, line_num);

 fl_set_browser_topline(disp_ifname, top_line);
 fl_deselect_browser(disp_ifname);
 fl_select_browser_line(disp_ifname, line_num);

 fl_set_browser_topline(disp_ifaddr, top_line);
 fl_deselect_browser(disp_ifaddr);
 fl_select_browser_line(disp_ifaddr, line_num);

 fl_set_browser_topline(disp_source, top_line);
 fl_deselect_browser(disp_source);
 fl_select_browser_line(disp_source, line_num);

 fl_set_browser_topline(disp_dest, top_line);
 fl_deselect_browser(disp_dest);
 fl_select_browser_line(disp_dest, line_num);

 /* and it would be a good idea to set the data capture line to be
    what we selected */

 /* ok manual inputs are easy enuf */

 fl_set_input(tosa_in, fl_get_browser_line(disp_tosa, line_num));
 fl_set_input(tosx_in, fl_get_browser_line(disp_tosx, line_num));
 fl_set_input(src_in, fl_get_browser_line(disp_source, line_num));
 fl_set_input(dest_in, fl_get_browser_line(disp_dest, line_num));

 /* now then, time to set the choice menu inputs,...
    tricky, may mean some hard coding later, but I hope not */

 fl_set_choice_text(type_in, fl_get_browser_line(disp_type, line_num));
 fl_set_choice_text(prot_in, fl_get_browser_line(disp_prot, line_num));
 fl_set_choice_text(opt_in, fl_get_browser_line(disp_opt, line_num));
 fl_set_choice_text(ifname_in, fl_get_browser_line(disp_ifname, line_num));
 fl_set_choice_text(ifaddr_in, fl_get_browser_line(disp_ifaddr, line_num));

}