Move roster into its own generic window

The roster list is now split from MainWindow into RosterWindow, which
can be used arbitrarily for selecting something from contacts. The
im_what, title, and target can be specified.

Right now it's in use under the Chat menu, "New chat…". Chats can only
have two members (DM/PM), so "New chat…" directly leads to the roster
list.

In the future, roster list might be extended to allow specification of
non-contact users based on their identifiers, or filtering based on
protocol/account― this could help it with "invites" to future rooms,
etc.

IM_CREATE_CHAT and IM_CHAT_CREATED were also added to the protocol API,
allowing a new chat to be explicitly created from Caya or the protocol.
This commit is contained in:
Jaidyn Ann 2021-05-26 07:48:25 -05:00
parent 5d9d16459f
commit 0326cee687
8 changed files with 388 additions and 199 deletions

View File

@ -71,6 +71,12 @@ enum im_what_code {
//! Logs received //! Logs received
IM_LOGS_RECEIVED = 28, IM_LOGS_RECEIVED = 28,
//! Create an individual chat
IM_CREATE_CHAT = 29,
//! Chat has been created
IM_CHAT_CREATED = 30,
/* /*
* Messages related to contact changes. * Messages related to contact changes.
*/ */

View File

@ -30,15 +30,13 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "PreferencesDialog.h" #include "PreferencesDialog.h"
#include "ReplicantStatusView.h" #include "ReplicantStatusView.h"
#include "RosterItem.h" #include "RosterWindow.h"
#include "RosterListView.h"
#include "SearchBarTextControl.h"
#include "Server.h" #include "Server.h"
#include "StatusView.h" #include "StatusView.h"
const uint32 kLogin = 'LOGI'; const uint32 kLogin = 'LOGI';
const uint32 kSearchContact = 'SRCH'; const uint32 CAYA_NEW_CHAT = 'CRCH';
MainWindow::MainWindow() MainWindow::MainWindow()
@ -48,14 +46,6 @@ MainWindow::MainWindow()
{ {
fStatusView = new StatusView("statusView"); fStatusView = new StatusView("statusView");
SearchBarTextControl* searchBox =
new SearchBarTextControl(new BMessage(kSearchContact));
fListView = new RosterListView("buddyView");
fListView->SetInvocationMessage(new BMessage(CAYA_OPEN_CHAT_WINDOW));
BScrollView* scrollView = new BScrollView("scrollview", fListView,
B_WILL_DRAW, false, true);
// Menubar // Menubar
BMenuBar* menuBar = new BMenuBar("MenuBar"); BMenuBar* menuBar = new BMenuBar("MenuBar");
@ -69,14 +59,18 @@ MainWindow::MainWindow()
new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY)); new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY));
programMenu->SetTargetForItems(this); programMenu->SetTargetForItems(this);
BMenu* chatMenu = new BMenu("Chat");
chatMenu->AddItem(new BMenuItem("New chat" B_UTF8_ELLIPSIS,
new BMessage(CAYA_NEW_CHAT)));
chatMenu->SetTargetForItems(this);
menuBar->AddItem(programMenu); menuBar->AddItem(programMenu);
menuBar->AddItem(chatMenu);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f) BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.Add(menuBar) .Add(menuBar)
.AddGroup(B_VERTICAL) .AddGroup(B_VERTICAL)
.SetInsets(5, 5, 5, 10) .SetInsets(5, 5, 5, 10)
.Add(searchBox)
.Add(scrollView)
.Add(fStatusView) .Add(fStatusView)
.End() .End()
.End(); .End();
@ -106,13 +100,14 @@ MainWindow::QuitRequested()
int32 button_index = 0; int32 button_index = 0;
if(!CayaPreferences::Item()->DisableQuitConfirm) if(!CayaPreferences::Item()->DisableQuitConfirm)
{ {
BAlert* alert = new BAlert("Closing", "Are you sure you wan to quit?", "Yes", "No", NULL, B_WIDTH_AS_USUAL, B_OFFSET_SPACING, B_WARNING_ALERT); BAlert* alert = new BAlert("Closing", "Are you sure you want to quit?",
"Yes", "No", NULL, B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
B_WARNING_ALERT);
alert->SetShortcut(0, B_ESCAPE); alert->SetShortcut(0, B_ESCAPE);
button_index = alert->Go(); button_index = alert->Go();
} }
if(button_index == 0) { if(button_index == 0) {
fListView->MakeEmpty();
fServer->Quit(); fServer->Quit();
CayaPreferences::Get()->Save(); CayaPreferences::Get()->Save();
ReplicantStatusView::RemoveReplicant(); ReplicantStatusView::RemoveReplicant();
@ -128,66 +123,18 @@ void
MainWindow::MessageReceived(BMessage* message) MainWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kSearchContact:
{
void* control = NULL;
if (message->FindPointer("source", &control) != B_OK)
return;
SearchBarTextControl* searchBox
= static_cast<SearchBarTextControl*>(control);
if (searchBox == NULL)
return;
RosterMap map = fServer->Contacts();
for (uint32 i = 0; i < map.CountItems(); i++) {
Contact* linker = map.ValueAt(i);
RosterItem* item = linker->GetRosterItem();
// If the search filter has been deleted show all the items,
// otherwise remove the item in order to show only items
// that matches the search criteria
if (strcmp(searchBox->Text(), "") == 0)
AddItem(item);
else if (linker->GetName().IFindFirst(searchBox->Text()) == B_ERROR)
RemoveItem(item);
else
AddItem(item);
UpdateListItem(item);
}
break;
}
case CAYA_SHOW_SETTINGS: case CAYA_SHOW_SETTINGS:
{ {
PreferencesDialog* dialog = new PreferencesDialog(); PreferencesDialog* dialog = new PreferencesDialog();
dialog->Show(); dialog->Show();
break; break;
} }
case CAYA_OPEN_CHAT_WINDOW:
case CAYA_NEW_CHAT:
{ {
// This is only used by RosterList, so try to open a one-on-one chat RosterWindow* roster = new RosterWindow("Invite contact to chat"
// if there is one― otherwise, creawte one. B_UTF8_ELLIPSIS, IM_CREATE_CHAT, new BMessenger(this), fServer);
int index = message->FindInt32("index"); roster->Show();
RosterItem* ritem = ItemAt(index);
User* user = ritem->GetContact();
if (ritem != NULL)
User* user = ritem->GetContact();
ChatMap chats = user->Conversations();
Conversation* chat;
// TODO: Poor way of creating necessary chatroom
if (chats.CountItems() == 0) {
chat = new Conversation(user->GetId(), fServer->Looper());
chat->SetProtocolLooper(user->GetProtocolLooper());
chat->AddUser(user);
chat->ShowWindow(false, true);
fServer->AddConversation(chat);
}
else
while (chat = chats.RemoveItemAt(0))
if (chat->Users().CountItems() == 1)
chat->ShowWindow(false, true);
break; break;
} }
@ -273,80 +220,6 @@ MainWindow::ImMessage(BMessage* msg)
} }
break; break;
} }
case IM_STATUS_SET:
{
int32 status;
if (msg->FindInt32("status", &status) != B_OK)
return;
RosterItem* rosterItem = fServer->ContactById(msg->FindString("user_id"))->GetRosterItem();
if (rosterItem) {
UpdateListItem(rosterItem);
// Add or remove item
switch (status) {
/*case CAYA_OFFLINE:
// By default offline contacts are hidden
if (!CayaPreferences::Item()->HideOffline)
break;
if (HasItem(rosterItem))
RemoveItem(rosterItem);
return;*/
default:
// Add item because it has a non-offline status
if (!HasItem(rosterItem))
AddItem(rosterItem);
break;
}
UpdateListItem(rosterItem);
// Sort list view again
fListView->Sort();
// Check if the user want the notification
if (!CayaPreferences::Item()->NotifyContactStatus)
break;
switch (status) {
case CAYA_ONLINE:
case CAYA_OFFLINE:
// Notify when contact is online or offline
if (status == CAYA_ONLINE) {
BString message;
message << rosterItem->GetContact()->GetName();
if (status == CAYA_ONLINE)
message << " is available!";
else
message << " is offline!";
BNotification notification(B_INFORMATION_NOTIFICATION);
notification.SetGroup(BString("Caya"));
notification.SetTitle(BString("Presence"));
notification.SetIcon(rosterItem->Bitmap());
notification.SetContent(message);
notification.Send();
}
break;
default:
break;
}
}
break;
}
case IM_AVATAR_SET:
case IM_CONTACT_INFO:
case IM_EXTENDED_CONTACT_INFO:
{
RosterItem* rosterItem
= fServer->ContactById(msg->FindString("user_id"))->GetRosterItem();
if (rosterItem)
UpdateListItem(rosterItem);
break;
}
} }
} }
@ -362,61 +235,6 @@ MainWindow::ObserveInteger(int32 what, int32 val)
} }
void
MainWindow::UpdateListItem(RosterItem* item)
{
if (fListView->HasItem(item))
fListView->InvalidateItem(fListView->IndexOf(item));
}
int32
MainWindow::CountItems() const
{
return fListView->CountItems();
}
RosterItem*
MainWindow::ItemAt(int index)
{
return dynamic_cast<RosterItem*>(fListView->ItemAt(index));
}
void
MainWindow::AddItem(RosterItem* item)
{
// Don't add offline items and avoid duplicates
if ((item->Status() == CAYA_OFFLINE)
&& CayaPreferences::Item()->HideOffline)
return;
if (HasItem(item))
return;
// Add item and sort
fListView->AddItem(item);
fListView->Sort();
}
bool
MainWindow::HasItem(RosterItem* item)
{
return fListView->HasItem(item);
}
void
MainWindow::RemoveItem(RosterItem* item)
{
// Remove item and sort
fListView->RemoveItem(item);
fListView->Sort();
}
void void
MainWindow::WorkspaceActivated(int32 workspace, bool active) MainWindow::WorkspaceActivated(int32 workspace, bool active)
{ {
@ -425,3 +243,5 @@ MainWindow::WorkspaceActivated(int32 workspace, bool active)
else else
fWorkspaceChanged = true; fWorkspaceChanged = true;
} }

