TreeForUsers.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:12k
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "TreeForUsers.h"
- #include "SearchUserDlg.h"
- #include "variantex.h"
- #include "Passwrd.h"
- CTreeForUsers::CTreeForUsers()
- {
- this->m_bIsUpdating = FALSE;
- this->m_hdrag_s = this->m_hdrag_d = NULL;
- }
- CTreeForUsers::~CTreeForUsers(){}
- BEGIN_MESSAGE_MAP(CTreeForUsers, CTreeCtrl)
- //{{AFX_MSG_MAP(CTreeForUsers)
- ON_WM_CREATE()
- ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
- ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
- ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBegindrag)
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- int CTreeForUsers::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- lpCreateStruct->style |= TVS_SHOWSELALWAYS;
- lpCreateStruct->style |= !TVS_DISABLEDRAGDROP;
- if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if(!this->m_imgTree.Create(IDB_WORKSPACE, 16, 3, RGB(255, 255, 255)))
- {
- TRACE0("Failed to create the m_imgTree for historyn");
- return -1;
- }
-
- this->SetImageList(&m_imgTree, TVSIL_NORMAL);
- this->UpdateTreeForUserDatas();
-
- return 0;
- }
- //update user tree's data.
- void CTreeForUsers::UpdateTreeForUserDatas()
- {
- CGuiRecordSet rsd, rsu;
- DWORD olddata = 0;
- CVariantEx varex;
- char sql[MAX_SQL_SIZE];
- try
- {
- //find all first level departs' info
-
- _snprintf(sql, MAX_SQL_SIZE - 1, SQL::CLIENT_GETSUBDEPARTS, -1);
- if(!rsd.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
-
- //enter departs looping
- HTREEITEM hselected = this->GetSelectedItem();
- if(hselected)
- {
- olddata = this->GetItemData(hselected);
- }
- this->DeleteAllItems();
-
- if(rsd.GetRecordCount() > 0)
- {
- for(rsd.MoveFirst(); !rsd.IsEof(); rsd.MoveNext())
- {
- //get depart.db table's field value.
- int departid = 0;
- string departname("");
-
- rsd.GetCollect("id", varex.get_variant_t());
- departid = varex.AsInteger();
-
- rsd.GetCollect("name", varex.get_variant_t());
- departname = varex.AsString();
-
- HTREEITEM departnode = this->InsertItem(departname.c_str(), 0, 0);
- this->SetItemData(departnode, MAKELONG(departid, NT_DEPART));
-
- if(olddata == this->GetItemData(departnode))
- {
- this->SelectItem(departnode);
- }
- //get all user list for specialed depart
-
- _snprintf(sql, MAX_SQL_SIZE - 1, SQL::CLIENT_GETUSRS_FORSPDEPART, departid);
- if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
-
- //enter users looping
- if(rsu.GetRecordCount() > 0)
- {
- for(rsu.MoveFirst(); !rsu.IsEof(); rsu.MoveNext())
- {
- int userid = 0, state = 0, imgindex = 1;
- string username("");
-
- rsu.GetCollect("id", varex.get_variant_t());
- userid = varex.AsInteger();
- //exclude current agent.
- //if(userid == theApp.m_oCSHelper.GetClientID()) continue;
-
- rsu.GetCollect("name", varex.get_variant_t());
- username = varex.AsString();
- if(username.empty())
- {
- rsu.GetCollect("hostname", varex.get_variant_t());
- username = varex.AsString();
- if(username.empty())
- {
- rsu.GetCollect("ipaddr", varex.get_variant_t());
- username = "Unknown / ";
- username = username + varex.AsString();
- }
- }
-
- rsu.GetCollect("state", varex.get_variant_t());
- state = varex.AsInteger();
-
- imgindex = state ? 1 : 2;
- HTREEITEM usernode = this->InsertItem(username.c_str(), imgindex, imgindex, departnode);
- this->SetItemData(usernode, MAKELONG(userid, NT_AGENT));
- if(olddata == this->GetItemData(usernode))
- {
- this->SelectItem(usernode);
- }
- }
- }
- rsu.Close();
- }
- }
- rsd.Close();
- //add all agents that not in anyone depart
- _snprintf(sql, MAX_SQL_SIZE - 1, SQL::CLIENT_GETUSRS_FORSPDEPART, -1);
- if(!rsu.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
- if(rsu.GetRecordCount() > 0)
- {
- HTREEITEM departnode = this->InsertItem(_LoadString(IDS_DEPARTNOGRP).GetBuffer(0), 0, 0);
- this->SetItemData(departnode, MAKELONG(0, NT_DEPART));
- for(rsu.MoveFirst(); !rsu.IsEof(); rsu.MoveNext())
- {
- int userid = 0, state = 0, imgindex = 1;
- string username("");
-
- rsu.GetCollect("id", varex.get_variant_t());
- userid = varex.AsInteger();
-
- rsu.GetCollect("name", varex.get_variant_t());
- username = varex.AsString();
- if(username.empty())
- {
- rsu.GetCollect("hostname", varex.get_variant_t());
- username = varex.AsString();
-
- if(username.empty())
- {
- rsu.GetCollect("ipaddr", varex.get_variant_t());
- username = "Unknown / ";
- username = username + varex.AsString();
- }
- }
- rsu.GetCollect("state", varex.get_variant_t());
- state = varex.AsInteger();
-
- imgindex = state ? 1 : 2;
- HTREEITEM usernode = this->InsertItem(username.c_str(), imgindex, imgindex, departnode);
- this->SetItemData(usernode, MAKELONG(userid, NT_AGENT));
-
- if(olddata == this->GetItemData(usernode))
- {
- this->SelectItem(usernode);
- }
- }
- }
- rsu.Close();
- }
- catch (_com_error e)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED));
- return ;
- }
- }
- //find specialed user node on this tree
- void CTreeForUsers::FindSpecialedUser()
- {
- CSearchUserDlg findusrdlg;
- if(IDCANCEL == findusrdlg.DoModal()) return ;
- int findagid = -1;
- //if searching user for host name or ip address then
-
- if(findusrdlg.m_chkhostname || findusrdlg.m_chkipaddr)
- {
- //query database data
-
- char sql[MAX_SQL_SIZE];
- CGuiRecordSet rs;
-
- try
- {
- findagid = 0;
-
- //if searching user for host name then
- if(findusrdlg.m_chkhostname)
- {
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_GETSPECEAGENT_FROM_HOST, findusrdlg.m_shostname.GetBuffer(0));
- }
- else if(findusrdlg.m_chkipaddr)
- {
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_GETSPECEAGENT_FROM_IP, findusrdlg.m_sipaddrtxt.GetBuffer(0));
- }
- //query database
- if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
- if(rs.GetRecordCount() == 0)
- {
- MSGBOX_INFO(_LoadString(IDS_DBRSISEMPRY).GetBuffer(0));
- return ;
- }
-
- //get every field values
-
- CVariantEx varex;
-
- rs.GetCollect("id", varex.get_variant_t());
- findagid = varex.AsInteger();
-
- rs.Close();
- }
- catch (_com_error e)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
- }
- HTREEITEM hitem = this->GetRootItem();
-
- for(; hitem != NULL; hitem = this->GetNextSiblingItem(hitem))
- {
- HTREEITEM hsubitem = this->GetChildItem(hitem), hnextitem = hsubitem;
- for(; hsubitem != NULL; )
- {
- CString caption = this->GetItemText(hsubitem);
-
- if(findagid == -1 && !caption.Compare(findusrdlg.m_susername.GetBuffer(0)))
- {
- this->SelectItem(hsubitem);
- return ;
- }
- //if searching user for user name then
- WORD agentid = LOWORD(this->GetItemData(hsubitem));
- if(findagid == agentid)
- {
- this->SelectItem(hsubitem);
- return ;
- }
- //next subitem
- hnextitem = this->GetNextItem(hsubitem, TVGN_NEXT);
- hsubitem = hnextitem;
- }
- }
- }
- bool CTreeForUsers::GetAGInfo(AGINFO &aginfo)
- {
- HTREEITEM hselected = this->GetSelectedItem();
- if(NULL == hselected)
- {
- MSGBOX_INFO(_LoadString(IDS_NOSELECTNODE).GetBuffer(0));
- return false;
- }
- else
- {
- CGuiRecordSet rs;
- CVariantEx varex;
- CString stemp;
- char sql[MAX_SQL_SIZE];
- try
- {
- if(HIWORD(this->GetItemData(hselected)) == NT_AGENT)
- {
- WORD agentid = LOWORD(this->GetItemData(hselected));
-
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_GETSPECEAGENT_FROM_ID, agentid);
- if(!rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql))
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return false;
- }
-
- if(rs.GetRecordCount() == 0) return false;
-
- rs.GetCollect("ipaddr", varex.get_variant_t());
- aginfo.shostip = varex.AsString();
-
- rs.GetCollect("hostname", varex.get_variant_t());
- aginfo.shostname = varex.AsString();
- rs.Close();
- return true;
- }
- else return false;
- }
- catch (_com_error e)
- {
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return false;
- }
- }
- }
- //!!!GuiToolKit Bug --- Right mouse click couldn't trigger WM_CONTEXTMENU message
- void CTreeForUsers::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
- {
- POINT ptcursor;
- ::GetCursorPos(&ptcursor);
- this->SendMessage(WM_CONTEXTMENU, (WPARAM)this->GetSafeHwnd(), MAKELONG(ptcursor.x, ptcursor.y));
- *pResult = 0;
- }
- void CTreeForUsers::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
- {
- AGINFO aginfo;
- if(this->GetAGInfo(aginfo))
- {
- CString stemp;
- stemp.Format("%s / %s", aginfo.shostname.c_str(), aginfo.shostip.c_str());
- char *puretxt = new char[stemp.GetLength()+1];
-
- _snprintf(puretxt, stemp.GetLength()+1, "%s", stemp.GetBuffer(0));
-
- stemp.Empty();
-
- ::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), UWM_UPDATESTATUSBAR, MAKEWORD(0, 1), (LPARAM)puretxt);
- }
- else
- {
- ::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), UWM_UPDATESTATUSBAR, MAKEWORD(0, 1), (LPARAM)NULL);
- }
- *pResult = 0;
- }
- void CTreeForUsers::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
- {
- this->m_hdrag_s = this->m_hdrag_s = NULL;
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
-
- *pResult = 0;
- if(HIWORD(this->GetItemData(pNMTreeView->itemNew.hItem)) == NT_DEPART) return ;
- if(!theApp.m_oAppInstance.m_binadminmode) return ;
- this->m_hdrag_s = pNMTreeView->itemNew.hItem;
- }
- void CTreeForUsers::OnLButtonUp(UINT nFlags, CPoint point)
- {
- UINT flags = 0;
- HTREEITEM hitem = this->HitTest(point, &flags);
-
- if(NULL != hitem && NULL != this->m_hdrag_s && theApp.m_oAppInstance.m_binadminmode)
- {
- if(HIWORD(this->GetItemData(hitem)) == NT_AGENT)
- {
- hitem = this->GetParentItem(hitem);
- }
- this->m_hdrag_d = hitem;
- //if dest node's parent is same as source node's parent then return...
- if(this->GetParentItem(this->m_hdrag_d) == this->GetParentItem(this->m_hdrag_s))
- {
- return ;
- }
-
- //@1---author to server
- if(!theApp.m_oCSHelper.VerifyCurrentUser())
- {
- this->m_hdrag_s = this->m_hdrag_d = NULL;
- CTreeCtrl::OnLButtonUp(nFlags, point);
- MSGBOX_ERROR(_LoadString(IDS_USRAUTHORFAIL).GetBuffer(0));
- return ;
- }
- //@2---update remote database contents.
- bool bsuccessful = true;
- int newdepid = LOWORD(this->GetItemData(this->m_hdrag_d)),
- agentid = LOWORD(this->GetItemData(this->m_hdrag_s));
- char sql[MAX_SQL_SIZE];
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::SERVER_UPDATE_AGFORDEPART, newdepid, agentid);
- CGuiRecordSet *prs = theApp.m_oCSHelper.SendAGUsrSql(sql, bsuccessful, false);
- _DELETE(prs);
- if(!bsuccessful)
- {
- this->m_hdrag_s = this->m_hdrag_d = NULL;
-
- CTreeCtrl::OnLButtonUp(nFlags, point);
- //MSGBOX_ERROR(_LoadString(IDS_DBOPERFAILED).GetBuffer(0));
- return ;
- }
- int nimage = 0, nselimage = 0;
- this->GetItemImage(this->m_hdrag_s, nimage, nselimage);
- DWORD dwdata = this->GetItemData(this->m_hdrag_s);
- hitem = this->InsertItem( this->GetItemText(this->m_hdrag_s).GetBuffer(0), nimage, nselimage, this->m_hdrag_d );
- this->SetItemData(hitem, dwdata);
- this->DeleteItem(this->m_hdrag_s);
- //@3---update local databse contents.
- CGuiRecordSet rs;
- try
- {
- _snprintf(sql, MAX_SQL_SIZE-1, SQL::CLIENT_UPDATE_AGFORDEPART, newdepid, agentid);
- rs.Open(theApp.m_oAppInstance.m_adoconn.GetConecction(), sql);
- }
- catch (_com_error e){}
- }
- this->m_hdrag_s = this->m_hdrag_d = NULL;
- CTreeCtrl::OnLButtonUp(nFlags, point);
- }
- void CTreeForUsers::OnMouseMove(UINT nFlags, CPoint point)
- {
- UINT flags = 0;
- HTREEITEM hitem = this->HitTest(point, &flags);
-
- if(NULL != hitem && NULL != this->m_hdrag_s)
- {
- this->SelectItem(hitem);
- }
- CTreeCtrl::OnMouseMove(nFlags, point);
- }