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

#include <gtk/gtk.h>

#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>

#include "newtella.h"
#include "gui.h"
#include "config.h"
#include "file.h"

GtkWidget *config_window;
GtkWidget *speed_combo;
GtkWidget *speed_edit_box, *port_edit_box, *ip_edit_box, *max_sr_edit_box;
GtkWidget *auto_reconnect_check_box;
GtkWidget *download_dir_label;
GtkWidget *shared_dir_list;

/* Check wether path is a directory */

gboolean is_directory(gchar *path)
{
	/* taken from gtk-gnutella */
        struct stat st;
        if (stat(path, &st) == -1) return FALSE;
        return S_ISDIR(st.st_mode);
}

int config_create_dir(char *c_dir)
{
	if (mkdir(c_dir, 0755) == -1) {
		g_print("Can't create config dir!\n");
                return -1;
        }

        return 1;
}

int config_save_to_file(void)
{
	GList *iter;
        DIR *cfg_dir;
	FILE *cfg_file;
        gchar *cfg_name = newtella_malloc(255);
	char ip[INET6_ADDRSTRLEN] = "\0";

        strcpy(cfg_name, getenv("HOME"));

        cfg_name = strcat(cfg_name, "/.gnewtellium");

        /* check if dir exist */
        cfg_dir = opendir(cfg_name);
        if (!cfg_dir && errno == ENOENT)
                config_create_dir(cfg_name);

	cfg_name = strcat(cfg_name, "/gnewtellium");

	cfg_file = fopen(cfg_name, "w+");

	if (cfg_file) {
		/* some useful comments. :-) */
		fprintf(cfg_file, "# "GNEWTELLIUM" - configuration file\n");
		fprintf(cfg_file, "# this is an auto-generated configuration\n");
		fprintf(cfg_file, "# please do not try to edit it manually\n");
		fprintf(cfg_file, "# the parser is not quite flexible in this version\n\n");

		/* dump configuration */
		fprintf(cfg_file, "speed=%d\n", gl_options->con_speed);
		fprintf(cfg_file, "listen_port=%d\n", gl_options->newtella_port);
		fprintf(cfg_file, "local_ip=");

		memcpy(ip, ip2str(gl_options->local_ip), sizeof(ip));
		fprintf(cfg_file, "%s", ip);

		fprintf(cfg_file, "\n");
		fprintf(cfg_file, "max_downloads=%d\n", gl_options->max_downloads);
		fprintf(cfg_file, "max_uploads=%d\n", gl_options->max_uploads);
		fprintf(cfg_file, "max_uploads_per_host=%d\n", gl_options->max_uploads_per_host);
		fprintf(cfg_file, "max_search_results=%d\n", gl_options->max_search_results);
		fprintf(cfg_file, "host_cache_length=%d\n", gl_options->host_cache_length);
		fprintf(cfg_file, "auto_reconnect=%d\n", gl_options->auto_reconnect);
		fprintf(cfg_file, "min_con=%d\n", gl_options->min_con);
		fprintf(cfg_file, "max_con=%d\n", gl_options->max_con);
		fprintf(cfg_file, "auto_connect=%d\n", gl_options->auto_connect);
		fprintf(cfg_file, "accept_in_con=%d\n", gl_options->accept_in_con);
		fprintf(cfg_file, "download_dir=%s\n", gl_options->download_dir);

		iter = g_list_first(gl_options->shared_dirs);
		while (iter) {
			fprintf(cfg_file, "shared_dir=%s\n", (gchar *) iter->data);
			iter = g_list_next(iter);
		}
	}

	fclose(cfg_file);

	return 1;
}

