Populate 'Accounts' menu with account submenus

This commit is contained in:
Jaidyn Ann 2021-08-06 14:14:14 -05:00
parent 2e746a975a
commit abb4d44684
3 changed files with 103 additions and 12 deletions

View File

@ -62,4 +62,7 @@ const uint32 APP_ROOM_INFO = 'CYrw';
//! Edit the contact roster //! Edit the contact roster
const uint32 APP_EDIT_ROSTER = 'CYer'; const uint32 APP_EDIT_ROSTER = 'CYer';
//! Edit a specific account
const uint32 APP_EDIT_ACCOUNT = 'CYea';
#endif // _APP_MESSAGES_H #endif // _APP_MESSAGES_H

View File

@ -17,11 +17,13 @@
#include <MenuBar.h> #include <MenuBar.h>
#include <TranslationUtils.h> #include <TranslationUtils.h>
#include "AccountDialog.h"
#include "AccountManager.h" #include "AccountManager.h"
#include "AccountsWindow.h" #include "AccountsWindow.h"
#include "AppMessages.h" #include "AppMessages.h"
#include "AppPreferences.h" #include "AppPreferences.h"
#include "Cardie.h" #include "Cardie.h"
#include "ChatProtocolAddOn.h"
#include "ChatProtocolMessages.h" #include "ChatProtocolMessages.h"
#include "ConversationItem.h" #include "ConversationItem.h"
#include "ConversationListView.h" #include "ConversationListView.h"
@ -29,10 +31,11 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "NotifyMessage.h" #include "NotifyMessage.h"
#include "PreferencesWindow.h" #include "PreferencesWindow.h"
#include "ProtocolManager.h"
#include "ProtocolSettings.h"
#include "ReplicantStatusView.h" #include "ReplicantStatusView.h"
#include "RosterEditWindow.h" #include "RosterEditWindow.h"
#include "RosterWindow.h" #include "RosterWindow.h"
#include "Server.h"
#include "StatusView.h" #include "StatusView.h"
#include "TemplateWindow.h" #include "TemplateWindow.h"
@ -117,6 +120,19 @@ MainWindow::MessageReceived(BMessage* message)
win->Show(); win->Show();
break; break;
} }
case APP_EDIT_ACCOUNT:
{
void* settings = NULL;
BString account = message->FindString("name");
message->FindPointer("settings", &settings);
if (account.IsEmpty() != true || settings != NULL) {
AccountDialog* win = new AccountDialog("Editing account",
(ProtocolSettings*)settings, account.String());
win->Show();
}
break;
}
case APP_NEW_CHAT: case APP_NEW_CHAT:
{ {
BMessage* newMsg = new BMessage(IM_MESSAGE); BMessage* newMsg = new BMessage(IM_MESSAGE);
@ -218,9 +234,11 @@ MainWindow::MessageReceived(BMessage* message)
} }
break; break;
} }
case APP_ACCOUNT_DISABLED: case APP_ACCOUNT_DISABLED: {
_ToggleMenuItems(); _ToggleMenuItems();
_RefreshAccountsMenu();
break; break;
}
case IM_MESSAGE: case IM_MESSAGE:
ImMessage(message); ImMessage(message);
break; break;
@ -283,8 +301,9 @@ MainWindow::ImMessage(BMessage* msg)
case IM_PROTOCOL_READY: { case IM_PROTOCOL_READY: {
if (fConversation == NULL) if (fConversation == NULL)
fChatView->MessageReceived(msg); fChatView->MessageReceived(msg);
_ToggleMenuItems();
fStatusView->MessageReceived(msg); fStatusView->MessageReceived(msg);
_ToggleMenuItems();
_RefreshAccountsMenu();
break; break;
} }
case IM_PROTOCOL_DISABLE: case IM_PROTOCOL_DISABLE:
@ -456,13 +475,6 @@ MainWindow::_CreateMenuBar()
new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY)); new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY));
programMenu->SetTargetForItems(this); programMenu->SetTargetForItems(this);
// Accounts
BMenu* accountsMenu = new BMenu(B_TRANSLATE("Accounts"));
accountsMenu->AddItem(
new BMenuItem(B_TRANSLATE("Manage accounts" B_UTF8_ELLIPSIS),
new BMessage(APP_SHOW_ACCOUNTS), '.', B_COMMAND_KEY));
accountsMenu->SetTargetForItems(this);
// Chat // Chat
BMenu* chatMenu = new BMenu(B_TRANSLATE("Chat")); BMenu* chatMenu = new BMenu(B_TRANSLATE("Chat"));
chatMenu->AddItem(new BMenuItem(B_TRANSLATE("Join room" B_UTF8_ELLIPSIS), chatMenu->AddItem(new BMenuItem(B_TRANSLATE("Join room" B_UTF8_ELLIPSIS),
@ -492,7 +504,7 @@ MainWindow::_CreateMenuBar()
windowMenu->SetTargetForItems(this); windowMenu->SetTargetForItems(this);
menuBar->AddItem(programMenu); menuBar->AddItem(programMenu);
menuBar->AddItem(accountsMenu); menuBar->AddItem(_CreateAccountsMenu());
menuBar->AddItem(chatMenu); menuBar->AddItem(chatMenu);
menuBar->AddItem(rosterMenu); menuBar->AddItem(rosterMenu);
menuBar->AddItem(windowMenu); menuBar->AddItem(windowMenu);
@ -501,6 +513,35 @@ MainWindow::_CreateMenuBar()
} }
BMenu*
MainWindow::_CreateAccountsMenu()
{
BMenu* accountsMenu = new BMenu(B_TRANSLATE("Accounts"));
ProtocolManager* pm = ProtocolManager::Get();
for (uint32 i = 0; i < pm->CountProtocolAddOns(); i++) {
ChatProtocolAddOn* addOn = pm->ProtocolAddOnAt(i);
ProtocolSettings* settings = new ProtocolSettings(addOn);
_PopulateWithAccounts(accountsMenu, settings);
}
accountsMenu->AddSeparatorItem();
accountsMenu->AddItem(
new BMenuItem(B_TRANSLATE("Manage accounts" B_UTF8_ELLIPSIS),
new BMessage(APP_SHOW_ACCOUNTS), '.', B_COMMAND_KEY));
accountsMenu->SetTargetForItems(this);
return accountsMenu;
}
void
MainWindow::_RefreshAccountsMenu()
{
_ReplaceMenu(B_TRANSLATE("Accounts"), _CreateAccountsMenu());
}
void void
MainWindow::_ToggleMenuItems() MainWindow::_ToggleMenuItems()
{ {
@ -550,6 +591,41 @@ MainWindow::_EnsureConversationItem(BMessage* msg)
fListView->SelectConversation(0); fListView->SelectConversation(0);
return item; return item;
} }
return NULL; return NULL;
} }
void
MainWindow::_PopulateWithAccounts(BMenu* menu, ProtocolSettings* settings)
{
if (!settings)
return;
BObjectList<BString> accounts = settings->Accounts();
// Add accounts to menu
for (int32 i = 0; i < accounts.CountItems(); i++) {
BString* account = accounts.ItemAt(i);
BMenu* accMenu = new BMenu(account->String());
BMessage* editMsg = new BMessage(APP_EDIT_ACCOUNT);
editMsg->AddPointer("settings", settings);
editMsg->AddString("account", *account);
accMenu->AddItem(
new BMenuItem(B_TRANSLATE("Modify account" B_UTF8_ELLIPSIS),
editMsg));
menu->AddItem(accMenu);
}
}
void
MainWindow::_ReplaceMenu(const char* name, BMenu* newMenu)
{
BMenuItem* old = fMenuBar->FindItem(name);
if (old == NULL || newMenu == NULL)
return;
int32 index = fMenuBar->IndexOf(old);
fMenuBar->RemoveItem(index);
fMenuBar->AddItem(newMenu, index);
}

