2010-05-07 04:47:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
2021-07-24 13:25:44 -05:00
|
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
2021-07-24 13:25:44 -05:00
|
|
|
* Jaidyn Levesque, jadedctrl@teknik.io
|
2010-05-07 04:47:10 -05:00
|
|
|
*/
|
|
|
|
|
2021-08-06 12:21:17 -05:00
|
|
|
#include "AccountsWindow.h"
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Button.h>
|
2021-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ControlLook.h>
|
2021-05-30 22:04:45 -05:00
|
|
|
#include <LayoutBuilder.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ListView.h>
|
2010-05-08 14:35:28 -05:00
|
|
|
#include <PopUpMenu.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <ScrollView.h>
|
|
|
|
|
|
|
|
#include <libinterface/BitmapMenuItem.h>
|
2021-07-23 23:43:42 -05:00
|
|
|
#include <libinterface/MenuButton.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-09 01:27:48 -05:00
|
|
|
#include "AccountDialog.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "AccountListItem.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "ChatProtocol.h"
|
Redesign add-on disconnection
Currently, add-ons are disconnected when ChatProtocol::Shutdown() is
called, which the add-on can do by itself― but there is no standard way
for add-ons to notify the app about their Shutdown. Because of this,
they tend to not call Shutdown()― instead (as in the case of the Jabber
add-on), they display a BAlert (IM_ERROR) notifying the user of the
connection error, but the account is considered active by Cardie (and
its threads are still existant, including its ProtocolLooper).
Zombies are bad, so this is redesigned somewhat with this commit:
Protocols should no longer call ChatProtocol::Shutdown() themselves,
they must send an IM_MESSAGE of IM_PROTOCOL_DISABLE to the app.
This will delete its ProtocolLooper, which in turn will send a
notification to the user and delete the ChatProtocol, and so
calling ChatProtocol::Shutdown().
In the included protocols, an IM_ERROR is sent right before
IM_PROTOCOL_DISABLE is sent if due to a connection error. This is not
required, but it is courteous to inform your user about the "why." :)
2021-07-18 17:52:36 -05:00
|
|
|
#include "ChatProtocolMessages.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "ProtocolManager.h"
|
|
|
|
#include "ProtocolSettings.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "MainWindow.h"
|
2021-06-11 20:33:28 -05:00
|
|
|
#include "Server.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "TheApp.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-06-11 20:33:28 -05:00
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
2021-08-06 12:21:17 -05:00
|
|
|
#define B_TRANSLATION_CONTEXT "AccountsWindow"
|
2021-07-19 09:54:27 -05:00
|
|
|
|
|
|
|
|
2021-06-11 20:33:28 -05:00
|
|
|
const uint32 kAddAccount = 'adac';
|
|
|
|
const uint32 kEditAccount = 'edac';
|
|
|
|
const uint32 kDelAccount = 'dlac';
|
|
|
|
const uint32 kToggleAccount = 'tgac';
|
|
|
|
const uint32 kSelect = 'selt';
|
2010-05-16 16:02:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare_by_name(const void* _item1, const void* _item2)
|
|
|
|
{
|
|
|
|
AccountListItem* item1 = *(AccountListItem**)_item1;
|
|
|
|
AccountListItem* item2 = *(AccountListItem**)_item2;
|
|
|
|
|
|
|
|
return strcasecmp(item1->Account(), item2->Account());
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::AccountsWindow()
|
|
|
|
:
|
|
|
|
BWindow(BRect(200, 200, 300, 400),
|
|
|
|
B_TRANSLATE("Accounts"), B_TITLED_WINDOW,
|
|
|
|
B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
fListView = new BListView("accountsListView");
|
|
|
|
fListView->SetInvocationMessage(new BMessage(kEditAccount));
|
|
|
|
fListView->SetSelectionMessage(new BMessage(kSelect));
|
2021-08-06 12:21:17 -05:00
|
|
|
fListView->SetExplicitMinSize(BSize(B_SIZE_UNSET, BFont().Size() * 20));
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
BScrollView* scrollView = new BScrollView("scrollView", fListView,
|
|
|
|
B_WILL_DRAW, false, true);
|
|
|
|
|
2010-05-28 12:11:16 -05:00
|
|
|
ProtocolManager* pm = ProtocolManager::Get();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-08 14:35:28 -05:00
|
|
|
fProtosMenu = new BPopUpMenu(NULL, true);
|
2010-05-28 12:11:16 -05:00
|
|
|
for (uint32 i = 0; i < pm->CountProtocolAddOns(); i++) {
|
2021-06-20 12:44:20 -05:00
|
|
|
ChatProtocolAddOn* addOn = pm->ProtocolAddOnAt(i);
|
2010-05-16 16:02:50 -05:00
|
|
|
ProtocolSettings* settings = new ProtocolSettings(addOn);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
// Add accounts to list view
|
2010-05-08 14:35:28 -05:00
|
|
|
_LoadListView(settings);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
// Add menu items
|
|
|
|
BMessage* msg = new BMessage(kAddAccount);
|
2010-05-16 16:02:50 -05:00
|
|
|
msg->AddPointer("settings", settings);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
BitmapMenuItem* item = new BitmapMenuItem(
|
2021-05-22 12:05:44 -05:00
|
|
|
addOn->ProtoFriendlySignature(), msg, addOn->ProtoIcon());
|
2010-05-07 04:47:10 -05:00
|
|
|
fProtosMenu->AddItem(item);
|
|
|
|
}
|
2021-08-06 12:21:17 -05:00
|
|
|
fProtosMenu->SetTargetForItems(this);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-07-23 23:43:42 -05:00
|
|
|
MenuButton* proto = new MenuButton("addButton", B_TRANSLATE("Add"), NULL);
|
2010-05-08 14:35:28 -05:00
|
|
|
proto->SetMenu(fProtosMenu);
|
2021-07-19 09:54:27 -05:00
|
|
|
fDelButton = new BButton(B_TRANSLATE_COMMENT("Del", "Short for 'delete'"),
|
|
|
|
new BMessage(kDelAccount));
|
|
|
|
fEditButton = new BButton(B_TRANSLATE("Edit" B_UTF8_ELLIPSIS),
|
|
|
|
new BMessage(kEditAccount));
|
|
|
|
fToggleButton = new BButton(B_TRANSLATE("Enable"),
|
|
|
|
new BMessage(kToggleAccount));
|
2010-05-07 04:47:10 -05:00
|
|
|
fDelButton->SetEnabled(false);
|
|
|
|
fEditButton->SetEnabled(false);
|
2021-06-11 20:33:28 -05:00
|
|
|
fToggleButton->SetEnabled(false);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-05-30 22:04:45 -05:00
|
|
|
BLayoutBuilder::Group<>(this, B_VERTICAL)
|
2021-08-06 12:21:17 -05:00
|
|
|
.SetInsets(B_USE_DEFAULT_SPACING)
|
2010-05-07 04:47:10 -05:00
|
|
|
.Add(scrollView)
|
2021-05-30 22:04:45 -05:00
|
|
|
.AddGroup(B_HORIZONTAL)
|
2010-05-07 04:47:10 -05:00
|
|
|
.Add(proto)
|
|
|
|
.Add(fDelButton)
|
|
|
|
.AddGlue()
|
2021-06-11 20:33:28 -05:00
|
|
|
.Add(fToggleButton)
|
2010-05-07 04:47:10 -05:00
|
|
|
.Add(fEditButton)
|
|
|
|
.End()
|
2021-05-30 22:04:45 -05:00
|
|
|
.End();
|
2021-08-07 17:37:16 -05:00
|
|
|
|
|
|
|
CenterOnScreen();
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::MessageReceived(BMessage* msg)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
switch (msg->what) {
|
|
|
|
case kSelect: {
|
2021-06-11 20:33:28 -05:00
|
|
|
int32 index;
|
|
|
|
|
|
|
|
if (msg->FindInt32("index", &index) == B_OK) {
|
|
|
|
fDelButton->SetEnabled(index >= 0);
|
|
|
|
fEditButton->SetEnabled(index >= 0);
|
|
|
|
fToggleButton->SetEnabled(index >= 0);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
AccountListItem* item = (AccountListItem*)fListView->ItemAt(fListView->CurrentSelection());
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
if (_AccountInstance(item->Account()) > -1)
|
2021-07-19 09:54:27 -05:00
|
|
|
fToggleButton->SetLabel(B_TRANSLATE("Disable"));
|
2021-08-05 19:28:05 -05:00
|
|
|
else
|
2021-07-19 09:54:27 -05:00
|
|
|
fToggleButton->SetLabel(B_TRANSLATE("Enable"));
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-06-11 20:33:28 -05:00
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
case kAddAccount: {
|
2010-05-16 16:02:50 -05:00
|
|
|
void *pointer = NULL;
|
|
|
|
if (msg->FindPointer("settings", &pointer) == B_OK) {
|
|
|
|
ProtocolSettings* settings
|
|
|
|
= reinterpret_cast<ProtocolSettings*>(pointer);
|
|
|
|
if (settings) {
|
2021-07-19 09:54:27 -05:00
|
|
|
AccountDialog* dialog = new AccountDialog(
|
|
|
|
B_TRANSLATE("Add account"), settings);
|
2010-05-16 16:02:50 -05:00
|
|
|
dialog->SetTarget(this);
|
|
|
|
dialog->Show();
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kEditAccount: {
|
|
|
|
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
|
|
|
int32 selected = fListView->CurrentSelection(i);
|
|
|
|
if (selected < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AccountListItem* item
|
|
|
|
= dynamic_cast<AccountListItem*>(fListView->ItemAt(selected));
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
AccountDialog* dialog = new AccountDialog(
|
|
|
|
B_TRANSLATE("Edit account"), item->Settings(),
|
|
|
|
item->Account());
|
2010-05-16 16:02:50 -05:00
|
|
|
dialog->SetTarget(this);
|
2010-05-07 04:47:10 -05:00
|
|
|
dialog->Show();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kDelAccount: {
|
|
|
|
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
|
|
|
int32 selected = fListView->CurrentSelection(i);
|
|
|
|
if (selected < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AccountListItem* item
|
|
|
|
= dynamic_cast<AccountListItem*>(fListView->ItemAt(selected));
|
|
|
|
|
|
|
|
const char* account = item->Account();
|
|
|
|
ProtocolSettings* settings = item->Settings();
|
|
|
|
if (settings->Delete(account) == B_OK)
|
|
|
|
fListView->RemoveItem(item);
|
|
|
|
delete settings;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-06-11 20:33:28 -05:00
|
|
|
case kToggleAccount: {
|
|
|
|
int32 current = fListView->CurrentSelection();
|
|
|
|
AccountListItem* item;
|
|
|
|
|
|
|
|
if (current < 0
|
|
|
|
|| (item = (AccountListItem*)fListView->ItemAt(current)) == NULL)
|
|
|
|
break;
|
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
const char* account = item->Account();
|
|
|
|
int64 instance = _AccountInstance(account);
|
2021-06-11 20:33:28 -05:00
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
if (instance == -1)
|
|
|
|
_EnableAccount(account, item->Settings());
|
|
|
|
else
|
|
|
|
_DisableAccount(account, instance);
|
2021-06-11 20:33:28 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-05-29 23:28:04 -05:00
|
|
|
case kAccountAdded:
|
2010-05-16 16:02:50 -05:00
|
|
|
case kAccountRenamed: {
|
|
|
|
void* pointer = NULL;
|
|
|
|
BString account;
|
|
|
|
BString account2;
|
|
|
|
|
|
|
|
if (msg->FindPointer("settings", &pointer) != B_OK)
|
|
|
|
return;
|
2010-05-29 23:28:04 -05:00
|
|
|
if (msg->what == kAccountAdded) {
|
2010-05-16 16:02:50 -05:00
|
|
|
if (msg->FindString("account", &account) != B_OK)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (msg->FindString("from", &account) != B_OK)
|
|
|
|
return;
|
|
|
|
if (msg->FindString("to", &account2) != B_OK)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtocolSettings* settings
|
|
|
|
= reinterpret_cast<ProtocolSettings*>(pointer);
|
|
|
|
if (!settings)
|
|
|
|
return;
|
|
|
|
|
2010-05-29 23:28:04 -05:00
|
|
|
if (msg->what == kAccountAdded) {
|
2010-05-16 16:02:50 -05:00
|
|
|
// Add list item
|
|
|
|
AccountListItem* listItem
|
|
|
|
= new AccountListItem(settings, account.String());
|
|
|
|
fListView->AddItem(listItem);
|
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
_EnableAccount(account, settings);
|
2010-05-16 16:02:50 -05:00
|
|
|
} else {
|
|
|
|
// Rename list item
|
|
|
|
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
|
|
|
AccountListItem* listItem
|
|
|
|
= dynamic_cast<AccountListItem*>(fListView->ItemAt(i));
|
|
|
|
if (!listItem)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (account == listItem->Account()) {
|
|
|
|
listItem->SetAccount(account2.String());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fListView->SortItems(compare_by_name);
|
|
|
|
break;
|
|
|
|
}
|
2010-05-07 04:47:10 -05:00
|
|
|
default:
|
2021-08-06 12:21:17 -05:00
|
|
|
BWindow::MessageReceived(msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
2010-05-08 14:35:28 -05:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::_LoadListView(ProtocolSettings* settings)
|
2010-05-08 14:35:28 -05:00
|
|
|
{
|
|
|
|
if (!settings)
|
|
|
|
return;
|
|
|
|
|
2012-02-25 14:33:28 -06:00
|
|
|
BObjectList<BString> accounts = settings->Accounts();
|
2010-05-08 14:35:28 -05:00
|
|
|
|
|
|
|
// Add accounts to list view
|
2012-02-25 14:33:28 -06:00
|
|
|
for (int32 i = 0; i < accounts.CountItems(); i++) {
|
|
|
|
BString* account = accounts.ItemAt(i);
|
2010-05-16 16:02:50 -05:00
|
|
|
AccountListItem* listItem
|
2012-02-25 14:33:28 -06:00
|
|
|
= new AccountListItem(settings, account->String());
|
2010-05-08 14:35:28 -05:00
|
|
|
fListView->AddItem(listItem);
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 20:33:28 -05:00
|
|
|
|
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
void
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::_DisableAccount(const char* account, int64 instance)
|
2021-06-11 20:33:28 -05:00
|
|
|
{
|
2021-08-05 19:28:05 -05:00
|
|
|
BMessage* remove = new BMessage(IM_MESSAGE);
|
|
|
|
remove->AddInt32("im_what", IM_PROTOCOL_DISABLE);
|
|
|
|
remove->AddInt64("instance", instance);
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->PostMessage(remove);
|
2021-06-11 20:33:28 -05:00
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
fToggleButton->SetLabel(B_TRANSLATE("Enable"));
|
|
|
|
fToggleButton->SetEnabled(false);
|
2021-06-11 20:33:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-05 19:28:05 -05:00
|
|
|
void
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::_EnableAccount(const char* account,
|
2021-08-05 19:28:05 -05:00
|
|
|
ProtocolSettings* settings)
|
|
|
|
{
|
|
|
|
ProtocolManager::Get()->AddAccount(settings->AddOn(), account,
|
|
|
|
((TheApp*)be_app)->GetMainWindow());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int64
|
2021-08-06 12:21:17 -05:00
|
|
|
AccountsWindow::_AccountInstance(const char* account)
|
2021-08-05 19:28:05 -05:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
AccountInstances accs =
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts();
|
|
|
|
int64 instance = accs.ValueFor(BString(account), &found);
|
|
|
|
|
|
|
|
if (found == false)
|
|
|
|
instance = -1;
|
|
|
|
return instance;
|
|
|
|
}
|