void config_parse_var(gchar *gv)
{
	gchar *seek;
	gchar *s;
	int pos;
	gchar teil[INET6_ADDRSTRLEN] = "\0";

	seek = (gchar *) memchr(gv, '=', 255);
	seek++;

	if (!strncmp(gv, "speed", 5))
		gl_options->con_speed = atoi(seek);
	else if (!strncmp(gv, "listen_port", 11))
		gl_options->newtella_port = atoi(seek);
	else if (!strncmp(gv, "local_ip", 8)){
	        pos = 0;
		while (*(seek)!='\n'){
                      teil[pos]=*(seek);
                      seek++;
		      pos++;
		}
		bzero(gl_options->local_ip, sizeof(gl_options->local_ip));
  		memcpy(gl_options->local_ip, str2ip(teil), sizeof(gl_options->local_ip));
	}
	else if (!strncmp(gv, "max_downloads", 13))
		gl_options->max_downloads = atoi(seek);
	else if (!strncmp(gv, "max_uploads_per_host", 20))
		gl_options->max_uploads_per_host = atoi(seek);
 	else if (!strncmp(gv, "max_uploads", 11))
		gl_options->max_uploads = atoi(seek);
	else if (!strncmp(gv, "max_search_results", 18))
		gl_options->max_search_results = atoi(seek);
	else if (!strncmp(gv, "host_cache_length", 17))
		gl_options->host_cache_length = atoi(seek);
	else if (!strncmp(gv, "auto_reconnect", 14))
		gl_options->auto_reconnect = atoi(seek);
	else if (!strncmp(gv, "min_con", 7))
		gl_options->min_con = atoi(seek);
	else if (!strncmp(gv, "max_con", 7))
		gl_options->max_con = atoi(seek);
	else if (!strncmp(gv, "auto_connect", 12))
		gl_options->auto_connect = atoi(seek);
	else if (!strncmp(gv, "accept_in_con", 13))
		gl_options->accept_in_con = atoi(seek);
	else if (!strncmp(gv, "download_dir", 12))
		strncpy(gl_options->download_dir, seek, strlen(seek)-1);
	else if (!strncmp(gv, "shared_dir", 10)) {
		s = newtella_malloc(strlen(seek)-1);
		s = memcpy(s, seek, strlen(seek)-1);
		gl_options->shared_dirs = g_list_append(gl_options->shared_dirs, s);
	}
}

int config_load_from_file(void)
{
	FILE *cfg_file;
        gchar *cfg_name = newtella_malloc(255);
        gchar cfg_var[255];

	strcpy(cfg_name, getenv("HOME"));

        cfg_name = strcat(cfg_name, "/.gnewtellium/gnewtellium");

       	cfg_file = fopen(cfg_name, "r");

	if (!cfg_file)
		return 0;

	while (fgets(cfg_var, sizeof(cfg_var), cfg_file)) {
		if (cfg_var[0] == '#' || cfg_var[0] == '\n') continue;

		config_parse_var(cfg_var);
	}

	fclose(cfg_file);

	return 1;
}

void config_refresh_main(void)
{
	/* update gui in the main window according to
	   active configuration
	*/
	/* connect automatically */
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(auto_connect_check_box),
				     gl_options->auto_connect);
	/* accept incoming connections */
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(accept_in_con_check_box),
				     gl_options->accept_in_con);

	/* min/max connections */
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(min_con), gl_options->min_con);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_con), gl_options->max_con);

	/* uploads/downloads */
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_uploads), gl_options->max_uploads);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_uploads_per_host), gl_options->max_uploads_per_host);
	gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_downloads), gl_options->max_downloads);
}

void config_refresh(void)
{
	GList *iter;
	gchar buf[255];
	gchar *s;
	gchar ip[INET6_ADDRSTRLEN] = "\0";

	/* update gui according to active configuration */
	switch (gl_options->con_speed) {
	case 56:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "Modem");
		break;
	case 64:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "ISDN");
		break;
	case 128:
	       	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "ISDN (Dual Channel)");
		break;
	case 512:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "Cable Modem");
		break;
	case 768:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "ADSL");
		break;
	case 1028:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "T1");
		break;
	case 10000:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "T3");
		break;
	default:
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "Custom...");
	}

	g_snprintf(buf, sizeof(buf), "%d", gl_options->con_speed);
	gtk_entry_set_text(GTK_ENTRY(speed_edit_box), buf);

	/* max search results */
	g_snprintf(buf, sizeof(buf), "%d", gl_options->max_search_results);
	gtk_entry_set_text(GTK_ENTRY(max_sr_edit_box), buf);
	/* listen port */
	g_snprintf(buf, sizeof(buf), "%d", gl_options->newtella_port);
	gtk_entry_set_text(GTK_ENTRY(port_edit_box), buf);
	/* local ip */
	memcpy(ip, ip2str(gl_options->local_ip), INET6_ADDRSTRLEN);
	gtk_entry_set_text(GTK_ENTRY(ip_edit_box), ip);
	/* download dir */
	gtk_label_set_text(GTK_LABEL(download_dir_label), g_strdup(gl_options->download_dir));
	/* auto reconnect */
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(auto_reconnect_check_box),
				     gl_options->auto_reconnect);

	/* shared directories */
	iter = g_list_first(gl_options->shared_dirs);
	while (iter) {
		s = newtella_malloc(255);
		s = strcpy(s, (gchar *)iter->data);
		gtk_clist_append(GTK_CLIST(shared_dir_list), &s);
		iter = g_list_next(iter);
	}
}

