diff --git a/application/CayaProtocol.h b/application/CayaProtocol.h index 94d7fad..4b3c2a5 100644 --- a/application/CayaProtocol.h +++ b/application/CayaProtocol.h @@ -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; diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index b7dfdb1..c4a9d2a 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -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. */ diff --git a/application/ChatWindow.cpp b/application/ChatWindow.cpp index 3f693b9..7d4fbe2 100644 --- a/application/ChatWindow.cpp +++ b/application/ChatWindow.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -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; diff --git a/application/Conversation.cpp b/application/Conversation.cpp index e534bee..c65d224 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -7,6 +7,7 @@ #include #include +#include #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 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; } diff --git a/application/Conversation.h b/application/Conversation.h index 77c1511..840cb16 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -57,6 +57,7 @@ public: private: void _LogChatMessage(BMessage* msg); + BStringList _GetChatLogs(); void _EnsureLogPath(); void _CreateChatWindow(); diff --git a/application/views/CayaRenderView.cpp b/application/views/CayaRenderView.cpp index a0a55b4..68239f5 100644 --- a/application/views/CayaRenderView.cpp +++ b/application/views/CayaRenderView.cpp @@ -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) { diff --git a/application/views/CayaRenderView.h b/application/views/CayaRenderView.h index 15d53ee..b902da4 100644 --- a/application/views/CayaRenderView.h +++ b/application/views/CayaRenderView.h @@ -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: diff --git a/protocols/xmpp/JabberHandler.h b/protocols/xmpp/JabberHandler.h index bb9b46d..d17e1d1 100644 --- a/protocols/xmpp/JabberHandler.h +++ b/protocols/xmpp/JabberHandler.h @@ -57,6 +57,8 @@ public: virtual uint32 GetEncoding(); + virtual bool SaveLogs() const { return true; } + virtual CayaProtocolMessengerInterface* MessengerInterface() const;