Move command/menu-item registration to call

Registration of custom chat commands and menu-items for protocols was
done by the IM_REGISTER_* messages, and is now done through direct calls
to the CayaProtocol object.

The new model for call/message for protocols is this: Temporary
information (chat messages, roster members, etc.) should be accessed
through messages. Relatively static data (protocol name, commands)
should be accessed through direct calls to the protocol object.
This commit is contained in:
Jaidyn Ann 2021-06-20 01:24:34 -05:00
parent d4f82dccc6
commit b1920dad47
12 changed files with 122 additions and 187 deletions

View File

@ -8,6 +8,7 @@
#define _CAYA_PROTOCOL_H
#include <Messenger.h>
#include <ObjectList.h>
class BBitmap;
@ -39,10 +40,38 @@ public:
virtual status_t UpdateSettings(BMessage*) = 0;
//! Return a settings template
// Currently there are two: "account" (used when creating/editing
// the user's account) and "room" (used when creating a room).
// Currently there are three: "account" (used when creating/editing
// the user's account), "room" (used when creating a room), and "roster"
// (used when adding or editing a roster member.
virtual BMessage SettingsTemplate(const char* name) = 0;
//! Custom chat commands― archived ChatCommand objects
// Requires: String "_name", String "_desc", Bool "_proto",
// Message "_msg", int32s "_argtype",
// String "class" = "ChatCommand"
virtual BObjectList<BMessage> Commands() {
return BObjectList<BMessage>();
}
//! Custom menu items used in the userlist right-click menu.
// Archived BMenuItem with some extra slots.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", Bool "x_priority", int32 "x_perms",
// int32 "x_target_perms", int32 "x_target_antiperms"
virtual BObjectList<BMessage> UserPopUpItems() = 0;
//! Custom menu items used in the conversation-list right-click menu.
// Archived BMenuItem with some extra slots.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", int32 "x_perms"
virtual BObjectList<BMessage> ChatPopUpItems() = 0;
//! Custom menubar items (in the "Protocol" menu).
// Archived BMenuItem with some extra slots.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol"
virtual BObjectList<BMessage> MenuBarItems() = 0;
//! Protocol signature
virtual const char* Signature() const = 0;

View File

@ -388,44 +388,6 @@ enum im_what_code {
//! Protocol is ready to receive messages
IM_PROTOCOL_READY = 1002,
/*
* GUI-related messages
*/
//! Register a chat command →Caya
// Just an archived ChatCommand; if "instance" isn't specified, the command
// is global, rather than protocol-only.
// Requires: String "_name", String "_desc", Bool "_proto",
// Message "_msg", int32s "_argtype",
// String "class" = "ChatCommand"
// Allowed: int64 "instance"
IM_REGISTER_COMMAND = 1100,
//! Register a pop-up item →Caya
// Just an archived BMenuItem with extra slots; if "instance" isn't
// specified, the item is global, rather than protocol-only.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", Bool "x_priority", int32 "x_perms",
// int32 "x_target_perms", int32 "x_target_antiperms"
// Allowed: int64 "instance"
IM_REGISTER_USERLIST_ITEM = 1101,
//! Register a pop-up item →Caya
// Just an archived BMenuItem with extra slots; if "instance" isn't
// specified, the item is global, rather than protocol-only.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", int32 "x_perms"
// Allowed: int64 "instance"
IM_REGISTER_CHATLIST_ITEM = 1102,
//! Register a "Protocol" menu item →Caya
// Just an archived BMenuItem with extra slots; it adds a menu item to
// the menubar's "Protocol" menu.
// Requires: String "_label", Message "_msg", String "class" = "BMenuItem"
// Bool "x_to_protocol", int64 "instance"
IM_REGISTER_MENUBAR_ITEM = 1103
};

View File