void config_set_default(void)
{
	/* default values for configuration */
	gl_options->con_speed = 56;
	gl_options->newtella_port = 6346;
	bzero(gl_options->local_ip, sizeof(gl_options->local_ip));
	memcpy(gl_options->local_ip, gl_my_host_info->ip, sizeof(gl_options->local_ip));

	gl_options->max_downloads = 10;
	gl_options->max_uploads = 10;
	gl_options->max_uploads_per_host = 3;
	gl_options->max_search_results = 1000;
	gl_options->host_cache_length = 100;

	gl_options->auto_reconnect = 1;

	gl_options->min_con = 2;
	gl_options->max_con = 3;
	gl_options->auto_connect = 1;
	gl_options->accept_in_con = 1;

	strcpy(gl_options->download_dir, getenv("HOME"));
}

void config_speed_change(GtkWidget *widget, gpointer data)
{
	gchar buf[255];
	gchar *selection = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry));

	if (!strcmp(selection, "Modem"))
		gl_options->con_speed = 56;
	else if (!strcmp(selection, "ISDN"))
		gl_options->con_speed = 64;
	else if (!strcmp(selection, "ISDN (Dual Channel)"))
		gl_options->con_speed = 128;
	else if (!strcmp(selection, "Cable Modem"))
		gl_options->con_speed = 512;
	else if (!strcmp(selection, "ADSL"))
		gl_options->con_speed = 768;
	else if (!strcmp(selection, "T1"))
		gl_options->con_speed = 1024;
	else if (!strcmp(selection, "T3"))
		gl_options->con_speed = 10000;
	else if (!strcmp(selection, "Custom..."))
		return;

	g_snprintf(buf, sizeof(buf), "%d", gl_options->con_speed);

	gtk_entry_set_text(GTK_ENTRY(speed_edit_box), buf);
}

void config_speed_entry_change(GtkWidget *widget, gpointer data)
{
	gchar *speed = gtk_entry_get_text(GTK_ENTRY(speed_edit_box));

	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(speed_combo)->entry), "Custom...");

	gl_options->con_speed = atoi(speed);
}

void config_file_request_destroy(GtkWidget *widget, gpointer data)
{
	gtk_widget_destroy(GTK_WIDGET(data));
}

void config_dowload_dir(GtkWidget *widget, gpointer data)
{
	gchar *download_path = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));

	if (is_directory(download_path)) {
		strcpy(gl_options->download_dir, download_path);
		gtk_label_set_text(GTK_LABEL(download_dir_label), g_strdup(gl_options->download_dir));
	}

	gtk_widget_destroy(GTK_WIDGET(data));
}

void config_download_dir_select(void)
{
	GtkWidget *filesel;

	filesel = gtk_file_selection_new("Please choose a directory to store your files");
	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
			   GTK_SIGNAL_FUNC(config_dowload_dir), (gpointer) filesel);
	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
			   GTK_SIGNAL_FUNC(config_file_request_destroy), (gpointer) filesel);

	gtk_widget_show(filesel);

}

int config_shared_exists(char *shared)
{
	GList *iter;

	iter = g_list_first(gl_options->shared_dirs);
	while (iter) {

		if (!strcmp((char *)iter->data, shared))
			return 1;

		iter = g_list_next(iter);
	}

	return 0;
}

