Custom chatlist pop-up items

This commit is contained in:
Jaidyn Ann 2021-06-16 04:33:06 -05:00
parent 8a9cb9effd
commit d5aebac5c5
10 changed files with 90 additions and 25 deletions

View File

@ -388,7 +388,15 @@ enum im_what_code {
// 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
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
};

View File

@ -95,6 +95,23 @@ DefaultCommands(BLooper* target)
}
void
DefaultChatPopUpItems(BLooper* target)
{
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);
}
void
DefaultUserPopUpItems(BLooper* target)
{
@ -142,7 +159,7 @@ _UserMenuItem(const char* label, BMessage* msg, int32 user_perms,
bool toProtocol)
{
BMessage* item = new BMessage(IM_MESSAGE);
item->AddInt32("im_what", IM_REGISTER_USER_ITEM);
item->AddInt32("im_what", IM_REGISTER_USERLIST_ITEM);
item->AddString("class", "BMenuItem");
item->AddString("_label", label);

View File

@ -12,6 +12,7 @@ class BMessage;
void DefaultCommands(BLooper* target);
void DefaultChatPopUpItems(BLooper* target);
void DefaultUserPopUpItems(BLooper* target);
BMessage* _UserMenuItem(const char* label, BMessage* msg,
int32 user_perms, int32 target_perms,

View File

@ -178,6 +178,20 @@ ProtocolLooper::AddUserPopUpItem(BMessage* archived)
}
BObjectList<BMessage>
ProtocolLooper::ChatPopUpItems() const
{
return fChatItems;
}
void
ProtocolLooper::AddChatPopUpItem(BMessage* archived)
{
fChatItems.AddItem(archived);
}
BString
ProtocolLooper::GetOwnId()
{

View File

@ -57,6 +57,10 @@ public:
UserPopUpItems() const;
void AddUserPopUpItem(BMessage* archived);
BObjectList<BMessage>
ChatPopUpItems() const;
void AddChatPopUpItem(BMessage* archived);
BString GetOwnId();
void SetOwnId(BString user_id);
@ -77,6 +81,7 @@ private:
CommandMap fCommands;
BObjectList<BMessage> fUserItems;
BObjectList<BMessage> fChatItems;
ConversationAccountItem*
fListItem;

View File

@ -532,7 +532,7 @@ Server::ImMessage(BMessage* msg)
looper->AddCommand(cmd);
break;
}
case IM_REGISTER_USER_ITEM:
case IM_REGISTER_USERLIST_ITEM:
{
ProtocolLooper* looper = _LooperFromMessage(msg);
if (looper == NULL)
@ -541,6 +541,15 @@ Server::ImMessage(BMessage* msg)
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_PROTOCOL_READY:
{
// Ready notification
@ -798,7 +807,7 @@ Server::CommandById(BString id, int64 instance)
BObjectList<BMessage>
Server::ConversationPopUpItems()
Server::ChatPopUpItems()
{
return fChatItems;
}

View File

@ -61,7 +61,7 @@ public:
ChatCommand* CommandById(BString id, int64 instance);
BObjectList<BMessage> ConversationPopUpItems();
BObjectList<BMessage> ChatPopUpItems();
BObjectList<BMessage> UserPopUpItems();
private:

View File

@ -46,21 +46,6 @@ ConversationListView::MessageReceived(BMessage* msg)
break;
}
case kLeaveSelectedChat:
{
ConversationItem* item;
int32 selIndex = CurrentSelection();
if ((item = (ConversationItem*)ItemAt(selIndex)) == NULL)
break;
BMessage leave(IM_MESSAGE);
leave.AddInt32("im_what", IM_LEAVE_ROOM);
leave.AddString("chat_id", item->GetConversation()->GetId());
item->GetConversation()->GetProtocolLooper()->MessageReceived(&leave);
break;
}
default:
BListView::MessageReceived(msg);
}
@ -172,13 +157,36 @@ BPopUpMenu*
ConversationListView::_ConversationPopUp()
{
BPopUpMenu* menu = new BPopUpMenu("chatPopUp");
menu->AddItem(new BMenuItem("Open chat" B_UTF8_ELLIPSIS,
new BMessage(kOpenSelectedChat)));
menu->AddItem(new BMenuItem("Leave chat", new BMessage(kLeaveSelectedChat)));
menu->SetTargetForItems(this);
int32 selIndex = CurrentSelection();
ConversationItem* item;
if ((item = (ConversationItem*)ItemAt(selIndex)) == NULL)
return _BlankPopUp();
Conversation* chat = item->GetConversation();
ProtocolLooper* looper = chat->GetProtocolLooper();
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
BObjectList<BMessage> items = server->ChatPopUpItems();
BObjectList<BMessage> protoItems = looper->ChatPopUpItems();
items.AddList(&protoItems);
for (int i = 0; i < items.CountItems(); i++) {
BMessage* itemMsg = items.ItemAt(i);
BMessage* msg = new BMessage(*itemMsg);
BMessage toSend;
msg->FindMessage("_msg", &toSend);
toSend.AddString("chat_id", chat->GetId());
toSend.AddInt64("instance", looper->GetInstance());
msg->ReplaceMessage("_msg", &toSend);
BMenuItem* item = new BMenuItem(msg);
if (msg->GetBool("x_to_protocol", true) == true)
item->SetTarget(looper);
else
item->SetTarget(Window());
menu->AddItem(item);
}
return menu;
}

View File

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

View File

@ -56,6 +56,7 @@ MainWindow::MainWindow()
// Register default commands & items
DefaultCommands(this);
DefaultUserPopUpItems(this);
DefaultChatPopUpItems(this);
// Also through the editing filter (enter to send)
AddCommonFilter(new EditingFilter(fSendView));