Chat-O-Matic/application/ProtocolLooper.cpp
Jaidyn Ann f0ce3e87c6 Support generally setting own nick, own UserItems
Seperate UserItems are now created for each list, too, rather than a
single one being created per-user. This functionally works a lot nicer.

But onto more important things… now setting the user's own nick should
work quite well. Finally. =w=

This has given me a good bit of trouble over the past couple of days―
setting the user's nick *worked*, but it wouldn't propagate to its
corresponding UserItem nor its UserInfoDialog. It would, however, work
with the StatusView.

These are all registered Observers of the User itself, so if one works,
they *all* should, them all being registered to the same User.

Now, if a given User isn't found in the ProtocolLooper's user-list,
the Conversation class will take it upon itself to create a new
one and add it to both of their respective lists.

So the user's own contact would be set in the ProtocolLooper― but it
*wouldn't* be added to the user-list.

Hilarity ensues as two seperate objects for the user's own contact would
be created.

Since the StatusView is registered to the ProtocolLooper's "real" own contact
slot, it would receive all updates… but since Conversations' user-lists and
items would be registered to the Conversation-created "fake" user, they
would be borked.

Simple oversight, but wow it hecked with me. :P
2021-08-05 14:28:56 -05:00

230 lines
3.7 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)
{
if (chat != NULL)
fChatMap.AddItem(chat->GetId(), chat);
}
void
ProtocolLooper::RemoveConversation(Conversation* chat)
{
if (chat != NULL)
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)
{
if (contact != NULL)
fRosterMap.AddItem(contact->GetId(), contact);
}
void
ProtocolLooper::RemoveContact(Contact* contact)
{
if (contact == NULL)
return;
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)
{
if (user != NULL)
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)
{
if (contact != NULL) {
fMySelf = contact;
AddUser(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);
}
}