PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llnearbychathandler.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 632 lines | 401 code | 134 blank | 97 comment | 75 complexity | 71cf0730b8783d25b7ceffdb464836f3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file LLNearbyChatHandler.cpp
  3. * @brief Nearby chat notification managment
  4. *
  5. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llagentdata.h" // for gAgentID
  28. #include "llnearbychathandler.h"
  29. #include "llchatitemscontainerctrl.h"
  30. #include "llfirstuse.h"
  31. #include "llfloaterscriptdebug.h"
  32. #include "llhints.h"
  33. #include "llnearbychat.h"
  34. #include "llrecentpeople.h"
  35. #include "llviewercontrol.h"
  36. #include "llfloaterreg.h"//for LLFloaterReg::getTypedInstance
  37. #include "llviewerwindow.h"//for screen channel position
  38. #include "llnearbychatbar.h"
  39. #include "llrootview.h"
  40. #include "lllayoutstack.h"
  41. //add LLNearbyChatHandler to LLNotificationsUI namespace
  42. using namespace LLNotificationsUI;
  43. //-----------------------------------------------------------------------------------------------
  44. //LLNearbyChatScreenChannel
  45. //-----------------------------------------------------------------------------------------------
  46. LLToastPanelBase* createToastPanel()
  47. {
  48. LLNearbyChatToastPanel* item = LLNearbyChatToastPanel::createInstance();
  49. return item;
  50. }
  51. class LLNearbyChatScreenChannel: public LLScreenChannelBase
  52. {
  53. LOG_CLASS(LLNearbyChatScreenChannel);
  54. public:
  55. typedef std::vector<LLHandle<LLToast> > toast_vec_t;
  56. typedef std::list<LLHandle<LLToast> > toast_list_t;
  57. LLNearbyChatScreenChannel(const Params& p)
  58. : LLScreenChannelBase(p)
  59. {
  60. mStopProcessing = false;
  61. LLControlVariable* ctrl = gSavedSettings.getControl("NearbyToastLifeTime").get();
  62. if (ctrl)
  63. {
  64. ctrl->getSignal()->connect(boost::bind(&LLNearbyChatScreenChannel::updateToastsLifetime, this));
  65. }
  66. ctrl = gSavedSettings.getControl("NearbyToastFadingTime").get();
  67. if (ctrl)
  68. {
  69. ctrl->getSignal()->connect(boost::bind(&LLNearbyChatScreenChannel::updateToastFadingTime, this));
  70. }
  71. }
  72. void addNotification (LLSD& notification);
  73. void arrangeToasts ();
  74. typedef boost::function<LLToastPanelBase* (void )> create_toast_panel_callback_t;
  75. void setCreatePanelCallback(create_toast_panel_callback_t value) { m_create_toast_panel_callback_t = value;}
  76. void onToastDestroyed (LLToast* toast, bool app_quitting);
  77. void onToastFade (LLToast* toast);
  78. void redrawToasts()
  79. {
  80. arrangeToasts();
  81. }
  82. // hide all toasts from screen, but not remove them from a channel
  83. // removes all toasts from a channel
  84. virtual void removeToastsFromChannel()
  85. {
  86. for(toast_vec_t::iterator it = m_active_toasts.begin(); it != m_active_toasts.end(); ++it)
  87. {
  88. addToToastPool(it->get());
  89. }
  90. m_active_toasts.clear();
  91. };
  92. virtual void deleteAllChildren()
  93. {
  94. LL_DEBUGS("NearbyChat") << "Clearing toast pool" << llendl;
  95. m_toast_pool.clear();
  96. m_active_toasts.clear();
  97. LLScreenChannelBase::deleteAllChildren();
  98. }
  99. protected:
  100. void deactivateToast(LLToast* toast);
  101. void addToToastPool(LLToast* toast)
  102. {
  103. if (!toast) return;
  104. LL_DEBUGS("NearbyChat") << "Pooling toast" << llendl;
  105. toast->setVisible(FALSE);
  106. toast->stopTimer();
  107. toast->setIsHidden(true);
  108. // Nearby chat toasts that are hidden, not destroyed. They are collected to the toast pool, so that
  109. // they can be used next time, this is done for performance. But if the toast lifetime was changed
  110. // (from preferences floater (STORY-36)) while it was shown (at this moment toast isn't in the pool yet)
  111. // changes don't take affect.
  112. // So toast's lifetime should be updated each time it's added to the pool. Otherwise viewer would have
  113. // to be restarted so that changes take effect.
  114. toast->setLifetime(gSavedSettings.getS32("NearbyToastLifeTime"));
  115. toast->setFadingTime(gSavedSettings.getS32("NearbyToastFadingTime"));
  116. m_toast_pool.push_back(toast->getHandle());
  117. }
  118. void createOverflowToast(S32 bottom, F32 timer);
  119. void updateToastsLifetime();
  120. void updateToastFadingTime();
  121. create_toast_panel_callback_t m_create_toast_panel_callback_t;
  122. bool createPoolToast();
  123. toast_vec_t m_active_toasts;
  124. toast_list_t m_toast_pool;
  125. bool mStopProcessing;
  126. bool mChannelRect;
  127. };
  128. //-----------------------------------------------------------------------------------------------
  129. // LLNearbyChatToast
  130. //-----------------------------------------------------------------------------------------------
  131. // We're deriving from LLToast to be able to override onClose()
  132. // in order to handle closing nearby chat toasts properly.
  133. class LLNearbyChatToast : public LLToast
  134. {
  135. LOG_CLASS(LLNearbyChatToast);
  136. public:
  137. LLNearbyChatToast(const LLToast::Params& p, LLNearbyChatScreenChannel* nc_channelp)
  138. : LLToast(p),
  139. mNearbyChatScreenChannelp(nc_channelp)
  140. {
  141. }
  142. /*virtual*/ void onClose(bool app_quitting);
  143. private:
  144. LLNearbyChatScreenChannel* mNearbyChatScreenChannelp;
  145. };
  146. //-----------------------------------------------------------------------------------------------
  147. // LLNearbyChatScreenChannel
  148. //-----------------------------------------------------------------------------------------------
  149. void LLNearbyChatScreenChannel::deactivateToast(LLToast* toast)
  150. {
  151. toast_vec_t::iterator pos = std::find(m_active_toasts.begin(), m_active_toasts.end(), toast->getHandle());
  152. if (pos == m_active_toasts.end())
  153. {
  154. llassert(pos == m_active_toasts.end());
  155. return;
  156. }
  157. LL_DEBUGS("NearbyChat") << "Deactivating toast" << llendl;
  158. m_active_toasts.erase(pos);
  159. }
  160. void LLNearbyChatScreenChannel::createOverflowToast(S32 bottom, F32 timer)
  161. {
  162. //we don't need overflow toast in nearby chat
  163. }
  164. void LLNearbyChatScreenChannel::onToastDestroyed(LLToast* toast, bool app_quitting)
  165. {
  166. LL_DEBUGS("NearbyChat") << "Toast destroyed (app_quitting=" << app_quitting << ")" << llendl;
  167. if (app_quitting)
  168. {
  169. // Viewer is quitting.
  170. // Immediately stop processing chat messages (EXT-1419).
  171. mStopProcessing = true;
  172. }
  173. else
  174. {
  175. // The toast is being closed by user (STORM-192).
  176. // Remove it from the list of active toasts to prevent
  177. // further references to the invalid pointer.
  178. deactivateToast(toast);
  179. }
  180. }
  181. void LLNearbyChatScreenChannel::onToastFade(LLToast* toast)
  182. {
  183. LL_DEBUGS("NearbyChat") << "Toast fading" << llendl;
  184. //fade mean we put toast to toast pool
  185. if(!toast)
  186. return;
  187. deactivateToast(toast);
  188. addToToastPool(toast);
  189. arrangeToasts();
  190. }
  191. void LLNearbyChatScreenChannel::updateToastsLifetime()
  192. {
  193. S32 seconds = gSavedSettings.getS32("NearbyToastLifeTime");
  194. toast_list_t::iterator it;
  195. for(it = m_toast_pool.begin(); it != m_toast_pool.end(); ++it)
  196. {
  197. (*it).get()->setLifetime(seconds);
  198. }
  199. }
  200. void LLNearbyChatScreenChannel::updateToastFadingTime()
  201. {
  202. S32 seconds = gSavedSettings.getS32("NearbyToastFadingTime");
  203. toast_list_t::iterator it;
  204. for(it = m_toast_pool.begin(); it != m_toast_pool.end(); ++it)
  205. {
  206. (*it).get()->setFadingTime(seconds);
  207. }
  208. }
  209. bool LLNearbyChatScreenChannel::createPoolToast()
  210. {
  211. LLToastPanelBase* panel= m_create_toast_panel_callback_t();
  212. if(!panel)
  213. return false;
  214. LLToast::Params p;
  215. p.panel = panel;
  216. p.lifetime_secs = gSavedSettings.getS32("NearbyToastLifeTime");
  217. p.fading_time_secs = gSavedSettings.getS32("NearbyToastFadingTime");
  218. LLToast* toast = new LLNearbyChatToast(p, this);
  219. toast->setOnFadeCallback(boost::bind(&LLNearbyChatScreenChannel::onToastFade, this, _1));
  220. // If the toast gets somehow prematurely destroyed, deactivate it to prevent crash (STORM-1352).
  221. toast->setOnToastDestroyedCallback(boost::bind(&LLNearbyChatScreenChannel::onToastDestroyed, this, _1, false));
  222. LL_DEBUGS("NearbyChat") << "Creating and pooling toast" << llendl;
  223. m_toast_pool.push_back(toast->getHandle());
  224. return true;
  225. }
  226. void LLNearbyChatScreenChannel::addNotification(LLSD& notification)
  227. {
  228. //look in pool. if there is any message
  229. if(mStopProcessing)
  230. return;
  231. /*
  232. find last toast and check ID
  233. */
  234. if(m_active_toasts.size())
  235. {
  236. LLUUID fromID = notification["from_id"].asUUID(); // agent id or object id
  237. std::string from = notification["from"].asString();
  238. LLToast* toast = m_active_toasts[0].get();
  239. if (toast)
  240. {
  241. LLNearbyChatToastPanel* panel = dynamic_cast<LLNearbyChatToastPanel*>(toast->getPanel());
  242. if(panel && panel->messageID() == fromID && panel->getFromName() == from && panel->canAddText())
  243. {
  244. panel->addMessage(notification);
  245. toast->reshapeToPanel();
  246. toast->startTimer();
  247. arrangeToasts();
  248. return;
  249. }
  250. }
  251. }
  252. if(m_toast_pool.empty())
  253. {
  254. //"pool" is empty - create one more panel
  255. LL_DEBUGS("NearbyChat") << "Empty pool" << llendl;
  256. if(!createPoolToast())//created toast will go to pool. so next call will find it
  257. return;
  258. addNotification(notification);
  259. return;
  260. }
  261. int chat_type = notification["chat_type"].asInteger();
  262. if( ((EChatType)chat_type == CHAT_TYPE_DEBUG_MSG))
  263. {
  264. if(gSavedSettings.getBOOL("ShowScriptErrors") == FALSE)
  265. return;
  266. if(gSavedSettings.getS32("ShowScriptErrorsLocation")== 1)
  267. return;
  268. }
  269. //take 1st element from pool, (re)initialize it, put it in active toasts
  270. LL_DEBUGS("NearbyChat") << "Getting toast from pool" << llendl;
  271. LLToast* toast = m_toast_pool.back().get();
  272. m_toast_pool.pop_back();
  273. LLToastPanelBase* panel = dynamic_cast<LLToastPanelBase*>(toast->getPanel());
  274. if(!panel)
  275. return;
  276. panel->init(notification);
  277. toast->reshapeToPanel();
  278. toast->startTimer();
  279. m_active_toasts.push_back(toast->getHandle());
  280. arrangeToasts();
  281. }
  282. static bool sort_toasts_predicate(LLHandle<LLToast> first, LLHandle<LLToast> second)
  283. {
  284. if (!first.get() || !second.get()) return false; // STORM-1352
  285. F32 v1 = first.get()->getTimeLeftToLive();
  286. F32 v2 = second.get()->getTimeLeftToLive();
  287. return v1 > v2;
  288. }
  289. void LLNearbyChatScreenChannel::arrangeToasts()
  290. {
  291. if(mStopProcessing || isHovering())
  292. return;
  293. if (mFloaterSnapRegion == NULL)
  294. {
  295. mFloaterSnapRegion = gViewerWindow->getRootView()->getChildView("floater_snap_region");
  296. }
  297. if (!getParent())
  298. {
  299. // connect to floater snap region just to get resize events, we don't care about being a proper widget
  300. mFloaterSnapRegion->addChild(this);
  301. setFollows(FOLLOWS_ALL);
  302. }
  303. LLRect toast_rect;
  304. updateRect();
  305. LLRect channel_rect;
  306. mFloaterSnapRegion->localRectToOtherView(mFloaterSnapRegion->getLocalRect(), &channel_rect, gFloaterView);
  307. channel_rect.mLeft += 10;
  308. channel_rect.mRight = channel_rect.mLeft + 300;
  309. S32 channel_bottom = channel_rect.mBottom;
  310. S32 bottom = channel_bottom + 80;
  311. S32 margin = gSavedSettings.getS32("ToastGap");
  312. //sort active toasts
  313. std::sort(m_active_toasts.begin(),m_active_toasts.end(),sort_toasts_predicate);
  314. //calc max visible item and hide other toasts.
  315. for(toast_vec_t::iterator it = m_active_toasts.begin(); it != m_active_toasts.end(); ++it)
  316. {
  317. LLToast* toast = it->get();
  318. if (!toast)
  319. {
  320. llwarns << "NULL found in the active chat toasts list!" << llendl;
  321. continue;
  322. }
  323. S32 toast_top = bottom + toast->getRect().getHeight() + margin;
  324. if(toast_top > channel_rect.getHeight())
  325. {
  326. while(it!=m_active_toasts.end())
  327. {
  328. addToToastPool(it->get());
  329. it=m_active_toasts.erase(it);
  330. }
  331. break;
  332. }
  333. toast_rect = toast->getRect();
  334. toast_rect.setLeftTopAndSize(channel_rect.mLeft , bottom + toast_rect.getHeight(), toast_rect.getWidth() ,toast_rect.getHeight());
  335. toast->setRect(toast_rect);
  336. bottom += toast_rect.getHeight() - toast->getTopPad() + margin;
  337. }
  338. // use reverse order to provide correct z-order and avoid toast blinking
  339. for(toast_vec_t::reverse_iterator it = m_active_toasts.rbegin(); it != m_active_toasts.rend(); ++it)
  340. {
  341. LLToast* toast = it->get();
  342. if (toast)
  343. {
  344. toast->setIsHidden(false);
  345. toast->setVisible(TRUE);
  346. }
  347. }
  348. }
  349. //-----------------------------------------------------------------------------------------------
  350. //LLNearbyChatHandler
  351. //-----------------------------------------------------------------------------------------------
  352. boost::scoped_ptr<LLEventPump> LLNearbyChatHandler::sChatWatcher(new LLEventStream("LLChat"));
  353. LLNearbyChatHandler::LLNearbyChatHandler(e_notification_type type, const LLSD& id)
  354. {
  355. mType = type;
  356. // Getting a Channel for our notifications
  357. LLNearbyChatScreenChannel::Params p;
  358. p.id = LLUUID(gSavedSettings.getString("NearByChatChannelUUID"));
  359. LLNearbyChatScreenChannel* channel = new LLNearbyChatScreenChannel(p);
  360. LLNearbyChatScreenChannel::create_toast_panel_callback_t callback = createToastPanel;
  361. channel->setCreatePanelCallback(callback);
  362. mChannel = LLChannelManager::getInstance()->addChannel(channel);
  363. }
  364. LLNearbyChatHandler::~LLNearbyChatHandler()
  365. {
  366. }
  367. void LLNearbyChatHandler::initChannel()
  368. {
  369. //LLRect snap_rect = gFloaterView->getSnapRect();
  370. //mChannel->init(snap_rect.mLeft, snap_rect.mLeft + 200);
  371. }
  372. void LLNearbyChatHandler::processChat(const LLChat& chat_msg,
  373. const LLSD &args)
  374. {
  375. if(chat_msg.mMuted == TRUE)
  376. return;
  377. if(chat_msg.mText.empty())
  378. return;//don't process empty messages
  379. LLFloater* chat_bar = LLFloaterReg::getInstance("chat_bar");
  380. LLNearbyChat* nearby_chat = chat_bar->findChild<LLNearbyChat>("nearby_chat");
  381. // Build notification data
  382. LLSD notification;
  383. notification["message"] = chat_msg.mText;
  384. notification["from"] = chat_msg.mFromName;
  385. notification["from_id"] = chat_msg.mFromID;
  386. notification["time"] = chat_msg.mTime;
  387. notification["source"] = (S32)chat_msg.mSourceType;
  388. notification["chat_type"] = (S32)chat_msg.mChatType;
  389. notification["chat_style"] = (S32)chat_msg.mChatStyle;
  390. // Pass sender info so that it can be rendered properly (STORM-1021).
  391. notification["sender_slurl"] = LLViewerChat::getSenderSLURL(chat_msg, args);
  392. if (chat_msg.mChatType == CHAT_TYPE_DIRECT &&
  393. chat_msg.mText.length() > 0 &&
  394. chat_msg.mText[0] == '@')
  395. {
  396. // Send event on to LLEventStream and exit
  397. sChatWatcher->post(notification);
  398. return;
  399. }
  400. // don't show toast and add message to chat history on receive debug message
  401. // with disabled setting showing script errors or enabled setting to show script
  402. // errors in separate window.
  403. if (chat_msg.mChatType == CHAT_TYPE_DEBUG_MSG)
  404. {
  405. if(gSavedSettings.getBOOL("ShowScriptErrors") == FALSE)
  406. return;
  407. // don't process debug messages from not owned objects, see EXT-7762
  408. if (gAgentID != chat_msg.mOwnerID)
  409. {
  410. return;
  411. }
  412. if (gSavedSettings.getS32("ShowScriptErrorsLocation")== 1)// show error in window //("ScriptErrorsAsChat"))
  413. {
  414. LLColor4 txt_color;
  415. LLViewerChat::getChatColor(chat_msg,txt_color);
  416. LLFloaterScriptDebug::addScriptLine(chat_msg.mText,
  417. chat_msg.mFromName,
  418. txt_color,
  419. chat_msg.mFromID);
  420. return;
  421. }
  422. }
  423. nearby_chat->addMessage(chat_msg, true, args);
  424. if(chat_msg.mSourceType == CHAT_SOURCE_AGENT
  425. && chat_msg.mFromID.notNull()
  426. && chat_msg.mFromID != gAgentID)
  427. {
  428. LLFirstUse::otherAvatarChatFirst();
  429. // Add sender to the recent people list.
  430. LLRecentPeople::instance().add(chat_msg.mFromID);
  431. }
  432. // Send event on to LLEventStream
  433. sChatWatcher->post(notification);
  434. if( !chat_bar->isMinimized()
  435. && nearby_chat->isInVisibleChain()
  436. || ( chat_msg.mSourceType == CHAT_SOURCE_AGENT
  437. && gSavedSettings.getBOOL("UseChatBubbles") )
  438. || !mChannel->getShowToasts() ) // to prevent toasts in Busy mode
  439. return;//no need in toast if chat is visible or if bubble chat is enabled
  440. // arrange a channel on a screen
  441. if(!mChannel->getVisible())
  442. {
  443. initChannel();
  444. }
  445. /*
  446. //comment all this due to EXT-4432
  447. ..may clean up after some time...
  448. //only messages from AGENTS
  449. if(CHAT_SOURCE_OBJECT == chat_msg.mSourceType)
  450. {
  451. if(chat_msg.mChatType == CHAT_TYPE_DEBUG_MSG)
  452. return;//ok for now we don't skip messeges from object, so skip only debug messages
  453. }
  454. */
  455. LLNearbyChatScreenChannel* channel = dynamic_cast<LLNearbyChatScreenChannel*>(mChannel);
  456. if(channel)
  457. {
  458. // Handle IRC styled messages.
  459. std::string toast_msg;
  460. if (chat_msg.mChatStyle == CHAT_STYLE_IRC)
  461. {
  462. if (!chat_msg.mFromName.empty())
  463. {
  464. toast_msg += chat_msg.mFromName;
  465. }
  466. toast_msg += chat_msg.mText.substr(3);
  467. }
  468. else
  469. {
  470. toast_msg = chat_msg.mText;
  471. }
  472. // Add a nearby chat toast.
  473. LLUUID id;
  474. id.generate();
  475. notification["id"] = id;
  476. std::string r_color_name = "White";
  477. F32 r_color_alpha = 1.0f;
  478. LLViewerChat::getChatColor( chat_msg, r_color_name, r_color_alpha);
  479. notification["text_color"] = r_color_name;
  480. notification["color_alpha"] = r_color_alpha;
  481. notification["font_size"] = (S32)LLViewerChat::getChatFontSize() ;
  482. notification["message"] = toast_msg;
  483. channel->addNotification(notification);
  484. }
  485. }
  486. void LLNearbyChatHandler::onDeleteToast(LLToast* toast)
  487. {
  488. }
  489. //-----------------------------------------------------------------------------------------------
  490. // LLNearbyChatToast
  491. //-----------------------------------------------------------------------------------------------
  492. // virtual
  493. void LLNearbyChatToast::onClose(bool app_quitting)
  494. {
  495. mNearbyChatScreenChannelp->onToastDestroyed(this, app_quitting);
  496. }
  497. // EOF