/*-*-linux-c-*-*/

/*
 *  gnewtellium - Newtella for Unix
 *  Copyright (C) 2001 Elias Athanasopoulos
 *
 *  11/07/2001 - Combo box for host cache selection
 *               added by Kervin.
 *
 *  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 "../config.h"
/* Include as backup */
#include "../pixmaps/gnewtellium.xpm"


/* These includes are needed in the specified
   order by the BSD family. It maybe better to
   add an #ifdef BSD or something here...
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>

#include <gtk/gtk.h>

#include "newtella.h"
#include "connection.h"
#include "gui.h"
#include "global.h"
#include "guid.h"
#include "gnutella.h"
#include "config.h"
#include "file.h"
#include "http.h"
#include "hosts.h"

extern struct newtella_options *gl_options;

gint newtella_idle(gpointer data)
{
	newtella_sched();
	return(TRUE);
}

gint delete(GtkWidget *widget, GtkWidget *event, gpointer data )
{
    	gtk_main_quit();
    	return(FALSE);
}

int main_init_old(void)
{
	struct hostent *our_host;
	char host_name[MAXHOSTNAMELEN]; //vorher 255
	struct IP our_ip;
	GList *iter;
	int i;

	/* global initialization */
	guidinit();

	signal(SIGPIPE, SIG_IGN);

	/* global init */
	gl_con         = NULL;
	gl_down_con    = NULL;
	gl_up_con      = NULL;
	gl_results     = NULL;
	gl_queries     = NULL;
	rmessages      = NULL;

	gl_options = newtella_malloc(sizeof(struct newtella_options));
	gl_options->shared_dirs = NULL;

	/* reset connections */
	gl_options->cons      = 0;
	gl_options->dcons     = 0;
	gl_options->ucons     = 0;
	gl_options->connected = 0;

        guidget(gl_options->gl_guid);

	gl_my_host_info = newtella_malloc(sizeof(struct host_info));

	// Der Name des Hostes ermitteln
	gethostname(host_name, sizeof(host_name));

	our_host = gethostbyname2(host_name, AF_INET6);

	if (our_host) {
		i = 0;
		while (host_is_private(our_host->h_addr_list[i], 1)){
			i++;
		}
		bzero(&our_ip.uints, 16);
		//memcpy(&our_ip.uints, our_host->h_addr, sizeof(our_ip.uints)); // 16
		memcpy(&our_ip.uints, our_host->h_addr_list[i], sizeof(our_ip.uints));
		bzero(gl_my_host_info->ip, 16);
		memcpy(gl_my_host_info->ip, our_ip.uints, sizeof(gl_my_host_info->ip));
	}


	gl_my_host_info->port = 6346;
	gl_my_host_info->file_count = 0;
	gl_my_host_info->files_total_size = 0;

	if (!config_load_from_file())
		config_set_default();

	host_load_cache_from_file();
	config_refresh_main();

	/* build our file database */
	if (gl_options->shared_dirs) {
		iter = g_list_first(gl_options->shared_dirs);
		while (iter) {
			file_build_db((gchar *) iter->data);
			iter = g_list_next(iter);
		}
	}

	return 1;
}

