diff --git a/application/CayaUtils.cpp b/application/CayaUtils.cpp index 1658525..6e3460e 100644 --- a/application/CayaUtils.cpp +++ b/application/CayaUtils.cpp @@ -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" { status_t diff --git a/application/CayaUtils.h b/application/CayaUtils.h index bebc119..bb37a14 100644 --- a/application/CayaUtils.h +++ b/application/CayaUtils.h @@ -20,6 +20,9 @@ const char* CayaAccountsPath(); const char* CayaAccountPath(const char* signature); 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); #endif // _CAYA_UTILS_H diff --git a/application/ChatWindow.cpp b/application/ChatWindow.cpp index 7e9a863..3f693b9 100644 --- a/application/ChatWindow.cpp +++ b/application/ChatWindow.cpp @@ -165,7 +165,7 @@ ChatWindow::MessageReceived(BMessage* message) msg.AddInt32("im_what", IM_SEND_MESSAGE); msg.AddString("chat_id", fConversation->GetId()); msg.AddString("body", message); - fConversation->Messenger().SendMessage(&msg); + fConversation->ImMessage(&msg); fSendView->SetText(""); break; diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 8d45373..5079a70 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -7,8 +7,11 @@ #include "CayaPreferences.h" #include "CayaProtocolMessages.h" +#include "CayaUtils.h" #include "ChatWindow.h" #include "MainWindow.h" +#include "ProtocolLooper.h" +#include "ProtocolManager.h" #include "Server.h" #include "TheApp.h" #include "WindowsManager.h" @@ -44,11 +47,18 @@ Conversation::ImMessage(BMessage* msg) { _EnsureUser(msg); + _LogChatMessage(msg); ChatWindow* win = GetChatWindow(); ShowWindow(); win->PostMessage(msg); break; } + case IM_SEND_MESSAGE: + { + _LogChatMessage(msg); + fMessenger.SendMessage(msg); + break; + } default: 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* Conversation::_EnsureUser(BMessage* msg) { diff --git a/application/Conversation.h b/application/Conversation.h index 35c7703..2c6f6f0 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -6,6 +6,7 @@ #define CONVERSATION_H #include +#include #include @@ -54,6 +55,9 @@ public: void AddUser(User* user); private: + void _LogChatMessage(BMessage* msg); + void _EnsureLogPath(); + void _CreateChatWindow(); Contact* _EnsureUser(BMessage* msg); Server* _GetServer(); @@ -66,6 +70,8 @@ private: BString fID; BString fName; + BPath fLogPath; + UserMap fUsers; };