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.
This commit is contained in:
Jaidyn Ann 2021-06-15 23:29:38 -05:00
parent a03746c034
commit 8a9cb9effd
10 changed files with 118 additions and 46 deletions

View File

@ -9,8 +9,6 @@
#include <Messenger.h> #include <Messenger.h>
#include "ChatCommand.h"
class BBitmap; class BBitmap;
@ -65,9 +63,6 @@ public:
//! Messenger interface used //! Messenger interface used
virtual CayaProtocolMessengerInterface* MessengerInterface() const = 0; virtual CayaProtocolMessengerInterface* MessengerInterface() const = 0;
//! Return a map of any custom commands
virtual CommandMap Commands() = 0;
}; };
#endif // _CAYA_PROTOCOL_H #endif // _CAYA_PROTOCOL_H

View File

@ -365,7 +365,30 @@ enum im_what_code {
IM_SPECIAL_FROM_PROTOCOL = 1001, IM_SPECIAL_FROM_PROTOCOL = 1001,
//! Protocol is ready to receive messages //! 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
}; };

View File

@ -5,15 +5,17 @@
#include "DefaultItems.h" #include "DefaultItems.h"
#include <Looper.h>
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include "CayaMessages.h" #include "CayaMessages.h"
#include "CayaProtocolMessages.h" #include "CayaProtocolMessages.h"
#include "ChatCommand.h"
#include "Role.h" #include "Role.h"
CommandMap void
DefaultCommands() DefaultCommands(BLooper* target)
{ {
List<int32> roomUser; List<int32> roomUser;
roomUser.AddItem(CMD_ROOM_PARTICIPANT); roomUser.AddItem(CMD_ROOM_PARTICIPANT);
@ -83,48 +85,54 @@ DefaultCommands()
help->SetDesc("List all current commands, or get help for certain command."); help->SetDesc("List all current commands, or get help for certain command.");
commands.AddItem("help", help); 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<BMessage> void
DefaultUserPopUpItems() DefaultUserPopUpItems(BLooper* target)
{ {
BObjectList<BMessage> items; BObjectList<BMessage> items;
BMessage* infoMsg = new BMessage(CAYA_USER_INFO); BMessage* infoMsg = new BMessage(CAYA_USER_INFO);
items.AddItem(_UserMenuItem("User info" B_UTF8_ELLIPSIS, infoMsg, 0, 0, 0, target->PostMessage(_UserMenuItem("User info" B_UTF8_ELLIPSIS, infoMsg, 0,
false, false)); 0, 0, false, false));
BMessage* kickMsg = new BMessage(IM_MESSAGE); BMessage* kickMsg = new BMessage(IM_MESSAGE);
kickMsg->AddInt32("im_what", IM_ROOM_KICK_PARTICIPANT); kickMsg->AddInt32("im_what", IM_ROOM_KICK_PARTICIPANT);
items.AddItem(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0, false, target->PostMessage(_UserMenuItem("Kick user", kickMsg, PERM_KICK, 0, 0,
true)); false, true));
BMessage* banMsg = new BMessage(IM_MESSAGE); BMessage* banMsg = new BMessage(IM_MESSAGE);
banMsg->AddInt32("im_what", IM_ROOM_BAN_PARTICIPANT); 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); BMessage* muteMsg = new BMessage(IM_MESSAGE);
muteMsg->AddInt32("im_what", IM_ROOM_MUTE_PARTICIPANT); muteMsg->AddInt32("im_what", IM_ROOM_MUTE_PARTICIPANT);
items.AddItem(_UserMenuItem("Mute user", muteMsg, PERM_MUTE, PERM_WRITE, 0, target->PostMessage(_UserMenuItem("Mute user", muteMsg, PERM_MUTE,
false, true)); PERM_WRITE, 0, false, true));
BMessage* unmuteMsg = new BMessage(IM_MESSAGE); BMessage* unmuteMsg = new BMessage(IM_MESSAGE);
unmuteMsg->AddInt32("im_what", IM_ROOM_UNMUTE_PARTICIPANT); unmuteMsg->AddInt32("im_what", IM_ROOM_UNMUTE_PARTICIPANT);
items.AddItem(_UserMenuItem("Unmute user", unmuteMsg, PERM_MUTE, 0, target->PostMessage(_UserMenuItem("Unmute user", unmuteMsg, PERM_MUTE, 0,
PERM_WRITE, false, true)); PERM_WRITE, false, true));
BMessage* deafenMsg = new BMessage(IM_MESSAGE); BMessage* deafenMsg = new BMessage(IM_MESSAGE);
deafenMsg->AddInt32("im_what", IM_ROOM_DEAFEN_PARTICIPANT); deafenMsg->AddInt32("im_what", IM_ROOM_DEAFEN_PARTICIPANT);
items.AddItem(_UserMenuItem("Deafen user", deafenMsg, PERM_DEAFEN, PERM_READ, target->PostMessage(_UserMenuItem("Deafen user", deafenMsg, PERM_DEAFEN,
0, false, true)); PERM_READ, 0, false, true));
BMessage* undeafenMsg = new BMessage(IM_MESSAGE); BMessage* undeafenMsg = new BMessage(IM_MESSAGE);
undeafenMsg->AddInt32("im_what", IM_ROOM_UNDEAFEN_PARTICIPANT); undeafenMsg->AddInt32("im_what", IM_ROOM_UNDEAFEN_PARTICIPANT);
items.AddItem(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN, 0, target->PostMessage(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN,
PERM_READ, false, true)); 0, PERM_READ, false, true));
return items;
} }
@ -133,7 +141,9 @@ _UserMenuItem(const char* label, BMessage* msg, int32 user_perms,
int32 target_perms, int32 target_lacks, bool ignorePriority, int32 target_perms, int32 target_lacks, bool ignorePriority,
bool toProtocol) 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("class", "BMenuItem");
item->AddString("_label", label); item->AddString("_label", label);
item->AddMessage("_msg", msg); item->AddMessage("_msg", msg);

View File

@ -5,13 +5,14 @@
#ifndef DEFAULTITEMS_H #ifndef DEFAULTITEMS_H
#define DEFAULTITEMS_H #define DEFAULTITEMS_H
#include "ChatCommand.h"
#include <ObjectList.h> #include <ObjectList.h>
class BLooper;
class BMessage;
CommandMap DefaultCommands();
BObjectList<BMessage> DefaultUserPopUpItems(); void DefaultCommands(BLooper* target);
void DefaultUserPopUpItems(BLooper* target);
BMessage* _UserMenuItem(const char* label, BMessage* msg, BMessage* _UserMenuItem(const char* label, BMessage* msg,
int32 user_perms, int32 target_perms, int32 user_perms, int32 target_perms,
int32 target_lacks, bool ignorePriority, int32 target_lacks, bool ignorePriority,

View File

@ -24,8 +24,7 @@ ProtocolLooper::ProtocolLooper(CayaProtocol* protocol, int64 instance)
BLooper(), BLooper(),
fProtocol(protocol), fProtocol(protocol),
fInstance(instance), fInstance(instance),
fListItem(NULL), fListItem(NULL)
fCommands(protocol->Commands())
{ {
Account* account = reinterpret_cast<Account*>( Account* account = reinterpret_cast<Account*>(
protocol->MessengerInterface()); protocol->MessengerInterface());
@ -158,6 +157,27 @@ 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);
}
BString BString
ProtocolLooper::GetOwnId() ProtocolLooper::GetOwnId()
{ {

View File

@ -8,6 +8,7 @@
#define _PROTOCOL_LOOPER_H #define _PROTOCOL_LOOPER_H
#include <Looper.h> #include <Looper.h>
#include <ObjectList.h>
#include <String.h> #include <String.h>
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
@ -50,6 +51,11 @@ public:
CommandMap Commands() const; CommandMap Commands() const;
ChatCommand* CommandById(BString id); ChatCommand* CommandById(BString id);
void AddCommand(ChatCommand* cmd);
BObjectList<BMessage>
UserPopUpItems() const;
void AddUserPopUpItem(BMessage* archived);
BString GetOwnId(); BString GetOwnId();
void SetOwnId(BString user_id); void SetOwnId(BString user_id);
@ -68,7 +74,9 @@ private:
ChatMap fChatMap; ChatMap fChatMap;
RosterMap fRosterMap; RosterMap fRosterMap;
UserMap fUserMap; UserMap fUserMap;
CommandMap fCommands; CommandMap fCommands;
BObjectList<BMessage> fUserItems;
ConversationAccountItem* ConversationAccountItem*
fListItem; fListItem;

View File

@ -42,9 +42,7 @@
Server::Server() Server::Server()
: :
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE), BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE)
fUserItems(DefaultUserPopUpItems()),
fCommands(DefaultCommands())
{ {
} }
@ -522,6 +520,27 @@ Server::ImMessage(BMessage* msg)
break; 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: case IM_PROTOCOL_READY:
{ {
// Ready notification // Ready notification

View File

@ -1,12 +1,13 @@
/* /*
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Copyright 2009-2011, Andrea Anzani. All rights reserved. * Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. 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. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Andrea Anzani, andrea.anzani@gmail.com * Andrea Anzani, andrea.anzani@gmail.com
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
* Jaidyn Levesque, jadedctrl@teknik.io
*/ */
#include <Application.h> #include <Application.h>
@ -23,6 +24,7 @@
#include "ConversationItem.h" #include "ConversationItem.h"
#include "ConversationListView.h" #include "ConversationListView.h"
#include "ConversationView.h" #include "ConversationView.h"
#include "DefaultItems.h"
#include "EditingFilter.h" #include "EditingFilter.h"
#include "JoinWindow.h" #include "JoinWindow.h"
#include "MainWindow.h" #include "MainWindow.h"
@ -51,6 +53,10 @@ MainWindow::MainWindow()
fServer = new Server(); fServer = new Server();
AddFilter(fServer); AddFilter(fServer);
// Register default commands & items
DefaultCommands(this);
DefaultUserPopUpItems(this);
// Also through the editing filter (enter to send) // Also through the editing filter (enter to send)
AddCommonFilter(new EditingFilter(fSendView)); AddCommonFilter(new EditingFilter(fSendView));
fSendView->MakeFocus(true); fSendView->MakeFocus(true);

View File

@ -341,13 +341,6 @@ JabberHandler::UpdateSettings(BMessage* msg)
} }
CommandMap
JabberHandler::Commands()
{
return CommandMap();
}
uint32 uint32
JabberHandler::GetEncoding() JabberHandler::GetEncoding()
{ {

View File

@ -34,7 +34,6 @@
#include <CayaProtocol.h> #include <CayaProtocol.h>
#include <CayaConstants.h> #include <CayaConstants.h>
#include <ChatCommand.h>
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
class BList; class BList;
@ -71,8 +70,6 @@ public:
virtual status_t UpdateSettings(BMessage* msg); virtual status_t UpdateSettings(BMessage* msg);
virtual CommandMap Commands();
virtual uint32 GetEncoding(); virtual uint32 GetEncoding();
virtual CayaProtocolMessengerInterface* virtual CayaProtocolMessengerInterface*