From abb4d44684ce3411f73cb9dc70dd50e7e609c999 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Fri, 6 Aug 2021 14:14:14 -0500 Subject: [PATCH] Populate 'Accounts' menu with account submenus --- application/AppMessages.h | 3 + application/windows/MainWindow.cpp | 100 +++++++++++++++++++++++++---- application/windows/MainWindow.h | 12 ++++ 3 files changed, 103 insertions(+), 12 deletions(-) diff --git a/application/AppMessages.h b/application/AppMessages.h index fce3ee3..83d9196 100644 --- a/application/AppMessages.h +++ b/application/AppMessages.h @@ -62,4 +62,7 @@ const uint32 APP_ROOM_INFO = 'CYrw'; //! Edit the contact roster const uint32 APP_EDIT_ROSTER = 'CYer'; +//! Edit a specific account +const uint32 APP_EDIT_ACCOUNT = 'CYea'; + #endif // _APP_MESSAGES_H diff --git a/application/windows/MainWindow.cpp b/application/windows/MainWindow.cpp index 91a289c..9e3a5aa 100644 --- a/application/windows/MainWindow.cpp +++ b/application/windows/MainWindow.cpp @@ -17,11 +17,13 @@ #include #include +#include "AccountDialog.h" #include "AccountManager.h" #include "AccountsWindow.h" #include "AppMessages.h" #include "AppPreferences.h" #include "Cardie.h" +#include "ChatProtocolAddOn.h" #include "ChatProtocolMessages.h" #include "ConversationItem.h" #include "ConversationListView.h" @@ -29,10 +31,11 @@ #include "MainWindow.h" #include "NotifyMessage.h" #include "PreferencesWindow.h" +#include "ProtocolManager.h" +#include "ProtocolSettings.h" #include "ReplicantStatusView.h" #include "RosterEditWindow.h" #include "RosterWindow.h" -#include "Server.h" #include "StatusView.h" #include "TemplateWindow.h" @@ -117,6 +120,19 @@ MainWindow::MessageReceived(BMessage* message) win->Show(); 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: { BMessage* newMsg = new BMessage(IM_MESSAGE); @@ -218,9 +234,11 @@ MainWindow::MessageReceived(BMessage* message) } break; } - case APP_ACCOUNT_DISABLED: + case APP_ACCOUNT_DISABLED: { _ToggleMenuItems(); + _RefreshAccountsMenu(); break; + } case IM_MESSAGE: ImMessage(message); break; @@ -283,8 +301,9 @@ MainWindow::ImMessage(BMessage* msg) case IM_PROTOCOL_READY: { if (fConversation == NULL) fChatView->MessageReceived(msg); - _ToggleMenuItems(); fStatusView->MessageReceived(msg); + _ToggleMenuItems(); + _RefreshAccountsMenu(); break; } case IM_PROTOCOL_DISABLE: @@ -456,13 +475,6 @@ MainWindow::_CreateMenuBar() new BMessage(B_QUIT_REQUESTED), 'Q', B_COMMAND_KEY)); 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 BMenu* chatMenu = new BMenu(B_TRANSLATE("Chat")); chatMenu->AddItem(new BMenuItem(B_TRANSLATE("Join room" B_UTF8_ELLIPSIS), @@ -492,7 +504,7 @@ MainWindow::_CreateMenuBar() windowMenu->SetTargetForItems(this); menuBar->AddItem(programMenu); - menuBar->AddItem(accountsMenu); + menuBar->AddItem(_CreateAccountsMenu()); menuBar->AddItem(chatMenu); menuBar->AddItem(rosterMenu); 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 MainWindow::_ToggleMenuItems() { @@ -550,6 +591,41 @@ MainWindow::_EnsureConversationItem(BMessage* msg) fListView->SelectConversation(0); return item; } - return NULL; } + + +void +MainWindow::_PopulateWithAccounts(BMenu* menu, ProtocolSettings* settings) +{ + if (!settings) + return; + BObjectList 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); +} diff --git a/application/windows/MainWindow.h b/application/windows/MainWindow.h index 413d71d..ca529b1 100644 --- a/application/windows/MainWindow.h +++ b/application/windows/MainWindow.h @@ -9,6 +9,9 @@ #include +#include "Server.h" + +class BMenu; class BSplitView; class BTextView; @@ -16,6 +19,7 @@ class Conversation; class ConversationItem; class ConversationListView; class ConversationView; +class ProtocolSettings; class RosterItem; class RosterEditWindow; class RosterWindow; @@ -44,11 +48,19 @@ public: private: void _InitInterface(); + BMenuBar* _CreateMenuBar(); + BMenu* _CreateAccountsMenu(); + void _RefreshAccountsMenu(); + void _ToggleMenuItems(); ConversationItem* _EnsureConversationItem(BMessage* msg); + + void _PopulateWithAccounts(BMenu* menu, + ProtocolSettings* settings); + void _ReplaceMenu(const char* name, BMenu* newMenu); Server* fServer; RosterWindow* fRosterWindow;