/* logjam - a GTK client for LiveJournal.
 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
 *
 * vim: tabstop=4 shiftwidth=4 noexpandtab :
 */

#include "gtk-all.h"
#include <gdk/gdkkeysyms.h>
#include <gdk/gdktypes.h>
#include <stdio.h>
#include "util.h"
#include "conf.h"
#include "jamdoc.h"

static void
select_toggled(GtkToggleButton *tb, GtkWidget *focus) {
	if (gtk_toggle_button_get_active(tb))
		gtk_widget_grab_focus(focus);
}

static void
entry_focus_cb(GtkWidget *w, GdkEventFocus *e, GtkToggleButton *radio) {
	gtk_toggle_button_set_active(radio, TRUE);
}

static GtkWidget*
radio_option(GSList *g, GtkWidget **radio, GtkWidget **entry,
		const char *caption, const char *label, const char *initial) {
	GtkWidget *vbox;

	vbox = gtk_vbox_new(FALSE, 0);
	*radio = gtk_radio_button_new_with_mnemonic(g, caption);
	gtk_box_pack_start(GTK_BOX(vbox), *radio, FALSE, FALSE, 0);

	*entry = gtk_entry_new();
	gtk_entry_set_activates_default(GTK_ENTRY(*entry), TRUE);
	g_signal_connect(G_OBJECT(*entry), "focus-in-event",
			G_CALLBACK(entry_focus_cb), *radio);
	if (initial)
		gtk_entry_set_text(GTK_ENTRY(*entry), initial);
	gtk_widget_set_size_request(*entry, 75, -1);
	g_signal_connect(G_OBJECT(*radio), "toggled",
			G_CALLBACK(select_toggled), *entry);
	g_signal_connect(G_OBJECT(*radio), "activate",
			G_CALLBACK(select_toggled), *entry);

	if (label) {
		GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
		gtk_box_pack_start(GTK_BOX(hbox), 
				gtk_label_new(label),
				FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(hbox), *entry, TRUE, TRUE, 0);
		gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
	} else {
		gtk_box_pack_start(GTK_BOX(vbox), *entry, TRUE, TRUE, 0);
	}

	return vbox;
}

#if !GLIB_CHECK_VERSION(2,2,0)
static gboolean
g_str_has_prefix(const gchar *str, const gchar *prefix) {
	return (strncmp(str, prefix, strlen(prefix)) == 0);
}
#endif
void
link_dialog_run(GtkWindow *win, JamDoc *doc) {
	GtkWidget *dlg;
	GtkWidget *vbox;

	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	char *input = "";
	gboolean url_input = FALSE; /* is the selection a URL? */

	GtkWidget *etext, *ruser, *euser, *rurl, *eurl;
	GtkWidget *subbox;
	GSList *rgroup;
	char *url, *text, *user, *link = NULL;

	JamAccount *acc = jam_doc_get_account(doc);

	buffer = jam_doc_get_text_buffer(doc);
	if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
		input = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
		url_input = g_str_has_prefix(input, "http://");
	}

	dlg = gtk_dialog_new_with_buttons(_("Make Link"),
			win, GTK_DIALOG_MODAL,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_OK, GTK_RESPONSE_OK,
			NULL);
	gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);

	vbox = gtk_vbox_new(FALSE, 10);

	etext = gtk_entry_new();
	gtk_entry_set_activates_default(GTK_ENTRY(etext), TRUE);
	gtk_entry_set_text(GTK_ENTRY(etext), url_input ? "" : input);
	subbox = labelled_box_new(_("Link _Text:"), etext);
	gtk_box_pack_start(GTK_BOX(vbox), subbox, FALSE, FALSE, 0);

	gtk_box_pack_start(GTK_BOX(vbox), 
			radio_option(NULL, &rurl, &eurl, _("_URL:"), NULL,
				url_input ? input : ""),
			FALSE, FALSE, 0);

	rgroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rurl));

	if (JAM_ACCOUNT_IS_LJ(acc)) {
		gtk_box_pack_start(GTK_BOX(vbox), 
				radio_option(rgroup, &ruser, &euser, 
					_("_LiveJournal User:"), ".../users/",
					url_input ? "" : input),
				FALSE, FALSE, 0);
	}

	jam_dialog_set_contents(GTK_DIALOG(dlg), vbox);
	if (gtk_dialog_run(GTK_DIALOG(dlg)) != GTK_RESPONSE_OK) {
		gtk_widget_destroy(dlg);
		return;
	}

	gtk_text_buffer_delete(buffer, &start, &end);

	url  = gtk_editable_get_chars(GTK_EDITABLE(eurl),  0, -1);
	xml_escape(&url);
	user = gtk_editable_get_chars(GTK_EDITABLE(euser), 0, -1);
	xml_escape(&user);
	text = gtk_editable_get_chars(GTK_EDITABLE(etext), 0, -1);
	xml_escape(&text);

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rurl))) {
		/* build a "url" link. */
		link = g_strdup_printf("<a href=\"%s\">%s</a>", url, text);
	} else if (ruser && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ruser))) {
		/* build a "friends" link. */
		g_free(url);
		url = g_strdup(jam_account_lj_get_server(JAM_ACCOUNT_LJ(acc))->url);
		xml_escape(&url);
		link = g_strdup_printf("<a href=\"%s/users/%s\">%s</a>",
				url, user, text);
	} 
	if (link)
		gtk_text_buffer_insert(buffer, &start, link, -1);
	g_free(url);
	g_free(user);
	g_free(text);
	g_free(link);
	
	gtk_widget_destroy(dlg);
}

