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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <gtkmozembed.h>


/* version information */
#define SKIPSTONE_MAJOR 0
#define SKIPSTONE_MINOR 3
#define SKIPSTONE_MICRO 0
/* ------------------- */

typedef struct 		/* Browser window structure */
{
	GtkWidget *window;
	GtkWidget *handlebox;
	GtkWidget *toolbar;
	GtkWidget *combo;
	GtkWidget *statusbar;
	GtkWidget *progressbar;

	GtkWidget *bookmark_menubar_item;
	GtkWidget *bookmark_menu;
	GtkWidget *bookmark_toolbar;
	GtkWidget *bmarkhandlebox;
     	GSList *bookmark_toolbar_widgets;
	GtkMozEmbed *embed;

	gint timer;
	GTimer *menu_timer;
	gint menu_timeout_id;
	gfloat menu_time;
	gchar *link;
}
Bdata;

typedef struct		/* Dialog window struct */
{
	GtkWidget *dialog;
	GtkWidget *entry;
}
DialogData;

typedef struct 		/* Config window struct */
{
	GtkWidget *window;
	GtkWidget *home_entry;
	GtkWidget *email_entry;
	GtkWidget *downloader_entry;
     	GtkObject *xsize_sbutton_adj;
	GtkWidget *xsize_sbutton;
	GtkObject *ysize_sbutton_adj;
	GtkWidget *ysize_sbutton;
	GtkWidget *ok_button;
	GtkWidget *cancel_button;
	GtkWidget *maxpopups;       
	GtkObject *maxpopups_adj;
}
Cdata;

typedef struct 		/* configuration data struct */
{
	gchar *home;
	gchar *mailer;
     	gchar *downloader;
     	gint xsize;
	gint ysize;
	gint layout;
	gint maxpopupitems;
	gint bmarktbar;
}
ConfigData;

typedef struct {
	gchar *label;
	gchar *url;
	gint pos; /* 0 - menu; 1 - toolbar */
}
BookmarkData;

typedef struct {
	GtkWidget *window;
	GtkWidget *clist;
	GtkWidget *text_entry;
	GtkWidget *url_entry;
	GtkWidget *menu_radio_button;
	GtkWidget *toolbar_radio_button;
}
BookmarkWindow;

/* callbacks */

void blank_new_window(GtkWidget *menuitem,gpointer data);

void open_in_new_window(GtkWidget *menuitem,gpointer data);

void close_window(GtkWidget *menuitem,Bdata *bdata);

void exit_app(GtkWidget *menuitem,gpointer data);

void open_config_dialog(GtkWidget *menuitem,gpointer data);

void change_layout(GtkWidget *menuitem,gpointer data);

void destroy_window_callback(GtkWidget *window,Bdata *bdata);

void on_entry(GtkWidget *entry,Bdata *bdata);

void on_list_select(GtkWidget *combo,Bdata *bdata);

void destroy_config_window(GtkWidget *window,Cdata *config_window);

void close_config_window(GtkWidget *button,Cdata *config_window);

void save_config(GtkWidget *button,Cdata *config_window);

void destroy_dialog(GtkWidget *dialog,DialogData *ddata);

void on_url_entry_ok(GtkWidget *button,DialogData *ddate);

void on_back(GtkWidget *button,Bdata *bdata);

void on_forward(GtkWidget *button,Bdata *bdata);

void on_reload(GtkWidget *button,Bdata *bdata);

void on_stop(GtkWidget *button,Bdata *bdata);

void go_home(GtkWidget *button,Bdata *bdata);

void create_dialog(gchar *title, gchar *labeltext, gint logo); /* generic dialog */

void close_generic_dialog(GtkWidget *button, GtkWidget *dialog);

void about_skipstone(GtkWidget *menuitem);

void bookmark_activate(GtkWidget *widget,Bdata *bdata);

void add_bookmark(GtkWidget *menuitem,Bdata *bdata);

void edit_bookmarks(GtkWidget *menuitem,gpointer user_data);

void on_bookmark_clist_select_row(GtkWidget *clist,gint row,gint col,GdkEventButton *event,BookmarkWindow *bwin);

void on_bookmark_clist_unselect_row(GtkWidget *clist,gint row,gint col,GdkEventButton *event,BookmarkWindow *bwin);

void on_bookmark_menu_radio_button_toggled(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_toolbar_radio_button_toggled(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_add_button_clicked(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_update_button_clicked(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_remove_button_clicked(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_cancel_button_clicked(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_ok_button_clicked(GtkWidget *button,BookmarkWindow *bwin);

void on_bookmark_delete_event(GtkWidget *widget,GdkEvent *event,BookmarkWindow *bwin);

void close_bookmark_window(BookmarkWindow *bwin);

void hide_bookmark_toolbar(GtkCheckMenuItem *cb_menuitem, Bdata *bdata);
/* moz_callbacks */

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

void link_message(GtkMozEmbed *embed,Bdata *bdata);

void title_changed(GtkMozEmbed *embed,Bdata *bdata);

void url_clicked(GtkMozEmbed *embed,Bdata *bdata);

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

gint handle_open_uri(GtkMozEmbed *embed,const gchar *uri,Bdata *bdata);

void moz_new_window_cb(GtkMozEmbed *embed, GtkMozEmbed **retval,guint chromemask);

void net_state_cb(GtkMozEmbed *embed, gint state, guint status, Bdata *bdata);

/* support */

gboolean open_uri(const gchar *uri);

gboolean process_uri(const gchar *uri);

gboolean  download_url(const gchar *uri);

void make_menu(Bdata *bdata,GtkWidget *menubar,GtkAccelGroup *accelgroup);

gint progress(Bdata *bdata);

void make_window(const gchar *url);

Bdata *make_browser(void);

void read_config(void);

void write_config(void);

void read_urls(void);

void write_urls(void);

void update_layout(void);

void insert_in_combo(gchar *url, Bdata *bdata);

gint menu_timeout(Bdata *bdata);

void quit_skipstone(void);

void make_window_from_popup(GtkWidget *menuitem, Bdata *bdata);

GtkWidget * get_popup_menu(Bdata *bdata);

void destroy_popup(void);

void browse_in_window_from_popup(GtkWidget *menuitem, Bdata *bdata);

void download_link_from_popup(GtkWidget *menuitem, Bdata *bdata);

void read_bookmarks(void);

void write_bookmarks(void);

void generate_bookmark_ui(Bdata *bdata);

/* signals */

void handle_usr1_signal(int sig);

void check_running(gchar *url);

void set_running(void);