View File

@ -9,6 +9,9 @@
#include <Window.h> #include <Window.h>
#include "Server.h"
class BMenu;
class BSplitView; class BSplitView;
class BTextView; class BTextView;
@ -16,6 +19,7 @@ class Conversation;
class ConversationItem; class ConversationItem;
class ConversationListView; class ConversationListView;
class ConversationView; class ConversationView;
class ProtocolSettings;
class RosterItem; class RosterItem;
class RosterEditWindow; class RosterEditWindow;
class RosterWindow; class RosterWindow;
@ -44,12 +48,20 @@ public:
private: private:
void _InitInterface(); void _InitInterface();
BMenuBar* _CreateMenuBar(); BMenuBar* _CreateMenuBar();
BMenu* _CreateAccountsMenu();
void _RefreshAccountsMenu();
void _ToggleMenuItems(); void _ToggleMenuItems();
ConversationItem* ConversationItem*
_EnsureConversationItem(BMessage* msg); _EnsureConversationItem(BMessage* msg);
void _PopulateWithAccounts(BMenu* menu,
ProtocolSettings* settings);
void _ReplaceMenu(const char* name, BMenu* newMenu);
Server* fServer; Server* fServer;
RosterWindow* fRosterWindow; RosterWindow* fRosterWindow;
RosterEditWindow* fRosterEditWindow; RosterEditWindow* fRosterEditWindow;