From 07350b3a0a654b0d849586b171b47e0c4d263d24 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 12 Jun 2021 21:42:10 -0500 Subject: [PATCH] Auto-join cached rooms on log-in Rooms are now cached in ~/config/settings/Caya/Cache/$account/Rooms/, with each file representing a seperate room. The filename is used as the identifier, and these files now also serve as log files. The log attribute name has also changed from "logs" to "Caya:logs". When a protocol has succesfuly connected, all cached rooms are automatically joined. --- application/CayaUtils.cpp | 33 ++++++++++++++++++++++++++++----- application/CayaUtils.h | 6 +++++- application/Conversation.cpp | 16 ++++++++-------- application/Conversation.h | 5 +++-- application/Server.cpp | 32 +++++++++++++++++++------------- 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/application/CayaUtils.cpp b/application/CayaUtils.cpp index b20a74e..549d8b9 100644 --- a/application/CayaUtils.cpp +++ b/application/CayaUtils.cpp @@ -1,6 +1,7 @@ /* * Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved. * Copyright 2014, Funky Idea Software + * Copyright 2021, Jaidyn Levesque * Distributed under the terms of the MIT License. */ #include @@ -126,18 +127,40 @@ CayaCachePath() const char* -CayaLogPath(const char* accountName) +CayaAccountCachePath(const char* accountName) { - BPath path; - if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) + BPath path(CayaCachePath()); + if (path.InitCheck() != B_OK) return NULL; - - path.Append("Caya/Logs"); path.Append(accountName); if (create_directory(path.Path(), 0755) != B_OK) return NULL; + return path.Path(); +} + +const char* +CayaRoomsCachePath(const char* accountName) +{ + BPath path(CayaAccountCachePath(accountName)); + if (path.InitCheck() != B_OK) + return NULL; + path.Append("Rooms"); + + if (create_directory(path.Path(), 0755) != B_OK) + return NULL; + return path.Path(); +} + + +const char* +CayaRoomCachePath(const char* accountName, const char* roomIdentifier) +{ + BPath path(CayaRoomsCachePath(accountName)); + if (path.InitCheck() != B_OK) + return NULL; + path.Append(roomIdentifier); return path.Path(); } diff --git a/application/CayaUtils.h b/application/CayaUtils.h index e631659..9b5222e 100644 --- a/application/CayaUtils.h +++ b/application/CayaUtils.h @@ -1,5 +1,7 @@ /* * Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved. + * Copyright 2014, Funky Idea Software + * Copyright 2021, Jaidyn Levesque * Distributed under the terms of the MIT License. */ #ifndef _CAYA_UTILS_H @@ -22,7 +24,9 @@ const char* CayaAccountPath(const char* signature); const char* CayaAccountPath(const char* signature, const char* subsignature); const char* CayaCachePath(); -const char* CayaLogPath(const char* accountName); +const char* CayaAccountCachePath(const char* accountName); +const char* CayaRoomsCachePath(const char* accountName); +const char* CayaRoomCachePath(const char* accountName, const char* roomIdentifier); rgb_color CayaTintColor(rgb_color color, int severity); rgb_color CayaForegroundColor(rgb_color background); diff --git a/application/Conversation.cpp b/application/Conversation.cpp index f259339..4dc5cf5 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -334,8 +334,8 @@ Conversation::_LogChatMessage(BMessage* msg) newLogMsg.AddStrings("body", bodies); newLogMsg.AddStrings("user_id", users); - BFile logFile(fLogPath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE); - WriteAttributeMessage(&logFile, "logs", &newLogMsg); + BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE); + WriteAttributeMessage(&logFile, "Caya:logs", &newLogMsg); // Plain-text logs BString uname; @@ -354,22 +354,22 @@ Conversation::_LogChatMessage(BMessage* msg) status_t Conversation::_GetChatLogs(BMessage* msg) { - _EnsureLogPath(); + _EnsureCachePath(); - BFile logFile(fLogPath.Path(), B_READ_WRITE | B_CREATE_FILE); + BFile logFile(fCachePath.Path(), B_READ_WRITE | B_CREATE_FILE); return ReadAttributeMessage(&logFile, "logs", msg); } void -Conversation::_EnsureLogPath() +Conversation::_EnsureCachePath() { - if (fLogPath.InitCheck() == B_OK) + if (fCachePath.InitCheck() == B_OK) return; - fLogPath.SetTo(CayaLogPath(fLooper->Protocol()->GetName())); - fLogPath.Append(fID); + fCachePath.SetTo(CayaRoomCachePath(fLooper->Protocol()->GetName(), + fID.String())); } diff --git a/application/Conversation.h b/application/Conversation.h index 17a81a1..02d2c15 100644 --- a/application/Conversation.h +++ b/application/Conversation.h @@ -71,7 +71,8 @@ public: private: void _LogChatMessage(BMessage* msg); status_t _GetChatLogs(BMessage* msg); - void _EnsureLogPath(); + + void _EnsureCachePath(); User* _EnsureUser(BMessage* msg); @@ -86,7 +87,7 @@ private: BBitmap* fIcon; - BPath fLogPath; + BPath fCachePath; BDateTimeFormat fDateFormatter; UserMap fUsers; diff --git a/application/Server.cpp b/application/Server.cpp index 7b7c6d3..00f71fc 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -25,6 +25,7 @@ #include "CayaProtocol.h" #include "CayaPreferences.h" #include "CayaProtocolMessages.h" +#include "CayaUtils.h" #include "ImageCache.h" #include "InviteDialogue.h" #include "ProtocolLooper.h" @@ -43,18 +44,8 @@ Server::Server() void Server::Quit() { - Contact* contact = NULL; - Conversation* conversation = NULL; - -// while (contact = fRosterMap.ValueAt(0)) { -// contact->DeletePopUp(); -// fRosterMap.RemoveItemAt(0); -// } - -// while (conversation = fChatMap.ValueAt(0)) { -// fChatMap.RemoveItemAt(0); -// delete conversation; -// } + for (int i = 0; i < fLoopers.CountItems(); i++) + RemoveProtocolLooper(fLoopers.KeyAt(i)); } @@ -468,9 +459,11 @@ Server::ImMessage(BMessage* msg) } case IM_PROTOCOL_READY: { + // Ready notification ProtocolLooper* looper = _LooperFromMessage(msg); if (looper == NULL) break; + CayaProtocol* proto = looper->Protocol(); BString content("%user% has connected!"); content.ReplaceAll("%user%", looper->Protocol()->GetName()); @@ -479,8 +472,21 @@ Server::ImMessage(BMessage* msg) notification.SetGroup(BString("Caya")); notification.SetTitle("Connected"); notification.SetContent(content); - notification.SetIcon(looper->Protocol()->Icon()); + notification.SetIcon(proto->Icon()); notification.Send(); + + // Join cached rooms + BEntry entry; + char fileName[B_FILE_NAME_LENGTH] = {'\0'}; + BDirectory dir(CayaRoomsCachePath(proto->GetName())); + + while (dir.GetNextEntry(&entry, true) == B_OK) + if (entry.GetName(fileName) == B_OK) { + BMessage join(IM_MESSAGE); + join.AddInt32("im_what", IM_JOIN_ROOM); + join.AddString("chat_id", fileName); + looper->PostMessage(&join); + } break; }