ChatPanelBar.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:34k
- #include "stdAfx.h"
- #include "trfAgent.h"
- #include "variantex.h"
- #include "ChatPanelBar.h"
- CChatPanelBar::CChatPanelBar()
- {
- this->m_ncurmsgagent = -1;
- this->m_nchatingAgent = -1;
- this->m_hUserStateRefThread = NULL;
- }
- CChatPanelBar::~CChatPanelBar(){}
- BEGIN_MESSAGE_MAP(CChatPanelBar, CSizingControlBar)
- //{{AFX_MSG_MAP(CChatPanelBar)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_DESTROY()
- ON_MESSAGE(UWM_CHATINGMSG, OnChatingMsg)
- ON_MESSAGE(UWM_CHARINPUTOVER, OnInputChatMsgOver)
- ON_MESSAGE(UWM_CHATSTARTLOGON, OnChatStartLogon)
- ON_MESSAGE(UWM_STATICCLICKED, OnStaticClickedMsg)
- ON_MESSAGE(WM_TASKBARNOTIFIERCLICKED, OnTaskbarNotifierClicked)
- //}}AFX_MSG_MAP
-
- ON_MESSAGE(WM_OUTBAR_NOTIFY, OnChatUsersBarNotify)
- END_MESSAGE_MAP()
- int CChatPanelBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if(CSizingControlBar::OnCreate(lpCreateStruct) == -1) return -1;
- CString strLogfile;
-
- m_guifont.CreateFont(
- 12, // nHeight
- 0, // nWidth
- 0, // nEscapement
- 0, // nOrientation
- FW_NORMAL, // nWeight
- FALSE, // bItalic
- FALSE, // bUnderline
- 0, // cStrikeOut
- ANSI_CHARSET, // nCharSet
- OUT_DEFAULT_PRECIS, // nOutPrecision
- CLIP_DEFAULT_PRECIS, // nClipPrecision
- DEFAULT_QUALITY, // nQuality
- DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
- _T("宋体")); // lpszFacename
- //-------------------------------------------------------------------
- if(!m_wndTipInfo.Create(_LoadString(IDS_CHATBARTITLE).GetBuffer(0),
- WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, 680))
- {
- TRACE0("Failed to create m_wndTipInfo(CChatPanelBar) n");
- return -1;
- }
- m_wndTipInfo.SetFont((HFONT)m_guifont);
- m_wndTipInfo.SetFontColor(RGB(0, 0, 0));
- m_wndTipInfo.SetBackColor(RGB(255, 255, 204));
- m_wndTipInfo.SetText(_LoadString(IDS_CHATBARTITLE).GetBuffer(0));
- if(!m_wndAddDelFriendBtn.Create(_LoadString(IDS_CHATADDFRIEND).GetBuffer(0),
- WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, 680))
- {
- TRACE0("Failed to create m_wndAddDelFriendBtn(CChatPanelBar) n");
- return -1;
- }
- m_wndAddDelFriendBtn.SetFont((HFONT)m_guifont);
- m_wndAddDelFriendBtn.SetFontColor(RGB(0, 0, 255));
- m_wndAddDelFriendBtn.SetBackColor(RGB(255, 255, 204));
- m_wndAddDelFriendBtn.SetText(_LoadString(IDS_CHATADDFRIEND).GetBuffer(0));
- if(!m_wndRefUsrsBtn.Create(_LoadString(IDS_CHATREFBTNTXT).GetBuffer(0),
- WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, 680))
- {
- TRACE0("Failed to create m_wndRefUsrsBtn(CChatPanelBar) n");
- return -1;
- }
- m_wndRefUsrsBtn.SetFont((HFONT)m_guifont);
- m_wndRefUsrsBtn.SetFontColor(RGB(0, 0, 255));
- m_wndRefUsrsBtn.SetBackColor(RGB(255, 255, 204));
- m_wndRefUsrsBtn.SetText(_LoadString(IDS_CHATREFBTNTXT).GetBuffer(0));
-
- if(!m_wndBroadcastBtn.Create(_LoadString(IDS_CHATBROADCASTOALLBTNTXT).GetBuffer(0),
- WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, 680))
- {
- TRACE0("Failed to create m_wndBroadcastBtn(CChatPanelBar) n");
- return -1;
- }
- m_wndBroadcastBtn.SetFont((HFONT)m_guifont);
- m_wndBroadcastBtn.SetFontColor(RGB(255, 0, 0));
- m_wndBroadcastBtn.SetBackColor(RGB(255, 255, 204));
- m_wndBroadcastBtn.SetText(_LoadString(IDS_CHATBROADCASTOALLBTNTXT).GetBuffer(0));
-
- //-------------------------------------------------------------------
- if(!m_wndREChatOutput.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
- ES_LEFT | ES_MULTILINE | ES_READONLY | WS_VSCROLL | WS_HSCROLL,
- CRect(0, 0, 0, 0), this, 681))
- {
- TRACE0("Failed to create m_wndREChatOutput(CChatPanelBar) n");
- return -1;
- }
- if(this->GetChatingLogfile(strLogfile))
- {
- if(strLogfile.IsEmpty()) {
- strLogfile.Format("%s\%s", ENV_SYSROOTDIR, ENV_DEFAULT_CHATINGLOGFILE);
- }
- if(!m_wndREChatOutput.Setlogfile(strLogfile.GetBuffer(0)))
- {
- TRACE0("Failed to open chating logfile(CChatPanelBar) n");
- return -1;
- }
- }
- //-------------------------------------------------------------------
- if(!m_wndEDChatInput.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
- ES_MULTILINE | WS_VSCROLL,
- CRect(0, 0, 0, 0), this, 682))
- {
- TRACE0("Failed to create m_wndEDChatInput(CChatPanelBar) n");
- return -1;
- }
- m_wndEDChatInput.SetLimitText(SIZE_BUFFER);
- //-------------------------------------------------------------------
- if(!this->m_wndTaskbar.Create(this))
- {
- TRACE0("Failed to create m_wndTaskbar(CChatPanelBar) n");
- return -1;
- }
- this->m_wndTaskbar.SetSkin(IDB_SKIN_BOARD, 255, 0, 255);
- this->m_wndTaskbar.SetTextFont("Arial", 90, TN_TEXT_NORMAL, TN_TEXT_UNDERLINE | TN_TEXT_BOLD);
- this->m_wndTaskbar.SetTextColor(RGB(255, 255, 255), RGB(0, 0, 255));
- this->m_wndTaskbar.SetTextRect(CRect(10, 20, m_wndTaskbar.m_nSkinWidth-10, m_wndTaskbar.m_nSkinHeight-20));
-
- //-------------------------------------------------------------------
- DWORD dwf = CGfxOutBarCtrl::fDragItems|
- CGfxOutBarCtrl::fEditGroups|
- CGfxOutBarCtrl::fEditItems|
- CGfxOutBarCtrl::fRemoveGroups|
- CGfxOutBarCtrl::fRemoveItems|
- CGfxOutBarCtrl::fAddGroups|
- CGfxOutBarCtrl::fAnimation;
-
- if(!m_wndUsersBar.Create(WS_CHILD | WS_VISIBLE, CRect(0,0,0,0), this, 683, dwf))
- {
- TRACE0("Failed to create m_wndUsersBar(CChatPanelBar) n");
- return -1;
- }
- this->m_wndUsersBar.SetOwner(this);
- //-------------------------------------------------------------------
- this->InitMyFriendsFolder();
- //-------------------------------------------------------------------
-
- //create UDP socket.
- int dwUDPort = this->GetUDPPort();
- if(!m_udpclt.Create(dwUDPort, 10, 10)) return FALSE;
-
- //create the user states refreshing thread and killer event.
-
- DWORD dwthreadid = 0x0000;
- this->m_hUserStateRefThreadKillEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
- this->m_hUserStateRefThread = ::CreateThread(NULL, 0, Routine_UserStateRef, this, 0, &dwthreadid);
- this->m_hBroadcastEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
- this->m_hBroadcastWorkThreadKillEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
- this->m_hBroadcastWorkThread = ::CreateThread(NULL, 0, Routine_BroadcastMsg, this, 0, &dwthreadid);
-
- return 0;
- }
- void CChatPanelBar::OnDestroy()
- {
- //deattach gdi object.
- m_guifont.DeleteObject();
- //kill all working threads.
- if(NULL != this->m_hBroadcastWorkThread)
- {
- ::SetEvent(this->m_hBroadcastWorkThreadKillEvent);
- ::WaitForSingleObject(this->m_hBroadcastWorkThread, INFINITE);
-
- ::CloseHandle(this->m_hBroadcastWorkThread);
- ::CloseHandle(this->m_hBroadcastWorkThreadKillEvent);
- ::CloseHandle(this->m_hBroadcastEvent);
-
- this->m_hBroadcastEvent = NULL;
- this->m_hBroadcastWorkThread = NULL;
- this->m_hBroadcastWorkThreadKillEvent = NULL;
- }
- if(NULL != this->m_hUserStateRefThread)
- {
- ::SetEvent(this->m_hUserStateRefThreadKillEvent);
- ::WaitForSingleObject(this->m_hUserStateRefThread, INFINITE);
-
- ::CloseHandle(this->m_hUserStateRefThread);
- ::CloseHandle(this->m_hUserStateRefThreadKillEvent);
- this->m_hUserStateRefThread = NULL;
- this->m_hUserStateRefThreadKillEvent = NULL;
- }
- //destroy sub-windows.
- this->m_wndTaskbar.DestroyWindow();
- this->m_wndTipInfo.DestroyWindow();
- this->m_wndRefUsrsBtn.DestroyWindow();
- this->m_wndBroadcastBtn.DestroyWindow();
- this->m_wndAddDelFriendBtn.DestroyWindow();
- }
- void CChatPanelBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
- {
- CSizingControlBar::OnUpdateCmdUI(pTarget, bDisableIfNoHndler);
- UpdateDialogControls(pTarget, bDisableIfNoHndler);
- }
- void CChatPanelBar::OnSize(UINT nType, int cx, int cy)
- {
- CSizingControlBar::OnSize(nType, cx, cy);
- AdjustChildrenRegion();
- }
- void CChatPanelBar::AdjustChildrenRegion(void)
- {
- const int WIDTH_OUTLOOKBAR = 15;
- const int HEIGHT_TIPINFO = 15;
- const int HEIGHT_REOUTPUT = 70;
- const int HEIGHT_EDINPUT = 30;
-
- if( !m_wndTipInfo.GetSafeHwnd() ||
- !m_wndAddDelFriendBtn.GetSafeHwnd() ||
- !m_wndRefUsrsBtn.GetSafeHwnd() ||
- !m_wndBroadcastBtn.GetSafeHwnd() ||
- !m_wndREChatOutput.GetSafeHwnd() ||
- !m_wndEDChatInput.GetSafeHwnd() ||
- !m_wndUsersBar.GetSafeHwnd())
- return ;
- int width = 0;
- CRect rcParent, rcChild;
- this->GetClientRect(&rcParent);
-
- //tip info static window.
- //1
- rcChild = rcParent;
- width = rcParent.Width() - ((rcParent.Width() * WIDTH_OUTLOOKBAR / 100) + 2);
-
- rcChild.bottom = rcChild.top + HEIGHT_TIPINFO;
- rcChild.right = rcChild.left + width / 2;
- m_wndTipInfo.MoveWindow(&rcChild);
- //2
- rcChild.left = rcChild.right;
- rcChild.right= rcChild.left + width / 6;
- m_wndAddDelFriendBtn.MoveWindow(&rcChild);
- //3
- rcChild.left = rcChild.right;
- rcChild.right= rcChild.left + width / 6 - 10;
- m_wndRefUsrsBtn.MoveWindow(&rcChild);
- //4
- rcChild.left = rcChild.right;
- rcChild.right= rcChild.left + width / 6 + 10;
- m_wndBroadcastBtn.MoveWindow(&rcChild);
- //chat contents output richedit window.
- rcChild = rcParent;
- rcChild.top += (HEIGHT_TIPINFO + 1);
- rcChild.right -= ((rcParent.Width() * WIDTH_OUTLOOKBAR / 100) - 2);
- rcChild.right -= GetSystemMetrics(SM_CXBORDER);
- rcChild.bottom= rcChild.top + ((rcParent.Height() - HEIGHT_TIPINFO)* HEIGHT_REOUTPUT / 100);
- m_wndREChatOutput.MoveWindow(&rcChild);
-
- //chat contents input window.
- rcChild.top = rcChild.bottom + 2;
- rcChild.bottom = rcChild.top + ((rcParent.Height() - HEIGHT_EDINPUT) * HEIGHT_EDINPUT / 100) - 2;
- rcChild.right -= GetSystemMetrics(SM_CXBORDER);
- m_wndEDChatInput.MoveWindow(&rcChild);
- //chating users outlook-bar window.
- rcChild = rcParent;
- rcChild.left = rcChild.right - (rcParent.Width() * WIDTH_OUTLOOKBAR / 100) + 2;
- rcChild.right -= 1;
- m_wndUsersBar.MoveWindow(&rcChild);
- return ;
- }
- void CChatPanelBar::OnInputChatMsgOver(WPARAM wparam, LPARAM lparam)
- {
- CString strContents;
- this->m_wndEDChatInput.GetWindowText(strContents);
- CHATINGPACKAGE cpg;
- memset(&cpg, 0x0, sizeof(cpg));
- sprintf(cpg.pktype, "%*d", SIZE_PKTYPE, CPT_MSGTXT);
- sprintf(cpg.agentid, "%*d", SIZE_AGENTID, theApp.m_oCSHelper.GetClientID());
- sprintf(cpg.headportrait, "%*d", SIZE_HADPTRT, this->GetUserHeadportrait());
- _snprintf(cpg.buf, sizeof(cpg.buf) - 1, "%s", strContents.GetBuffer(0));
- if(this->m_nchatingAgent != -1)
- {
- std::string strName, strIp;
- if(this->GetAgentNameIpInfo(m_nchatingAgent, strName, strIp))
- {
- SOCKADDR_IN from;
- CHATINGPACKAGE cpgResponse;
-
- this->m_udpclt.Write(strIp.c_str(), (char*)&cpg, sizeof(cpg));
- this->m_udpclt.Read(from, (char*)&cpgResponse, sizeof(cpgResponse));
- this->m_wndREChatOutput.AppendText(
- "to",
- strName.c_str(),
- strContents.GetBuffer(0));
- this->m_wndEDChatInput.SetWindowText("");
- this->m_wndEDChatInput.SetFocus();
- }
- }
- else
- {
- MSGBOX_INFO(_LoadString(IDS_CHATPLSELTOUSR).GetBuffer(0));
- this->m_wndEDChatInput.SetFocus();
- return ;
- }
- }
- //chating users outlook-bar notify message handler.
- long CChatPanelBar::OnChatUsersBarNotify(WPARAM wParam, LPARAM lParam)
- {
- switch (wParam)
- {
- case NM_FOLDERCHANGE:
- {
- PlaySound(MAKEINTRESOURCE(IDR_FOLDERSOUND),
- AfxGetResourceHandle(),
- SND_RESOURCE | SND_ASYNC | SND_NODEFAULT);
- if((int)lParam == 0)
- {
- this->m_wndBroadcastBtn.SetText(_LoadString(IDS_CHATBROADCASTOALLBTNTXT));
- this->m_wndBroadcastBtn.SetFontColor(RGB(255, 0, 0));
- }
- else if(int(lParam) == 1)
- {
- this->m_wndBroadcastBtn.SetText(_LoadString(IDS_CHATBROADCASTOGROUPBTNTXT));
- this->m_wndBroadcastBtn.SetFontColor(RGB(0, 0, 255));
- }
- this->m_wndBroadcastBtn.Invalidate();
- }
- return 1;
- case NM_OB_ITEMCLICK:
- {
- //cast the lParam to an integer to get the clicked item
- int index = (int)lParam;
- int nfolderindex = this->m_wndUsersBar.GetSelFolder();
-
- if(nfolderindex == 0 || nfolderindex == 1)
- {
- DWORD dwdata = this->m_wndUsersBar.GetFolderItemData(nfolderindex, index);
- this->m_nchatingAgent = HIWORD(dwdata);
- CString strtipinfo;
- strtipinfo.Format("%s : %s",
- _LoadString(IDS_CHATBARTITLE).GetBuffer(0),
- m_wndUsersBar.GetFolderItemText(nfolderindex, index));
- this->m_wndTipInfo.SetText(strtipinfo.GetBuffer(0));
- this->UpdateAddDelFriendBtnCaption();
- this->m_wndEDChatInput.SetFocus();
- }
- }
- return 0;
-
- case NM_OB_ONLABELENDEDIT:
- {
- // cast the lParam to an OUTBAR_INFO * struct; it will contain info about the edited item
- // return 1 to do the change and 0 to cancel it
- OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
- }
- return 1;
-
- case NM_OB_ONGROUPENDEDIT:
- {
- // cast the lParam to an OUTBAR_INFO * struct; it will contain info about the edited folder
- // return 1 to do the change and 0 to cancel it
- OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
- }
- return 1;
-
- case NM_OB_DRAGITEM:
- {
- // cast the lParam to an OUTBAR_INFO * struct; it will contain info about the dragged items
- // return 1 to do the change and 0 to cancel it
- OUTBAR_INFO * pOI = (OUTBAR_INFO *) lParam;
- }
- return 1;
- }
- return 0;
- }
- void CChatPanelBar::OnChatingMsg(WPARAM wparam, LPARAM lparam)
- {
- //get the chating package.
- char *pbuf = (char*)lparam;
- LPCHATINGPACKAGE pcp = (LPCHATINGPACKAGE)lparam;
- if(NULL == pbuf) return ;
- //get package type.
- int nPKType = (pcp->pktype[0] - '0');
- switch(nPKType) {
- case CPT_LOGIN:
- case CPT_PROBE:
- ProcessClientLogon(pcp);
- break;
- case CPT_LOGOFF:
- ProcessClientLogoff(pcp);
- break;
- case CPT_MSGTXT:
- ProcessClientMsgtxt(pcp);
- break;
- }
- delete []pbuf;
- }
- //user states refreshing thread routine.
- DWORD CChatPanelBar::Routine_UserStateRef(LPVOID lpParameters)
- {
- ASSERT(lpParameters != NULL);
- CChatPanelBar *pthis = (CChatPanelBar*)lpParameters;
- for(; ::WaitForSingleObject(pthis->m_hUserStateRefThreadKillEvent, 0) == WAIT_TIMEOUT; )
- {
- for(int nFolder = 0; nFolder < pthis->m_wndUsersBar.GetFolderCount(); ++nFolder)
- {
- for(int nItem = 0; nItem < pthis->m_wndUsersBar.GetFolderItemCount(nFolder); ++nItem)
- {
- DWORD dwdata = pthis->m_wndUsersBar.GetFolderItemData(nFolder, nItem);
- int nAgentId = HIWORD(dwdata);
- WORD wHeadportraitIndex = LOWORD(dwdata);
- int nNormalImgIndex = 0, nDisableImgIndex = 0;
- pthis->m_wndUsersBar.GetImageIndex(wHeadportraitIndex, nNormalImgIndex, nDisableImgIndex);
- int nitemimgindex = pthis->m_wndUsersBar.GetFolderItemImage(nFolder, nItem);
- BOOL bIsMyFriend = pthis->InFriendsGroup(nAgentId);
- if(pthis->AgentIsOnline(nAgentId))
- {
- if(nitemimgindex != nNormalImgIndex)
- {
- if(bIsMyFriend)
- pthis->m_wndUsersBar.SetFolderItemImage(nFolder, nItem, nNormalImgIndex);
- else
- pthis->m_wndUsersBar.RemoveFolderItem(nFolder, nItem);
- }
- }
- else
- {
- if(nitemimgindex != nDisableImgIndex)
- {
- if(bIsMyFriend)
- pthis->m_wndUsersBar.SetFolderItemImage(nFolder, nItem, nDisableImgIndex);
- else
- pthis->m_wndUsersBar.RemoveFolderItem(nFolder, nItem);
- }
- }
- }
- }
- Sleep(6000);
- }
- return 0;
- }
- //broadcast working thread.
- DWORD CChatPanelBar::Routine_BroadcastMsg(LPVOID lpParameters)
- {
- ASSERT(lpParameters != NULL);
- int nconlens = 0;
- char *pcontents = NULL;
- SOCKADDR_IN from;
- CHATINGPACKAGE cpg, cpgResponse;
-
- CChatPanelBar *pthis = (CChatPanelBar*)lpParameters;
- memset(&cpg, 0x0, sizeof(cpg));
- sprintf(cpg.pktype, "%*d", SIZE_PKTYPE, CPT_MSGTXT);
- sprintf(cpg.agentid, "%*d", SIZE_AGENTID, theApp.m_oCSHelper.GetClientID());
- sprintf(cpg.headportrait, "%*d", SIZE_HADPTRT, pthis->GetUserHeadportrait());
-
-
- for(; ::WaitForSingleObject(pthis->m_hBroadcastWorkThreadKillEvent, 0) == WAIT_TIMEOUT; )
- {
- if(::WaitForSingleObject(pthis->m_hBroadcastEvent, 10) == WAIT_TIMEOUT) continue;
- nconlens = pthis->m_wndEDChatInput.GetWindowTextLength();
-
- pcontents = new char[nconlens + 1];
- memset(pcontents, 0x0, nconlens + 1);
- pthis->m_wndEDChatInput.GetWindowText(pcontents, nconlens);
- memset(cpg.buf, 0x0, sizeof(cpg.buf));
- _snprintf(cpg.buf, sizeof(cpg.buf) - 1, "%s", pcontents);
- int nFolder = pthis->m_wndUsersBar.GetSelFolder();
- if(nFolder == 0)
- {
- for(nFolder = 0; nFolder < pthis->m_wndUsersBar.GetFolderCount(); ++nFolder)
- {
- for(int nItem = 0; nItem < pthis->m_wndUsersBar.GetFolderItemCount(nFolder); ++nItem)
- {
- DWORD dwdata = pthis->m_wndUsersBar.GetFolderItemData(nFolder, nItem);
- int nAgentId = HIWORD(dwdata);
-
- std::string strName, strIp;
-
- if(pthis->GetAgentNameIpInfo(nAgentId, strName, strIp))
- {
- pthis->m_udpclt.Write(strIp.c_str(), (char*)&cpg, sizeof(cpg));
- pthis->m_udpclt.Read(from, (char*)&cpgResponse, sizeof(cpgResponse));
- }
- }
- }
- }
- else if(nFolder == 1)
- {
- for(int nItem = 0; nItem < pthis->m_wndUsersBar.GetFolderItemCount(nFolder); ++nItem)
- {
- DWORD dwdata = pthis->m_wndUsersBar.GetFolderItemData(nFolder, nItem);
- int nAgentId = HIWORD(dwdata);
-
- std::string strName, strIp;
-
- if(pthis->GetAgentNameIpInfo(nAgentId, strName, strIp))
- {
- pthis->m_udpclt.Write(strIp.c_str(), (char*)&cpg, sizeof(cpg));
- pthis->m_udpclt.Read(from, (char*)&cpgResponse, sizeof(cpgResponse));
- }
- }
- }
- //free the memory space.
-
- delete []pcontents;
- pcontents = NULL;
- }
- return 0;
- }
- BOOL CChatPanelBar::GetAgentNameIpInfo(const int nAgentId,
- std::string &strName,
- std::string &strIp)
- {
- //get the user basic information.
- CGuiRecordSet rsu;
- CVariantEx varex;
- char sql[MAX_SQL_SIZE];
-
- strName.erase();
- strIp.erase();
- try
- {
- if(nAgentId < 1) return FALSE;
- //find all first level departs' info
-
- _snprintf(sql, MAX_SQL_SIZE - 1, SQL::SERVER_GET_AGENTINFO_FRMID, nAgentId);
- if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return FALSE;
- }
- rsu.MoveFirst();
- if(!rsu.IsEof())
- {
- rsu.GetCollect("name", varex.get_variant_t());
- strName = varex.AsString();
-
- if(strName.empty())
- {
- rsu.GetCollect("hostname", varex.get_variant_t());
- strName = varex.AsString();
-
- if(strName.empty())
- {
- rsu.GetCollect("ipaddr", varex.get_variant_t());
- strName = varex.AsString();
- strIp = varex.AsString();
- if(strName.empty()) return FALSE;
- }
- }
- rsu.GetCollect("ipaddr", varex.get_variant_t());
- strIp = varex.AsString();
- if(strIp.empty()) return FALSE;
- return TRUE;
- }
- }
- catch(_com_error e)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED));
- return FALSE;
- }
- return FALSE;
- }
- BOOL CChatPanelBar::AgentIsOnline(const int nAgentid)
- {
- std::string strName, strIp;
- if(!this->GetAgentNameIpInfo(nAgentid, strName, strIp)) return FALSE;
- SOCKADDR_IN from;
- CHATINGPACKAGE cpg;
-
- memset(&cpg, 0x0, sizeof(cpg));
- sprintf(cpg.pktype, "%*d", SIZE_PKTYPE, CPT_PROBE);
- sprintf(cpg.agentid, "%*d", SIZE_AGENTID, nAgentid);
- sprintf(cpg.headportrait, "%*d", SIZE_HADPTRT, 0);
- CHATINGPACKAGE cpgResponse;
- m_udpclt.Write(strIp.c_str(), NULL, 0);
- return (m_udpclt.Read(from, (char*)&cpgResponse, sizeof(cpgResponse)) > 0);
- }
- BOOL CChatPanelBar::GetAgentFolderItemIndex(const int nAgentId, int &nFolderIndex, int &nItemIndex)
- {
- for(int nFolder = 0; nFolder < this->m_wndUsersBar.GetFolderCount(); ++nFolder)
- {
- for(int nItem = 0; nItem < this->m_wndUsersBar.GetFolderItemCount(nFolder); ++nItem)
- {
- DWORD dwdata = this->m_wndUsersBar.GetFolderItemData(nFolder, nItem);
- if(HIWORD(dwdata) == nAgentId)
- {
- nFolderIndex = nFolder;
- nItemIndex = nItem;
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- void CChatPanelBar::ProcessClientLogon(LPCHATINGPACKAGE pcp)
- {
- if(!pcp) return ;
- int nItemIndex = -1;
- int nAgentId = atoi(pcp->agentid);
- int nHeadportrait = atoi(pcp->headportrait);
- int nNormalImgIndex = 0, nDisableImgIndex = 0;
- bool bneedsoundnotify = false;
- std::string strname, strip;
- //filter self.
- if(theApp.m_oCSHelper.GetClientID() == nAgentId) return ;
- this->m_wndUsersBar.GetImageIndex(nHeadportrait, nNormalImgIndex, nDisableImgIndex);
-
- //update agent state that in all folder window.
-
- if(this->InFriendsGroup(nAgentId))
- {
- if(this->m_wndUsersBar.ExistsAgent(1, nAgentId, nItemIndex))
- {
- //if exists this user then change the user's state(hot image).
- this->m_wndUsersBar.SetItemImage(nItemIndex, nNormalImgIndex);
- bneedsoundnotify = true;
- }
- else
- {
- if(this->GetAgentNameIpInfo(nAgentId, strname, strip))
- {
- int index = this->m_wndUsersBar.GetFolderItemCount(1);
- this->m_wndUsersBar.InsertItem(1, index, strname.c_str(),
- nNormalImgIndex, MAKELONG(nHeadportrait, nAgentId));
-
- bneedsoundnotify = true;
- }
- }
- }
- else
- {
- if(!this->m_wndUsersBar.ExistsAgent(0, nAgentId, nItemIndex))
- {
- if(this->GetAgentNameIpInfo(nAgentId, strname, strip))
- {
- int index = this->m_wndUsersBar.GetFolderItemCount(0);
- this->m_wndUsersBar.InsertItem(0, index, strname.c_str(),
- nNormalImgIndex, MAKELONG(nHeadportrait, nAgentId));
-
- bneedsoundnotify = true;
- }
- }
- else
- {
- this->m_wndUsersBar.SetItemImage(nItemIndex, nNormalImgIndex);
- }
- }
- //sound notify.
- if(bneedsoundnotify)
- PlaySound(MAKEINTRESOURCE(IDR_GOLINESOUND), AfxGetResourceHandle(),
- SND_RESOURCE | SND_ASYNC | SND_NODEFAULT);
- }
- void CChatPanelBar::ProcessClientLogoff(LPCHATINGPACKAGE pcp)
- {
- if(!pcp) return ;
- int nItemIndex = -1;
- int nAgentId = atoi(pcp->agentid);
- int nHeadportrait = atoi(pcp->headportrait);
- int nNormalImgIndex = 0, nDisableImgIndex = 0;
- this->m_wndUsersBar.GetImageIndex(nHeadportrait, nNormalImgIndex, nDisableImgIndex);
-
- //update agent state that in all folder window.(disable)
-
- if(this->m_wndUsersBar.ExistsAgent(0, nAgentId, nItemIndex))
- {
- //if exists this user then delete the from 0 folder.
- this->m_wndUsersBar.RemoveFolderItem(0, nItemIndex);
- }
- //
- if(this->m_wndUsersBar.ExistsAgent(1, nAgentId, nItemIndex))
- {
- //if exists this user then change the user's state(hot image).
- this->m_wndUsersBar.SetFolderItemImage(1, nItemIndex, nDisableImgIndex);
- }
- }
- void CChatPanelBar::ProcessClientMsgtxt(LPCHATINGPACKAGE pcp)
- {
- if(!pcp) return ;
- int nAgentId = atoi(pcp->agentid);
- int nHeadportrait = atoi(pcp->headportrait);
- int nFolderIndex = -1, nItemIndex = -1;
- this->m_ncurmsgagent = nAgentId;
- //sound notify.
- PlaySound(MAKEINTRESOURCE(IDR_MSGSOUND),
- AfxGetResourceHandle(),
- SND_RESOURCE | SND_ASYNC | SND_NODEFAULT);
- if(this->GetAgentFolderItemIndex(nAgentId, nFolderIndex, nItemIndex))
- {
- if(nFolderIndex != this->m_wndUsersBar.GetSelFolder())
- this->m_wndUsersBar.SetSelFolder(nFolderIndex);
- }
- //if "this" window is unactive then open sound or
- //tip window notifying function.
- if(!this->IsWindowVisible())
- {
- //Load setting from registry
- CRegister regKey;
-
- if(regKey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
- {
- DWORD dwValue = regKey.Read_DWord_Value(REG_NMSGNOTIFYTYPE);
- regKey.Close();
- if(dwValue != 2)
- {
- this->m_wndTaskbar.Show((char*)pcp->buf);
- }
- }
- }
- //display message in message output window.(RE)
- std::string strName, strIp;
- if(this->GetAgentNameIpInfo(nAgentId, strName, strIp))
- {
- m_wndREChatOutput.AppendText("from", strName.c_str(), (char*)pcp->buf);
- }
- }
- BOOL CChatPanelBar::LogonChatingRoom(void)
- {
- //get the user basic information.
- CGuiRecordSet rsu;
- CVariantEx varex;
- std::string strIp;
- int nAgentId,
- nItemIndex;
- char sql[MAX_SQL_SIZE];
- CHATINGPACKAGE cpgResponse;
- try
- {
- //find all agents' info
-
- _snprintf(sql, MAX_SQL_SIZE - 1, SQL::SERVER_AGENT_GETALL, -1);
- if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return FALSE;
- }
- //make the package.
- SOCKADDR_IN from;
-
- CHATINGPACKAGE cpg;
- sprintf(cpg.pktype, "%*d", SIZE_PKTYPE, CPT_LOGIN);
- sprintf(cpg.agentid, "%*d", SIZE_AGENTID, theApp.m_oCSHelper.GetClientID());
- sprintf(cpg.headportrait, "%*d", SIZE_HADPTRT, this->GetUserHeadportrait());
-
- for(rsu.MoveFirst(); !rsu.IsEof(); rsu.MoveNext())
- {
- rsu.GetCollect("id", varex.get_variant_t());
- nAgentId = varex.AsInteger();
- if(theApp.m_oCSHelper.GetClientID() == nAgentId) continue;
- rsu.GetCollect("ipaddr", varex.get_variant_t());
- strIp = varex.AsString();
- if(strIp.empty()) continue;
- if(m_udpclt.Write(strIp.c_str(), (char*)&cpg, sizeof(cpg)) == sizeof(cpg))
- {
- memset(&cpgResponse, 0x0, sizeof(cpgResponse));
-
- if(m_udpclt.Read(from, (char*)&cpgResponse, sizeof(cpgResponse)) == sizeof(cpgResponse))
- {
- //update agent state that in all folder window.
- int nNormalImgIndex = 0, nDisableImgIndex = 0;
- int nHeadportrait = atoi((char*)cpgResponse.headportrait);
-
- this->m_wndUsersBar.GetImageIndex(nHeadportrait, nNormalImgIndex, nDisableImgIndex);
- if(this->InFriendsGroup(nAgentId))
- {
- if(this->m_wndUsersBar.ExistsAgent(1, nAgentId, nItemIndex))
- {
- //if exists this user then change the user's state(hot image).
- this->m_wndUsersBar.SetItemImage(nItemIndex, nNormalImgIndex);
- }
- }
- else
- {
- if(!this->m_wndUsersBar.ExistsAgent(0, nAgentId, nItemIndex))
- {
- int index = this->m_wndUsersBar.GetFolderItemCount(0);
-
- std::string strname, strip;
-
- if(this->GetAgentNameIpInfo(nAgentId, strname, strip))
- {
- this->m_wndUsersBar.InsertItem(0, index, strname.c_str(),
- nNormalImgIndex, MAKELONG(nHeadportrait, nAgentId));
- }
- }
- }
- }
- }
- }
- }
- catch(_com_error e)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED));
- return FALSE;
- }
- return TRUE;
- }
- int CChatPanelBar::GetUDPPort(void)
- {
- CRegister regkey;
- DWORD dwUDPort;
-
- //read the UDP server listening port.
- if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY)) return false;
-
- dwUDPort = regkey.Read_DWord_Value(REG_NCHATUDPPORT);
- regkey.Close();
-
- //determine the UDP port whether is valid.
- if(dwUDPort < 1000 || dwUDPort > 65535) dwUDPort = 5558;
-
- return dwUDPort;
- }
- int CChatPanelBar::GetUserHeadportrait(void)
- {
- //Load setting from registry
- CRegister regKey;
-
- if(!regKey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
- {
- TRACE0("Failed to open the registry keyn");
- return -1;
- }
- //Integer
- DWORD dwValue = regKey.Read_DWord_Value(REG_NHEADBMPINDEX);
-
- return dwValue;
- }
- BOOL CChatPanelBar::InFriendsGroup(const int nAgentId)
- {
- CArray<DWORD, DWORD> arrMyfriends;
- if(this->GetMyFriendsList(arrMyfriends))
- {
- DWORD dwdata = 0x0000;
- for(int index = 0; index < arrMyfriends.GetSize(); ++index)
- {
- dwdata = arrMyfriends[index];
- if(HIWORD(dwdata) == nAgentId) return TRUE;
- }
- }
- return FALSE;
- }
- void CChatPanelBar::OnChatStartLogon(WPARAM wparam, LPARAM lparam)
- {
- this->LogonChatingRoom();
- }
- LRESULT CChatPanelBar::OnTaskbarNotifierClicked(WPARAM wParam, LPARAM lParam)
- {
- if(this->m_ncurmsgagent != -1)
- {
- this->m_nchatingAgent = this->m_ncurmsgagent;
- this->UpdateAddDelFriendBtnCaption();
- }
- return 0;
- }
- void CChatPanelBar::OnStaticClickedMsg(WPARAM wparam, LPARAM lparam)
- {
- HWND hfromWnd = (HWND)wparam;
- if(hfromWnd == this->m_wndTipInfo.GetSafeHwnd())
- {
- std::string strName, strIp;
- if( this->m_nchatingAgent != -1 &&
- this->GetAgentNameIpInfo(this->m_nchatingAgent, strName, strIp))
- {
- CString strTipInfo;
- strTipInfo.Format("Current Chating Username : %s nHost Ip : %s",
- strName.c_str(), strIp.c_str());
- MSGBOX_INFO(strTipInfo.GetBuffer(0));
- }
- }
- else if(hfromWnd == this->m_wndRefUsrsBtn.GetSafeHwnd())
- {
- CString strlogfile;
- if(this->GetChatingLogfile(strlogfile) && FileExists(strlogfile.GetBuffer(0)))
- {
- ShellExecute(this->GetSafeHwnd(), "open",
- strlogfile.GetBuffer(0), NULL, NULL, SW_MAXIMIZE);
- }
- else
- {
- MSGBOX_ERROR(_LoadString(IDS_CHATOPENLOGFILEFAILED).GetBuffer(0));
- return ;
- }
- }
- else if(hfromWnd == this->m_wndAddDelFriendBtn.GetSafeHwnd())
- {
- DWORD exdata = this->m_wndAddDelFriendBtn.m_exData;
- int nfolderindex = this->m_wndUsersBar.GetSelFolder();
- if(this->m_nchatingAgent != -1)
- {
- int nitemindex = -1;
- std::string strName, strIp;
- //add friend.
- if(exdata == 0)
- {
- if( this->m_wndUsersBar.ExistsAgent(0, m_nchatingAgent, nitemindex) &&
- !this->m_wndUsersBar.ExistsAgent(1, m_nchatingAgent, nitemindex) &&
- this->GetAgentNameIpInfo(m_nchatingAgent, strName, strIp))
- {
- int nimgindex= this->m_wndUsersBar.GetFolderItemImage(0, nitemindex);
- DWORD dwdata = this->m_wndUsersBar.GetFolderItemData(0, nitemindex);
- this->m_wndUsersBar.RemoveFolderItem(0, nitemindex);
- this->m_wndUsersBar.InsertItem(1, 0, strName.c_str(), nimgindex, dwdata);
-
- this->AddMyFriend(dwdata);
- }
- else
- {
- MSGBOX_INFO(_LoadString(IDS_CHATUSRINFRIEND).GetBuffer(0));
- this->m_wndEDChatInput.SetFocus();
- return ;
- }
- }
- //delete friend.
- else if(exdata == 1)
- {
- if( this->m_wndUsersBar.ExistsAgent(1, m_nchatingAgent, nitemindex) &&
- !this->m_wndUsersBar.ExistsAgent(0, m_nchatingAgent, nitemindex) &&
- this->GetAgentNameIpInfo(m_nchatingAgent, strName, strIp))
- {
- int nimgindex= this->m_wndUsersBar.GetFolderItemImage(1, nitemindex);
- DWORD dwdata = this->m_wndUsersBar.GetFolderItemData(1, nitemindex);
- int nNormalImgIndex, nDisableImgIndex;
-
- this->m_wndUsersBar.GetImageIndex(LOWORD(dwdata),
- nNormalImgIndex, nDisableImgIndex);
- //if this user is online then add it into first folder.
- if(nNormalImgIndex == nimgindex)
- {
- this->m_wndUsersBar.InsertItem(0, 0, strName.c_str(), nimgindex, dwdata);
- }
- this->m_wndUsersBar.RemoveFolderItem(1, nitemindex);
- this->DeleteMyFriend(m_nchatingAgent);
- }
- else
- {
- MSGBOX_INFO(_LoadString(IDS_CHATUSRINFRIEND).GetBuffer(0));
- this->m_wndEDChatInput.SetFocus();
- return ;
- }
- }
- }
- }
- else if(hfromWnd == this->m_wndBroadcastBtn.GetSafeHwnd())
- {
- int nfolderindex = this->m_wndUsersBar.GetSelFolder();
- if(nfolderindex == 0)
- {
- if(!MSGBOX_CONFIRM(_LoadString(IDS_CHATBROADCASTOALLSURE).GetBuffer(0)))
- return ;
- }
- else if(nfolderindex == 1)
- {
- if(!MSGBOX_CONFIRM(_LoadString(IDS_CHATBROADCASTOFRDSURE).GetBuffer(0)))
- return ;
- }
- ::SetEvent(this->m_hBroadcastEvent);
- }
- }
- BOOL CChatPanelBar::AddMyFriend(DWORD dwItemData)
- {
- CRegister regKey;
- if(regKey.Open(HKEY_CURRENT_USER, REG_SCHATFRNDSKEY))
- {
- CString stridentity;
- stridentity.Format("%d", dwItemData);
- return regKey.Write_DWord_Value(stridentity.GetBuffer(0), dwItemData);
- }
- return FALSE;
- }
- BOOL CChatPanelBar::DeleteMyFriend(const int nAgentId)
- {
- CRegister regKey;
- if(regKey.Open(HKEY_CURRENT_USER, REG_SCHATFRNDSKEY))
- {
- DWORD cbValueName = 0, dwType = 0,
- dwData = 0, cbData = 0;
- const int VALUENAME_SIZE = 32;
- char *lpValueName = new char[VALUENAME_SIZE];
-
- for(int index = 0; index < 1000; ++index)
- {
- cbValueName = VALUENAME_SIZE;
- //if this value's type is REG_DWORD.
- LONG lRet = ::RegEnumValue(regKey.Get_CurrentKey(), index,
- lpValueName, &cbValueName, NULL, &dwType, NULL, NULL);
-
- if(lRet != ERROR_SUCCESS)
- {
- delete []lpValueName;
-
- return (lRet == ERROR_NO_MORE_ITEMS);
- }
- if(dwType == REG_DWORD)
- {
- lRet = ::RegEnumValue(regKey.Get_CurrentKey(), index,
- lpValueName, &cbValueName, NULL, &dwType,
- (LPBYTE)&dwData, &cbData);
-
- if(lRet != ERROR_SUCCESS)
- {
- delete []lpValueName;
- return FALSE;
- }
- if(HIWORD(dwData) == nAgentId)
- {
- regKey.DeleteValue(lpValueName);
- break;
- }
- }
- }
- delete []lpValueName;
- return TRUE;
- }
- return FALSE;
- }
- BOOL CChatPanelBar::GetMyFriendsList(CArray<DWORD, DWORD> &arrMyfriends)
- {
- CRegister regKey;
-
- arrMyfriends.RemoveAll();
- if(regKey.Open(HKEY_CURRENT_USER, REG_SCHATFRNDSKEY))
- {
- const VALUENAME_SIZE = 32;
- DWORD cbValueName = 0, dwType = 0,
- dwData = 0, cbData = 0;
-
- char *lpValueName = new char[VALUENAME_SIZE];
- for(int index = 0; index < 1000; ++index)
- {
- cbValueName = cbData = VALUENAME_SIZE;
-
- //if this value's type is REG_DWORD.
- LONG lRet = ::RegEnumValue(regKey.Get_CurrentKey(), index,
- lpValueName, &cbValueName, NULL, &dwType, NULL, NULL);
-
- if(lRet != ERROR_SUCCESS)
- {
- delete []lpValueName;
- return (lRet == ERROR_NO_MORE_ITEMS);
- }
- if(dwType == REG_DWORD)
- {
- cbValueName = cbData = VALUENAME_SIZE;
- lRet = ::RegEnumValue(regKey.Get_CurrentKey(), index,
- lpValueName, &cbValueName, NULL, &dwType,
- (LPBYTE)&dwData, &cbData);
- if(lRet != ERROR_SUCCESS)
- {
- delete []lpValueName;
- return FALSE;
- }
- arrMyfriends.Add(dwData);
- }
- }
- delete []lpValueName;
- return TRUE;
- }
- return FALSE;
- }
- void CChatPanelBar::UpdateAddDelFriendBtnCaption(void)
- {
- if(!this->InFriendsGroup(this->m_nchatingAgent))
- this->m_wndAddDelFriendBtn.SetText(_LoadString(IDS_CHATADDFRIEND));
- else
- this->m_wndAddDelFriendBtn.SetText(_LoadString(IDS_CHATDELFRIEND));
- this->m_wndAddDelFriendBtn.m_exData ^= 1;
- }
- void CChatPanelBar::InitMyFriendsFolder(void)
- {
- CArray<DWORD, DWORD> arrMyfriends;
- int nNormalImgIndex = 0, nDisableImgIndex = 0;
- if(this->GetMyFriendsList(arrMyfriends))
- {
- DWORD dwdata = 0x0000;
- WORD wAgentId = 0, wHeadportraitIndex = 0;
- std::string strName, strIp;
- for(int index = 0; index < arrMyfriends.GetSize(); ++index)
- {
- dwdata = arrMyfriends[index];
- wAgentId = HIWORD(dwdata);
- wHeadportraitIndex = LOWORD(dwdata);
- if( this->m_wndUsersBar.GetImageIndex(wHeadportraitIndex, nNormalImgIndex, nDisableImgIndex) &&
- this->GetAgentNameIpInfo(wAgentId, strName, strIp))
- this->m_wndUsersBar.InsertItem(1, index, strName.c_str(), nDisableImgIndex, dwdata);
- }
- }
- }
- //get chating logfile.
- BOOL CChatPanelBar::GetChatingLogfile(CString &strLogfile)
- {
- //Load setting from registry
- CRegister regKey;
-
- strLogfile.Empty();
- if(!regKey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
- {
- TRACE0("Failed to open the registry keyn");
- return FALSE;
- }
- strLogfile = regKey.Read_String_Value(REG_STRCHATLOGFILE);
- strLogfile.TrimLeft();
- strLogfile.TrimRight();
- return TRUE;
- }