2021-06-19 18:25:58 -05:00
|
|
|
/*
|
|
|
|
* 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 "RosterEditWindow.h"
|
|
|
|
|
|
|
|
#include <Button.h>
|
2021-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2021-06-19 18:25:58 -05:00
|
|
|
#include <LayoutBuilder.h>
|
|
|
|
#include <MenuField.h>
|
|
|
|
#include <Notification.h>
|
|
|
|
#include <ScrollView.h>
|
|
|
|
#include <SeparatorView.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-06-19 18:25:58 -05:00
|
|
|
#include "RosterItem.h"
|
|
|
|
#include "RosterListView.h"
|
|
|
|
#include "RosterView.h"
|
|
|
|
#include "TemplateWindow.h"
|
|
|
|
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "RosterEditWindow"
|
|
|
|
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
const uint32 kSendMessage = 'RWSM';
|
|
|
|
const uint32 kAddMember = 'RWAM';
|
|
|
|
const uint32 kRemoveMember = 'RWRM';
|
|
|
|
const uint32 kEditMember = 'RWEM';
|
|
|
|
const uint32 kSelAccount = 'RWSA';
|
|
|
|
const uint32 kSelNoAccount = 'RWNA';
|
|
|
|
|
2021-06-19 22:03:02 -05:00
|
|
|
RosterEditWindow* RosterEditWindow::fInstance = NULL;
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
|
|
|
|
RosterEditWindow::RosterEditWindow(Server* server)
|
|
|
|
:
|
2021-07-24 15:44:23 -05:00
|
|
|
BWindow(BRect(0, 0, 300, 400), B_TRANSLATE("Roster"), B_FLOATING_WINDOW,
|
|
|
|
B_AUTO_UPDATE_SIZE_LIMITS),
|
2021-06-19 22:03:02 -05:00
|
|
|
fServer(server),
|
|
|
|
fEditingWindow(NULL)
|
2021-06-19 18:25:58 -05:00
|
|
|
{
|
|
|
|
fRosterView = new RosterView("buddyView", server);
|
|
|
|
fRosterView->SetInvocationMessage(new BMessage(kEditMember));
|
|
|
|
|
|
|
|
fAccountField = new BMenuField("accountMenuField", NULL,
|
2021-07-21 12:12:02 -05:00
|
|
|
new AccountsMenu("accountMenu", BMessage(kSelAccount),
|
2021-06-19 18:25:58 -05:00
|
|
|
new BMessage(kSelNoAccount)));
|
|
|
|
|
|
|
|
font_height fontHeight;
|
|
|
|
fRosterView->GetFontHeight(&fontHeight);
|
|
|
|
int16 buttonHeight = int16(fontHeight.ascent + fontHeight.descent + 12);
|
|
|
|
BSize charButtonSize(buttonHeight, buttonHeight);
|
2021-07-24 15:44:23 -05:00
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
BButton* fAddButton = new BButton("+", new BMessage(kAddMember));
|
|
|
|
BButton* fRemoveButton = new BButton("-", new BMessage(kRemoveMember));
|
|
|
|
fAddButton->SetExplicitSize(charButtonSize);
|
|
|
|
fRemoveButton->SetExplicitSize(charButtonSize);
|
|
|
|
|
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
|
|
|
|
.SetInsets(B_USE_DEFAULT_SPACING)
|
|
|
|
.Add(fRosterView)
|
|
|
|
.AddGroup(B_HORIZONTAL, 0, 0.0)
|
|
|
|
.Add(fAccountField)
|
|
|
|
.AddGlue()
|
|
|
|
|
|
|
|
.Add(new BSeparatorView(B_VERTICAL))
|
|
|
|
.AddGroup(B_VERTICAL, 0, 0.0)
|
|
|
|
.Add(new BSeparatorView(B_HORIZONTAL))
|
|
|
|
.AddGroup(B_HORIZONTAL, 1, 0.0)
|
|
|
|
.SetInsets(1)
|
|
|
|
.Add(fRemoveButton)
|
|
|
|
.Add(fAddButton)
|
|
|
|
.End()
|
|
|
|
.Add(new BSeparatorView(B_HORIZONTAL))
|
|
|
|
.End()
|
|
|
|
.Add(new BSeparatorView(B_VERTICAL))
|
|
|
|
.End()
|
|
|
|
.End();
|
|
|
|
CenterOnScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 22:03:02 -05:00
|
|
|
RosterEditWindow::~RosterEditWindow()
|
|
|
|
{
|
|
|
|
fInstance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RosterEditWindow*
|
|
|
|
RosterEditWindow::Get(Server* server)
|
|
|
|
{
|
|
|
|
if (fInstance == NULL) {
|
|
|
|
fInstance = new RosterEditWindow(server);
|
|
|
|
}
|
|
|
|
return fInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
RosterEditWindow::Check()
|
|
|
|
{
|
|
|
|
return (fInstance != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
void
|
|
|
|
RosterEditWindow::MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch (message->what) {
|
|
|
|
case kEditMember:
|
|
|
|
{
|
2021-06-19 22:03:02 -05:00
|
|
|
if (fEditingWindow != NULL && fEditingWindow->Lock()) {
|
|
|
|
fEditingWindow->Quit();
|
|
|
|
fEditingUser.SetTo("");
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:25:58 -05:00
|
|
|
int index = message->FindInt32("index");
|
|
|
|
RosterItem* ritem = fRosterView->ListView()->RosterItemAt(index);
|
|
|
|
if (ritem == NULL)
|
|
|
|
return;
|
|
|
|
|
2021-06-19 22:03:02 -05:00
|
|
|
User* user = ritem->GetContact();
|
|
|
|
fEditingUser.SetTo(user->GetId().String());
|
|
|
|
|
|
|
|
// The response IM_EXTENDED_CONTACT_INFO is used to populate the
|
|
|
|
// TemplateWindow.
|
|
|
|
BMessage* request = new BMessage(IM_MESSAGE);
|
|
|
|
request->AddInt32("im_what", IM_GET_EXTENDED_CONTACT_INFO);
|
|
|
|
request->AddString("user_id", user->GetId());
|
|
|
|
user->GetProtocolLooper()->PostMessage(request);
|
|
|
|
|
|
|
|
BMessage* edit = new BMessage(IM_MESSAGE);
|
|
|
|
edit->AddInt32("im_what", IM_CONTACT_LIST_EDIT_CONTACT);
|
|
|
|
|
|
|
|
fEditingWindow =
|
2021-07-19 09:54:27 -05:00
|
|
|
new TemplateWindow(B_TRANSLATE("Editing contact"), "roster",
|
|
|
|
edit, fServer, user->GetProtocolLooper()->GetInstance());
|
2021-06-19 22:03:02 -05:00
|
|
|
fEditingWindow->Show();
|
|
|
|
break;
|
|
|
|
}
|
2021-06-19 18:25:58 -05:00
|
|
|
case kAddMember:
|
|
|
|
{
|
|
|
|
BMessage* add = new BMessage(IM_MESSAGE);
|
|
|
|
add->AddInt32("im_what", IM_CONTACT_LIST_ADD_CONTACT);
|
|
|
|
TemplateWindow* win =
|
2021-07-19 09:54:27 -05:00
|
|
|
new TemplateWindow(B_TRANSLATE("Adding contact"), "roster",
|
|
|
|
add, fServer);
|
2021-06-19 18:25:58 -05:00
|
|
|
win->Show();
|
|
|
|
break;
|
|
|
|
}
|
2021-06-19 22:37:20 -05:00
|
|
|
case kRemoveMember:
|
|
|
|
{
|
|
|
|
int index = message->FindInt32("index");
|
|
|
|
RosterItem* ritem = fRosterView->ListView()->RosterItemAt(index);
|
|
|
|
if (ritem == NULL)
|
|
|
|
return;
|
|
|
|
User* user = ritem->GetContact();
|
|
|
|
|
|
|
|
BMessage* rem = new BMessage(IM_MESSAGE);
|
|
|
|
rem->AddInt32("im_what", IM_CONTACT_LIST_REMOVE_CONTACT);
|
|
|
|
rem->AddString("user_id", user->GetId());
|
|
|
|
|
|
|
|
user->GetProtocolLooper()->PostMessage(rem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IM_MESSAGE: {
|
|
|
|
if (message->GetInt32("im_what", 0) == IM_EXTENDED_CONTACT_INFO)
|
|
|
|
if (message->GetString("user_id", "") == fEditingUser)
|
|
|
|
fEditingWindow->PostMessage(message);
|
|
|
|
fRosterView->MessageReceived(message);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-19 18:25:58 -05:00
|
|
|
case kSelAccount:
|
|
|
|
{
|
2021-07-21 12:12:02 -05:00
|
|
|
AccountInstances accounts = fServer->GetActiveAccounts();
|
|
|
|
|
2021-06-19 18:25:58 -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-19 18:25:58 -05:00
|
|
|
return;
|
2021-07-21 12:12:02 -05:00
|
|
|
fRosterView->SetAccount(accounts.ValueAt(index));
|
2021-06-19 18:25:58 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSelNoAccount:
|
|
|
|
fRosterView->SetAccount(-1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BWindow::MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
RosterEditWindow::UpdateListItem(RosterItem* item)
|
|
|
|
{
|
|
|
|
fRosterView->UpdateListItem(item);
|
|
|
|
}
|