#include "ecdb.h"

static void free_file(Ewl_Filelist_File *file);

void
ecdb_filelist_dnd_dropped_cb(Ewl_Widget *w, void *ev, void *data)
{
	int i;
	Ewl_Event_Dnd_Data_Received *dnd = ev;
	char *path, **files = dnd->data;
	Ecdb_Source *parent, *child;

	/* Get the parent, find the path of the file(s) dropped
	 * and add them as children to parent */
	parent = ewl_widget_data_get(w, "source");

	/* Only handle uris for now */
	if (strcmp(dnd->type, "text/uri-list"))
		return;

	for (i = 0; i < dnd->len; i++)
	{
		printf("file: %s\n", files[i]);

		/* Cheap and dirty hax */
		/* XXX FIXME XXX */
		path = strdup(&files[i][7]);
		child = ecdb_source_new();
		ecdb_source_data_set(child, path);
		ecdb_source_child_append(parent, child);
	}

	ecdb_filelist_directory_set(EWL_FILELIST(w), parent);
}

void
ecdb_filelist_directory_set(Ewl_Filelist *fl, Ecdb_Source *src)
{
	if (src)
	{
		Ewl_Filelist_Directory *data;
		Ewl_Event_Action_Response ev_data;

		data = ewl_mvc_data_get(EWL_MVC(fl->controller));
		if (data) ewl_filelist_model_data_unref(data);

		data = ecdb_filelist_directory_new(src);
		ewl_mvc_data_set(EWL_MVC(fl->controller), data);
		ewl_mvc_dirty_set(EWL_MVC(fl->controller), TRUE);
		ev_data.response = EWL_FILELIST_EVENT_DIR_CHANGE;
		ewl_callback_call_with_event_data(EWL_WIDGET(fl),
				EWL_CALLBACK_VALUE_CHANGED, &ev_data);
		
		/* Set the source as needed for file operations */
		ewl_widget_data_set(EWL_WIDGET(fl), "source", src);
	}
}

Ewl_Filelist_Directory *
ecdb_filelist_directory_new(Ecdb_Source *parent)
{
	Ecdb_Source *src;
	Ewl_Filelist_Directory *dir;
	Ewl_Filelist_File *file;

	struct stat st;
	int nf = 0, nd = 0, i = 0;
	Ecore_List *files, *dirs;

	files = ecore_list_new();
	dirs = ecore_list_new();
	ecore_list_free_cb_set(files, ECORE_FREE_CB(free_file));
	ecore_list_free_cb_set(dirs, ECORE_FREE_CB(free_file));

	if (!parent)
		return NULL;

	/* No up for now */

	while ((src = parent->children[i]))
	{
		if (!ecore_file_exists(src->dst))
		{
			printf("File doesn't exist!\n");
			break;
		}
		file = calloc(1, sizeof(Ewl_Filelist_File));
		file->name = ecore_string_instance(ecore_file_file_get
				(src->dst));

		stat(src->dst, &st);
		file->size = st.st_size;
		file->modtime = st.st_mtime;
		file->mode = st.st_mode;
		file->groupname = st.st_gid;
		file->username = st.st_uid;
		file->is_dir = src->dir;
		file->readable = ecore_file_can_read(src->dst);
		file->writeable = ecore_file_can_write(src->dst);

		if (src->dir)
		{
			ecore_list_append(dirs, file);
			nd++;
		}

		else
		{
			ecore_list_append(files, file);
			nf++;
		}

		/* Oops */
		i++;
	}

	dir = calloc(1, sizeof(Ewl_Filelist_Directory));

	dir->rfiles = ecore_list_new();
	dir->rdirs = ecore_list_new();

	/* Also dirty hax, need filtering instead of this */
	//dir->files = ecore_list_new();
	//dir->dirs = ecore_list_new();
	dir->name = ecore_string_instance("bunny");

	dir->files = files;
	dir->dirs = dirs;
	dir->filter = NULL;
	dir->num_dirs = nd;
	dir->num_files = nf;

	return dir;
}

static void free_file(Ewl_Filelist_File *file)
{
        ecore_string_release(file->name);
        FREE(file);
}
