75de08a18b
Protocol messages was added to the API to allow joining abstract rooms by their chat_id― IM_JOIN_ROOM and IM_ROOM_JOINED. To make room in anticipation of future room-related calls, some messages' values have been shifted. A JoinWindow was created (found through [Chat→Join Room] or [Alt-J] in the main window), to allow joining a room with this protocol message. The user can select which account the room should be joined from through a drop-down menu in the lower left-hand corner― a design I think could be replicated in other parts of Caya well. Path() and SetPath() in CayaProtocol were renamed to AddOnPath() and SetAddOnPath() respectively. GetName() and SetName() were also added, where "name" is the account name (aka the leaf of the protocols settings path). To Server, a new KeyMap was added for convenience (AccountInstances), to associate these account names with their instance IDs.
117 lines
2.4 KiB
C++
117 lines
2.4 KiB
C++
/*
|
||
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
||
* All rights reserved. Distributed under the terms of the MIT license.
|
||
*/
|
||
|
||
#include "JoinWindow.h"
|
||
|
||
#include <Alert.h>
|
||
#include <Button.h>
|
||
#include <LayoutBuilder.h>
|
||
#include <Messenger.h>
|
||
#include <StringView.h>
|
||
|
||
#include "CayaProtocolMessages.h"
|
||
|
||
|
||
const uint32 kJoinRoom = 'JWjr';
|
||
const uint32 kAccSelected = 'JWas';
|
||
|
||
|
||
JoinWindow::JoinWindow(BMessenger* messenger, AccountInstances accounts)
|
||
:
|
||
BWindow(BRect(0, 0, 300, 100), "Join a room", B_FLOATING_WINDOW, 0),
|
||
fTarget(messenger),
|
||
fAccounts(accounts),
|
||
fSelectedAcc(0)
|
||
{
|
||
_InitInterface();
|
||
|
||
CenterOnScreen();
|
||
}
|
||
|
||
|
||
void
|
||
JoinWindow::MessageReceived(BMessage* msg)
|
||
{
|
||
switch (msg->what)
|
||
{
|
||
case kAccSelected:
|
||
{
|
||
int32 index;
|
||
if (msg->FindInt32("index", &index) == B_OK)
|
||
fSelectedAcc = index;
|
||
break;
|
||
}
|
||
|
||
case kJoinRoom:
|
||
{
|
||
BString roomId = fTextBox->Text();
|
||
BString selected = fMenuField->Menu()->ItemAt(fSelectedAcc)->Label();
|
||
int64 instanceId = fAccounts.ValueFor(selected);
|
||
|
||
if (roomId.IsEmpty() == true) {
|
||
BAlert* alert = new BAlert("No room ID", "You can't join a room "
|
||
"with no name― you need to specify a room ID.", "OK", "", "",
|
||
B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_IDEA_ALERT);
|
||
alert->Go();
|
||
return;
|
||
}
|
||
|
||
BMessage* joinMsg = new BMessage(IM_MESSAGE);
|
||
joinMsg->AddInt32("im_what", IM_JOIN_ROOM);
|
||
joinMsg->AddInt64("instance", instanceId);
|
||
joinMsg->AddString("chat_id", roomId);
|
||
fTarget->SendMessage(joinMsg);
|
||
|
||
Quit();
|
||
break;
|
||
}
|
||
|
||
default:
|
||
BWindow::MessageReceived(msg);
|
||
}
|
||
}
|
||
|
||
|
||
void
|
||
JoinWindow::_InitInterface()
|
||
{
|
||
fMenuField = new BMenuField("accountMenuField", NULL, _CreateAccountMenu());
|
||
BButton* join = new BButton("Join", new BMessage(kJoinRoom));
|
||
fTextBox = new BTextControl("Room ID:", "", NULL);
|
||
|
||
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
||
.SetInsets(B_USE_DEFAULT_SPACING)
|
||
.Add(fTextBox)
|
||
.AddGroup(B_HORIZONTAL)
|
||
.Add(fMenuField)
|
||
.AddGlue()
|
||
.Add(new BButton("Cancel", new BMessage(B_QUIT_REQUESTED)))
|
||
.Add(join)
|
||
.End()
|
||
.End();
|
||
|
||
fTextBox->MakeFocus(true);
|
||
join->MakeDefault(true);
|
||
}
|
||
|
||
|
||
BMenu*
|
||
JoinWindow::_CreateAccountMenu()
|
||
{
|
||
BMenu* menu = new BMenu("accountMenu");
|
||
|
||
for (int i = 0; i < fAccounts.CountItems(); i++)
|
||
menu->AddItem(new BMenuItem(fAccounts.KeyAt(i).String(),
|
||
new BMessage(kAccSelected)));
|
||
|
||
menu->SetRadioMode(true);
|
||
menu->SetLabelFromMarked(true);
|
||
menu->ItemAt(fSelectedAcc)->SetMarked(true);
|
||
|
||
return menu;
|
||
}
|
||
|
||
|