Populate conversations with logs, if any saved

Also adds a function to protocols to toggle this― if CayaProtocol::SaveLogs()
returns true, then local logs will be used to popualte conversation
windows. Otherwise, it's up to the protocol itself.

A new protocol IM_MESSAGE was also created, IM_LOGS_RECEIVED, which can
be used to populate conversations with old logs.
This commit is contained in:
Jaidyn Ann 2021-05-24 19:12:42 -05:00
parent cba947475c
commit 3d020c4628
8 changed files with 67 additions and 6 deletions

View File

@ -47,6 +47,9 @@ public:
//! Protocol icon
virtual BBitmap* Icon() const = 0;
//! Use local logs to populate chat
virtual bool SaveLogs() const = 0;
//! Add-on's path
virtual void SetPath(BPath path) = 0;
virtual BPath Path() = 0;

View File

@ -68,6 +68,9 @@ enum im_what_code {
//! User stopped typing
IM_USER_STOPPED_TYPING = 27,
//! Logs received
IM_LOGS_RECEIVED = 28,
/*
* Messages related to contact changes.
*/

View File

@ -24,6 +24,7 @@
#include <SpaceLayoutItem.h>
#include <ScrollView.h>
#include <String.h>
#include <StringList.h>
#include <Notification.h>
#include <libinterface/BitmapView.h>
@ -236,6 +237,17 @@ ChatWindow::ImMessage(BMessage* msg)
break;
}
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;
}
case IM_CONTACT_STARTED_TYPING:
fStatus->SetText("Contact is typing...");
break;

View File

@ -7,6 +7,7 @@
#include <DateTimeFormat.h>
#include <Locale.h>
#include <StringList.h>
#include "CayaPreferences.h"
#include "CayaProtocolMessages.h"
@ -216,15 +217,21 @@ Conversation::_CreateChatWindow()
{
fChatWindow = new ChatWindow(this);
WindowsManager::Get()->RelocateWindow(fChatWindow);
if (fLooper->Protocol()->SaveLogs() == false)
return;
BStringList logs = _GetChatLogs();
BMessage logMsg(IM_MESSAGE);
logMsg.AddInt32("im_what", IM_LOGS_RECEIVED);
logMsg.AddStrings("log", logs);
fChatWindow->ImMessage(&logMsg);
}
#include <iostream>
void
Conversation::_LogChatMessage(BMessage* msg)
{
_EnsureLogPath();
BString date;
fDateFormatter.Format(date, time(0), B_SHORT_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
@ -236,7 +243,6 @@ Conversation::_LogChatMessage(BMessage* msg)
else
uname = "You";
BString logLine("[");
logLine << date;
logLine << "] ";
@ -246,8 +252,33 @@ Conversation::_LogChatMessage(BMessage* msg)
logLine << "\n";
BFile log(fLogPath.Path(), B_WRITE_ONLY | B_OPEN_AT_END | B_CREATE_FILE);
log.Write(logLine.String(), logLine.Length());
// 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;
}

View File

@ -57,6 +57,7 @@ public:
private:
void _LogChatMessage(BMessage* msg);
BStringList _GetChatLogs();
void _EnsureLogPath();
void _CreateChatWindow();

View File

@ -48,6 +48,14 @@ CayaRenderView::AppendOwnMessage(const char* message)
}
void
CayaRenderView::AppendGenericMessage(const char* message)
{
Append(message, COL_TEXT, COL_TEXT, R_TEXT);
ScrollToSelection();
}
void
CayaRenderView::AddEmoticText(const char * txt, int16 cols , int16 font , int16 cols2 , int16 font2)
{

View File

@ -39,6 +39,7 @@ class CayaRenderView : public RunView
void AppendOtherMessage(const char* otherNick, const char* message);
void AppendOwnMessage(const char* message);
void AppendGenericMessage(const char* message);
void AddEmoticText(const char * txt, int16 cols , int16 font , int16 cols2 , int16 font2);
protected:

View File

@ -57,6 +57,8 @@ public:
virtual uint32 GetEncoding();
virtual bool SaveLogs() const { return true; }
virtual CayaProtocolMessengerInterface*
MessengerInterface() const;