From b1920dad47b4e4b49a9a72bb60d0727d2a7edeb9 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sun, 20 Jun 2021 01:24:34 -0500 Subject: [PATCH] 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. --- application/CayaProtocol.h | 33 +++++++++++- application/CayaProtocolMessages.h | 38 -------------- application/DefaultItems.cpp | 60 +++++++++++----------- application/DefaultItems.h | 7 ++- application/ProtocolLooper.cpp | 60 ++++------------------ application/ProtocolLooper.h | 19 +------ application/Server.cpp | 48 +++-------------- application/views/ConversationListView.cpp | 2 +- application/views/UserListView.cpp | 2 +- application/windows/MainWindow.cpp | 7 +-- protocols/xmpp/JabberHandler.cpp | 28 ++++++++++ protocols/xmpp/JabberHandler.h | 5 ++ 12 files changed, 122 insertions(+), 187 deletions(-) diff --git a/application/CayaProtocol.h b/application/CayaProtocol.h index 05440f7..f03cd71 100644 --- a/application/CayaProtocol.h +++ b/application/CayaProtocol.h @@ -8,6 +8,7 @@ #define _CAYA_PROTOCOL_H #include +#include 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 Commands() { + return BObjectList(); + } + + //! 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 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 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 MenuBarItems() = 0; + //! Protocol signature virtual const char* Signature() const = 0; diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index ca60625..f254bed 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -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 }; diff --git a/application/DefaultItems.cpp b/application/DefaultItems.cpp index c064015..390c1cd 100644 --- a/application/DefaultItems.cpp +++ b/application/DefaultItems.cpp @@ -14,8 +14,8 @@ #include "Role.h" -void -DefaultCommands(BLooper* target) +BObjectList +DefaultCommands() { List 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 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 +DefaultChatPopUpItems() { + BObjectList 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); - - target->PostMessage(item); + items.AddItem(item); + + return items; } -void -DefaultUserPopUpItems(BLooper* target) +BObjectList +DefaultUserPopUpItems() { BObjectList 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); diff --git a/application/DefaultItems.h b/application/DefaultItems.h index f64f38a..4c65462 100644 --- a/application/DefaultItems.h +++ b/application/DefaultItems.h @@ -7,13 +7,12 @@ #include -class BLooper; class BMessage; -void DefaultCommands(BLooper* target); -void DefaultChatPopUpItems(BLooper* target); -void DefaultUserPopUpItems(BLooper* target); +BObjectList DefaultCommands(); +BObjectList DefaultChatPopUpItems(); +BObjectList DefaultUserPopUpItems(); BMessage* _UserMenuItem(const char* label, BMessage* msg, int32 user_perms, int32 target_perms, int32 target_lacks, bool ignorePriority, diff --git a/application/ProtocolLooper.cpp b/application/ProtocolLooper.cpp index f40a5fc..9214e32 100644 --- a/application/ProtocolLooper.cpp +++ b/application/ProtocolLooper.cpp @@ -29,6 +29,8 @@ ProtocolLooper::ProtocolLooper(CayaProtocol* protocol, int64 instance) Account* account = reinterpret_cast( 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 -ProtocolLooper::UserPopUpItems() const -{ - return fUserItems; -} - - -void -ProtocolLooper::AddUserPopUpItem(BMessage* archived) -{ - fUserItems.AddItem(archived); -} - - -BObjectList -ProtocolLooper::ChatPopUpItems() const -{ - return fChatItems; -} - - -void -ProtocolLooper::AddChatPopUpItem(BMessage* archived) -{ - fChatItems.AddItem(archived); -} - - -BObjectList -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 commands = fProtocol->Commands(); + for (int i = 0; i < commands.CountItems(); i++) { + ChatCommand* cmd = new ChatCommand(commands.ItemAt(i)); + fCommands.AddItem(cmd->GetName(), cmd); + } +} diff --git a/application/ProtocolLooper.h b/application/ProtocolLooper.h index 06a3460..520333f 100644 --- a/application/ProtocolLooper.h +++ b/application/ProtocolLooper.h @@ -52,19 +52,6 @@ public: CommandMap Commands() const; ChatCommand* CommandById(BString id); - void AddCommand(ChatCommand* cmd); - - BObjectList - UserPopUpItems() const; - void AddUserPopUpItem(BMessage* archived); - - BObjectList - ChatPopUpItems() const; - void AddChatPopUpItem(BMessage* archived); - - BObjectList - 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 fUserItems; - BObjectList fChatItems; - BObjectList fMenuItems; ConversationAccountItem* fListItem; diff --git a/application/Server.cpp b/application/Server.cpp index 7ee6fc0..a339777 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -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 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); } - - diff --git a/application/views/ConversationListView.cpp b/application/views/ConversationListView.cpp index 5b519c4..96ac4ab 100644 --- a/application/views/ConversationListView.cpp +++ b/application/views/ConversationListView.cpp @@ -167,7 +167,7 @@ ConversationListView::_ConversationPopUp() Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer(); BObjectList items = server->ChatPopUpItems(); - BObjectList protoItems = looper->ChatPopUpItems(); + BObjectList protoItems = looper->Protocol()->ChatPopUpItems(); items.AddList(&protoItems); for (int i = 0; i < items.CountItems(); i++) { diff --git a/application/views/UserListView.cpp b/application/views/UserListView.cpp index fdfa3ab..a2809a1 100644 --- a/application/views/UserListView.cpp +++ b/application/views/UserListView.cpp @@ -65,7 +65,7 @@ UserListView::_UserPopUp() Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer(); BObjectList items = server->UserPopUpItems(); - BObjectList protoItems = fChat->GetProtocolLooper()->UserPopUpItems(); + BObjectList protoItems = fChat->GetProtocolLooper()->Protocol()->UserPopUpItems(); items.AddList(&protoItems); for (int i = 0; i < items.CountItems(); i++) { diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index 9c49006..a2f3f6e 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -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 menuItems = looper->MenuBarItems(); + BObjectList menuItems = looper->Protocol()->MenuBarItems(); for (int i = 0; i < menuItems.CountItems(); i++) { BMessage* itemMsg = menuItems.ItemAt(i); BMessage* msg = new BMessage(*itemMsg); diff --git a/protocols/xmpp/JabberHandler.cpp b/protocols/xmpp/JabberHandler.cpp index c6fd730..843ef50 100644 --- a/protocols/xmpp/JabberHandler.cpp +++ b/protocols/xmpp/JabberHandler.cpp @@ -345,6 +345,34 @@ JabberHandler::GetName() } +BObjectList +JabberHandler::Commands() +{ + return BObjectList(); +} + + +BObjectList +JabberHandler::ChatPopUpItems() +{ + return BObjectList(); +} + + +BObjectList +JabberHandler::UserPopUpItems() +{ + return BObjectList(); +} + + +BObjectList +JabberHandler::MenuBarItems() +{ + return BObjectList(); +} + + status_t JabberHandler::UpdateSettings(BMessage* msg) { diff --git a/protocols/xmpp/JabberHandler.h b/protocols/xmpp/JabberHandler.h index 90494b0..64ac4fd 100644 --- a/protocols/xmpp/JabberHandler.h +++ b/protocols/xmpp/JabberHandler.h @@ -68,6 +68,11 @@ public: virtual void SetName(const char* name); virtual const char* GetName(); + virtual BObjectList Commands(); + virtual BObjectList ChatPopUpItems(); + virtual BObjectList UserPopUpItems(); + virtual BObjectList MenuBarItems(); + virtual status_t UpdateSettings(BMessage* msg); virtual uint32 GetEncoding();