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>
|
|
|
|
#include <GroupLayout.h>
|
|
|
|
#include <GroupLayoutBuilder.h>
|
|
|
|
#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"
|
|
|
|
#include "CayaProtocol.h"
|
|
|
|
#include "PreferencesAccounts.h"
|
|
|
|
#include "ProtocolManager.h"
|
|
|
|
#include "ProtocolSettings.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "TheApp.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-16 16:02:50 -05:00
|
|
|
const uint32 kAddAccount = 'adac';
|
|
|
|
const uint32 kEditAccount = 'edac';
|
|
|
|
const uint32 kDelAccount = 'dlac';
|
|
|
|
const uint32 kSelect = 'selt';
|
|
|
|
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
CayaProtocolAddOn* 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(
|
2010-05-16 16:02:50 -05:00
|
|
|
addOn->FriendlySignature(), msg, addOn->Icon());
|
2010-05-07 04:47:10 -05:00
|
|
|
fProtosMenu->AddItem(item);
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:35:28 -05:00
|
|
|
ToolButton* proto = new ToolButton("+", NULL);
|
|
|
|
proto->SetMenu(fProtosMenu);
|
|
|
|
fDelButton = new ToolButton("-", new BMessage(kDelAccount));
|
|
|
|
fEditButton = new ToolButton("Edit...", new BMessage(kEditAccount));
|
2010-05-07 04:47:10 -05:00
|
|
|
fDelButton->SetEnabled(false);
|
|
|
|
fEditButton->SetEnabled(false);
|
|
|
|
|
|
|
|
const float spacing = be_control_look->DefaultItemSpacing();
|
|
|
|
|
|
|
|
SetLayout(new BGroupLayout(B_HORIZONTAL, spacing));
|
|
|
|
AddChild(BGroupLayoutBuilder(B_VERTICAL)
|
|
|
|
.Add(scrollView)
|
|
|
|
.AddGroup(B_HORIZONTAL, spacing)
|
|
|
|
.Add(proto)
|
|
|
|
.Add(fDelButton)
|
|
|
|
.AddGlue()
|
|
|
|
.Add(fEditButton)
|
|
|
|
.End()
|
|
|
|
.SetInsets(spacing, spacing, spacing, spacing)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PreferencesAccounts::AttachedToWindow()
|
|
|
|
{
|
|
|
|
fListView->SetTarget(this);
|
|
|
|
fProtosMenu->SetTargetForItems(this);
|
|
|
|
fDelButton->SetTarget(this);
|
|
|
|
fEditButton->SetTarget(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PreferencesAccounts::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
|
|
|
case kSelect: {
|
|
|
|
int32 index;
|
|
|
|
|
|
|
|
if (msg->FindInt32("index", &index) == B_OK) {
|
|
|
|
fDelButton->SetEnabled(index >= 0);
|
|
|
|
fEditButton->SetEnabled(index >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
}
|
2010-05-16 16:02:50 -05:00
|
|
|
case kAccountSaved:
|
|
|
|
case kAccountRenamed: {
|
|
|
|
void* pointer = NULL;
|
|
|
|
BString account;
|
|
|
|
BString account2;
|
|
|
|
|
|
|
|
if (msg->FindPointer("settings", &pointer) != B_OK)
|
|
|
|
return;
|
|
|
|
if (msg->what == kAccountSaved) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (msg->what == kAccountSaved) {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
List<BString> accounts = settings->Accounts();
|
|
|
|
|
|
|
|
// Add accounts to list view
|
|
|
|
for (uint32 i = 0; i < accounts.CountItems(); i++) {
|
|
|
|
BString account = accounts.ItemAt(i);
|
2010-05-16 16:02:50 -05:00
|
|
|
AccountListItem* listItem
|
|
|
|
= new AccountListItem(settings, account.String());
|
2010-05-08 14:35:28 -05:00
|
|
|
fListView->AddItem(listItem);
|
|
|
|
}
|
|
|
|
}
|