From 8a9cb9effd1828fa6b03e427bb14db04bf2b7880 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 15 Jun 2021 23:29:38 -0500 Subject: [PATCH] Register commands & pop-up items through messages Use IM_MESSAGEs, IM_REGISTER_COMMAND and IM_REGISTER_USER_ITEM, for registering commands and user-list pop-up items. The former replaces CayaProtocol::Commands(), which was a real bad idea in the first place. Just awful. No idea why I did that instead of this, which is nicer and significantly easier anyway. --- application/CayaProtocol.h | 5 --- application/CayaProtocolMessages.h | 25 ++++++++++++++- application/DefaultItems.cpp | 50 ++++++++++++++++++------------ application/DefaultItems.h | 9 +++--- application/ProtocolLooper.cpp | 24 ++++++++++++-- application/ProtocolLooper.h | 8 +++++ application/Server.cpp | 25 +++++++++++++-- application/windows/MainWindow.cpp | 8 ++++- protocols/xmpp/JabberHandler.cpp | 7 ----- protocols/xmpp/JabberHandler.h | 3 -- 10 files changed, 118 insertions(+), 46 deletions(-) diff --git a/application/CayaProtocol.h b/application/CayaProtocol.h index b61ea00..4205591 100644 --- a/application/CayaProtocol.h +++ b/application/CayaProtocol.h @@ -9,8 +9,6 @@ #include -#include "ChatCommand.h" - class BBitmap; @@ -65,9 +63,6 @@ public: //! Messenger interface used virtual CayaProtocolMessengerInterface* MessengerInterface() const = 0; - - //! Return a map of any custom commands - virtual CommandMap Commands() = 0; }; #endif // _CAYA_PROTOCOL_H diff --git a/application/CayaProtocolMessages.h b/application/CayaProtocolMessages.h index 8f5b665..4a1a36b 100644 --- a/application/CayaProtocolMessages.h +++ b/application/CayaProtocolMessages.h @@ -365,7 +365,30 @@ enum im_what_code { IM_SPECIAL_FROM_PROTOCOL = 1001, //! Protocol is ready to receive messages - IM_PROTOCOL_READY = 1002 + 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_USER_ITEM = 1101 }; diff --git a/application/DefaultItems.cpp b/application/DefaultItems.cpp index f199c4f..68193e3 100644 --- a/application/DefaultItems.cpp +++ b/application/DefaultItems.cpp @@ -5,15 +5,17 @@ #include "DefaultItems.h" +#include #include #include "CayaMessages.h" #include "CayaProtocolMessages.h" +#include "ChatCommand.h" #include "Role.h" -CommandMap -DefaultCommands() +void +DefaultCommands(BLooper* target) { List roomUser; roomUser.AddItem(CMD_ROOM_PARTICIPANT); @@ -83,48 +85,54 @@ DefaultCommands() help->SetDesc("List all current commands, or get help for certain command."); commands.AddItem("help", help); - return commands; + 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); + cmd->Archive(item); + target->PostMessage(item); + } } -BObjectList -DefaultUserPopUpItems() +void +DefaultUserPopUpItems(BLooper* target) { BObjectList items; BMessage* infoMsg = new BMessage(CAYA_USER_INFO); - items.AddItem(_UserMenuItem("User info" B_UTF8_ELLIPSIS, infoMsg, 0, 0, 0, - false, false)); + target->PostMessage(_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); - items.AddItem(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0, false, - true)); + target->PostMessage(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0, + false, true)); BMessage* banMsg = new BMessage(IM_MESSAGE); banMsg->AddInt32("im_what", IM_ROOM_BAN_PARTICIPANT); - items.AddItem(_UserMenuItem("Ban user", banMsg, PERM_BAN, 0, 0, false, true)); + target->PostMessage(_UserMenuItem("Ban user", banMsg, PERM_BAN, 0, 0, false, + true)); BMessage* muteMsg = new BMessage(IM_MESSAGE); muteMsg->AddInt32("im_what", IM_ROOM_MUTE_PARTICIPANT); - items.AddItem(_UserMenuItem("Mute user", muteMsg, PERM_MUTE, PERM_WRITE, 0, - false, true)); + target->PostMessage(_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); - items.AddItem(_UserMenuItem("Unmute user", unmuteMsg, PERM_MUTE, 0, - PERM_WRITE, false, true)); + target->PostMessage(_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); - items.AddItem(_UserMenuItem("Deafen user", deafenMsg, PERM_DEAFEN, PERM_READ, - 0, false, true)); + target->PostMessage(_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); - items.AddItem(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN, 0, - PERM_READ, false, true)); - return items; + target->PostMessage(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN, + 0, PERM_READ, false, true)); } @@ -133,7 +141,9 @@ _UserMenuItem(const char* label, BMessage* msg, int32 user_perms, int32 target_perms, int32 target_lacks, bool ignorePriority, bool toProtocol) { - BMessage* item = new BMessage(); + BMessage* item = new BMessage(IM_MESSAGE); + item->AddInt32("im_what", IM_REGISTER_USER_ITEM); + item->AddString("class", "BMenuItem"); item->AddString("_label", label); item->AddMessage("_msg", msg); diff --git a/application/DefaultItems.h b/application/DefaultItems.h index 3309821..93fa0cb 100644 --- a/application/DefaultItems.h +++ b/application/DefaultItems.h @@ -5,13 +5,14 @@ #ifndef DEFAULTITEMS_H #define DEFAULTITEMS_H -#include "ChatCommand.h" - #include +class BLooper; +class BMessage; -CommandMap DefaultCommands(); -BObjectList DefaultUserPopUpItems(); + +void DefaultCommands(BLooper* target); +void DefaultUserPopUpItems(BLooper* target); 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 fc85826..fdad8e1 100644 --- a/application/ProtocolLooper.cpp +++ b/application/ProtocolLooper.cpp @@ -24,8 +24,7 @@ ProtocolLooper::ProtocolLooper(CayaProtocol* protocol, int64 instance) BLooper(), fProtocol(protocol), fInstance(instance), - fListItem(NULL), - fCommands(protocol->Commands()) + fListItem(NULL) { Account* account = reinterpret_cast( protocol->MessengerInterface()); @@ -158,6 +157,27 @@ 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); +} + + BString ProtocolLooper::GetOwnId() { diff --git a/application/ProtocolLooper.h b/application/ProtocolLooper.h index 9f4f69e..9d3f1fe 100644 --- a/application/ProtocolLooper.h +++ b/application/ProtocolLooper.h @@ -8,6 +8,7 @@ #define _PROTOCOL_LOOPER_H #include +#include #include #include @@ -50,6 +51,11 @@ public: CommandMap Commands() const; ChatCommand* CommandById(BString id); + void AddCommand(ChatCommand* cmd); + + BObjectList + UserPopUpItems() const; + void AddUserPopUpItem(BMessage* archived); BString GetOwnId(); void SetOwnId(BString user_id); @@ -68,7 +74,9 @@ private: ChatMap fChatMap; RosterMap fRosterMap; UserMap fUserMap; + CommandMap fCommands; + BObjectList fUserItems; ConversationAccountItem* fListItem; diff --git a/application/Server.cpp b/application/Server.cpp index ad6c5e9..7b5db21 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -42,9 +42,7 @@ Server::Server() : - BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE), - fUserItems(DefaultUserPopUpItems()), - fCommands(DefaultCommands()) + BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE) { } @@ -522,6 +520,27 @@ 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_USER_ITEM: + { + ProtocolLooper* looper = _LooperFromMessage(msg); + if (looper == NULL) + fUserItems.AddItem(new BMessage(*msg)); + else + looper->AddUserPopUpItem(new BMessage(*msg)); + break; + } case IM_PROTOCOL_READY: { // Ready notification diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index ad289ce..d99c884 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -1,12 +1,13 @@ /* - * Copyright 2021, Jaidyn Levesque. All rights reserved. * 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 @@ -23,6 +24,7 @@ #include "ConversationItem.h" #include "ConversationListView.h" #include "ConversationView.h" +#include "DefaultItems.h" #include "EditingFilter.h" #include "JoinWindow.h" #include "MainWindow.h" @@ -51,6 +53,10 @@ MainWindow::MainWindow() fServer = new Server(); AddFilter(fServer); + // Register default commands & items + DefaultCommands(this); + DefaultUserPopUpItems(this); + // Also through the editing filter (enter to send) AddCommonFilter(new EditingFilter(fSendView)); fSendView->MakeFocus(true); diff --git a/protocols/xmpp/JabberHandler.cpp b/protocols/xmpp/JabberHandler.cpp index ac56a9b..d6671be 100644 --- a/protocols/xmpp/JabberHandler.cpp +++ b/protocols/xmpp/JabberHandler.cpp @@ -341,13 +341,6 @@ JabberHandler::UpdateSettings(BMessage* msg) } -CommandMap -JabberHandler::Commands() -{ - return CommandMap(); -} - - uint32 JabberHandler::GetEncoding() { diff --git a/protocols/xmpp/JabberHandler.h b/protocols/xmpp/JabberHandler.h index 0ced3bb..feebb5f 100644 --- a/protocols/xmpp/JabberHandler.h +++ b/protocols/xmpp/JabberHandler.h @@ -34,7 +34,6 @@ #include #include -#include #include class BList; @@ -71,8 +70,6 @@ public: virtual status_t UpdateSettings(BMessage* msg); - virtual CommandMap Commands(); - virtual uint32 GetEncoding(); virtual CayaProtocolMessengerInterface*