llavatartracker.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @file llavatartracker.h
  3. * @brief Definition of the LLAvatarTracker and associated classes
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLAVATARTRACKER_H
  33. #define LL_LLAVATARTRACKER_H
  34. #include <map>
  35. #include <set>
  36. #include <string>
  37. #include <vector>
  38. #include "hbfastmap.h"
  39. #include "lluserrelations.h"
  40. #include "llvector3d.h"
  41. #define TRACK_POWER 0 // Not implemented
  42. class LLAvatarName;
  43. class LLMessageSystem;
  44. class LLTrackingData;
  45. class LLFriendObserver
  46. {
  47. public:
  48. // This enumeration is a way to refer to what changed in a more human
  49. // readable format. You can mask the value provided by changed() to see if
  50. // the observer is interested in the change.
  51. enum
  52. {
  53. NONE = 0,
  54. ADD = 1,
  55. REMOVE = 2,
  56. ONLINE = 4,
  57. POWERS = 8,
  58. ALL = 0xffffffff
  59. };
  60. virtual ~LLFriendObserver() = default;
  61. virtual void changed(U32 mask) = 0;
  62. LL_INLINE virtual void changedBuddies(const uuid_list_t& buddies)
  63. {
  64. }
  65. };
  66. #if TRACK_POWER
  67. struct LLBuddyInfo
  68. {
  69. LLBuddyInfo()
  70. : mIsOnline(false),
  71. mIsEmpowered(false)
  72. {
  73. }
  74. bool mIsOnline;
  75. bool mIsEmpowered;
  76. };
  77. #endif
  78. // This is used as a base class for doing operations on all buddies.
  79. class LLRelationshipFunctor
  80. {
  81. public:
  82. virtual ~LLRelationshipFunctor() = default;
  83. virtual bool operator()(const LLUUID& buddy_id, LLRelationship* buddy) = 0;
  84. };
  85. class LLAvatarTracker
  86. {
  87. protected:
  88. LOG_CLASS(LLAvatarTracker);
  89. private:
  90. // do not implement
  91. LLAvatarTracker(const LLAvatarTracker&);
  92. bool operator==(const LLAvatarTracker&);
  93. public:
  94. // Do not you dare create or delete this object
  95. LLAvatarTracker();
  96. ~LLAvatarTracker();
  97. void track(const LLUUID& avatar_id, const std::string& name);
  98. void untrack(const LLUUID& avatar_id);
  99. LL_INLINE bool isTrackedAgentValid() { return mTrackedAgentValid; }
  100. LL_INLINE void setTrackedAgentValid(bool b) { mTrackedAgentValid = b; }
  101. void findAgent();
  102. // Coarse update information
  103. void setTrackedCoarseLocation(const LLVector3d& global_pos);
  104. // Dealing with the tracked agent location
  105. bool haveTrackingInfo();
  106. void getDegreesAndDist(F32& rot, F64& horiz_dist, F64& vert_dist);
  107. LLVector3d getGlobalPos();
  108. // Get the name passed in, returns null string if uninitialized.
  109. const std::string& getName();
  110. // Get the avatar being tracked, returns LLUUID::null if uninitialized
  111. const LLUUID& getAvatarID();
  112. // Add or remove agents from buddy list. Each method takes a set of buddies
  113. // and returns how many were actually added or removed.
  114. typedef fast_hmap<LLUUID, LLRelationship*> buddy_map_t;
  115. S32 addBuddyList(const buddy_map_t& buddies);
  116. //S32 removeBuddyList(const buddy_list_t& exes);
  117. void copyBuddyList(buddy_map_t& buddies) const;
  118. // Deal with termination of friendhsip
  119. void terminateBuddy(const LLUUID& id);
  120. // Flag the buddy list dirty to force an update
  121. void dirtyBuddies();
  122. // Get full info
  123. const LLRelationship* getBuddyInfo(const LLUUID& id) const;
  124. // Online status
  125. void setBuddyOnline(const LLUUID& id, bool is_online);
  126. bool isBuddyOnline(const LLUUID& id) const;
  127. LL_INLINE bool isBuddy(const LLUUID& id) const
  128. {
  129. return mBuddyInfo.count(id) > 0;
  130. }
  131. #if TRACK_POWER
  132. // Simple empowered status
  133. void setBuddyEmpowered(const LLUUID& id, bool is_empowered);
  134. bool isBuddyEmpowered(const LLUUID& id) const;
  135. // Set the empower bit & message the server.
  136. void empowerList(const buddy_map_t& list, bool grant);
  137. void empower(const LLUUID& id, bool grant); // wrapper for above
  138. #endif
  139. // Register callbacks
  140. void registerCallbacks(LLMessageSystem* msg);
  141. // Add/remove an observer. If the observer is destroyed, be sure to remove
  142. // it. On destruction of the tracker, it will delete any observers left
  143. // behind.
  144. void addObserver(LLFriendObserver* observer);
  145. void removeObserver(LLFriendObserver* observer);
  146. void idleNotifyObservers();
  147. void notifyObservers();
  148. // Apply the functor to every buddy. Do not actually modify the buddy list
  149. // in the functor or bad things will happen.
  150. void applyFunctor(LLRelationshipFunctor& f);
  151. // Stores flag for change and optionnally the Id of the buddy the change
  152. // applies to.
  153. void addChangedMask(U32 mask, const LLUUID& buddy_id = LLUUID::null);
  154. static void formFriendship(const LLUUID& friend_id);
  155. static bool isAgentFriend(const LLUUID& agent_id);
  156. static bool isAgentMappable(const LLUUID& agent_id);
  157. protected:
  158. void deleteTrackingData();
  159. void agentFound(const LLUUID& prey,
  160. const LLVector3d& estimated_global_pos);
  161. // Message system functionality
  162. static void processAgentFound(LLMessageSystem* msg, void**);
  163. static void processOnlineNotification(LLMessageSystem* msg, void**);
  164. static void processOfflineNotification(LLMessageSystem* msg, void**);
  165. static void processTerminateFriendship(LLMessageSystem* msg, void**);
  166. static void processChangeUserRights(LLMessageSystem* msg, void**);
  167. static void callbackLoadAvatarName(const LLUUID& id, bool online,
  168. const LLAvatarName& avatar_name);
  169. void processNotify(LLMessageSystem* msg, bool online);
  170. void processChange(LLMessageSystem* msg);
  171. protected:
  172. LLTrackingData* mTrackingData;
  173. U32 mModifyMask;
  174. buddy_map_t mBuddyInfo;
  175. uuid_list_t mChangedBuddyIDs;
  176. typedef std::vector<LLFriendObserver*> observer_list_t;
  177. observer_list_t mObservers;
  178. bool mTrackedAgentValid;
  179. bool mIsNotifyObservers;
  180. };
  181. // Collect set of LLUUIDs we are a proxy for
  182. class LLCollectProxyBuddies final : public LLRelationshipFunctor
  183. {
  184. public:
  185. LL_INLINE LLCollectProxyBuddies() = default;
  186. bool operator()(const LLUUID& buddy_id, LLRelationship* buddy) override;
  187. public:
  188. typedef std::set<LLUUID> buddy_list_t;
  189. buddy_list_t mProxy;
  190. };
  191. // Collect dictionary sorted map of name -> agent_id for every mappable buddy
  192. class LLCollectMappableBuddies final : public LLRelationshipFunctor
  193. {
  194. public:
  195. LL_INLINE LLCollectMappableBuddies() = default;
  196. bool operator()(const LLUUID& buddy_id, LLRelationship* buddy) override;
  197. public:
  198. typedef std::map<std::string, LLUUID, LLDictionaryLess> buddy_map_t;
  199. buddy_map_t mMappable;
  200. std::string mFirst;
  201. std::string mLast;
  202. };
  203. // Collect dictionary sorted map of name -> agent_id for every online buddy
  204. class LLCollectOnlineBuddies final : public LLRelationshipFunctor
  205. {
  206. public:
  207. LL_INLINE LLCollectOnlineBuddies() = default;
  208. bool operator()(const LLUUID& buddy_id, LLRelationship* buddy) override;
  209. public:
  210. typedef std::map<std::string, LLUUID, LLDictionaryLess> buddy_map_t;
  211. buddy_map_t mOnline;
  212. std::string mFirst;
  213. std::string mLast;
  214. };
  215. // Collect dictionary sorted map of name -> agent_id for every buddy, one map
  216. // is offline and the other map is online.
  217. class LLCollectAllBuddies final : public LLRelationshipFunctor
  218. {
  219. public:
  220. LL_INLINE LLCollectAllBuddies() = default;
  221. bool operator()(const LLUUID& buddy_id, LLRelationship* buddy) override;
  222. public:
  223. typedef std::map<std::string, LLUUID, LLDictionaryLess> buddy_map_t;
  224. buddy_map_t mOnline;
  225. buddy_map_t mOffline;
  226. std::string mFirst;
  227. std::string mLast;
  228. };
  229. extern LLAvatarTracker gAvatarTracker;
  230. #endif // LL_LLAVATARTRACKER_H