Chat-O-Matic/application/windows/RosterWindow.cpp

129 lines
3.2 KiB
C++
Raw Normal View History

/*
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Copyright 2021, Jaidyn Levesque. 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
* Jaidyn Levesque, jadedctrl@teknik.io
*/
#include "RosterWindow.h"
2021-06-18 18:42:10 -05:00
#include <Button.h>
#include <LayoutBuilder.h>
2021-06-18 18:42:10 -05:00
#include <MenuField.h>
#include <Notification.h>
#include <ScrollView.h>
2021-06-20 12:44:20 -05:00
#include "AppMessages.h"
#include "AppPreferences.h"
#include "ChatProtocolMessages.h"
#include "Utils.h"
#include "RosterItem.h"
#include "RosterListView.h"
#include "RosterView.h"
const uint32 kSendMessage = 'RWSM';
2021-06-18 18:42:10 -05:00
const uint32 kSelAccount = 'RWSA';
const uint32 kSelNoAccount = 'RWNA';
2021-06-08 21:43:30 -05:00
RosterWindow::RosterWindow(const char* title, BMessage* selectMsg,
2021-06-18 18:42:10 -05:00
BMessenger* messenger, Server* server, bigtime_t instance)
:
2021-06-01 21:42:22 -05:00
BWindow(BRect(0, 0, 300, 400), title, B_FLOATING_WINDOW, 0),
fTarget(messenger),
2021-06-08 21:43:30 -05:00
fMessage(selectMsg),
2021-06-18 18:42:10 -05:00
fAccounts(server->GetAccounts()),
fServer(server)
{
2021-06-18 18:42:10 -05:00
fRosterView = new RosterView("buddyView", server, instance),
fRosterView->SetInvocationMessage(new BMessage(kSendMessage));
2021-06-18 18:42:10 -05:00
fOkButton = new BButton("OK", new BMessage(kSendMessage));
BMenu* accountMenu;
// If a specific instance is given, disallow selecting other accounts
if (instance > -1) {
accountMenu = new BMenu("accountMenu");
BString name = "N/A";
for (int i = 0; i < fAccounts.CountItems(); i++)
if (fAccounts.ValueAt(i) == instance) {
name = fAccounts.KeyAt(i);
break;
}
accountMenu->AddItem(new BMenuItem(name.String(), NULL));
accountMenu->SetLabelFromMarked(true);
accountMenu->ItemAt(0)->SetMarked(true);
accountMenu->SetEnabled(false);
}
else
accountMenu = CreateAccountMenu(fAccounts, BMessage(kSelAccount),
new BMessage(kSelNoAccount));
fAccountField = new BMenuField("accountMenuField", NULL, accountMenu);
2021-06-18 18:42:10 -05:00
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
2021-06-18 18:42:10 -05:00
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(fRosterView)
.AddGroup(B_HORIZONTAL)
.Add(fAccountField)
.AddGlue()
.Add(new BButton("Cancel", new BMessage(B_QUIT_REQUESTED)))
.Add(fOkButton)
.End()
.End();
CenterOnScreen();
}
void
RosterWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kSendMessage:
{
int index = message->FindInt32("index");
RosterItem* ritem = fRosterView->ListView()->RosterItemAt(index);
if (ritem == NULL)
return;
User* user = ritem->GetContact();
fMessage->AddString("user_id", user->GetId());
fMessage->AddInt64("instance", user->GetProtocolLooper()->GetInstance());
fTarget->SendMessage(fMessage);
PostMessage(B_QUIT_REQUESTED);
break;
}
2021-06-18 18:42:10 -05:00
case kSelAccount:
{
int index = message->FindInt32("index") - 1;
if (index < 0 || index > (fAccounts.CountItems() - 1))
return;
fRosterView->SetAccount(fAccounts.ValueAt(index));
break;
}
case kSelNoAccount:
fRosterView->SetAccount(-1);
break;
2021-05-30 17:12:41 -05:00
case IM_MESSAGE:
fRosterView->MessageReceived(message);
2021-05-30 17:12:41 -05:00
break;
default:
BWindow::MessageReceived(message);
}
}
void
RosterWindow::UpdateListItem(RosterItem* item)
{
fRosterView->UpdateListItem(item);
}