/* copyright    Maher Awamy <muhri@muhri.net>           *
 *              Charlie Schmidt <ishamael@themes.org>   *
 *                                                      *
 *  released under the GPL                              */
  
#include "skipstone.h"

extern GList *bookmarks;
extern GSList *window_count;

void bookmark_activate(GtkWidget *widget,Bdata *bdata) {
	BookmarkData *bmark = gtk_object_get_data(GTK_OBJECT(widget),"bmark");
	gtk_moz_embed_load_url(bdata->embed,bmark->url);
	return;
}

void add_bookmark(GtkWidget *menuitem,Bdata *bdata) {
	gchar *url = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(bdata->combo)->entry));
	gchar *title = gtk_moz_embed_get_title(bdata->embed);
	gint i;

	BookmarkData *bmark;

	bmark = g_new0(BookmarkData,1);

	bmark->url = g_strdup(url);
	bmark->label = g_strdup(title);

	bookmarks = g_list_append(bookmarks,bmark);

	for (i = 0; i < g_slist_length(window_count); i++) {
		Bdata *bdata = g_slist_nth_data(window_count,i);
		GtkWidget *item;

		item = gtk_menu_item_new_with_label(bmark->label);
		gtk_menu_append(GTK_MENU(bdata->bookmark_menu),item);
		gtk_object_set_data(GTK_OBJECT(item),"bmark",bmark);
		gtk_signal_connect(GTK_OBJECT(item),"activate",GTK_SIGNAL_FUNC(bookmark_activate),bdata);
		gtk_widget_show(item);
	}
	
	write_bookmarks();
	return;
}

