[JDEV] JabberCOM c++ sample

Dai Jin dai_jin at email.com.cn
Wed May 23 19:23:36 CDT 2001


Hello, Robert Temple,

    I have imitated your jabbin project. When i inherit from a CDocument
class, it run. But when i inherit from a CDialog class, it can't run.
Can u give me some help?

Best Regard,
Jim


// JabberDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Jabber.h"
#include "JabberDlg.h"

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

#define MIN_TRACK_WIDTH				120
#define MIN_TRACK_LENGTH			180

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJabberDlg dialog

CJabberDlg::CJabberDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CJabberDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJabberDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	HRESULT hr = CoInitialize(NULL);
	ASSERT(SUCCEEDED(hr));

	hr = spJabberSession.CreateInstance(__uuidof(JabberSession));
	ASSERT(SUCCEEDED(hr));

	// 连接Jabber服务器
	ConnectSink();
}

void CJabberDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJabberDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_DISPATCH_MAP(CJabberDlg, CDialog)
	//{{AFX_DISPATCH_MAP(CJabberDlg)
	DISP_FUNCTION(CJabberDlg, "OnConnect",               OnConnect,               VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnDisconnect",            OnDisconnect,            VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnAuthError",             OnAuthError,             VT_EMPTY, VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnCommError",             OnCommError,             VT_EMPTY, VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnQueryError",            OnQueryError,            VT_EMPTY, VTS_BSTR VTS_BSTR VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnRosterStart",           OnRosterStart,           VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnRosterItem",            OnRosterItem,            VT_EMPTY, VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnRosterEnd",             OnRosterEnd,             VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnMessage",               OnMessage,               VT_EMPTY, VTS_DISPATCH VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnPresence",              OnPresence,              VT_EMPTY, VTS_BSTR VTS_BOOL VTS_BSTR VTS_BOOL VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnSubscriptionRequest",   OnSubscriptionRequest,   VT_EMPTY, VTS_BSTR VTS_BSTR VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnSubscriptionApproved",  OnSubscriptionApproved,  VT_EMPTY, VTS_BSTR VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnSubscriptionDenied",    OnSubscriptionDenied,    VT_EMPTY, VTS_BSTR VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnXML",                   OnXML,                   VT_EMPTY, VTS_I4 VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnJabberIQ",              OnJabberIQ,              VT_EMPTY, VTS_BSTR VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnUnhandledQuery",        OnUnhandledQuery,        VT_EMPTY, VTS_BSTR VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnAgentsStart",           OnAgentsStart,           VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnAgentItem",             OnAgentItem,             VT_EMPTY, VTS_DISPATCH VTS_BSTR)
	DISP_FUNCTION(CJabberDlg, "OnAgentsEnd",             OnAgentsEnd,             VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CJabberDlg, "OnXMLTag",                OnXMLTag,                VT_EMPTY, VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnVersionGet",            OnVersionGet,            VT_EMPTY, VTS_BSTR VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnTimeGet",               OnTimeGet,               VT_EMPTY, VTS_BSTR VTS_DISPATCH)
	DISP_FUNCTION(CJabberDlg, "OnGetOOB",                OnGetOOB,                VT_EMPTY, VTS_BSTR VTS_BSTR VTS_BSTR VTS_DISPATCH)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

BEGIN_INTERFACE_MAP(CJabberDlg, CDialog)
	INTERFACE_PART(CJabberDlg, __uuidof(IJabberSessionEvents), Dispatch)
END_INTERFACE_MAP()

BEGIN_MESSAGE_MAP(CJabberDlg, CDialog)
	//{{AFX_MSG_MAP(CJabberDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_GETMINMAXINFO()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJabberDlg message handlers

BOOL CJabberDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_sJabberID = m_sUsername + '@' + m_sServer;

	Connect();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJabberDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJabberDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CJabberDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CJabberDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	// TODO: Add your message handler code here and/or call default
	lpMMI->ptMinTrackSize.x = MIN_TRACK_WIDTH;
	lpMMI->ptMinTrackSize.y = MIN_TRACK_LENGTH;
	
	CDialog::OnGetMinMaxInfo(lpMMI);
}

