4905dbbe6c
Currently, add-ons are disconnected when ChatProtocol::Shutdown() is called, which the add-on can do by itself― but there is no standard way for add-ons to notify the app about their Shutdown. Because of this, they tend to not call Shutdown()― instead (as in the case of the Jabber add-on), they display a BAlert (IM_ERROR) notifying the user of the connection error, but the account is considered active by Cardie (and its threads are still existant, including its ProtocolLooper). Zombies are bad, so this is redesigned somewhat with this commit: Protocols should no longer call ChatProtocol::Shutdown() themselves, they must send an IM_MESSAGE of IM_PROTOCOL_DISABLE to the app. This will delete its ProtocolLooper, which in turn will send a notification to the user and delete the ChatProtocol, and so calling ChatProtocol::Shutdown(). In the included protocols, an IM_ERROR is sent right before IM_PROTOCOL_DISABLE is sent if due to a connection error. This is not required, but it is courteous to inform your user about the "why." :)
221 lines
3.5 KiB
C++
221 lines
3.5 KiB
C++
/*
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*
|
|
* Authors:
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
* Jaidyn Levesque, jadedctrl@teknik.io
|
|
*/
|
|
|
|
#include "ProtocolLooper.h"
|
|
|
|
#include <Bitmap.h>
|
|
#include <String.h>
|
|
|
|
#include "Account.h"
|
|
#include "AppMessages.h"
|
|
#include "Conversation.h"
|
|
#include "ConversationAccountItem.h"
|
|
|
|
|
|
ProtocolLooper::ProtocolLooper(ChatProtocol* protocol, int64 instance)
|
|
:
|
|
BLooper(),
|
|
fProtocol(protocol),
|
|
fInstance(instance),
|
|
fListItem(NULL),
|
|
fMySelf(NULL)
|
|
{
|
|
Account* account = reinterpret_cast<Account*>(
|
|
protocol->MessengerInterface());
|
|
|
|
LoadCommands();
|
|
|
|
BString name(protocol->FriendlySignature());
|
|
name << " - " << account->Name();
|
|
|
|
SetName(name.String());
|
|
Run();
|
|
}
|
|
|
|
|
|
ProtocolLooper::~ProtocolLooper()
|
|
{
|
|
BMessage* msg = new BMessage(APP_ACCOUNT_DISABLED);
|
|
BBitmap* icon = fProtocol->Icon();
|
|
|
|
icon->Archive(msg);
|
|
msg->AddString("name", fProtocol->GetName());
|
|
fProtocol->MessengerInterface()->SendMessage(msg);
|
|
|
|
fProtocol->Shutdown();
|
|
delete fProtocol;
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::MessageReceived(BMessage* msg)
|
|
{
|
|
if (Protocol()->Process(msg) != B_OK)
|
|
BLooper::MessageReceived(msg);
|
|
}
|
|
|
|
|
|
ChatProtocol*
|
|
ProtocolLooper::Protocol()
|
|
{
|
|
return fProtocol;
|
|
}
|
|
|
|
|
|
ChatMap
|
|
ProtocolLooper::Conversations() const
|
|
{
|
|
return fChatMap;
|
|
}
|
|
|
|
|
|
Conversation*
|
|
ProtocolLooper::ConversationById(BString id)
|
|
{
|
|
bool found = false;
|
|
return fChatMap.ValueFor(id, &found);
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::AddConversation(Conversation* chat)
|
|
{
|
|
fChatMap.AddItem(chat->GetId(), chat);
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::RemoveConversation(Conversation* chat)
|
|
{
|
|
fChatMap.RemoveItemFor(chat->GetId());
|
|
}
|
|
|
|
|
|
RosterMap
|
|
ProtocolLooper::Contacts() const
|
|
{
|
|
return fRosterMap;
|
|
}
|
|
|
|
|
|
Contact*
|
|
ProtocolLooper::ContactById(BString id)
|
|
{
|
|
bool found = false;
|
|
return fRosterMap.ValueFor(id, &found);
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::AddContact(Contact* contact)
|
|
{
|
|
fRosterMap.AddItem(contact->GetId(), contact);
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::RemoveContact(Contact* contact)
|
|
{
|
|
fRosterMap.RemoveItemFor(contact->GetId());
|
|
fUserMap.AddItem(contact->GetId(), (User*)contact);
|
|
}
|
|
|
|
|
|
UserMap
|
|
ProtocolLooper::Users() const
|
|
{
|
|
UserMap users = fUserMap;
|
|
|
|
for (int i = 0; i < fRosterMap.CountItems(); i++) {
|
|
User* user = (User*)fRosterMap.ValueAt(i);
|
|
users.AddItem(user->GetId(), user);
|
|
}
|
|
|
|
return users;
|
|
}
|
|
|
|
|
|
User*
|
|
ProtocolLooper::UserById(BString id)
|
|
{
|
|
bool found = false;
|
|
User* user = ContactById(id);
|
|
if (user == NULL)
|
|
user = fUserMap.ValueFor(id, &found);
|
|
|
|
return user;
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::AddUser(User* user)
|
|
{
|
|
fUserMap.AddItem(user->GetId(), user);
|
|
}
|
|
|
|
|
|
CommandMap
|
|
ProtocolLooper::Commands() const
|
|
{
|
|
return fCommands;
|
|
}
|
|
|
|
|
|
ChatCommand*
|
|
ProtocolLooper::CommandById(BString id)
|
|
{
|
|
return fCommands.ValueFor(id);
|
|
}
|
|
|
|
|
|
Contact*
|
|
ProtocolLooper::GetOwnContact()
|
|
{
|
|
return fMySelf;
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::SetOwnContact(Contact* contact)
|
|
{
|
|
fMySelf = contact;
|
|
}
|
|
|
|
|
|
int64
|
|
ProtocolLooper::GetInstance()
|
|
{
|
|
return fInstance;
|
|
}
|
|
|
|
|
|
ConversationAccountItem*
|
|
ProtocolLooper::GetListItem()
|
|
{
|
|
if (fListItem == NULL)
|
|
fListItem = new ConversationAccountItem(fProtocol->GetName(),
|
|
fInstance);
|
|
return fListItem;
|
|
}
|
|
|
|
|
|
void
|
|
ProtocolLooper::LoadCommands()
|
|
{
|
|
fCommands = CommandMap();
|
|
BObjectList<BMessage> commands = fProtocol->Commands();
|
|
for (int i = 0; i < commands.CountItems(); i++) {
|
|
ChatCommand* cmd = new ChatCommand(commands.ItemAt(i));
|
|
fCommands.AddItem(cmd->GetName(), cmd);
|
|
}
|
|
}
|