/*  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.
*/
/*
** user information window
*/

#include "common.h"

#include <MUI/NListview_mcc.h>
#include <MUI/NFloattext_mcc.h>

static ULONG userinfo_new(struct IClass *cl, Object *obj, struct opSet *msg);
static void userinfo_showver(struct userinfodata *data, ikspak *pak);


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

		case USERINFO_UPDATE:
		{
			struct userinfodata *data = INST_DATA(cl,obj);
			char *tmp;
			iks *x, *y;
			tmp = mui_sget(data->namestr);
			x = iks_make_iq(IKS_TYPE_SET, IKS_NS_ROSTER);
			y = iks_insert(iks_child(x), "item");
			iks_insert_attrib(y, "jid", iks_id_printx(data->u->id, IKS_ID_USER | IKS_ID_SERVER));
			if(tmp) iks_insert_attrib(y, "name", convert_utf8(tmp));
			iks_send(net.parser, x);
			iks_delete(x);
			return 0;
		}

		case USERINFO_ASKVER:
		{
			struct userinfodata *data = INST_DATA(cl,obj);
			ikspak *pak = (ikspak *)MARG1;

			if(pak)
			{
				userinfo_showver(data, pak);
			}
			else
			{
				iks *x;
				x = iks_make_iq(IKS_TYPE_GET, IKS_NS_VERSION);
				iks_insert_attrib(x, "to", iks_id_print(data->u->id));
				iks_send(net.parser, x);
				iks_delete(x);
			}
			return 0;
		}

		case USERINFO_CLOSE:
			((struct userinfodata *)INST_DATA(cl,obj))->u->infowin = NULL;
			DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
			return 0;
	}
	return DoSuperMethodA(cl,obj,msg);
}


