Chat-O-Matic/application/ProtocolManager.cpp

201 lines
4.1 KiB
C++
Raw Normal View History

/*
2011-12-03 16:38:03 -06:00
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrea Anzani, andrea.anzani@gmail.com
*/
#include <stdio.h>
#include <image.h>
#include <Bitmap.h>
#include <Directory.h>
#include <Entry.h>
#include <Handler.h>
#include "Account.h"
#include "AppMessages.h"
#include "ProtocolManager.h"
2021-06-20 12:44:20 -05:00
#include "ChatProtocol.h"
#include "MainWindow.h"
#include "Server.h"
#include "TheApp.h"
2021-06-20 12:44:20 -05:00
#include "Utils.h"
static ProtocolManager* fInstance = NULL;
void
ProtocolManager::Init(BDirectory dir, BHandler* target)
{
BEntry entry;
BPath path;
dir.Rewind();
while (dir.GetNextEntry(&entry) == B_OK) {
path = BPath(&entry);
// Load protocol addon
image_id id = load_add_on(path.Path());
if (id < 0)
continue;
2010-07-10 14:28:29 -05:00
// If add-on's API version fits then load accounts...
2021-06-20 12:44:20 -05:00
ChatProtocolAddOn* addOn = new ChatProtocolAddOn(id, path.Path());
if (addOn->Version() != APP_VERSION)
continue;
// If add-on has multiple protocols, also load them
2021-06-11 21:43:50 -05:00
for (int32 i = 0; i < addOn->CountProtocols(); i++) {
2021-06-20 12:44:20 -05:00
ChatProtocolAddOn* subAddOn = addOn;
2021-06-11 21:43:50 -05:00
if (i > 0)
2021-06-20 12:44:20 -05:00
subAddOn = new ChatProtocolAddOn(id, path.Path(), i);
2021-06-11 21:43:50 -05:00
2021-06-20 12:44:20 -05:00
ChatProtocol* proto = subAddOn->Protocol();
fAddOnMap.AddItem(proto->Signature(), subAddOn);
2021-06-11 21:43:50 -05:00
_LoadAccounts(path.Path(), subAddOn, i, target);
delete proto;
2010-07-10 14:28:29 -05:00
}
}
}
ProtocolManager::ProtocolManager()
{
}
ProtocolManager*
ProtocolManager::Get()
{
if (fInstance == NULL)
fInstance = new ProtocolManager();
return fInstance;
}
2010-05-28 12:11:16 -05:00
uint32
ProtocolManager::CountProtocolAddOns() const
{
2010-05-28 12:11:16 -05:00
return fAddOnMap.CountItems();
}
2021-06-20 12:44:20 -05:00
ChatProtocolAddOn*
2010-05-28 12:11:16 -05:00
ProtocolManager::ProtocolAddOnAt(uint32 i) const
{
2010-05-28 12:11:16 -05:00
return fAddOnMap.ValueAt(i);
}
2021-06-20 12:44:20 -05:00
ChatProtocolAddOn*
2010-05-28 12:11:16 -05:00
ProtocolManager::ProtocolAddOn(const char* signature)
{
2010-05-28 12:11:16 -05:00
return fAddOnMap.ValueFor(signature);
}
2010-05-28 12:11:16 -05:00
uint32
ProtocolManager::CountProtocolInstances() const
{
2010-05-28 12:11:16 -05:00
return fProtocolMap.CountItems();
}
2021-06-20 12:44:20 -05:00
ChatProtocol*
2010-05-28 12:11:16 -05:00
ProtocolManager::ProtocolInstanceAt(uint32 i) const
{
return fProtocolMap.ValueAt(i);
}
2021-06-20 12:44:20 -05:00
ChatProtocol*
2010-05-28 12:11:16 -05:00
ProtocolManager::ProtocolInstance(bigtime_t identifier)
{
return fProtocolMap.ValueFor(identifier);
}
void
2021-06-20 12:44:20 -05:00
ProtocolManager::AddAccount(ChatProtocolAddOn* addOn, const char* account,
BHandler* target)
{
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
bigtime_t instanceId = system_time();
2021-06-20 12:44:20 -05:00
ChatProtocol* cayap = addOn->Protocol();
Account* acc =
new Account(instanceId, cayap, account, addOn->Signature(), target);
// Send a "whoops" notification if hits a failure
if (acc->InitCheck() != B_OK) {
BMessage error(APP_ACCOUNT_FAILED);
cayap->Icon()->Archive(&error);
error.AddString("name", account);
theApp->GetMainWindow()->MessageReceived(&error);
return;
}
fProtocolMap.AddItem(instanceId, cayap);
theApp->GetMainWindow()->GetServer()->AddProtocolLooper(
instanceId, cayap);
}
void
2021-06-20 12:44:20 -05:00
ProtocolManager::_LoadAccounts(const char* image_path, ChatProtocolAddOn* addOn,
2021-06-11 21:43:50 -05:00
int protoIndex, BHandler* target)
{
// Find accounts path for this protocol
2021-06-20 12:44:20 -05:00
BPath path(AccountPath(addOn->Signature(), addOn->Protocol()->Signature()));
if (path.InitCheck() != B_OK)
return;
BDirectory dir(path.Path());
BEntry entry;
2021-06-11 21:43:50 -05:00
bool firstDone = false;
while (dir.GetNextEntry(&entry) == B_OK) {
2021-06-11 21:43:50 -05:00
_LoadAccount(addOn, entry, 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...
2021-06-20 12:44:20 -05:00
ChatProtocolAddOn* addOn = new ChatProtocolAddOn(id, imagePath, protoIndex);
if (addOn->Version() != APP_VERSION)
2021-06-11 21:43:50 -05:00
return;
_LoadAccount(addOn, accountEntry, target);
}
void
2021-06-20 12:44:20 -05:00
ProtocolManager::_LoadAccount(ChatProtocolAddOn* addOn, BEntry accountEntry,
2021-06-11 21:43:50 -05:00
BHandler* target)
{
BFile file(&accountEntry, B_READ_ONLY);
BMessage msg;
if (msg.Unflatten(&file) == B_OK) {
char buffer[B_PATH_NAME_LENGTH];
if (accountEntry.GetName(buffer) == B_OK) {
printf("Found %s for protocol %s!\n", buffer, addOn->Protocol()->Signature());
AddAccount(addOn, buffer, target);
}
}
}