BOOL CJabberDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if ((pMsg->message == WM_KEYDOWN) && 
		(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE))
	{
		return TRUE;
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CJabberDlg::ConnectSink()
{
	// 得到JabberSession组件的IDispatch接口指针
	IUnknown* pUnkSink = GetIDispatch(FALSE);
	// 与事件接受器建立连接
	BOOL bSucceed = AfxConnectionAdvise(spJabberSession, 
										__uuidof(IJabberSessionEvents), 
										pUnkSink, FALSE, &m_dwCookie);
	ASSERT(bSucceed);
}

void CJabberDlg::DisConnectSink()
{
	// 得到JabberSession组件的IDispatch接口指针
	IUnknown* pUnkSink = GetIDispatch(FALSE);
	// 与事件接受器建立连接
	BOOL bSucceed = AfxConnectionUnadvise(spJabberSession,
										  __uuidof(IJabberSessionEvents),
										  pUnkSink, FALSE, m_dwCookie);
	ASSERT(bSucceed);
}

void CJabberDlg::OnConnect()
{
	MessageBox("OK");
}

void CJabberDlg::OnDisconnect()
{
}

void CJabberDlg::OnAuthError(LPSTR pTxt)
{
}

void CJabberDlg::OnCommError(LPSTR PTxt)
{
}

void CJabberDlg::OnQueryError(LPSTR PTxt, LPSTR fromjid, IXMLTagPtr tag)
{
}

void CJabberDlg::OnRosterStart()
{
}

void CJabberDlg::OnRosterItem(IJabberRosterItemPtr item)
{
}

void CJabberDlg::OnRosterEnd()
{
}

void CJabberDlg::OnMessage(IJabberMsgPtr msg, IXMLTagPtr tag)
{
}

void CJabberDlg::OnPresence(LPSTR presJID, BOOL available, LPSTR status, BOOL inRoster, IXMLTagPtr tag)
{
}

void CJabberDlg::OnSubscriptionRequest(LPSTR fromJID, LPSTR subType, LPSTR status)
{
}

void CJabberDlg::OnSubscriptionApproved(LPSTR fromJID, LPSTR status)
{
}

void CJabberDlg::OnSubscriptionDenied(LPSTR fromJID, LPSTR status)
{
}

void CJabberDlg::OnXML(long direction, LPSTR txt)
{
}

void CJabberDlg::OnJabberIQ(LPSTR nameSpace, IJabberIQPtr iq)
{
}

void CJabberDlg::OnUnhandledQuery(LPSTR nameSpace, IXMLTagPtr tag)
{
}

void CJabberDlg::OnAgentsStart()
{
}

void CJabberDlg::OnAgentItem(IJabberAgentItemPtr agent, LPSTR iqID)
{
}

void CJabberDlg::OnAgentsEnd()
{
}

void CJabberDlg::OnXMLTag(IXMLTagPtr Tag)
{
}

void CJabberDlg::OnVersionGet(LPSTR fromJID, IXMLTagPtr Tag)
{
}

void CJabberDlg::OnTimeGet(LPSTR fromJID, IXMLTagPtr Tag)
{
}

void CJabberDlg::OnGetOOB(LPSTR fromJID, LPSTR URL, LPSTR Desc, IXMLTagPtr Tag)
{
}

void CJabberDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	DisConnectSink();
	DisConnect();

	CoUninitialize();

	CDialog::OnClose();
}

void CJabberDlg::Connect()
{
	spJabberSession->PutPassword((LPCSTR)m_sUsername);
	spJabberSession->PutPassword((LPCSTR)m_sPassword);
	spJabberSession->PutServer((LPCSTR)m_sServer);
	spJabberSession->PutPort(5222);
	spJabberSession->DoConnect(VARIANT_FALSE, jatDigest);
}

void CJabberDlg::DisConnect()
{
	spJabberSession->PutAvailable(VARIANT_FALSE);
	spJabberSession->DoDisconnect(VARIANT_FALSE);
}



            Dai Jin
            dai_jin at email.com.cn




More information about the JDev mailing list