From d4f82dccc6580f5d8ee85d679174087707aa0230 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 19 Jun 2021 22:37:20 -0500 Subject: [PATCH] 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. --- application/CayaProtocolMessages.h | 2 +- application/ProtocolLooper.cpp | 8 ++++++ application/ProtocolLooper.h | 1 + application/Server.cpp | 12 +++++++++ application/views/RosterListView.cpp | 6 +++-- application/views/RosterView.cpp | 17 ++++++++++++- application/windows/MainWindow.cpp | 3 ++- application/windows/RosterEditWindow.cpp | 31 +++++++++++++++++------- protocols/xmpp/JabberHandler.cpp | 15 ++++++++++++ 9 files changed, 81 insertions(+), 14 deletions(-) diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index d5c3918..ca60625 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -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 diff --git a/application/ProtocolLooper.cpp b/application/ProtocolLooper.cpp index 34450e6..f40a5fc 100644 --- a/application/ProtocolLooper.cpp +++ b/application/ProtocolLooper.cpp @@ -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 { diff --git a/application/ProtocolLooper.h b/application/ProtocolLooper.h index bdf2f1b..06a3460 100644 --- a/application/ProtocolLooper.h +++ b/application/ProtocolLooper.h @@ -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); diff --git a/application/Server.cpp b/application/Server.cpp index 5262cc1..7ee6fc0 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -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; diff --git a/application/views/RosterListView.cpp b/application/views/RosterListView.cpp index e6490e0..ad757fe 100644 --- a/application/views/RosterListView.cpp +++ b/application/views/RosterListView.cpp @@ -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; } diff --git a/application/views/RosterView.cpp b/application/views/RosterView.cpp index af8b0ad..a8b8175 100644 --- a/application/views/RosterView.cpp +++ b/application/views/RosterView.cpp @@ -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) { diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index 3b30567..9c49006 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -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) diff --git a/application/windows/RosterEditWindow.cpp b/application/windows/RosterEditWindow.cpp index eb2ec3c..2689a1c 100644 --- a/application/windows/RosterEditWindow.cpp +++ b/application/windows/RosterEditWindow.cpp @@ -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; diff --git a/protocols/xmpp/JabberHandler.cpp b/protocols/xmpp/JabberHandler.cpp index d7bc4f5..c6fd730 100644 --- a/protocols/xmpp/JabberHandler.cpp +++ b/protocols/xmpp/JabberHandler.cpp @@ -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");