View File

@ -48,6 +48,7 @@ SRCS = \
application/ProtocolLooper.cpp \ application/ProtocolLooper.cpp \
application/ProtocolManager.cpp \ application/ProtocolManager.cpp \
application/ProtocolSettings.cpp \ application/ProtocolSettings.cpp \
application/RosterWindow.cpp \
application/Server.cpp \ application/Server.cpp \
application/TheApp.cpp \ application/TheApp.cpp \
application/User.cpp \ application/User.cpp \

View File

@ -0,0 +1,266 @@
/*
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrea Anzani, andrea.anzani@gmail.com
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
*/
#include "RosterWindow.h"
#include <LayoutBuilder.h>
#include <Notification.h>
#include <ScrollView.h>
#include "CayaMessages.h"
#include "CayaPreferences.h"
#include "CayaProtocolMessages.h"
#include "RosterItem.h"
#include "RosterListView.h"
#include "SearchBarTextControl.h"
#include "Server.h"
const uint32 kSearchContact = 'RWSC';
const uint32 kSendMessage = 'RWSM';
RosterWindow::RosterWindow(const char* title, int32 selectMsg,
BMessenger* messenger, Server* server)
:
BWindow(BRect(0, 0, 300, 400), title, B_TITLED_WINDOW, 0),
fTarget(messenger),
fServer(server)
{
fMessage = new BMessage(IM_MESSAGE);
fMessage->AddInt32("im_what", selectMsg);
SearchBarTextControl* searchBox =
new SearchBarTextControl(new BMessage(kSearchContact));
fListView = new RosterListView("buddyView");
fListView->SetInvocationMessage(new BMessage(kSendMessage));
BScrollView* scrollView = new BScrollView("scrollview", fListView,
B_WILL_DRAW, false, true);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.AddGroup(B_VERTICAL)
.SetInsets(5, 5, 5, 10)
.Add(searchBox)
.Add(scrollView)
.End()
.End();
_PopulateRosterList();
CenterOnScreen();
}
void
RosterWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kSearchContact:
{
void* control = NULL;
if (message->FindPointer("source", &control) != B_OK)
return;
SearchBarTextControl* searchBox
= static_cast<SearchBarTextControl*>(control);
if (searchBox == NULL)
return;
RosterMap map = fServer->Contacts();
for (uint32 i = 0; i < map.CountItems(); i++) {
Contact* linker = map.ValueAt(i);
RosterItem* item = linker->GetRosterItem();
// If the search filter has been deleted show all the items,
// otherwise remove the item in order to show only items
// that matches the search criteria
if (strcmp(searchBox->Text(), "") == 0)
AddItem(item);
else if (linker->GetName().IFindFirst(searchBox->Text()) == B_ERROR)
RemoveItem(item);
else
AddItem(item);
UpdateListItem(item);
}
break;
}
case kSendMessage:
{
int index = message->FindInt32("index");
RosterItem* ritem = ItemAt(index);
if (ritem == NULL)
return;
User* user = ritem->GetContact();
fMessage->AddString("user_id", user->GetId());
fTarget->SendMessage(fMessage);
PostMessage(B_QUIT_REQUESTED);
break;
}
default:
BWindow::MessageReceived(message);
}
}
void
RosterWindow::ImMessage(BMessage* msg)
{
int32 im_what = msg->FindInt32("im_what");
switch (im_what) {
case IM_STATUS_SET:
{
int32 status;
if (msg->FindInt32("status", &status) != B_OK)
return;
RosterItem* rosterItem = fServer->ContactById(msg->FindString("user_id"))->GetRosterItem();
if (rosterItem) {
UpdateListItem(rosterItem);
// Add or remove item
switch (status) {
/*case CAYA_OFFLINE:
// By default offline contacts are hidden
if (!CayaPreferences::Item()->HideOffline)
break;
if (HasItem(rosterItem))
RemoveItem(rosterItem);
return;*/
default:
// Add item because it has a non-offline status
if (!HasItem(rosterItem))
AddItem(rosterItem);
break;
}
UpdateListItem(rosterItem);
// Sort list view again
fListView->Sort();
// Check if the user want the notification
if (!CayaPreferences::Item()->NotifyContactStatus)
break;
switch (status) {
case CAYA_ONLINE:
case CAYA_OFFLINE:
// Notify when contact is online or offline
if (status == CAYA_ONLINE) {
BString message;
message << rosterItem->GetContact()->GetName();
if (status == CAYA_ONLINE)
message << " is available!";
else
message << " is offline!";
BNotification notification(B_INFORMATION_NOTIFICATION);
notification.SetGroup(BString("Caya"));
notification.SetTitle(BString("Presence"));
notification.SetIcon(rosterItem->Bitmap());
notification.SetContent(message);
notification.Send();
}
break;
default:
break;
}
}
break;
}
case IM_AVATAR_SET:
case IM_CONTACT_INFO:
case IM_EXTENDED_CONTACT_INFO:
{
RosterItem* rosterItem
= fServer->ContactById(msg->FindString("user_id"))->GetRosterItem();
if (rosterItem)
UpdateListItem(rosterItem);
break;
}
}
}
int32
RosterWindow::CountItems() const
{
return fListView->CountItems();
}
RosterItem*
RosterWindow::ItemAt(int index)
{
return dynamic_cast<RosterItem*>(fListView->ItemAt(index));
}
void
RosterWindow::AddItem(RosterItem* item)
{
// Don't add offline items and avoid duplicates
if ((item->Status() == CAYA_OFFLINE)
&& CayaPreferences::Item()->HideOffline)
return;
if (HasItem(item))
return;
// Add item and sort
fListView->AddItem(item);
fListView->Sort();
}
bool
RosterWindow::HasItem(RosterItem* item)
{
return fListView->HasItem(item);
}
void
RosterWindow::RemoveItem(RosterItem* item)
{
// Remove item and sort
fListView->RemoveItem(item);
fListView->Sort();
}
void
RosterWindow::UpdateListItem(RosterItem* item)
{
if (fListView->HasItem(item))
fListView->InvalidateItem(fListView->IndexOf(item));
}
void
RosterWindow::_PopulateRosterList()
{
RosterMap contacts = fServer->Contacts();
for (int i = 0; i < contacts.CountItems(); i++)
AddItem(contacts.ValueAt(i)->GetRosterItem());
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrea Anzani, andrea.anzani@gmail.com
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
*/
#ifndef ROSTERWINDOW_H
#define ROSTERWINDOW_H
#include <Window.h>
class RosterItem;
class RosterListView;
class Server;
/* A window with the a list of the user's contacts, will send a message to
the server with contact info, once a contact is selected. */
class RosterWindow : public BWindow {
public:
RosterWindow(const char* title, int32 selectMsg, BMessenger* messenger,
Server* server);
void MessageReceived(BMessage* message);
void ImMessage(BMessage* msg);
int32 CountItems() const;
RosterItem* ItemAt(int index);
void AddItem(RosterItem*);
bool HasItem(RosterItem*);
void RemoveItem(RosterItem*);
void UpdateListItem(RosterItem* item);
private:
void _PopulateRosterList();
Server* fServer;
RosterListView* fListView;
BMessenger* fTarget;
BMessage* fMessage;
};
#endif // ROSTERWINDOW_H

View File

@ -322,6 +322,26 @@ Server::ImMessage(BMessage* msg)
contact->SetNotifyAvatarBitmap(NULL); contact->SetNotifyAvatarBitmap(NULL);
break; break;
} }
case IM_CREATE_CHAT:
{
BString user_id = msg->FindString("user_id");
if (user_id.IsEmpty() == false) {
User* user = ContactById(user_id);
user->GetProtocolLooper()->PostMessage(msg);
}
break;
}
case IM_CHAT_CREATED:
{
Conversation* chat = _EnsureConversation(msg);
User* user = _EnsureContact(msg);
if (chat != NULL && user != NULL) {
chat->AddUser(user);
chat->ShowWindow(false, true);
}
break;
}
case IM_SEND_MESSAGE: case IM_SEND_MESSAGE:
{ {
// Route this message through the appropriate ProtocolLooper // Route this message through the appropriate ProtocolLooper

View File

@ -102,6 +102,7 @@ JabberHandler::Process(BMessage* msg)
} }
break; break;
} }
case IM_SEND_MESSAGE: { case IM_SEND_MESSAGE: {
const char* id = msg->FindString("chat_id"); const char* id = msg->FindString("chat_id");
const char* subject = msg->FindString("subject"); const char* subject = msg->FindString("subject");
@ -119,6 +120,19 @@ JabberHandler::Process(BMessage* msg)
_MessageSent(id, subject, body); _MessageSent(id, subject, body);
break; break;
} }
case IM_CREATE_CHAT: {
const char* invite_id = msg->FindString("user_id");
// TODO: Contact validation, make sure permssion is granted
if (!invite_id)
return B_ERROR;
_ChatCreated(invite_id);
break;
}
default: default:
return B_ERROR; return B_ERROR;
} }
@ -535,6 +549,17 @@ JabberHandler::_MessageSent(const char* id, const char* subject,
} }
void
JabberHandler::_ChatCreated(const char* id)
{
BMessage msg(IM_MESSAGE);
msg.AddInt32("im_what", IM_CHAT_CREATED);
msg.AddString("chat_id", id);
msg.AddString("user_id", id);
_SendMessage(&msg);
}
status_t status_t
JabberHandler::_SetupAvatarCache() JabberHandler::_SetupAvatarCache()
{ {

View File

@ -99,10 +99,12 @@ private:
BList* fAvatars; BList* fAvatars;
void _SendMessage(BMessage* msg); void _SendMessage(BMessage* msg);
void _MessageSent(const char* id, const char* subject, void _MessageSent(const char* id, const char* subject,
const char* body); const char* body);
void _ChatCreated(const char* id);
void _Notify(notification_type type, const char* title, const char* message); void _Notify(notification_type type, const char* title, const char* message);
void _NotifyProgress(const char* title, const char* message, float progress); void _NotifyProgress(const char* title, const char* message, float progress);