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.
This commit is contained in:
Jaidyn Ann 2021-06-12 21:42:10 -05:00
parent 9cd4ce18e1
commit 07350b3a0a
5 changed files with 63 additions and 29 deletions

View File

@ -1,6 +1,7 @@
/* /*
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved. * Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Copyright 2014, Funky Idea Software * Copyright 2014, Funky Idea Software
* Copyright 2021, Jaidyn Levesque
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include <memory.h> #include <memory.h>
@ -126,18 +127,40 @@ CayaCachePath()
const char* const char*
CayaLogPath(const char* accountName) CayaAccountCachePath(const char* accountName)
{ {
BPath path; BPath path(CayaCachePath());
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) if (path.InitCheck() != B_OK)
return NULL; return NULL;
path.Append("Caya/Logs");
path.Append(accountName); path.Append(accountName);
if (create_directory(path.Path(), 0755) != B_OK) if (create_directory(path.Path(), 0755) != B_OK)
return NULL; 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(); return path.Path();
} }

View File

@ -1,5 +1,7 @@
/* /*
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved. * 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. * Distributed under the terms of the MIT License.
*/ */
#ifndef _CAYA_UTILS_H #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* CayaAccountPath(const char* signature, const char* subsignature);
const char* CayaCachePath(); 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 CayaTintColor(rgb_color color, int severity);
rgb_color CayaForegroundColor(rgb_color background); rgb_color CayaForegroundColor(rgb_color background);

View File

@ -334,8 +334,8 @@ Conversation::_LogChatMessage(BMessage* msg)
newLogMsg.AddStrings("body", bodies); newLogMsg.AddStrings("body", bodies);
newLogMsg.AddStrings("user_id", users); newLogMsg.AddStrings("user_id", users);
BFile logFile(fLogPath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE); BFile logFile(fCachePath.Path(), B_READ_WRITE | B_OPEN_AT_END | B_CREATE_FILE);
WriteAttributeMessage(&logFile, "logs", &newLogMsg); WriteAttributeMessage(&logFile, "Caya:logs", &newLogMsg);
// Plain-text logs // Plain-text logs
BString uname; BString uname;
@ -354,22 +354,22 @@ Conversation::_LogChatMessage(BMessage* msg)
status_t status_t
Conversation::_GetChatLogs(BMessage* msg) 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); return ReadAttributeMessage(&logFile, "logs", msg);
} }
void void
Conversation::_EnsureLogPath() Conversation::_EnsureCachePath()
{ {
if (fLogPath.InitCheck() == B_OK) if (fCachePath.InitCheck() == B_OK)
return; return;
fLogPath.SetTo(CayaLogPath(fLooper->Protocol()->GetName())); fCachePath.SetTo(CayaRoomCachePath(fLooper->Protocol()->GetName(),
fLogPath.Append(fID); fID.String()));
} }

View File

@ -71,7 +71,8 @@ public:
private: private:
void _LogChatMessage(BMessage* msg); void _LogChatMessage(BMessage* msg);
status_t _GetChatLogs(BMessage* msg); status_t _GetChatLogs(BMessage* msg);
void _EnsureLogPath();
void _EnsureCachePath();
User* _EnsureUser(BMessage* msg); User* _EnsureUser(BMessage* msg);
@ -86,7 +87,7 @@ private:
BBitmap* fIcon; BBitmap* fIcon;
BPath fLogPath; BPath fCachePath;
BDateTimeFormat fDateFormatter; BDateTimeFormat fDateFormatter;
UserMap fUsers; UserMap fUsers;

View File

@ -25,6 +25,7 @@
#include "CayaProtocol.h" #include "CayaProtocol.h"
#include "CayaPreferences.h" #include "CayaPreferences.h"
#include "CayaProtocolMessages.h" #include "CayaProtocolMessages.h"
#include "CayaUtils.h"
#include "ImageCache.h" #include "ImageCache.h"
#include "InviteDialogue.h" #include "InviteDialogue.h"
#include "ProtocolLooper.h" #include "ProtocolLooper.h"
@ -43,18 +44,8 @@ Server::Server()
void void
Server::Quit() Server::Quit()
{ {
Contact* contact = NULL; for (int i = 0; i < fLoopers.CountItems(); i++)
Conversation* conversation = NULL; RemoveProtocolLooper(fLoopers.KeyAt(i));
// while (contact = fRosterMap.ValueAt(0)) {
// contact->DeletePopUp();
// fRosterMap.RemoveItemAt(0);
// }
// while (conversation = fChatMap.ValueAt(0)) {
// fChatMap.RemoveItemAt(0);
// delete conversation;
// }
} }
@ -468,9 +459,11 @@ Server::ImMessage(BMessage* msg)
} }
case IM_PROTOCOL_READY: case IM_PROTOCOL_READY:
{ {
// Ready notification
ProtocolLooper* looper = _LooperFromMessage(msg); ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL) if (looper == NULL)
break; break;
CayaProtocol* proto = looper->Protocol();
BString content("%user% has connected!"); BString content("%user% has connected!");
content.ReplaceAll("%user%", looper->Protocol()->GetName()); content.ReplaceAll("%user%", looper->Protocol()->GetName());
@ -479,8 +472,21 @@ Server::ImMessage(BMessage* msg)
notification.SetGroup(BString("Caya")); notification.SetGroup(BString("Caya"));
notification.SetTitle("Connected"); notification.SetTitle("Connected");
notification.SetContent(content); notification.SetContent(content);
notification.SetIcon(looper->Protocol()->Icon()); notification.SetIcon(proto->Icon());
notification.Send(); 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; break;
} }