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

#include "skipstone.h"


extern GSList *window_count;
extern ConfigData config;
extern GList *combo_urls;
extern GtkWidget *main_notebook;
extern GtkWidget *main_window;
extern GoMenu *notebook_go_menu;


static void retab(SkipStone *skipstone);


gchar *skipstone_get_engine(void)
{
   
   return "WebKit";
   
}


static void retab(SkipStone *skipstone)
{
     	gchar *title;
	D_ENTER;
     	g_signal_handlers_destroy(GTK_OBJECT(skipstone->window));
     	title = GTK_WINDOW(skipstone->window)->title;

     	if (title)
     		skipstone->notebook_label = gtk_label_new(title);
     	else
       		skipstone->notebook_label = gtk_label_new("SkipStone -");
     	
     	skipstone->is_notebook = 1;

     	g_object_ref(G_OBJECT(skipstone->vbox));
	gtk_container_remove(GTK_CONTAINER(skipstone->window), skipstone->vbox);
     	
     	if (skipstone->menuhandlebox && GTK_IS_WIDGET(skipstone->menuhandlebox)) gtk_widget_destroy(skipstone->menuhandlebox);
     	if (skipstone->bmarkhandlebox && GTK_IS_WIDGET(skipstone->bmarkhandlebox)) gtk_widget_destroy(skipstone->bmarkhandlebox);
	
     	skipstone->menuhandlebox = NULL;
     	skipstone->bmarkhandlebox = NULL;
     
     	if (skipstone->bookmark_toolbar_widgets) {
	     	g_slist_free(skipstone->bookmark_toolbar_widgets);
	     	skipstone->bookmark_toolbar_widgets = NULL;
	}      
     	
     	if (skipstone->go_menu && skipstone->go_menu->items) {
	     	g_slist_free(skipstone->go_menu->items); 
	     	g_free(skipstone->go_menu);
	}
     	skipstone_append_tab(skipstone);
     	gtk_widget_destroy(skipstone->window);
	D_RETURN;
}





void link_message(WebKitWebView *embed, const gchar *title, const gchar *link, SkipStone *skipstone)
{
	
	D_ENTER;

	   gtk_statusbar_pop(GTK_STATUSBAR(skipstone->statusbar),
			  skipstone->sbar_temp_msg_id);
	if (link) {
	  gtk_statusbar_push(GTK_STATUSBAR(skipstone->statusbar),
			     skipstone->sbar_temp_msg_id,
			     link);
	   if (skipstone->link) g_free (skipstone->link);
	   skipstone->link = g_strdup(link);
	}
   

	D_RETURN;
}


void title_changed(WebKitWebView *embed, WebKitWebFrame *web_frame,const gchar *title, SkipStone *skipstone)
{
   
	gchar *doc_title=NULL;
	GSList *l;
     	D_ENTER;
     	if (skipstone->title) g_free(skipstone->title);

   skipstone->title = NULL;

   skipstone->title = g_strdup(title);
     	if (!skipstone->title || !strcmp(skipstone->title,"")) skipstone->title = g_strdup(_("Untitled Document"));
		
     	if (skipstone->is_notebook) {
	     	
	     	if (get_current_browser_from_notebook() == skipstone) {
		     gchar *t = g_strdup_printf(_("SkipStone - %s"), skipstone->title);
		     gtk_window_set_title(GTK_WINDOW(main_window), t);
		     g_free(t);

		}
	     	gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(main_notebook), skipstone->vbox, skipstone->title);
	     	
	     	if (config.shorten_tabs == 1 && strlen(skipstone->title) > config.tab_text_length) {
		     	gchar *label_title = g_strndup(skipstone->title, config.tab_text_length);
		     	gtk_label_set_text(GTK_LABEL(skipstone->notebook_label), label_title);
			g_free(label_title);
		} else {
		     	gtk_label_set_text(GTK_LABEL(skipstone->notebook_label), skipstone->title);
		}

	     	if (config.shorten_tabs == 2)
	       		gtk_widget_set_usize(GTK_WIDGET(skipstone->notebook_label), config.tab_text_length, -2);
	     	else
	       		gtk_widget_set_usize(GTK_WIDGET(skipstone->notebook_label), -1, -2);
	     
		gtk_widget_queue_resize(skipstone->notebook_label->parent);
		update_navigation_buttons(skipstone);	     	
	     	D_RETURN;
	}
     	doc_title = g_strdup_printf(_("SkipStone - %s"), skipstone->title);
     	gtk_window_set_title(GTK_WINDOW(skipstone->window), doc_title);
     	g_free(doc_title);

	for (l = window_count; l ; l = l->next) {
		SkipStone *skipstone = l->data;
		if (!skipstone->is_notebook && skipstone->menubar)
	     		make_window_menu(skipstone);
	}
     	update_navigation_buttons(skipstone);
	D_RETURN;
}


