Removal of roster members

Now roster members can be deleted through the RosterEditWindow.

IM_CONTACT_LIST_REMOVED_CONTACT was renamed to
IM_CONTACT_LIST_CONTACT_REMOVED to fit style of other API messages.

Fixes #1.
This commit is contained in:
Jaidyn Ann 2021-06-19 22:37:20 -05:00
parent a21c8f7601
commit d4f82dccc6
9 changed files with 81 additions and 14 deletions

View File

@ -47,7 +47,7 @@ enum im_what_code {
//! Contact(s) removed from the server-side list →Caya
// Requires: String "user_id"
IM_CONTACT_LIST_REMOVED_CONTACT = 5,
IM_CONTACT_LIST_CONTACT_REMOVED = 5,
//! Edit some data on contact →Protocol
// The slots for this message are determined by the protocol's

View File

@ -110,6 +110,14 @@ ProtocolLooper::AddContact(Contact* contact)
}
void
ProtocolLooper::RemoveContact(Contact* contact)
{
fRosterMap.RemoveItemFor(contact->GetId());
fUserMap.AddItem(contact->GetId(), (User*)contact);
}
UserMap
ProtocolLooper::Users() const
{

View File

@ -44,6 +44,7 @@ public:
RosterMap Contacts() const;
Contact* ContactById(BString id);
void AddContact(Contact* contact);
void RemoveContact(Contact* contact);
UserMap Users() const;
User* UserById(BString id);

View File

@ -193,6 +193,18 @@ Server::ImMessage(BMessage* msg)
result = B_SKIP_MESSAGE;
break;
}
case IM_CONTACT_LIST_CONTACT_REMOVED:
{
Contact* contact = _EnsureContact(msg);
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL || contact == NULL) {
result = B_SKIP_MESSAGE;
break;
}
looper->RemoveContact(contact);
break;
}
case IM_OWN_STATUS_SET:
{
int32 status;

View File

@ -218,9 +218,11 @@ RosterListView::Draw(BRect updateRect)
bool
RosterListView::AddRosterItem(RosterItem* item)
{
bool ret = false;
if (HasItem(item) == false)
return AddItem(item);
return false;
ret = AddItem(item);
Sort();
return ret;
}

View File

@ -162,6 +162,22 @@ RosterView::ImMessage(BMessage* msg)
}
break;
}
case IM_CONTACT_LIST_CONTACT_REMOVED:
{
int32 status = -1;
int64 instance;
BString user_id = msg->FindString("user_id");
if (msg->FindInt32("status", &status) != B_OK
|| msg->FindInt64("instance", &instance) != B_OK
|| user_id.IsEmpty() == true)
return;
Contact* contact = fServer->ContactById(user_id, instance);
if (contact == NULL)
return;
RosterItem* rosterItem = contact->GetRosterItem();
if (rosterItem)
fListView->RemoveItem(rosterItem);
}
case IM_AVATAR_SET:
case IM_CONTACT_INFO:
case IM_EXTENDED_CONTACT_INFO:
@ -201,7 +217,6 @@ RosterView::SetInvocationMessage(BMessage* msg)
fListView->SetInvocationMessage(msg);
}
void
RosterView::SetAccount(bigtime_t instance_id)
{

View File

@ -285,9 +285,10 @@ MainWindow::ImMessage(BMessage* msg)
break;
}
case IM_AVATAR_SET:
case IM_STATUS_SET:
case IM_CONTACT_INFO:
case IM_EXTENDED_CONTACT_INFO:
case IM_STATUS_SET:
case IM_CONTACT_LIST_CONTACT_REMOVED:
if (fRosterWindow != NULL)
fRosterWindow->PostMessage(msg);
if (RosterEditWindow::Check() == true)

View File

@ -60,9 +60,7 @@ RosterEditWindow::RosterEditWindow(Server* server)
BButton* fAddButton = new BButton("+", new BMessage(kAddMember));
BButton* fRemoveButton = new BButton("-", new BMessage(kRemoveMember));
fAddButton->SetExplicitSize(charButtonSize);
fAddButton->SetEnabled(true);
fRemoveButton->SetExplicitSize(charButtonSize);
fRemoveButton->SetEnabled(false);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.SetInsets(B_USE_DEFAULT_SPACING)
@ -146,13 +144,6 @@ RosterEditWindow::MessageReceived(BMessage* message)
fEditingWindow->Show();
break;
}
case IM_MESSAGE: {
if (message->GetInt32("im_what", 0) == IM_EXTENDED_CONTACT_INFO)
if (message->GetString("user_id", "") == fEditingUser)
fEditingWindow->PostMessage(message);
fRosterView->MessageReceived(message);
break;
}
case kAddMember:
{
BMessage* add = new BMessage(IM_MESSAGE);
@ -162,6 +153,28 @@ RosterEditWindow::MessageReceived(BMessage* message)
win->Show();
break;
}
case kRemoveMember:
{
int index = message->FindInt32("index");
RosterItem* ritem = fRosterView->ListView()->RosterItemAt(index);
if (ritem == NULL)
return;
User* user = ritem->GetContact();
BMessage* rem = new BMessage(IM_MESSAGE);
rem->AddInt32("im_what", IM_CONTACT_LIST_REMOVE_CONTACT);
rem->AddString("user_id", user->GetId());
user->GetProtocolLooper()->PostMessage(rem);
break;
}
case IM_MESSAGE: {
if (message->GetInt32("im_what", 0) == IM_EXTENDED_CONTACT_INFO)
if (message->GetString("user_id", "") == fEditingUser)
fEditingWindow->PostMessage(message);
fRosterView->MessageReceived(message);
break;
}
case kSelAccount:
{
int index = message->FindInt32("index") - 1;

View File

@ -262,6 +262,21 @@ JabberHandler::Process(BMessage* msg)
break;
}
case IM_CONTACT_LIST_REMOVE_CONTACT: {
BString user_id;
if (msg->FindString("user_id", &user_id) != B_OK)
break;
fClient->rosterManager()->remove(gloox::JID(user_id.String()));
fClient->rosterManager()->unsubscribe(gloox::JID(user_id.String()));
fClient->rosterManager()->synchronize();
BMessage rm(IM_MESSAGE);
rm.AddInt32("im_what", IM_CONTACT_LIST_CONTACT_REMOVED);
rm.AddString("user_id", user_id);
_SendMessage(&rm);
break;
}
case IM_CONTACT_LIST_EDIT_CONTACT: {
BString user_id;
BString user_name = msg->FindString("user_name");