/*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
                       Matthias Münch (matthias@amigaworld.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    In addition, as a special exception, Tom Parker and Matthias Münch give
    permission to link the code of this program with a TCP stack of your 
    choice, any official MUI libraries or classes and any custom MUI classes
    that should be necessary for the operation of this program.  This 
    exception also gives you permission to distribute linked combinations 
    including this software with any of the before-mentioned libraries and 
    classes.  You must obey the GNU General Public License in all respects for
    all of the code used other than that provided by the before-mentioned 
    libraries and classes.  As part of this exception you are obliged to 
    follow the license terms of the before-mentioned libraries, this license
    does not compel you to follow those terms, but if you do not then you may
    not link with those libraries.  If you modify this file, you may extend
    this exception to your version of the file, but you are not obligated to
    do so.  If you do not wish to do so, delete this exception statement from
    your version.
*/
/*
** file transfer window
*/

#include "common.h"

#include <MUI/NListview_mcc.h>
#include <libraries/asl.h>
#include <proto/dos.h>

static Object *xf_list = NULL;
static Object *xf_accbut, *xf_rejbut, *xf_abobut, *xf_resbut, *xf_clebut;

list *xfers = NULL;

static ULONG xfer_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void xfer_op(int op);
static MUI_LIST_DISP_DECL(xfer_disp, struct xferdata *xf);
static MUI_LIST_COMP_DECL(xfer_comp, struct xferdata *xf1, struct xferdata *xf2);
static ULONG xfer_req_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void xfer_req_send(struct xferreqdata *data);


MUI_DISPATCH(xfer_dispatch)
{
	switch(msg->MethodID)
	{
	case OM_NEW:
		return xfer_new(cl,obj,(APTR)msg);

	case TRANSFER_OP:
		xfer_op((int)MARG1);
		return 0;

	}
	return DoSuperMethodA(cl, obj, msg);
}


static ULONG xfer_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static struct Hook dispHook = { {0,0}, &xfer_disp, NULL, NULL };
	static struct Hook compHook = { {0,0}, &xfer_comp, NULL, NULL };

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_ID, MAKE_ID('X', 'F', 'E', 'R'),
		MUIA_Window_Title, "File Transfers",
		WindowContents, VGroup,
			Child, NListviewObject,
				MUIA_NListview_NList, xf_list = NListObject,
					InputListFrame,
					MUIA_Font, MUIV_Font_Tiny,
					MUIA_NList_Title, TRUE,
					MUIA_NList_Format, "BAR, BAR, BAR,",
					MUIA_NList_DisplayHook, &dispHook,
					MUIA_NList_CompareHook, &compHook,
					MUIA_CycleChain, 1,
				End,
			End,
			Child, HGroup,
				Child, xf_accbut = mui_button("Accept"),
				Child, xf_rejbut = mui_button("Reject"),
				Child, xf_abobut = mui_button("Abort"),
				Child, xf_resbut = mui_button("Resume"),
				Child, xf_clebut = mui_button("Cleanup"),
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);


	DoMethod(xf_accbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 0);
	DoMethod(xf_rejbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 1);
	DoMethod(xf_abobut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 2);
	DoMethod(xf_resbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 3);
	DoMethod(xf_clebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 4);

	DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);

	return (ULONG)obj;
}


MUI_LIST_DISP_STATIC(xfer_disp, struct xferdata *xf)
{
	static char buf[50];
	char *t = "";

	if(xf)
	{
		switch(xf->type)
		{
		case XFER_SEND: t = "Send"; break;
		case XFER_RECEIVE: t = "Receive"; break;
		}
		*array++ = t;
		switch(xf->state)
		{
		case XFER_OFFERING:    t = "Offering";    break;
		case XFER_OFFERED:     t = "Offered";     break; 
		case XFER_CONNECTED:   t = "Connected";   break;
		case XFER_CONNECTING:  t = "Connecting";  break;
		case XFER_REQUESTING:  t = "Requesting";  break;
		case XFER_SENDING:     t = "Sending";     break;
		case XFER_RECEIVING:   t = "Receiving";   break;
		case XFER_FINISHED:    t = "Finished";    break;
		case XFER_REJECTED:    t = "Rejected";    break;
		case XFER_TIMEOUT:     t = "Timeout";     break;
		case XFER_ERROR:       t = "Error";       break;
		default: t = "???";
		}
		*array++ = t;
		*array++ = xf->name;
		if(xf->size)
			sprintf(buf, "\33r%ld / %ld (%d%%)", xf->cursize, xf->size, (xf->cursize * 100) / xf->size);
		else
			buf[0] = '\0';
		*array = buf;
	}
	else
	{
		*array++ = "Operation";
		*array++ = "State";
		*array++ = "File";
		*array = "Size";
	}

	return 0;
}


MUI_LIST_COMP_STATIC(xfer_comp, struct xferdata *xf1, struct xferdata *xf2)
{
	if(xf1->state == XFER_SEND && xf2->state != XFER_SEND) return(1);
	return(-1);
}


static void xfer_op(int op)
{
	struct xferdata *xf;

	DoMethod(xf_list, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &xf);
	if(!xf) return;

	switch(op)
	{
	case 0:
		if(xf->type != XFER_RECEIVE || xf->state != XFER_OFFERED) return;
		xf->state = XFER_CONNECTING;
		xfer_refresh(xf);
		open_url(xf->name);
		break;

	}
}


void xfer_packet(ikspak *pak)
{
	struct xferdata *xf;

	if(pak->subtype != IKS_TYPE_SET) return;

	xf = malloc(sizeof(struct xferdata));
	memset(xf, 0, sizeof(struct xferdata));
	xf->type = XFER_RECEIVE;
	xf->state = XFER_OFFERED;
	xf->name = iks_find_cdata(iks_find(pak->x, "query"), "url");
	xf->pak = pak;

	list_append(&xfers, xf);
	DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);

	set(gui.xferwin, MUIA_Window_Open, TRUE);
}


int xfer_test(void)
{
	struct xferdata *xf;

	xf = malloc(sizeof(struct xferdata));
	memset(xf, 0, sizeof(struct xferdata));
	xf->file = "sys:Veriler/gfx/ehore.gif";
	xf->name = FilePart(xf->file);
	xf->type = XFER_SEND;
	xf->state = XFER_OFFERING;
	list_append(&xfers, xf);
	DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);


http_up();
	/* fix? */
}


void xfer_share(juser u)
{
	Object *win;

	win = NewObject(gui.xfer_req_mcc->mcc_Class, NULL, TRANSFER_TO, u, TAG_DONE);
	if(win)
	{
		DoMethod(gui.app, OM_ADDMEMBER, win);
		set(win, MUIA_Window_Open, TRUE);
	}
}


void xfer_refresh(struct xferdata *xf)
{
	long pos = MUIV_NList_GetPos_Start;

/*	DoMethod(xf_list, MUIM_NList_Sort); */
	if(xf)
	{
		DoMethod(xf_list, MUIM_NList_GetPos, xf, &pos);
		if(pos != MUIV_NList_GetPos_End)
			DoMethod(xf_list, MUIM_NList_Redraw, pos);
	}
}


MUI_DISPATCH(xfer_req_dispatch)
{
	switch(msg->MethodID)
	{
	case OM_NEW:
		return xfer_req_new(cl,obj,(APTR)msg);

	case TRANSFER_SEND:
		xfer_req_send(INST_DATA(cl,obj));
		DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
		return 0;

	}
	return DoSuperMethodA(cl, obj, msg);
}


static ULONG xfer_req_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	struct xferreqdata *data;
	juser u;
	Object *filestr, *descstr, *totxt, *sendbut, *canbut;

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_Title, "Send File",
		WindowContents, VGroup,
			Child, ColGroup(2),
				Child, Label2(MSG_XFER_TO),
				Child, totxt = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
				Child, Label2(MSG_XFER_FILE),
				Child, filestr = PopaslObject,
					MUIA_Popstring_String, StringObject,
						StringFrame,
						MUIA_String_MaxLen, 256,
						MUIA_CycleChain, 1,
					End,
					MUIA_Popstring_Button, PopButton(MUII_PopFile),
					MUIA_Popasl_Type, ASL_FileRequest,
				End,
				Child, Label2(MSG_XFER_DESC),
				Child, descstr = StringObject,
					StringFrame,
					MUIA_String_MaxLen, 256,
					MUIA_CycleChain, 1,
				End,
			End,
			Child, RectangleObject,
				MUIA_FixHeightTxt, "M",
				MUIA_Rectangle_HBar, TRUE,
			End,
			Child, HGroup,
				Child, sendbut = mui_button(MSG_XFER_SEND_GAD),
				Child, canbut = mui_button(MSG_XFER_CANCEL_GAD),
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);

	u = (juser)GetTagData(TRANSFER_TO, NULL, msg->ops_AttrList);
	if(u->name)
		set(totxt, MUIA_Text_Contents, my_printf("%s (%s)", u->name, iks_id_print(u->id)));
	else
		set(totxt, MUIA_Text_Contents, iks_id_print(u->id));

	data = INST_DATA(cl,obj);
	data->filestr = filestr;
	data->descstr = descstr;
	data->jid = iks_strdup(iks_id_print(u->id));

	DoMethod(sendbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, TRANSFER_SEND);

	DoMethod(canbut, MUIM_Notify, MUIA_Pressed, FALSE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
	DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);

	return((ULONG)obj);
}


static void xfer_req_send(struct xferreqdata *data)
{
	struct xferdata *xf;
	iks *x, *y;
	char *file, *desc;

	http_up();

	file = mui_sget(data->filestr);
	desc = mui_sget(data->descstr);
	if(!file) return;
	x = iks_make_iq(IKS_TYPE_SET, IKS_NS_OOB);
	iks_insert_attrib(x, "to", data->jid);
	y = iks_find(x, "query");

	xf = malloc(sizeof(struct xferdata));
	memset(xf, 0, sizeof(struct xferdata));
	xf->file = strdup(file);
	xf->name = FilePart(xf->file);
	xf->type = XFER_SEND;
	xf->state = XFER_OFFERING;
	list_append(&xfers, xf);
	DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);

	/* FIX ME */
	iks_insert_cdata(iks_insert(y, "url"), my_printf("http://%s/%s", net_myip(), xf->name) , -1);
	if(desc) iks_insert_cdata(iks_insert(y, "desc"), desc, -1);

	iks_send(net.parser, x);
	iks_delete(x);
}
