/*  xtraceroute - graphically show traceroute information.
 *  Copyright (C) 1996-1998  Björn Augustsson 
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "xt.h"


/* Allow updating the database.
 * get the text uncentered. */


static gint text_has_changed = FALSE;
static gint guard = FALSE;

static GtkWidget *ip_entry;
static GtkWidget *lat_entry;
static GtkWidget *lon_entry;
static GtkWidget *info_entry;

static gint destroy_widget_callback(GtkWidget *wi, gpointer *target)
{
  gtk_widget_destroy(GTK_WIDGET(target));
  guard = FALSE;
  return TRUE;
}

static gint yesbutton_callback(GtkWidget *wi, gpointer *data)
{
  dbentry *ent;
  
  if(text_has_changed)
    {
      // tell_user(_("Info has changed!\n Updating the database is not yet supported!"));
      text_has_changed = FALSE;
      
      ent = (dbentry *)malloc(sizeof(dbentry));

      /* This is a good place to do some sanity/bounds checking! */
      
      ent->lat = atof(gtk_entry_get_text(GTK_ENTRY(lat_entry)));
      ent->lon = atof(gtk_entry_get_text(GTK_ENTRY(lon_entry)));
      strcpy(ent->ip,   gtk_entry_get_text(GTK_ENTRY(ip_entry)));
      strcpy(ent->info, gtk_entry_get_text(GTK_ENTRY(info_entry)));
      
      /* Always add new entries to the private database. */
      addToNetDB(local_user_nets, ent);
      
      free(ent); 
    }

  return TRUE;
}

static gint helpbutton_callback(GtkWidget *wi, gpointer *data)
{
  tell_user(_("No help yet!"));

  return TRUE;
}

static gint text_change_callback(GtkWidget *wi, gpointer *data)
{
    text_has_changed = TRUE;
    return TRUE;
}

gint addNet(GtkWidget* wi)
{
  GtkWidget *dialog;
  GtkWidget *table;
  GtkWidget *yesbutton;
  GtkWidget *nobutton;
  GtkWidget *helpbutton;
  GtkWidget *title;
  GtkWidget *label;
  GtkWidget *frame;
  char string[100];
  
  if(guard == TRUE)
    {
      tell_user(_("You can only have one\ndatabase window at at time!"));
      return TRUE;
    }
  guard = TRUE;
  
  dialog = gtk_dialog_new();
  
  sprintf(string, _("New network record"));
  title  = gtk_label_new(string);
  gtk_widget_show(GTK_WIDGET(title));
  
  gtk_window_set_title (GTK_WINDOW(dialog),_("Record info"));
  gtk_container_border_width(GTK_CONTAINER(dialog), 10);
  
  
  table  = gtk_table_new(4,2,FALSE);
  gtk_container_border_width(GTK_CONTAINER(table), 10);
  
  
  label = gtk_label_new(_("IP number:"));
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(label), 0, 1, 0 ,1);
  gtk_widget_show(GTK_WIDGET(label));

  ip_entry = gtk_entry_new();
  gtk_signal_connect(GTK_OBJECT(ip_entry),"changed",
		     GTK_SIGNAL_FUNC(text_change_callback), NULL);
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(ip_entry), 1, 2, 0 ,1);
  gtk_widget_show(GTK_WIDGET(ip_entry));

  label = gtk_label_new(_("Latitude:"));
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(label), 0, 1, 1 ,2);
  gtk_widget_show(GTK_WIDGET(label));

  lat_entry = gtk_entry_new();
  gtk_signal_connect(GTK_OBJECT(lat_entry),"changed",
	  GTK_SIGNAL_FUNC(text_change_callback), NULL);
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(lat_entry), 1, 2, 1 ,2);
  gtk_widget_show(GTK_WIDGET(lat_entry));

  label = gtk_label_new(_("Longitude:"));
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(label), 0, 1, 2 ,3);
  gtk_widget_show(GTK_WIDGET(label));

  lon_entry = gtk_entry_new();
  gtk_signal_connect(GTK_OBJECT(lon_entry),"changed",
	  GTK_SIGNAL_FUNC(text_change_callback), NULL);
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(lon_entry), 1, 2, 2 ,3);
  gtk_widget_show(GTK_WIDGET(lon_entry));

  label = gtk_label_new(_("Info:"));
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(label), 0, 1, 3 ,4);
  gtk_widget_show(GTK_WIDGET(label));

  info_entry = gtk_entry_new();
  gtk_signal_connect(GTK_OBJECT(info_entry),"changed",
	  GTK_SIGNAL_FUNC(text_change_callback), NULL);
  gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(info_entry), 1, 2, 3 ,4);
  gtk_widget_show(GTK_WIDGET(info_entry));

  gtk_widget_show(GTK_WIDGET(table));

  /* Pack everything */

  frame = gtk_frame_new (_("Network info"));
  gtk_container_border_width (GTK_CONTAINER (frame), 10);
  gtk_container_add(GTK_CONTAINER(frame), table);
  gtk_widget_show(GTK_WIDGET(frame));

  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), title, TRUE,
		     TRUE, 0);
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), frame, TRUE,
		     TRUE, 0);
  
  
  yesbutton = gtk_button_new_with_label(_("OK"));
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), yesbutton,
		     TRUE, TRUE, 0);
  gtk_signal_connect(GTK_OBJECT (yesbutton), "clicked",
		     GTK_SIGNAL_FUNC (yesbutton_callback), dialog);
  gtk_signal_connect(GTK_OBJECT (yesbutton), "clicked",
		     GTK_SIGNAL_FUNC (destroy_widget_callback), dialog);
  GTK_WIDGET_SET_FLAGS(yesbutton, GTK_CAN_DEFAULT);
  gtk_widget_grab_default(yesbutton);
  gtk_widget_show(yesbutton);

  nobutton = gtk_button_new_with_label(_("Cancel"));
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), nobutton,
		     TRUE, TRUE, 0);
  gtk_signal_connect(GTK_OBJECT (nobutton), "clicked",
		      GTK_SIGNAL_FUNC(destroy_widget_callback), dialog);
  gtk_widget_show(nobutton);

  helpbutton = gtk_button_new_with_label(_("Help"));
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), helpbutton,
		     TRUE, TRUE, 0);
  gtk_signal_connect(GTK_OBJECT (helpbutton), "clicked",
		      GTK_SIGNAL_FUNC(helpbutton_callback), dialog);
  gtk_widget_show(helpbutton);

  gtk_widget_show (dialog);

  return TRUE;
}
