Pass relevant cache paths directly to add-ons

Now, instead of using Utils.cpp's AccountCachePath and AddOnCachePath,
and add-on should implement ChatProtocol::SetAccountCachePath and
ChatProtocol::SetAddOnCachePath.

In addition, most path-related functions in Utils now return BPaths― and
some of interest to chat add-ons can accept the path given by
ChatProtocol::SetAccountCachePath as the main argument.
This commit is contained in:
Jaidyn Ann 2022-02-18 15:42:06 -06:00
parent 866899eaad
commit 8d50a6b9a4
18 changed files with 151 additions and 108 deletions

View File

@ -31,11 +31,13 @@ Account::Account(bigtime_t instanceId, ChatProtocol* cayap,
fProtocol->Init(this); fProtocol->Init(this);
// Find user's settings path // Find user's settings path
BPath path(AccountPath(addOnSignature, fProtocol->Signature())); BPath path = AccountPath(addOnSignature, fProtocol->Signature());
if (path.InitCheck() == B_OK) { if (path.InitCheck() == B_OK) {
path.Append(name); path.Append(name);
fProtocol->SetName(name); fProtocol->SetName(name);
fProtocol->SetAccountCachePath(AccountCachePath(name));
fProtocol->SetAddOnCachePath(AddOnCachePath(addOnSignature));
// Load settings file // Load settings file
BFile file(path.Path(), B_READ_ONLY); BFile file(path.Path(), B_READ_ONLY);

View File

@ -88,9 +88,11 @@ public:
//! Protocol icon //! Protocol icon
virtual BBitmap* Icon() const { return NULL; } virtual BBitmap* Icon() const { return NULL; }
//! Add-on's path //! Pertinent paths
virtual void SetAddOnPath(BPath path) = 0;
virtual BPath AddOnPath() = 0; virtual BPath AddOnPath() = 0;
virtual void SetAddOnPath(BPath path) = 0;
virtual void SetAccountCachePath(BPath path) { };
virtual void SetAddOnCachePath(BPath path) { };
//! Name of account file (leaf) //! Name of account file (leaf)
virtual const char* GetName() = 0; virtual const char* GetName() = 0;

View File

@ -52,7 +52,7 @@ ChatProtocol*
ChatProtocolAddOn::ProtocolAt(int32 i) const ChatProtocolAddOn::ProtocolAt(int32 i) const
{ {
ChatProtocol* proto = fGetProtocol(i); ChatProtocol* proto = fGetProtocol(i);
proto->SetAddOnPath(BPath(fPath.String())); proto->SetAddOnPath(fPath.String());
return proto; return proto;
} }

View File

@ -35,8 +35,5 @@ Contact::_EnsureCachePath()
{ {
if (fCachePath.InitCheck() == B_OK) if (fCachePath.InitCheck() == B_OK)
return; return;
fCachePath.SetTo(ContactCachePath(fLooper->Protocol()->GetName(), fCachePath = ContactCachePath(fLooper->Protocol()->GetName(), fID.String());
fID.String()));
} }

View File

@ -636,8 +636,7 @@ Conversation::_EnsureCachePath()
{ {
if (fCachePath.InitCheck() == B_OK) if (fCachePath.InitCheck() == B_OK)
return; return;
fCachePath.SetTo(RoomCachePath(fLooper->Protocol()->GetName(), fCachePath = RoomCachePath(fLooper->Protocol()->GetName(), fID.String());
fID.String()));
} }

View File

@ -49,7 +49,7 @@ ProtocolSettings::Accounts() const
{ {
BObjectList<BString> list(true); BObjectList<BString> list(true);
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature())); BPath path = AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature());
if (path.InitCheck() != B_OK) if (path.InitCheck() != B_OK)
return list; return list;
@ -93,7 +93,7 @@ ProtocolSettings::Load(const char* account, BMessage** settings)
status_t ret = B_ERROR; status_t ret = B_ERROR;
// Find user's settings path // Find user's settings path
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature())); BPath path = AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature());
if ((ret = path.InitCheck()) != B_OK) if ((ret = path.InitCheck()) != B_OK)
return ret; return ret;
@ -127,7 +127,7 @@ status_t
ProtocolSettings::Save(const char* account, BMessage settings) ProtocolSettings::Save(const char* account, BMessage settings)
{ {
// Find user's settings path // Find user's settings path
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature())); BPath path = AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature());
status_t ret; status_t ret;
if ((ret = path.InitCheck()) != B_OK) if ((ret = path.InitCheck()) != B_OK)
@ -146,7 +146,7 @@ ProtocolSettings::Rename(const char* from, const char* to)
status_t ret = B_ERROR; status_t ret = B_ERROR;
// Find user's settings path // Find user's settings path
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature())); BPath path = AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature());
if ((ret = path.InitCheck()) != B_OK) if ((ret = path.InitCheck()) != B_OK)
return ret; return ret;
@ -168,7 +168,7 @@ ProtocolSettings::Delete(const char* account)
status_t ret = B_ERROR; status_t ret = B_ERROR;
// Find user's settings path // Find user's settings path
BPath path(AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature())); BPath path = AccountPath(fAddOn->Signature(), fAddOn->ProtoSignature());
if ((ret = path.InitCheck()) != B_OK) if ((ret = path.InitCheck()) != B_OK)
return ret; return ret;

