Store default commands/menu items in resources

This commit is contained in:
Jaidyn Ann 2021-06-30 14:27:58 -05:00
parent f6a006dddc
commit fb070e841a
9 changed files with 78 additions and 258 deletions

View File

@ -15,11 +15,11 @@
#include "ChatCommand.h" #include "ChatCommand.h"
#include "ConversationItem.h" #include "ConversationItem.h"
#include "ConversationView.h" #include "ConversationView.h"
#include "Flags.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "NotifyMessage.h" #include "NotifyMessage.h"
#include "ProtocolLooper.h" #include "ProtocolLooper.h"
#include "ProtocolManager.h" #include "ProtocolManager.h"
#include "RoomFlags.h"
#include "Server.h" #include "Server.h"
#include "TheApp.h" #include "TheApp.h"
#include "Utils.h" #include "Utils.h"

View File

@ -1,178 +0,0 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "DefaultItems.h"
#include <Looper.h>
#include <InterfaceDefs.h>
#include "AppMessages.h"
#include "ChatProtocolMessages.h"
#include "ChatCommand.h"
#include "Role.h"
BObjectList<BMessage>
DefaultCommands()
{
List<int32> roomUser;
roomUser.AddItem(CMD_ROOM_PARTICIPANT);
List<int32> kickBody;
kickBody.AddItem(CMD_ROOM_PARTICIPANT);
kickBody.AddItem(CMD_BODY_STRING);
List<int32> knownUser;
knownUser.AddItem(CMD_KNOWN_USER);
List<int32> anyUser;
anyUser.AddItem(CMD_ANY_USER);
CommandMap commands;
BMessage kickMsg(IM_MESSAGE);
kickMsg.AddInt32("im_what", IM_ROOM_KICK_PARTICIPANT);
ChatCommand* kick = new ChatCommand("kick", kickMsg, true, kickBody);
kick->SetDesc("Force a user to temporarily leave the room, assuming your "
"power level's high enough.");
commands.AddItem("kick", kick);
BMessage banMsg(IM_MESSAGE);
banMsg.AddInt32("im_what", IM_ROOM_BAN_PARTICIPANT);
ChatCommand* ban = new ChatCommand("ban", banMsg, true, kickBody);
ban->SetDesc("Kick a user out of the room and slam the door behind them― "
"locking it while you're at it.");
commands.AddItem("ban", ban);
BMessage unbanMsg(IM_MESSAGE);
unbanMsg.AddInt32("im_what", IM_ROOM_UNBAN_PARTICIPANT);
ChatCommand* unban = new ChatCommand("unban", unbanMsg, true, anyUser);
unban->SetDesc("Undo a previous ban, allowing the user to rejoin (if they "
"still want to).");
commands.AddItem("unban", unban);
BMessage muteMsg(IM_MESSAGE);
muteMsg.AddInt32("im_what", IM_ROOM_MUTE_PARTICIPANT);
ChatCommand* mute = new ChatCommand("mute", muteMsg, true, roomUser);
mute->SetDesc("Disallow a user from sending visible messages.");
commands.AddItem("mute", mute);
BMessage unmuteMsg(IM_MESSAGE);
unmuteMsg.AddInt32("im_what", IM_ROOM_UNMUTE_PARTICIPANT);
ChatCommand* unmute = new ChatCommand("unmute", unmuteMsg, true, roomUser);
unmute->SetDesc("Restore a user's ability to send messages.");
commands.AddItem("unmute", unmute);
BMessage deafenMsg(IM_MESSAGE);
deafenMsg.AddInt32("im_what", IM_ROOM_DEAFEN_PARTICIPANT);
ChatCommand* deafen = new ChatCommand("deafen", deafenMsg, true, roomUser);
deafen->SetDesc("Disallow a user from reading messages sent in the room.");
commands.AddItem("deafen", deafen);
BMessage undeafenMsg(IM_MESSAGE);
undeafenMsg.AddInt32("im_what", IM_ROOM_UNDEAFEN_PARTICIPANT);
ChatCommand* undeafen = new ChatCommand("undeafen", undeafenMsg, true, roomUser);
undeafen->SetDesc("Restore a user's ability to receive messages.");
commands.AddItem("undeafen", undeafen);
BMessage inviteMsg(IM_MESSAGE);
inviteMsg.AddInt32("im_what", IM_ROOM_SEND_INVITE);
ChatCommand* invite = new ChatCommand("invite", inviteMsg, true, knownUser);
invite->SetDesc("Invite a user to the current room.");
commands.AddItem("invite", invite);
BMessage helpMsg(APP_REQUEST_HELP);
ChatCommand* help = new ChatCommand("help", helpMsg, false, List<int>());
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++) {
ChatCommand* cmd = commands.ValueAt(i);
BMessage* item = new BMessage();
cmd->Archive(item);
cmds.AddItem(item);
}
return cmds;
}
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->AddString("class", "BMenuItem");
item->AddString("_label", "Leave chat");
item->AddMessage("_msg", leave);
item->AddBool("x_to_protocol", true);
items.AddItem(item);
return items;
}
BObjectList<BMessage>
DefaultUserPopUpItems()
{
BObjectList<BMessage> items;
BMessage* infoMsg = new BMessage(APP_USER_INFO);
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);
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);
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);
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);
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);
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);
items.AddItem(_UserMenuItem("Undeafen user", undeafenMsg, PERM_DEAFEN,
0, PERM_READ, false, true));
return items;
}
BMessage*
_UserMenuItem(const char* label, BMessage* msg, int32 user_perms,
int32 target_perms, int32 target_lacks, bool ignorePriority,
bool toProtocol)
{
BMessage* item = new BMessage(IM_MESSAGE);
item->AddString("class", "BMenuItem");
item->AddString("_label", label);
item->AddMessage("_msg", msg);
item->AddInt32("x_perms", user_perms);
item->AddInt32("x_target_perms", target_perms);
item->AddInt32("x_target_antiperms", target_lacks);
item->AddBool("x_priority", ignorePriority);
item->AddBool("x_to_protocol", toProtocol);
return item;
}

