/* 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;


void
start_load(GtkMozEmbed *embed,Bdata *bdata) 
{

     	gtk_progress_set_activity_mode(GTK_PROGRESS(bdata->progressbar), 1);
     	bdata->timer = gtk_timeout_add (100, (GtkFunction)progress, bdata);
     	return;
}



void 
link_message(GtkMozEmbed *embed, Bdata *bdata)
{
	gchar *message;

	message = gtk_moz_embed_get_link_message(bdata->embed);
     	gtk_statusbar_push(GTK_STATUSBAR(bdata->statusbar), 1, message);
	if (bdata->link) {
		g_free(bdata->link);
		bdata->link=NULL;
	}

	bdata->link = g_strdup(message);

	if (bdata->menu_timer != NULL) {
		g_timer_destroy(bdata->menu_timer);
		gtk_timeout_remove(bdata->menu_timeout_id);
		bdata->menu_timer = NULL;
	} else {
		bdata->menu_time = 1.0;
		bdata->menu_timer = g_timer_new();
		g_timer_start(bdata->menu_timer);
		bdata->menu_timeout_id = gtk_timeout_add(50, (GSourceFunc)menu_timeout, (gpointer) bdata);
	}

	return;
}


void 
title_changed(GtkMozEmbed *embed, Bdata *bdata)
{
	gchar *title;

	title = gtk_moz_embed_get_title(bdata->embed);
     	gtk_window_set_title(GTK_WINDOW(bdata->window), title);
     	g_free(title);
     	return;
}


void
url_clicked(GtkMozEmbed *embed, Bdata *bdata)
{
	gchar *location;

	location = gtk_moz_embed_get_location(bdata->embed);
     	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(bdata->combo)->entry),location);
     	g_free(location);
     	return;
}



void 
stop_load(GtkMozEmbed *embed,Bdata *bdata)
{

     	gtk_progress_set_value(GTK_PROGRESS(bdata->progressbar), 0);
     	gtk_progress_set_activity_mode(GTK_PROGRESS(bdata->progressbar), 0);
     	gtk_timeout_remove(bdata->timer);
     	return;
}


gint
handle_open_uri(GtkMozEmbed *embed,const gchar *uri,Bdata *bdata)
{
       	gboolean status;
     	status = open_uri (uri);
     	return status;
}


void
  moz_new_window_cb(GtkMozEmbed *embed, GtkMozEmbed **retval, guint chromemask)
{
	Bdata *bdata;
     	bdata = make_browser ();
     	*retval = GTK_MOZ_EMBED (bdata->embed);
     	return;
}


void net_state_cb (GtkMozEmbed *embed, gint state, guint status, Bdata *bdata)
{
	gchar *message = NULL;
	if (state & GTK_MOZ_EMBED_FLAG_IS_REQUEST) {
		if (state & GTK_MOZ_EMBED_FLAG_REDIRECTING)
			message = g_strdup("Redirecting ...");
		else if (state & GTK_MOZ_EMBED_FLAG_TRANSFERRING)
			message = g_strdup("Transfering ...");
		else if (state & GTK_MOZ_EMBED_FLAG_NEGOTIATING)
		        message = g_strdup("Authorizing ...");
		else if (state & GTK_MOZ_EMBED_FLAG_START)
			message = g_strdup("Loading ...");
		else if (state & GTK_MOZ_EMBED_FLAG_STOP)
			 message = g_strdup("Done ...");
			 
	}

	if (status == GTK_MOZ_EMBED_STATUS_FAILED_DNS)
		 message = g_strdup("Could not find site ...");
	else if (status == GTK_MOZ_EMBED_STATUS_FAILED_CONNECT)
       		message = g_strdup("Could not connect to site ...");
	else if (status == GTK_MOZ_EMBED_STATUS_FAILED_TIMEOUT)
	      	 message = g_strdup("Connection timeout ...");
	else if (status == GTK_MOZ_EMBED_STATUS_FAILED_USERCANCELED)
		message = g_strdup("Transfer interrupted ...");
     	
     	gtk_statusbar_push(GTK_STATUSBAR(bdata->statusbar), 1, message);
     	g_free(message);
	return;
}