View File

@ -627,7 +627,7 @@ Server::ImMessage(BMessage* msg)
// Join cached rooms // Join cached rooms
BEntry entry; BEntry entry;
char fileName[B_FILE_NAME_LENGTH] = {'\0'}; char fileName[B_FILE_NAME_LENGTH] = {'\0'};
BDirectory dir(RoomsCachePath(looper->Protocol()->GetName())); BDirectory dir(RoomsCachePath(looper->Protocol()->GetName()).Path());
while (dir.GetNextEntry(&entry, true) == B_OK) while (dir.GetNextEntry(&entry, true) == B_OK)
if (entry.GetName(fileName) == B_OK) { if (entry.GetName(fileName) == B_OK) {

View File

@ -227,8 +227,7 @@ User::_EnsureCachePath()
{ {
if (fCachePath.InitCheck() == B_OK) if (fCachePath.InitCheck() == B_OK)
return; return;
fCachePath.SetTo(UserCachePath(fLooper->Protocol()->GetName(), fCachePath = UserCachePath(fLooper->Protocol()->GetName(), fID.String());
fID.String()));
} }

View File

@ -150,88 +150,104 @@ AccountPath(const char* signature, const char* subsignature)
} }
const char* BPath
CachePath() CachePath()
{ {
BPath path(SettingsPath()); BPath path = SettingsPath();
if (path.InitCheck() != B_OK) path.Append("Cache/");
return NULL; create_directory(path.Path(), 0755);
return path;
path.Append("Cache");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
} }
const char* BPath
AccountCachePath(const char* accountName) AccountCachePath(const char* accountName)
{ {
BPath path(CachePath()); BPath path = CachePath();
path.Append("Accounts"); path.Append("Accounts/");
if (path.InitCheck() != B_OK)
return NULL;
path.Append(accountName); path.Append(accountName);
if (create_directory(path.Path(), 0755) != B_OK) create_directory(path.Path(), 0755);
return NULL; return path;
return path.Path();
} }
const char* BPath
RoomsCachePath(const char* accountName) RoomsCachePath(const char* accountName)
{ {
BPath path(AccountCachePath(accountName)); return RoomsCachePath(AccountCachePath(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* BPath
RoomsCachePath(BPath accPath)
{
accPath.Append("Rooms/");
create_directory(accPath.Path(), 0755);
return accPath;
}
BPath
RoomCachePath(const char* accountName, const char* roomIdentifier) RoomCachePath(const char* accountName, const char* roomIdentifier)
{ {
BPath path(RoomsCachePath(accountName)); return RoomCachePath(AccountCachePath(accountName), roomIdentifier);
if (path.InitCheck() != B_OK)
return NULL;
path.Append(roomIdentifier);
return path.Path();
} }
const char* BPath
RoomCachePath(BPath accPath, const char* roomIdentifier)
{
BPath path = RoomsCachePath(accPath);
path.Append(roomIdentifier);
return path;
}
BPath
UserCachePath(const char* accountName, const char* userIdentifier) UserCachePath(const char* accountName, const char* userIdentifier)
{ {
BPath path(AccountCachePath(accountName)); return UserCachePath(AccountCachePath(accountName), userIdentifier);
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Users");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
path.Append(userIdentifier);
return path.Path();
} }
const char* BPath
UserCachePath(BPath accPath, const char* userIdentifier)
{
accPath.Append("Users/");
create_directory(accPath.Path(), 0755);
accPath.Append(userIdentifier);
return accPath;
}
BPath
ContactCachePath(const char* accountName, const char* userIdentifier) ContactCachePath(const char* accountName, const char* userIdentifier)
{ {
BPath path(AccountCachePath(accountName)); return ContactCachePath(AccountCachePath(accountName), userIdentifier);
if (path.InitCheck() != B_OK) }
return NULL;
path.Append("Contacts");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL; BPath
path.Append(userIdentifier); ContactCachePath(BPath accPath, const char* userIdentifier)
return path.Path(); {
accPath.Append("Contacts/");
create_directory(accPath.Path(), 0755);
accPath.Append(userIdentifier);
return accPath;
}
BPath
AddOnCachePath(const char* signature)
{
BPath path = CachePath();
path.Append("Add-Ons/");
path.Append(signature);
create_directory(path.Path(), 0755);
return path;
} }

View File

@ -36,12 +36,17 @@ const char* SettingsPath();
const char* AccountsPath(); const char* AccountsPath();
const char* AccountPath(const char* signature, const char* subsignature); const char* AccountPath(const char* signature, const char* subsignature);
const char* CachePath(); BPath CachePath();
const char* AccountCachePath(const char* accountName); BPath AccountCachePath(const char* accountName);
const char* RoomsCachePath(const char* accountName); BPath RoomsCachePath(const char* accountName);
const char* RoomCachePath(const char* accountName, const char* roomIdentifier); BPath RoomsCachePath(BPath accPath);
const char* UserCachePath(const char* accountName, const char* userIdentifier); BPath RoomCachePath(const char* accountName, const char* roomIdentifier);
const char* ContactCachePath(const char* accountName, const char* userIdentifier); BPath RoomCachePath(BPath accPath, const char* roomIdentifier);
BPath UserCachePath(const char* accountName, const char* userIdentifier);
BPath UserCachePath(BPath accPath, const char* userIdentifier);
BPath ContactCachePath(const char* accountName, const char* userIdentifier);
BPath ContactCachePath(BPath accPath, const char* userIdentifier);
BPath AddOnCachePath(const char* signature);
rgb_color TintColor(rgb_color color, int severity); rgb_color TintColor(rgb_color color, int severity);
rgb_color ForegroundColor(rgb_color background); rgb_color ForegroundColor(rgb_color background);
@ -55,4 +60,3 @@ extern "C" status_t our_image(image_info& image);
#endif // _APP_UTILS_H #endif // _APP_UTILS_H

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io> * Copyright 2021-2022, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2017, Akshay Agarwal <agarwal.akshay.akshay8@gmail.com> * Copyright 2017, Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license. * All rights reserved. Distributed under the terms of the MIT license.
*/ */
@ -37,7 +37,7 @@ connect_thread(void* data)
IrcProtocol* protocol = (IrcProtocol*)data; IrcProtocol* protocol = (IrcProtocol*)data;
protocol->Connect(); protocol->Connect();
status_t status = protocol->Loop(); status_t status = protocol->Loop();
exit(status); return status;
} }
@ -1275,7 +1275,7 @@ IrcProtocol::_RoleTitle(UserRole role)
const char* const char*
IrcProtocol::_ContactsCache() IrcProtocol::_ContactsCache()
{ {
BPath path(AccountCachePath(fName)); BPath path(fCachePath);
path.Append("contact_list"); path.Append("contact_list");
return path.Path(); return path.Path();
} }
@ -1286,7 +1286,7 @@ IrcProtocol::_JoinDefaultRooms()
{ {
// Hardcoded default room… I'm so awful, aren't I? ;-) // Hardcoded default room… I'm so awful, aren't I? ;-)
if (fServer == "irc.oftc.net") { if (fServer == "irc.oftc.net") {
BFile room(RoomCachePath(fName, "#haiku"), B_READ_ONLY); BFile room(RoomCachePath(fCachePath, "#haiku").Path(), B_READ_ONLY);
if (room.InitCheck() != B_OK) { if (room.InitCheck() != B_OK) {
BString cmd("JOIN #haiku"); BString cmd("JOIN #haiku");
_SendIrc(cmd); _SendIrc(cmd);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io> * Copyright 2021-2022, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2017, Akshay Agarwal <agarwal.akshay.akshay8@gmail.com> * Copyright 2017, Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license. * All rights reserved. Distributed under the terms of the MIT license.
*/ */
@ -45,6 +45,7 @@ public:
virtual BBitmap* Icon() const; virtual BBitmap* Icon() const;
virtual void SetAccountCachepath(BPath path) { fCachePath = path; }
virtual void SetAddOnPath(BPath path) { fAddOnPath = path; } virtual void SetAddOnPath(BPath path) { fAddOnPath = path; }
virtual BPath AddOnPath() { return fAddOnPath; } virtual BPath AddOnPath() { return fAddOnPath; }
@ -151,6 +152,7 @@ private:
BStringList fOfflineContacts; BStringList fOfflineContacts;
BPath fAddOnPath; BPath fAddOnPath;
BPath fCachePath;
BString fName; BString fName;
ChatProtocolMessengerInterface* fMessenger; ChatProtocolMessengerInterface* fMessenger;
bool fReady; bool fReady;

View File

@ -115,6 +115,11 @@ PurpleApp::MessageReceived(BMessage* msg)
{ {
BString accName = msg->FindString("account_name"); BString accName = msg->FindString("account_name");
BString username = fAccounts.ValueFor(accName); BString username = fAccounts.ValueFor(accName);
BString accountCache = msg->FindString("account_cache");
fAddOnCache = msg->FindString("addon_cache");
fAccountCache.AddItem(accName, accountCache);
int64 thread; int64 thread;
if (username.IsEmpty() == true if (username.IsEmpty() == true
|| msg->FindInt64("thread_id", &thread) != B_OK) || msg->FindInt64("thread_id", &thread) != B_OK)
@ -1956,13 +1961,7 @@ purple_connection_error_name(const PurpleConnectionErrorInfo* error)
const char* const char*
purple_cache() purple_cache()
{ {
BPath path; return ((PurpleApp*)be_app)->fAddOnCache;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return NULL;
path.Append(APP_NAME "/Cache/Add-Ons/" PURPLE_ADDON);
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
} }
@ -1970,26 +1969,25 @@ const char*
account_cache(PurpleAccount* account) account_cache(PurpleAccount* account)
{ {
const char* purple_user = purple_account_get_username(account); const char* purple_user = purple_account_get_username(account);
const char* cardie_user = NULL; const char* app_user = NULL;
StringMap usernames = ((PurpleApp*)be_app)->fAccounts; StringMap usernames = ((PurpleApp*)be_app)->fAccounts;
for (int i = 0; i < usernames.CountItems(); i++) for (int i = 0; i < usernames.CountItems(); i++)
if (usernames.ValueAt(i) == purple_user) { if (usernames.ValueAt(i) == purple_user) {
cardie_user = usernames.KeyAt(i); app_user = usernames.KeyAt(i);
break; break;
} }
if (cardie_user == NULL)
return NULL;
BPath path; const char* path = NULL;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) if (app_user != NULL) {
return NULL; bool found = false;
path.Append(APP_NAME "/Cache/Accounts/"); path = ((PurpleApp*)be_app)->fAccountCache.ValueFor(app_user, &found).String();
path.Append(cardie_user);
if (create_directory(path.Path(), 0755) != B_OK) if (found == false || create_directory(path, 0755) != B_OK)
return NULL; path = NULL;
return path.Path(); }
return path;
} }

View File

@ -26,6 +26,7 @@
#include <Application.h> #include <Application.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <ObjectList.h> #include <ObjectList.h>
#include <Path.h>
#include <StringList.h> #include <StringList.h>
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
@ -86,8 +87,11 @@ public:
HashMap fInviteList; HashMap fInviteList;
StringMap fUserNicks; // Purple username → Nickname for Cardie StringMap fUserNicks; // Purple username → Nickname for Cardie
StringMap fAccounts; // Cardie account name → Purple username StringMap fAccounts; // Cardie account name → Purple username
StringMap fAccountCache; // Cardie account name → Cache path
RoomMap fRoomlists; // Purple account → Purple roomlist RoomMap fRoomlists; // Purple account → Purple roomlist
BString fAddOnCache;
private: private:
void _SendSysText(PurpleConversation* conv, const char* text); void _SendSysText(PurpleConversation* conv, const char* text);

View File

@ -260,6 +260,8 @@ PurpleProtocol::UpdateSettings(BMessage* msg)
BMessage* account = new BMessage(PURPLE_REGISTER_THREAD); BMessage* account = new BMessage(PURPLE_REGISTER_THREAD);
account->AddInt64("thread_id", fBirdThread); account->AddInt64("thread_id", fBirdThread);
account->AddString("addon_cache", fAddOnCachePath.Path());
account->AddString("account_cache", fAccountCachePath.Path());
_SendPrplMessage(account); _SendPrplMessage(account);
resume_thread(fBirdThread); resume_thread(fBirdThread);
@ -330,6 +332,13 @@ PurpleProtocol::Icon() const
} }
BPath
PurpleProtocol::AddOnPath()
{
return fAddOnPath;
}
void void
PurpleProtocol::SetAddOnPath(BPath path) PurpleProtocol::SetAddOnPath(BPath path)
{ {
@ -337,10 +346,17 @@ PurpleProtocol::SetAddOnPath(BPath path)
} }
BPath void
PurpleProtocol::AddOnPath() PurpleProtocol::SetAccountCachePath(BPath path)
{ {
return fAddOnPath; fAccountCachePath = path;
}
void
PurpleProtocol::SetAddOnCachePath(BPath path)
{
fAddOnCachePath = path;
} }

View File

@ -66,8 +66,10 @@ public:
virtual BBitmap* Icon() const; virtual BBitmap* Icon() const;
virtual void SetAddOnPath(BPath path);
virtual BPath AddOnPath(); virtual BPath AddOnPath();
virtual void SetAddOnPath(BPath path);
virtual void SetAccountCachePath(BPath path);
virtual void SetAddOnCachePath(BPath path);
virtual const char* GetName(); virtual const char* GetName();
virtual void SetName(const char* name); virtual void SetName(const char* name);
@ -92,6 +94,8 @@ private:
BString fName; BString fName;
BPath fAddOnPath; BPath fAddOnPath;
BPath fAccountCachePath;
BPath fAddOnCachePath;
BString fSignature; BString fSignature;
BString fFriendlySignature; BString fFriendlySignature;

View File

@ -1,6 +1,6 @@
/* /*
* Copyright 2010, Pier Luigi Fiorini. All rights reserved. * Copyright 2010, Pier Luigi Fiorini. All rights reserved.
* Copyright 2021, Jaidyn Levesque. All rights reserved. * Copyright 2021-2022, Jaidyn Levesque. All rights reserved.
* Distributed under the terms of the GPL v2 License. * Distributed under the terms of the GPL v2 License.
* *
* Authors: * Authors:

View File

@ -1,6 +1,6 @@
/* /*
* Copyright 2010, Pier Luigi Fiorini. All rights reserved. * Copyright 2010, Pier Luigi Fiorini. All rights reserved.
* Copyright 2021, Jaidyn Levesque. All rights reserved. * Copyright 2021-2022, Jaidyn Levesque. All rights reserved.
* Distributed under the terms of the GPL v2 License. * Distributed under the terms of the GPL v2 License.
*/ */
#ifndef _JABBER_HANDLER_H #ifndef _JABBER_HANDLER_H