2021-05-23 15:10:14 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
|
|
|
|
* Copyright 2012, Dario Casalinuovo. All rights reserved.
|
2021-06-13 17:34:30 -05:00
|
|
|
* Copyright 2021, Jaidyn Levesque. All rights reserved.
|
2021-05-23 15:10:14 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
* Dario Casalinuovo
|
|
|
|
*/
|
|
|
|
#include "User.h"
|
|
|
|
|
2021-06-13 17:34:30 -05:00
|
|
|
#include <Bitmap.h>
|
|
|
|
#include <BitmapStream.h>
|
|
|
|
#include <TranslationUtils.h>
|
|
|
|
#include <TranslatorRoster.h>
|
2021-05-23 15:10:14 -05:00
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "ChatProtocolAddOn.h"
|
|
|
|
#include "AppResources.h"
|
2021-05-24 01:47:21 -05:00
|
|
|
#include "Conversation.h"
|
2021-06-13 17:34:30 -05:00
|
|
|
#include "ImageCache.h"
|
2021-05-23 15:10:14 -05:00
|
|
|
#include "NotifyMessage.h"
|
|
|
|
#include "ProtocolLooper.h"
|
|
|
|
#include "ProtocolManager.h"
|
2021-05-31 10:50:43 -05:00
|
|
|
#include "UserItem.h"
|
2021-05-23 15:10:14 -05:00
|
|
|
#include "UserPopUp.h"
|
2021-06-20 12:44:20 -05:00
|
|
|
#include "Utils.h"
|
2021-05-23 15:10:14 -05:00
|
|
|
|
|
|
|
|
|
|
|
User::User(BString id, BMessenger msgn)
|
|
|
|
:
|
|
|
|
fID(id),
|
|
|
|
fName(id),
|
|
|
|
fMessenger(msgn),
|
|
|
|
fLooper(NULL),
|
2021-05-31 10:50:43 -05:00
|
|
|
fListItem(NULL),
|
2021-06-20 12:44:20 -05:00
|
|
|
fItemColor(ForegroundColor(ui_color(B_LIST_BACKGROUND_COLOR))),
|
2021-06-29 11:40:58 -05:00
|
|
|
fStatus(STATUS_ONLINE),
|
2021-06-13 17:34:30 -05:00
|
|
|
fAvatarBitmap(NULL),
|
2021-05-23 15:10:14 -05:00
|
|
|
fPopUp(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
void
|
|
|
|
User::RegisterObserver(Conversation* chat)
|
|
|
|
{
|
|
|
|
Notifier::RegisterObserver(chat);
|
|
|
|
fConversations.AddItem(chat->GetId(), chat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::UnregisterObserver(Conversation* chat)
|
|
|
|
{
|
|
|
|
Notifier::UnregisterObserver(chat);
|
|
|
|
fConversations.RemoveItemFor(chat->GetId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 15:10:14 -05:00
|
|
|
void
|
|
|
|
User::ShowPopUp(BPoint where)
|
|
|
|
{
|
|
|
|
if (fPopUp == NULL) {
|
|
|
|
fPopUp = new UserPopUp(this);
|
|
|
|
RegisterObserver(fPopUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fPopUp->Show();
|
|
|
|
fPopUp->MoveTo(where);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::HidePopUp()
|
|
|
|
{
|
|
|
|
if ((fPopUp != NULL) && !fPopUp->IsHidden())
|
|
|
|
fPopUp->Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::DeletePopUp()
|
|
|
|
{
|
|
|
|
if (fPopUp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fPopUp->Lock()) {
|
|
|
|
UnregisterObserver(fPopUp);
|
|
|
|
fPopUp->Quit();
|
|
|
|
fPopUp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
|
|
|
User::GetId() const
|
|
|
|
{
|
|
|
|
return fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BMessenger
|
|
|
|
User::Messenger() const
|
|
|
|
{
|
|
|
|
return fMessenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::SetMessenger(BMessenger messenger)
|
|
|
|
{
|
|
|
|
fMessenger = messenger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProtocolLooper*
|
|
|
|
User::GetProtocolLooper() const
|
|
|
|
{
|
|
|
|
return fLooper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
|
|
|
User::GetName() const
|
|
|
|
{
|
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
User::AvatarBitmap() const
|
|
|
|
{
|
2021-06-13 17:34:30 -05:00
|
|
|
if (fAvatarBitmap == NULL)
|
|
|
|
return ImageCache::Get()->GetImage("kPersonIcon");
|
2021-05-23 15:10:14 -05:00
|
|
|
return fAvatarBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
User::ProtocolBitmap() const
|
|
|
|
{
|
2021-06-20 12:44:20 -05:00
|
|
|
ChatProtocol* protocol = fLooper->Protocol();
|
|
|
|
ChatProtocolAddOn* addOn
|
2021-05-23 15:10:14 -05:00
|
|
|
= ProtocolManager::Get()->ProtocolAddOn(protocol->Signature());
|
|
|
|
|
|
|
|
return addOn->ProtoIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-31 10:50:43 -05:00
|
|
|
UserItem*
|
|
|
|
User::GetListItem()
|
|
|
|
{
|
2021-06-02 16:53:03 -05:00
|
|
|
if (fListItem == NULL) {
|
2021-06-03 23:39:50 -05:00
|
|
|
fListItem = new UserItem(fName, this, (int32)fStatus);
|
2021-06-02 16:53:03 -05:00
|
|
|
RegisterObserver(fListItem);
|
|
|
|
}
|
2021-05-31 10:50:43 -05:00
|
|
|
return fListItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-20 12:44:20 -05:00
|
|
|
UserStatus
|
2021-05-23 15:10:14 -05:00
|
|
|
User::GetNotifyStatus() const
|
|
|
|
{
|
|
|
|
return fStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BString
|
|
|
|
User::GetNotifyPersonalStatus() const
|
|
|
|
{
|
|
|
|
return fPersonalStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::SetProtocolLooper(ProtocolLooper* looper)
|
|
|
|
{
|
2021-06-13 17:34:30 -05:00
|
|
|
if (looper != NULL) {
|
2021-05-23 15:10:14 -05:00
|
|
|
fLooper = looper;
|
2021-06-13 17:34:30 -05:00
|
|
|
BBitmap* avatar = _GetCachedAvatar();
|
|
|
|
if (avatar != NULL && avatar->IsValid()) {
|
|
|
|
fAvatarBitmap = avatar;
|
|
|
|
NotifyPointer(PTR_AVATAR_BITMAP, (void*)avatar);
|
|
|
|
}
|
2021-05-23 15:10:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::SetNotifyName(BString name)
|
|
|
|
{
|
|
|
|
if (fName.Compare(name) != 0) {
|
|
|
|
fName = name;
|
|
|
|
NotifyString(STR_CONTACT_NAME, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::SetNotifyAvatarBitmap(BBitmap* bitmap)
|
|
|
|
{
|
|
|
|
if ((fAvatarBitmap != bitmap) && (bitmap != NULL)) {
|
|
|
|
fAvatarBitmap = bitmap;
|
2021-06-13 17:34:30 -05:00
|
|
|
_SetCachedAvatar(bitmap);
|
2021-05-23 15:10:14 -05:00
|
|
|
NotifyPointer(PTR_AVATAR_BITMAP, (void*)bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-06-20 12:44:20 -05:00
|
|
|
User::SetNotifyStatus(UserStatus status)
|
2021-05-23 15:10:14 -05:00
|
|
|
{
|
|
|
|
if (fStatus != status) {
|
|
|
|
fStatus = status;
|
|
|
|
NotifyInteger(INT_CONTACT_STATUS, (int32)fStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::SetNotifyPersonalStatus(BString personalStatus)
|
|
|
|
{
|
|
|
|
if (fPersonalStatus.Compare(personalStatus) != 0) {
|
|
|
|
fPersonalStatus = personalStatus;
|
|
|
|
NotifyString(STR_PERSONAL_STATUS, personalStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 01:47:21 -05:00
|
|
|
ChatMap
|
|
|
|
User::Conversations()
|
|
|
|
{
|
|
|
|
return fConversations;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-13 17:34:30 -05:00
|
|
|
void
|
|
|
|
User::_EnsureCachePath()
|
|
|
|
{
|
|
|
|
if (fCachePath.InitCheck() == B_OK)
|
|
|
|
return;
|
2021-06-20 12:44:20 -05:00
|
|
|
fCachePath.SetTo(UserCachePath(fLooper->Protocol()->GetName(),
|
2021-06-13 17:34:30 -05:00
|
|
|
fID.String()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
User::_GetCachedAvatar()
|
|
|
|
{
|
|
|
|
_EnsureCachePath();
|
|
|
|
|
|
|
|
// Try loading cached avatar
|
|
|
|
BFile cacheFile(fCachePath.Path(), B_READ_ONLY);
|
|
|
|
BBitmap* bitmap = BTranslationUtils::GetBitmap(&cacheFile);
|
|
|
|
if (bitmap != NULL && bitmap->IsValid() == true)
|
|
|
|
return bitmap;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
User::_SetCachedAvatar(BBitmap* bitmap)
|
|
|
|
{
|
|
|
|
_EnsureCachePath();
|
|
|
|
BFile cacheFile(fCachePath.Path(), B_WRITE_ONLY | B_CREATE_FILE);
|
2021-08-01 06:37:25 -05:00
|
|
|
if (cacheFile.InitCheck() != B_OK)
|
|
|
|
return;
|
2021-06-13 17:34:30 -05:00
|
|
|
|
2021-08-01 06:37:25 -05:00
|
|
|
BBitmapStream stream(bitmap);
|
2021-06-13 17:34:30 -05:00
|
|
|
BTranslatorRoster* roster = BTranslatorRoster::Default();
|
|
|
|
|
|
|
|
int32 format_count;
|
|
|
|
translator_info info;
|
|
|
|
const translation_format* formats = NULL;
|
2021-08-01 06:37:25 -05:00
|
|
|
roster->Identify(&stream, new BMessage(), &info, 0, "image");
|
2021-06-13 17:34:30 -05:00
|
|
|
roster->GetOutputFormats(info.translator, &formats, &format_count);
|
|
|
|
|
2021-08-01 06:37:25 -05:00
|
|
|
roster->Translate(info.translator, &stream, NULL, &cacheFile,
|
2021-06-13 17:34:30 -05:00
|
|
|
formats[0].type);
|
|
|
|
}
|