2021-07-21 12:12:02 -05:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "AccountsMenu.h"
|
2021-07-24 21:11:34 -05:00
|
|
|
|
|
2021-08-03 10:29:19 -05:00
|
|
|
|
#include <Bitmap.h>
|
2021-07-24 21:11:34 -05:00
|
|
|
|
#include <Catalog.h>
|
|
|
|
|
#include <MenuItem.h>
|
|
|
|
|
|
2021-08-03 10:29:19 -05:00
|
|
|
|
#include <libinterface/BitmapMenuItem.h>
|
|
|
|
|
#include <libinterface/BitmapUtils.h>
|
|
|
|
|
|
2021-07-28 10:13:31 -05:00
|
|
|
|
#include "AccountMenuItem.h"
|
2021-08-03 10:29:19 -05:00
|
|
|
|
#include "ImageCache.h"
|
2021-07-21 12:12:02 -05:00
|
|
|
|
#include "MainWindow.h"
|
|
|
|
|
#include "Server.h"
|
|
|
|
|
#include "TheApp.h"
|
|
|
|
|
|
2021-07-24 21:11:34 -05:00
|
|
|
|
|
|
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
|
#define B_TRANSLATION_CONTEXT "AccountsMenu"
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
|
|
|
|
|
2021-07-28 10:13:31 -05:00
|
|
|
|
int32 AccountsMenu::fDefaultSelection = 0;
|
|
|
|
|
|
|
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
|
AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg,
|
|
|
|
|
Server* server)
|
2021-07-21 12:12:02 -05:00
|
|
|
|
:
|
2021-08-03 13:19:25 -05:00
|
|
|
|
BPopUpMenu(name),
|
2021-07-21 12:12:02 -05:00
|
|
|
|
fAccountMessage(msg),
|
2021-08-03 13:19:25 -05:00
|
|
|
|
fAllMessage(allMsg),
|
|
|
|
|
fServer(server)
|
2021-07-21 12:12:02 -05:00
|
|
|
|
{
|
|
|
|
|
_PopulateMenu();
|
|
|
|
|
SetRadioMode(true);
|
|
|
|
|
SetLabelFromMarked(true);
|
2021-08-03 13:19:25 -05:00
|
|
|
|
fServer->RegisterObserver(this);
|
|
|
|
|
}
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
|
|
|
|
|
|
AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg)
|
|
|
|
|
:
|
|
|
|
|
AccountsMenu(name, msg, allMsg,
|
|
|
|
|
((TheApp*)be_app)->GetMainWindow()->GetServer())
|
|
|
|
|
{
|
2021-07-21 12:12:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
|
|
|
|
|
|
|
2021-07-21 12:12:02 -05:00
|
|
|
|
AccountsMenu::~AccountsMenu()
|
|
|
|
|
{
|
|
|
|
|
delete fAllMessage;
|
2021-08-03 13:19:25 -05:00
|
|
|
|
fServer->UnregisterObserver(this);
|
2021-07-21 12:12:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
AccountsMenu::ObserveInteger(int32 what, int32 value)
|
|
|
|
|
{
|
|
|
|
|
_PopulateMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-28 10:13:31 -05:00
|
|
|
|
void
|
|
|
|
|
AccountsMenu::SetDefaultSelection(BMenuItem* item)
|
|
|
|
|
{
|
|
|
|
|
fDefaultSelection = IndexOf(item);
|
|
|
|
|
if (fAllMessage != NULL)
|
|
|
|
|
fDefaultSelection--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-21 12:12:02 -05:00
|
|
|
|
void
|
|
|
|
|
AccountsMenu::_PopulateMenu()
|
|
|
|
|
{
|
2021-08-03 10:29:19 -05:00
|
|
|
|
// Add 'all' item if missing
|
|
|
|
|
if (fAllMessage != NULL && FindItem(B_TRANSLATE("All")) == NULL) {
|
|
|
|
|
BBitmap* icon = _EnsureAsteriskIcon();
|
|
|
|
|
AddItem(new BitmapMenuItem(B_TRANSLATE("All"), new BMessage(*fAllMessage),
|
|
|
|
|
icon, 0, 0, false));
|
|
|
|
|
}
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
|
AccountInstances accounts = fServer->GetActiveAccounts();
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
2021-08-03 10:29:19 -05:00
|
|
|
|
// Add protocol item if not already in menu
|
2021-07-24 15:44:23 -05:00
|
|
|
|
for (int i = 0; i < accounts.CountItems(); i++) {
|
2021-08-03 10:29:19 -05:00
|
|
|
|
int64 instance = accounts.ValueAt(i);
|
2021-07-24 15:44:23 -05:00
|
|
|
|
BString label = accounts.KeyAt(i).String();
|
|
|
|
|
if (label.CountChars() > 15) {
|
|
|
|
|
label.RemoveChars(16, label.CountChars() - 16);
|
|
|
|
|
label << B_UTF8_ELLIPSIS;
|
|
|
|
|
}
|
2021-08-03 10:29:19 -05:00
|
|
|
|
|
|
|
|
|
if (FindItem(label.String()) != NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
|
ProtocolLooper* looper = fServer->GetProtocolLooper(instance);
|
2021-08-03 10:29:19 -05:00
|
|
|
|
BBitmap* icon = _EnsureProtocolIcon(label.String(), looper);
|
|
|
|
|
|
|
|
|
|
BMessage* message = new BMessage(fAccountMessage);
|
|
|
|
|
message->AddInt64("instance", instance);
|
|
|
|
|
AddItem(new AccountMenuItem(label.String(), message, icon));
|
2021-07-24 15:44:23 -05:00
|
|
|
|
}
|
2021-07-21 12:12:02 -05:00
|
|
|
|
|
2021-07-28 10:13:31 -05:00
|
|
|
|
int32 selection = fDefaultSelection;
|
|
|
|
|
|
2021-08-03 10:29:19 -05:00
|
|
|
|
// If an account has been disabled since last population… get ridda it
|
|
|
|
|
if ((fAllMessage != NULL && CountItems() - 1 > accounts.CountItems())
|
|
|
|
|
|| (fAllMessage == NULL && CountItems() > accounts.CountItems()))
|
|
|
|
|
for (int i = 0; i < CountItems(); i++) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
int64 instance = ItemAt(i)->Message()->GetInt64("instance", 0);
|
|
|
|
|
for (int j = 0; j < accounts.CountItems(); j++)
|
|
|
|
|
if (accounts.ValueAt(j) == instance)
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
|
|
if (found == false)
|
|
|
|
|
RemoveItem(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply last/default selection
|
2021-07-28 10:13:31 -05:00
|
|
|
|
if (fAllMessage == NULL && selection < CountItems() && selection >= 0)
|
|
|
|
|
ItemAt(selection)->SetMarked(true);
|
|
|
|
|
else if (CountItems() > 0)
|
2021-07-21 12:12:02 -05:00
|
|
|
|
ItemAt(0)->SetMarked(true);
|
|
|
|
|
else
|
|
|
|
|
SetEnabled(false);
|
|
|
|
|
}
|
2021-08-03 10:29:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
|
AccountsMenu::_EnsureProtocolIcon(const char* label, ProtocolLooper* looper)
|
|
|
|
|
{
|
|
|
|
|
BFont font;
|
|
|
|
|
BBitmap* icon = ImageCache::Get()->GetImage(label);
|
|
|
|
|
|
|
|
|
|
if (icon == NULL && looper != NULL && looper->Protocol()->Icon() != NULL) {
|
|
|
|
|
BBitmap* unscaled = looper->Protocol()->Icon();
|
|
|
|
|
icon = RescaleBitmap(unscaled, font.Size(), font.Size());
|
|
|
|
|
ImageCache::Get()->AddImage(label, icon);
|
|
|
|
|
}
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
|
AccountsMenu::_EnsureAsteriskIcon()
|
|
|
|
|
{
|
|
|
|
|
BFont font;
|
|
|
|
|
BBitmap* icon = ImageCache::Get()->GetImage("kAsteriskScaled");
|
|
|
|
|
|
|
|
|
|
if (icon == NULL) {
|
|
|
|
|
BBitmap* unscaled = ImageCache::Get()->GetImage("kAsteriskIcon");
|
|
|
|
|
icon = RescaleBitmap(unscaled, font.Size(), font.Size());
|
|
|
|
|
ImageCache::Get()->AddImage("kAsteriskScaled", icon);
|
|
|
|
|
}
|
|
|
|
|
return icon;
|
|
|
|
|
}
|