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

// Function to send loggouting packet
void additional_function_logoff_send_packet()
{
	// Count sending text lenght
	sending_text_len = LENGHT_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);

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

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

// Last program function
void function_exit_client(int code)
{
	closesocket(s);
	WSACleanup();
	gtk_main_quit();
	exit(code);
}

// Signal when closing app
void signal_go_offline()
{
	additional_function_logoff_send_packet();
}

// Signal when closing conversation
void signal_close_conversation()
{
	gtk_widget_hide(conversation_window);
	conversation_shown = false;
}

// Signal when closing chat
void signal_close_chat(GtkWidget *unused_widget, int function_buddy_number)
{
	// Delete oldies
	additional_function_chat_single_bodies_delete_oldies(function_buddy_number);

	// Remove page and decrease number of pages
	gtk_notebook_remove_page(GTK_NOTEBOOK(conversation_notebook), gtk_notebook_get_current_page(GTK_NOTEBOOK(conversation_notebook)));
	buddy[function_buddy_number].apended = false;
	--conversation_tabs;

	// If no pages hide window
	if (conversation_tabs == 0)
		gtk_widget_hide(conversation_window);
}

// Signal when closing requests
void signal_close_requests()
{
	gtk_widget_hide(requests_window);
}