2021-05-24 01:47:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Conversation.h"
|
|
|
|
|
2021-05-24 14:48:25 -05:00
|
|
|
#include <DateTimeFormat.h>
|
|
|
|
#include <Locale.h>
|
2021-05-24 19:12:42 -05:00
|
|
|
#include <StringList.h>
|
2021-05-24 14:48:25 -05:00
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "CayaPreferences.h"
|
|
|
|
#include "CayaProtocolMessages.h"
|
2021-05-28 22:26:32 -05:00
|
|
|
#include "CayaRenderView.h"
|
2021-05-24 14:20:57 -05:00
|
|
|
#include "CayaUtils.h"
|
2021-05-27 11:15:30 -05:00
|
|
|
#include "ConversationItem.h"
|
2021-05-28 22:26:32 -05:00
|
|
|
#include "ConversationView.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "MainWindow.h"
|
2021-05-24 14:20:57 -05:00
|
|
|
#include "ProtocolLooper.h"
|
|
|
|
#include "ProtocolManager.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
|
|
|
#include "WindowsManager.h"
|
|
|
|
|
|
|
|
|
|
|
|
Conversation::Conversation(BString id, BMessenger msgn)
|
|
|
|
:
|
|
|
|
fID(id),
|
|
|
|
fName(id),
|
|
|
|
fMessenger(msgn),
|
2021-05-28 22:26:32 -05:00
|
|
|
fChatView(NULL),
|
2021-05-24 14:48:25 -05:00
|
|
|
fLooper(NULL),
|
|
|
|
fDateFormatter()
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
2021-05-27 11:15:30 -05:00
|
|
|
fConversationItem = new ConversationItem(fName.String(), this);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
|
|
|
Conversation::GetId() const
|
|
|
|
{
|
|
|
|
return fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ImMessage(BMessage* msg)
|
|
|
|
{
|
|
|
|
int32 im_what = msg->FindInt32("im_what");
|
|
|
|
|
|
|
|
switch(im_what)
|
|
|
|
{
|
|
|
|
case IM_MESSAGE_RECEIVED:
|
|
|
|
{
|
|
|
|
_EnsureUser(msg);
|
2021-05-24 14:20:57 -05:00
|
|
|
_LogChatMessage(msg);
|
2021-05-28 22:26:32 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
2021-05-24 01:47:21 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_MESSAGE_SENT:
|
2021-05-24 14:20:57 -05:00
|
|
|
{
|
|
|
|
_LogChatMessage(msg);
|
2021-05-30 19:07:50 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_SEND_MESSAGE:
|
|
|
|
{
|
2021-05-24 14:20:57 -05:00
|
|
|
fMessenger.SendMessage(msg);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-30 19:07:50 -05:00
|
|
|
case IM_LOGS_RECEIVED:
|
2021-05-24 01:47:21 -05:00
|
|
|
default:
|
2021-05-28 22:26:32 -05:00
|
|
|
GetView()->MessageReceived(msg);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ObserveString(int32 what, BString str)
|
|
|
|
{
|
2021-05-28 22:26:32 -05:00
|
|
|
if (fChatView != NULL)
|
|
|
|
fChatView->ObserveString(what, str);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ObservePointer(int32 what, void* ptr)
|
|
|
|
{
|
2021-05-28 22:26:32 -05:00
|
|
|
if (fChatView != NULL)
|
|
|
|
fChatView->ObservePointer(what, ptr);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::ObserveInteger(int32 what, int32 val)
|
|
|
|
{
|
2021-05-28 22:26:32 -05:00
|
|
|
if (fChatView != NULL)
|
|
|
|
fChatView->ObserveInteger(what, val);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BMessenger
|
|
|
|
Conversation::Messenger() const
|
|
|
|
{
|
|
|
|
return fMessenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::SetMessenger(BMessenger messenger)
|
|
|
|
{
|
|
|
|
fMessenger = messenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProtocolLooper*
|
|
|
|
Conversation::GetProtocolLooper() const
|
|
|
|
{
|
|
|
|
return fLooper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::SetProtocolLooper(ProtocolLooper* looper)
|
|
|
|
{
|
|
|
|
fLooper = looper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
|
|
|
Conversation::GetName() const
|
|
|
|
{
|
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserMap
|
|
|
|
Conversation::Users()
|
|
|
|
{
|
|
|
|
return fUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Contact*
|
|
|
|
Conversation::UserById(BString id)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
return fUsers.ValueFor(id, &found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::AddUser(User* user)
|
|
|
|
{
|
|
|
|
BMessage msg;
|
|
|
|
msg.AddString("user_id", user->GetId());
|
|
|
|
_EnsureUser(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 20:45:24 -05:00
|
|
|
void
|
|
|
|
Conversation::ShowView(bool typing, bool userAction)
|
|
|
|
{
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->SetConversation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
ConversationView*
|
|
|
|
Conversation::GetView()
|
2021-05-24 01:47:21 -05:00
|
|
|
{
|
2021-05-30 19:07:50 -05:00
|
|
|
if (fChatView != NULL)
|
|
|
|
return fChatView;
|
|
|
|
|
|
|
|
fChatView = new ConversationView(this);
|
|
|
|
|
|
|
|
if (fLooper->Protocol()->SaveLogs() == false)
|
|
|
|
return fChatView;
|
|
|
|
|
|
|
|
BStringList logs = _GetChatLogs();
|
|
|
|
BMessage logMsg(IM_MESSAGE);
|
|
|
|
logMsg.AddInt32("im_what", IM_LOGS_RECEIVED);
|
|
|
|
logMsg.AddStrings("body", logs);
|
|
|
|
fChatView->MessageReceived(&logMsg);
|
|
|
|
|
2021-05-28 22:26:32 -05:00
|
|
|
return fChatView;
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 20:45:24 -05:00
|
|
|
ConversationItem*
|
|
|
|
Conversation::GetListItem()
|
|
|
|
{
|
|
|
|
return fConversationItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 14:20:57 -05:00
|
|
|
void
|
|
|
|
Conversation::_LogChatMessage(BMessage* msg)
|
|
|
|
{
|
2021-05-24 14:48:25 -05:00
|
|
|
BString date;
|
|
|
|
fDateFormatter.Format(date, time(0), B_SHORT_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
|
|
|
|
|
2021-05-24 14:20:57 -05:00
|
|
|
BString id = msg->FindString("user_id");
|
|
|
|
BString uname;
|
|
|
|
|
|
|
|
if (id.IsEmpty() == false)
|
|
|
|
uname = UserById(id)->GetName();
|
|
|
|
else
|
|
|
|
uname = "You";
|
|
|
|
|
2021-05-24 14:48:25 -05:00
|
|
|
BString logLine("[");
|
|
|
|
logLine << date;
|
|
|
|
logLine << "] ";
|
|
|
|
logLine << uname;
|
2021-05-24 14:20:57 -05:00
|
|
|
logLine << ": ";
|
|
|
|
logLine << msg->FindString("body");
|
|
|
|
logLine << "\n";
|
|
|
|
|
|
|
|
|
2021-05-24 19:12:42 -05:00
|
|
|
// TODO: Don't hardcode 21, expose maximum as a setting
|
|
|
|
BStringList logs = _GetChatLogs();
|
|
|
|
logs.Remove(21);
|
|
|
|
logs.Add(logLine, 0);
|
|
|
|
|
|
|
|
BMessage newLogMsg;
|
|
|
|
newLogMsg.AddStrings("log", logs);
|
|
|
|
|
|
|
|
BFile logFile(fLogPath.Path(), B_READ_WRITE | B_CREATE_FILE);
|
|
|
|
newLogMsg.Flatten(&logFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BStringList
|
|
|
|
Conversation::_GetChatLogs()
|
|
|
|
{
|
|
|
|
_EnsureLogPath();
|
|
|
|
|
|
|
|
BFile logFile(fLogPath.Path(), B_READ_WRITE | B_CREATE_FILE);
|
|
|
|
BMessage logMsg;
|
|
|
|
BStringList logs;
|
|
|
|
|
|
|
|
if (logMsg.Unflatten(&logFile) == B_OK) {
|
|
|
|
logMsg.FindStrings("log", &logs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return logs;
|
2021-05-24 14:20:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Conversation::_EnsureLogPath()
|
|
|
|
{
|
|
|
|
if (fLogPath.InitCheck() == B_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char* sig = fLooper->Protocol()->Signature();
|
|
|
|
CayaProtocolAddOn* protoAdd = ProtocolManager::Get()->ProtocolAddOn(sig);
|
|
|
|
|
|
|
|
fLogPath.SetTo(CayaLogPath(protoAdd->Signature(), protoAdd->ProtoSignature()));
|
|
|
|
fLogPath.Append(fID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
Contact*
|
|
|
|
Conversation::_EnsureUser(BMessage* msg)
|
|
|
|
{
|
|
|
|
BString id = msg->FindString("user_id");
|
|
|
|
if (id.IsEmpty() == true) return NULL;
|
|
|
|
|
|
|
|
Contact* user = UserById(id);
|
|
|
|
Contact* serverUser = _GetServer()->ContactById(id);
|
|
|
|
|
|
|
|
if (user == NULL && serverUser != NULL) {
|
|
|
|
fUsers.AddItem(id, serverUser);
|
|
|
|
user = serverUser;
|
2021-05-31 10:50:43 -05:00
|
|
|
GetView()->UpdateUserList(fUsers);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
else if (user == NULL) {
|
|
|
|
user = new Contact(id, _GetServer()->Looper());
|
|
|
|
user->SetProtocolLooper(fLooper);
|
|
|
|
|
|
|
|
_GetServer()->AddContact(user);
|
|
|
|
fUsers.AddItem(id, user);
|
2021-05-31 10:50:43 -05:00
|
|
|
GetView()->UpdateUserList(fUsers);
|
2021-05-24 01:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
user->RegisterObserver(this);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Server*
|
|
|
|
Conversation::_GetServer()
|
|
|
|
{
|
|
|
|
return ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|