Add text logging of chats

Now chat sessions are logged to ~/config/settings/Caya/Cache/Logs/.
This commit is contained in:
Jaidyn Ann 2021-05-24 14:20:57 -05:00
parent 48d0b7bc96
commit 385bfbff35
5 changed files with 93 additions and 1 deletions

View File

@ -110,6 +110,41 @@ CayaAccountPath(const char* signature, const char* subsignature)
} }
const char*
CayaCachePath()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return NULL;
path.Append("Caya/Cache");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
}
const char*
CayaLogPath(const char* signature, const char* subsignature)
{
BPath path(CayaCachePath());
path.Append("Logs");
path.Append(signature);
if (BString(signature) != BString(subsignature)
|| BString(subsignature).IsEmpty() == false)
{
path.Append(subsignature);
}
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
}
extern "C" { extern "C" {
status_t status_t

View File

@ -20,6 +20,9 @@ const char* CayaAccountsPath();
const char* CayaAccountPath(const char* signature); const char* CayaAccountPath(const char* signature);
const char* CayaAccountPath(const char* signature, const char* subsignature); const char* CayaAccountPath(const char* signature, const char* subsignature);
const char* CayaCachePath();
const char* CayaLogPath(const char* signature, const char* subsignature);
extern "C" status_t our_image(image_info& image); extern "C" status_t our_image(image_info& image);
#endif // _CAYA_UTILS_H #endif // _CAYA_UTILS_H

View File

@ -165,7 +165,7 @@ ChatWindow::MessageReceived(BMessage* message)
msg.AddInt32("im_what", IM_SEND_MESSAGE); msg.AddInt32("im_what", IM_SEND_MESSAGE);
msg.AddString("chat_id", fConversation->GetId()); msg.AddString("chat_id", fConversation->GetId());
msg.AddString("body", message); msg.AddString("body", message);
fConversation->Messenger().SendMessage(&msg); fConversation->ImMessage(&msg);
fSendView->SetText(""); fSendView->SetText("");
break; break;

View File

@ -7,8 +7,11 @@
#include "CayaPreferences.h" #include "CayaPreferences.h"
#include "CayaProtocolMessages.h" #include "CayaProtocolMessages.h"
#include "CayaUtils.h"
#include "ChatWindow.h" #include "ChatWindow.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "ProtocolLooper.h"
#include "ProtocolManager.h"
#include "Server.h" #include "Server.h"
#include "TheApp.h" #include "TheApp.h"
#include "WindowsManager.h" #include "WindowsManager.h"
@ -44,11 +47,18 @@ Conversation::ImMessage(BMessage* msg)
{ {
_EnsureUser(msg); _EnsureUser(msg);
_LogChatMessage(msg);
ChatWindow* win = GetChatWindow(); ChatWindow* win = GetChatWindow();
ShowWindow(); ShowWindow();
win->PostMessage(msg); win->PostMessage(msg);
break; break;
} }
case IM_SEND_MESSAGE:
{
_LogChatMessage(msg);
fMessenger.SendMessage(msg);
break;
}
default: default:
GetChatWindow()->PostMessage(msg); GetChatWindow()->PostMessage(msg);
} }
@ -205,6 +215,44 @@ Conversation::_CreateChatWindow()
} }
void
Conversation::_LogChatMessage(BMessage* msg)
{
_EnsureLogPath();
BString id = msg->FindString("user_id");
BString uname;
if (id.IsEmpty() == false)
uname = UserById(id)->GetName();
else
uname = "You";
BString logLine = uname;
logLine << ": ";
logLine << msg->FindString("body");
logLine << "\n";
BFile log(fLogPath.Path(), B_WRITE_ONLY | B_OPEN_AT_END | B_CREATE_FILE);
log.Write(logLine.String(), logLine.Length());
}
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);
}
Contact* Contact*
Conversation::_EnsureUser(BMessage* msg) Conversation::_EnsureUser(BMessage* msg)
{ {

View File

@ -6,6 +6,7 @@
#define CONVERSATION_H #define CONVERSATION_H
#include <Messenger.h> #include <Messenger.h>
#include <Path.h>
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
@ -54,6 +55,9 @@ public:
void AddUser(User* user); void AddUser(User* user);
private: private:
void _LogChatMessage(BMessage* msg);
void _EnsureLogPath();
void _CreateChatWindow(); void _CreateChatWindow();
Contact* _EnsureUser(BMessage* msg); Contact* _EnsureUser(BMessage* msg);
Server* _GetServer(); Server* _GetServer();
@ -66,6 +70,8 @@ private:
BString fID; BString fID;
BString fName; BString fName;
BPath fLogPath;
UserMap fUsers; UserMap fUsers;
}; };