From ab6988d9ba18be69aad953b5a0da12acbeebffcd Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 14 Aug 2021 23:37:59 -0500 Subject: [PATCH] Ensure selection & add account buffer to chat list Ensures that an item in the chat list is always selected (as long as there are items to be selected), and that an account's "buffer" (fake conversation) is added immediately after connection, and removed immediately after disconnection. --- application/views/ConversationListView.cpp | 65 ++++++++++++++++++---- application/views/ConversationListView.h | 5 ++ application/windows/MainWindow.cpp | 4 +- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/application/views/ConversationListView.cpp b/application/views/ConversationListView.cpp index d01d51e..ab4b9f8 100644 --- a/application/views/ConversationListView.cpp +++ b/application/views/ConversationListView.cpp @@ -75,19 +75,18 @@ ConversationListView::MessageReceived(BMessage* msg) switch (msg->what) { case kOpenSelectedChat: { - ConversationItem* citem; - ConversationAccountItem* caitem; int32 selIndex = CurrentSelection(); + if (selIndex < 0) + break; - if (selIndex >= 0 - && (citem = (ConversationItem*)ItemAt(selIndex)) != NULL - && citem->OutlineLevel() == 1) + ConversationItem* citem + = dynamic_cast(ItemAt(selIndex)); + ConversationAccountItem* caitem + = dynamic_cast(ItemAt(selIndex)); + + if (citem != NULL) citem->GetConversation()->ShowView(false, true); - - else if (selIndex >= 0 - && (caitem = (ConversationAccountItem*)ItemAt(selIndex)) - != NULL - && caitem->OutlineLevel() == 0) + else if (caitem != NULL) caitem->GetLooper()->ShowView(); break; } @@ -131,6 +130,20 @@ ConversationListView::SelectionChanged() } +void +ConversationListView::RemoveItemSelecting(BListItem* item) +{ + int32 selection = CurrentSelection(); + int32 itemIndex = IndexOf(item); + RemoveItem(item); + + if (itemIndex == selection && CountItems() > selection) + Select(selection); + else if (itemIndex == selection && CountItems() >= 1) + Select(0); +} + + void ConversationListView::AddConversation(Conversation* chat) { @@ -147,7 +160,37 @@ ConversationListView::AddConversation(Conversation* chat) void ConversationListView::RemoveConversation(Conversation* chat) { - RemoveItem(chat->GetListItem()); + RemoveItemSelecting(chat->GetListItem()); +} + + +void +ConversationListView::AddAccount(int64 instance) +{ + Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer(); + ProtocolLooper* looper = server->GetProtocolLooper(instance); + if (looper == NULL) + return; + AddItem(looper->GetListItem()); + if (CurrentSelection() < 0) + Select(0); +} + + +void +ConversationListView::RemoveAccount(int64 instance) +{ + int32 selection = 0; + for (int i = 0; i < CountItems(); i++) { + ConversationAccountItem* item + = dynamic_cast(ItemAt(i)); + if (item != NULL && item->GetInstance() == instance) { + RemoveItemSelecting(item); + break; + } + } + if (CountItems() == 0) + ((TheApp*)be_app)->GetMainWindow()->SetConversation(NULL); } diff --git a/application/views/ConversationListView.h b/application/views/ConversationListView.h index ddb6bdc..9ee8c25 100644 --- a/application/views/ConversationListView.h +++ b/application/views/ConversationListView.h @@ -20,8 +20,13 @@ public: virtual void SelectionChanged(); virtual void MouseDown(BPoint where); + // After removing item, select another + void RemoveItemSelecting(BListItem* item); + void AddConversation(Conversation* chat); void RemoveConversation(Conversation* chat); + void AddAccount(int64 instance); + void RemoveAccount(int64 instance); void SortConversation(Conversation* chat); private: diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index ca709ab..37d5fe2 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -252,6 +252,7 @@ MainWindow::MessageReceived(BMessage* message) case APP_ACCOUNT_DISABLED: { _ToggleMenuItems(); _RefreshAccountsMenu(); + fListView->RemoveAccount(message->GetInt64("instance", -1)); break; } case IM_MESSAGE: @@ -315,10 +316,11 @@ MainWindow::ImMessage(BMessage* msg) } case IM_PROTOCOL_READY: { if (fConversation == NULL) - fChatView->MessageReceived(msg); + fBackupChatView->MessageReceived(msg); fStatusView->MessageReceived(msg); _ToggleMenuItems(); _RefreshAccountsMenu(); + fListView->AddAccount(msg->GetInt64("instance", -1)); break; } case IM_PROTOCOL_DISABLE: