/*************************************************************************************************************
* This file contains buddy compare row function and signal when activated
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "BuddyList.h"

//For signal when clicked
static GtkTreeModel *model;
static GtkTreeIter iter;
static gint buddy_number;
static gint return_value;

// My own shorting function which is used by three model
gint function_compare_rows (GtkTreeModel *input_treemodel, GtkTreeIter  *input_first_iter, GtkTreeIter  *input_second_iter, gpointer userdata)
{
	// Refresh variables
	return_value = 0;
	gchar *first_name, *second_name;

	// Get the names for future use
	gtk_tree_model_get(input_treemodel, input_first_iter, STATUS_H_COLUM, &first_name, -1);
	gtk_tree_model_get(input_treemodel, input_second_iter, STATUS_H_COLUM, &second_name, -1);

	// Process empty or collate 
	if (first_name == NULL || second_name == NULL)
	{
		if (first_name == NULL && second_name == NULL)
			return_value = 0;
		else
			return_value = (first_name == NULL) ? -1 : 1;
	}
	else return_value = g_utf8_collate(first_name,second_name);

	// Free variables
	g_free(first_name);
	g_free(second_name);
	return return_value;
}

// Function after clicking to some buddy in buddy list (double click)
void signal_list_row_activated(GtkTreeView *input_treeview,  GtkTreePath *input_tree_path, GtkTreeViewColumn *input_colum, gpointer userdata)
{
	// Create buffer
	gchar *clicked_name;

	// Get model or report with warning
	model = gtk_tree_view_get_model(input_treeview);

	// Get iter or show warning
	if (gtk_tree_model_get_iter(model, &iter, input_tree_path))
	{
		// Get datas from colum
		gtk_tree_model_get(model, &iter, NICK_COLUM, &clicked_name, -1);

		// Get current buddy
		buddy_number = function_get_buddy_number(clicked_name);
		if (buddy_number == MAX_BUDDIES + 1)
			function_show_warning(LANG_XBUDDY_XCHAT);

		// If already shown (hidden) just show conversation window else create new chat
		if((buddy[buddy_number].singlebody_conversation_shown || buddy[buddy_number].singlebody_history_shown || buddy[buddy_number].singlebody_history_view_shown || buddy[buddy_number].singlebody_info_shown) && !conversation_shown)
			gtk_widget_show_all(conversation_window);
		else
			singlebody_chat_conversation_info_shown(NULL, buddy_number, 3);

		// Present the window and show the notebook page
		gtk_window_present(GTK_WINDOW(conversation_window));
		gtk_notebook_set_current_page(GTK_NOTEBOOK(conversation_notebook), buddy[buddy_number].notebook_position);

		// Free memory
		g_free(clicked_name);
	}
	else function_show_warning(LANG_XITER_BUDDYLIST);
}