void edit_bookmarks(GtkWidget *menuitem,gpointer user_data) {
	BookmarkWindow *bwin;
	GtkWidget *window;
	GtkWidget *vbox1;
	GtkWidget *scrolled_window;
	GtkWidget *clist;
	GtkWidget *hbox1;
	GtkWidget *text_label;
	GtkWidget *text_entry;
	GtkWidget *url_label;
	GtkWidget *url_entry;
	GtkWidget *hbox2;
	GSList *radio_buttons_group = NULL;
	GtkWidget *menu_radio_button;
	GtkWidget *toolbar_radio_button;
	GtkWidget *hbox3;
	GtkWidget *add_button;
	GtkWidget *update_button;
	GtkWidget *remove_button;
	GtkWidget *hbox4;
	GtkWidget *cancel_button;
	GtkWidget *ok_button;

	gchar *titles[] = {"Text","Location","Url"};
	gint i;

	bwin = g_new0(BookmarkWindow,1);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_object_set_data(GTK_OBJECT(window),"window",window);
	gtk_window_set_title(GTK_WINDOW(window),"Edit Bookmarks");
	gtk_widget_set_usize(window,450,250);

	vbox1 = gtk_vbox_new(FALSE,0);
	gtk_widget_show(vbox1);
	gtk_container_add(GTK_CONTAINER(window),vbox1);

	scrolled_window = gtk_scrolled_window_new(NULL,NULL);
	gtk_widget_show(scrolled_window);
	gtk_box_pack_start(GTK_BOX(vbox1),scrolled_window,TRUE,TRUE,0);

	clist = gtk_clist_new_with_titles(3,titles);
	gtk_widget_show(clist);
	gtk_container_add(GTK_CONTAINER(scrolled_window),clist);
	gtk_clist_set_column_width(GTK_CLIST(clist),0,149);
	gtk_clist_set_column_width(GTK_CLIST(clist),1,60);
	gtk_clist_set_column_width(GTK_CLIST(clist),2,80);
	gtk_clist_column_titles_show(GTK_CLIST(clist));

	for (i = 0; i < g_list_length(bookmarks); i++) {
		BookmarkData *bmark = g_list_nth_data(bookmarks,i);
		gchar *clist_entry[3];
		clist_entry[0] = bmark->label;
		clist_entry[1] = g_strdup_printf("%d",bmark->pos);
		clist_entry[2] = bmark->url;

		gtk_clist_append(GTK_CLIST(clist),clist_entry);
	}
	gtk_signal_connect(GTK_OBJECT(clist),"select_row",GTK_SIGNAL_FUNC(on_bookmark_clist_select_row),bwin);
	gtk_signal_connect(GTK_OBJECT(clist),"unselect_row",GTK_SIGNAL_FUNC(on_bookmark_clist_unselect_row),bwin);


	hbox1 = gtk_hbox_new(FALSE,0);
	gtk_widget_show(hbox1);
	gtk_box_pack_start(GTK_BOX(vbox1),hbox1,FALSE,FALSE,0);
	gtk_container_set_border_width(GTK_CONTAINER(hbox1),2);

	text_label = gtk_label_new("Text:");
	gtk_widget_show(text_label);
	gtk_box_pack_start(GTK_BOX(hbox1),text_label,TRUE,FALSE,0);

	text_entry = gtk_entry_new();
	gtk_widget_show(text_entry);
	gtk_box_pack_start(GTK_BOX(hbox1),text_entry,TRUE,TRUE,0);

	url_label = gtk_label_new("Url:");
	gtk_widget_show(url_label);
	gtk_box_pack_start(GTK_BOX(hbox1),url_label,TRUE,FALSE,0);

	url_entry = gtk_entry_new();
	gtk_widget_show(url_entry);
	gtk_box_pack_start(GTK_BOX(hbox1),url_entry,TRUE,TRUE,0);

	hbox2 = gtk_hbox_new(FALSE,0);
	gtk_widget_show(hbox2);
	gtk_box_pack_start(GTK_BOX(vbox1),hbox2,FALSE,FALSE,0);
	gtk_container_set_border_width(GTK_CONTAINER(hbox2),2);

	menu_radio_button = gtk_radio_button_new_with_label(radio_buttons_group,"Place in menu");
	radio_buttons_group = gtk_radio_button_group(GTK_RADIO_BUTTON(menu_radio_button));
	gtk_widget_show(menu_radio_button);
	gtk_box_pack_start(GTK_BOX(hbox2),menu_radio_button,TRUE,FALSE,0);
	gtk_signal_connect(GTK_OBJECT(menu_radio_button),"toggled",GTK_SIGNAL_FUNC(on_bookmark_menu_radio_button_toggled),bwin);

	toolbar_radio_button = gtk_radio_button_new_with_label(radio_buttons_group,"Place in toolbar");
	radio_buttons_group = gtk_radio_button_group(GTK_RADIO_BUTTON(toolbar_radio_button));
	gtk_widget_show(toolbar_radio_button);
	gtk_box_pack_start(GTK_BOX(hbox2),toolbar_radio_button,TRUE,FALSE,0);
	gtk_signal_connect(GTK_OBJECT(toolbar_radio_button),"toggled",GTK_SIGNAL_FUNC(on_bookmark_toolbar_radio_button_toggled),bwin);

	hbox3 = gtk_hbox_new(FALSE,0);
	gtk_widget_show(hbox3);
	gtk_box_pack_start(GTK_BOX(vbox1),hbox3,FALSE,FALSE,0);
	gtk_container_set_border_width(GTK_CONTAINER(hbox3),2);

	add_button = gtk_button_new_with_label("Add");
	gtk_widget_show(add_button);
	gtk_box_pack_start(GTK_BOX(hbox3),add_button,TRUE,TRUE,5);
	gtk_signal_connect(GTK_OBJECT(add_button),"clicked",GTK_SIGNAL_FUNC(on_bookmark_add_button_clicked),bwin);

	update_button = gtk_button_new_with_label("Update");
	gtk_widget_show(update_button);
	gtk_box_pack_start(GTK_BOX(hbox3),update_button,TRUE,TRUE,5);
	gtk_signal_connect(GTK_OBJECT(update_button),"clicked",GTK_SIGNAL_FUNC(on_bookmark_update_button_clicked),bwin);

	remove_button = gtk_button_new_with_label("Remove");
	gtk_widget_show(remove_button);
	gtk_box_pack_start(GTK_BOX(hbox3),remove_button,TRUE,TRUE,5);
	gtk_signal_connect(GTK_OBJECT(remove_button),"clicked",GTK_SIGNAL_FUNC(on_bookmark_remove_button_clicked),bwin);

	hbox4 = gtk_hbox_new(FALSE,0);
	gtk_widget_show(hbox4);
	gtk_box_pack_start(GTK_BOX(vbox1),hbox4,FALSE,FALSE,0);
	gtk_container_set_border_width(GTK_CONTAINER(hbox4),2);

	cancel_button = gtk_button_new_with_label("Cancel");
	gtk_widget_show(cancel_button);
	gtk_box_pack_end(GTK_BOX(hbox4),cancel_button,TRUE,TRUE,5);
	gtk_signal_connect(GTK_OBJECT(cancel_button),"clicked",GTK_SIGNAL_FUNC(on_bookmark_cancel_button_clicked),bwin);

	ok_button = gtk_button_new_with_label("Ok");
	gtk_widget_show(ok_button);
	gtk_box_pack_end(GTK_BOX(hbox4),ok_button,TRUE,TRUE,5);
	gtk_signal_connect(GTK_OBJECT(ok_button),"clicked",GTK_SIGNAL_FUNC(on_bookmark_ok_button_clicked),bwin);

	gtk_widget_show(window);
	gtk_signal_connect(GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(on_bookmark_delete_event),bwin);

	bwin->window = window;
	bwin->clist = clist;
	bwin->text_entry = text_entry;
	bwin->url_entry = url_entry;
	bwin->menu_radio_button = menu_radio_button;
	bwin->toolbar_radio_button = toolbar_radio_button;

	return;
}

