/*************************************************************************************************************
This file contains ftp download functions
2012/6 by Dlxko
**************************************************************************************************************/
#include "FTPDownload.h"

//Dialog tmp
char dialog_tmp[400];

// End iter for file transfer text
static GtkTextIter saved_end_iter;

// For buddy number
static int saved_friend_buddy_number;

// Saved path stores
static char saved_file_name[FILE_NAME_SIZE];
static char saved_remote_addr[FILE_PATH_SIZE];

// Curl variables
static  CURL *curl;
static  CURLcode res;

// Threat variables
static GThread *thread;
static GError *error = NULL;

// File struct
struct FtpFile
{
	const char *filename;
	FILE *stream;
};

/***************************** WRITING FUNCTION *****************************/
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
	// Ouput file from file struc
	struct FtpFile *out = (struct FtpFile *)stream;

	// Open file for writing
	if(out && !out->stream)
	{
		out->stream=fopen(saved_file_name, "wb");
		// If error report
		if(!out->stream)
		{
			function_show_warning("Can't open file for download");
			return -1;
		}
	}
	return fwrite(buffer, size, nmemb, out->stream);
}

/************* THREAD FUNCTION *************/
static gpointer thread_func( gpointer data )
{
	// Struct for saved file
	struct FtpFile ftpfile =
	{
		saved_file_name,
		NULL
	};

	// Init curl
	curl_global_init(CURL_GLOBAL_DEFAULT);
	curl = curl_easy_init();

	// Check if initialized
	if(curl)
	{
		// Set remote adress
		curl_easy_setopt(curl, CURLOPT_URL, saved_remote_addr);
		// Define writing funcion
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
		// Set pointer to struc
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
		// Debug ouput
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		// Perform script
		res = curl_easy_perform(curl);
		// Cleanup
		curl_easy_cleanup(curl);

		// Check status
		if(CURLE_OK != res)
		{
			// Add mark of failed status
			gtk_text_buffer_insert_pixbuf(buddy[saved_friend_buddy_number].buffer, &saved_end_iter, px_failed);
		}
		else
		{
			// Add mark of sended status
			gtk_text_buffer_insert_pixbuf(buddy[saved_friend_buddy_number].buffer, &saved_end_iter, px_send);
		}
	}

	// Close local file
	if(ftpfile.stream)
		fclose(ftpfile.stream);

	// Global cleanup
	curl_global_cleanup();

	// Exit thread and return
	g_thread_exit( NULL );
	return ( NULL );
}

/*********************************************** FTP DOWNLOAD MAIN FUNCTION ***************************************************/
bool ftp_download_action(char function_remote_addr[FILE_PATH_SIZE], char function_file_name[FILE_NAME_SIZE], int function_buddy_number, GtkTextIter function_end_iter)
{
	// Check function datas
	if((strlen(function_remote_addr) < 1) || (strlen(function_file_name) < 1) || (function_buddy_number == (MAX_BUDDIES + 1)))
	{
		function_show_warning("Can't get data for sending function!");
		return false;
	}

	// Save end iter
	saved_end_iter = function_end_iter;

	// Store buddy number
	saved_friend_buddy_number = function_buddy_number;

	// Save remote adress
	strcpy_s(saved_remote_addr, FILE_PATH_SIZE, function_remote_addr);

	// Save filename
	strcpy_s(saved_file_name, FILE_NAME_SIZE, function_file_name);

	// Create thread with parameters else return error
	thread = g_thread_create_full( thread_func, NULL, 0, FALSE, TRUE,  G_THREAD_PRIORITY_LOW, &error );
	if( ! thread )
	{
		memset(dialog_tmp, 0, 400);
		sprintf(dialog_tmp, "Can't create thread: %s", error->message);
		function_show_warning(dialog_tmp);
		return false;
	}

	return true;
}