/*  Copyright (C) 2001-2002  Kenichi Suto
 *
 *  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 "defs.h"
#include "global.h"

static HISTORY *history_tail = NULL;
extern CONTENT_AREA *dict_area;

gchar *ebook_get_text(BOOK_INFO *binfo, int page, int offset);
void show_text(BOOK_INFO *binfo, char *text);

GList *word_history=NULL;

void show_history(CONTENT_AREA *content_area)
{
	HISTORY *history;
	GList *list;
	
	g_print("*** HISTORY ***\n");

	list = g_list_first(content_area->history);
	while(list){
		history = (HISTORY *)(list->data);
		g_print("bookinfo = %08x\n", history->book_info);
		g_print("page = %08x\n", history->page);
		g_print("offset = %08x\n", history->offset);
		list = g_list_next(list);
	}
	g_print("*** HISTORY END ***\n");
}


void history_back(CONTENT_AREA *content_area)
{
	gchar *text;
	HISTORY *history;
	GList *previous;

	if(eb_web == 0){
		if(content_area->current_in_history == NULL) {
			return;
		}

		previous = g_list_previous(content_area->current_in_history);
		if(previous == NULL){
			return;
		}
	
		history = (HISTORY *)(previous->data);
		g_assert(history != NULL);
		text = ebook_get_text(history->book_info,
				      history->page,
				      history->offset);

		content_area->show_func(history->book_info, text);
	
		current_book_info = history->book_info;
		current_position.page = history->page;
		current_position.offset = history->offset;

		content_area->current_in_history = previous;
	} else {
	}

	if(hex_dlg != NULL)
		dump_hex();
	if(text_dlg != NULL)
		dump_text();



}

void history_forward(CONTENT_AREA *content_area)
{
	gchar *text;
	HISTORY *history;
	GList *next;

	if(eb_web == 0){
		if(content_area->current_in_history == NULL)
			return;

		next = g_list_next(content_area->current_in_history);
		if(next == NULL)
			return;
	
		history = (HISTORY *)(next->data);
		g_assert(history != NULL);
		text = ebook_get_text(history->book_info,
				      history->page,
				      history->offset);

		content_area->show_func(history->book_info, text);
	
		current_book_info = history->book_info;
		current_position.page = history->page;
		current_position.offset = history->offset;

		content_area->current_in_history = next;
	} else {
//		web_forward(NULL, NULL);
	}

	if(hex_dlg != NULL)
		dump_hex();
	if(text_dlg != NULL)
		dump_text();

}

void save_history(CONTENT_AREA *content_area, BOOK_INFO *binfo, gint page, gint offset)
{

	HISTORY *history;

	GList *next;

	// $B8=:_I=<(FbMF$,%R%9%H%j$N:G8e$G$J$$>l9g$K$O(B
	// $B0J9_$r:o=|$9$k(B
	if(content_area->current_in_history){
		next = g_list_next(content_area->current_in_history);
		
		while(next){
			history = (HISTORY *)(next->data);
			content_area->history = g_list_remove(content_area->history, next->data);
			free(history);
			next = g_list_next(content_area->current_in_history);
		}
	}

//	show_history(content_area);

	history = (HISTORY *)calloc(sizeof(HISTORY),1);
	history->book_info = binfo;
	history->page = page;
	history->offset = offset;

	content_area->history = g_list_append(content_area->history, history);
	content_area->current_in_history = g_list_last(content_area->history);

//	show_history(content_area);
}

void clear_history(CONTENT_AREA *content_area)
{

	HISTORY *history;
	GList *next;

	if(content_area->history == NULL)
		return;

	// $B8=:_I=<(FbMF$,%R%9%H%j$N:G8e$G$J$$>l9g$K$O(B
	// $B0J9_$r:o=|$9$k(B
	next = g_list_first(content_area->history);
	while(next){
		history = (HISTORY *)(next->data);
		g_assert(next->data != NULL);
		free(next->data);
		next = g_list_next(next);
	}
	g_list_free(content_area->history);
	content_area->history = NULL;
	content_area->current_in_history = NULL;
}


int check_duplicate_entry(GList *list, char *word){
	GList *l;

	l = list;
	while(l != NULL){
		if(strcmp(l->data, word) == 0)
			return 1;
		l = l->next;
	}
	return 0;
}

void show_glist(GList *list){
	GList *l;

	l = list;
	while(l != NULL){
		printf("list : %s\n", (char *)l->data);
		l = l->next;
	}
}

void save_word_history(gchar *word){
	GList *list;
	
//	show_glist(word_history);
	
	if(check_duplicate_entry(word_history, word))
		return;

	if(word_history == NULL){
		word_history = g_list_append(word_history, strdup(word));	
	} else if(g_list_length(word_history) == max_remember_words){
//		list = g_list_last(word_history);
		list = g_list_nth(word_history, max_remember_words-1);
		free(list->data);
		list = g_list_remove(word_history, list->data);
		list = g_list_first(list);
		word_history = g_list_prepend(list, strdup(word));
	} else {
		list = g_list_first(word_history);
		word_history = g_list_prepend(list, strdup(word));
	}

//	show_glist(word_history);
	gtk_combo_set_popdown_strings( GTK_COMBO(combo_word), word_history) ;

}




syntax highlighted by Code2HTML, v. 0.9.1