Chat-O-Matic/application/ProtocolManager.cpp

147 lines
2.8 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 <Directory.h>
#include <Entry.h>
#include <Handler.h>
#include "Account.h"
#include "ProtocolManager.h"
#include "CayaProtocol.h"
#include "CayaUtils.h"
#include "MainWindow.h"
#include "Server.h"
#include "TheApp.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...
CayaProtocolAddOn* addOn = new CayaProtocolAddOn(id, path.Path());
2010-07-10 14:28:29 -05:00
if (addOn->Protocol()->Version() == CAYA_VERSION) {
fAddOnMap.AddItem(addOn->Signature(), addOn);
_GetAccounts(addOn, target);
}
}
}
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();
}
2010-05-28 12:11:16 -05:00
CayaProtocolAddOn*
ProtocolManager::ProtocolAddOnAt(uint32 i) const
{
2010-05-28 12:11:16 -05:00
return fAddOnMap.ValueAt(i);
}
2010-05-28 12:11:16 -05:00
CayaProtocolAddOn*
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();
}
CayaProtocol*
ProtocolManager::ProtocolInstanceAt(uint32 i) const
{
return fProtocolMap.ValueAt(i);
}
CayaProtocol*
ProtocolManager::ProtocolInstance(bigtime_t identifier)
{
return fProtocolMap.ValueFor(identifier);
}
void
ProtocolManager::AddAccount(CayaProtocolAddOn* addOn, const char* account,
BHandler* target)
{
bigtime_t instanceId = system_time();
CayaProtocol* cayap = addOn->Protocol();
(void)new Account(instanceId, cayap, account, target);
fProtocolMap.AddItem(instanceId, cayap);
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
theApp->GetMainWindow()->GetServer()->AddProtocolLooper(
instanceId, cayap);
}
void
ProtocolManager::_GetAccounts(CayaProtocolAddOn* addOn, BHandler* target)
{
// Find accounts path for this protocol
BPath path(CayaAccountPath(addOn->Signature()));
if (path.InitCheck() != B_OK)
return;
BDirectory dir(path.Path());
BEntry entry;
while (dir.GetNextEntry(&entry) == B_OK) {
BFile file(&entry, B_READ_ONLY);
BMessage msg;
if (msg.Unflatten(&file) == B_OK) {
char buffer[B_PATH_NAME_LENGTH];
if (entry.GetName(buffer) == B_OK) {
printf("Found %s for protocol %s!\n", buffer, addOn->Signature());
AddAccount(addOn, buffer, target);
}
}
}
}