@ -14,8 +14,8 @@
#include "Role.h"
void
DefaultCommands(BLooper* target)
BObjectList<BMessage>
DefaultCommands()
{
List<int32> roomUser;
roomUser.AddItem(CMD_ROOM_PARTICIPANT);
@ -85,71 +85,75 @@ DefaultCommands(BLooper* target)
help->SetDesc("List all current commands, or get help for certain command.");
commands.AddItem("help", help);
BObjectList<BMessage> cmds;
for (int i = 0; i < commands.CountItems(); i++) {
BMessage* item = new BMessage(IM_MESSAGE);
item->AddInt32("im_what", IM_REGISTER_COMMAND);
ChatCommand* cmd = commands.ValueAt(i);
BMessage* item = new BMessage();
cmd->Archive(item);
target->PostMessage(item);
cmds.AddItem(item);
}
return cmds;
}
void
DefaultChatPopUpItems(BLooper* target)
BObjectList<BMessage>
DefaultChatPopUpItems()
{
BObjectList<BMessage> items;
BMessage* leave = new BMessage(IM_MESSAGE);
leave->AddInt32("im_what", IM_LEAVE_ROOM);
BMessage* item = new BMessage(IM_MESSAGE);
item->AddInt32("im_what", IM_REGISTER_CHATLIST_ITEM);
item->AddString("class", "BMenuItem");
item->AddString("_label", "Leave chat");
item->AddMessage("_msg", leave);
item->AddBool("x_to_protocol", true);
items.AddItem(item);
target->PostMessage(item);
return items;
}
void
DefaultUserPopUpItems(BLooper* target)
BObjectList<BMessage>
DefaultUserPopUpItems()
{
BObjectList<BMessage> items;
BMessage* infoMsg = new BMessage(CAYA_USER_INFO);
target->PostMessage(_UserMenuItem("User info" B_UTF8_ELLIPSIS, infoMsg, 0,
0, 0, false, false));
items.AddItem(_UserMenuItem("User info" B_UTF8_ELLIPSIS, infoMsg, 0,
0, 0, false, false));
BMessage* kickMsg = new BMessage(IM_MESSAGE);
kickMsg->AddInt32("im_what", IM_ROOM_KICK_PARTICIPANT);
target->PostMessage(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0,
false, true));
items.AddItem(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0,
false, true));
BMessage* banMsg = new BMessage(IM_MESSAGE);
banMsg->AddInt32("im_what", IM_ROOM_BAN_PARTICIPANT);
target->PostMessage(_UserMenuItem("Ban user", banMsg, PERM_BAN, 0, 0, false,
true));
items.AddItem(_UserMenuItem("Ban user", banMsg, PERM_BAN, 0, 0, false,
true));
BMessage* muteMsg = new BMessage(IM_MESSAGE);
muteMsg->AddInt32("im_what", IM_ROOM_MUTE_PARTICIPANT);
target->PostMessage(_UserMenuItem("Mute user", muteMsg, PERM_MUTE,
PERM_WRITE, 0, false, true));
items.AddItem(_UserMenuItem("Mute user", muteMsg, PERM_MUTE,
PERM_WRITE, 0, false, true));
BMessage* unmuteMsg = new BMessage(IM_MESSAGE);
unmuteMsg->AddInt32("im_what", IM_ROOM_UNMUTE_PARTICIPANT);
target->PostMessage(_UserMenuItem("Unmute user", unmuteMsg, PERM_MUTE, 0,
PERM_WRITE, false, true));
items.AddItem(_UserMenuItem("Unmute user", unmuteMsg, PERM_MUTE, 0,
PERM_WRITE, false, true));
BMessage* deafenMsg = new BMessage(IM_MESSAGE);
deafenMsg->AddInt32("im_what", IM_ROOM_DEAFEN_PARTICIPANT);
target->PostMessage(_UserMenuItem("Deafen user", deafenMsg, PERM_DEAFEN,
PERM_READ, 0, false, true));
items.AddItem(_UserMenuItem("Deafen user", deafenMsg, PERM_DEAFEN,
PERM_READ, 0, false, true));
BMessage* undeafenMsg = new BMessage(IM_MESSAGE);
undeafenMsg->AddInt32("im_what", IM_ROOM_UNDEAFEN_PARTICIPANT);
target->PostMessage(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN,
0, PERM_READ, false, true));
items.AddItem(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN,
0, PERM_READ, false, true));
return items;
}
@ -159,8 +163,6 @@ _UserMenuItem(const char* label, BMessage* msg, int32 user_perms,
bool toProtocol)
{
BMessage* item = new BMessage(IM_MESSAGE);
item->AddInt32("im_what", IM_REGISTER_USERLIST_ITEM);
item->AddString("class", "BMenuItem");
item->AddString("_label", label);
item->AddMessage("_msg", msg);

View File

@ -7,13 +7,12 @@
#include <ObjectList.h>
class BLooper;
class BMessage;
void DefaultCommands(BLooper* target);
void DefaultChatPopUpItems(BLooper* target);
void DefaultUserPopUpItems(BLooper* target);
BObjectList<BMessage> DefaultCommands();
BObjectList<BMessage> DefaultChatPopUpItems();
BObjectList<BMessage> DefaultUserPopUpItems();
BMessage* _UserMenuItem(const char* label, BMessage* msg,
int32 user_perms, int32 target_perms,
int32 target_lacks, bool ignorePriority,

View File

@ -29,6 +29,8 @@ ProtocolLooper::ProtocolLooper(CayaProtocol* protocol, int64 instance)
Account* account = reinterpret_cast<Account*>(
protocol->MessengerInterface());
_InitCommands();
BString name(protocol->FriendlySignature());
name << " - " << account->Name();
@ -165,55 +167,6 @@ ProtocolLooper::CommandById(BString id)
}
void
ProtocolLooper::AddCommand(ChatCommand* cmd)
{
fCommands.AddItem(cmd->GetName(), cmd);
}
BObjectList<BMessage>
ProtocolLooper::UserPopUpItems() const
{
return fUserItems;
}
void
ProtocolLooper::AddUserPopUpItem(BMessage* archived)
{
fUserItems.AddItem(archived);
}
BObjectList<BMessage>
ProtocolLooper::ChatPopUpItems() const
{
return fChatItems;
}
void
ProtocolLooper::AddChatPopUpItem(BMessage* archived)
{
fChatItems.AddItem(archived);
}
BObjectList<BMessage>
ProtocolLooper::MenuBarItems() const
{
return fMenuItems;
}
void
ProtocolLooper::AddMenuBarItem(BMessage* archived)
{
fMenuItems.AddItem(archived);
}
BString
ProtocolLooper::GetOwnId()
{
@ -245,3 +198,12 @@ ProtocolLooper::GetListItem()
}
void
ProtocolLooper::_InitCommands()
{
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);
}
}

