AIMCC Technote 11

Loading a user's Buddy Info for display


Overview

This tutorial demonstrates how to write code that will allow your client to display a user's buddy info. Using the Open AIM SDK you can download and load the user's info.. In the following code samples, we will demonstrate how to implement this code in the following languages, C++, C#, VB, and JAVA. We also have sample clients included showing this code in the Open AIM SDK, you can find the clients in the "samples" folder.

Using AccUserProp_HtmlInfo

Requests the user's aggregated HTML info.
The result will be returned via DAccEvents::OnUserRequestPropertyResult and is of type IAccIm.
The details of how this function works, including the set of acceptable serviceIds and the format of the returned result, are expected to change.

C++ Example

static _ATL_FUNC_INFO oarprInfo =
    { CC_STDCALL, VT_EMPTY, 6, { VT_DISPATCH, VT_DISPATCH, VT_I4, VT_I4, VT_I4, VT_VARIANT } };
    
class CSessionManager : public CComObjectRoot,    
    public IDispEventImpl<1, CSampleApp, &__uuidof(DAccEvents), &LIBID_AccCoreLib, 1>
{

private:
CComPtr<IAccSession> m_spiSession;
CAtlList<int>        m_requests;
	
public:

// DAccEvents Methods    
BEGIN_SINK_MAP(CSessionManager) 
    SINK_ENTRY_INFO(1, __uuidof(DAccEvents), 
            ACCDISPID_ONUSERREQUESTPROPERTYRESULT, OnUserRequestPropertyResult, &oarprInfo)
END_SINK_MAP() 

HRESULT Init(const char* userName, const char* password)
{        
    // create aimcc main object, hook up for events, set id, and signon
    HRESULT hr;
    if (SUCCEEDED(hr = AccCreateSession(IID_IAccSession, (void**)&m_spiSession)) &&
        SUCCEEDED(hr = DispEventAdvise(m_spiSession)) &&
        SUCCEEDED(hr = m_sp->put_Identity(CComBSTR(userName))))
    {
        // more code goes here
    }
    return hr
}
HRESULT CSessionManager::RequestBuddyInfo(CComBSTR& user)
{
    // request authenetication to load the 
    // AIM Expressions web page
    AccTransId transId;
    CComPtr spiUser;
    m_spiSession->CreateUser(user, &spiUser);
    spiUser->RequestProperty(AccUserProp_HtmlInfo, &transId);
    if (SUCCEEDED(hr))
        m_requests.AddTail(id);
    return hr;
}

HRESULT CSessionManager::OnUserRequestPropertyResult(IAccSession* piSession, IAccUser* piUser, AccUserProp prop, AccTransId transId, 
        AccResult hr, xp_kvariant propValue)
{
    // QI for an IAccIm object
    CComBSTR text;
    CComQIPtr spiIm(propValue.pdispVal);
    spiIm->GetConvertedText(OLESTR("application/xhtml+xml"), &text);

    // custom dialog to display HTML content
    // You can create your own
    CViewProfileDlg* dlg = new CViewProfileDlg();
    dlg->SetDialogData(piUser, spiIm, true);
    dlg->ShowWindow(SW_SHOW);
    dlg->BringWindowToTop();
}
	

C# Example

private AccSession s;	
private int[] transIdMap;
private void Run(string username, string password)
{
    try 
    {
        // create control to allow invokes
        CreateControl();	
				
        // create and init session
        object o;
        AccCreateSession(typeof(IAccSession).GUID, out o);				
        s = (AccSession)o;

        // we need to set up a listener for getting logged into the expressions web page
        s.OnUserRequestPropertyResult += new DAccEvents_OnUserRequestPropertyResultEventHandler(s_OnUserRequestPropertyResult);
    }
    catch (COMException e)
    {
        Console.WriteLine(e.Message);
    }
}

private void RequestBuddyInfo()
{
    try
    {
        // create a user object then request the buddy info for the user
        IAccUser user = s.CreateUser(tokens[1]);
        int transId = user.RequestProperty(AccUserProp.AccUserProp_HtmlInfo);
        int i = transIdMap.Length;
        transIdMap[i] = transId;
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        Console.WriteLine("There was an error getting the buddy info page {0}.", ex.ErrorCode);	
    } 
}

private void s_OnUserRequestPropertyResult(AccSession session, IAccUser user, AccUserProp Property, int transId, AccResult hr, object propertyValue)
{
    // check the transId to make sure we made the request
    foreach (int i in transIdMap)
    {
        if (transId == i)
        {
            // take the propertyValue and typecast it to an IAccIm
            // get the html
            IAccIm im = (IAccIm) propertyValue;
            string text = im.GetConvertedText("application/xhtml+xml");

           // take the string and display it in a browser
           // to keep the sample simple we will just write to file
           // and load file in the browser
           TextWriter tw = new StreamWriter("c:\buddyInfo.html");
           // write a line of text to the file
           tw.WriteLine(text);

           // close the stream
           tw.Close();
           System.Diagnostics.Process.Start("c:\buddyInfo.html");
        }
    }
}
		        				            		        
	

VB.net Example

Imports System.IO
Private s As AccSession
Private transIdMap() As Integer

Public Sub Run(ByVal username As String, ByVal password As String)
    Try
        ' create control to allow invokes
        CreateControl()

        ' create and init session
        Dim o As Object
        AccCreateSession(GetType(IAccSession).GUID, o)
        s = CType(o, AccSession)

        ' we need to set up a listener for getting a user's info
        AddHandler s.OnUserRequestPropertyResult, AddressOf s_OnUserRequestPropertyResult                    
       ...
   Catch e As COMException
       Console.WriteLine(e.Message)
   End Try
End Sub                    

Public Sub RequestBuddyInfo()
    Try
        ' create a user object then request the buddy info for the user
        Dim user As IAccUser = s.CreateUser(tokens(1))
        Dim transId As Integer = user.RequestProperty(AccUserProp.AccUserProp_HtmlInfo)
        Dim i As Integer = transIdMap.Length
        transIdMap(i) = transId
    Catch ex As System.Runtime.InteropServices.COMException
        Console.WriteLine("There was an error getting the buddy info page {0}.", ex.ErrorCode)
    End Try
End Sub

Private Sub s_OnUserRequestPropertyResult(ByVal session As AccSession, ByVal user As IAccUser, ByVal prop As AccUserProp, ByVal transId As Integer, ByVal hr As AccResult, ByVal propertyValue As Object)
{
    ' check the transId to make sure we made the request
    Dim i As Integer
    For Each i In transIdMap
        If transId = i Then
            ' take the propertyValue and typecast it to an IAccIm
            ' get the html
            Dim im As IAccIm = CType(propertyValue, IAccIm)
            Dim text As String = im.GetConvertedText("application/xhtml+xml")

            ' take the string and display it in a browser
            ' to keep the sample simple we will just write to file
            ' and load file in the browser
            Dim tw As TextWriter = New StreamWriter("c:\buddyInfo.html")
            ' write a line of text to the file
            tw.WriteLine(text)

            ' close the stream
            tw.Close()
            System.Diagnostics.Process.Start("c:\buddyInfo.html")
        End If
    Next
End Sub
	

JAVA Sample

public AccJSample(String username, String password, String key) throws AccException
{
    // Create main session object
    session = new AccSession();

    // Add event listener
    session.setEventListener(this);

    // set key
    AccClientInfo info = session.getClientInfo();
    info.setDescription(key);

    // set screen name
    session.setIdentity(username);

    session.signOn(password);

    msgPump();
}

private void requestBuddyInfo(String user)
{
    IAccUser user = session.createUser(user);
    int transId = user.requestHtmlInfo();
}

private void msgPump()
{
    //msg pump
    while( running ) 
    {
        try {
            AccSession.pump(50);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void OnUserRequestPropertyResult(AccSession arg0, AccUser arg1, AccUserProp arg2, int arg3, AccResult arg4, AccVariant arg5) {

}
	



rev. 2008/03/03