int main_init(void)
{
	struct addrinfo *res, hints, *ressave;
	struct sockaddr_in6 *sin6;
	char host_name[MAXHOSTNAMELEN]; //vorher 255
	struct IP our_ip;
	GList *iter;
	int error;

	/* global initialization */
	guidinit();

	signal(SIGPIPE, SIG_IGN);

	/* global init */
	gl_con         = NULL;
	gl_down_con    = NULL;
	gl_up_con      = NULL;
	gl_results     = NULL;
	gl_queries     = NULL;
	rmessages      = NULL;

	gl_options = newtella_malloc(sizeof(struct newtella_options));
	gl_options->shared_dirs = NULL;

	/* reset connections */
	gl_options->cons      = 0;
	gl_options->dcons     = 0;
	gl_options->ucons     = 0;
	gl_options->connected = 0;

        guidget(gl_options->gl_guid);

	gl_my_host_info = newtella_malloc(sizeof(struct host_info));

	// Der Name des Hostes ermitteln
	gethostname(host_name, sizeof(host_name));

	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_INET6;
	hints.ai_socktype = SOCK_STREAM;
	error = getaddrinfo(host_name, "6346", &hints, &res);

	if (error == 0) {
		ressave = res;
	        sin6 = (struct sockaddr_in6 *) res->ai_addr;
		while (host_is_private(&sin6->sin6_addr, 1) && res!= NULL){
			res = res->ai_next;
			sin6 = (struct sockaddr_in6 *) res->ai_addr;
		}
		bzero(our_ip.uints, sizeof(our_ip.uints));
		memcpy(our_ip.uints, &sin6->sin6_addr, sizeof(our_ip.uints)); // 16
		bzero(gl_my_host_info->ip, sizeof(gl_my_host_info->ip));
		memcpy(gl_my_host_info->ip, our_ip.uints, sizeof(gl_my_host_info->ip));

		freeaddrinfo(ressave);
	}

	gl_my_host_info->port = 6346;
	gl_my_host_info->file_count = 0;
	gl_my_host_info->files_total_size = 0;

	if (!config_load_from_file())
		config_set_default();

	/* build our file database */
	if (gl_options->shared_dirs) {
		iter = g_list_first(gl_options->shared_dirs);
		while (iter) {
			file_build_db((gchar *) iter->data);
			iter = g_list_next(iter);
		}
	}

	host_load_cache_from_file();
	config_refresh_main();

	/* build our file database */
	/*if (gl_options->shared_dirs) {
		iter = g_list_first(gl_options->shared_dirs);
		while (iter) {
			file_build_db((gchar *) iter->data);
			iter = g_list_next(iter);
		}
	}*/
	return 1;
}


int main(int argc, char *argv[])
{
	gtk_init(&argc, &argv);

	draw_main();
	main_init();

	gtk_timeout_add(1000, newtella_idle, NULL);

	gtk_main();

	return(0);
}