void url_clicked(WebKitWebView *embed, WebKitWebFrame *web_frame, SkipStone *skipstone)
{
	const gchar *location;
	D_ENTER;
     	g_return_if_fail (skipstone->embed !=NULL);

	location = webkit_web_frame_get_uri(web_frame);

     	if (location && strcmp(location, "about:blank")) gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(skipstone->combo)->entry),location);

	D_RETURN;
}


void handle_progress(WebKitWebView *embed, gint progress, SkipStone *skipstone)
{
	/* Future, maybe download time remaining? */

   gchar *msg;
   D_ENTER;


   g_return_if_fail(skipstone->progressbar != NULL);
   g_return_if_fail(GTK_IS_PROGRESS(skipstone->progressbar));
     
     	gtk_progress_set_format_string(GTK_PROGRESS(skipstone->progressbar), "%p%%");

	if (skipstone->timer) {
	     	gtk_timeout_remove(skipstone->timer);
	     	skipstone->timer = 0;
	}
     	
     	gtk_progress_set_activity_mode(GTK_PROGRESS(skipstone->progressbar), FALSE);
   gtk_progress_set_value(GTK_PROGRESS(skipstone->progressbar), progress);




	     	/* msg = g_strdup_printf("(%d%)",progress ); */



     	/* gtk_statusbar_pop(GTK_STATUSBAR(skipstone->statusbar), */
		/* 	  skipstone->sbar_temp_msg_id); */
     	/* gtk_statusbar_push(GTK_STATUSBAR(skipstone->statusbar), */
		/* 	   skipstone->sbar_temp_msg_id, */
		/* 	   msg); */
     	/* g_free(msg); */
	D_RETURN;
}

void update_navigation_buttons(SkipStone *skipstone)
{
     	gboolean can_go_back;
     	gboolean can_go_forward;
     	D_ENTER;
#ifndef WebKit   
     	can_go_back = gtk_moz_embed_can_go_back(GTK_MOZ_EMBED(skipstone->embed));
     	can_go_forward = gtk_moz_embed_can_go_forward(GTK_MOZ_EMBED(skipstone->embed));
#else
       	can_go_back =webkit_web_view_can_go_back(skipstone->embed);
     	can_go_forward = webkit_web_view_can_go_forward(skipstone->embed);

#endif   
   if (skipstone->back) {
	   
	     if (can_go_back)
	     	gtk_widget_set_sensitive(skipstone->back, TRUE);
	     else
	       	gtk_widget_set_sensitive(skipstone->back, FALSE);
	}
     	if (skipstone->forward) {
	     if (can_go_forward)
	       	gtk_widget_set_sensitive(skipstone->forward, TRUE);
	     else
	       	gtk_widget_set_sensitive(skipstone->forward, FALSE);
	}
     	D_RETURN;
}

