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);
// Find user's settings path
BPath path(AccountPath(addOnSignature, fProtocol->Signature()));
BPath path = AccountPath(addOnSignature, fProtocol->Signature());
if (path.InitCheck() == B_OK) {
path.Append(name);
fProtocol->SetName(name);
fProtocol->SetAccountCachePath(AccountCachePath(name));
fProtocol->SetAddOnCachePath(AddOnCachePath(addOnSignature));
// Load settings file
BFile file(path.Path(), B_READ_ONLY);

View File

@ -88,9 +88,11 @@ public:
//! Protocol icon
virtual BBitmap* Icon() const { return NULL; }
//! Add-on's path
virtual void SetAddOnPath(BPath path) = 0;
//! Pertinent paths
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)
virtual const char* GetName() = 0;

View File

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

View File

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

View File

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

View File

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

View File

@ -627,7 +627,7 @@ Server::ImMessage(BMessage* msg)
// Join cached rooms
BEntry entry;
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)
if (entry.GetName(fileName) == B_OK) {

View File

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

View File

@ -150,88 +150,104 @@ AccountPath(const char* signature, const char* subsignature)
}
const char*
BPath
CachePath()
{
BPath path(SettingsPath());
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Cache");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
BPath path = SettingsPath();
path.Append("Cache/");
create_directory(path.Path(), 0755);
return path;
}
const char*
BPath
AccountCachePath(const char* accountName)
{
BPath path(CachePath());
path.Append("Accounts");
if (path.InitCheck() != B_OK)
return NULL;
BPath path = CachePath();
path.Append("Accounts/");
path.Append(accountName);
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
create_directory(path.Path(), 0755);
return path;
}
const char*
BPath
RoomsCachePath(const char* accountName)
{
BPath path(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();
return RoomsCachePath(AccountCachePath(accountName));
}
const char*
BPath
RoomsCachePath(BPath accPath)
{
accPath.Append("Rooms/");
create_directory(accPath.Path(), 0755);
return accPath;
}
BPath
RoomCachePath(const char* accountName, const char* roomIdentifier)
{
BPath path(RoomsCachePath(accountName));
if (path.InitCheck() != B_OK)
return NULL;
path.Append(roomIdentifier);
return path.Path();
return RoomCachePath(AccountCachePath(accountName), roomIdentifier);
}
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)
{
BPath path(AccountCachePath(accountName));
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();
return UserCachePath(AccountCachePath(accountName), userIdentifier);
}
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)
{
BPath path(AccountCachePath(accountName));
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Contacts");
return ContactCachePath(AccountCachePath(accountName), userIdentifier);
}
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
path.Append(userIdentifier);
return path.Path();
BPath
ContactCachePath(BPath accPath, const char* userIdentifier)
{
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* AccountPath(const char* signature, const char* subsignature);
const char* CachePath();
const char* AccountCachePath(const char* accountName);
const char* RoomsCachePath(const char* accountName);
const char* RoomCachePath(const char* accountName, const char* roomIdentifier);
const char* UserCachePath(const char* accountName, const char* userIdentifier);
const char* ContactCachePath(const char* accountName, const char* userIdentifier);
BPath CachePath();
BPath AccountCachePath(const char* accountName);
BPath RoomsCachePath(const char* accountName);
BPath RoomsCachePath(BPath accPath);
BPath RoomCachePath(const char* accountName, const char* roomIdentifier);
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 ForegroundColor(rgb_color background);
@ -55,4 +60,3 @@ extern "C" status_t our_image(image_info& image);
#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>
* All rights reserved. Distributed under the terms of the MIT license.
*/
@ -37,7 +37,7 @@ connect_thread(void* data)
IrcProtocol* protocol = (IrcProtocol*)data;
protocol->Connect();
status_t status = protocol->Loop();
exit(status);
return status;
}
@ -1275,7 +1275,7 @@ IrcProtocol::_RoleTitle(UserRole role)
const char*
IrcProtocol::_ContactsCache()
{
BPath path(AccountCachePath(fName));
BPath path(fCachePath);
path.Append("contact_list");
return path.Path();
}
@ -1286,7 +1286,7 @@ IrcProtocol::_JoinDefaultRooms()
{
// Hardcoded default room… I'm so awful, aren't I? ;-)
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) {
BString cmd("JOIN #haiku");
_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>
* All rights reserved. Distributed under the terms of the MIT license.
*/
@ -45,6 +45,7 @@ public:
virtual BBitmap* Icon() const;
virtual void SetAccountCachepath(BPath path) { fCachePath = path; }
virtual void SetAddOnPath(BPath path) { fAddOnPath = path; }
virtual BPath AddOnPath() { return fAddOnPath; }
@ -151,6 +152,7 @@ private:
BStringList fOfflineContacts;
BPath fAddOnPath;
BPath fCachePath;
BString fName;
ChatProtocolMessengerInterface* fMessenger;
bool fReady;

View File

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

View File

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

View File

@ -260,6 +260,8 @@ PurpleProtocol::UpdateSettings(BMessage* msg)
BMessage* account = new BMessage(PURPLE_REGISTER_THREAD);
account->AddInt64("thread_id", fBirdThread);
account->AddString("addon_cache", fAddOnCachePath.Path());
account->AddString("account_cache", fAccountCachePath.Path());
_SendPrplMessage(account);
resume_thread(fBirdThread);
@ -330,6 +332,13 @@ PurpleProtocol::Icon() const
}
BPath
PurpleProtocol::AddOnPath()
{
return fAddOnPath;
}
void
PurpleProtocol::SetAddOnPath(BPath path)
{
@ -337,10 +346,17 @@ PurpleProtocol::SetAddOnPath(BPath path)
}
BPath
PurpleProtocol::AddOnPath()
void
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 void SetAddOnPath(BPath path);
virtual BPath AddOnPath();
virtual void SetAddOnPath(BPath path);
virtual void SetAccountCachePath(BPath path);
virtual void SetAddOnCachePath(BPath path);
virtual const char* GetName();
virtual void SetName(const char* name);
@ -92,6 +94,8 @@ private:
BString fName;
BPath fAddOnPath;
BPath fAccountCachePath;
BPath fAddOnCachePath;
BString fSignature;
BString fFriendlySignature;

View File

@ -1,6 +1,6 @@
/*
* 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.
*
* Authors:

View File

@ -1,6 +1,6 @@
/*
* 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.
*/
#ifndef _JABBER_HANDLER_H