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

void additional_function_logoff_send_packet(int function_type)
{
	// 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);
	if(function_type == 1)
		memcpy(&sending_text[LENGHT_SIZE],DX_OPCODE_OFL,OPCODE_SIZE);
	else if(function_type == 2)
		memcpy(&sending_text[LENGHT_SIZE],DX_OPCODE_LOF,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)
{
	// Close connection and exit
	closesocket(s);
	WSACleanup();
	gtk_main_quit();
	exit(code);
}

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

// Signal when closing app from loggining
void signal_loggin_go_offline()
{
	additional_function_logoff_send_packet(2);
}

// Signal when closing chat
void signal_close_chat(GtkWidget *chat)
{
	gtk_widget_hide(chat);
}

// Signal when closing history window
void signal_close_history(GtkWidget *history)
{
	gtk_widget_hide(history);
}

// When closing history view window
void signal_close_history_view(GtkWidget *history_view)
{
	gtk_widget_hide(history_view);
}

// Signal when closing info window
void signal_close_info(GtkWidget *info)
{
	gtk_widget_hide(info);
}

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