void config_shared_dir_add(GtkWidget *widget, gpointer data)
{
       	gchar *shared_dir_new = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));
	gchar *s = newtella_malloc(255);

	s = strcpy(s, shared_dir_new);
	if (is_directory(shared_dir_new) && !config_shared_exists(shared_dir_new)) {
		gl_options->shared_dirs = g_list_append(gl_options->shared_dirs, s);
		/* update database */
		file_build_db(shared_dir_new);
		/* add entry to the list box */
		gtk_clist_append(GTK_CLIST(shared_dir_list), &shared_dir_new);
	}

	gtk_widget_destroy(GTK_WIDGET(data));
}

void config_shared_dir_select(void)
{
	GtkWidget *filesel;

	filesel = gtk_file_selection_new("Please choose a directory containing your files");
	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
			   GTK_SIGNAL_FUNC(config_shared_dir_add), (gpointer) filesel);
	gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
			   GTK_SIGNAL_FUNC(config_file_request_destroy), (gpointer) filesel);

	gtk_widget_show(filesel);
}

void config_shared_dir_remove(void)
{
	gint row = (gint) GTK_CLIST(shared_dir_list)->selection->data;
	gchar *shared;
	GList *iter;
        gtk_clist_get_text(GTK_CLIST(shared_dir_list), row, 0, &shared);

	/* remove songs from db */
	file_exclude_from_db(shared);

       	gl_options->shared_dirs = g_list_remove(gl_options->shared_dirs, shared);

	iter = g_list_first(gl_options->shared_dirs);
	while (iter) {

		if (!strcmp(shared, iter->data)) {
			gl_options->shared_dirs = g_list_remove(gl_options->shared_dirs,
								iter->data);
			newtella_free(iter->data);
			break;
		}

		iter = g_list_next(iter);
	}

	/* remove entry from clist */
	gtk_clist_remove(GTK_CLIST(shared_dir_list), row);

	newtella_free(shared);
}

int config_button_pressed(GtkWidget *widget, gpointer data)
{
	switch ((int)data) {
	case BTN_DWNLD_DIR:
		config_download_dir_select();
		break;
	case BTN_SHR_DIR_ADD:
		config_shared_dir_select();
		break;
	case BTN_SHR_DIR_REMOVE:
		config_shared_dir_remove();
		break;
	case BTN_CONF_OK:
		config_save_to_file();
		gtk_widget_destroy(GTK_WIDGET(config_window));
		break;
	case BTN_CONF_APPLY:
		config_save_to_file();
		break;
	case BTN_CONF_CANCEL:
		gtk_widget_destroy(GTK_WIDGET(config_window));
		break;
	default:
		break;
	}

	return 1;
}

int config_toggle_reconnect(GtkToggleButton *toggle, gpointer data)
{
	gl_options->auto_reconnect = gtk_toggle_button_get_active(toggle);

	return 1;
}


int config_change_search_results(GtkWidget *widget, gpointer data)
{
	gchar *sr = gtk_entry_get_text(GTK_ENTRY(max_sr_edit_box));
	gl_options->max_search_results = atoi(sr);

	return 1;
}

int config_change_listen_port(GtkWidget *widget, gpointer data)
{
	gchar *listen_port = gtk_entry_get_text(GTK_ENTRY(port_edit_box));
	gl_options->newtella_port= atoi(listen_port);

	return 1;
}

int config_change_local_ip(GtkWidget *widget, gpointer data)
{
	gchar *local_ip = gtk_entry_get_text(GTK_ENTRY(ip_edit_box));
	bzero(gl_options->local_ip, sizeof(gl_options->local_ip));
	memcpy(gl_options->local_ip, str2ip(local_ip), sizeof(gl_options->local_ip));

	return 1;
}

