/*************************************************************************************************************
* This file contains close scripts
* 2012/6 by Dlxko
**************************************************************************************************************/
#include "Logoff.h"

// Local static variables
static char sending_text[BUF_SIZE]; // For socket text
static int sending_text_len; // Socket text len
static char sending_text_len_char[LENGHT_SIZE+1];

/******** Exit client *****/
void function_exit_client(int code)
{
	// Close connection and exit
	WSACleanup();
	gtk_main_quit();
	exit(code);
}

/******** When closed program *****/
void signal_go_offline()
{
	// Count sending text lenght
	sending_text_len = LENGHT_SIZE + CODE_SIZE + OPCODE_SIZE;
	memset(sending_text_len_char, 0, LENGHT_SIZE);
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_MSG" and friend datas
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text,sending_text_len_char,LENGHT_SIZE);
	memcpy(&sending_text[LENGHT_SIZE],DX_OPCODE_OFL,OPCODE_SIZE);
	memcpy(&sending_text[OPCODE_SIZE+LENGHT_SIZE], DX_CODE_OFF, CODE_SIZE);

	// Send packet and Erase GTK text entry
	function_send_data(sending_text, sending_text_len);

	// Close connection and exit
	function_exit_client(0);
}

/******** When closed chat ******/
void signal_close_chat(GtkWidget *chat)
{
	if (chat)
		gtk_widget_hide(chat);
	else
		function_show_warning("Can't close non-existing chat widget");
}

/******** When closed chat ******/
void signal_close_requests()
{
	if (requests_window)
		gtk_widget_hide(requests_window);
	else
		function_show_warning("Can't close non-existing requests widget");
}