2011-12-14 17:36:27 -06:00
|
|
|
/*
|
2012-09-25 16:45:29 -05:00
|
|
|
* Copyright 2011-2012, Dario Casalinuovo. All rights reserved.
|
2011-12-14 17:36:27 -06:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Dario Casalinuovo
|
|
|
|
*/
|
|
|
|
|
2021-05-19 16:12:19 -05:00
|
|
|
#include "ReplicantStatusView.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
#include <Application.h>
|
|
|
|
#include <AppFileInfo.h>
|
|
|
|
#include <Bitmap.h>
|
2021-07-19 09:54:27 -05:00
|
|
|
#include <Catalog.h>
|
2012-03-03 20:27:16 -06:00
|
|
|
#include <Deskbar.h>
|
2011-12-14 17:36:27 -06:00
|
|
|
#include <IconUtils.h>
|
|
|
|
#include <Message.h>
|
|
|
|
#include <MenuField.h>
|
|
|
|
#include <MenuItem.h>
|
|
|
|
#include <PopUpMenu.h>
|
|
|
|
#include <Roster.h>
|
|
|
|
#include <Window.h>
|
|
|
|
|
|
|
|
#include <libinterface/BitmapMenuItem.h>
|
|
|
|
#include <libinterface/BitmapUtils.h>
|
2021-05-19 16:12:19 -05:00
|
|
|
#include <libinterface/BitmapView.h>
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
#include "AccountManager.h"
|
2021-06-22 01:06:00 -05:00
|
|
|
#include "Cardie.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "AppMessages.h"
|
|
|
|
#include "AppPreferences.h"
|
|
|
|
#include "ChatProtocolMessages.h"
|
2011-12-14 17:36:27 -06:00
|
|
|
#include "NicknameTextControl.h"
|
|
|
|
#include "ReplicantMenuItem.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "Utils.h"
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "ReplicantStatusView"
|
|
|
|
|
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
extern "C" _EXPORT BView *instantiate_deskbar_item(void);
|
|
|
|
|
|
|
|
|
|
|
|
// The following handler is added to the Deskbar's looper
|
|
|
|
// to receive notifications from Caya
|
|
|
|
class ReplicantHandler : public BHandler {
|
|
|
|
public:
|
|
|
|
ReplicantHandler(const char* name, ReplicantStatusView* target)
|
2012-09-25 16:45:29 -05:00
|
|
|
:
|
|
|
|
BHandler(name)
|
2011-12-14 17:36:27 -06:00
|
|
|
{
|
|
|
|
fTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ReplicantHandler() {}
|
|
|
|
|
|
|
|
virtual void MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch (message->what) {
|
|
|
|
case IM_OWN_STATUS_SET:
|
|
|
|
{
|
|
|
|
int32 status;
|
|
|
|
|
|
|
|
if (message->FindInt32("status", &status) != B_OK)
|
|
|
|
return;
|
2021-06-20 12:44:20 -05:00
|
|
|
fTarget->SetStatus((UserStatus)status);
|
2011-12-14 17:36:27 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
BHandler::MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ReplicantStatusView* fTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ReplicantStatusView::ReplicantStatusView()
|
|
|
|
:
|
|
|
|
BView(BRect(0, 0, 15, 15), "ReplicantStatusView",
|
|
|
|
B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW)
|
|
|
|
{
|
|
|
|
_Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReplicantStatusView::ReplicantStatusView(BMessage* archive)
|
|
|
|
:
|
|
|
|
BView(archive)
|
|
|
|
{
|
|
|
|
_Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReplicantStatusView::~ReplicantStatusView()
|
|
|
|
{
|
|
|
|
delete fCayaMsg;
|
|
|
|
delete fReplicantHandler;
|
2012-05-15 12:31:31 -05:00
|
|
|
delete fReplicantMenu;
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
// TODO: Use a list for that
|
|
|
|
// maybe our List wrapper to std::list
|
|
|
|
delete fConnectingIcon;
|
2021-06-20 12:44:20 -05:00
|
|
|
delete fIcon;
|
2011-12-14 17:36:27 -06:00
|
|
|
delete fOfflineIcon;
|
|
|
|
delete fBusyIcon;
|
|
|
|
delete fAwayIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
2021-06-20 12:44:20 -05:00
|
|
|
case APP_REPLICANT_STATUS_SET:
|
2011-12-14 17:36:27 -06:00
|
|
|
{
|
|
|
|
int32 status;
|
|
|
|
|
|
|
|
if (msg->FindInt32("status", &status) != B_OK)
|
|
|
|
return;
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
SetStatus((UserStatus)status);
|
2011-12-14 17:36:27 -06:00
|
|
|
fCayaMsg->SendMessage(msg);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-20 12:44:20 -05:00
|
|
|
case APP_REPLICANT_EXIT:
|
|
|
|
case APP_SHOW_SETTINGS:
|
|
|
|
case APP_REPLICANT_SHOW_WINDOW:
|
|
|
|
case APP_REPLICANT_MESSENGER:
|
2011-12-14 17:36:27 -06:00
|
|
|
fCayaMsg->SendMessage(msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BView::MessageReceived(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-20 12:44:20 -05:00
|
|
|
ReplicantStatusView::SetStatus(UserStatus status)
|
2011-12-14 17:36:27 -06:00
|
|
|
{
|
|
|
|
for (int32 i = 0; i < fReplicantMenu->CountItems(); i++) {
|
|
|
|
ReplicantMenuItem* item
|
|
|
|
= dynamic_cast<ReplicantMenuItem*>(fReplicantMenu->ItemAt(i));
|
|
|
|
if (item == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (item->IsMarked())
|
|
|
|
item->SetMarked(false);
|
|
|
|
|
|
|
|
if (item && item->Status() == status && !item->IsCustom())
|
|
|
|
item->SetMarked(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
2021-06-20 12:44:20 -05:00
|
|
|
case STATUS_AWAY:
|
2011-12-14 17:36:27 -06:00
|
|
|
fIcon = fAwayIcon;
|
|
|
|
break;
|
2021-06-20 12:44:20 -05:00
|
|
|
case STATUS_DO_NOT_DISTURB:
|
2011-12-14 17:36:27 -06:00
|
|
|
fIcon = fBusyIcon;
|
|
|
|
break;
|
2021-06-20 12:44:20 -05:00
|
|
|
case STATUS_CUSTOM_STATUS:
|
|
|
|
fIcon = fAppIcon;
|
2012-05-15 12:20:11 -05:00
|
|
|
break;
|
2021-06-20 12:44:20 -05:00
|
|
|
case STATUS_INVISIBLE:
|
|
|
|
case STATUS_OFFLINE:
|
2011-12-14 17:36:27 -06:00
|
|
|
fIcon = fOfflineIcon;
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-20 12:44:20 -05:00
|
|
|
fIcon = fIcon;
|
2011-12-14 17:36:27 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw our deskbar icon.
|
|
|
|
void
|
|
|
|
ReplicantStatusView::Draw(BRect rect)
|
|
|
|
{
|
|
|
|
SetDrawingMode(B_OP_ALPHA);
|
|
|
|
DrawBitmap(fIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReplicantStatusView*
|
|
|
|
ReplicantStatusView::Instantiate(BMessage* archive)
|
|
|
|
{
|
|
|
|
if (!validate_instantiation(archive, "ReplicantStatusView"))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return new ReplicantStatusView(archive);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
ReplicantStatusView::Archive(BMessage* archive, bool deep) const
|
|
|
|
{
|
|
|
|
status_t status = BView::Archive(archive, deep);
|
|
|
|
|
|
|
|
if (status == B_OK)
|
2021-06-20 12:44:20 -05:00
|
|
|
status = archive->AddString("add_on", APP_SIGNATURE);
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
if (status == B_OK)
|
|
|
|
status = archive->AddString("class", "ReplicantStatusView");
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::AttachedToWindow()
|
|
|
|
{
|
|
|
|
BView::AttachedToWindow();
|
|
|
|
if (Parent())
|
|
|
|
SetViewColor(Parent()->ViewColor());
|
|
|
|
else
|
|
|
|
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
|
|
|
|
|
|
|
SetLowColor(ViewColor());
|
|
|
|
|
|
|
|
fReplicantHandler = new ReplicantHandler("CayaReplicantHandler", this);
|
|
|
|
if (Window()->Lock()) {
|
|
|
|
Window()->AddHandler(fReplicantHandler);
|
|
|
|
Window()->Unlock();
|
|
|
|
}
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
BMessage msg(APP_REPLICANT_MESSENGER);
|
2011-12-14 17:36:27 -06:00
|
|
|
BMessenger messenger(fReplicantHandler);
|
|
|
|
if (!messenger.IsValid())
|
|
|
|
return;
|
|
|
|
msg.AddMessenger("messenger", messenger);
|
|
|
|
fCayaMsg->SendMessage(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::DetachedFromWindow()
|
|
|
|
{
|
|
|
|
if (Window()->Lock()) {
|
|
|
|
Window()->RemoveHandler(fReplicantHandler);
|
|
|
|
Window()->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::MouseDown(BPoint point)
|
|
|
|
{
|
|
|
|
|
2017-01-30 00:19:59 -06:00
|
|
|
uint32 buttons;
|
2011-12-14 17:36:27 -06:00
|
|
|
if (LockLooper()) {
|
|
|
|
GetMouse(&point, &buttons, false);
|
|
|
|
UnlockLooper();
|
|
|
|
}
|
|
|
|
if (buttons & B_PRIMARY_MOUSE_BUTTON) {
|
|
|
|
// Show / Hide Window command
|
2021-06-20 12:44:20 -05:00
|
|
|
BMessage msg(APP_REPLICANT_SHOW_WINDOW);
|
2011-12-14 17:36:27 -06:00
|
|
|
fCayaMsg->SendMessage(&msg);
|
|
|
|
} else if(buttons & B_SECONDARY_MOUSE_BUTTON) {
|
|
|
|
// Build replicant menu
|
|
|
|
_ShowMenu(point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::_Init()
|
|
|
|
{
|
|
|
|
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
|
|
|
|
|
|
|
// Creating the Send messenger and sending
|
|
|
|
// a messenger targeting this to Caya.
|
|
|
|
// This will allow the Replicant to communicate
|
|
|
|
// whith Caya.
|
2021-06-20 12:44:20 -05:00
|
|
|
fCayaMsg = new BMessenger(APP_SIGNATURE);
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
fResources = ChatResources();
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
//Get icons from resources
|
2012-05-15 11:48:53 -05:00
|
|
|
fConnectingIcon = _GetIcon(kOnlineReplicant);
|
2021-06-20 12:44:20 -05:00
|
|
|
fAppIcon = _GetIcon(kIconReplicant);
|
2011-12-14 17:36:27 -06:00
|
|
|
fOfflineIcon = _GetIcon(kOfflineReplicant);
|
|
|
|
fIcon = fOfflineIcon;
|
|
|
|
fBusyIcon = _GetIcon(kBusyReplicant);
|
|
|
|
fAwayIcon = _GetIcon(kAwayReplicant);
|
|
|
|
fExitMenuIcon = _GetIcon(kExitMenuReplicant);
|
2012-03-11 20:41:46 -05:00
|
|
|
fPreferencesIcon = _GetIcon(kToolIcon);
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
// Build the replicant menu
|
|
|
|
_BuildMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
ReplicantStatusView::_GetIcon(const uint32 id)
|
|
|
|
{
|
2021-08-01 06:37:25 -05:00
|
|
|
BBitmap* icon = IconFromResources(&fResources, id, B_MINI_ICON);
|
2011-12-14 17:36:27 -06:00
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::_BuildMenu()
|
|
|
|
{
|
|
|
|
// Status menu
|
2012-05-15 11:48:53 -05:00
|
|
|
|
2011-12-14 17:36:27 -06:00
|
|
|
fReplicantMenu = new BPopUpMenu(" - ", false, false);
|
|
|
|
// Add status menu items
|
2021-06-20 12:44:20 -05:00
|
|
|
int32 s = STATUS_ONLINE;
|
|
|
|
while (s >= STATUS_ONLINE && s < STATUS_STATUSES) {
|
|
|
|
BMessage* msg = new BMessage(APP_REPLICANT_STATUS_SET);
|
2011-12-14 17:36:27 -06:00
|
|
|
msg->AddInt32("status", s);
|
|
|
|
|
|
|
|
ReplicantMenuItem* item = new ReplicantMenuItem(
|
2021-06-20 12:44:20 -05:00
|
|
|
UserStatusToString((UserStatus)s), (UserStatus)s);
|
2011-12-14 17:36:27 -06:00
|
|
|
fReplicantMenu->AddItem(item);
|
|
|
|
|
|
|
|
// Mark offline status by default
|
2021-06-20 12:44:20 -05:00
|
|
|
if (s == STATUS_OFFLINE)
|
2011-12-14 17:36:27 -06:00
|
|
|
item->SetMarked(true);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fReplicantMenu->AddItem(new BSeparatorItem());
|
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
fReplicantMenu->AddItem(new BitmapMenuItem(B_TRANSLATE("Preferences "),
|
2021-06-20 12:44:20 -05:00
|
|
|
new BMessage(APP_SHOW_SETTINGS), fPreferencesIcon));
|
2011-12-14 17:36:27 -06:00
|
|
|
|
2021-07-19 09:54:27 -05:00
|
|
|
fReplicantMenu->AddItem(new BitmapMenuItem(B_TRANSLATE("Exit"),
|
2021-06-20 12:44:20 -05:00
|
|
|
new BMessage(APP_REPLICANT_EXIT), fExitMenuIcon));
|
2011-12-14 17:36:27 -06:00
|
|
|
|
|
|
|
fReplicantMenu->SetTargetForItems(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ReplicantStatusView::_ShowMenu(BPoint point)
|
|
|
|
{
|
|
|
|
fReplicantMenu->SetTargetForItems(this);
|
|
|
|
ConvertToScreen(&point);
|
|
|
|
fReplicantMenu->Go(point, true, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" _EXPORT BView *
|
|
|
|
instantiate_deskbar_item(void)
|
|
|
|
{
|
|
|
|
return new ReplicantStatusView();
|
|
|
|
}
|
2012-03-03 20:27:16 -06:00
|
|
|
|
|
|
|
|
|
|
|
// The following methods install
|
|
|
|
// and remove the Caya's replicant
|
|
|
|
// from Deskbar.
|
|
|
|
status_t
|
|
|
|
ReplicantStatusView::InstallReplicant()
|
|
|
|
{
|
2021-07-28 19:10:09 -05:00
|
|
|
if (AppPreferences::Get()->DisableReplicant == true)
|
2012-03-03 20:27:16 -06:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
BDeskbar deskbar;
|
|
|
|
if (deskbar.HasItem("ReplicantStatusView")) {
|
|
|
|
ReplicantStatusView::RemoveReplicant();
|
|
|
|
}
|
|
|
|
ReplicantStatusView* view = new ReplicantStatusView();
|
|
|
|
return deskbar.AddItem(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
ReplicantStatusView::RemoveReplicant()
|
|
|
|
{
|
|
|
|
BDeskbar deskbar;
|
|
|
|
return deskbar.RemoveItem("ReplicantStatusView");
|
|
|
|
}
|