View File

@ -1,22 +0,0 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef DEFAULTITEMS_H
#define DEFAULTITEMS_H
#include <ObjectList.h>
class BMessage;
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,
bool toProtocol);
#endif // DEFAULTITEMS_H

40
application/Flags.h Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef FLAGS_H
#define FLAGS_H
// AUTOJOIN, AUTOCREATE, LOG, POPULATE
// Auto-join on login, auto-create on login (non-persistent rooms), keep local
// logs, populate chat with local logs on join…
// JCLP
// 0000
#define ROOM_AUTOJOIN 1
#define ROOM_AUTOCREATE 2
#define ROOM_LOG_LOCALLY 4
#define ROOM_POPULATE_LOGS 8
// NAME, SUBJECT, ROLECHANGE, BAN, KICK, DEAFEN, MUTE, NICK, READ, WRITE
// Set name of room, set subject, change user's "role" (permission presets
// defined by the protocol), etc…
// NSRBKDMNRW
// 0000000000
#define PERM_WRITE 1
#define PERM_READ 2
#define PERM_NICK 4
#define PERM_MUTE 8
#define PERM_DEAFEN 16
#define PERM_KICK 32
#define PERM_BAN 64
#define PERM_ROLECHANGE 128
#define PERM_ROOM_SUBJECT 256
#define PERM_ROOM_NAME 512
#define PERM_ALL 1023
#endif // FLAGS_H

View File

@ -38,7 +38,6 @@ SRCS = \
application/ChatCommand.cpp \ application/ChatCommand.cpp \
application/Contact.cpp \ application/Contact.cpp \
application/Conversation.cpp \ application/Conversation.cpp \
application/DefaultItems.cpp \
application/EditingFilter.cpp \ application/EditingFilter.cpp \
application/ImageCache.cpp \ application/ImageCache.cpp \
application/Notifier.cpp \ application/Notifier.cpp \
@ -87,6 +86,7 @@ SRCS = \
# Specify the resource definition files to use. Full or relative paths can be # Specify the resource definition files to use. Full or relative paths can be
# used. # used.
RDEFS = Cardie.rdef \ RDEFS = Cardie.rdef \
data/misc/Templates.rdef \
data/icons/status/Away.rdef \ data/icons/status/Away.rdef \
data/icons/status/Busy.rdef \ data/icons/status/Busy.rdef \
data/icons/status/Offline.rdef \ data/icons/status/Offline.rdef \

View File

