2010-05-07 04:47:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Button.h>
|
|
|
|
#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>
|
2010-05-08 14:35:28 -05:00
|
|
|
#include <libinterface/ToolButton.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 "PreferencesAccounts.h"
|
|
|
|
#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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
PreferencesAccounts::PreferencesAccounts()
|
|
|
|
: BView("Accounts", B_WILL_DRAW)
|
|
|
|
{
|
|
|
|
fListView = new BListView("accountsListView");
|
|
|
|
fListView->SetInvocationMessage(new BMessage(kEditAccount));
|
|
|
|
fListView->SetSelectionMessage(new BMessage(kSelect));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:59:40 -05:00
|
|
|
ToolButton* proto = new ToolButton("Add", NULL);
|
2010-05-08 14:35:28 -05:00
|
|
|
proto->SetMenu(fProtosMenu);
|
2021-05-30 22:04:45 -05:00
|
|
|
fDelButton = new BButton("Del", new BMessage(kDelAccount));
|
|
|
|
fEditButton = new BButton("Edit" B_UTF8_ELLIPSIS, new BMessage(kEditAccount));
|
2021-06-11 20:33:28 -05:00
|
|
|
fToggleButton = new BButton("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)
|
|
|
|
.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)
|
|
|
|
.SetInsets(0, 0, 0, 15)
|
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();
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PreferencesAccounts::AttachedToWindow()
|
|
|
|
{
|
|
|
|
fListView->SetTarget(this);
|
|
|
|
fProtosMenu->SetTargetForItems(this);
|
|
|
|
fDelButton->SetTarget(this);
|
|
|
|
fEditButton->SetTarget(this);
|
2021-06-11 20:33:28 -05:00
|
|
|
fToggleButton->SetTarget(this);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PreferencesAccounts::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
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-06-11 20:33:28 -05:00
|
|
|
if (_AccountEnabled(item->Account() ) == true) {
|
|
|
|
fToggleButton->SetLabel("Disable");
|
|
|
|
fToggleButton->SetEnabled(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fToggleButton->SetLabel("Enable");
|
|
|
|
fToggleButton->SetEnabled(false);
|
|
|
|
}
|
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) {
|
|
|
|
AccountDialog* dialog = new AccountDialog("Add account",
|
|
|
|
settings);
|
|
|
|
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));
|
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
AccountDialog* dialog = new AccountDialog("Edit account",
|
|
|
|
item->Settings(), item->Account());
|
|
|
|
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;
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts();
|
|
|
|
int64 instance = accs.ValueFor(BString(item->Account()), &found);
|
|
|
|
if (found == false)
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
BMessage* remove = new BMessage(IM_MESSAGE);
|
|
|
|
remove->AddInt32("im_what", IM_PROTOCOL_DISABLE);
|
2021-06-11 20:33:28 -05:00
|
|
|
remove->AddInt64("instance", instance);
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->PostMessage(remove);
|
|
|
|
|
|
|
|
fToggleButton->SetLabel("Enable");
|
|
|
|
fToggleButton->SetEnabled(false);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Add protocol/account instance
|
|
|
|
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
|
|
|
|
ProtocolManager::Get()->AddAccount(settings->AddOn(),
|
|
|
|
account.String(), theApp->GetMainWindow());
|
|
|
|
} 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:
|
|
|
|
BView::MessageReceived(msg);
|
|
|
|
}
|
|
|
|
}
|
2010-05-08 14:35:28 -05:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PreferencesAccounts::_LoadListView(ProtocolSettings* settings)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreferencesAccounts::_AccountEnabled(const char* account)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts();
|
|
|
|
accs.ValueFor(BString(account), &found);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|