static GtkWidget*
add_menu_item(GtkMenuShell *ms, const gchar *id, const gchar *text) {
	GtkWidget *hbox;
	GtkWidget *item;

	hbox = gtk_hbox_new(FALSE, 3);
	gtk_box_pack_start(GTK_BOX(hbox), 
			gtk_image_new_from_stock(id, GTK_ICON_SIZE_MENU), 
			FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), 
			gtk_label_new(text), FALSE, FALSE, 0);

	item = gtk_menu_item_new();
	gtk_container_add(GTK_CONTAINER(item), hbox);
	gtk_menu_shell_append(ms, item);
	return item;
}

static GtkWidget*
make_usertype_omenu() {
	GtkWidget *omenu, *menu;

	menu = gtk_menu_new();
	add_menu_item(GTK_MENU_SHELL(menu), "logjam-ljuser", _("User"));
	add_menu_item(GTK_MENU_SHELL(menu), "logjam-ljcomm", _("Community"));

	omenu = gtk_option_menu_new();
	gtk_option_menu_set_menu(GTK_OPTION_MENU(omenu), menu);
	return omenu;
}

void
link_jam_dialog_run(GtkWindow *win, JamDoc *doc) {
	GtkWidget *dlg;
	GtkWidget *vbox, *hbox, *entry, *omenu;
	GtkSizeGroup *sizegroup;
	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	char *username = NULL;
	int usertype;
	gboolean selection = FALSE;

	buffer = jam_doc_get_text_buffer(doc);
	if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
		username = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
		selection = TRUE;
	}

	dlg = gtk_dialog_new_with_buttons(_("Insert lj user / lj community Tag"),
			win, GTK_DIALOG_MODAL,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_OK, GTK_RESPONSE_OK,
			NULL);
	gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
	vbox = gtk_vbox_new(FALSE, 5);
	gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);

	sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);

	entry = gtk_entry_new();
	gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
	if (username) {
		gtk_entry_set_text(GTK_ENTRY(entry), username);
		g_free(username);
	}
	hbox = labelled_box_new_sg(_("_Username:"), entry, sizegroup);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	omenu = make_usertype_omenu();
	hbox = labelled_box_new_sg(_("User _Type:"), omenu, sizegroup);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	jam_dialog_set_contents(GTK_DIALOG(dlg), vbox);

	if (gtk_dialog_run(GTK_DIALOG(dlg)) != GTK_RESPONSE_OK) {
		gtk_widget_destroy(dlg);
		return;
	}
	username = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
	usertype = gtk_option_menu_get_history(GTK_OPTION_MENU(omenu));
	gtk_widget_destroy(dlg);
	if (username[0] == 0) {
		g_free(username);
		return;
	}

	if (selection)
		gtk_text_buffer_delete(buffer, &start, &end);
	else
		gtk_text_buffer_get_iter_at_mark(buffer, &start,
				gtk_text_buffer_get_insert(buffer));

	gtk_text_buffer_insert(buffer, &start, "<lj ", -1);
	if (usertype == 0)
		gtk_text_buffer_insert(buffer, &start, "user=\"", -1);
	else
		gtk_text_buffer_insert(buffer, &start, "comm=\"", -1);
	xml_escape(&username);
	gtk_text_buffer_insert(buffer, &start, username, -1);
	g_free(username);
	gtk_text_buffer_insert(buffer, &start, "\" />", -1);
}

