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 //! Contact(s) removed from the server-side list →Caya
// Requires: String "user_id" // Requires: String "user_id"
IM_CONTACT_LIST_REMOVED_CONTACT = 5, IM_CONTACT_LIST_CONTACT_REMOVED = 5,
//! Edit some data on contact →Protocol //! Edit some data on contact →Protocol
// The slots for this message are determined by the protocol's // 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 UserMap
ProtocolLooper::Users() const ProtocolLooper::Users() const
{ {

View File

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

View File

@ -193,6 +193,18 @@ Server::ImMessage(BMessage* msg)
result = B_SKIP_MESSAGE; result = B_SKIP_MESSAGE;
break; 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: case IM_OWN_STATUS_SET:
{ {
int32 status; int32 status;

View File

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

View File

@ -162,6 +162,22 @@ RosterView::ImMessage(BMessage* msg)
} }
break; 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_AVATAR_SET:
case IM_CONTACT_INFO: case IM_CONTACT_INFO:
case IM_EXTENDED_CONTACT_INFO: case IM_EXTENDED_CONTACT_INFO:
@ -201,7 +217,6 @@ RosterView::SetInvocationMessage(BMessage* msg)
fListView->SetInvocationMessage(msg); fListView->SetInvocationMessage(msg);
} }
void void
RosterView::SetAccount(bigtime_t instance_id) RosterView::SetAccount(bigtime_t instance_id)
{ {

View File

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

View File

@ -60,9 +60,7 @@ RosterEditWindow::RosterEditWindow(Server* server)
BButton* fAddButton = new BButton("+", new BMessage(kAddMember)); BButton* fAddButton = new BButton("+", new BMessage(kAddMember));
BButton* fRemoveButton = new BButton("-", new BMessage(kRemoveMember)); BButton* fRemoveButton = new BButton("-", new BMessage(kRemoveMember));
fAddButton->SetExplicitSize(charButtonSize); fAddButton->SetExplicitSize(charButtonSize);
fAddButton->SetEnabled(true);
fRemoveButton->SetExplicitSize(charButtonSize); fRemoveButton->SetExplicitSize(charButtonSize);
fRemoveButton->SetEnabled(false);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f) BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.SetInsets(B_USE_DEFAULT_SPACING) .SetInsets(B_USE_DEFAULT_SPACING)
@ -146,13 +144,6 @@ RosterEditWindow::MessageReceived(BMessage* message)
fEditingWindow->Show(); fEditingWindow->Show();
break; 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: case kAddMember:
{ {
BMessage* add = new BMessage(IM_MESSAGE); BMessage* add = new BMessage(IM_MESSAGE);
@ -162,6 +153,28 @@ RosterEditWindow::MessageReceived(BMessage* message)
win->Show(); win->Show();
break; 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: case kSelAccount:
{ {
int index = message->FindInt32("index") - 1; int index = message->FindInt32("index") - 1;

View File

@ -262,6 +262,21 @@ JabberHandler::Process(BMessage* msg)
break; 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: { case IM_CONTACT_LIST_EDIT_CONTACT: {
BString user_id; BString user_id;
BString user_name = msg->FindString("user_name"); BString user_name = msg->FindString("user_name");