void on_bookmark_clist_select_row(GtkWidget *clist,gint row,gint col,GdkEventButton *event,BookmarkWindow *bwin) {
	BookmarkData *bmark = g_list_nth_data(bookmarks,row);
	gtk_entry_set_text(GTK_ENTRY(bwin->text_entry),bmark->label);
	gtk_entry_set_text(GTK_ENTRY(bwin->url_entry),bmark->url);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bwin->menu_radio_button),!bmark->pos);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bwin->toolbar_radio_button),bmark->pos);
	return;
}

void on_bookmark_clist_unselect_row(GtkWidget *clist,gint row,gint col,GdkEventButton *event,BookmarkWindow *bwin) {
	gtk_entry_set_text(GTK_ENTRY(bwin->text_entry),"");
	gtk_entry_set_text(GTK_ENTRY(bwin->url_entry),"");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bwin->menu_radio_button),TRUE);
	return;
}

void on_bookmark_menu_radio_button_toggled(GtkWidget *button,BookmarkWindow *bwin) {
	return;
}

void on_bookmark_toolbar_radio_button_toggled(GtkWidget *button,BookmarkWindow *bwin) {
	return;
}

void on_bookmark_add_button_clicked(GtkWidget *button,BookmarkWindow *bwin) {
	BookmarkData *bmark;
	gchar *clist_entry[3];

	bmark = g_new0(BookmarkData,1);

	bmark->label = g_strdup(gtk_entry_get_text(GTK_ENTRY(bwin->text_entry)));
	bmark->url = g_strdup(gtk_entry_get_text(GTK_ENTRY(bwin->url_entry)));
	bmark->pos = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(bwin->menu_radio_button));

	clist_entry[0] = bmark->label;
	clist_entry[1] = g_strdup_printf("%d",bmark->pos);
     	clist_entry[2] = bmark->url;

	bookmarks = g_list_append(bookmarks,bmark);

	gtk_clist_append(GTK_CLIST(bwin->clist),clist_entry);

	return;
}

void on_bookmark_update_button_clicked(GtkWidget *button,BookmarkWindow *bwin) {
        GList *selection;
	BookmarkData *bmark;
	gchar *clist_entry[3];
	gint row;

        selection = GTK_CLIST(bwin->clist)->selection;

        if (g_list_length(selection) == 0) {
                return;
        }

        row = (gint) g_list_nth_data(selection,0);

	bmark = g_new0(BookmarkData,1);

	bmark->label = g_strdup(gtk_entry_get_text(GTK_ENTRY(bwin->text_entry)));
	bmark->url = g_strdup(gtk_entry_get_text(GTK_ENTRY(bwin->url_entry)));
	bmark->pos = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(bwin->menu_radio_button));

	clist_entry[0] = bmark->label;
	clist_entry[1] = g_strdup_printf("%d",bmark->pos);
     	clist_entry[2] = bmark->url;

	bookmarks = g_list_remove(bookmarks,g_list_nth_data(bookmarks,row));
	bookmarks = g_list_insert(bookmarks,bmark,row);

	gtk_clist_remove(GTK_CLIST(bwin->clist),row);
	gtk_clist_insert(GTK_CLIST(bwin->clist),row,clist_entry);

	return;
}

void on_bookmark_remove_button_clicked(GtkWidget *button,BookmarkWindow *bwin) {
        GList *selection;
	BookmarkData *bmark;
	gint row;

        selection = GTK_CLIST(bwin->clist)->selection;

        if (g_list_length(selection) == 0) {
                return;
        }

        row = (gint) g_list_nth_data(selection,0);

	bmark = g_list_nth_data(bookmarks,row);
	bookmarks = g_list_remove(bookmarks,bmark);
	g_free(bmark->label);
	g_free(bmark->url);
	g_free(bmark);
	
	gtk_clist_remove(GTK_CLIST(bwin->clist),row);

	return;
}

void on_bookmark_cancel_button_clicked(GtkWidget *button,BookmarkWindow *bwin) {
	close_bookmark_window(bwin);
	return;
}

void on_bookmark_ok_button_clicked(GtkWidget *button,BookmarkWindow *bwin) {
	gint i;

	for (i = 0; i < g_slist_length(window_count); i++) {
		Bdata *bdata = g_slist_nth_data(window_count,i);
		generate_bookmark_ui(bdata);
	}

	write_bookmarks();
	close_bookmark_window(bwin);
	return;
}

void on_bookmark_delete_event(GtkWidget *widget,GdkEvent *event,BookmarkWindow *bwin) {
	close_bookmark_window(bwin);
	return;
}

void close_bookmark_window(BookmarkWindow *bwin) {
	gtk_widget_destroy(bwin->window);
	g_free(bwin);
	return;
}
