User.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2009-2011, Andrea Anzani. All rights reserved.
  3. * Copyright 2012, Dario Casalinuovo. All rights reserved.
  4. * Copyright 2021, Jaidyn Levesque. All rights reserved.
  5. * Distributed under the terms of the MIT License.
  6. *
  7. * Authors:
  8. * Andrea Anzani, andrea.anzani@gmail.com
  9. * Dario Casalinuovo
  10. */
  11. #include "User.h"
  12. #include <Bitmap.h>
  13. #include <BitmapStream.h>
  14. #include <TranslationUtils.h>
  15. #include <TranslatorRoster.h>
  16. #include "ChatProtocolAddOn.h"
  17. #include "AppResources.h"
  18. #include "Conversation.h"
  19. #include "ImageCache.h"
  20. #include "NotifyMessage.h"
  21. #include "ProtocolLooper.h"
  22. #include "ProtocolManager.h"
  23. #include "UserPopUp.h"
  24. #include "Utils.h"
  25. User::User(BString id, BMessenger msgn)
  26. :
  27. fID(id),
  28. fName(id),
  29. fMessenger(msgn),
  30. fLooper(NULL),
  31. fItemColor(ForegroundColor(ui_color(B_LIST_BACKGROUND_COLOR))),
  32. fStatus(STATUS_ONLINE),
  33. fAvatarBitmap(NULL),
  34. fPopUp(NULL)
  35. {
  36. }
  37. void
  38. User::RegisterObserver(Conversation* chat)
  39. {
  40. Notifier::RegisterObserver(chat);
  41. fConversations.AddItem(chat->GetId(), chat);
  42. }
  43. void
  44. User::UnregisterObserver(Conversation* chat)
  45. {
  46. Notifier::UnregisterObserver(chat);
  47. fConversations.RemoveItemFor(chat->GetId());
  48. }
  49. void
  50. User::ShowPopUp(BPoint where)
  51. {
  52. if (fPopUp == NULL) {
  53. fPopUp = new UserPopUp(this);
  54. RegisterObserver(fPopUp);
  55. }
  56. fPopUp->Show();
  57. fPopUp->MoveTo(where);
  58. }
  59. void
  60. User::HidePopUp()
  61. {
  62. if ((fPopUp != NULL) && !fPopUp->IsHidden())
  63. fPopUp->Hide();
  64. }
  65. void
  66. User::DeletePopUp()
  67. {
  68. if (fPopUp == NULL)
  69. return;
  70. if (fPopUp->Lock()) {
  71. UnregisterObserver(fPopUp);
  72. fPopUp->Quit();
  73. fPopUp = NULL;
  74. }
  75. }
  76. BString
  77. User::GetId() const
  78. {
  79. return fID;
  80. }
  81. BMessenger
  82. User::Messenger() const
  83. {
  84. return fMessenger;
  85. }
  86. void
  87. User::SetMessenger(BMessenger messenger)
  88. {
  89. fMessenger = messenger;
  90. }
  91. ProtocolLooper*
  92. User::GetProtocolLooper() const
  93. {
  94. return fLooper;
  95. }
  96. BString
  97. User::GetName() const
  98. {
  99. return fName;
  100. }
  101. BBitmap*
  102. User::AvatarBitmap() const
  103. {
  104. if (fAvatarBitmap == NULL)
  105. return ImageCache::Get()->GetImage("kPersonIcon");
  106. return fAvatarBitmap;
  107. }
  108. BBitmap*
  109. User::ProtocolBitmap() const
  110. {
  111. ChatProtocol* protocol = fLooper->Protocol();
  112. ChatProtocolAddOn* addOn
  113. = ProtocolManager::Get()->ProtocolAddOn(protocol->Signature());
  114. return addOn->ProtoIcon();
  115. }
  116. UserStatus
  117. User::GetNotifyStatus() const
  118. {
  119. return fStatus;
  120. }
  121. BString
  122. User::GetNotifyPersonalStatus() const
  123. {
  124. return fPersonalStatus;
  125. }
  126. void
  127. User::SetProtocolLooper(ProtocolLooper* looper)
  128. {
  129. if (looper != NULL) {
  130. fLooper = looper;
  131. BBitmap* avatar = _GetCachedAvatar();
  132. if (avatar != NULL && avatar->IsValid()) {
  133. fAvatarBitmap = avatar;
  134. NotifyPointer(PTR_AVATAR_BITMAP, (void*)avatar);
  135. }
  136. }
  137. }
  138. void
  139. User::SetNotifyName(BString name)
  140. {
  141. if (fName.Compare(name) != 0) {
  142. fName = name;
  143. NotifyString(STR_CONTACT_NAME, name);
  144. }
  145. }
  146. void
  147. User::SetNotifyAvatarBitmap(BBitmap* bitmap)
  148. {
  149. if ((fAvatarBitmap != bitmap) && (bitmap != NULL)) {
  150. fAvatarBitmap = bitmap;
  151. _SetCachedAvatar(bitmap);
  152. NotifyPointer(PTR_AVATAR_BITMAP, (void*)_GetCachedAvatar());
  153. }
  154. }
  155. void
  156. User::SetNotifyStatus(UserStatus status)
  157. {
  158. if (fStatus != status) {
  159. fStatus = status;
  160. NotifyInteger(INT_CONTACT_STATUS, (int32)fStatus);
  161. }
  162. }
  163. void
  164. User::SetNotifyPersonalStatus(BString personalStatus)
  165. {
  166. if (fPersonalStatus.Compare(personalStatus) != 0) {
  167. fPersonalStatus = personalStatus;
  168. NotifyString(STR_PERSONAL_STATUS, personalStatus);
  169. }
  170. }
  171. ChatMap
  172. User::Conversations()
  173. {
  174. return fConversations;
  175. }
  176. void
  177. User::_EnsureCachePath()
  178. {
  179. if (fCachePath.InitCheck() == B_OK)
  180. return;
  181. fCachePath.SetTo(UserCachePath(fLooper->Protocol()->GetName(),
  182. fID.String()));
  183. }
  184. BBitmap*
  185. User::_GetCachedAvatar()
  186. {
  187. _EnsureCachePath();
  188. // Try loading cached avatar
  189. BFile cacheFile(fCachePath.Path(), B_READ_ONLY);
  190. BBitmap* bitmap = BTranslationUtils::GetBitmap(&cacheFile);
  191. if (bitmap != NULL && bitmap->IsValid() == true)
  192. return bitmap;
  193. return NULL;
  194. }
  195. void
  196. User::_SetCachedAvatar(BBitmap* bitmap)
  197. {
  198. _EnsureCachePath();
  199. BFile cacheFile(fCachePath.Path(), B_WRITE_ONLY | B_CREATE_FILE);
  200. if (cacheFile.InitCheck() != B_OK)
  201. return;
  202. BBitmapStream stream(bitmap);
  203. BTranslatorRoster* roster = BTranslatorRoster::Default();
  204. int32 format_count;
  205. translator_info info;
  206. const translation_format* formats = NULL;
  207. roster->Identify(&stream, new BMessage(), &info, 0, "image");
  208. roster->GetOutputFormats(info.translator, &formats, &format_count);
  209. roster->Translate(info.translator, &stream, NULL, &cacheFile,
  210. formats[0].type);
  211. }