void config_draw(void)
{
	GtkWidget *notebook;
	GtkWidget *vbox, *hbox;
	GtkWidget *label;
	GtkWidget *ignore_edit_box;
	GtkWidget *button;
	GtkWidget *frame;
	GtkWidget *scrolled_window;
	GtkWidget *ignore_list;
	GList *entries = NULL;

    	config_window = gtk_dialog_new();

	gtk_container_set_border_width(GTK_CONTAINER(config_window), 10);

	/* config notebook */
    	notebook = gtk_notebook_new();
   	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(config_window)->vbox), notebook, FALSE, FALSE, 10);

	/* page 1 */
	vbox = gtk_vbox_new(FALSE, 0);
	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Internet Connection Type");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(label);

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);

	speed_combo = gtk_combo_new();
	entries = g_list_append(entries, "Modem");
	entries = g_list_append(entries, "ISDN");
	entries = g_list_append(entries, "ISDN (Dual Channel)");
	entries = g_list_append(entries, "Cable Modem");
	entries = g_list_append(entries, "ADSL");
	entries = g_list_append(entries, "T1");
	entries = g_list_append(entries, "T3");
	entries = g_list_append(entries, "Custom...");

	gtk_combo_set_popdown_strings(GTK_COMBO(speed_combo), entries);
	g_list_free(entries);
	gtk_signal_connect(GTK_OBJECT(GTK_COMBO(speed_combo)->entry), "changed",
			   GTK_SIGNAL_FUNC(config_speed_change), NULL);

	gtk_widget_show(speed_combo);
	gtk_box_pack_start(GTK_BOX(hbox), speed_combo, FALSE, FALSE, 0);

	label = gtk_label_new("Speed:");
	gtk_widget_show(label);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);

	speed_edit_box = gtk_entry_new_with_max_length(5);
	gtk_widget_set_usize(speed_edit_box, 40, 20);
	gtk_signal_connect(GTK_OBJECT(speed_edit_box), "activate",
			   GTK_SIGNAL_FUNC(config_speed_entry_change), NULL);

	gtk_widget_show(speed_edit_box);
	gtk_box_pack_start(GTK_BOX(hbox), speed_edit_box, FALSE, FALSE, 0);

	label = gtk_label_new("kbps");
	gtk_widget_show(label);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
	gtk_widget_show(hbox);

	label = gtk_label_new("Let Newtella know what speed your Internet Connection is.
Setting this to an accurate number will help you get the most out of
Newtella's advanced bandwidth management.");
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(label);

	gtk_widget_show(vbox);

	label = gtk_label_new("Connection");
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);

	/* page 1 */
	/* page 2 */
	vbox = gtk_vbox_new(FALSE, 0);
	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Save downloads to:");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(label);

	button = gtk_button_new_with_label("  Directory for downloads  ");
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_DWNLD_DIR);

	download_dir_label = gtk_label_new("");
	gtk_widget_show(download_dir_label);

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(hbox), download_dir_label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(button);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Select the directory where you want to store your newly
downloaded files.");
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(label);

	label = gtk_label_new("Download");
	gtk_widget_show(label);
	gtk_widget_show(vbox);
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);

	/* page 2 */

	/* page 3 */
	vbox = gtk_vbox_new(FALSE, 0);

	frame = gtk_frame_new("Shared directories");
	gtk_container_set_border_width(GTK_CONTAINER(frame), 10);

        scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 5);
	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);

	shared_dir_list = gtk_clist_new(1);
	gtk_container_add(GTK_CONTAINER(scrolled_window), shared_dir_list);
	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, FALSE, FALSE, 5);
	gtk_widget_show(shared_dir_list);

	hbox = gtk_hbox_new(FALSE, 0);
	button = gtk_button_new_with_label("Add");
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_SHR_DIR_ADD);
	gtk_widget_show(button);

	button = gtk_button_new_with_label("Remove");
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_SHR_DIR_REMOVE);
	gtk_widget_show(button);
	gtk_widget_show(hbox);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

	label = gtk_label_new("Add any number of directories here.
All files under any of these directories can then be
downloaded by other Newtella users.");
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);
	gtk_widget_show(label);

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

	gtk_widget_show(frame);

	label = gtk_label_new("Shared files");
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);
	/* page 3 */

	/* page 4 */
	vbox = gtk_vbox_new(FALSE, 0);
	hbox = gtk_hbox_new(FALSE, 0);

	label = gtk_label_new("Listen port:");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 15);

	port_edit_box = gtk_entry_new_with_max_length(4);
	gtk_widget_set_usize(port_edit_box, 50, 20);
	gtk_signal_connect(GTK_OBJECT(port_edit_box), "changed",
			   GTK_SIGNAL_FUNC(config_change_listen_port), (gpointer) NULL);

	gtk_box_pack_start(GTK_BOX(hbox), port_edit_box, FALSE, FALSE, 172);
	gtk_widget_show(label);
	gtk_widget_show(port_edit_box);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);
	gtk_widget_show(hbox);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Set the port where Newtella should listen for incoming connections.");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 15);
	gtk_widget_show(label);
	gtk_widget_show(hbox);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Max search results returned:");
	max_sr_edit_box = gtk_entry_new_with_max_length(5);
	gtk_widget_set_usize(max_sr_edit_box, 50, 20);
	gtk_signal_connect(GTK_OBJECT(max_sr_edit_box), "changed",
			   GTK_SIGNAL_FUNC(config_change_search_results), (gpointer) NULL);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 15);
	gtk_box_pack_start(GTK_BOX(hbox), max_sr_edit_box, FALSE, FALSE, 75);
	gtk_widget_show(label);
	gtk_widget_show(max_sr_edit_box);
	gtk_widget_show(hbox);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Select how much search results Newtella will maximally send to
