Re-enabling of accounts after disable

Accounts can now be toggled on-and-off freely.
Fixes #42
This commit is contained in:
Jaidyn Ann 2021-08-05 19:28:05 -05:00
parent f0ce3e87c6
commit a49e957b16
5 changed files with 64 additions and 39 deletions

View File

@ -87,6 +87,8 @@ Server::Server()
ChatCommand* cmd = new ChatCommand(&temp); ChatCommand* cmd = new ChatCommand(&temp);
fCommands.AddItem(cmd->GetName(), cmd); fCommands.AddItem(cmd->GetName(), cmd);
} }
fStarted = false;
} }
@ -101,14 +103,21 @@ Server::Quit()
void void
Server::LoginAll() Server::LoginAll()
{ {
for (uint32 i = 0; i < fLoopers.CountItems(); i++) { for (uint32 i = 0; i < fLoopers.CountItems(); i++)
ProtocolLooper* looper = fLoopers.ValueAt(i); Login(fLoopers.ValueAt(i));
fStarted = true;
}
void
Server::Login(ProtocolLooper* looper)
{
BMessage* msg = new BMessage(IM_MESSAGE); BMessage* msg = new BMessage(IM_MESSAGE);
msg->AddInt32("im_what", IM_SET_OWN_STATUS); msg->AddInt32("im_what", IM_SET_OWN_STATUS);
msg->AddInt32("status", STATUS_ONLINE); msg->AddInt32("status", STATUS_ONLINE);
if (looper != NULL)
looper->PostMessage(msg); looper->PostMessage(msg);
}
} }
@ -668,6 +677,9 @@ Server::AddProtocolLooper(bigtime_t instanceId, ChatProtocol* cayap)
fLoopers.AddItem(instanceId, looper); fLoopers.AddItem(instanceId, looper);
fAccounts.AddItem(cayap->GetName(), instanceId); fAccounts.AddItem(cayap->GetName(), instanceId);
fAccountEnabled.AddItem(cayap->GetName(), false); fAccountEnabled.AddItem(cayap->GetName(), false);
if (fStarted == true)
Login(looper);
} }

View File

@ -35,6 +35,7 @@ public:
Server(); Server();
void Quit(); void Quit();
void LoginAll(); void LoginAll();
void Login(ProtocolLooper* looper);
virtual filter_result Filter(BMessage* message, BHandler** target); virtual filter_result Filter(BMessage* message, BHandler** target);
filter_result ImMessage(BMessage* msg); filter_result ImMessage(BMessage* msg);
@ -90,12 +91,11 @@ private:
ProtocolLoopers fLoopers; ProtocolLoopers fLoopers;
AccountInstances fAccounts; AccountInstances fAccounts;
BoolMap fAccountEnabled; BoolMap fAccountEnabled;
bool fStarted;
CommandMap fCommands; CommandMap fCommands;
BObjectList<BMessage> fChatItems; BObjectList<BMessage> fChatItems;
BObjectList<BMessage> fUserItems; BObjectList<BMessage> fUserItems;
}; };
#endif // _SERVER_H #endif // _SERVER_H

View File

