Slight redesign of add-on loading

This commit is contained in:
Jaidyn Ann 2021-06-11 21:43:50 -05:00
parent dcc5e443d3
commit fe2f2ecad2
2 changed files with 58 additions and 21 deletions

View File

@ -45,17 +45,16 @@ ProtocolManager::Init(BDirectory dir, BHandler* target)
if (addOn->Version() != CAYA_VERSION) if (addOn->Version() != CAYA_VERSION)
continue; continue;
fAddOnMap.AddItem(addOn->Signature(), addOn);
_GetAccounts(addOn, addOn->Signature(), target);
// If add-on has multiple protocols, also load them // If add-on has multiple protocols, also load them
for (int32 i = 1; i < addOn->CountProtocols(); i++) { for (int32 i = 0; i < addOn->CountProtocols(); i++) {
CayaProtocolAddOn* subAddOn = CayaProtocolAddOn* subAddOn = addOn;
new CayaProtocolAddOn(id,path.Path(), i); if (i > 0)
subAddOn = new CayaProtocolAddOn(id, path.Path(), i);
CayaProtocol* proto = subAddOn->Protocol(); CayaProtocol* proto = subAddOn->Protocol();
fAddOnMap.AddItem(proto->Signature(), subAddOn); fAddOnMap.AddItem(proto->Signature(), subAddOn);
_GetAccounts(subAddOn, proto->Signature(), target); _LoadAccounts(path.Path(), subAddOn, i, target);
delete proto; delete proto;
} }
} }
@ -134,28 +133,60 @@ ProtocolManager::AddAccount(CayaProtocolAddOn* addOn, const char* account,
void void
ProtocolManager::_GetAccounts(CayaProtocolAddOn* addOn, const char* subProtocol, ProtocolManager::_LoadAccounts(const char* image_path, CayaProtocolAddOn* addOn,
BHandler* target) int protoIndex, BHandler* target)
{ {
// Find accounts path for this protocol // Find accounts path for this protocol
BPath path(CayaAccountPath(addOn->Signature(), subProtocol)); BPath path(CayaAccountPath(addOn->Signature(), addOn->Protocol()->Signature()));
if (path.InitCheck() != B_OK) if (path.InitCheck() != B_OK)
return; return;
BDirectory dir(path.Path()); BDirectory dir(path.Path());
BEntry entry; BEntry entry;
bool firstDone = false;
while (dir.GetNextEntry(&entry) == B_OK) { while (dir.GetNextEntry(&entry) == B_OK) {
BFile file(&entry, B_READ_ONLY); // if (firstDone == false) {
_LoadAccount(addOn, entry, target);
// firstDone = true;
// }
// else
// _LoadAccount(image_path, entry, protoIndex, target);
}
}
void
ProtocolManager::_LoadAccount(const char* imagePath, BEntry accountEntry,
int protoIndex, BHandler* target)
{
image_id id = load_add_on(imagePath);
if (id < 0)
return;
// If add-on's API version fits then load accounts...
CayaProtocolAddOn* addOn = new CayaProtocolAddOn(id, imagePath, protoIndex);
if (addOn->Version() != CAYA_VERSION)
return;
_LoadAccount(addOn, accountEntry, target);
}
void
ProtocolManager::_LoadAccount(CayaProtocolAddOn* addOn, BEntry accountEntry,
BHandler* target)
{
BFile file(&accountEntry, B_READ_ONLY);
BMessage msg; BMessage msg;
if (msg.Unflatten(&file) == B_OK) { if (msg.Unflatten(&file) == B_OK) {
char buffer[B_PATH_NAME_LENGTH]; char buffer[B_PATH_NAME_LENGTH];
if (entry.GetName(buffer) == B_OK) { if (accountEntry.GetName(buffer) == B_OK) {
printf("Found %s for protocol %s!\n", buffer, subProtocol); printf("Found %s for protocol %s!\n", buffer, addOn->Protocol()->Signature());
AddAccount(addOn, buffer, target); AddAccount(addOn, buffer, target);
} }
} }
}
} }

View File

@ -41,9 +41,15 @@ private:
typedef KeyMap<bigtime_t, CayaProtocol*> ProtocolMap; typedef KeyMap<bigtime_t, CayaProtocol*> ProtocolMap;
ProtocolManager(); ProtocolManager();
void _GetAccounts(CayaProtocolAddOn* addOn,
const char* subProtocol, void _LoadAccounts(const char* image_path,
CayaProtocolAddOn* addOn, int protoIndex,
BHandler* target); BHandler* target);
void _LoadAccount(const char* imagePath,
BEntry accountEntry, int protoIndex,
BHandler* target);
void _LoadAccount(CayaProtocolAddOn* addOn,
BEntry accountEntry, BHandler* target);
AddOnMap fAddOnMap; AddOnMap fAddOnMap;
ProtocolMap fProtocolMap; ProtocolMap fProtocolMap;