Account-toggling through Accounts menu

This commit is contained in:
Jaidyn Ann 2021-08-07 17:37:16 -05:00
parent abb4d44684
commit 4bd821eee5
5 changed files with 53 additions and 8 deletions

View File

@ -65,4 +65,7 @@ const uint32 APP_EDIT_ROSTER = 'CYer';
//! Edit a specific account
const uint32 APP_EDIT_ACCOUNT = 'CYea';
//! Toggle a specific account
const uint32 APP_TOGGLE_ACCOUNT = 'CYta';
#endif // _APP_MESSAGES_H

View File

@ -512,7 +512,7 @@ ConversationView::_FakeChatNoAccounts()
welcome.AddString("body", B_TRANSLATE("Afterward, you can join a room or start a chat through the Chat menu. :-)"));
welcome.AddString("user_id", B_TRANSLATE("Master Foo"));
welcome.AddString("body", B_TRANSLATE("Add an account through the [Program→Preferences] menu to get started."));
welcome.AddString("body", B_TRANSLATE("Add an account through the [Accounts] menu to get started."));
welcome.AddString("user_id", B_TRANSLATE("Master Foo"));
welcome.AddString("body", B_TRANSLATE("It looks like you don't have any accounts set up."));

View File

@ -110,6 +110,8 @@ AccountsWindow::AccountsWindow()
.Add(fEditButton)
.End()
.End();
CenterOnScreen();
}

View File

@ -123,16 +123,37 @@ MainWindow::MessageReceived(BMessage* message)
case APP_EDIT_ACCOUNT:
{
void* settings = NULL;
BString account = message->FindString("name");
BString account = message->FindString("account");
message->FindPointer("settings", &settings);
if (account.IsEmpty() != true || settings != NULL) {
if (account.IsEmpty() == false && settings != NULL) {
AccountDialog* win = new AccountDialog("Editing account",
(ProtocolSettings*)settings, account.String());
win->Show();
}
break;
}
case APP_TOGGLE_ACCOUNT:
{
ProtocolSettings* settings = NULL;
BString account = message->FindString("account");
int64 instance = message->GetInt64("instance", -1);
message->FindPointer("settings", (void**)&settings);
if (account.IsEmpty() == false && settings != NULL) {
// Enable
if (instance == -1)
ProtocolManager::Get()->AddAccount(settings->AddOn(),
account, this);
else {
BMessage remove(IM_MESSAGE);
remove.AddInt32("im_what", IM_PROTOCOL_DISABLE);
remove.AddInt64("instance", instance);
PostMessage(&remove);
}
}
break;
}
case APP_NEW_CHAT:
{
BMessage* newMsg = new BMessage(IM_MESSAGE);
@ -519,14 +540,17 @@ MainWindow::_CreateAccountsMenu()
BMenu* accountsMenu = new BMenu(B_TRANSLATE("Accounts"));
ProtocolManager* pm = ProtocolManager::Get();
bool hasAccounts = false;
for (uint32 i = 0; i < pm->CountProtocolAddOns(); i++) {
ChatProtocolAddOn* addOn = pm->ProtocolAddOnAt(i);
ProtocolSettings* settings = new ProtocolSettings(addOn);
_PopulateWithAccounts(accountsMenu, settings);
if (_PopulateWithAccounts(accountsMenu, settings) == true)
hasAccounts = true;
}
accountsMenu->AddSeparatorItem();
if (hasAccounts == true)
accountsMenu->AddSeparatorItem();
accountsMenu->AddItem(
new BMenuItem(B_TRANSLATE("Manage accounts" B_UTF8_ELLIPSIS),
new BMessage(APP_SHOW_ACCOUNTS), '.', B_COMMAND_KEY));
@ -595,11 +619,11 @@ MainWindow::_EnsureConversationItem(BMessage* msg)
}
void
bool
MainWindow::_PopulateWithAccounts(BMenu* menu, ProtocolSettings* settings)
{
if (!settings)
return;
return false;
BObjectList<BString> accounts = settings->Accounts();
// Add accounts to menu
@ -607,15 +631,31 @@ MainWindow::_PopulateWithAccounts(BMenu* menu, ProtocolSettings* settings)
BString* account = accounts.ItemAt(i);
BMenu* accMenu = new BMenu(account->String());
BString toggleLabel = B_TRANSLATE("Enable");
bool isActive = false;
int64 instance = fServer->GetActiveAccounts().ValueFor(*account, &isActive);
if (isActive == true)
toggleLabel = B_TRANSLATE("Disable");
BMessage* toggleMsg = new BMessage(APP_TOGGLE_ACCOUNT);
toggleMsg->AddPointer("settings", settings);
toggleMsg->AddString("account", *account);
if (isActive == true)
toggleMsg->AddInt64("instance", instance);
BMessage* editMsg = new BMessage(APP_EDIT_ACCOUNT);
editMsg->AddPointer("settings", settings);
editMsg->AddString("account", *account);
accMenu->AddItem(new BMenuItem(toggleLabel.String(), toggleMsg));
accMenu->AddItem(
new BMenuItem(B_TRANSLATE("Modify account" B_UTF8_ELLIPSIS),
editMsg));
menu->AddItem(accMenu);
}
return (accounts.CountItems() > 0);
}

View File

@ -58,7 +58,7 @@ private:
ConversationItem*
_EnsureConversationItem(BMessage* msg);
void _PopulateWithAccounts(BMenu* menu,
bool _PopulateWithAccounts(BMenu* menu,
ProtocolSettings* settings);
void _ReplaceMenu(const char* name, BMenu* newMenu);