2021-05-28 08:38:33 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ConversationView.h"
|
|
|
|
|
|
|
|
#include <LayoutBuilder.h>
|
2021-05-31 10:50:43 -05:00
|
|
|
#include <ListView.h>
|
2021-05-28 22:26:32 -05:00
|
|
|
#include <Notification.h>
|
2021-05-28 08:38:33 -05:00
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <StringList.h>
|
2021-05-31 10:50:43 -05:00
|
|
|
#include <StringView.h>
|
2021-05-28 08:38:33 -05:00
|
|
|
|
|
|
|
#include <libinterface/BitmapView.h>
|
|
|
|
|
|
|
|
#include "CayaMessages.h"
|
|
|
|
#include "CayaPreferences.h"
|
|
|
|
#include "CayaProtocolMessages.h"
|
|
|
|
#include "CayaRenderView.h"
|
2021-06-04 13:57:04 -05:00
|
|
|
#include "CayaUtils.h"
|
2021-05-28 08:38:33 -05:00
|
|
|
#include "Conversation.h"
|
|
|
|
#include "NotifyMessage.h"
|
2021-05-31 11:56:45 -05:00
|
|
|
#include "User.h"
|
2021-05-31 10:50:43 -05:00
|
|
|
#include "UserItem.h"
|
|
|
|
#include "UserListView.h"
|
2021-05-28 08:38:33 -05:00
|
|
|
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
ConversationView::ConversationView()
|
2021-05-28 08:38:33 -05:00
|
|
|
:
|
|
|
|
BGroupView("chatView", B_VERTICAL, B_USE_DEFAULT_SPACING),
|
2021-05-28 22:26:32 -05:00
|
|
|
fMessageQueue()
|
2021-05-28 08:38:33 -05:00
|
|
|
{
|
|
|
|
fMessageCount = 0;
|
2021-05-31 10:50:43 -05:00
|
|
|
_InitInterface();
|
2021-05-28 08:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
ConversationView::ConversationView(Conversation* chat)
|
|
|
|
: ConversationView()
|
|
|
|
{
|
|
|
|
SetConversation(chat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 08:38:33 -05:00
|
|
|
bool
|
|
|
|
ConversationView::QuitRequested()
|
|
|
|
{
|
|
|
|
BMessage msg(CAYA_CLOSE_CHAT_WINDOW);
|
|
|
|
msg.AddString("chat_id", fConversation->GetId());
|
|
|
|
fConversation->Messenger().SendMessage(&msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
void
|
2021-05-31 10:50:43 -05:00
|
|
|
ConversationView::AttachedToWindow()
|
2021-05-28 08:38:33 -05:00
|
|
|
{
|
2021-05-31 10:50:43 -05:00
|
|
|
while (fMessageQueue.IsEmpty() == false) {
|
|
|
|
BMessage* msg = fMessageQueue.RemoveItemAt(0);
|
|
|
|
ImMessage(msg);
|
|
|
|
}
|
2021-05-28 08:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationView::MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch (message->what) {
|
|
|
|
case CAYA_CHAT:
|
|
|
|
{
|
|
|
|
BString text = message->FindString("body");
|
|
|
|
if (text == "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
BMessage msg(IM_MESSAGE);
|
|
|
|
msg.AddInt32("im_what", IM_SEND_MESSAGE);
|
|
|
|
msg.AddString("chat_id", fConversation->GetId());
|
|
|
|
msg.AddString("body", text);
|
|
|
|
fConversation->ImMessage(&msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_MESSAGE:
|
|
|
|
ImMessage(message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BGroupView::MessageReceived(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationView::ImMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
|
|
|
|
|
|
|
switch (im_what) {
|
2021-06-04 16:32:18 -05:00
|
|
|
case IM_ROOM_LEFT:
|
|
|
|
{
|
|
|
|
delete fConversation;
|
|
|
|
delete this;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-28 08:38:33 -05:00
|
|
|
case IM_MESSAGE_RECEIVED:
|
|
|
|
{
|
|
|
|
BString message = msg->FindString("body");
|
|
|
|
BString id = msg->FindString("user_id");
|
|
|
|
User* sender = fConversation->UserById(id);
|
|
|
|
BString uname = sender->GetName();
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
// Send a notification, if it's appropriate
|
|
|
|
if ((Window() == NULL || Window()->IsActive() == false)
|
|
|
|
&& (!CayaPreferences::Item()->NotifyNewMessage))
|
|
|
|
{
|
|
|
|
fMessageCount++;
|
|
|
|
BString notify_message;
|
|
|
|
notify_message << "You've got ";
|
|
|
|
notify_message << fMessageCount;
|
|
|
|
if (fMessageCount==1)
|
|
|
|
notify_message << " new message from ";
|
|
|
|
else
|
|
|
|
notify_message << " new messages from ";
|
|
|
|
notify_message << uname;
|
|
|
|
|
|
|
|
BNotification notification(B_INFORMATION_NOTIFICATION);
|
|
|
|
notification.SetGroup(BString("Caya"));
|
|
|
|
notification.SetTitle(BString("New message"));
|
|
|
|
notification.SetIcon(sender->AvatarBitmap());
|
|
|
|
notification.SetContent(notify_message);
|
|
|
|
notification.SetMessageID(uname);
|
|
|
|
notification.Send();
|
|
|
|
// Check if the user want the notification
|
|
|
|
if (!CayaPreferences::Item()->NotifyNewMessage)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-30 19:07:50 -05:00
|
|
|
_AppendOrEnqueueMessage(msg);
|
2021-05-28 08:38:33 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_MESSAGE_SENT:
|
2021-05-28 08:38:33 -05:00
|
|
|
case IM_LOGS_RECEIVED:
|
|
|
|
{
|
2021-05-30 19:07:50 -05:00
|
|
|
_AppendOrEnqueueMessage(msg);
|
2021-05-28 08:38:33 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
Conversation*
|
|
|
|
ConversationView::GetConversation()
|
|
|
|
{
|
|
|
|
return fConversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationView::SetConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
fConversation = chat;
|
2021-06-04 13:57:04 -05:00
|
|
|
fNameTextView->SetText(chat->GetName());
|
|
|
|
fProtocolView->SetBitmap(chat->ProtocolBitmap());
|
2021-05-31 10:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-04 13:57:04 -05:00
|
|
|
ConversationView::UpdateIcon()
|
2021-05-31 10:50:43 -05:00
|
|
|
{
|
2021-06-04 13:57:04 -05:00
|
|
|
if (fConversation->IconBitmap() != NULL)
|
|
|
|
fIcon->SetBitmap(fConversation->IconBitmap());
|
2021-05-31 10:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 08:38:33 -05:00
|
|
|
void
|
2021-06-03 23:39:50 -05:00
|
|
|
ConversationView::InvalidateUserList()
|
2021-05-28 08:38:33 -05:00
|
|
|
{
|
2021-06-03 23:39:50 -05:00
|
|
|
for (int i = 0; i < fUserList->CountItems(); i++)
|
|
|
|
fUserList->InvalidateItem(i);
|
2021-05-28 08:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-03 23:39:50 -05:00
|
|
|
ConversationView::ObserveString(int32 what, BString str)
|
2021-05-28 08:38:33 -05:00
|
|
|
{
|
2021-06-04 13:57:04 -05:00
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case STR_ROOM_SUBJECT:
|
|
|
|
{
|
|
|
|
fSubjectTextView->SetText(str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 08:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
void
|
|
|
|
ConversationView::_InitInterface()
|
|
|
|
{
|
|
|
|
fReceiveView = new CayaRenderView("fReceiveView");
|
|
|
|
BScrollView* scrollViewReceive = new BScrollView("receiveScrollView",
|
|
|
|
fReceiveView, B_WILL_DRAW, false, true);
|
|
|
|
|
2021-06-04 13:57:04 -05:00
|
|
|
fNameTextView = new BTextView("roomName", B_WILL_DRAW);
|
|
|
|
fNameTextView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
|
|
|
|
fNameTextView->MakeEditable(false);
|
2021-05-31 10:50:43 -05:00
|
|
|
|
2021-06-04 13:57:04 -05:00
|
|
|
fSubjectTextView = new BTextView("roomSubject", B_WILL_DRAW);
|
|
|
|
fSubjectTextView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
|
|
|
|
fSubjectTextView->MakeEditable(false);
|
2021-05-31 10:50:43 -05:00
|
|
|
|
2021-06-04 13:57:04 -05:00
|
|
|
fIcon = new BitmapView("ContactIcon");
|
|
|
|
fIcon->SetExplicitMinSize(BSize(50, 50));
|
|
|
|
fIcon->SetExplicitPreferredSize(BSize(50, 50));
|
|
|
|
fIcon->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE));
|
2021-05-31 10:50:43 -05:00
|
|
|
|
|
|
|
fProtocolView = new BitmapView("protocolView");
|
|
|
|
|
|
|
|
fUserList = new UserListView("userList");
|
|
|
|
BScrollView* scrollViewUsers = new BScrollView("userScrollView",
|
|
|
|
fUserList, B_WILL_DRAW, false, true);
|
|
|
|
|
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
|
|
|
.AddGroup(B_HORIZONTAL)
|
2021-06-04 13:57:04 -05:00
|
|
|
.Add(fIcon)
|
|
|
|
.AddGroup(B_VERTICAL)
|
|
|
|
.Add(fNameTextView)
|
|
|
|
.Add(fSubjectTextView)
|
|
|
|
.End()
|
2021-05-31 10:50:43 -05:00
|
|
|
.Add(fProtocolView)
|
|
|
|
.End()
|
|
|
|
.AddSplit(B_HORIZONTAL, 0)
|
|
|
|
.Add(scrollViewReceive, 5)
|
|
|
|
.Add(scrollViewUsers, 1)
|
|
|
|
.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 19:07:50 -05:00
|
|
|
bool
|
|
|
|
ConversationView::_AppendOrEnqueueMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
// If not attached to the chat window, then re-handle this message
|
|
|
|
// later [AttachedToWindow()], since you can't edit an unattached
|
|
|
|
// RenderView.
|
|
|
|
if (Window() == NULL) {
|
|
|
|
fMessageQueue.AddItem(new BMessage(*msg));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright, we're good to append!
|
|
|
|
_AppendMessage(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationView::_AppendMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
|
|
|
|
BString message = msg->FindString("body");
|
|
|
|
BString id = msg->FindString("user_id");
|
|
|
|
BString uname = "";
|
|
|
|
|
|
|
|
if (id.IsEmpty() == false) {
|
|
|
|
User* sender = fConversation->UserById(id);
|
|
|
|
|
|
|
|
if (sender == NULL || (uname = sender->GetName()) == NULL)
|
|
|
|
uname = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->FindInt32("im_what") == IM_MESSAGE_SENT)
|
|
|
|
fReceiveView->AppendOwnMessage(message.String());
|
|
|
|
|
|
|
|
else if (uname.IsEmpty() == false)
|
|
|
|
fReceiveView->AppendOtherMessage(uname.String(), message.String());
|
|
|
|
|
|
|
|
else {
|
|
|
|
BStringList bodies;
|
|
|
|
if (msg->FindStrings("body", &bodies) != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = bodies.CountStrings(); i >= 0; i--)
|
|
|
|
fReceiveView->AppendGenericMessage(bodies.StringAt(i).String());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|