2021-05-27 11:15:30 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ConversationListView.h"
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2021-05-27 11:15:30 -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-05-27 11:15:30 -05:00
|
|
|
#include "Conversation.h"
|
2021-06-11 17:24:04 -05:00
|
|
|
#include "ConversationAccountItem.h"
|
2021-05-27 11:15:30 -05:00
|
|
|
#include "ConversationItem.h"
|
2021-06-12 16:13:52 -05:00
|
|
|
#include "MainWindow.h"
|
2021-06-04 16:32:18 -05:00
|
|
|
#include "ProtocolLooper.h"
|
2021-06-12 16:13:52 -05:00
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
2021-05-27 11:15:30 -05:00
|
|
|
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "ConversationListView"
|
|
|
|
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
const uint32 kOpenSelectedChat = 'CVos';
|
|
|
|
const uint32 kLeaveSelectedChat = 'CVcs';
|
|
|
|
|
|
|
|
|
|
|
|
ConversationListView::ConversationListView(const char* name)
|
2021-06-11 17:24:04 -05:00
|
|
|
: BOutlineListView(name)
|
2021-05-27 11:15:30 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationListView::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
|
|
|
case kOpenSelectedChat:
|
|
|
|
{
|
|
|
|
ConversationItem* item;
|
|
|
|
int32 selIndex = CurrentSelection();
|
|
|
|
|
2021-06-11 20:33:28 -05:00
|
|
|
if (selIndex >= 0
|
|
|
|
&& (item = (ConversationItem*)ItemAt(selIndex)) != NULL
|
|
|
|
&& item->OutlineLevel() == 1)
|
2021-05-28 22:26:32 -05:00
|
|
|
item->GetConversation()->ShowView(false, true);
|
2021-05-27 11:15:30 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-05-30 12:30:26 -05:00
|
|
|
BListView::MessageReceived(msg);
|
2021-05-27 11:15:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationListView::MouseDown(BPoint where)
|
|
|
|
{
|
2021-06-03 23:39:20 -05:00
|
|
|
int32 selection = CurrentSelection();
|
|
|
|
|
2021-06-11 17:24:04 -05:00
|
|
|
BOutlineListView::MouseDown(where);
|
2021-05-27 11:15:30 -05:00
|
|
|
|
2021-06-11 17:24:04 -05:00
|
|
|
int32 newSel = CurrentSelection();
|
|
|
|
|
|
|
|
// Don't allow selecting an AccountItem
|
2021-06-11 20:33:28 -05:00
|
|
|
if (newSel >= 0 && ItemAt(newSel)->OutlineLevel() == 0) {
|
2021-06-11 17:24:04 -05:00
|
|
|
Select(selection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow deselecting a room
|
|
|
|
if (newSel < 0 && selection >= 0)
|
2021-06-03 23:39:20 -05:00
|
|
|
Select(selection);
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
uint32 buttons = 0;
|
|
|
|
Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons);
|
|
|
|
|
|
|
|
if (!(buttons & B_SECONDARY_MOUSE_BUTTON))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CurrentSelection() >= 0)
|
|
|
|
_ConversationPopUp()->Go(ConvertToScreen(where), true, false);
|
|
|
|
else
|
|
|
|
_BlankPopUp()->Go(ConvertToScreen(where), true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 20:45:24 -05:00
|
|
|
void
|
|
|
|
ConversationListView::SelectionChanged()
|
|
|
|
{
|
|
|
|
MessageReceived(new BMessage(kOpenSelectedChat));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-11 17:24:04 -05:00
|
|
|
void
|
|
|
|
ConversationListView::AddConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
ConversationAccountItem* superItem = _EnsureAccountItem(chat);
|
|
|
|
ConversationItem* item = chat->GetListItem();
|
|
|
|
if (superItem == NULL || item == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddUnder(item, superItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationListView::RemoveConversation(Conversation* chat)
|
|
|
|
{
|
|
|
|
RemoveItem(chat->GetListItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
ConversationListView::CountConversations()
|
|
|
|
{
|
|
|
|
int32 count = 0;
|
|
|
|
for (int32 i = 0; i < CountItems(); i++)
|
|
|
|
if (ItemAt(i)->OutlineLevel() == 1)
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
ConversationListView::ConversationIndexOf(Conversation* chat)
|
|
|
|
{
|
|
|
|
ConversationItem* item = chat->GetListItem();
|
|
|
|
int32 index = IndexOf(item);
|
|
|
|
int32 chatIndex = index;
|
|
|
|
|
|
|
|
if (item == NULL || index < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < index; i++)
|
|
|
|
if (ItemAt(i)->OutlineLevel() == 0) // If AccountItem
|
|
|
|
chatIndex--;
|
|
|
|
return chatIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationListView::SelectConversation(int32 index)
|
|
|
|
{
|
|
|
|
for (int32 i = 0, cindex = -1; i < CountItems(); i++) {
|
|
|
|
if (ItemAt(i)->OutlineLevel() == 1) // If ConversationItem
|
|
|
|
cindex++;
|
|
|
|
|
|
|
|
if (cindex == index) {
|
|
|
|
Select(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
BPopUpMenu*
|
|
|
|
ConversationListView::_ConversationPopUp()
|
|
|
|
{
|
|
|
|
BPopUpMenu* menu = new BPopUpMenu("chatPopUp");
|
2021-06-16 04:33:06 -05:00
|
|
|
int32 selIndex = CurrentSelection();
|
2021-05-27 11:15:30 -05:00
|
|
|
|
2021-06-16 04:33:06 -05:00
|
|
|
ConversationItem* item;
|
|
|
|
if ((item = (ConversationItem*)ItemAt(selIndex)) == NULL)
|
|
|
|
return _BlankPopUp();
|
|
|
|
Conversation* chat = item->GetConversation();
|
|
|
|
ProtocolLooper* looper = chat->GetProtocolLooper();
|
2021-05-27 11:15:30 -05:00
|
|
|
|
2021-06-16 04:33:06 -05:00
|
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
BObjectList<BMessage> items = server->ChatPopUpItems();
|
2021-06-20 01:24:34 -05:00
|
|
|
BObjectList<BMessage> protoItems = looper->Protocol()->ChatPopUpItems();
|
2021-06-16 04:33:06 -05:00
|
|
|
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;
|
2021-05-27 11:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BPopUpMenu*
|
|
|
|
ConversationListView::_BlankPopUp()
|
|
|
|
{
|
2021-06-12 16:13:52 -05:00
|
|
|
bool enabled = false;
|
|
|
|
|
|
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
|
|
if (server != NULL && server->GetAccounts().CountItems() > 0)
|
|
|
|
enabled = true;
|
|
|
|
|
2021-05-27 11:15:30 -05:00
|
|
|
BPopUpMenu* menu = new BPopUpMenu("blankPopUp");
|
2021-07-19 09:54:27 -05:00
|
|
|
BMenuItem* newChat = new BMenuItem(B_TRANSLATE("New chat" B_UTF8_ELLIPSIS),
|
2021-06-20 12:44:20 -05:00
|
|
|
new BMessage(APP_NEW_CHAT), 'M', B_COMMAND_KEY);
|
2021-06-12 16:13:52 -05:00
|
|
|
newChat->SetEnabled(enabled);
|
|
|
|
|
|
|
|
menu->AddItem(newChat);
|
2021-05-27 11:15:30 -05:00
|
|
|
menu->SetTargetForItems(Window());
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-11 17:24:04 -05:00
|
|
|
ConversationAccountItem*
|
|
|
|
ConversationListView::_EnsureAccountItem(Conversation* chat)
|
|
|
|
{
|
|
|
|
ProtocolLooper* looper;
|
|
|
|
if (chat == NULL || (looper = chat->GetProtocolLooper()) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ConversationAccountItem* item = looper->GetListItem();
|
|
|
|
if (HasItem(item) == false)
|
|
|
|
AddItem(item);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|