View File

@ -52,19 +52,6 @@ public:
CommandMap Commands() const;
ChatCommand* CommandById(BString id);
void AddCommand(ChatCommand* cmd);
BObjectList<BMessage>
UserPopUpItems() const;
void AddUserPopUpItem(BMessage* archived);
BObjectList<BMessage>
ChatPopUpItems() const;
void AddChatPopUpItem(BMessage* archived);
BObjectList<BMessage>
MenuBarItems() const;
void AddMenuBarItem(BMessage* archived);
BString GetOwnId();
void SetOwnId(BString user_id);
@ -75,6 +62,8 @@ public:
GetListItem();
private:
void _InitCommands();
CayaProtocol* fProtocol;
int64 fInstance;
@ -83,11 +72,7 @@ private:
ChatMap fChatMap;
RosterMap fRosterMap;
UserMap fUserMap;
CommandMap fCommands;
BObjectList<BMessage> fUserItems;
BObjectList<BMessage> fChatItems;
BObjectList<BMessage> fMenuItems;
ConversationAccountItem*
fListItem;

View File

@ -42,8 +42,15 @@
Server::Server()
:
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE)
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE),
fChatItems(DefaultChatPopUpItems()),
fUserItems(DefaultUserPopUpItems())
{
BObjectList<BMessage> commands = DefaultCommands();
for (int i = 0; i < commands.CountItems(); i++) {
ChatCommand* cmd = new ChatCommand(commands.ItemAt(i));
fCommands.AddItem(cmd->GetName(), cmd);
}
}
@ -535,43 +542,6 @@ Server::ImMessage(BMessage* msg)
break;
}
case IM_REGISTER_COMMAND:
{
ChatCommand* cmd = new ChatCommand(msg);
if (cmd == NULL) break;
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL)
fCommands.AddItem(cmd->GetName(), cmd);
else
looper->AddCommand(cmd);
break;
}
case IM_REGISTER_USERLIST_ITEM:
{
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL)
fUserItems.AddItem(new BMessage(*msg));
else
looper->AddUserPopUpItem(new BMessage(*msg));
break;
}
case IM_REGISTER_CHATLIST_ITEM:
{
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL)
fChatItems.AddItem(new BMessage(*msg));
else
looper->AddChatPopUpItem(new BMessage(*msg));
break;
}
case IM_REGISTER_MENUBAR_ITEM:
{
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper != NULL)
looper->AddMenuBarItem(new BMessage(*msg));
break;
}
case IM_PROTOCOL_READY:
{
// Ready notification
@ -956,5 +926,3 @@ Server::_GetRole(BMessage* msg)
return new Role(title, perms, priority);
}

