hbfloaterbump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**
  2. * @file hbfloaterbump.cpp
  3. * @brief Floater listing bumps, pushes and hits, and allowing to take actions.
  4. * @author Henri Beauchamp
  5. *
  6. * $LicenseInfo:firstyear=2020&license=viewergpl$
  7. *
  8. * Copyright (c) 2020, Henri Beauchamp
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "llviewerprecompiledheaders.h"
  34. #include "hbfloaterbump.h"
  35. #include "llbutton.h"
  36. #include "llcachename.h"
  37. #include "llscrolllistctrl.h"
  38. #include "lluictrlfactory.h"
  39. #include "llagent.h"
  40. #include "llgridmanager.h"
  41. #include "llfloateravatarinfo.h"
  42. #include "llfloaterreporter.h"
  43. //MK
  44. #include "mkrlinterface.h"
  45. //mk
  46. #define COMMENT_PREFIX "\342\200\243 "
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // LLMeanCollisionData class (used to be in llmeancollisiondata.h, but now
  49. // only used privately by HBFloaterBump). I also modified and extended it with
  50. // new statistics (number of events, first time, max magnitude, automatic
  51. // time_t to time stamp string (with seconds and date) conversion). HB
  52. ///////////////////////////////////////////////////////////////////////////////
  53. class LLMeanCollisionData
  54. {
  55. public:
  56. LL_INLINE LLMeanCollisionData(const LLUUID& perpetrator_id, time_t time,
  57. EMeanCollisionType type, F32 mag)
  58. : mPerpetratorId(perpetrator_id),
  59. mFirstTime(time),
  60. mType(type),
  61. mMag(mag),
  62. mMaxMag(mag),
  63. mNumber(1)
  64. {
  65. setTime(time);
  66. mFirstTimeStr = mLastTimeStr;
  67. }
  68. void setTime(time_t time)
  69. {
  70. mLastTime = time;
  71. mLastTimeStr = LLGridManager::getTimeStamp(time, "%Y-%m-%d %H:%M:%S");
  72. }
  73. public:
  74. LLUUID mPerpetratorId;
  75. std::string mFullName;
  76. std::string mFirstTimeStr;
  77. std::string mLastTimeStr;
  78. time_t mFirstTime;
  79. time_t mLastTime;
  80. EMeanCollisionType mType;
  81. F32 mMag;
  82. F32 mMaxMag;
  83. U32 mNumber;
  84. };
  85. ///////////////////////////////////////////////////////////////////////////////
  86. // HBFloaterBump class proper
  87. ///////////////////////////////////////////////////////////////////////////////
  88. HBFloaterBump::collisions_list_t HBFloaterBump::sMeanCollisionsList;
  89. bool HBFloaterBump::sListUpdated = false;
  90. HBFloaterBump::HBFloaterBump(const LLSD&)
  91. {
  92. LLUICtrlFactory::getInstance()->buildFloater(this, "floater_bumps.xml");
  93. }
  94. //virtual
  95. bool HBFloaterBump::postBuild()
  96. {
  97. mBumpsList = getChild<LLScrollListCtrl>("bump_list");
  98. childSetAction("close_btn", onButtonClose, this);
  99. mClearButton = getChild<LLButton>("clear_btn");
  100. mClearButton->setClickedCallback(onButtonClear, this);
  101. mFocusButton = getChild<LLButton>("focus_btn");
  102. mFocusButton->setClickedCallback(onButtonFocus, this);
  103. mProfileButton = getChild<LLButton>("profile_btn");
  104. mProfileButton->setClickedCallback(onButtonProfile, this);
  105. mReportButton = getChild<LLButton>("report_btn");
  106. mReportButton->setClickedCallback(onButtonReport, this);
  107. sListUpdated = true; // Force a list refresh on first draw()
  108. return true;
  109. }
  110. //virtual
  111. void HBFloaterBump::refresh()
  112. {
  113. sListUpdated = false;
  114. mBumpsList->deleteAllItems();
  115. if (sMeanCollisionsList.empty())
  116. {
  117. static const std::string none = COMMENT_PREFIX +
  118. getString("none_detected");
  119. mBumpsList->addCommentText(none);
  120. return;
  121. }
  122. static const std::string bump_str = getString("bump");
  123. static const std::string llpushobject_str = getString("llpushobject");
  124. static const std::string selected_obj_str =
  125. getString("selected_object_collide");
  126. static const std::string scripted_obj_str =
  127. getString("scripted_object_collide");
  128. static const std::string physical_obj_str =
  129. getString("physical_object_collide");
  130. const std::string* type;
  131. for (collisions_list_t::iterator iter = sMeanCollisionsList.begin(),
  132. end = sMeanCollisionsList.end();
  133. iter != end; ++iter)
  134. {
  135. const LLMeanCollisionData& mcd = *iter;
  136. switch (mcd.mType)
  137. {
  138. case MEAN_BUMP:
  139. type = &bump_str;
  140. break;
  141. case MEAN_LLPUSHOBJECT:
  142. type = &llpushobject_str;
  143. break;
  144. case MEAN_SELECTED_OBJECT_COLLIDE:
  145. type = &selected_obj_str;
  146. break;
  147. case MEAN_SCRIPTED_OBJECT_COLLIDE:
  148. type = &scripted_obj_str;
  149. break;
  150. case MEAN_PHYSICAL_OBJECT_COLLIDE:
  151. type = &physical_obj_str;
  152. break;
  153. default:
  154. llwarns_once << "Unknown mean collision type: " << mcd.mType
  155. << llendl;
  156. continue;
  157. }
  158. LLSD element;
  159. LLSD& columns = element["columns"];
  160. columns[0]["column"] = "time_stamp";
  161. columns[0]["font"] = "SANSSERIF_SMALL";
  162. columns[0]["value"] = mcd.mLastTimeStr;
  163. columns[1]["column"] = "name";
  164. columns[1]["font"] = "SANSSERIF_SMALL";
  165. columns[1]["value"] = mcd.mFullName;
  166. columns[2]["column"] = "magnitude";
  167. columns[2]["font"] = "SANSSERIF_SMALL";
  168. columns[2]["value"] = llformat("%d/%d", (S32)(mcd.mMag + 0.5f),
  169. (S32)(mcd.mMaxMag + 0.5f));
  170. columns[3]["column"] = "type";
  171. columns[3]["font"] = "SANSSERIF_SMALL";
  172. columns[3]["value"] = *type;
  173. columns[4]["column"] = "number";
  174. columns[4]["font"] = "SANSSERIF_SMALL";
  175. columns[4]["value"] = llformat("%d", mcd.mNumber);
  176. // Hidden column. We do not use element["id"], because the same
  177. // perpetrator could use several types of aggressions...
  178. columns[5]["column"] = "perp_id";
  179. columns[5]["value"] = mcd.mPerpetratorId;
  180. LLScrollListItem* itemp = mBumpsList->addElement(element);
  181. if (itemp && mcd.mLastTimeStr != mcd.mFirstTimeStr)
  182. {
  183. static const std::string first_event =
  184. getString("first_such_event");
  185. itemp->setToolTip(first_event + " " + mcd.mFirstTimeStr);
  186. }
  187. }
  188. // Automatically clamped to last line
  189. mBumpsList->setScrollPos(S32_MAX);
  190. }
  191. //virtual
  192. void HBFloaterBump::draw()
  193. {
  194. //MK
  195. if (gRLenabled &&
  196. (gRLInterface.mContainsShownames ||
  197. gRLInterface.mContainsShownametags))
  198. {
  199. close();
  200. return;
  201. }
  202. //mk
  203. if (sListUpdated)
  204. {
  205. refresh();
  206. }
  207. mClearButton->setEnabled(!sMeanCollisionsList.empty());
  208. bool enabled = mBumpsList->getNumSelected() > 0;
  209. mFocusButton->setEnabled(enabled);
  210. mProfileButton->setEnabled(enabled);
  211. mReportButton->setEnabled(enabled);
  212. LLFloater::draw();
  213. }
  214. //static
  215. void HBFloaterBump::cleanup()
  216. {
  217. sMeanCollisionsList.clear();
  218. sListUpdated = true;
  219. }
  220. //static
  221. void HBFloaterBump::meanNameCallback(const LLUUID& id,
  222. const std::string& fullname, bool)
  223. {
  224. for (collisions_list_t::iterator iter = sMeanCollisionsList.begin(),
  225. end = sMeanCollisionsList.end();
  226. iter != end; ++iter)
  227. {
  228. LLMeanCollisionData& mcd = *iter;
  229. if (mcd.mPerpetratorId == id)
  230. {
  231. mcd.mFullName = fullname;
  232. sListUpdated = true;
  233. }
  234. }
  235. }
  236. //static
  237. void HBFloaterBump::addMeanCollision(const LLUUID& id, U32 time,
  238. EMeanCollisionType type, F32 mag)
  239. {
  240. for (collisions_list_t::iterator iter = sMeanCollisionsList.begin(),
  241. end = sMeanCollisionsList.end();
  242. iter != end; ++iter)
  243. {
  244. LLMeanCollisionData& mcd = *iter;
  245. if (mcd.mPerpetratorId == id && mcd.mType == type)
  246. {
  247. mcd.setTime(time);
  248. if (mag > mcd.mMaxMag)
  249. {
  250. mcd.mMaxMag = mag;
  251. }
  252. mcd.mMag = mag;
  253. ++mcd.mNumber;
  254. sListUpdated = true;
  255. return;
  256. }
  257. }
  258. sMeanCollisionsList.emplace_back(id, time, type, mag);
  259. if (gCacheNamep)
  260. {
  261. // Note: sListUpdated will be set on name resolution callback
  262. gCacheNamep->get(id, false, meanNameCallback);
  263. }
  264. }
  265. //static
  266. std::string HBFloaterBump::getMeanCollisionsStats(const LLUUID& perpetrator_id)
  267. {
  268. // Gather the statistics about the prepetrator's assault
  269. S32 total_hits = 0;
  270. F32 max_mag = 0.f;
  271. time_t first_time = time_max();
  272. time_t last_time = 0;
  273. std::string first, last;
  274. for (collisions_list_t::iterator iter = sMeanCollisionsList.begin(),
  275. end = sMeanCollisionsList.end();
  276. iter != end; ++iter)
  277. {
  278. const LLMeanCollisionData& mcd = *iter;
  279. if (mcd.mPerpetratorId == perpetrator_id)
  280. {
  281. if (mcd.mFirstTime < first_time)
  282. {
  283. first_time = mcd.mFirstTime;
  284. first = mcd.mFirstTimeStr;
  285. }
  286. if (mcd.mLastTime > last_time)
  287. {
  288. last_time = mcd.mLastTime;
  289. last = mcd.mLastTimeStr;
  290. }
  291. if (mcd.mMaxMag > max_mag)
  292. {
  293. max_mag = mcd.mMaxMag;
  294. }
  295. total_hits += mcd.mNumber;
  296. }
  297. }
  298. if (!total_hits)
  299. {
  300. return "";
  301. }
  302. // Remove the seconds from the time stamps
  303. size_t i = first.rfind(':');
  304. if (i != std::string::npos)
  305. {
  306. first.erase(i);
  307. }
  308. i = last.rfind(':');
  309. if (i != std::string::npos)
  310. {
  311. last.erase(i);
  312. }
  313. // Create a description of the assault from the statistics
  314. static const char* short_assault =
  315. "Total pushes: %d - Max magnitude: %d - Occured at %s SLT.";
  316. static const char* long_assault =
  317. "Total pushes: %d - Max magnitude: %d - Extended over %s to %s, SLT.";
  318. std::string desc;
  319. if (last_time - first_time > 60)
  320. {
  321. desc = llformat(long_assault, total_hits, (S32)(max_mag + 0.5f),
  322. first.c_str(), last.c_str());
  323. }
  324. else
  325. {
  326. desc = llformat(short_assault, total_hits, (S32)(max_mag + 0.5f),
  327. first.c_str());
  328. }
  329. return desc;
  330. }
  331. //static
  332. void HBFloaterBump::onButtonClose(void* data)
  333. {
  334. HBFloaterBump* self = (HBFloaterBump*)data;
  335. if (self)
  336. {
  337. self->close();
  338. }
  339. }
  340. //static
  341. void HBFloaterBump::onButtonClear(void*)
  342. {
  343. cleanup();
  344. }
  345. //static
  346. void HBFloaterBump::onButtonFocus(void* userdata)
  347. {
  348. HBFloaterBump* self = (HBFloaterBump*)userdata;
  349. if (self)
  350. {
  351. LLScrollListItem* itemp = self->mBumpsList->getFirstSelected();
  352. if (itemp)
  353. {
  354. LLUUID perpetrator_id = itemp->getColumn(5)->getValue().asUUID();
  355. gAgent.lookAtObject(perpetrator_id, CAMERA_POSITION_OBJECT);
  356. }
  357. }
  358. }
  359. //static
  360. void HBFloaterBump::onButtonProfile(void* data)
  361. {
  362. HBFloaterBump* self = (HBFloaterBump*)data;
  363. if (self)
  364. {
  365. LLScrollListItem* itemp = self->mBumpsList->getFirstSelected();
  366. if (itemp)
  367. {
  368. LLUUID perpetrator_id = itemp->getColumn(5)->getValue().asUUID();
  369. LLFloaterAvatarInfo::showFromDirectory(perpetrator_id);
  370. }
  371. }
  372. }
  373. //static
  374. void HBFloaterBump::onButtonReport(void* data)
  375. {
  376. HBFloaterBump* self = (HBFloaterBump*)data;
  377. if (!self)
  378. {
  379. return;
  380. }
  381. LLScrollListItem* itemp = self->mBumpsList->getFirstSelected();
  382. if (!itemp)
  383. {
  384. return;
  385. }
  386. LLUUID perpetrator_id = itemp->getColumn(5)->getValue().asUUID();
  387. std::string desc = getMeanCollisionsStats(perpetrator_id);
  388. if (!desc.empty())
  389. {
  390. // Spawn the abuse reporting floater. Note: 35 is the category value
  391. // for Assault__Safe_area, as defined in floater_report_abuse.xml
  392. LLFloaterReporter::showFromAvatar(perpetrator_id, desc, 35);
  393. }
  394. }