2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2011-12-03 16:38:03 -06:00
|
|
|
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AccountManager.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "ChatProtocolMessages.h"
|
2010-05-16 16:02:50 -05:00
|
|
|
#include "MainWindow.h"
|
2010-05-07 04:47:10 -05:00
|
|
|
#include "NotifyMessage.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "TheApp.h"
|
|
|
|
|
2012-02-27 18:46:15 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
static AccountManager* fInstance = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
AccountManager::AccountManager()
|
2010-05-19 17:28:26 -05:00
|
|
|
:
|
2021-06-20 12:44:20 -05:00
|
|
|
fStatus(STATUS_OFFLINE),
|
2012-02-27 18:46:15 -06:00
|
|
|
fReplicantMessenger(NULL)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AccountManager::~AccountManager()
|
|
|
|
{
|
2012-02-27 18:46:15 -06:00
|
|
|
delete fReplicantMessenger;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AccountManager*
|
|
|
|
AccountManager::Get()
|
|
|
|
{
|
|
|
|
if (fInstance == NULL)
|
|
|
|
fInstance = new AccountManager();
|
|
|
|
return fInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-08-03 13:19:25 -05:00
|
|
|
AccountManager::SetNickname(BString nick, int64 instance)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
// Create message
|
|
|
|
BMessage* msg = new BMessage(IM_MESSAGE);
|
2010-05-19 15:37:26 -05:00
|
|
|
msg->AddInt32("im_what", IM_SET_OWN_NICKNAME);
|
2021-06-07 11:45:30 -05:00
|
|
|
msg->AddString("user_name", nick);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
// Send message
|
|
|
|
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
|
|
|
|
MainWindow* win = theApp->GetMainWindow();
|
2021-08-03 13:19:25 -05:00
|
|
|
if (instance > -1) {
|
|
|
|
msg->AddInt64("instance", instance);
|
|
|
|
win->GetServer()->SendProtocolMessage(msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
win->GetServer()->SendAllProtocolMessage(msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-27 18:46:15 -06:00
|
|
|
void
|
|
|
|
AccountManager::SetReplicantMessenger(BMessenger* messenger)
|
|
|
|
{
|
|
|
|
fReplicantMessenger = messenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
UserStatus
|
2010-05-07 04:47:10 -05:00
|
|
|
AccountManager::Status() const
|
|
|
|
{
|
|
|
|
return fStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-08-03 13:19:25 -05:00
|
|
|
AccountManager::SetStatus(UserStatus status, const char* str, int64 instance)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
2021-08-03 13:19:25 -05:00
|
|
|
if (fStatus == status && instance == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create status change message
|
|
|
|
BMessage* msg = new BMessage(IM_MESSAGE);
|
|
|
|
msg->AddInt32("im_what", IM_SET_OWN_STATUS);
|
|
|
|
msg->AddInt32("status", (int32)status);
|
|
|
|
if (str != NULL)
|
|
|
|
msg->AddString("message", str);
|
|
|
|
|
|
|
|
// Send message
|
|
|
|
TheApp* theApp = reinterpret_cast<TheApp*>(be_app);
|
|
|
|
MainWindow* win = theApp->GetMainWindow();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2021-08-03 13:19:25 -05:00
|
|
|
if (instance > -1) {
|
|
|
|
msg->AddInt64("instance", instance);
|
|
|
|
win->GetServer()->SendProtocolMessage(msg);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
2021-08-03 13:19:25 -05:00
|
|
|
else
|
|
|
|
win->GetServer()->SendAllProtocolMessage(msg);
|
|
|
|
|
|
|
|
// Notify status change
|
|
|
|
fStatus = status;
|
|
|
|
NotifyInteger(INT_ACCOUNT_STATUS, (int32)fStatus);
|
|
|
|
ReplicantStatusNotify((UserStatus)status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
AccountManager::SetStatus(UserStatus status, int64 instance)
|
|
|
|
{
|
|
|
|
SetStatus(status, NULL, instance);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
2012-02-27 18:46:15 -06:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-20 12:44:20 -05:00
|
|
|
AccountManager::ReplicantStatusNotify(UserStatus status, bool wait)
|
2012-02-27 18:46:15 -06:00
|
|
|
{
|
|
|
|
if(fReplicantMessenger != NULL && fReplicantMessenger->IsValid()) {
|
|
|
|
printf("notification sent\n");
|
|
|
|
BMessage mess(IM_OWN_STATUS_SET);
|
|
|
|
mess.AddInt32("status", status);
|
|
|
|
fReplicantMessenger->SendMessage(&mess);
|
2012-03-03 20:27:16 -06:00
|
|
|
}
|
2012-02-27 18:46:15 -06:00
|
|
|
}
|