void start_load(WebKitWebView *embed,WebKitWebFrame *frame, SkipStone *skipstone)
{
       gchar *message;



	D_ENTER;
     	
     	if (skipstone->stop) {
	     	gtk_widget_set_sensitive(skipstone->stop, TRUE);
	}
     	
     	if (skipstone->reload) {
     		gtk_widget_set_sensitive(skipstone->reload, FALSE);
	}

	if (skipstone->is_notebook && config.red_while_loading) {
	     GtkWidget *w = g_object_get_data(G_OBJECT(skipstone->vbox), "anim");
	     if (w) gtk_widget_show(w);
	}

     	g_return_if_fail(skipstone->progressbar != NULL);

     	gtk_progress_set_activity_mode(GTK_PROGRESS(skipstone->progressbar), 1);

   	gtk_progress_configure(GTK_PROGRESS(skipstone->progressbar), 0, 0, 100);
     	skipstone->timer = g_timeout_add (100, (GSourceFunc)progress, skipstone);
     	skipstone->is_loading = TRUE;
	
	message = g_strdup(_("Loading ..."));
	gtk_statusbar_pop(GTK_STATUSBAR(skipstone->statusbar),
			  skipstone->sbar_status_msg_id);
	gtk_statusbar_push(GTK_STATUSBAR(skipstone->statusbar),
			   skipstone->sbar_status_msg_id,
			   message);
	g_free(message);
	
	D_RETURN;
}

void stop_load(WebKitWebView *embed, WebKitWebFrame *frame,  SkipStone *skipstone)
{
   const gchar *location;
   gchar *message;
	D_ENTER;

	g_return_if_fail(skipstone->embed !=NULL);
     	g_return_if_fail(skipstone->progressbar != NULL);
      g_return_if_fail(GTK_IS_PROGRESS_BAR(skipstone->progressbar));          
        if (!skipstone->initilized) {
	   gchar *first_url = NULL;
	   skipstone->initilized = 1;
	   first_url = g_object_get_data(G_OBJECT(skipstone->embed), "first_url");
	   if (first_url) { 
		_skipstone_load_url(skipstone, first_url);
		g_free(first_url);
		g_object_set_data(G_OBJECT(skipstone->embed), "first_url", NULL);
		D_RETURN;
	   } else {

		gtk_widget_grab_focus(GTK_WIDGET(skipstone->embed));
	   }
	     
	}
   
        frame = webkit_web_view_get_main_frame(skipstone->embed);
     	location = webkit_web_frame_get_uri(frame);

	if (location) add_to_history(location);
	
     
	if (skipstone->is_notebook) {
	  go_menu_append(notebook_go_menu, location, skipstone);
	      if (config.red_while_loading) {
		   GtkWidget *w = g_object_get_data(G_OBJECT(skipstone->vbox), "anim");
		   if (w) gtk_widget_hide(w);
	      }
	} else
	  go_menu_append(skipstone->go_menu, location, skipstone);
	

     
   
     	if (skipstone->stop) {
     		gtk_widget_set_sensitive(skipstone->stop, FALSE);
	}
     	
	if (skipstone->reload) {
     		gtk_widget_set_sensitive(skipstone->reload, TRUE);
	}
     
	if (skipstone->timer) {
	     	gtk_timeout_remove(skipstone->timer);
	     	skipstone->timer = 0;
	}
     	gtk_progress_set_activity_mode(GTK_PROGRESS(skipstone->progressbar), FALSE);	
	gtk_progress_set_format_string(GTK_PROGRESS(skipstone->progressbar), ""); 
     	if (GTK_IS_PROGRESS(skipstone->progressbar)) gtk_progress_set_value(GTK_PROGRESS(skipstone->progressbar), 0);
	skipstone->is_loading = FALSE;
     	skipstone->prev_bytes=0;
     	skipstone->prev_seconds=0;
     	skipstone->prev_useconds=0;
   	message = g_strdup(_("Done ..."));
	gtk_statusbar_pop(GTK_STATUSBAR(skipstone->statusbar),
			  skipstone->sbar_status_msg_id);
	gtk_statusbar_push(GTK_STATUSBAR(skipstone->statusbar),
			   skipstone->sbar_status_msg_id,
			   message);
	g_free(message);
	


//     	if (location) g_free(location);
	D_RETURN;
}

gint on_mouse_click(WebKitWebView *embed, GdkEventButton *event , SkipStone *skipstone)
{
  	

   D_ENTER;   
   if (!skipstone->link) D_RETURN_(FALSE);
   
     	switch(event->button) {
	 case 2:
	   make_window(skipstone->link);
	   g_free(skipstone->link);
	   skipstone->link = NULL;
	   D_RETURN_(TRUE);
	 default:
	     break;
	
	}
   
     D_RETURN_(FALSE);
}