View File

@ -167,7 +167,7 @@ ConversationListView::_ConversationPopUp()
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
BObjectList<BMessage> items = server->ChatPopUpItems();
BObjectList<BMessage> protoItems = looper->ChatPopUpItems();
BObjectList<BMessage> protoItems = looper->Protocol()->ChatPopUpItems();
items.AddList(&protoItems);
for (int i = 0; i < items.CountItems(); i++) {

View File

@ -65,7 +65,7 @@ UserListView::_UserPopUp()
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
BObjectList<BMessage> items = server->UserPopUpItems();
BObjectList<BMessage> protoItems = fChat->GetProtocolLooper()->UserPopUpItems();
BObjectList<BMessage> protoItems = fChat->GetProtocolLooper()->Protocol()->UserPopUpItems();
items.AddList(&protoItems);
for (int i = 0; i < items.CountItems(); i++) {

View File

@ -54,11 +54,6 @@ MainWindow::MainWindow()
fServer = new Server();
AddFilter(fServer);
// Register default commands & items
DefaultCommands(this);
DefaultUserPopUpItems(this);
DefaultChatPopUpItems(this);
// Also through the editing filter (enter to send)
AddCommonFilter(new EditingFilter(fSendView));
fSendView->MakeFocus(true);
@ -382,7 +377,7 @@ MainWindow::SetConversation(Conversation* chat)
// Add and populate "Protocol" menu, if appropriate
if (fConversation != NULL) {
ProtocolLooper* looper = fConversation->GetProtocolLooper();
BObjectList<BMessage> menuItems = looper->MenuBarItems();
BObjectList<BMessage> menuItems = looper->Protocol()->MenuBarItems();
for (int i = 0; i < menuItems.CountItems(); i++) {
BMessage* itemMsg = menuItems.ItemAt(i);
BMessage* msg = new BMessage(*itemMsg);

View File

@ -345,6 +345,34 @@ JabberHandler::GetName()
}
BObjectList<BMessage>
JabberHandler::Commands()
{
return BObjectList<BMessage>();
}
BObjectList<BMessage>
JabberHandler::ChatPopUpItems()
{
return BObjectList<BMessage>();
}
BObjectList<BMessage>
JabberHandler::UserPopUpItems()
{
return BObjectList<BMessage>();
}
BObjectList<BMessage>
JabberHandler::MenuBarItems()
{
return BObjectList<BMessage>();
}
status_t
JabberHandler::UpdateSettings(BMessage* msg)
{

View File

@ -68,6 +68,11 @@ public:
virtual void SetName(const char* name);
virtual const char* GetName();
virtual BObjectList<BMessage> Commands();
virtual BObjectList<BMessage> ChatPopUpItems();
virtual BObjectList<BMessage> UserPopUpItems();
virtual BObjectList<BMessage> MenuBarItems();
virtual status_t UpdateSettings(BMessage* msg);
virtual uint32 GetEncoding();