/*************************************************************************************************************
* This file contains creating and functionality of history
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Adder.h"

// Widgets
static GtkWidget *new_friend_vbox;
static GtkWidget *new_friend_username;

// Signal after clicked add button
static void signal_new_friend()
{
	// Get name from text entry
	entry_text = gtk_entry_get_text (GTK_ENTRY (new_friend_username));

	// If correct username continue
	if(strlen(gtk_entry_get_text (GTK_ENTRY (new_friend_username))) > 0)
	{
		// Count lenght
		sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE;
		sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

		// Update text for server with "DX_OPCODE_NFR" and name datas
		memset(sending_text,0,BUF_SIZE);
		memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
		memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_NFR, OPCODE_SIZE);
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entry_text, strlen(entry_text));

		// Send socket
		function_send_data(sending_text, sending_text_len);
	}
	else function_show_warning(LANG_ADDER_NOTHING);
}

// Function to create buddy adder window
void signal_create_buddy_adder()
{
	// Look if already exist
	if(gtk_widget_get_visible(new_friend_window))
		return;

	// New window, parameters, title, closing
	new_friend_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (new_friend_window), 280, 50);
	gtk_window_set_title (GTK_WINDOW (new_friend_window), LANG_ADD_OR_REM_FR);
	gtk_window_set_icon(GTK_WINDOW(new_friend_window), default_icon);

	// Create new box and container and show
	new_friend_vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (new_friend_window), new_friend_vbox);

	// Create text entry widget for username, lenght, set text, position, editable, pack, show
	new_friend_username = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (new_friend_username), 30);
	gtk_entry_set_text (GTK_ENTRY (new_friend_username), LANG_USERNAME);
	g_signal_connect (new_friend_username, "activate", G_CALLBACK (signal_new_friend), new_friend_username);
	gtk_editable_select_region (GTK_EDITABLE (new_friend_username), 0, GTK_ENTRY (new_friend_username)->text_length);
	gtk_box_pack_start (GTK_BOX (new_friend_vbox), new_friend_username, TRUE, TRUE, 0);

	// Create label, wrapping, pack, show
	new_friend_label = gtk_label_new (LANG_ENTER_NAME_TO_ADDER);
	gtk_label_set_justify (GTK_LABEL (new_friend_label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (new_friend_label), TRUE);
	gtk_box_pack_start (GTK_BOX (new_friend_vbox), new_friend_label, FALSE, TRUE, 5);

	// Show whole window
	gtk_widget_show_all(new_friend_window);
}