178 Lotus Instant Messaging/Web Conferencing (Sametime): Building Sametime-Enabled Applications
If the reason is different then ST_ATTRS_NOT_EXIST, we will display an error
message to the user, such as the one shown in Figure 7-9.
Figure 7-9 Displaying an error message when the server storage is down
7.6 Keeping the buddylist updated
Now that we have the buddylist content loaded, we will want to save it and keep
it updated with any additional changes. As you may have noticed, the Extended
Live Names application does not present groups. Instead, this application sample
only allows you to add or remove people using a flat list. When adding people to
the official buddylist, you must have them inside a private group. Remember that
at the end of 7.2, “Overview of the Extended Live Names sample” on page 166
that the reason was provided for why it does not make sense to add people to a
public group. For the purposes of illustrating functionality in this sample, we will
therefore create a hardcoded group for our application called
ExLiveNamesGroup. In the case of a private group, the group name and ID are
identical. We will declare the group ID as a global variable at the top of the
AwarnessList.cpp file by adding this line:
CString sampleGroupId = "ExLiveNamesGroup";
7.6.1 Adding a new user to a private group
When adding a new person, we would like to store the name inside the
ExLiveNamesGroup. First, we will check whether or not this group exists. If not,
we will create the group, add the new user, and then update the BL structure.
This is demonstrated in Example 7-8 on page 179.
Chapter 7. BuddyList service 179
Example 7-8 Adding a new user
void AwarenessList::AddUser(STUser* user, BOOL newUser)
{
if (!user || (FindItemByUserId(user->GetId()) != -1))
return;
STWatchedUser* pWatchedUser = new
STWatchedUser(*user,STGroup(),STUserStatus(ST_USER_STATUS_DISCONNECTED, L""));
if (!pWatchedUser)
return;
int pos = GetUsers();
LV_ITEM item;
item.mask = LVIF_PARAM;
item.iItem = pos;
item.iSubItem = 0;
item.lParam = (DWORD)pWatchedUser;
int index = m_ctlAwarenessListView.InsertItem(&item);
m_ctlAwarenessListView.RedrawItems(index, index);
m_ctlAwarenessListView.UpdateWindow();
if (m_pWatchList)
m_pWatchList->addItem(pWatchedUser);
if (!m_BLServiceIsOn)
{
AfxMessageBox("There is a temporary problem with the storage service,
can't save your buddylist.");
return;
}
if (m_pMyBuddyList)
{
list<BLGroup*> myBuddyListGroups = m_pMyBuddyList->getblGroups();
list<BLGroup*>::iterator itr;
BOOL groupFound = FALSE;
STBLUser* newUser = new STBLUser(STId(user->GetId().getId(),L""),
user->GetName(),
user->GetDesc(),
user->getNickName());
for (itr = myBuddyListGroups.begin(); itr != myBuddyListGroups.end();
itr++)
{
BLGroup* currGroup = *itr;
if (typeid(*currGroup)==typeid(PrivateGroup))
{
PrivateGroup* pvtGroup = (PrivateGroup*)currGroup;
CString currGroupId = pvtGroup->getBLId().c_str();
180 Lotus Instant Messaging/Web Conferencing (Sametime): Building Sametime-Enabled Applications
if (currGroupId == sampleGroupId)
{
pvtGroup->addUser(newUser);
groupFound = TRUE;
break;
}
}
}
if (!groupFound)
{
//there is no sample group, we need to add a new private group
BSTR bstrId(sampleGroupId.AllocSysString());
list<BLUser*> users;
users.push_back(newUser);
PrivateGroup* pvtGroup = new PrivateGroup(bstrId,bstrId,L"",true,users);
m_pMyBuddyList->addGroup(pvtGroup);
::SysFreeString(bstrId);
}
m_pBuddyListService->setBuddyList(*m_pMyBuddyList);
}
}
If the user trying to add people while the BuddyList service is not available, the
service will display a message indicating that this change will not be saved.
If the BuddyList service is available, we will search for the ExLiveNamesGroup. If
we find it, we will add the new user to this group. Otherwise, we will create the
group and add the user to the new group. The final step is to call the
setBuddyList API method for storing the update list on the server.
If you log in from another application (for example, the Sametime Connect client),
you will see a new private group called ExLiveNamesGroup with the user that
you added.
7.6.2 Removing a person from the list
When removing a person from the list, we would like to immediately update the
stored buddylist on the server. To accomplish this, we will update the
AwarenessList::RemoveSelectedUser method.
First, we need to save the removed user ID before deleting the user object:
CString removedUserId(pWatchedUser->GetId().getId().c_str());
delete pWatchedUser;
We will use the user ID for searching for this user in our BL structure.
Chapter 7. BuddyList service 181
At the end of the AwarenessList::RemoveSelectedUser, we will add the lines
shown in Example 7-9.
Example 7-9 Handling remove action
void AwarenessList::RemoveSelectedUser()
{
...
if (!m_BLServiceIsOn)
{
AfxMessageBox("There is a temporary problem with the storage service,
can't save your buddylist.");
return;
}
if (m_pMyBuddyList)
{
list<BLGroup*> myBuddyListGroups = m_pMyBuddyList->getblGroups();
list<BLGroup*>::iterator itr;
for (itr = myBuddyListGroups.begin(); itr != myBuddyListGroups.end();
itr++)
{
BLGroup* currGroup = *itr;
if (typeid(*currGroup)==typeid(PrivateGroup))
{
PrivateGroup* pvtGroup = (PrivateGroup*)currGroup;
list<BLUser*> users = pvtGroup->getUsersInGroup();
list<BLUser*>::iterator userItr;
for (userItr = users.begin(); userItr != users.end(); userItr++)
{
BLUser* blUser = (*userItr);
CString currId(blUser->getBLId().c_str());
if (currId == removedUserId)
pvtGroup->removeUser(blUser);
}
}
}
m_pBuddyListService->setBuddyList(*m_pMyBuddyList);
}
}
First, we need to check that the BuddyList service is available. If it is not
available, the service will display an error message to the user indicating that his
change cannot be saved at this time. If the BuddyList service is available, we will
search for this user in all the private groups in the BL structure and remove it
from the groups.

Get Lotus Instant Messaging/Web Conferencing (Sametime): Building Sametime Enabled Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.