static ULONG userinfo_new(struct IClass *cl, Object *obj, struct opSet *msg)
{
	static const char *pages[] =
	{
		"Presence",
		"Client",
		NULL
	};
	static const char *subsents[] =
	{
		"None",
		"To",
		"From",
		"Both",
		NULL
	};
	struct userinfodata *data;
	Object *namestr, *idtxt, *subs, *subscyc, *stat, *note, *upbut, *canbut;
	Object *clientstr, *verstr, *osstr, *verbut;
	juser u;
	char *tmp;

	obj = (Object *)DoSuperNew(cl,obj,
		MUIA_Window_ID, MAKE_ID('U','I','N','F'),
		MUIA_Window_Title, "User Information",
		WindowContents, VGroup,
			Child, ColGroup(2),
				Child, Label2(MSG_USERINFO_NAME),
				Child, namestr = StringObject,
					StringFrame,
					MUIA_CycleChain, 1,
				End,
				Child, Label2(MSG_USERINFO_JID),
				Child, idtxt = TextObject,
					TextFrame,
					MUIA_Background, MUII_TextBack,
				End,
			End,
			Child, RegisterGroup(pages),
				Child, ColGroup(2),
					Child, Label1(MSG_USERINFO_SUBS),
					Child, HGroup,
						Child, subs = TextObject,
							TextFrame,
							MUIA_Background, MUII_TextBack,
						End,
						Child, Label1(MSG_USERINFO_CHANGETO),
						Child, subscyc = CycleObject,
							MUIA_HorizWeight, 0,
							MUIA_CycleChain, 1,
							MUIA_Cycle_Entries, subsents,
						End,
					End,
					Child, Label1(MSG_USERINFO_STATUS),
					Child, stat = TextObject,
						TextFrame,
						MUIA_Background, MUII_TextBack,
					End,
					Child, VGroup,
						MUIA_HorizWeight, 0,
						Child, Label1(MSG_USERINFO_NOTE),
						Child, RectangleObject, End,
					End,
					Child, NListviewObject,
						MUIA_FixHeightTxt, "\n\n\n",
						MUIA_NListview_NList, note = NFloattextObject,
							ReadListFrame,
						End,
					End,
				End,
				Child, VGroup,
					Child, ColGroup(2),
						Child, Label1(MSG_USERINFO_CLIENT),
						Child, clientstr = TextObject,
							TextFrame,
							MUIA_Background, MUII_TextBack,
						End,
						Child, Label1(MSG_USERINFO_VERSION),
						Child, verstr = TextObject,
							TextFrame,
							MUIA_Background, MUII_TextBack,
						End,
						Child, Label1(MSG_USERINFO_OS),
						Child, osstr = TextObject,
							TextFrame,
							MUIA_Background, MUII_TextBack,
						End,
					End,
					Child, HGroup,
						Child, HSpace(0),
						Child, verbut = mui_button(MSG_USERINFO_ASKVER_GAD),
					End,
				End,
			End,
			Child, RectangleObject,
				MUIA_FixHeightTxt, "M",
				MUIA_Rectangle_HBar, TRUE,
			End,
			Child, HGroup,
				Child, upbut = mui_button(MSG_USERINFO_UPDATE_GAD),
				Child, canbut = mui_button(MSG_USERINFO_CLOSE_GAD),
			End,
		End,
		TAG_MORE, msg->ops_AttrList);

	if(!obj) return(0);

	u = (juser)GetTagData(USERINFO_OF, NULL, msg->ops_AttrList);
	set(namestr, MUIA_String_Contents, convert_locale(u->name));
	set(idtxt, MUIA_Text_Contents, iks_id_print(u->id));
	set(note, MUIA_Floattext_Text, convert_locale(u->status));

	tmp = "None.";
	switch(u->subs)
	{
	case JUSER_TO: tmp="To."; break;
	case JUSER_FROM: tmp="From."; break;
	case JUSER_BOTH: tmp="Both."; break;
	}
	set(subs, MUIA_Text_Contents, tmp);

	tmp = "Available.";
	switch(u->show)
	{
	case IKS_SHOW_UNAVAILABLE: tmp="Unavailable."; break;
	case IKS_SHOW_CHAT: tmp="Free for chat."; break;
	case IKS_SHOW_AWAY: tmp="Temporarily away."; break;
	case IKS_SHOW_XA: tmp="Away for an extended period."; break;
	case IKS_SHOW_DND: tmp="Does not wish to be disturbed."; break;
	}
	set(stat, MUIA_Text_Contents, tmp);

	data = INST_DATA(cl,obj);
	data->u = u;
	data->namestr = namestr;
	data->clientstr = clientstr;
	data->verstr = verstr;
	data->osstr = osstr;

	userinfo_showver(data, NULL);

	DoMethod(upbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, USERINFO_UPDATE);
	DoMethod(verbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, USERINFO_ASKVER, 0);

	DoMethod(canbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, USERINFO_CLOSE);
	DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 1, USERINFO_CLOSE);

	return((ULONG)obj);
}


static void userinfo_showver(struct userinfodata *data, ikspak *pak)
{
	iks *x;

	if(pak)
	{
		if(data->u->verpak) iks_delete(data->u->verpak->x);
		data->u->verpak = pak;
	}
	else
	{
		pak = data->u->verpak;
		if(!pak) return;
	}

	x = iks_find(pak->x, "query");
	set(data->clientstr, MUIA_Text_Contents, iks_find_cdata(x, "name"));
	set(data->verstr, MUIA_Text_Contents, iks_find_cdata(x, "version"));
	set(data->osstr, MUIA_Text_Contents, iks_find_cdata(x, "os"));
}


void userinfo_of(juser u)
{
	Object *win;

	if(u->infowin) {
		DoMethod(u->infowin, MUIM_Window_ToFront);
		return;
	}

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


void userinfo_update(ikspak *pak)
{
	juser u;

	u = roster_find(pak->from);
	if(!u) return;

	if(u->infowin)
		DoMethod(u->infowin, USERINFO_ASKVER, pak);
	else
		u->verpak = pak;
}