gint draw_main(void)
{
	const char *button_names[20] = { "Advanced Configuration",
					 "Spread the word",
					 "About",
					 "Exit Newtella",
					 "Start",
					 "Clear",
					 "Clear all downloads",
					 "Add",
					 "Reset Connections",
					 "Clear Host Cache",
					 "Resume",
					 "Stop",
					 "Kill",
					 "Clear Finished",
					 "Resume",
					 "Stop",
					 "Kill",
					 "Clear Finished" };

	gchar *search_list_titles[5] = { "File",
					 "Size",
					 "Speed",
					 "Location",
					 "Servent" };

	gchar *download_list_titles[5] = { "File",
					   "Size",
					   "Status",
					   "Speed",
					   "Server" };

	gchar *upload_list_titles[5] = { "File",
					 "Size",
					 "Status",
					 "Speed",
					 "Downloader" };

	gchar *con_list_titles[4] = { "IP",
				      "Status",
				      "Packets Sent",
				      "Packets Received" };

	GtkWidget *frame;
	GtkWidget *label;
	GtkWidget *button;
	GtkWidget *main_vbox, *frame_vbox;
	GtkWidget *nb_vbox_1, *nb_vbox_2, *nb_vbox_3, *nb_vbox_4;
	GtkWidget *nb_hbox_1, *nb_hbox_2;
	GtkWidget *button_box, *dld_button_box, *uld_button_box, *pixmap_box;
	/*GtkWidget *stree;*/
	GtkWidget *scrolled_window;
	GtkAdjustment *adj;
	GtkWidget *align;
	GtkWidget *table;
	GtkWidget *hscale;
	GtkWidget *pixmapwid;
	GtkWidget *new_list_item;
	GdkPixmap *pixmap = NULL;
	GdkBitmap *mask;
	GtkStyle  *style;

	gchar *xpm_path;

	gint i;

    	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    	gtk_signal_connect(GTK_OBJECT(window), "delete_event",
			   GTK_SIGNAL_FUNC(delete), NULL);
    	gtk_container_set_border_width(GTK_CONTAINER(window), 10);

	gtk_window_set_title(GTK_WINDOW(window), GNEWTELLIUM);

	/* boxes */
	main_vbox = gtk_vbox_new(FALSE, 0);
	nb_vbox_1 = gtk_vbox_new(FALSE, 0);
	nb_vbox_2 = gtk_vbox_new(FALSE, 0);
	nb_vbox_3 = gtk_vbox_new(FALSE, 0);
	nb_vbox_4 = gtk_vbox_new(FALSE, 0);
	frame_vbox = gtk_vbox_new(FALSE, 0);

	button_box = gtk_hbox_new(TRUE, 0);
	pixmap_box = gtk_hbox_new(TRUE, 0);
	dld_button_box = gtk_hbox_new(FALSE, 0);
	uld_button_box = gtk_hbox_new(FALSE, 0);

	/* main notebook */
    	main_notebook = gtk_notebook_new();
   	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_notebook),
				 GTK_POS_TOP);

	/* page 1 */
	nb_hbox_1 = gtk_hbox_new(FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER(nb_hbox_1), 10);
	nb_hbox_2 = gtk_hbox_new(FALSE, 0);

	search_edit_box = gtk_entry_new_with_max_length(255);
	gtk_signal_connect(GTK_OBJECT(search_edit_box), "activate",
			   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)BTN_STRT_SEARCH);
	gtk_widget_show(search_edit_box);

	gtk_box_pack_start(GTK_BOX(nb_hbox_1), search_edit_box,
			   TRUE, TRUE, 2);
	for (i=4; i<6; i++) {
		button = gtk_button_new_with_label(button_names[i]);
		gtk_box_pack_start(GTK_BOX(nb_hbox_1), button, TRUE, TRUE, 1);
		gtk_signal_connect(GTK_OBJECT(button), "clicked",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)i);
		gtk_widget_show(button);
	}

	label = gtk_label_new("    Files found:");
	files_found_label = gtk_label_new("0");
	status_label = gtk_label_new("Idle...");

	gtk_box_pack_start(GTK_BOX(nb_hbox_2), label, FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_2), files_found_label,
			   FALSE, FALSE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_2), status_label,
			   FALSE, FALSE, 10);

	gtk_widget_show(label);
	gtk_widget_show(files_found_label);
	gtk_widget_show(status_label);

	frame = gtk_frame_new("Results");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 10);

        scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_usize(scrolled_window, 100, 240);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
	gtk_widget_show(scrolled_window);


	search_list = gtk_clist_new_with_titles(5, search_list_titles);
	// Igor: Hab ich etwas angepaßt
	gtk_clist_set_column_width(GTK_CLIST(search_list), 0, 220);
	gtk_clist_set_column_width(GTK_CLIST(search_list), 1, 60);
	gtk_clist_set_column_width(GTK_CLIST(search_list), 2, 50);
	gtk_clist_set_column_width(GTK_CLIST(search_list), 3, 190);
	//gtk_clist_set_column_width(GTK_CLIST(search_list), 4, 60);


	gtk_signal_connect(GTK_OBJECT(search_list), "select_row",
                           GTK_SIGNAL_FUNC(gui_download_file), NULL);

	gtk_container_add(GTK_CONTAINER(scrolled_window), search_list);
	gtk_container_add(GTK_CONTAINER(frame), scrolled_window);
	gtk_widget_show(search_list);

