PageRenderTime 34ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llinspectgroup.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 322 lines | 192 code | 48 blank | 82 comment | 14 complexity | c69f73ab13146580ea0bf8fa2e224228 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llinspectgroup.cpp
  3. *
  4. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "llviewerprecompiledheaders.h"
  26. #include "llinspectgroup.h"
  27. // viewer files
  28. #include "llgroupactions.h"
  29. #include "llgroupmgr.h"
  30. #include "llinspect.h"
  31. #include "llstartup.h"
  32. // Linden libraries
  33. #include "llcontrol.h" // LLCachedControl
  34. #include "llfloater.h"
  35. #include "llfloaterreg.h"
  36. #include "llresmgr.h" // getMonetaryString()
  37. #include "lltooltip.h" // positionViewNearMouse()
  38. #include "lltrans.h"
  39. #include "lluictrl.h"
  40. class LLFetchGroupData;
  41. //////////////////////////////////////////////////////////////////////////////
  42. // LLInspectGroup
  43. //////////////////////////////////////////////////////////////////////////////
  44. /// Group Inspector, a small information window used when clicking
  45. /// on group names in the 2D UI
  46. class LLInspectGroup : public LLInspect
  47. {
  48. friend class LLFloaterReg;
  49. public:
  50. // key["group_id"] - Group ID for which to show information
  51. // Inspector will be positioned relative to current mouse position
  52. LLInspectGroup(const LLSD& key);
  53. virtual ~LLInspectGroup();
  54. // Because floater is single instance, need to re-parse data on each spawn
  55. // (for example, inspector about same group but in different position)
  56. /*virtual*/ void onOpen(const LLSD& group_id);
  57. // When closing they should close their gear menu
  58. /*virtual*/ void onClose(bool app_quitting);
  59. // Update view based on information from group manager
  60. void processGroupData();
  61. // Make network requests for all the data to display in this view.
  62. // Used on construction and if avatar id changes.
  63. void requestUpdate();
  64. // Callback for gCacheName to look up group name
  65. // Faster than waiting for group properties to return
  66. void nameUpdatedCallback(const LLUUID& id,
  67. const std::string& name,
  68. bool is_group);
  69. // Button/menu callbacks
  70. void onClickViewProfile();
  71. void onClickJoin();
  72. void onClickLeave();
  73. private:
  74. LLUUID mGroupID;
  75. // an in-flight network request for group properties
  76. // is represented by this object
  77. LLFetchGroupData* mPropertiesRequest;
  78. };
  79. //////////////////////////////////////////////////////////////////////////////
  80. // LLFetchGroupData
  81. //////////////////////////////////////////////////////////////////////////////
  82. // This object represents a pending request for avatar properties information
  83. class LLFetchGroupData : public LLGroupMgrObserver
  84. {
  85. public:
  86. // If the inspector closes it will delete the pending request object, so the
  87. // inspector pointer will be valid for the lifetime of this object
  88. LLFetchGroupData(const LLUUID& group_id, LLInspectGroup* inspector)
  89. : LLGroupMgrObserver(group_id),
  90. mInspector(inspector)
  91. {
  92. LLGroupMgr* mgr = LLGroupMgr::getInstance();
  93. // register ourselves as an observer
  94. mgr->addObserver(this);
  95. // send a request
  96. mgr->sendGroupPropertiesRequest(group_id);
  97. }
  98. ~LLFetchGroupData()
  99. {
  100. // remove ourselves as an observer
  101. LLGroupMgr::getInstance()->removeObserver(this);
  102. }
  103. void changed(LLGroupChange gc)
  104. {
  105. if (gc == GC_PROPERTIES)
  106. {
  107. mInspector->processGroupData();
  108. }
  109. }
  110. LLInspectGroup* mInspector;
  111. };
  112. LLInspectGroup::LLInspectGroup(const LLSD& sd)
  113. : LLInspect( LLSD() ), // single_instance, doesn't really need key
  114. mGroupID(), // set in onOpen()
  115. mPropertiesRequest(NULL)
  116. {
  117. mCommitCallbackRegistrar.add("InspectGroup.ViewProfile",
  118. boost::bind(&LLInspectGroup::onClickViewProfile, this));
  119. mCommitCallbackRegistrar.add("InspectGroup.Join",
  120. boost::bind(&LLInspectGroup::onClickJoin, this));
  121. mCommitCallbackRegistrar.add("InspectGroup.Leave",
  122. boost::bind(&LLInspectGroup::onClickLeave, this));
  123. // can't make the properties request until the widgets are constructed
  124. // as it might return immediately, so do it in postBuild.
  125. }
  126. LLInspectGroup::~LLInspectGroup()
  127. {
  128. // clean up any pending requests so they don't call back into a deleted
  129. // view
  130. delete mPropertiesRequest;
  131. mPropertiesRequest = NULL;
  132. }
  133. // Multiple calls to showInstance("inspect_avatar", foo) will provide different
  134. // LLSD for foo, which we will catch here.
  135. //virtual
  136. void LLInspectGroup::onOpen(const LLSD& data)
  137. {
  138. // start fade animation
  139. LLInspect::onOpen(data);
  140. mGroupID = data["group_id"];
  141. // Position the inspector relative to the mouse cursor
  142. // Similar to how tooltips are positioned
  143. // See LLToolTipMgr::createToolTip
  144. if (data.has("pos"))
  145. {
  146. LLUI::positionViewNearMouse(this, data["pos"]["x"].asInteger(), data["pos"]["y"].asInteger());
  147. }
  148. else
  149. {
  150. LLUI::positionViewNearMouse(this);
  151. }
  152. // can't call from constructor as widgets are not built yet
  153. requestUpdate();
  154. }
  155. // virtual
  156. void LLInspectGroup::onClose(bool app_quitting)
  157. {
  158. // *TODO: If we add a gear menu, close it here
  159. }
  160. void LLInspectGroup::requestUpdate()
  161. {
  162. // Don't make network requests when spawning from the debug menu at the
  163. // login screen (which is useful to work on the layout).
  164. if (mGroupID.isNull())
  165. {
  166. if (LLStartUp::getStartupState() >= STATE_STARTED)
  167. {
  168. // once we're running we don't want to show the test floater
  169. // for bogus LLUUID::null links
  170. closeFloater();
  171. }
  172. return;
  173. }
  174. // Clear out old data so it doesn't flash between old and new
  175. getChild<LLUICtrl>("group_name")->setValue("");
  176. getChild<LLUICtrl>("group_subtitle")->setValue("");
  177. getChild<LLUICtrl>("group_details")->setValue("");
  178. getChild<LLUICtrl>("group_cost")->setValue("");
  179. // Must have a visible button so the inspector can take focus
  180. getChild<LLUICtrl>("view_profile_btn")->setVisible(true);
  181. getChild<LLUICtrl>("leave_btn")->setVisible(false);
  182. getChild<LLUICtrl>("join_btn")->setVisible(false);
  183. // Make a new request for properties
  184. delete mPropertiesRequest;
  185. mPropertiesRequest = new LLFetchGroupData(mGroupID, this);
  186. // Name lookup will be faster out of cache, use that
  187. gCacheName->getGroup(mGroupID,
  188. boost::bind(&LLInspectGroup::nameUpdatedCallback,
  189. this, _1, _2, _3));
  190. }
  191. void LLInspectGroup::nameUpdatedCallback(
  192. const LLUUID& id,
  193. const std::string& name,
  194. bool is_group)
  195. {
  196. if (id == mGroupID)
  197. {
  198. getChild<LLUICtrl>("group_name")->setValue( LLSD(name) );
  199. }
  200. // Otherwise possibly a request for an older inspector, ignore it
  201. }
  202. void LLInspectGroup::processGroupData()
  203. {
  204. LLGroupMgrGroupData* data =
  205. LLGroupMgr::getInstance()->getGroupData(mGroupID);
  206. if (data)
  207. {
  208. // Noun pluralization depends on language
  209. std::string lang = LLUI::getLanguage();
  210. std::string members =
  211. LLTrans::getCountString(lang, "GroupMembers", data->mMemberCount);
  212. getChild<LLUICtrl>("group_subtitle")->setValue( LLSD(members) );
  213. getChild<LLUICtrl>("group_details")->setValue( LLSD(data->mCharter) );
  214. getChild<LLUICtrl>("group_icon")->setValue( LLSD(data->mInsigniaID) );
  215. std::string cost;
  216. bool is_member = LLGroupActions::isInGroup(mGroupID);
  217. if (is_member)
  218. {
  219. cost = getString("YouAreMember");
  220. }
  221. else if (data->mOpenEnrollment)
  222. {
  223. if (data->mMembershipFee == 0)
  224. {
  225. cost = getString("FreeToJoin");
  226. }
  227. else
  228. {
  229. std::string amount =
  230. LLResMgr::getInstance()->getMonetaryString(
  231. data->mMembershipFee);
  232. LLStringUtil::format_map_t args;
  233. args["[AMOUNT]"] = amount;
  234. cost = getString("CostToJoin", args);
  235. }
  236. }
  237. else
  238. {
  239. cost = getString("PrivateGroup");
  240. }
  241. getChild<LLUICtrl>("group_cost")->setValue(cost);
  242. getChild<LLUICtrl>("join_btn")->setVisible(!is_member);
  243. getChild<LLUICtrl>("leave_btn")->setVisible(is_member);
  244. // Only enable join button if you are allowed to join
  245. bool can_join = !is_member && data->mOpenEnrollment;
  246. getChild<LLUICtrl>("join_btn")->setEnabled(can_join);
  247. }
  248. // Delete the request object as it has been satisfied
  249. delete mPropertiesRequest;
  250. mPropertiesRequest = NULL;
  251. }
  252. void LLInspectGroup::onClickViewProfile()
  253. {
  254. closeFloater();
  255. LLGroupActions::show(mGroupID);
  256. }
  257. void LLInspectGroup::onClickJoin()
  258. {
  259. closeFloater();
  260. LLGroupActions::join(mGroupID);
  261. }
  262. void LLInspectGroup::onClickLeave()
  263. {
  264. closeFloater();
  265. LLGroupActions::leave(mGroupID);
  266. }
  267. //////////////////////////////////////////////////////////////////////////////
  268. // LLInspectGroupUtil
  269. //////////////////////////////////////////////////////////////////////////////
  270. void LLInspectGroupUtil::registerFloater()
  271. {
  272. LLFloaterReg::add("inspect_group", "inspect_group.xml",
  273. &LLFloaterReg::build<LLInspectGroup>);
  274. }