2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2011-12-03 16:38:03 -06:00
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
*/
|
|
|
|
|
2021-05-19 16:12:19 -05:00
|
|
|
#include "ChatWindow.h"
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Alert.h>
|
|
|
|
#include <Application.h>
|
|
|
|
#include <Box.h>
|
|
|
|
#include <Button.h>
|
|
|
|
#include <CheckBox.h>
|
|
|
|
#include <GridLayout.h>
|
|
|
|
#include <GridLayoutBuilder.h>
|
|
|
|
#include <GroupLayout.h>
|
|
|
|
#include <GroupLayoutBuilder.h>
|
|
|
|
#include <Layout.h>
|
2012-11-28 09:20:17 -06:00
|
|
|
#include <LayoutBuilder.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ListView.h>
|
|
|
|
#include <Message.h>
|
|
|
|
#include <SpaceLayoutItem.h>
|
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <String.h>
|
2021-05-24 19:12:42 -05:00
|
|
|
#include <StringList.h>
|
2013-04-06 14:47:04 -05:00
|
|
|
#include <Notification.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-05-19 16:12:19 -05:00
|
|
|
#include <libinterface/BitmapView.h>
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "CayaMessages.h"
|
2010-05-19 15:37:26 -05:00
|
|
|
#include "CayaProtocolMessages.h"
|
2013-04-06 14:47:04 -05:00
|
|
|
#include "CayaPreferences.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "Conversation.h"
|
2021-05-23 14:39:07 -05:00
|
|
|
#include "Contact.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "EditingFilter.h"
|
|
|
|
#include "CayaConstants.h"
|
|
|
|
#include "CayaRenderView.h"
|
|
|
|
#include "NotifyMessage.h"
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
ChatWindow::ChatWindow(Conversation* cl)
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
|
|
|
BWindow(BRect(200, 200, 500, 500),
|
2012-05-15 11:48:53 -05:00
|
|
|
cl->GetName().String(), B_TITLED_WINDOW, 0),
|
2021-05-24 01:47:21 -05:00
|
|
|
fConversation(cl),
|
|
|
|
fContact(cl->Users().ValueAt(0))
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2013-07-30 13:54:22 -05:00
|
|
|
fMessageCount = 0;
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
fReceiveView = new CayaRenderView("fReceiveView");
|
2010-05-20 16:31:55 -05:00
|
|
|
BScrollView* scrollViewReceive = new BScrollView("scrollviewR",
|
|
|
|
fReceiveView, B_WILL_DRAW, false, true);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
fSendView = new BTextView("fReceiveView");
|
|
|
|
BScrollView* scrollViewSend = new BScrollView("scrollviewS", fSendView,
|
2010-05-28 17:38:16 -05:00
|
|
|
B_WILL_DRAW, false, true);
|
2010-05-07 04:47:10 -05:00
|
|
|
fSendView->SetWordWrap(true);
|
|
|
|
AddCommonFilter(new EditingFilter(fSendView));
|
|
|
|
fSendView->MakeFocus(true);
|
|
|
|
|
2012-03-07 18:47:00 -06:00
|
|
|
|
|
|
|
fPersonalMessage = new BTextView("personalMessage", B_WILL_DRAW);
|
2012-05-15 11:48:53 -05:00
|
|
|
fPersonalMessage->SetExplicitAlignment(
|
|
|
|
BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
|
|
|
|
|
2021-05-23 14:39:07 -05:00
|
|
|
fPersonalMessage->SetText(fContact->GetNotifyPersonalStatus());
|
2012-03-07 18:47:00 -06:00
|
|
|
fPersonalMessage->SetExplicitMaxSize(BSize(400, 200));
|
|
|
|
fPersonalMessage->MakeEditable(false);
|
|
|
|
fPersonalMessage->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
|
|
|
|
2012-03-07 13:22:09 -06:00
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
fStatus = new BStringView("status", "");
|
2010-05-28 18:05:57 -05:00
|
|
|
fStatus->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
|
2010-05-28 17:38:16 -05:00
|
|
|
|
2012-03-07 13:22:09 -06:00
|
|
|
fAvatar = new BitmapView("ContactIcon");
|
|
|
|
fAvatar->SetExplicitMaxSize(BSize(50, 50));
|
2012-03-07 18:47:00 -06:00
|
|
|
fAvatar->SetExplicitMinSize(BSize(50, 50));
|
2012-03-07 13:22:09 -06:00
|
|
|
fAvatar->SetExplicitPreferredSize(BSize(50, 50));
|
|
|
|
fAvatar->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE));
|
2021-05-23 14:39:07 -05:00
|
|
|
fAvatar->SetBitmap(fContact->AvatarBitmap());
|
2012-03-07 13:22:09 -06:00
|
|
|
|
2021-05-23 14:39:07 -05:00
|
|
|
BBitmap* protocolBitmap = fContact->ProtocolBitmap();
|
2012-05-01 17:42:09 -05:00
|
|
|
BitmapView* protocolView = new BitmapView("protocolView");
|
|
|
|
protocolView->SetBitmap(protocolBitmap);
|
|
|
|
|
2012-11-28 09:20:17 -06:00
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
|
2012-03-07 18:47:00 -06:00
|
|
|
.AddGroup(B_HORIZONTAL)
|
2012-05-01 17:42:09 -05:00
|
|
|
.Add(protocolView)
|
2012-03-07 18:47:00 -06:00
|
|
|
.Add(fPersonalMessage)
|
|
|
|
.Add(fAvatar)
|
|
|
|
.End()
|
2013-01-04 11:37:53 -06:00
|
|
|
.AddSplit(B_VERTICAL, 2.0f)
|
|
|
|
.Add(scrollViewReceive, 3)
|
|
|
|
.Add(scrollViewSend, 2)
|
2012-11-28 09:20:17 -06:00
|
|
|
.End()
|
2012-05-15 11:48:53 -05:00
|
|
|
.Add(fStatus, 4)
|
2012-11-28 09:20:17 -06:00
|
|
|
.SetInsets(5, 5, 5, 5);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2));
|
|
|
|
|
|
|
|
fSendView->MakeFocus(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-07 11:46:07 -05:00
|
|
|
void
|
|
|
|
ChatWindow::ShowWindow()
|
|
|
|
{
|
|
|
|
if (IsHidden())
|
|
|
|
Show();
|
|
|
|
|
|
|
|
if (IsMinimized())
|
|
|
|
Minimize(false);
|
|
|
|
|
|
|
|
if (!IsActive())
|
|
|
|
Activate(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
bool
|
|
|
|
ChatWindow::QuitRequested()
|
|
|
|
{
|
2011-12-14 17:36:27 -06:00
|
|
|
BMessage msg(CAYA_CLOSE_CHAT_WINDOW);
|
2021-05-24 01:47:21 -05:00
|
|
|
msg.AddString("chat_id", fConversation->GetId());
|
|
|
|
fConversation->Messenger().SendMessage(&msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-07 13:22:09 -06:00
|
|
|
void
|
|
|
|
ChatWindow::UpdateAvatar()
|
|
|
|
{
|
2021-05-23 14:39:07 -05:00
|
|
|
if (fContact->AvatarBitmap() != NULL) {
|
2012-03-07 19:17:32 -06:00
|
|
|
LockLooper();
|
2021-05-23 14:39:07 -05:00
|
|
|
fAvatar->SetBitmap(fContact->AvatarBitmap());
|
2012-03-07 19:17:32 -06:00
|
|
|
UnlockLooper();
|
|
|
|
}
|
2012-03-07 18:47:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ChatWindow::UpdatePersonalMessage()
|
|
|
|
{
|
2021-05-23 14:39:07 -05:00
|
|
|
if (fContact->GetNotifyPersonalStatus() != NULL) {
|
2012-03-07 18:47:00 -06:00
|
|
|
LockLooper();
|
2021-05-23 14:39:07 -05:00
|
|
|
fPersonalMessage->SetText(fContact->GetNotifyPersonalStatus());
|
2012-03-07 18:47:00 -06:00
|
|
|
UnlockLooper();
|
|
|
|
}
|
2012-03-07 13:22:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
void
|
|
|
|
ChatWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
2010-05-19 17:28:26 -05:00
|
|
|
switch (message->what) {
|
2010-05-16 16:02:50 -05:00
|
|
|
case CAYA_CHAT:
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
BString message = fSendView->Text();
|
|
|
|
if (message == "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
fReceiveView->AppendOwnMessage(message.String());
|
|
|
|
|
|
|
|
BMessage msg(IM_MESSAGE);
|
|
|
|
msg.AddInt32("im_what", IM_SEND_MESSAGE);
|
2021-05-24 01:47:21 -05:00
|
|
|
msg.AddString("chat_id", fConversation->GetId());
|
2010-05-19 15:37:26 -05:00
|
|
|
msg.AddString("body", message);
|
2021-05-24 14:20:57 -05:00
|
|
|
fConversation->ImMessage(&msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
fSendView->SetText("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_MESSAGE:
|
|
|
|
ImMessage(message);
|
|
|
|
break;
|
2012-06-07 11:46:07 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(message);
|
|
|
|
break;
|
2010-05-28 17:38:16 -05:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ChatWindow::ImMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
2010-05-30 17:00:40 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
switch (im_what) {
|
|
|
|
case IM_MESSAGE_RECEIVED:
|
|
|
|
{
|
2010-05-19 15:37:26 -05:00
|
|
|
BString message = msg->FindString("body");
|
2021-05-24 01:47:21 -05:00
|
|
|
BString id = msg->FindString("user_id");
|
|
|
|
User* sender = fConversation->UserById(id);
|
|
|
|
BString uname = sender->GetName();
|
|
|
|
|
|
|
|
fReceiveView->AppendOtherMessage(uname.String(), message.String());
|
2010-05-30 17:00:40 -05:00
|
|
|
|
2010-05-28 18:05:57 -05:00
|
|
|
// Message received, clear status anyway
|
2010-05-30 17:00:40 -05:00
|
|
|
fStatus->SetText("");
|
2013-04-06 14:47:04 -05:00
|
|
|
|
|
|
|
if (IsActive()) break;
|
|
|
|
|
2013-07-30 13:54:22 -05:00
|
|
|
fMessageCount++;
|
|
|
|
|
2013-04-06 14:47:04 -05:00
|
|
|
// Mark unread window
|
|
|
|
if (CayaPreferences::Item()->MarkUnreadWindow) {
|
2013-07-30 13:54:22 -05:00
|
|
|
BString title = "[";
|
2021-05-24 01:47:21 -05:00
|
|
|
title << fMessageCount;
|
|
|
|
title << "] ";
|
|
|
|
title << uname;
|
2013-07-30 13:54:22 -05:00
|
|
|
SetTitle(title);
|
2013-04-06 14:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user want the notification
|
|
|
|
if (!CayaPreferences::Item()->NotifyNewMessage)
|
|
|
|
break;
|
|
|
|
|
|
|
|
BString notify_message;
|
2013-07-30 13:54:22 -05:00
|
|
|
notify_message << "You've got ";
|
|
|
|
notify_message << fMessageCount;
|
|
|
|
if (fMessageCount==1) {
|
|
|
|
notify_message << " new message from ";
|
|
|
|
} else {
|
|
|
|
notify_message << " new messages from ";
|
|
|
|
};
|
2021-05-24 01:47:21 -05:00
|
|
|
notify_message << uname;
|
2013-04-06 14:47:04 -05:00
|
|
|
|
|
|
|
BNotification notification(B_INFORMATION_NOTIFICATION);
|
|
|
|
notification.SetGroup(BString("Caya"));
|
|
|
|
notification.SetTitle(BString("New message"));
|
2021-05-24 01:47:21 -05:00
|
|
|
notification.SetIcon(sender->AvatarBitmap());
|
2013-04-06 14:47:04 -05:00
|
|
|
notification.SetContent(notify_message);
|
2021-05-24 01:47:21 -05:00
|
|
|
notification.SetMessageID(uname);
|
2013-04-06 14:47:04 -05:00
|
|
|
notification.Send();
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
2010-05-28 17:38:16 -05:00
|
|
|
}
|
2021-05-24 19:12:42 -05:00
|
|
|
case IM_LOGS_RECEIVED:
|
|
|
|
{
|
|
|
|
BStringList logs;
|
|
|
|
if (msg->FindStrings("log", &logs) != B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = logs.CountStrings(); i >= 0; i--)
|
|
|
|
fReceiveView->AppendGenericMessage(logs.StringAt(i).String());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-05-28 17:38:16 -05:00
|
|
|
case IM_CONTACT_STARTED_TYPING:
|
2010-05-30 17:00:40 -05:00
|
|
|
fStatus->SetText("Contact is typing...");
|
2010-05-28 17:38:16 -05:00
|
|
|
break;
|
|
|
|
case IM_CONTACT_STOPPED_TYPING:
|
2010-05-30 17:00:40 -05:00
|
|
|
fStatus->SetText("");
|
2010-05-28 17:38:16 -05:00
|
|
|
break;
|
2010-10-24 01:26:01 -05:00
|
|
|
case IM_CONTACT_GONE:
|
|
|
|
fStatus->SetText("Contact closed the chat window!");
|
|
|
|
snooze(10000);
|
|
|
|
fStatus->SetText("");
|
|
|
|
break;
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
|
2013-04-06 14:47:04 -05:00
|
|
|
void
|
|
|
|
ChatWindow::WindowActivated(bool active)
|
|
|
|
{
|
2021-05-23 14:39:07 -05:00
|
|
|
SetTitle(fContact->GetName());
|
2013-07-30 13:54:22 -05:00
|
|
|
fMessageCount=0;
|
2013-04-06 14:47:04 -05:00
|
|
|
}
|
2010-05-16 16:02:50 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
ChatWindow::ObserveString(int32 what, BString str)
|
|
|
|
{
|
2010-05-19 17:28:26 -05:00
|
|
|
switch (what) {
|
2010-05-07 04:47:10 -05:00
|
|
|
case STR_CONTACT_NAME:
|
2010-05-20 16:31:55 -05:00
|
|
|
if (Lock()) {
|
2010-05-07 04:47:10 -05:00
|
|
|
SetTitle(str);
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STR_PERSONAL_STATUS:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
ChatWindow::ObservePointer(int32 what, void* ptr)
|
|
|
|
{
|
|
|
|
switch (what) {
|
2010-05-28 17:38:16 -05:00
|
|
|
case PTR_AVATAR_BITMAP:
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
ChatWindow::ObserveInteger(int32 what, int32 val)
|
|
|
|
{
|
|
|
|
switch (what) {
|
|
|
|
case INT_CONTACT_STATUS:
|
|
|
|
if (Lock()) {
|
|
|
|
AppendStatus((CayaStatus)val);
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 17:38:16 -05:00
|
|
|
void
|
2010-05-07 04:47:10 -05:00
|
|
|
ChatWindow::AppendStatus(CayaStatus status)
|
2010-05-28 17:38:16 -05:00
|
|
|
{
|
2021-05-23 14:39:07 -05:00
|
|
|
BString message(fContact->GetName());
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
switch (status) {
|
2010-05-07 04:47:10 -05:00
|
|
|
case CAYA_ONLINE:
|
|
|
|
message << " is available";
|
2010-05-19 17:28:26 -05:00
|
|
|
break;
|
|
|
|
case CAYA_AWAY:
|
2010-05-07 04:47:10 -05:00
|
|
|
message << " is away";
|
2010-05-20 16:31:55 -05:00
|
|
|
break;
|
2010-05-19 17:28:26 -05:00
|
|
|
case CAYA_DO_NOT_DISTURB:
|
2010-05-07 04:47:10 -05:00
|
|
|
message << " is busy, please do not disturb!";
|
2010-05-20 16:31:55 -05:00
|
|
|
break;
|
2012-05-15 12:20:11 -05:00
|
|
|
case CAYA_CUSTOM_STATUS:
|
|
|
|
message << " has set a custom status.";
|
|
|
|
break;
|
|
|
|
case CAYA_INVISIBLE:
|
|
|
|
message << " is invisible.";
|
|
|
|
break;
|
2010-05-20 16:31:55 -05:00
|
|
|
case CAYA_OFFLINE:
|
2010-05-28 17:38:16 -05:00
|
|
|
message << " is offline";
|
2010-05-19 17:28:26 -05:00
|
|
|
break;
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
2010-05-20 16:31:55 -05:00
|
|
|
break;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
2010-05-19 17:28:26 -05:00
|
|
|
fReceiveView->Append(message.String(), COL_TEXT, COL_TEXT, R_TEXT);
|
2010-05-07 04:47:10 -05:00
|
|
|
fReceiveView->Append("\n", COL_TEXT, COL_TEXT, R_TEXT);
|
|
|
|
fReceiveView->ScrollToSelection();
|
|
|
|
}
|
2012-06-07 11:46:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ChatWindow::AvoidFocus(bool avoid)
|
|
|
|
{
|
|
|
|
// This is needed to avoid the window focus when
|
|
|
|
// a new message is received, since it could be a lot annoying
|
|
|
|
// for the user
|
|
|
|
if (avoid)
|
|
|
|
SetFlags(B_AVOID_FOCUS);
|
|
|
|
else
|
|
|
|
SetFlags(Flags() &~ B_AVOID_FOCUS);
|
|
|
|
}
|