other clients. Use this to limit the impact of stupid searches on your
computer.");
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_widget_show(label);
	gtk_widget_show(hbox);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);

	hbox = gtk_hbox_new(FALSE, 0);
	auto_reconnect_check_box =
		gtk_check_button_new_with_label("Recconect to the Internet if disconnected");
	gtk_signal_connect(GTK_OBJECT(auto_reconnect_check_box), "toggled",
		       GTK_SIGNAL_FUNC(config_toggle_reconnect), NULL);

	gtk_box_pack_start(GTK_BOX(hbox), auto_reconnect_check_box, FALSE, FALSE, 10);
	gtk_widget_show(hbox);
	gtk_widget_show(auto_reconnect_check_box);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Force local IP to:");
	ip_edit_box = gtk_entry_new_with_max_length(INET6_ADDRSTRLEN);
	gtk_widget_set_usize(ip_edit_box, 200, 20);
	gtk_signal_connect(GTK_OBJECT(ip_edit_box), "changed",
			   GTK_SIGNAL_FUNC(config_change_local_ip), (gpointer)NULL);

	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 15);
	gtk_box_pack_start(GTK_BOX(hbox), ip_edit_box, FALSE, FALSE, 75);
	gtk_widget_show(label);
	gtk_widget_show(ip_edit_box);
	gtk_widget_show(hbox);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Returns the entered IP with search results instead of your real one.");
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_widget_show(label);
	gtk_widget_show(hbox);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);

	gtk_widget_show(vbox);

	label = gtk_label_new("Advanced Network");
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
	/* page 4 */

	/* page 5 */
	vbox = gtk_vbox_new(FALSE, 0);

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

        scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 5);
	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);

	ignore_list = gtk_clist_new(1);
	gtk_container_add(GTK_CONTAINER(scrolled_window), ignore_list);
	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, FALSE, FALSE, 5);
	gtk_widget_show(ignore_list);

	hbox = gtk_hbox_new(FALSE, 0);
	ignore_edit_box = gtk_entry_new_with_max_length(255);
	gtk_box_pack_start(GTK_BOX(hbox), ignore_edit_box, FALSE, FALSE, 10);
	gtk_widget_show(ignore_edit_box);
	button = gtk_button_new_with_label("Add");
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_widget_show(button);
	button = gtk_button_new_with_label("Remove");
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_widget_show(button);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
	gtk_widget_show(hbox);

	hbox = gtk_hbox_new(FALSE, 0);
	label = gtk_label_new("Enter all the results you want to ignore here.");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
	gtk_widget_show(label);

	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);
	gtk_widget_show(hbox);

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

	gtk_widget_show(frame);
	label = gtk_label_new("Ignore");
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);

	/* page 5 */
	gtk_widget_show(notebook);

	hbox = gtk_hbox_new(FALSE, 0);
	button = gtk_button_new_with_label("     OK     ");
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_CONF_OK);

	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_widget_show(button);



	button = gtk_button_new_with_label("   Cancel   ");
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_CONF_CANCEL);

	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_widget_show(button);


	button = gtk_button_new_with_label("   Apply   ");
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   GTK_SIGNAL_FUNC(config_button_pressed), (gpointer) BTN_CONF_APPLY);

	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 10);
	gtk_widget_show(button);


	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(config_window)->vbox), hbox, FALSE, FALSE, 10);
	gtk_widget_show(hbox);

	gtk_widget_show(config_window);

	/* adjust config settings */
	config_refresh();
}
