⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contact.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
//Contacts

#include "stdafx.h"
#include "Netmsg.h"
#include "NetSocket.h"
#include "Contact.h"
#include "ContactView.h"
#include "Ipclass.h"
#include "FileSend.h"
#include "ConversationThread.h"
#include "Conversation.h"
#include "PopWndThread.h"
#include <mmsystem.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif


CContact::CContact()
{
	Flags = CFL_OFFLINE;
	pThread = NULL;
	Attempted = false;
	NextInvalid = false;
	ClientSocket = NULL;
	Transfer = NULL;
	ClientSocket = NULL;

	memset(&uFont.lfFont, 0, sizeof(LOGFONT));

	uFont.lfFont.lfHeight = 16;
	uFont.lfFont.lfOutPrecision = 3;
	uFont.lfFont.lfClipPrecision = 2;
	uFont.lfFont.lfQuality = 1;
	uFont.lfFont.lfPitchAndFamily = 34;
	strcpy(uFont.lfFont.lfFaceName, "MS Sans Serif");
	uFont.lfFont.lfWeight = FW_BOLD;
	uFont.crColor = RGB(0, 0, 0);
	uFont.nPointSize = 100;
}

CContact::~CContact()
{

}

void CContact::SetScreenName(const char *NewName)
{
	if (!IsContact(this)) 
		return;

	ScreenName = NewName;
	if (IsConvWindowOpen())
	{
		pThread->Conv->ScreenNameChanged(NewName);
	}
}

CString CContact::GetScreenName()
{
	if (!IsContact(this)) 
		return "";
	return ScreenName;
}

CString CContact::GetTruncatedScreenName(int len)
{
	if (!IsContact(this)) 
		return "";

	if (ScreenName.GetLength() < len) return ScreenName;
	CString tmp;
	tmp = ScreenName.Left(len);
	tmp += "...";
	return tmp;
}

IP CContact::GetHostName()
{
	if (!IsContact(this)) 
		return IP("0.0.0.0");

	return HostName;
}

void CContact::SetHost(IP host)
{
	if (!IsContact(this)) 
		return;

	HostName = host;
}

void CContact::SetSock(CNetSocket *Socket)
{
	if (!IsContact(this)) 
		return;

	if (ClientSocket && Socket)
	{
		if (ClientSocket != Socket)
		{
			if (::IsSocket(ClientSocket))
			delete ClientSocket;
		}
	}

	ClientSocket = Socket;

	if (IsSocket(ClientSocket))
	{
		ClientSocket->Contact = this;
	}
}

CNetSocket* CContact::GetSock()
{
	if (!IsContact(this)) 
		return NULL;

	if (::IsSocket(ClientSocket))
	return ClientSocket;

	return NULL;
}

void CContact::SignOffline()
{
	if (!IsContact(this)) 
		return;

	if (::IsSocket(ClientSocket))
	delete ClientSocket;

	ClientSocket = NULL;

	ShowOffline();

	if (IsTransfering())
	{
		if (!Transfer->Finished)
		Transfer->TransferFailed();
	}
}

void CContact::ShowOnline()
{
	if (!IsContact(this)) 
		return;

	bool show = false;

	if (!WeConnected) show = true;

	if (GetApp()->View->Load > 0 && WeConnected) show = true;

	bool bDontShow = false;

	if (IsOnline())
		bDontShow = true;

	Flags |= CFL_ONLINE;
	Flags &= ~CFL_OFFLINE;

	if (show && AfxGetApp()->GetProfileInt("Settings", "UsePopup", 1) && !bDontShow)
	{
		sndPlaySound("alert1.wav", SND_ASYNC);
		char buf[512];
		sprintf(buf, "%s\nhas just signed in", GetTruncatedScreenName(20));
		CPopWndThread *PopThread = new CPopWndThread(buf);
		PopThread->View = GetApp()->View;
		PopThread->Contact = this;
		PopThread->pLast = GetApp()->View->LastPopup;
		PopThread->CreateThread();
	}

	if (IsConvWindowOpen())
	{
		pThread->Conv->UpdateWindow();
	}

	PluginsContactStatusChanged(this, CT_ONLINE);
	GetApp()->View->BuildList();
}

void CContact::ShowOffline()
{
	if (!IsContact(this)) 
		return;

	Flags |= CFL_OFFLINE;
	Flags &= ~CFL_ONLINE;
	Flags &= ~CFL_AWAY;

	if (IsConvWindowOpen())
	{
		pThread->Conv->UpdateWindow();
	}

	PluginsContactStatusChanged(this, CT_OFFLINE);

	GetApp()->View->BuildList();
}

void CContact::SetAwayReason(const char *Reason)
{
	if (!IsContact(this)) 
		return;

	AwayReason = Reason;
}

CString CContact::GetAwayReason()
{
	if (!IsContact(this)) 
		return "";

	return AwayReason;
}

void CContact::SetBlocked(bool Block, HWND wndParent)
{
	if (!IsContact(this)) 
		return;
	if (!IsWindow(wndParent)) 
		return;

	if (Block)
	{
		if (IsTransfering())
		{
			::MessageBox(wndParent, "You cannot block a contact with a file transfer in progress", "Block contact", MB_OK|MB_ICONSTOP);
			return;
		}
		Flags |= CFL_BLOCKED;
	}
	else
		Flags &= ~CFL_BLOCKED;
	GetApp()->View->BuildList();
	if (IsSocket(ClientSocket))
	{
		if (Block)
			ClientSocket->SendString("OFFLINE");
		else
		{
			::SendMyOnline(this);
		}	
	}
	PluginsContactStatusChanged(this, CT_BLOCK);
	if (!(IsConvWindowOpen())) return;
	pThread->Conv->UpdateWindow();
}

void CContact::SetAway(bool bAway, char *Reason)
{
	if (!IsContact(this)) 
		return;

	CContact &Me = GetApp()->View->Me;

	if (this == &Me)
	{
		if (bAway)
		{
			Flags |= CFL_AWAY;
			if (!strcmp(Reason, ""))
			SetAwayReason("Away");
			else SetAwayReason(Reason);
			SendToAllOnline(false, false, "AWAY %s", Me.GetAwayReason());
			GetApp()->View->BuildList();
		}
		else
		{
			Flags &= ~CFL_AWAY;
			SetAwayReason("");
			SendToAllOnline(false, false, "BACK");
			GetApp()->View->BuildList();
		}
		POSITION pos;
		CList<CContact *, CContact *>&List = GetApp()->View->List;
		pos = List.GetHeadPosition();
		while (pos)
		{
			CContact *Contact = List.GetNext(pos);
			if (Contact->IsConvWindowOpen())
			{
				Contact->pThread->Conv->UpdateWindow();
			}
		}
	}
	else
	{
		if (bAway)
		{
			Flags |= CFL_AWAY;
			if (!strcmp(Reason, ""))
				SetAwayReason("Away");
			else SetAwayReason(Reason);
			if (IsConvWindowOpen())
			{
				pThread->Conv->UpdateWindow();
			}
			GetApp()->View->BuildList();
		}
		else
		{
			Flags &= ~CFL_AWAY;
			if (IsConvWindowOpen())
			{
				pThread->Conv->UpdateWindow();
			}
			GetApp()->View->BuildList();
		}
	}
	PluginsContactStatusChanged(this, CT_AWAY);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -