From f0ce3e87c65e2de80c735a5d4ed1a8af41769952 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Thu, 5 Aug 2021 14:10:21 -0500 Subject: [PATCH] Support generally setting own nick, own UserItems MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seperate UserItems are now created for each list, too, rather than a single one being created per-user. This functionally works a lot nicer. But onto more important things… now setting the user's own nick should work quite well. Finally. =w= This has given me a good bit of trouble over the past couple of days― setting the user's nick *worked*, but it wouldn't propagate to its corresponding UserItem nor its UserInfoDialog. It would, however, work with the StatusView. These are all registered Observers of the User itself, so if one works, they *all* should, them all being registered to the same User. Now, if a given User isn't found in the ProtocolLooper's user-list, the Conversation class will take it upon itself to create a new one and add it to both of their respective lists. So the user's own contact would be set in the ProtocolLooper― but it *wouldn't* be added to the user-list. Hilarity ensues as two seperate objects for the user's own contact would be created. Since the StatusView is registered to the ProtocolLooper's "real" own contact slot, it would receive all updates… but since Conversations' user-lists and items would be registered to the Conversation-created "fake" user, they would be borked. Simple oversight, but wow it hecked with me. :P --- application/ProtocolLooper.cpp | 4 +++- application/Server.cpp | 11 +++++++++++ application/User.cpp | 13 ------------- application/User.h | 5 ----- application/views/ConversationView.cpp | 5 ++--- application/views/UserItem.cpp | 15 ++++++++++----- application/views/UserItem.h | 15 +++++++-------- application/views/UserListView.cpp | 26 ++++++++++++++++++++++++++ application/views/UserListView.h | 6 ++++++ 9 files changed, 65 insertions(+), 35 deletions(-) diff --git a/application/ProtocolLooper.cpp b/application/ProtocolLooper.cpp index cafa399..007a41d 100644 --- a/application/ProtocolLooper.cpp +++ b/application/ProtocolLooper.cpp @@ -193,8 +193,10 @@ ProtocolLooper::GetOwnContact() void ProtocolLooper::SetOwnContact(Contact* contact) { - if (contact != NULL) + if (contact != NULL) { fMySelf = contact; + AddUser(contact); + } } diff --git a/application/Server.cpp b/application/Server.cpp index 543b94a..85abcbe 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -279,6 +279,17 @@ Server::ImMessage(BMessage* msg) } break; } + case IM_OWN_NICKNAME_SET: + { + BString nick = msg->FindString("user_name"); + ProtocolLooper* looper = _LooperFromMessage(msg); + if (looper == NULL) + break; + Contact* contact = looper->GetOwnContact(); + if (contact != NULL) + contact->SetNotifyName(nick.String()); + break; + } case IM_STATUS_SET: { int32 status; diff --git a/application/User.cpp b/application/User.cpp index cefcf1f..4fbd8b4 100644 --- a/application/User.cpp +++ b/application/User.cpp @@ -22,7 +22,6 @@ #include "NotifyMessage.h" #include "ProtocolLooper.h" #include "ProtocolManager.h" -#include "UserItem.h" #include "UserPopUp.h" #include "Utils.h" @@ -33,7 +32,6 @@ User::User(BString id, BMessenger msgn) fName(id), fMessenger(msgn), fLooper(NULL), - fListItem(NULL), fItemColor(ForegroundColor(ui_color(B_LIST_BACKGROUND_COLOR))), fStatus(STATUS_ONLINE), fAvatarBitmap(NULL), @@ -148,17 +146,6 @@ User::ProtocolBitmap() const } -UserItem* -User::GetListItem() -{ - if (fListItem == NULL) { - fListItem = new UserItem(fName, this, (int32)fStatus); - RegisterObserver(fListItem); - } - return fListItem; -} - - UserStatus User::GetNotifyStatus() const { diff --git a/application/User.h b/application/User.h index 129b49d..967afd8 100644 --- a/application/User.h +++ b/application/User.h @@ -22,7 +22,6 @@ class BBitmap; class Conversation; class ProtocolLooper; -class UserItem; class UserPopUp; @@ -51,8 +50,6 @@ public: void SetProtocolLooper(ProtocolLooper* looper); BBitmap* ProtocolBitmap() const; - UserItem* GetListItem(); - BString GetName() const; BBitmap* AvatarBitmap() const; UserStatus GetNotifyStatus() const; @@ -76,8 +73,6 @@ protected: BMessenger fMessenger; ProtocolLooper* fLooper; - UserItem* fListItem; - BString fID; BString fName; BString fPersonalStatus; diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index b8ae76a..cda259f 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -30,7 +30,6 @@ #include "RenderView.h" #include "SendTextView.h" #include "User.h" -#include "UserItem.h" #include "UserListView.h" #include "Utils.h" @@ -273,8 +272,8 @@ ConversationView::UpdateUserList(UserMap users) fUserList->MakeEmpty(); for (int i = 0; i < users.CountItems(); i++) { User* user = users.ValueAt(i); - if (fUserList->HasItem(user->GetListItem()) == false) { - fUserList->AddItem(user->GetListItem()); + if (fUserList->HasUser(user) == false) { + fUserList->AddUser(user); fUserList->Sort(); } } diff --git a/application/views/UserItem.cpp b/application/views/UserItem.cpp index 7f61e26..5c80430 100644 --- a/application/views/UserItem.cpp +++ b/application/views/UserItem.cpp @@ -14,12 +14,19 @@ #include "Utils.h" -UserItem::UserItem(const char* name, User* user, int32 status) +UserItem::UserItem(User* user) : - BStringItem(name), + BStringItem(user->GetName()), fUser(user), - fStatus(status) + fStatus(user->GetNotifyStatus()) { + user->RegisterObserver(this); +} + + +UserItem::~UserItem() +{ + fUser->UnregisterObserver(this); } @@ -81,5 +88,3 @@ UserItem::_GetTextColor(rgb_color highColor) } return highColor; } - - diff --git a/application/views/UserItem.h b/application/views/UserItem.h index c9d5b9c..9cd59e3 100644 --- a/application/views/UserItem.h +++ b/application/views/UserItem.h @@ -15,23 +15,22 @@ class User; class UserItem : public BStringItem, public Observer { public: - UserItem(const char* name, User* user, int32 status); + UserItem(User* user); + ~UserItem(); - void DrawItem(BView* owner, BRect frame, bool complete); + virtual void DrawItem(BView* owner, BRect frame, bool complete); - void ObserveString(int32 what, BString str); - void ObserveInteger(int32 what, int32 value); + virtual void ObserveString(int32 what, BString str); + virtual void ObserveInteger(int32 what, int32 value); - User* GetUser(); + User* GetUser(); protected: - rgb_color _GetTextColor(rgb_color highColor); + rgb_color _GetTextColor(rgb_color highColor); private: User* fUser; int fStatus; }; - #endif // USERITEM_H - diff --git a/application/views/UserListView.cpp b/application/views/UserListView.cpp index 72ebe8a..2733b1c 100644 --- a/application/views/UserListView.cpp +++ b/application/views/UserListView.cpp @@ -69,6 +69,32 @@ UserListView::Sort() } +bool +UserListView::HasUser(User* user) +{ + for (int i = 0; i < CountItems(); i++) + if (user == ((UserItem*)ItemAt(i))->GetUser()) + return true; + return false; +} + + +void +UserListView::AddUser(User* user) +{ + AddItem(new UserItem(user)); +} + + +void +UserListView::RemoveUser(User* user) +{ + for (int i = 0; i < CountItems(); i++) + if (user == ((UserItem*)ItemAt(i))->GetUser()) + RemoveItem(i); +} + + BPopUpMenu* UserListView::_UserPopUp() { diff --git a/application/views/UserListView.h b/application/views/UserListView.h index f3810a3..a79872d 100644 --- a/application/views/UserListView.h +++ b/application/views/UserListView.h @@ -10,7 +10,9 @@ #include "Role.h" class BPopUpMenu; + class Conversation; +class User; enum @@ -33,6 +35,10 @@ public: void Sort(); + bool HasUser(User* user); + void AddUser(User* user); + void RemoveUser(User* user); + void SetConversation(Conversation* chat) { fChat = chat; } private: