/* priave session handling functions, called by libmeanwhile callbacks */
void MeanwhileSession::handleSessionStateChange(
        enum mwSessionState state, gpointer data)
{
    HERE;
    this->state = state;

    switch (state) {
        case mwSession_STARTING:
        case mwSession_HANDSHAKE:
        case mwSession_HANDSHAKE_ACK:
        case mwSession_LOGIN:
        case mwSession_LOGIN_CONT:
        case mwSession_LOGIN_ACK:
            break;

        case mwSession_LOGIN_REDIR:
            handleRedirect((char *)data);
            break;

        case mwSession_STARTED:
            {
                struct mwUserStatus stat = { mwStatus_ACTIVE, 0, 0L };
                mwSession_setUserStatus(session, &stat);
                struct mwLoginInfo *logininfo = mwSession_getLoginInfo(session);
                if (logininfo) {
                    account->myself()->setNickName(getNickName(logininfo));
                }
                syncContactsFromServer();
            }
            break;

        case mwSession_STOPPING:
            {
                unsigned int info = GPOINTER_TO_UINT(data);
                if (info & ERR_FAILURE) {
                    if (info == INCORRECT_LOGIN)
                        account->password().setWrong();
                    char *reason = mwError(info);
                    emit serverNotification(QString(reason));
                    free(reason);
                }
            }

            emit sessionStateChange(
                    static_cast<MeanwhileProtocol *>(account->protocol())
		    ->statusOffline);
            break;

        case mwSession_STOPPED:
            break;

        case mwSession_UNKNOWN:
        default:
            mwDebug() << "Unhandled state change " << state << endl;
    }
}
VOID CALLBACK IdleTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	CSametimeProto* proto = (CSametimeProto*)idEvent;

	KillTimer(0, proto->idle_timerid);
	proto->idle_timerid = 0;

	if (proto->idle_status) {
		struct mwUserStatus us;
		us.time = (DWORD)time(0);
		us.status = mwStatus_IDLE;
		us.desc = 0;
		mwSession_setUserStatus(proto->session, &us);
	}
	else proto->SetSessionStatus(proto->m_iStatus);
}
int CSametimeProto::SetSessionStatus(int status)
{
	struct mwUserStatus us;
	debugLog(_T("SetSessionStatus() start  status=[%d]"), status);

	if (idle_timerid) KillTimer(0, idle_timerid);

	us.time = (DWORD)time(0);
	//us.time = 0;

	switch (status) {
	case ID_STATUS_FREECHAT:
	case ID_STATUS_ONLINE:
		us.desc = AwayMessages.szOnline; us.status = mwStatus_ACTIVE;
		break;
	case ID_STATUS_NA:
	case ID_STATUS_INVISIBLE:
	case ID_STATUS_ONTHEPHONE:
	case ID_STATUS_OUTTOLUNCH:
	case ID_STATUS_AWAY:
		us.desc = AwayMessages.szAway; us.status = mwStatus_AWAY;
		break;
	case ID_STATUS_OCCUPIED:	// link 'Occupied' MIR_NG status with 'in meeting' Sametime status
		us.desc = AwayMessages.szOccupied; us.status = mwStatus_IN_MEETING;
		break;
	case ID_STATUS_DND:
		us.desc = AwayMessages.szDND; us.status = mwStatus_BUSY;
		break;
	default:
		// act as online for unsupported status
		us.desc = AwayMessages.szOnline; us.status = mwStatus_ACTIVE; break;
	}

	debugLog(_T("SetSessionStatus() mwSession_setUserStatus  us.status=[%d], us.desc:len=[%d]"), us.status, us.desc == NULL ? -1 : strlen(us.desc));
	mwSession_setUserStatus(session, &us);

	return 0;
}
void MeanwhileSession::setStatus(Kopete::OnlineStatus status,
        const Kopete::StatusMessage &msg)
{
    HERE;
    mwDebug() << "setStatus: " << status.description() << '('
        << status.internalStatus() << ')' << endl;
    if (status.internalStatus() == 0)
        return;

    struct mwUserStatus stat;
    mwUserStatus_clone(&stat, mwSession_getUserStatus(session));

    free(stat.desc);

    stat.status = (mwStatusType)status.internalStatus();
    if (msg.isEmpty())
        stat.desc = ::strdup(status.description().toUtf8().constData());
    else
        stat.desc = ::strdup(msg.message().toUtf8().constData());

    mwSession_setUserStatus(session, &stat);
    /* will free stat.desc */
    mwUserStatus_clear(&stat);
}