2010-05-07 04:47:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009, Andrea Anzani. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <ListView.h>
|
|
|
|
#include <Message.h>
|
|
|
|
#include <SpaceLayoutItem.h>
|
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <String.h>
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "CayaMessages.h"
|
2010-05-19 15:37:26 -05:00
|
|
|
#include "CayaProtocolMessages.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "ChatWindow.h"
|
|
|
|
#include "ContactLinker.h"
|
|
|
|
#include "EditingFilter.h"
|
|
|
|
#include "CayaConstants.h"
|
|
|
|
#include "CayaRenderView.h"
|
|
|
|
#include "NotifyMessage.h"
|
|
|
|
|
|
|
|
|
|
|
|
ChatWindow::ChatWindow(ContactLinker* cl)
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
|
|
|
BWindow(BRect(200, 200, 500, 500),
|
2010-05-07 04:47:10 -05:00
|
|
|
cl->GetName().String(), B_DOCUMENT_WINDOW, 0),
|
|
|
|
fContactLinker(cl)
|
|
|
|
{
|
|
|
|
fReceiveView = new CayaRenderView("fReceiveView");
|
|
|
|
fReceiveView->SetOtherNick(cl->GetName());
|
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);
|
|
|
|
|
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
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
SetLayout(new BGroupLayout(B_HORIZONTAL));
|
|
|
|
|
|
|
|
AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
|
|
|
|
.Add(scrollViewReceive, 2)
|
|
|
|
.Add(scrollViewSend)
|
2010-05-28 17:38:16 -05:00
|
|
|
.Add(fStatus)
|
2010-05-07 04:47:10 -05:00
|
|
|
.SetInsets(5, 5, 5, 5)
|
2010-05-28 17:38:16 -05:00
|
|
|
);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2));
|
|
|
|
|
|
|
|
fSendView->MakeFocus(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ChatWindow::QuitRequested()
|
|
|
|
{
|
2010-05-16 16:02:50 -05:00
|
|
|
BMessage msg(CAYA_CLOSE_WINDOW);
|
2010-05-07 04:47:10 -05:00
|
|
|
msg.AddString("id", fContactLinker->GetId());
|
2010-05-16 16:02:50 -05:00
|
|
|
fContactLinker->Messenger().SendMessage(&msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
msg.AddString("id", fContactLinker->GetId());
|
2010-05-19 15:37:26 -05:00
|
|
|
msg.AddString("body", message);
|
2010-05-16 16:02:50 -05:00
|
|
|
fContactLinker->Messenger().SendMessage(&msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
fSendView->SetText("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_MESSAGE:
|
|
|
|
ImMessage(message);
|
|
|
|
break;
|
|
|
|
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");
|
|
|
|
switch (im_what) {
|
|
|
|
case IM_MESSAGE_RECEIVED:
|
|
|
|
{
|
2010-05-19 15:37:26 -05:00
|
|
|
BString message = msg->FindString("body");
|
2010-05-07 04:47:10 -05:00
|
|
|
fReceiveView->AppendOtherMessage(message.String());
|
2010-05-28 18:05:57 -05:00
|
|
|
// Message received, clear status anyway
|
|
|
|
fStatus->SetText(" ");
|
2010-05-07 04:47:10 -05:00
|
|
|
break;
|
2010-05-28 17:38:16 -05:00
|
|
|
}
|
|
|
|
case IM_CONTACT_STARTED_TYPING:
|
|
|
|
fStatus->SetText("Buddy is typing ...");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_CONTACT_STOPPED_TYPING:
|
2010-05-28 18:05:57 -05:00
|
|
|
fStatus->SetText(" ");
|
2010-05-28 17:38:16 -05:00
|
|
|
break;
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-16 16:02:50 -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);
|
|
|
|
fReceiveView->SetOtherNick(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
|
|
|
{
|
2010-05-07 04:47:10 -05:00
|
|
|
BString message(fContactLinker->GetName());
|
|
|
|
|
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_EXTENDED_AWAY:
|
|
|
|
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;
|
|
|
|
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();
|
|
|
|
}
|