2021-05-31 10:50:43 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UserListView.h"
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2021-05-31 10:50:43 -05:00
|
|
|
#include <PopUpMenu.h>
|
|
|
|
#include <MenuItem.h>
|
|
|
|
#include <Window.h>
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "AppMessages.h"
|
|
|
|
#include "ChatProtocolMessages.h"
|
2021-06-06 01:49:11 -05:00
|
|
|
#include "Conversation.h"
|
2021-06-15 21:05:21 -05:00
|
|
|
#include "MainWindow.h"
|
2021-06-06 16:31:25 -05:00
|
|
|
#include "ProtocolLooper.h"
|
2021-06-06 01:49:11 -05:00
|
|
|
#include "Role.h"
|
2021-06-15 21:05:21 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
2021-05-31 10:50:43 -05:00
|
|
|
#include "User.h"
|
|
|
|
#include "UserInfoWindow.h"
|
|
|
|
#include "UserItem.h"
|
|
|
|
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "UserListView"
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
|
2021-07-25 14:42:38 -05:00
|
|
|
static int
|
|
|
|
compare_by_name(const void* _item1, const void* _item2)
|
|
|
|
{
|
|
|
|
UserItem* item1 = *(UserItem**)_item1;
|
|
|
|
UserItem* item2 = *(UserItem**)_item2;
|
|
|
|
|
|
|
|
return item1->GetUser()->GetName().ICompare(item2->GetUser()->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
UserListView::UserListView(const char* name)
|
2021-06-06 01:49:11 -05:00
|
|
|
: BListView(name),
|
|
|
|
fChat(NULL)
|
2021-05-31 10:50:43 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserListView::MouseDown(BPoint where)
|
|
|
|
{
|
|
|
|
BListView::MouseDown(where);
|
|
|
|
|
|
|
|
uint32 buttons = 0;
|
|
|
|
Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons);
|
|
|
|
|
|
|
|
if (!(buttons & B_SECONDARY_MOUSE_BUTTON))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CurrentSelection() >= 0)
|
|
|
|
_UserPopUp()->Go(ConvertToScreen(where), true, false);
|
|
|
|
else
|
|
|
|
_BlankPopUp()->Go(ConvertToScreen(where), true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-25 14:42:38 -05:00
|
|
|
void
|
|
|
|
UserListView::Sort()
|
|
|
|
{
|
|
|
|
SortItems(compare_by_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Support generally setting own nick, own UserItems
Seperate UserItems are now created for each list, too, rather than a
single one being created per-user. This functionally works a lot nicer.
But onto more important things… now setting the user's own nick should
work quite well. Finally. =w=
This has given me a good bit of trouble over the past couple of days―
setting the user's nick *worked*, but it wouldn't propagate to its
corresponding UserItem nor its UserInfoDialog. It would, however, work
with the StatusView.
These are all registered Observers of the User itself, so if one works,
they *all* should, them all being registered to the same User.
Now, if a given User isn't found in the ProtocolLooper's user-list,
the Conversation class will take it upon itself to create a new
one and add it to both of their respective lists.
So the user's own contact would be set in the ProtocolLooper― but it
*wouldn't* be added to the user-list.
Hilarity ensues as two seperate objects for the user's own contact would
be created.
Since the StatusView is registered to the ProtocolLooper's "real" own contact
slot, it would receive all updates… but since Conversations' user-lists and
items would be registered to the Conversation-created "fake" user, they
would be borked.
Simple oversight, but wow it hecked with me. :P
2021-08-05 14:10:21 -05:00
|
|
|
bool
|
|
|
|
UserListView::HasUser(User* user)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CountItems(); i++)
|
|
|
|
if (user == ((UserItem*)ItemAt(i))->GetUser())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserListView::AddUser(User* user)
|
|
|
|
{
|
|
|
|
AddItem(new UserItem(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserListView::RemoveUser(User* user)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CountItems(); i++)
|
|
|
|
if (user == ((UserItem*)ItemAt(i))->GetUser())
|
|
|
|
RemoveItem(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
BPopUpMenu*
|
|
|
|
UserListView::_UserPopUp()
|
|
|
|
{
|
|
|
|
BPopUpMenu* menu = new BPopUpMenu("userPopUp");
|
2021-06-06 01:49:11 -05:00
|
|
|
UserItem* item = (UserItem*)ItemAt(CurrentSelection());
|
|
|
|
User* selected_user;
|
|
|
|
if (item == NULL || (selected_user = item->GetUser()) == NULL)
|
2021-06-15 21:05:21 -05:00
|
|
|
return _BlankPopUp();
|
|
|
|
|
2021-07-06 14:46:28 -05:00
|
|
|
Role* own_role = fChat->GetRole(fChat->GetOwnContact()->GetId());
|
2021-06-06 01:49:11 -05:00
|
|
|
|
2021-06-06 16:31:25 -05:00
|
|
|
Role* selected_role = fChat->GetRole(selected_user->GetId());
|
2021-06-15 21:05:21 -05:00
|
|
|
|
|
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
BObjectList<BMessage> items = server->UserPopUpItems();
|
2021-06-20 01:24:34 -05:00
|
|
|
BObjectList<BMessage> protoItems = fChat->GetProtocolLooper()->Protocol()->UserPopUpItems();
|
2021-06-16 04:33:06 -05:00
|
|
|
items.AddList(&protoItems);
|
2021-06-15 21:05:21 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < items.CountItems(); i++) {
|
|
|
|
BMessage* itemMsg = items.ItemAt(i);
|
|
|
|
_ProcessItem(itemMsg, menu, own_role, selected_role, selected_user->GetId());
|
|
|
|
}
|
2021-06-06 01:49:11 -05:00
|
|
|
return menu;
|
2021-05-31 10:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BPopUpMenu*
|
|
|
|
UserListView::_BlankPopUp()
|
|
|
|
{
|
|
|
|
BPopUpMenu* menu = new BPopUpMenu("blankPopUp");
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
BMenuItem* invite = new BMenuItem(B_TRANSLATE("Invite user"
|
|
|
|
B_UTF8_ELLIPSIS), new BMessage(APP_SEND_INVITE), 'I', B_COMMAND_KEY);
|
2021-06-12 16:13:52 -05:00
|
|
|
if (fChat == NULL)
|
|
|
|
invite->SetEnabled(false);
|
2021-05-31 10:50:43 -05:00
|
|
|
|
|
|
|
menu->AddItem(invite);
|
|
|
|
menu->SetTargetForItems(Window());
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 16:31:25 -05:00
|
|
|
void
|
2021-06-15 21:05:21 -05:00
|
|
|
UserListView::_ProcessItem(BMessage* itemMsg, BPopUpMenu* menu, Role* user,
|
|
|
|
Role* target, BString target_id)
|
2021-06-06 16:31:25 -05:00
|
|
|
{
|
2021-06-15 21:05:21 -05:00
|
|
|
BMessage* msg = new BMessage(*itemMsg);
|
|
|
|
bool priority = msg->GetBool("x_priority", false);
|
|
|
|
int32 perms = msg->GetInt32("x_perms", 0);
|
|
|
|
int32 target_perms = msg->GetInt32("x_target_perms", 0);
|
|
|
|
int32 target_antiperms = msg->GetInt32("x_target_antiperms", 0);
|
|
|
|
|
|
|
|
BMessage toSend;
|
|
|
|
msg->FindMessage("_msg", &toSend);
|
|
|
|
toSend.AddString("user_id", target_id);
|
|
|
|
toSend.AddString("chat_id", fChat->GetId());
|
|
|
|
toSend.AddInt64("instance", fChat->GetProtocolLooper()->GetInstance());
|
|
|
|
msg->ReplaceMessage("_msg", &toSend);
|
|
|
|
|
2021-07-06 13:47:37 -05:00
|
|
|
if ((perms == 0 || ((user != NULL) && (user->fPerms & perms)))
|
|
|
|
&& ((target == NULL) ||
|
|
|
|
((target_perms == 0 || (target->fPerms & target_perms))
|
|
|
|
&& (target_antiperms == 0 || (!(target->fPerms & target_antiperms)))
|
|
|
|
&& ((priority == false) || (user->fPriority > target->fPriority)))))
|
2021-06-15 21:05:21 -05:00
|
|
|
{
|
|
|
|
BMenuItem* item = new BMenuItem(msg);
|
|
|
|
if (msg->GetBool("x_to_protocol", true) == true)
|
|
|
|
item->SetTarget(fChat->GetProtocolLooper());
|
|
|
|
else
|
|
|
|
item->SetTarget(Window());
|
|
|
|
|
|
|
|
menu->AddItem(item);
|
|
|
|
}
|
2021-06-06 16:31:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|