From a49e957b16d3908559b2f2671114d77c0e9d53c6 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Thu, 5 Aug 2021 19:28:05 -0500 Subject: [PATCH] Re-enabling of accounts after disable Accounts can now be toggled on-and-off freely. Fixes #42 --- application/Server.cpp | 24 +++++-- application/Server.h | 4 +- application/preferences/AccountDialog.cpp | 1 - .../preferences/PreferencesAccounts.cpp | 67 +++++++++++-------- application/preferences/PreferencesAccounts.h | 7 +- 5 files changed, 64 insertions(+), 39 deletions(-) diff --git a/application/Server.cpp b/application/Server.cpp index 85abcbe..ae1d108 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -87,6 +87,8 @@ Server::Server() ChatCommand* cmd = new ChatCommand(&temp); fCommands.AddItem(cmd->GetName(), cmd); } + + fStarted = false; } @@ -101,14 +103,21 @@ Server::Quit() void Server::LoginAll() { - for (uint32 i = 0; i < fLoopers.CountItems(); i++) { - ProtocolLooper* looper = fLoopers.ValueAt(i); + for (uint32 i = 0; i < fLoopers.CountItems(); i++) + Login(fLoopers.ValueAt(i)); + fStarted = true; +} - BMessage* msg = new BMessage(IM_MESSAGE); - msg->AddInt32("im_what", IM_SET_OWN_STATUS); - msg->AddInt32("status", STATUS_ONLINE); + +void +Server::Login(ProtocolLooper* looper) +{ + BMessage* msg = new BMessage(IM_MESSAGE); + msg->AddInt32("im_what", IM_SET_OWN_STATUS); + msg->AddInt32("status", STATUS_ONLINE); + + if (looper != NULL) looper->PostMessage(msg); - } } @@ -668,6 +677,9 @@ Server::AddProtocolLooper(bigtime_t instanceId, ChatProtocol* cayap) fLoopers.AddItem(instanceId, looper); fAccounts.AddItem(cayap->GetName(), instanceId); fAccountEnabled.AddItem(cayap->GetName(), false); + + if (fStarted == true) + Login(looper); } diff --git a/application/Server.h b/application/Server.h index 74839c7..1563e26 100644 --- a/application/Server.h +++ b/application/Server.h @@ -35,6 +35,7 @@ public: Server(); void Quit(); void LoginAll(); + void Login(ProtocolLooper* looper); virtual filter_result Filter(BMessage* message, BHandler** target); filter_result ImMessage(BMessage* msg); @@ -90,12 +91,11 @@ private: ProtocolLoopers fLoopers; AccountInstances fAccounts; BoolMap fAccountEnabled; + bool fStarted; CommandMap fCommands; BObjectList fChatItems; BObjectList fUserItems; }; - #endif // _SERVER_H - diff --git a/application/preferences/AccountDialog.cpp b/application/preferences/AccountDialog.cpp index 251063f..4289eea 100644 --- a/application/preferences/AccountDialog.cpp +++ b/application/preferences/AccountDialog.cpp @@ -122,7 +122,6 @@ AccountDialog::MessageReceived(BMessage* msg) saveMsg->AddString("account", fAccountName->Text()); BMessenger(fTarget).SendMessage(saveMsg); } - Close(); } else { BAlert* alert = new BAlert("", error.String(), diff --git a/application/preferences/PreferencesAccounts.cpp b/application/preferences/PreferencesAccounts.cpp index ae0a560..ac4634e 100644 --- a/application/preferences/PreferencesAccounts.cpp +++ b/application/preferences/PreferencesAccounts.cpp @@ -133,14 +133,10 @@ PreferencesAccounts::MessageReceived(BMessage* msg) if (index >= 0) { AccountListItem* item = (AccountListItem*)fListView->ItemAt(fListView->CurrentSelection()); - if (_AccountEnabled(item->Account() ) == true) { + if (_AccountInstance(item->Account()) > -1) fToggleButton->SetLabel(B_TRANSLATE("Disable")); - fToggleButton->SetEnabled(true); - } - else { + else fToggleButton->SetLabel(B_TRANSLATE("Enable")); - fToggleButton->SetEnabled(false); - } } } break; @@ -201,19 +197,13 @@ PreferencesAccounts::MessageReceived(BMessage* msg) || (item = (AccountListItem*)fListView->ItemAt(current)) == NULL) break; - bool found = false; - AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts(); - int64 instance = accs.ValueFor(BString(item->Account()), &found); - if (found == false) - return; + const char* account = item->Account(); + int64 instance = _AccountInstance(account); - BMessage* remove = new BMessage(IM_MESSAGE); - remove->AddInt32("im_what", IM_PROTOCOL_DISABLE); - remove->AddInt64("instance", instance); - ((TheApp*)be_app)->GetMainWindow()->PostMessage(remove); - - fToggleButton->SetLabel(B_TRANSLATE("Enable")); - fToggleButton->SetEnabled(false); + if (instance == -1) + _EnableAccount(account, item->Settings()); + else + _DisableAccount(account, instance); break; } case kAccountAdded: @@ -245,10 +235,7 @@ PreferencesAccounts::MessageReceived(BMessage* msg) = new AccountListItem(settings, account.String()); fListView->AddItem(listItem); - // Add protocol/account instance - TheApp* theApp = reinterpret_cast(be_app); - ProtocolManager::Get()->AddAccount(settings->AddOn(), - account.String(), theApp->GetMainWindow()); + _EnableAccount(account, settings); } else { // Rename list item for (int32 i = 0; i < fListView->CountItems(); i++) { @@ -263,7 +250,6 @@ PreferencesAccounts::MessageReceived(BMessage* msg) } } } - fListView->SortItems(compare_by_name); break; } @@ -291,14 +277,37 @@ PreferencesAccounts::_LoadListView(ProtocolSettings* settings) } -bool -PreferencesAccounts::_AccountEnabled(const char* account) +void +PreferencesAccounts::_DisableAccount(const char* account, int64 instance) { - bool found = false; - AccountInstances accs = ((TheApp*)be_app)->GetMainWindow()->GetServer()->GetAccounts(); - accs.ValueFor(BString(account), &found); + BMessage* remove = new BMessage(IM_MESSAGE); + remove->AddInt32("im_what", IM_PROTOCOL_DISABLE); + 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; +} diff --git a/application/preferences/PreferencesAccounts.h b/application/preferences/PreferencesAccounts.h index a1372be..1c3be07 100644 --- a/application/preferences/PreferencesAccounts.h +++ b/application/preferences/PreferencesAccounts.h @@ -29,7 +29,12 @@ private: BButton* fToggleButton; 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