2021-05-26 07:48:25 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
2021-06-18 16:41:09 -05:00
|
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
2021-05-26 07:48:25 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
2021-06-18 16:41:09 -05:00
|
|
|
* Jaidyn Levesque, jadedctrl@teknik.io
|
2021-05-26 07:48:25 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "RosterWindow.h"
|
|
|
|
|
2021-06-18 18:42:10 -05:00
|
|
|
#include <Button.h>
|
2021-05-26 07:48:25 -05:00
|
|
|
#include <LayoutBuilder.h>
|
2021-06-18 18:42:10 -05:00
|
|
|
#include <MenuField.h>
|
2021-05-26 07:48:25 -05:00
|
|
|
#include <Notification.h>
|
|
|
|
#include <ScrollView.h>
|
|
|
|
|
2021-07-21 12:12:02 -05:00
|
|
|
#include "AccountsMenu.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "AppMessages.h"
|
|
|
|
#include "AppPreferences.h"
|
|
|
|
#include "ChatProtocolMessages.h"
|
2021-05-26 07:48:25 -05:00
|
|
|
#include "RosterItem.h"
|
|
|
|
#include "RosterListView.h"
|
2021-06-18 16:41:09 -05:00
|
|
|
#include "RosterView.h"
|
2021-05-26 07:48:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
const uint32 kSendMessage = 'RWSM';
|
2021-06-18 18:42:10 -05:00
|
|
|
const uint32 kSelAccount = 'RWSA';
|
|
|
|
const uint32 kSelNoAccount = 'RWNA';
|
2021-05-26 07:48:25 -05:00
|
|
|
|
|
|
|
|
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-05-26 07:48:25 -05:00
|
|
|
:
|
2021-07-24 15:44:23 -05:00
|
|
|
BWindow(BRect(0, 0, 300, 400), title, B_FLOATING_WINDOW,
|
|
|
|
B_AUTO_UPDATE_SIZE_LIMITS),
|
2021-05-26 07:48:25 -05:00
|
|
|
fTarget(messenger),
|
2021-06-08 21:43:30 -05:00
|
|
|
fMessage(selectMsg),
|
2021-05-26 07:48:25 -05:00
|
|
|
fServer(server)
|
|
|
|
{
|
2021-06-18 18:42:10 -05:00
|
|
|
fRosterView = new RosterView("buddyView", server, instance),
|
2021-06-18 16:41:09 -05:00
|
|
|
fRosterView->SetInvocationMessage(new BMessage(kSendMessage));
|
2021-05-26 07:48:25 -05:00
|
|
|
|
2021-06-18 18:42:10 -05:00
|
|
|
fOkButton = new BButton("OK", new BMessage(kSendMessage));
|
2021-06-19 00:11:02 -05:00
|
|
|
|
2021-07-21 12:12:02 -05:00
|
|
|
AccountInstances accounts = fServer->GetActiveAccounts();
|
2021-06-19 00:11:02 -05:00
|
|
|
|
|
|
|
// If a specific instance is given, disallow selecting other accounts
|
2021-07-21 12:12:02 -05:00
|
|
|
// In fact, don't even bother populating with them
|
2021-06-19 00:11:02 -05:00
|
|
|
if (instance > -1) {
|
2021-07-21 12:12:02 -05:00
|
|
|
BMenu* accountMenu = new BMenu("accountMenu");
|
|
|
|
|
2021-06-19 00:11:02 -05:00
|
|
|
BString name = "N/A";
|
2021-07-21 12:12:02 -05:00
|
|
|
for (int i = 0; i < accounts.CountItems(); i++)
|
|
|
|
if (accounts.ValueAt(i) == instance) {
|
|
|
|
name = accounts.KeyAt(i);
|
2021-06-19 00:11:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
accountMenu->AddItem(new BMenuItem(name.String(), NULL));
|
|
|
|
accountMenu->SetLabelFromMarked(true);
|
|
|
|
accountMenu->ItemAt(0)->SetMarked(true);
|
|
|
|
accountMenu->SetEnabled(false);
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
|
|
fAccountField = new BMenuField("accountMenuField", NULL, accountMenu);
|
2021-06-19 00:11:02 -05:00
|
|
|
}
|
|
|
|
else
|
2021-07-21 12:12:02 -05:00
|
|
|
fAccountField = new BMenuField("accountMenuField", NULL,
|
|
|
|
new AccountsMenu("accountMenu", BMessage(kSelAccount),
|
|
|
|
new BMessage(kSelNoAccount)));
|
2021-06-19 00:11:02 -05:00
|
|
|
|
2021-06-18 18:42:10 -05:00
|
|
|
|
2021-05-26 07:48:25 -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)
|
2021-05-26 07:48:25 -05:00
|
|
|
.End()
|
|
|
|
.End();
|
|
|
|
CenterOnScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
RosterWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch (message->what) {
|
|
|
|
case kSendMessage:
|
|
|
|
{
|
|
|
|
int index = message->FindInt32("index");
|
2021-06-18 16:41:09 -05:00
|
|
|
RosterItem* ritem = fRosterView->ListView()->RosterItemAt(index);
|
2021-07-28 16:53:59 -05:00
|
|
|
const char* search = fRosterView->SearchBox()->Text();
|
|
|
|
BString user_id;
|
|
|
|
int64 instance;
|
|
|
|
|
|
|
|
if (ritem != NULL) {
|
|
|
|
User* user = ritem->GetContact();
|
|
|
|
user_id = user->GetId();
|
|
|
|
instance = user->GetProtocolLooper()->GetInstance();
|
|
|
|
}
|
|
|
|
else if (search != NULL) {
|
|
|
|
user_id = search;
|
|
|
|
instance = fRosterView->GetAccount();
|
|
|
|
}
|
|
|
|
else
|
2021-05-26 07:48:25 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
User* user = ritem->GetContact();
|
2021-07-28 16:53:59 -05:00
|
|
|
fMessage->AddString("user_id", user_id);
|
|
|
|
fMessage->AddInt64("instance", instance);
|
2021-05-26 07:48:25 -05:00
|
|
|
fTarget->SendMessage(fMessage);
|
|
|
|
PostMessage(B_QUIT_REQUESTED);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-18 18:42:10 -05:00
|
|
|
case kSelAccount:
|
|
|
|
{
|
2021-07-21 12:12:02 -05:00
|
|
|
AccountInstances accounts = fServer->GetActiveAccounts();
|
|
|
|
|
2021-06-18 18:42:10 -05:00
|
|
|
int index = message->FindInt32("index") - 1;
|
2021-07-21 12:12:02 -05:00
|
|
|
if (index < 0 || index > (accounts.CountItems() - 1))
|
2021-06-18 18:42:10 -05:00
|
|
|
return;
|
2021-07-21 12:12:02 -05:00
|
|
|
fRosterView->SetAccount(accounts.ValueAt(index));
|
2021-06-18 18:42:10 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSelNoAccount:
|
|
|
|
fRosterView->SetAccount(-1);
|
|
|
|
break;
|
2021-05-30 17:12:41 -05:00
|
|
|
case IM_MESSAGE:
|
2021-06-18 16:41:09 -05:00
|
|
|
fRosterView->MessageReceived(message);
|
2021-05-30 17:12:41 -05:00
|
|
|
break;
|
2021-05-26 07:48:25 -05:00
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
RosterWindow::UpdateListItem(RosterItem* item)
|
|
|
|
{
|
2021-06-18 16:41:09 -05:00
|
|
|
fRosterView->UpdateListItem(item);
|
2021-05-26 07:48:25 -05:00
|
|
|
}
|