From fe4d0661a7234736cb332bd5ca1f23e1b71c52d3 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Fri, 16 Jul 2021 16:28:58 -0500 Subject: [PATCH] Move notifications from ConversationView to Conversation itself --- application/Conversation.cpp | 47 +++++++++++++++++++++++++- application/Conversation.h | 1 + application/views/ConversationView.cpp | 47 +------------------------- application/views/ConversationView.h | 1 - 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/application/Conversation.cpp b/application/Conversation.cpp index db282d3..c25df99 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -7,9 +7,12 @@ #include #include +#include +#include #include #include "AppPreferences.h" +#include "Cardie.h" #include "ChatProtocolMessages.h" #include "RenderView.h" #include "ChatCommand.h" @@ -35,7 +38,8 @@ Conversation::Conversation(BString id, BMessenger msgn) fIcon(NULL), fDateFormatter(), fRoomFlags(0), - fDisallowedFlags(0) + fDisallowedFlags(0), + fNotifyCount(0) { fConversationItem = new ConversationItem(fName.String(), this); RegisterObserver(fConversationItem); @@ -74,6 +78,47 @@ Conversation::ImMessage(BMessage* msg) _EnsureUser(msg); _LogChatMessage(msg); GetView()->MessageReceived(msg); + + BString text = msg->FindString("body"); + Contact* contact = GetOwnContact(); + bool mentioned = ((text.IFindFirst(contact->GetName()) != B_ERROR) + || (text.IFindFirst(contact->GetName()) != B_ERROR)); + + // Send a notification, if it's appropriate + BWindow* win = fChatView->Window(); + if ((win == NULL || !win->IsFront() || win->IsMinimized()) + && AppPreferences::Item()->NotifyNewMessage + && (fUsers.CountItems() <= 2 || mentioned)) + { + fNotifyCount++; + + BString notifyTitle = "New mention"; + BString notifyText = "You've been summoned from %source%."; + + if (mentioned == false) { + notifyTitle.SetTo("New message"); + notifyText.SetTo(""); + + BStringFormat pmFormat("{0, plural," + "=1{You've got a new message from %source%.}" + "other{You've got # new messages from %source%.}}"); + pmFormat.Format(notifyText, fNotifyCount); + } + notifyText.ReplaceAll("%source%", GetName()); + + BBitmap* icon = IconBitmap(); + if (icon == NULL) + icon = ProtocolBitmap(); + + + BNotification notification(B_INFORMATION_NOTIFICATION); + notification.SetGroup(BString(APP_NAME)); + notification.SetTitle(notifyTitle); + notification.SetIcon(icon); + notification.SetContent(notifyText); + notification.SetMessageID(fID); + notification.Send(); + } break; } case IM_MESSAGE_SENT: diff --git a/application/Conversation.h b/application/Conversation.h index d9f27de..5ce59b7 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -90,6 +90,7 @@ private: ProtocolLooper* fLooper; ConversationView* fChatView; ConversationItem* fConversationItem; + int32 fNotifyCount; BString fID; BString fName; diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index 8402d10..fac611a 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -12,9 +12,7 @@ #include #include -#include #include -#include #include #include @@ -22,11 +20,10 @@ #include "AppMessages.h" #include "AppPreferences.h" -#include "Cardie.h" #include "ChatProtocolMessages.h" -#include "RenderView.h" #include "Conversation.h" #include "NotifyMessage.h" +#include "RenderView.h" #include "SendTextView.h" #include "User.h" #include "UserItem.h" @@ -40,7 +37,6 @@ ConversationView::ConversationView() fMessageQueue(), fConversation(NULL) { - fMessageCount = 0; _InitInterface(); fSendView->MakeFocus(true); } @@ -132,47 +128,6 @@ ConversationView::ImMessage(BMessage* msg) } case IM_MESSAGE_RECEIVED: { - BString text = msg->FindString("body"); - Contact* contact = fConversation->GetOwnContact(); - bool mentioned = ((text.IFindFirst(contact->GetName()) != B_ERROR) - || (text.IFindFirst(contact->GetName()) != B_ERROR)); - - // Send a notification, if it's appropriate - BWindow* win = Window(); - if ((win == NULL || !win->IsFront() || win->IsMinimized()) - && AppPreferences::Item()->NotifyNewMessage - && (fConversation->Users().CountItems() <= 2 || mentioned)) - { - fMessageCount++; - - BString notifyTitle = "New mention"; - BString notifyText = "You've been summoned from %source%."; - - if (mentioned == false) { - notifyTitle.SetTo("New message"); - notifyText.SetTo(""); - - BStringFormat pmFormat("{0, plural," - "=1{You've got a new message from %source%.}" - "other{You've got # new messages from %source%.}}"); - pmFormat.Format(notifyText, fMessageCount); - } - notifyText.ReplaceAll("%source%", fConversation->GetName()); - - BBitmap* icon = fConversation->IconBitmap(); - if (icon == NULL) - icon = fConversation->ProtocolBitmap(); - - - BNotification notification(B_INFORMATION_NOTIFICATION); - notification.SetGroup(BString(APP_NAME)); - notification.SetTitle(notifyTitle); - notification.SetIcon(icon); - notification.SetContent(notifyText); - notification.SetMessageID(fConversation->GetId()); - notification.Send(); - } - _AppendOrEnqueueMessage(msg); break; } diff --git a/application/views/ConversationView.h b/application/views/ConversationView.h index c858540..cc9fa21 100644 --- a/application/views/ConversationView.h +++ b/application/views/ConversationView.h @@ -55,7 +55,6 @@ private: BMessage* msg); Conversation* fConversation; - int32 fMessageCount; BObjectList fMessageQueue; BTextView* fNameTextView;