@ -2,29 +2,12 @@
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io> * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license. * All rights reserved. Distributed under the terms of the MIT license.
*/ */
#ifndef PERMS_H #ifndef _ROLE_H
#define PERMS_H #define _ROLE_H
#include <SupportDefs.h> #include <SupportDefs.h>
// NAME, SUBJECT, ROLECHANGE, BAN, KICK, DEAFEN, MUTE, NICK, READ, WRITE #include "Flags.h"
// Set name of room, set subject, change user's "role" (permission presets
// defined by the protocol), etc…
// NSRBKDMNRW
// 0000000000
#define PERM_WRITE 1
#define PERM_READ 2
#define PERM_NICK 4
#define PERM_MUTE 8
#define PERM_DEAFEN 16
#define PERM_KICK 32
#define PERM_BAN 64
#define PERM_ROLECHANGE 128
#define PERM_ROOM_SUBJECT 256
#define PERM_ROOM_NAME 512
#define PERM_ALL 1023
class Role { class Role {
@ -49,6 +32,4 @@ public:
// simulated in add-ons. // simulated in add-ons.
}; };
#endif // _ROLE_H
#endif // PERMS_H

View File

@ -1,24 +0,0 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef ROOMFLAGS_H
#define ROOMFLAGS_H
#include <SupportDefs.h>
// AUTOJOIN, AUTOCREATE, LOG, POPULATE
// Auto-join on login, auto-create on login (non-persistent rooms), keep local
// logs, populate chat with local logs on join…
// JCLP
// 0000
#define ROOM_AUTOJOIN 1
#define ROOM_AUTOCREATE 2
#define ROOM_LOG_LOCALLY 4
#define ROOM_POPULATE_LOGS 8
#endif // ROOMFLAGS_H

View File

@ -30,12 +30,11 @@
#include "ChatProtocol.h" #include "ChatProtocol.h"
#include "AppPreferences.h" #include "AppPreferences.h"
#include "ChatProtocolMessages.h" #include "ChatProtocolMessages.h"
#include "DefaultItems.h" #include "Flags.h"
#include "ImageCache.h" #include "ImageCache.h"
#include "InviteDialogue.h" #include "InviteDialogue.h"
#include "ProtocolLooper.h" #include "ProtocolLooper.h"
#include "ProtocolManager.h" #include "ProtocolManager.h"
#include "RoomFlags.h"
#include "RosterItem.h" #include "RosterItem.h"
#include "UserInfoWindow.h" #include "UserInfoWindow.h"
#include "Utils.h" #include "Utils.h"
@ -43,13 +42,37 @@
Server::Server() Server::Server()
: :
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE), BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE)
fChatItems(DefaultChatPopUpItems()),
fUserItems(DefaultUserPopUpItems())
{ {
BObjectList<BMessage> commands = DefaultCommands(); if (fChatItems.IsEmpty() == false || fUserItems.IsEmpty() == false
for (int i = 0; i < commands.CountItems(); i++) { || fCommands.CountItems() > 0)
ChatCommand* cmd = new ChatCommand(commands.ItemAt(i)); return;
BResources* res = ChatResources();
// Loading user pop-up items
for (int i = 0; i < 6; i++) {
size_t size;
BMessage temp;
const void* buff = res->LoadResource(B_MESSAGE_TYPE, 1100 + i, &size);
temp.Unflatten((const char*)buff);
fUserItems.AddItem(new BMessage(temp));
}
// Loading room pop-up items
BMessage leave;
size_t leaveSize;
const void* leaveBuff = res->LoadResource(B_MESSAGE_TYPE, 1120, &leaveSize);
leave.Unflatten((const char*)leaveBuff);
fChatItems.AddItem(new BMessage(leave));
// Loading default chat commands
for (int i = 0; i < 9; i++) {
size_t size;
BMessage temp;
const void* buff = res->LoadResource(B_MESSAGE_TYPE, 1140 + i, &size);
temp.Unflatten((const char*)buff);
ChatCommand* cmd = new ChatCommand(&temp);
fCommands.AddItem(cmd->GetName(), cmd); fCommands.AddItem(cmd->GetName(), cmd);
} }
} }

View File

@ -18,8 +18,8 @@
#include <libsupport/SHA1.h> #include <libsupport/SHA1.h>
#include <ChatProtocolMessages.h> #include <ChatProtocolMessages.h>
#include <Flags.h>
#include <Role.h> #include <Role.h>
#include <RoomFlags.h>
#include <gloox/chatstatefilter.h> #include <gloox/chatstatefilter.h>
#include <gloox/messageeventfilter.h> #include <gloox/messageeventfilter.h>