📄 buddylist.cpp
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006 Daniel Lindstr鰉 and Daniel Nilsson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "BuddyList.h"
#include "defines.h"
#include "PacketQuery.h"
#include "PacketLookUp.h"
#include "PacketConnect.h"
#define MAX_USER_RESULT 100
#define MAX_GROUP_RESULT 100
BuddyList::BuddyList(ConnectionHandler *ch, int cid) :
favUsers(-1),
favGroups(-1),
selectedGroups(-1),
userCache(NULL),
groupCache(NULL)
{
this->ui = NULL;
this->conHandler = ch;
this->serverCid = cid;
this->onlyFavGroups = false;
this->onlyFavUsers = false;
this->lowestStatus = STATUS_AWAY;
this->refreshUserStatus = false;
}
BuddyList::~BuddyList(void) {
}
bool BuddyList::load(char *filename) {
int i, j;
FILE *f = fopen(filename, "r");
if (f == NULL)
return false;
if (fscanf(f, "%d %d %d\n", &i, &j, &lowestStatus) != 3) {
fclose(f);
return false;
}
onlyFavUsers = (i != 0);
onlyFavGroups = (j != 0);
if (fscanf(f, "%d\n", &j) != 1) {
fclose(f);
return false;
}
for (i = 0; i < j; i++) {
int uid;
if (fscanf(f, "%d ", &uid) != 1) {
fclose(f);
return false;
}
addFavUser(uid);
}
fscanf(f, "\n");
if (fscanf(f, "%d\n", &j) != 1) {
fclose(f);
return false;
}
for (i = 0; i < j; i++) {
int gid;
if (fscanf(f, "%d ", &gid) != 1) {
fclose(f);
return false;
}
addFavGroup(gid);
}
fscanf(f, "\n");
fclose(f);
return true;
}
bool BuddyList::save(char *filename) {
int i;
FILE *f = fopen(filename, "w");
if (f == NULL)
return false;
fprintf(f, "%d %d %d\n", onlyFavUsers ? 1 : 0, onlyFavGroups ? 1 : 0, lowestStatus);
fprintf(f, "%d\n", favUsers.getCount());
for (i = 0; i < favUsers.getSize(); i++) {
int uid = favUsers.getItem(i);
if (uid != -1) {
fprintf(f, "%d ", uid);
}
}
fprintf(f, "\n");
fprintf(f, "%d\n", favGroups.getCount());
for (i = 0; i < favGroups.getSize(); i++) {
int gid = favGroups.getItem(i);
if (gid != -1) {
fprintf(f, "%d ", gid);
}
}
fprintf(f, "\n");
fclose(f);
return true;
}
void BuddyList::setUI(BuddyListUI *ui) {
this->ui = ui;
}
void BuddyList::refresh(bool usr, bool grp, bool status) {
int i, j, n = selectedGroups.getCount();
int flags = 0;
int *groups = NULL;
PacketQuery *pq;
if (usr) {
refreshUserStatus = status;
if (n > 0) {
groups = new int[n];
if (!groups) {
printf("Failed to allocate groups");
return;
}
for (i = j = 0; i < selectedGroups.getSize(); i++) {
if (selectedGroups.getItem(i) != -1) {
groups[j++] = selectedGroups.getItem(i);
}
}
if (j != n) {
printf("BUG! Wrong number of groups!");
}
flags |= QUERY_FLAG_GROUPS;
}
flags |= QUERY_GET_USERS;
switch (lowestStatus) {
case STATUS_OFFLINE:
flags |= QUERY_FLAG_OFFLINE;
/* fall through */
case STATUS_AWAY:
flags |= QUERY_FLAG_AWAY;
/* fall through */
case STATUS_ONLINE:
flags |= QUERY_FLAG_ONLINE;
break;
default:
printf("Strange lowestStatus: %d\n", lowestStatus);
}
pq = new PacketQuery(flags, groups, n, MAX_USER_RESULT);
pq->setCId(serverCid);
conHandler->sendPacket(pq);
if (groups != NULL) {
delete groups;
}
}
if (grp) {
pq = new PacketQuery(QUERY_GET_GROUPS, NULL, 0, MAX_GROUP_RESULT);
pq->setCId(serverCid);
conHandler->sendPacket(pq);
}
}
void BuddyList::showOnlyFavUsers(bool b) {
onlyFavUsers = b;
}
void BuddyList::showOnlyFavGroups(bool b) {
onlyFavGroups = b;
}
bool BuddyList::getOnlyFavUsers(void) {
return onlyFavUsers;
}
bool BuddyList::getOnlyFavGroups(void) {
return onlyFavGroups;
}
void BuddyList::setLowestStatus(int status) {
lowestStatus = status;
}
int BuddyList::getLowestStatus(void) {
return lowestStatus;
}
void BuddyList::selectGroup(int gid) {
if (!selectedGroups.exists(gid)) {
selectedGroups.addItem(gid);
}
}
void BuddyList::deselectGroup(int gid) {
selectedGroups.removeItem(gid);
}
bool BuddyList::isSelectedGroup(int gid) {
return selectedGroups.exists(gid);
}
void BuddyList::addFavUser(int uid) {
if (!favUsers.exists(uid)) {
favUsers.addItem(uid);
}
}
void BuddyList::removeFavUser(int uid) {
favUsers.removeItem(uid);
}
bool BuddyList::isFavUser(int uid) {
return favUsers.exists(uid);
}
void BuddyList::addFavGroup(int gid) {
if (!favGroups.exists(gid)) {
favGroups.addItem(gid);
}
}
void BuddyList::removeFavGroup(int gid) {
favGroups.removeItem(gid);
}
bool BuddyList::isFavGroup(int gid) {
return favGroups.exists(gid);
}
void BuddyList::newUserList(const int *uids, int num) {
int i;
PacketLookUp *plu;
ui->clearUsers();
for (i = 0; i < num; i++) {
if (!onlyFavUsers || favUsers.exists(uids[i])) {
CacheEntry *ce = userCache.get(uids[i]);
if (ce == NULL) {
ce = makeUserCacheEntry(uids[i]);
plu = new PacketLookUp(uids[i], LOOKUP_USER | LOOKUP_NAME | LOOKUP_STATUS);
plu->setCId(serverCid);
conHandler->sendPacket(plu);
} else if (refreshUserStatus) {
plu = new PacketLookUp(uids[i], LOOKUP_USER | LOOKUP_STATUS);
plu->setCId(serverCid);
conHandler->sendPacket(plu);
}
ui->addUser(uids[i], ce->name, ce->status);
}
}
refreshUserStatus = false;
}
void BuddyList::newGroupList(const int *gids, int num) {
int i;
PacketLookUp *plu;
ui->clearGroups();
for (i = 0; i < num; i++) {
if (!onlyFavGroups || favGroups.exists(gids[i])) {
CacheEntry *ce = groupCache.get(gids[i]);
if (ce == NULL) {
ce = makeGroupCacheEntry(gids[i]);
plu = new PacketLookUp(gids[i], LOOKUP_GROUP | LOOKUP_NAME | LOOKUP_STATUS);
plu->setCId(serverCid);
conHandler->sendPacket(plu);
}
ui->addGroup(gids[i], ce->name, ce->status, selectedGroups.exists(gids[i]));
}
}
}
CacheEntry *BuddyList::makeUserCacheEntry(int uid) {
CacheEntry *ce = new CacheEntry;
_snwprintf(ce->name, NAME_BUF_SIZE-1, L"User #%d", uid);
ce->status = 0;
userCache.put(uid, ce);
return ce;
}
CacheEntry *BuddyList::makeGroupCacheEntry(int gid) {
CacheEntry *ce = new CacheEntry;
_snwprintf(ce->name, NAME_BUF_SIZE-1, L"Group #%d", gid);
ce->status = 0;
groupCache.put(gid, ce);
return ce;
}
void BuddyList::setUserName(int uid, WCHAR *name) {
CacheEntry *ce = userCache.get(uid);
if (ce == NULL) {
ce = makeUserCacheEntry(uid);
}
wcscpy_s(ce->name, NAME_BUF_SIZE, name);
ui->changeUser(uid, ce->name, ce->status);
}
void BuddyList::setGroupName(int gid, WCHAR *name) {
CacheEntry *ce = groupCache.get(gid);
if (ce == NULL) {
ce = makeGroupCacheEntry(gid);
}
wcscpy_s(ce->name, NAME_BUF_SIZE, name);
ui->changeGroup(gid, ce->name, ce->status, selectedGroups.exists(gid));
}
void BuddyList::setUserStatus(int uid, int status) {
CacheEntry *ce = userCache.get(uid);
if (ce == NULL) {
ce = makeUserCacheEntry(uid);
}
ce->status = status;
ui->changeUser(uid, ce->name, ce->status);
}
void BuddyList::setGroupStatus(int gid, int status) {
CacheEntry *ce = groupCache.get(gid);
if (ce == NULL) {
ce = makeGroupCacheEntry(gid);
}
ce->status = status;
ui->changeGroup(gid, ce->name, ce->status, selectedGroups.exists(gid));
}
void BuddyList::connectTo(int uid){
//TODO:: open a socket for incomming connection first.
PacketConnect *packet = new PacketConnect(uid);
packet->setCId(this->serverCid);
this->conHandler->sendPacket(packet);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -