/* Nessus
 * Copyright (C) 1998 Renaud Deraison
 *
 * 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.
 *
 * 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.
 *
 * 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.
 *
 */

#include <includes.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "../xstuff.h"
#include "../preferences.h"

static int add_rule_callback(GtkWidget *, struct arglist *);
static void sigh_button_event(GtkWidget *, GdkEventButton *, struct arglist *);
              
                
struct arglist * prefs_dialog_user(prefs)
 struct arglist * prefs;
{
 struct arglist * ctrls = emalloc(sizeof(struct arglist));
 GtkWidget * frame;
 GtkWidget * table;
 GtkWidget * label;
 GtkWidget * button;
 GtkWidget * entry;
 GtkWidget * list;
 GtkWidget * s_window;
 
 frame = gtk_frame_new("User");
 gtk_container_border_width(GTK_CONTAINER(frame), 10);
 gtk_widget_show(frame);
 arg_add_value(ctrls, "FRAME", ARG_PTR, -1, frame);
 
 table = gtk_table_new(3,4,FALSE);
 gtk_table_set_row_spacings(GTK_TABLE(table), 15);
 gtk_table_set_col_spacing(GTK_TABLE(table), 1, 10);
 gtk_container_add(GTK_CONTAINER(frame), table);
 gtk_container_border_width(GTK_CONTAINER(table), 10);
 gtk_widget_show(table);
 
 label = gtk_label_new("Email : ");
 gtk_table_attach(GTK_TABLE(table), label, 0,1,0,1, GTK_FILL | GTK_EXPAND, 0,0,0);
 gtk_widget_show(label);
 
 entry = gtk_entry_new();
 gtk_table_attach(GTK_TABLE(table), entry, 1,3,0,1, GTK_FILL | GTK_EXPAND, 0,0,0);
 gtk_widget_show(entry);
 arg_add_value(ctrls, "EMAIL", ARG_PTR, -1, entry);
 
 label = gtk_label_new("Rules : ");
 gtk_table_attach(GTK_TABLE(table), label, 0,1,1,2, GTK_FILL | GTK_EXPAND, 0,0,0);
 gtk_widget_show(label);
 
 entry = gtk_entry_new();
 gtk_table_attach(GTK_TABLE(table), entry, 1,2,1,2, GTK_FILL | GTK_EXPAND, 0,0,0);
 gtk_widget_show(entry);
 arg_add_value(ctrls, "RULE", ARG_PTR, -1, entry);
 
 button = gtk_button_new_with_label("Add rule");
 gtk_table_attach(GTK_TABLE(table), button, 2,3,1,2, 0, 0, 0,0);
 gtk_signal_connect(GTK_OBJECT(button), "clicked",
 	GTK_SIGNAL_FUNC(add_rule_callback), ctrls);
 gtk_widget_show(button);

 
 s_window = gtk_scrolled_window_new(NULL, NULL);
 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(s_window),
                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 gtk_table_attach_defaults(GTK_TABLE(table), s_window, 0,3,2,3);
 gtk_widget_show(s_window);
 list = gtk_list_new();
 gtk_signal_connect(GTK_OBJECT(list),                           
                       "button_release_event",                      
                       GTK_SIGNAL_FUNC(sigh_button_event),
                       ctrls);   
 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 10);
 gtk_container_add(GTK_CONTAINER(s_window), list);
 gtk_widget_show(list);
 arg_add_value(ctrls, "RULES", ARG_PTR, -1, list);
 arg_add_value(ctrls, "PREFERENCES", ARG_ARGLIST, -1, prefs);
 return(ctrls);
}
 
 
static void sigh_button_event(w, event, ctrls)
 GtkWidget * w;
 GdkEventButton * event;
 struct arglist * ctrls;
{
  if (event->type==GDK_BUTTON_RELEASE)
  {
   GList * dlist;
   struct arglist * p = arg_get_value(ctrls, "PREFERENCES");
   struct arglist * r = arg_get_value(p, "RULES");
   dlist = GTK_LIST(w)->selection;
   if(dlist)while(dlist)
   {
    char * name = gtk_object_get_data(GTK_OBJECT(dlist->data), "rule");
    gtk_widget_hide(dlist->data);
    if(name)arg_set_value(r, name, 0, "");
    else fprintf(stderr, "Warning: could not actually delete the selected rule");
    dlist = dlist->next;
   }
  }
}


static int add_rule_callback(w, ctrls)
 GtkWidget * w;
 struct arglist * ctrls;
{ 
 struct arglist * p = arg_get_value(ctrls, "PREFERENCES");
 int num = 1;
 GtkWidget * text;
 GtkWidget * list, * box, *label, * item;
 GList * dlist = NULL;
 char * rule, *z;
 struct arglist * t;
 char * name;
 int addstuff = 0;
 
 p = arg_get_value(p, "RULES");
 list = arg_get_value(ctrls, "RULES");
 text = arg_get_value(ctrls, "RULE");
 t = p;
 while(t && t->next){num++;t=t->next;}
 
 z = gtk_entry_get_text(GTK_ENTRY(text));
 if(strlen(z) && z[strlen(z)-1]!=';')addstuff = 1;
 rule = emalloc(strlen(z)+addstuff+1);
 strncpy(rule, z, strlen(z));
 if(addstuff)strcat(rule, ";");
 
  name = emalloc(10);
  sprintf(name, "%d", num);
  arg_add_value(p, name, ARG_STRING, strlen(rule), rule);
  item = gtk_list_item_new();
  gtk_object_set_data(GTK_OBJECT(item), "rule", name);
  box = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(item), box);
  gtk_widget_show(box);
  label = gtk_label_new(rule);
  gtk_widget_show(label);
  gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
  dlist = g_list_append(dlist, item);
  gtk_widget_show(item);
  gtk_list_append_items(GTK_LIST(list), dlist);
  gtk_entry_set_text(GTK_ENTRY(text), "");
  return(0);
}