/*
	stree = gtk_tree_new();
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
					      stree);
	gtk_container_add(GTK_CONTAINER(frame), scrolled_window);
	gtk_widget_show(stree);
*/
	gtk_widget_show(frame);

	gtk_box_pack_start(GTK_BOX(nb_vbox_1), nb_hbox_1, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(nb_vbox_1), nb_hbox_2, FALSE, FALSE, 1);
	gtk_box_pack_start(GTK_BOX(nb_vbox_1), frame, TRUE, TRUE, 1);

	label = gtk_label_new("Search");
	gtk_widget_show(nb_hbox_1);
	gtk_widget_show(nb_hbox_2);
	gtk_widget_show(nb_vbox_1);
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), nb_vbox_1,
				 label);
	/* page 1 */

	/* page 2 */
	nb_hbox_1 = gtk_hbox_new(FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER(nb_hbox_1), 20);

	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_usize(scrolled_window, 100, 200);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
	gtk_widget_show(scrolled_window);

	download_list = gtk_clist_new_with_titles(5, download_list_titles);
	gtk_clist_set_column_width(GTK_CLIST(download_list), 0, 210);
	gtk_clist_set_column_width(GTK_CLIST(download_list), 1,  70);
	gtk_clist_set_column_width(GTK_CLIST(download_list), 2,  80);
	gtk_clist_set_column_width(GTK_CLIST(download_list), 3,  60);
	// Igor. Etwas größer, damit man scrollen kann
	gtk_clist_set_column_width(GTK_CLIST(download_list), 4, 350);

	gtk_container_add(GTK_CONTAINER(scrolled_window), download_list);
	gtk_widget_show(download_list);

	/* download buttons */
	for (i=11; i<15; i++) {
		button = gtk_button_new_with_label(button_names[i-1]);
		gtk_box_pack_start(GTK_BOX(dld_button_box), button, TRUE, TRUE, 3);
		gtk_signal_connect(GTK_OBJECT(button), "clicked",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)i);
		gtk_widget_show(button);
	}

	gtk_widget_show(dld_button_box);
	/* download buttons */

	frame = gtk_frame_new("Limits");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 10);

	label = gtk_label_new("Maximum Downloads:");
	adj = (GtkAdjustment *) gtk_adjustment_new(1, 1, 30, 1,
						   10, 0);
	max_downloads = gtk_spin_button_new(adj, 0, 0);
	gtk_signal_connect(GTK_OBJECT(adj), "value_changed",
			   GTK_SIGNAL_FUNC(spin_changed),
			   (gpointer) SPN_MAX_DOWNLD);

	button = gtk_button_new_with_label(button_names[BTN_CLEAR]);
       	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)BTN_CLEAR);
	gtk_widget_show(button);
	gtk_widget_show(label);
	gtk_widget_show(max_downloads);
	gtk_box_pack_start(GTK_BOX(nb_hbox_1), label, TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_1), max_downloads, TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_1), button, TRUE, TRUE, 3);

	gtk_container_add(GTK_CONTAINER(frame), nb_hbox_1);

	gtk_widget_show(nb_hbox_1);
	gtk_widget_show(frame);

	gtk_box_pack_start(GTK_BOX(nb_vbox_2), scrolled_window,
			   TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_vbox_2), dld_button_box,
			   FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(nb_vbox_2), frame,
			   FALSE, FALSE, 3);

	label = gtk_label_new("Downloads");
	gtk_widget_show(nb_vbox_2);
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), nb_vbox_2,
				 label);

	/* page 2 */

	/* page 3 */
	nb_vbox_3 = gtk_vbox_new(FALSE, 0);
	table = gtk_table_new(2, 4, FALSE);
	gtk_container_set_border_width(GTK_CONTAINER(table), 5);

	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_usize(scrolled_window, 100, 200);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
	gtk_widget_show(scrolled_window);

	upload_list = gtk_clist_new_with_titles(5, upload_list_titles);
	gtk_clist_set_column_width(GTK_CLIST(upload_list), 0, 210);
	gtk_clist_set_column_width(GTK_CLIST(upload_list), 1,  70);
	gtk_clist_set_column_width(GTK_CLIST(upload_list), 2,  80);
	gtk_clist_set_column_width(GTK_CLIST(upload_list), 3,  60);
	gtk_clist_set_column_width(GTK_CLIST(upload_list), 4, 100);
	gtk_container_add(GTK_CONTAINER(scrolled_window), upload_list);
	gtk_widget_show(upload_list);

        /* upload buttons */
	for (i=15; i<19; i++) {
		button = gtk_button_new_with_label(button_names[i-1]);
		gtk_box_pack_start(GTK_BOX(uld_button_box), button, TRUE, TRUE, 3);
		gtk_signal_connect(GTK_OBJECT(button), "clicked",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)i);
		gtk_widget_show(button);
	}
	gtk_widget_show(uld_button_box);
	/* upload buttons */

	frame = gtk_frame_new("Limits");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
	gtk_widget_show(frame);

	label = gtk_label_new("Maximum Uploads:");
	adj = (GtkAdjustment *) gtk_adjustment_new(1, 1, 30, 1,
						   10, 0);
	max_uploads = gtk_spin_button_new(adj, 0, 0);
	gtk_signal_connect(GTK_OBJECT(adj), "value_changed",
			   GTK_SIGNAL_FUNC(spin_changed),
			   (gpointer) SPN_MAX_UPLD);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), max_uploads, 1, 2, 0, 1);

	gtk_widget_show(label);
	gtk_widget_show(max_uploads);

	label = gtk_label_new("Maximum Uploads Per Host:");
	adj = (GtkAdjustment *) gtk_adjustment_new(1, 1, 30, 1,
						   10, 0);
	max_uploads_per_host = gtk_spin_button_new(adj, 0, 0);
	gtk_signal_connect(GTK_OBJECT(adj), "value_changed",
			   GTK_SIGNAL_FUNC(spin_changed),
			   (gpointer) SPN_MAX_UPLD_PER_HOST);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
	gtk_table_attach_defaults(GTK_TABLE(table), max_uploads_per_host,
				  1, 2, 1, 2);

	gtk_widget_show(label);
	gtk_widget_show(max_uploads_per_host);

	label = gtk_label_new("Bandwidth");
	gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 4, 0, 1);
	gtk_widget_show(label);

	adj = (GtkAdjustment *) gtk_adjustment_new(0, 0, 100, 1, 5, 1);
	hscale = gtk_hscale_new(adj);
	gtk_table_attach_defaults(GTK_TABLE(table), hscale, 2, 4, 1, 2);
	gtk_widget_show(hscale);

	gtk_container_add(GTK_CONTAINER(frame), table);
	gtk_widget_show(table);

	gtk_box_pack_start(GTK_BOX(nb_vbox_3), scrolled_window,
			   TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_vbox_3), uld_button_box,
			   FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(nb_vbox_3), frame,
			   FALSE, FALSE, 3);

	label = gtk_label_new("Uploads");
	gtk_widget_show(nb_vbox_3);
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), nb_vbox_3,
				 label);

	/* page 3 */

	/* page 4 */
	nb_vbox_4 = gtk_vbox_new(FALSE, 0);
	nb_hbox_1 = gtk_hbox_new(FALSE, 0);
	nb_hbox_2 = gtk_hbox_new(FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER(nb_hbox_1), 5);
	gtk_container_set_border_width(GTK_CONTAINER(nb_hbox_2), 5);

	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_usize(scrolled_window, 100, 160);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
	gtk_widget_show(scrolled_window);

	con_list = gtk_clist_new_with_titles(4, con_list_titles);
	// Igor: Habe ich etwas angepaßt
	gtk_clist_set_column_width(GTK_CLIST(con_list), 0, 240);
	gtk_clist_set_column_width(GTK_CLIST(con_list), 1, 60);
	gtk_clist_set_column_width(GTK_CLIST(con_list), 2, 180);

	gtk_container_add(GTK_CONTAINER(scrolled_window), con_list);
	gtk_widget_show(con_list);

	gtk_box_pack_start(GTK_BOX(nb_vbox_4), scrolled_window,
			   TRUE, TRUE, 3);

        host_combo_box = gtk_combo_new();
	gtk_box_pack_start(GTK_BOX(nb_hbox_1), host_combo_box,
			   TRUE, TRUE, 3);

	/* Kervin: This is the first item in the list because
	   draw_init is called before main_init.
	   in main_init the host from the config file are read
	*/
	new_list_item = gtk_list_item_new_with_label(HOST_CACHE_SERVER);
	gtk_widget_show(new_list_item);
	gtk_container_add (GTK_CONTAINER (GTK_COMBO(host_combo_box)->list), new_list_item);
	gtk_widget_show(host_combo_box);

	/* Kervin: Connect the host_combo_box->entry activate signal to gui_button_pressed
	   handler and make it seem like the add button had been pressed.  This is
	   to make the action that occurs when the add button is pressed and the
	   entry box gets an "enter" keypress identical */
	gtk_signal_connect(GTK_OBJECT(GTK_COMBO(host_combo_box)->entry), "activate",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)BTN_ADD_CON);

	gtk_combo_disable_activate(GTK_COMBO(host_combo_box));

	for (i=7; i<9; i++) {
		button = gtk_button_new_with_label(button_names[i]);
		gtk_box_pack_start(GTK_BOX(nb_hbox_1), button, TRUE, TRUE, 1);
		gtk_signal_connect(GTK_OBJECT(button), "clicked",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)i);
		gtk_widget_show(button);
	}

	gtk_box_pack_start(GTK_BOX(nb_vbox_4), nb_hbox_1,
			   FALSE, FALSE, 3);
	gtk_widget_show(nb_hbox_1);

	frame = gtk_frame_new("Hosts");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 3);

	label = gtk_label_new("    Hosts in cache:");
	gtk_widget_show(label);

	host_in_cache_label = gtk_label_new("0");
	gtk_widget_show(host_in_cache_label);

	button = gtk_button_new_with_label(button_names[BTN_CLR_HCACHE-1]);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)BTN_CLR_HCACHE);
	gtk_widget_show(button);

	gtk_box_pack_start(GTK_BOX(nb_hbox_2), label, TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_2), host_in_cache_label,
			   TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(nb_hbox_2), button, TRUE, TRUE, 60);

	gtk_box_pack_start(GTK_BOX(frame_vbox), nb_hbox_2, FALSE, FALSE, 5);
	gtk_container_add(GTK_CONTAINER(frame), frame_vbox);
	gtk_widget_show(nb_hbox_2);
	gtk_widget_show(frame_vbox);

	gtk_box_pack_start(GTK_BOX(nb_vbox_4), frame,
			   FALSE, FALSE, 3);
	gtk_widget_show(frame);

	frame = gtk_frame_new("Limits");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 5);
	gtk_widget_show(frame);

	table = gtk_table_new(2, 4, TRUE);

	label = gtk_label_new("Min. Connections:");
	adj = (GtkAdjustment *) gtk_adjustment_new(1, 0, 30., 1.,
						   10., 0.);
	min_con = gtk_spin_button_new(adj, 0, 0);
	gtk_signal_connect(GTK_OBJECT(adj), "value_changed",
		      GTK_SIGNAL_FUNC(gui_min_con_changed),
		      (gpointer)NULL);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), min_con, 1, 2, 0, 1);
	gtk_widget_show(label);
	gtk_widget_show(min_con);

	label = gtk_label_new("Max. Connections:");
	adj = (GtkAdjustment *) gtk_adjustment_new(1, 0, 30., 1.,
						   10., 0.);
	max_con = gtk_spin_button_new(adj, 0, 0);
	gtk_signal_connect(GTK_OBJECT(adj), "value_changed",
		      GTK_SIGNAL_FUNC(gui_max_con_changed),
		      (gpointer)NULL);

	gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 0, 1);
	gtk_table_attach_defaults(GTK_TABLE(table), max_con, 3, 4, 0, 1);
	gtk_widget_show(label);
	gtk_widget_show(max_con);

	auto_connect_check_box = gtk_check_button_new_with_label("Connect automatically");
	gtk_signal_connect(GTK_OBJECT(auto_connect_check_box), "toggled",
			   GTK_SIGNAL_FUNC(gui_toggle_auto_connect), NULL);

	gtk_table_attach_defaults(GTK_TABLE(table), auto_connect_check_box, 0, 2, 1, 2);
	gtk_widget_show(auto_connect_check_box);
	accept_in_con_check_box = gtk_check_button_new_with_label("Accept Incoming connections");
	gtk_signal_connect(GTK_OBJECT(accept_in_con_check_box), "toggled",
			   GTK_SIGNAL_FUNC(gui_toggle_accept_in_con), NULL);

	gtk_table_attach_defaults(GTK_TABLE(table), accept_in_con_check_box, 2, 4, 1, 2);
	gtk_widget_show(accept_in_con_check_box);

	gtk_container_add(GTK_CONTAINER(frame), table);
	gtk_widget_show(table);
	gtk_box_pack_start(GTK_BOX(nb_vbox_4), frame,
			   FALSE, FALSE, 3);

	label = gtk_label_new("Connections");
	gtk_widget_show(nb_vbox_4);
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), nb_vbox_4,
				 label);

	/* page 4 */
	gtk_widget_show(window);

        /* add the logo */
	align = gtk_alignment_new(1.0, 0.0, 0.0, 0.0);
	gtk_widget_show(align);

	style = gtk_widget_get_style(window);

	xpm_path = strdup(PACKAGE_DATA_DIR"/pixmaps/gnewtellium.xpm");

	pixmap = gdk_pixmap_create_from_xpm(window->window, &mask,
					    &style->white,
					    xpm_path);
					    // "/usr/local/share/gnewtellium/pixmaps/gnewtellium.xpm");

	/* If there was a problem with the xpm file
	   Use the data
	*/
	if (!pixmap)
		pixmap = gdk_pixmap_create_from_xpm_d(window->window, &mask,
						      &style->white,
						      gnewtellium_xpm);

	pixmapwid = gtk_pixmap_new(pixmap, mask);
	gtk_widget_show(pixmapwid);

	gtk_container_add(GTK_CONTAINER(align), pixmapwid);
	gtk_box_pack_start(GTK_BOX(pixmap_box), align, TRUE, TRUE, 3);
	gtk_box_pack_start(GTK_BOX(main_vbox), pixmap_box, FALSE, FALSE, 3);
	gtk_widget_show(pixmap_box);

	gtk_container_add(GTK_CONTAINER(main_vbox), main_notebook);
	gtk_widget_show(main_notebook);

	/* buttons */
	for (i=0; i<4; i++) {
		button = gtk_button_new_with_label(button_names[i]);
		gtk_box_pack_start(GTK_BOX(button_box), button, TRUE, TRUE, 3);
		gtk_signal_connect(GTK_OBJECT(button), "clicked",
				   GTK_SIGNAL_FUNC(gui_button_pressed), (gpointer)i);
		gtk_widget_show(button);
	}

	gtk_box_pack_start(GTK_BOX(main_vbox), button_box, FALSE, FALSE, 3);
	gtk_container_add(GTK_CONTAINER(window), main_vbox);

	gtk_widget_show(button_box);
	gtk_widget_show(main_vbox);

	gtk_widget_set_uposition(window, 100, 20);

	gtk_widget_show(window);
	return 1;
}

void spin_changed(GtkWidget *widget, gpointer data)
{
	switch ((int)data) {
	case SPN_MAX_DOWNLD:
		gl_options->max_downloads =
			gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(max_downloads));
		break;
	case SPN_MAX_UPLD:
		gl_options->max_uploads =
			gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(max_uploads));
		break;
	case SPN_MAX_UPLD_PER_HOST:
		gl_options->max_uploads_per_host =
			gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(max_uploads_per_host));
		break;
	default:
		break;
	}
}