@ -122,7 +122,6 @@ AccountDialog::MessageReceived(BMessage* msg)
saveMsg->AddString("account", fAccountName->Text()); saveMsg->AddString("account", fAccountName->Text());
BMessenger(fTarget).SendMessage(saveMsg); BMessenger(fTarget).SendMessage(saveMsg);
} }
Close(); Close();
} else { } else {
BAlert* alert = new BAlert("", error.String(), BAlert* alert = new BAlert("", error.String(),

View File

@ -133,14 +133,10 @@ PreferencesAccounts::MessageReceived(BMessage* msg)
if (index >= 0) { if (index >= 0) {
AccountListItem* item = (AccountListItem*)fListView->ItemAt(fListView->CurrentSelection()); AccountListItem* item = (AccountListItem*)fListView->ItemAt(fListView->CurrentSelection());
if (_AccountEnabled(item->Account() ) == true) { if (_AccountInstance(item->Account()) > -1)
fToggleButton->SetLabel(B_TRANSLATE("Disable")); fToggleButton->SetLabel(B_TRANSLATE("Disable"));
fToggleButton->SetEnabled(true); else
}
else {
fToggleButton->SetLabel(B_TRANSLATE("Enable")); fToggleButton->SetLabel(B_TRANSLATE("Enable"));
fToggleButton->SetEnabled(false);
}
} }
} }
break; break;
@ -201,19 +197,13 @@ PreferencesAccounts::MessageReceived(BMessage* msg)
|| (item = (AccountListItem*)fListView->ItemAt(current)) == NULL) || (item = (AccountListItem*)fListView->ItemAt(current)) == NULL)
break; break;
bool found = false; const char* account = item->Account();
AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts(); int64 instance = _AccountInstance(account);
int64 instance = accs.ValueFor(BString(item->Account()), &found);
if (found == false)
return;
BMessage* remove = new BMessage(IM_MESSAGE); if (instance == -1)
remove->AddInt32("im_what", IM_PROTOCOL_DISABLE); _EnableAccount(account, item->Settings());
remove->AddInt64("instance", instance); else
((TheApp*)be_app)->GetMainWindow()->PostMessage(remove); _DisableAccount(account, instance);
fToggleButton->SetLabel(B_TRANSLATE("Enable"));
fToggleButton->SetEnabled(false);
break; break;
} }
case kAccountAdded: case kAccountAdded:
@ -245,10 +235,7 @@ PreferencesAccounts::MessageReceived(BMessage* msg)
= new AccountListItem(settings, account.String()); = new AccountListItem(settings, account.String());
fListView->AddItem(listItem); fListView->AddItem(listItem);
// Add protocol/account instance _EnableAccount(account, settings);
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
ProtocolManager::Get()->AddAccount(settings->AddOn(),
account.String(), theApp->GetMainWindow());
} else { } else {
// Rename list item // Rename list item
for (int32 i = 0; i < fListView->CountItems(); i++) { for (int32 i = 0; i < fListView->CountItems(); i++) {
@ -263,7 +250,6 @@ PreferencesAccounts::MessageReceived(BMessage* msg)
} }
} }
} }
fListView->SortItems(compare_by_name); fListView->SortItems(compare_by_name);
break; break;
} }
@ -291,14 +277,37 @@ PreferencesAccounts::_LoadListView(ProtocolSettings* settings)
} }
bool void
PreferencesAccounts::_AccountEnabled(const char* account) PreferencesAccounts::_DisableAccount(const char* account, int64 instance)
{ {
bool found = false; BMessage* remove = new BMessage(IM_MESSAGE);
AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts(); remove->AddInt32("im_what", IM_PROTOCOL_DISABLE);
accs.ValueFor(BString(account), &found); remove->AddInt64("instance", instance);
((TheApp*)be_app)->GetMainWindow()->PostMessage(remove);
return found; fToggleButton->SetLabel(B_TRANSLATE("Enable"));
fToggleButton->SetEnabled(false);
} }
void
PreferencesAccounts::_EnableAccount(const char* account,
ProtocolSettings* settings)
{
ProtocolManager::Get()->AddAccount(settings->AddOn(), account,
((TheApp*)be_app)->GetMainWindow());
}
int64
PreferencesAccounts::_AccountInstance(const char* account)
{
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;
}

View File

@ -29,7 +29,12 @@ private:
BButton* fToggleButton; BButton* fToggleButton;
void _LoadListView(ProtocolSettings* settings); void _LoadListView(ProtocolSettings* settings);
bool _AccountEnabled(const char* account);
void _DisableAccount(const char* account, int64 instance);
void _EnableAccount(const char* account,
ProtocolSettings* settings);
int64 _AccountInstance(const char* account);
}; };
#endif // _PREFERENCES_ACCOUNTS_H #endif // _PREFERENCES_ACCOUNTS_H