llchatbar.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /**
  2. * @file llchatbar.cpp
  3. * @brief LLChatBar class implementation
  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. #include "llviewerprecompiledheaders.h"
  33. #include "llchatbar.h"
  34. #include "imageids.h"
  35. #include "llbutton.h"
  36. #include "llcachename.h"
  37. #include "llcombobox.h"
  38. #include "llkeyboard.h"
  39. #include "lllineeditor.h"
  40. #include "llmultigesture.h"
  41. #include "llparcel.h"
  42. #include "llspellcheck.h"
  43. #include "lluictrlfactory.h"
  44. #include "llmessage.h"
  45. #include "llagent.h"
  46. #include "llcommandhandler.h" // For secondlife:///app/chat/ support
  47. #include "llfloaterchat.h"
  48. #include "hbfloatertextinput.h"
  49. #include "llgesturemgr.h"
  50. #include "llinventorymodel.h"
  51. //MK
  52. #include "mkrlinterface.h"
  53. //mk
  54. #include "hbviewerautomation.h"
  55. #include "llviewergesture.h" // For triggering gestures
  56. #include "llviewermenu.h" // For deleting object with DEL key
  57. #include "llviewerstats.h"
  58. #include "llworld.h"
  59. constexpr F32 AGENT_TYPING_TIMEOUT = 5.f; // seconds
  60. // Pointer to the main chat bar (there is also the chat floater chat bar) which
  61. // instance is created and destroyed in LLViewerWindow::initBase()
  62. LLChatBar* gChatBarp = NULL;
  63. //static
  64. bool LLChatBar::sSwappedShortcuts = false;
  65. LLChatBar::av_names_list_t LLChatBar::sIgnoredNames;
  66. // Helper function
  67. void send_chat_from_viewer(const std::string& utf8_out_text, EChatType type,
  68. S32 channel)
  69. {
  70. //MK
  71. bool could_censor = gRLenabled && type != CHAT_TYPE_START &&
  72. type != CHAT_TYPE_STOP;
  73. if (could_censor && channel >= 2147483647 &&
  74. gRLInterface.contains("sendchat"))
  75. {
  76. // When prevented from talking, remove the ability to talk on the
  77. // DEBUG_CHANNEL altogether, since it is a way of cheating
  78. return;
  79. }
  80. LLMessageSystem* msg = gMessageSystemp;
  81. if (could_censor && channel == 0)
  82. {
  83. std::string restriction;
  84. // We might want to redirect this chat or emote (and exit this function
  85. // early on)
  86. if (utf8_out_text.find("/me ") == 0 ||
  87. utf8_out_text.find("/me'") == 0) // emote
  88. {
  89. if (gRLInterface.containsSubstr("rediremote:"))
  90. {
  91. restriction = "rediremote:";
  92. }
  93. }
  94. else if (utf8_out_text.find("((") != 0 ||
  95. utf8_out_text.find("))") != utf8_out_text.length() - 2)
  96. {
  97. if (gRLInterface.containsSubstr("redirchat:"))
  98. {
  99. restriction = "redirchat:";
  100. }
  101. }
  102. if (!restriction.empty())
  103. {
  104. // Public chat or emote redirected => for each redirection, send
  105. // the same message on the target channel
  106. rl_map_it_t it = gRLInterface.mSpecialObjectBehaviours.begin();
  107. std::string behav;
  108. while (it != gRLInterface.mSpecialObjectBehaviours.end())
  109. {
  110. behav = it->second;
  111. if (behav.find (restriction) == 0)
  112. {
  113. S32 ch = atoi(behav.substr(restriction.length()).c_str());
  114. std::ostringstream stream;
  115. stream << "" << ch;
  116. if (!gRLInterface.contains("sendchannel_except:" +
  117. stream.str()) &&
  118. !gRLInterface.containsWithoutException("sendchannel",
  119. stream.str()))
  120. {
  121. if (ch > 0 && ch < 2147483647)
  122. {
  123. msg->newMessageFast(_PREHASH_ChatFromViewer);
  124. msg->nextBlockFast(_PREHASH_AgentData);
  125. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  126. msg->addUUIDFast(_PREHASH_SessionID,
  127. gAgentSessionID);
  128. msg->nextBlockFast(_PREHASH_ChatData);
  129. msg->addStringFast(_PREHASH_Message,
  130. utf8_out_text);
  131. msg->addU8Fast(_PREHASH_Type, type);
  132. msg->addS32(_PREHASH_Channel, ch);
  133. gAgent.sendReliableMessage();
  134. }
  135. }
  136. }
  137. ++it;
  138. }
  139. gViewerStats.incStat(LLViewerStats::ST_CHAT_COUNT);
  140. // We have redirected the chat message, do not send it on the
  141. // original channel
  142. return;
  143. }
  144. }
  145. std::string crunched_text = utf8_out_text;
  146. // There is a redirection in force but this particular message is an emote
  147. // or an OOC text, so we did not redirect it. However it has not gone
  148. // through crunchEmote yet, so we need to do this here to prevent cheating
  149. // with emote-like chat (true emotes must however go through untouched).
  150. if (could_censor && channel == 0 &&
  151. gRLInterface.containsSubstr("redirchat:"))
  152. {
  153. crunched_text = gRLInterface.crunchEmote(crunched_text);
  154. }
  155. //mk
  156. if (channel >= 0)
  157. {
  158. msg->newMessageFast(_PREHASH_ChatFromViewer);
  159. msg->nextBlockFast(_PREHASH_AgentData);
  160. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  161. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  162. msg->nextBlockFast(_PREHASH_ChatData);
  163. //// msg->addStringFast(_PREHASH_Message, utf8_out_text);
  164. //MK
  165. msg->addStringFast(_PREHASH_Message, crunched_text);
  166. //mk
  167. msg->addU8Fast(_PREHASH_Type, type);
  168. msg->addS32(_PREHASH_Channel, channel);
  169. }
  170. else
  171. {
  172. // *HACK: ChatFromViewer does not allow negative channels
  173. msg->newMessage(_PREHASH_ScriptDialogReply);
  174. msg->nextBlock(_PREHASH_AgentData);
  175. msg->addUUID(_PREHASH_AgentID, gAgentID);
  176. msg->addUUID(_PREHASH_SessionID, gAgentSessionID);
  177. msg->nextBlock(_PREHASH_Data);
  178. msg->addUUID(_PREHASH_ObjectID, gAgentID);
  179. msg->addS32(_PREHASH_ChatChannel, channel);
  180. msg->addS32(_PREHASH_ButtonIndex, 0);
  181. //// msg->addStringFast(_PREHASH_ButtonLabel, utf8_out_text);
  182. //MK
  183. msg->addString(_PREHASH_ButtonLabel, crunched_text);
  184. //mk
  185. }
  186. gAgent.sendReliableMessage();
  187. gViewerStats.incStat(LLViewerStats::ST_CHAT_COUNT);
  188. }
  189. ///////////////////////////////////////////////////////////////////////////////
  190. // Command handler
  191. ///////////////////////////////////////////////////////////////////////////////
  192. class LLChatHandler final : public LLCommandHandler
  193. {
  194. public:
  195. // Not allowed from outside the app
  196. LLChatHandler()
  197. : LLCommandHandler("chat", UNTRUSTED_BLOCK)
  198. {
  199. }
  200. // Your code here
  201. bool handle(const LLSD& tokens, const LLSD&, LLMediaCtrl*) override
  202. {
  203. if (tokens.size() < 2) return false;
  204. S32 channel = tokens[0].asInteger();
  205. std::string mesg = tokens[1].asString();
  206. EChatType type = CHAT_TYPE_NORMAL;
  207. //MK
  208. if (gRLenabled && channel == 0 && gRLInterface.contains("chatnormal"))
  209. {
  210. type = CHAT_TYPE_WHISPER;
  211. }
  212. //mk
  213. send_chat_from_viewer(mesg, type, channel);
  214. return true;
  215. }
  216. };
  217. // Creating the object registers with the dispatcher.
  218. LLChatHandler gChatHandler;
  219. ///////////////////////////////////////////////////////////////////////////////
  220. // Observer
  221. ///////////////////////////////////////////////////////////////////////////////
  222. class LLChatBarGestureObserver final : public LLGestureManagerObserver
  223. {
  224. public:
  225. LL_INLINE LLChatBarGestureObserver(LLChatBar* chat_barp)
  226. : mChatBar(chat_barp)
  227. {
  228. }
  229. LL_INLINE ~LLChatBarGestureObserver() override
  230. {
  231. }
  232. LL_INLINE void changed() override { mChatBar->refreshGestures(); }
  233. private:
  234. LLChatBar* mChatBar;
  235. };
  236. ///////////////////////////////////////////////////////////////////////////////
  237. // LLChatBar class proper
  238. ///////////////////////////////////////////////////////////////////////////////
  239. // Constructor for chat bars embedded in floaters, etc
  240. LLChatBar::LLChatBar(const std::string& name)
  241. : LLPanel(name, LLRect(), BORDER_NO),
  242. mInputEditor(NULL),
  243. mOpenTextEditorButton(NULL),
  244. mSayFlyoutButton(NULL),
  245. mGestureLabelTimer(),
  246. mLastSpecialChatChannel(0),
  247. mIsBuilt(false),
  248. mHasScrolledOnce(false),
  249. mGestureCombo(NULL),
  250. mSecondary(true),
  251. mObserver(NULL)
  252. {
  253. sSwappedShortcuts = gSavedSettings.getBool("SwapShoutWhisperShortcuts");
  254. mLastSwappedShortcuts = sSwappedShortcuts;
  255. }
  256. LLChatBar::LLChatBar(const std::string& name, const LLRect& rect)
  257. : LLPanel(name, rect, BORDER_NO),
  258. mInputEditor(NULL),
  259. mOpenTextEditorButton(NULL),
  260. mHistoryButton(NULL),
  261. mSayFlyoutButton(NULL),
  262. mGestureLabelTimer(),
  263. mLastSpecialChatChannel(0),
  264. mIsBuilt(false),
  265. mHasScrolledOnce(false),
  266. mGestureCombo(NULL),
  267. mSecondary(false),
  268. mObserver(NULL)
  269. {
  270. sSwappedShortcuts = gSavedSettings.getBool("SwapShoutWhisperShortcuts");
  271. mLastSwappedShortcuts = sSwappedShortcuts;
  272. LLUICtrlFactory::getInstance()->buildPanel(this, "panel_chat_bar.xml");
  273. setIsChrome(true);
  274. setFocusRoot(true);
  275. setRect(rect); // override xml rect
  276. setBackgroundOpaque(true);
  277. setBackgroundVisible(true);
  278. // Start visible if we left the app while chatting.
  279. setVisible(gSavedSettings.getBool("ChatVisible"));
  280. // Apply custom layout.
  281. layout();
  282. }
  283. LLChatBar::~LLChatBar()
  284. {
  285. gGestureManager.removeObserver(mObserver);
  286. delete mObserver;
  287. mObserver = NULL;
  288. HBFloaterTextInput::abort(mInputEditor);
  289. // LLView destructor cleans up children
  290. }
  291. bool LLChatBar::postBuild()
  292. {
  293. mHistoryButton = getChild<LLButton>("History", true, false);
  294. if (mHistoryButton)
  295. {
  296. mHistoryButton->setClickedCallback(toggleChatHistory, this);
  297. }
  298. mSayFlyoutButton = getChild<LLFlyoutButton>("Say", true, false);
  299. if (mSayFlyoutButton)
  300. {
  301. mSayFlyoutButton->setCommitCallback(onClickSay);
  302. mSayFlyoutButton->setCallbackUserData(this);
  303. if (sSwappedShortcuts)
  304. {
  305. mSayFlyoutButton->setToolTip(getString("swapped_shortcuts"));
  306. }
  307. else
  308. {
  309. mSayFlyoutButton->setToolTip(getString("normal_shortcuts"));
  310. }
  311. }
  312. mOpenTextEditorButton = getChild<LLButton>("open_text_editor_btn",
  313. true, false);
  314. if (mOpenTextEditorButton)
  315. {
  316. mOpenTextEditorButton->setClickedCallback(onClickOpenTextEditor, this);
  317. }
  318. // Attempt to bind to an existing combo box named gesture
  319. setGestureCombo(getChild<LLComboBox>("Gesture", true, false));
  320. mInputEditor = getChild<LLLineEditor>("Chat Editor", true, false);
  321. if (mInputEditor)
  322. {
  323. mInputEditor->setCallbackUserData(this);
  324. mInputEditor->setKeystrokeCallback(&onInputEditorKeystroke);
  325. mInputEditor->setScrolledCallback(&onInputEditorScrolled, this);
  326. mInputEditor->setFocusLostCallback(&onInputEditorFocusLost, this);
  327. mInputEditor->setFocusReceivedCallback(&onInputEditorGainFocus, this);
  328. mInputEditor->setCommitOnFocusLost(false);
  329. mInputEditor->setRevertOnEsc(false);
  330. mInputEditor->setIgnoreTab(true);
  331. mInputEditor->setPassDelete(true);
  332. mInputEditor->setReplaceNewlinesWithSpaces(false);
  333. mInputEditor->setMaxTextLength(DB_CHAT_MSG_STR_LEN);
  334. mInputEditor->setEnableLineHistory(true);
  335. mInputEditor->setCustomMenuType("chat_input");
  336. }
  337. mIsBuilt = true;
  338. return true;
  339. }
  340. //virtual
  341. void LLChatBar::reshape(S32 width, S32 height, bool called_from_parent)
  342. {
  343. LLPanel::reshape(width, height, called_from_parent);
  344. if (mIsBuilt)
  345. {
  346. layout();
  347. }
  348. }
  349. //virtual
  350. bool LLChatBar::handleKeyHere(KEY key, MASK mask)
  351. {
  352. if (HBFloaterTextInput::hasFloaterFor(mInputEditor))
  353. {
  354. HBFloaterTextInput::show(mInputEditor);
  355. return true;
  356. }
  357. if (LLView::sDebugKeys)
  358. {
  359. llinfos << "key = " << std::hex << (U32)key << std::dec << " - mask = "
  360. << mask << llendl;
  361. }
  362. bool handled = false;
  363. // ALT-RETURN is reserved for windowed/fullscreen toggle
  364. if (key == KEY_RETURN)
  365. {
  366. if ((sSwappedShortcuts && mask == MASK_SHIFT) ||
  367. (!sSwappedShortcuts && mask == MASK_CONTROL))
  368. {
  369. // Shout
  370. sendChat(CHAT_TYPE_SHOUT);
  371. handled = true;
  372. }
  373. else if ((sSwappedShortcuts && mask == MASK_CONTROL) ||
  374. (!sSwappedShortcuts && mask == MASK_SHIFT))
  375. {
  376. // Whisper
  377. sendChat(CHAT_TYPE_WHISPER);
  378. handled = true;
  379. }
  380. else if (mask == MASK_NONE)
  381. {
  382. // Say
  383. sendChat(CHAT_TYPE_NORMAL);
  384. handled = true;
  385. }
  386. else if (mInputEditor && mask == (MASK_SHIFT | MASK_CONTROL))
  387. {
  388. S32 cursor = mInputEditor->getCursor();
  389. std::string text = mInputEditor->getText();
  390. // For some reason, the event is triggered twice: let's insert only
  391. // one newline character. HB
  392. if (cursor == 0 || text[cursor - 1] != '\n')
  393. {
  394. text = text.insert(cursor, "\n");
  395. mInputEditor->setText(text);
  396. mInputEditor->setCursor(cursor + 1);
  397. }
  398. handled = true;
  399. }
  400. }
  401. else if (mInputEditor && KEY_TAB == key && mask == MASK_NONE &&
  402. gSavedSettings.getBool("TabAutoCompleteName"))
  403. {
  404. std::string text = mInputEditor->getText();
  405. if (text.length() > 0)
  406. {
  407. S32 word_start = 0;
  408. S32 word_len = 0;
  409. S32 cursor = mInputEditor->getCursor();
  410. S32 pos = cursor;
  411. // Go back one character if the current one is not a letter (this
  412. // could be the ned of the text, a space, or a punctuation)
  413. if (pos >= (S32)text.length() ||
  414. (pos > 0 && !LLStringUtil::isPartOfWord(text[pos])))
  415. {
  416. --pos;
  417. }
  418. if (mInputEditor->getWordBoundriesAt(pos, &word_start, &word_len))
  419. {
  420. if (cursor > word_start)
  421. {
  422. word_len = cursor - word_start;
  423. std::string word = text.substr(word_start, word_len);
  424. std::string suggestion = getMatchingAvatarName(word);
  425. if (suggestion != word)
  426. {
  427. text = text.replace(word_start, word_len, suggestion);
  428. mInputEditor->setText(text);
  429. S32 end = cursor + suggestion.length() - word.length();
  430. if (gSavedSettings.getBool("SelectAutoCompletedPart"))
  431. {
  432. mInputEditor->setSelection(cursor, end);
  433. }
  434. else
  435. {
  436. mInputEditor->setCursor(end);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. handled = true;
  443. }
  444. // Only do this in main chatbar
  445. else if (KEY_ESCAPE == key && mask == MASK_NONE && gChatBarp &&
  446. gChatBarp == this)
  447. {
  448. stopChat();
  449. handled = true;
  450. }
  451. return handled;
  452. }
  453. void LLChatBar::layout()
  454. {
  455. // If this is not the main chat bar, return
  456. if (mSecondary) return;
  457. LLRect r = getRect();
  458. // Get the width of the chat bar
  459. S32 rect_width = r.getWidth();
  460. // Padding (hard-coded) and origin of first element
  461. S32 pad = 4;
  462. S32 x = pad;
  463. // Width consumed by the buttons and gesture combo (i.e. all elements but
  464. // the input line).
  465. S32 consumed_width = x;
  466. // Calculate the elements height and centering
  467. S32 height = gBtnHeight;
  468. if (height < 20)
  469. {
  470. height = 20;
  471. }
  472. S32 y = (r.getHeight() - height) / 2;
  473. if (y < 2)
  474. {
  475. height = r.getHeight() - 4;
  476. y = 2;
  477. }
  478. // Gesture combo width
  479. S32 gesture_width = 0;
  480. if (mGestureCombo)
  481. {
  482. r = mGestureCombo->getRect();
  483. gesture_width = r.getWidth();
  484. consumed_width += gesture_width + pad;
  485. }
  486. // Say button width
  487. S32 say_btn_width = 0;
  488. if (mSayFlyoutButton)
  489. {
  490. r = mSayFlyoutButton->getRect();
  491. say_btn_width = r.getWidth();
  492. consumed_width += say_btn_width + pad;
  493. }
  494. // Editor button width
  495. S32 editor_btn_width = 0;
  496. if (mOpenTextEditorButton)
  497. {
  498. r = mOpenTextEditorButton->getRect();
  499. // Keep the button round if it is already
  500. if (r.getWidth() == r.getHeight())
  501. {
  502. editor_btn_width = height;
  503. }
  504. else
  505. {
  506. editor_btn_width = r.getWidth();
  507. }
  508. consumed_width += editor_btn_width + pad;
  509. }
  510. // History button width
  511. if (mHistoryButton)
  512. {
  513. r = mHistoryButton->getRect();
  514. S32 history_btn_width = r.getWidth();
  515. r.setOriginAndSize(x, y, history_btn_width, height);
  516. mHistoryButton->setRect(r);
  517. x += history_btn_width + pad;
  518. consumed_width += history_btn_width + pad;
  519. }
  520. if (mInputEditor)
  521. {
  522. S32 input_width = rect_width - (consumed_width + pad);
  523. r.setOriginAndSize(x, y + 2, input_width, height - 2);
  524. mInputEditor->reshape(r.getWidth(), r.getHeight());
  525. mInputEditor->setRect(r);
  526. x += input_width + pad;
  527. }
  528. if (mOpenTextEditorButton)
  529. {
  530. r.setOriginAndSize(x, y, editor_btn_width, height);
  531. mOpenTextEditorButton->setRect(r);
  532. x += editor_btn_width + pad;
  533. }
  534. if (mSayFlyoutButton)
  535. {
  536. r.setOriginAndSize(x, y, say_btn_width, height);
  537. mSayFlyoutButton->reshape(r.getWidth(), r.getHeight());
  538. mSayFlyoutButton->setRect(r);
  539. x += say_btn_width + pad;
  540. }
  541. r.setOriginAndSize(x, y, gesture_width, height);
  542. if (mGestureCombo)
  543. {
  544. mGestureCombo->setRect(r);
  545. }
  546. }
  547. //virtual
  548. void LLChatBar::refresh()
  549. {
  550. if (!mSecondary)
  551. {
  552. // call superclass setVisible() so that we do not overwrite the saved
  553. // setting
  554. static LLCachedControl<bool> chat_visible(gSavedSettings,
  555. "ChatVisible");
  556. LLPanel::setVisible(chat_visible);
  557. }
  558. // *HACK: Leave the name of the gesture in place for a few seconds.
  559. constexpr F32 SHOW_GESTURE_NAME_TIME = 2.f;
  560. if (mGestureLabelTimer.getStarted() &&
  561. mGestureLabelTimer.getElapsedTimeF32() > SHOW_GESTURE_NAME_TIME)
  562. {
  563. if (mGestureCombo)
  564. {
  565. mGestureCombo->selectFirstItem();
  566. }
  567. mGestureLabelTimer.stop();
  568. }
  569. if (gAgent.getTypingTime() > AGENT_TYPING_TIMEOUT &&
  570. gAgent.getRenderState() & AGENT_STATE_TYPING)
  571. {
  572. gAgent.stopTyping();
  573. }
  574. if (!mSecondary && mHistoryButton)
  575. {
  576. mHistoryButton->setValue(LLFloaterChat::instanceVisible(LLSD()));
  577. }
  578. if (mInputEditor)
  579. {
  580. bool has_text_editor = HBFloaterTextInput::hasFloaterFor(mInputEditor);
  581. bool empty = mInputEditor->getText().size() == 0;
  582. if (empty && !has_text_editor)
  583. {
  584. // Reset this flag if the chat input line is empty
  585. mHasScrolledOnce = false;
  586. }
  587. mInputEditor->setEnabled(!has_text_editor);
  588. if (mSayFlyoutButton)
  589. {
  590. mSayFlyoutButton->setEnabled(!empty && !has_text_editor);
  591. }
  592. if (mGestureCombo)
  593. {
  594. mGestureCombo->setEnabled(!has_text_editor);
  595. }
  596. }
  597. if (mSayFlyoutButton && sSwappedShortcuts != mLastSwappedShortcuts)
  598. {
  599. mLastSwappedShortcuts = sSwappedShortcuts;
  600. if (sSwappedShortcuts)
  601. {
  602. mSayFlyoutButton->setToolTip(getString("swapped_shortcuts"));
  603. }
  604. else
  605. {
  606. mSayFlyoutButton->setToolTip(getString("normal_shortcuts"));
  607. }
  608. }
  609. }
  610. void LLChatBar::refreshGestures()
  611. {
  612. if (mGestureCombo)
  613. {
  614. // Store current selection so we can maintain it
  615. std::string cur_gesture = mGestureCombo->getValue().asString();
  616. mGestureCombo->selectFirstItem();
  617. std::string label = mGestureCombo->getValue().asString();;
  618. // clear
  619. mGestureCombo->clearRows();
  620. // Collect list of unique gestures
  621. std::map <std::string, bool> unique;
  622. LLGestureManager::item_map_t::iterator it;
  623. for (it = gGestureManager.mActive.begin();
  624. it != gGestureManager.mActive.end(); ++it)
  625. {
  626. LLMultiGesture* gesture = (*it).second;
  627. if (gesture)
  628. {
  629. if (!gesture->mTrigger.empty())
  630. {
  631. unique[gesture->mTrigger] = true;
  632. }
  633. }
  634. }
  635. // Add unique gestures
  636. std::map <std::string, bool>::iterator it2;
  637. for (it2 = unique.begin(); it2 != unique.end(); ++it2)
  638. {
  639. mGestureCombo->addSimpleElement((*it2).first);
  640. }
  641. mGestureCombo->sortByName();
  642. // Insert label after sorting, at top, with separator below it
  643. mGestureCombo->addSeparator(ADD_TOP);
  644. mGestureCombo->addSimpleElement(getString("gesture_label"), ADD_TOP);
  645. if (!cur_gesture.empty())
  646. {
  647. mGestureCombo->selectByValue(LLSD(cur_gesture));
  648. }
  649. else
  650. {
  651. mGestureCombo->selectFirstItem();
  652. }
  653. }
  654. }
  655. // Move the cursor to the correct input field.
  656. void LLChatBar::setKeyboardFocus(bool focus)
  657. {
  658. if (focus)
  659. {
  660. if (mInputEditor)
  661. {
  662. mInputEditor->setFocus(true);
  663. mInputEditor->selectAll();
  664. }
  665. }
  666. else if (gFocusMgr.childHasKeyboardFocus(this))
  667. {
  668. if (mInputEditor)
  669. {
  670. mInputEditor->deselect();
  671. }
  672. setFocus(false);
  673. }
  674. }
  675. // Ignore arrow keys in chat bar
  676. void LLChatBar::setIgnoreArrowKeys(bool b)
  677. {
  678. if (mInputEditor)
  679. {
  680. mInputEditor->setIgnoreArrowKeys(b);
  681. }
  682. }
  683. bool LLChatBar::hasTextEditor()
  684. {
  685. return HBFloaterTextInput::hasFloaterFor(mInputEditor);
  686. }
  687. bool LLChatBar::inputEditorHasFocus()
  688. {
  689. return mInputEditor && mInputEditor->hasFocus();
  690. }
  691. std::string LLChatBar::getCurrentChat()
  692. {
  693. return mInputEditor ? mInputEditor->getText() : LLStringUtil::null;
  694. }
  695. void LLChatBar::setGestureCombo(LLComboBox* combo)
  696. {
  697. mGestureCombo = combo;
  698. if (mGestureCombo)
  699. {
  700. mGestureCombo->setCommitCallback(onCommitGesture);
  701. mGestureCombo->setCallbackUserData(this);
  702. // Now register observer since we have a place to put the results
  703. mObserver = new LLChatBarGestureObserver(this);
  704. gGestureManager.addObserver(mObserver);
  705. // Refresh list from current active gestures
  706. refreshGestures();
  707. }
  708. }
  709. // If input of the form "/20foo" or "/20 foo", returns "foo" and channel 20.
  710. // Otherwise returns input and channel 0.
  711. LLWString LLChatBar::stripChannelNumber(const LLWString& mesg, S32* channel)
  712. {
  713. if (mesg[0] == '/' && mesg[1] == '/')
  714. {
  715. // This is a "repeat channel send"
  716. *channel = mLastSpecialChatChannel;
  717. return mesg.substr(2, mesg.length() - 2);
  718. }
  719. else if (mesg[0] == '/' && mesg[1] &&
  720. (LLStringOps::isDigit(mesg[1]) ||
  721. (mesg[1] == '-' && mesg[2] &&
  722. LLStringOps::isDigit(mesg[2]))))
  723. {
  724. // This a special "/20" speak on a channel
  725. S32 pos = 0;
  726. // Copy the channel number into a string
  727. LLWString channel_string;
  728. llwchar c;
  729. do
  730. {
  731. c = mesg[++pos];
  732. channel_string.push_back(c);
  733. }
  734. while (c && pos < 64 &&
  735. (LLStringOps::isDigit(c) || (pos == 1 && c == '-')));
  736. // Move the pointer forward to the first non-whitespace char
  737. // Check isspace before looping, so we can handle "/33foo"
  738. // as well as "/33 foo"
  739. while (c && iswspace(c))
  740. {
  741. c = mesg[++pos];
  742. }
  743. mLastSpecialChatChannel = strtol(wstring_to_utf8str(channel_string).c_str(),
  744. NULL, 10);
  745. *channel = mLastSpecialChatChannel;
  746. return mesg.substr(pos, mesg.length() - pos);
  747. }
  748. else
  749. {
  750. // This is normal chat.
  751. *channel = 0;
  752. return mesg;
  753. }
  754. }
  755. void LLChatBar::sendChat(EChatType type)
  756. {
  757. if (mInputEditor)
  758. {
  759. LLWString text = mInputEditor->getConvertedText();
  760. if (!text.empty())
  761. {
  762. // Store sent line in history, duplicates will get filtered
  763. if (mInputEditor) mInputEditor->updateHistory();
  764. // Check if this is destined for another channel
  765. S32 channel = 0;
  766. stripChannelNumber(text, &channel);
  767. std::string utf8text = wstring_to_utf8str(text);
  768. // Try to trigger a gesture, if not chat to a script.
  769. std::string utf8_revised_text;
  770. if (channel == 0)
  771. {
  772. if (gSavedSettings.getBool("AutoCloseOOC"))
  773. {
  774. // Try to find any unclosed OOC chat (i.e. an opening
  775. // double parenthesis without a matching closing double
  776. // parenthesis.
  777. if (utf8text.find("((") != std::string::npos &&
  778. utf8text.find("))") == std::string::npos)
  779. {
  780. if (utf8text[utf8text.length() - 1] == ')')
  781. {
  782. // Cosmetic: add a space first to avoid a closing
  783. // triple parenthesis
  784. utf8text += " ";
  785. }
  786. // add the missing closing double parenthesis.
  787. utf8text += "))";
  788. }
  789. }
  790. // Convert MU*s style poses into IRC emotes here.
  791. if (gSavedSettings.getBool("AllowMUpose") &&
  792. utf8text.length() > 3 && utf8text[0] == ':')
  793. {
  794. if (utf8text.compare(0, 2, ":'") == 0)
  795. {
  796. utf8text.replace(0, 1, "/me");
  797. }
  798. // Allow a space, for phrases starting with non-ASCII
  799. // characters...
  800. else if (utf8text.compare(0, 2, ": ") == 0)
  801. {
  802. utf8text.replace(0, 1, "/me");
  803. }
  804. // Do not prevent smileys and such.
  805. else if (isalpha(utf8text[1]))
  806. {
  807. utf8text.replace(0, 1, "/me ");
  808. }
  809. }
  810. //MK
  811. bool found_gesture =
  812. //mk
  813. gGestureManager.triggerAndReviseString(utf8text,
  814. &utf8_revised_text);
  815. //MK
  816. if (gRLenabled && gRLInterface.contains("sendchat") &&
  817. !gRLInterface.containsSubstr("redirchat:"))
  818. {
  819. // User is forbidden to send any chat message on channel 0
  820. // except emotes and OOC text
  821. utf8_revised_text = gRLInterface.crunchEmote(utf8_revised_text,
  822. 20);
  823. if (found_gesture && utf8_revised_text == "...")
  824. {
  825. utf8_revised_text.clear();
  826. }
  827. }
  828. //mk
  829. }
  830. else
  831. {
  832. //MK
  833. std::ostringstream stream;
  834. stream << "" << channel;
  835. if (gRLenabled &&
  836. (gRLInterface.contains("sendchannel_except:" +
  837. stream.str()) ||
  838. gRLInterface.containsWithoutException("sendchannel",
  839. stream.str())))
  840. {
  841. utf8_revised_text = "";
  842. }
  843. else
  844. //mk
  845. {
  846. utf8_revised_text = utf8text;
  847. }
  848. }
  849. utf8_revised_text = utf8str_trim(utf8_revised_text);
  850. if (!utf8_revised_text.empty())
  851. {
  852. // Chat with animation
  853. sendChatFromViewer(utf8_revised_text, type, true);
  854. }
  855. }
  856. }
  857. childSetValue("Chat Editor", LLStringUtil::null);
  858. gAgent.stopTyping();
  859. if (gChatBarp == this)
  860. {
  861. if (gSavedSettings.getBool("CloseChatOnReturn"))
  862. {
  863. stopChat();
  864. }
  865. if (gSavedSettings.getBool("AutoFocusChat") &&
  866. gSavedSettings.getBool("ShowChatHistory"))
  867. {
  868. LLFloaterChat::focus();
  869. }
  870. }
  871. }
  872. //static
  873. void LLChatBar::toggleChatHistory(void*)
  874. {
  875. LLFloaterChat::toggleInstance(LLSD());
  876. }
  877. //static
  878. void LLChatBar::startChat(const char* line)
  879. {
  880. if (!gChatBarp) return;
  881. gChatBarp->setVisible(true);
  882. gChatBarp->setKeyboardFocus(true);
  883. gSavedSettings.setBool("ChatVisible", true);
  884. if (gChatBarp->mInputEditor)
  885. {
  886. if (line)
  887. {
  888. std::string line_string(line);
  889. gChatBarp->mInputEditor->setText(line_string);
  890. }
  891. // Always move cursor to end so users do not obliterate chat when
  892. // accidentally hitting WASD
  893. gChatBarp->mInputEditor->setCursorToEnd();
  894. }
  895. }
  896. // Exit "chat mode" and do the appropriate focus changes
  897. //static
  898. void LLChatBar::stopChat()
  899. {
  900. if (!gChatBarp || !gKeyboardp) return;
  901. // In simple UI mode, we never release focus from the chat bar
  902. gChatBarp->setKeyboardFocus(false);
  903. // If we typed a movement key and pressed return during the same frame, the
  904. // keyboard handlers will see the key as having gone down this frame and
  905. // try to move the avatar.
  906. gKeyboardp->resetKeys();
  907. gKeyboardp->resetMaskKeys();
  908. // Stop typing animation
  909. gAgent.stopTyping();
  910. // Hide chat bar so it does not grab focus back
  911. gChatBarp->setVisible(false);
  912. gSavedSettings.setBool("ChatVisible", false);
  913. }
  914. void LLChatBar::setVisible(bool visible)
  915. {
  916. // If this is not the main chat bar, return
  917. if (mSecondary) return;
  918. gSavedSettings.setBool("ChatVisible", visible);
  919. LLPanel::setVisible(visible);
  920. }
  921. //static
  922. void LLChatBar::onInputEditorKeystroke(LLLineEditor* caller, void* userdata)
  923. {
  924. LLChatBar* self = (LLChatBar*)userdata;
  925. if (!self || !gKeyboardp) return;
  926. LLWString raw_text;
  927. if (self->mInputEditor)
  928. {
  929. raw_text = self->mInputEditor->getWText();
  930. }
  931. // Cannot trim the end, because that will cause autocompletion to eat
  932. // trailing spaces that might be part of a gesture.
  933. LLWStringUtil::trimHead(raw_text);
  934. S32 length = raw_text.length();
  935. // Note: forward slash is used for escape (eg. emote) sequences
  936. if (length > 0 && raw_text[0] != '/')
  937. {
  938. gAgent.startTyping();
  939. }
  940. else
  941. {
  942. gAgent.stopTyping();
  943. }
  944. KEY key = gKeyboardp->currentKey();
  945. // Ignore "special" keys, like backspace, arrows, etc.
  946. if (length > 1 && raw_text[0] == '/' && key < KEY_SPECIAL)
  947. {
  948. // We are starting a gesture, attempt to autocomplete
  949. std::string utf8_trigger = wstring_to_utf8str(raw_text);
  950. std::string utf8_out_str(utf8_trigger);
  951. if (gGestureManager.matchPrefix(utf8_trigger, &utf8_out_str))
  952. {
  953. if (self->mInputEditor)
  954. {
  955. std::string rest_of_match =
  956. utf8_out_str.substr(utf8_trigger.size());
  957. // Keep original capitalization for user-entered part:
  958. self->mInputEditor->setText(utf8_trigger + rest_of_match);
  959. // Length in characters
  960. S32 outlength = self->mInputEditor->getLength();
  961. // Select to end of line, starting from the character after the
  962. // last one the user typed.
  963. self->mInputEditor->setSelection(length, outlength);
  964. }
  965. }
  966. }
  967. }
  968. //static
  969. void LLChatBar::onInputEditorScrolled(LLLineEditor* caller, void* userdata)
  970. {
  971. LLChatBar* self = (LLChatBar*)userdata;
  972. if (!self || !caller) return;
  973. if (!self->mHasScrolledOnce &&
  974. gSavedSettings.getBool("AutoOpenTextInput"))
  975. {
  976. self->mHasScrolledOnce = true;
  977. HBFloaterTextInput::show(caller);
  978. }
  979. }
  980. //static
  981. void LLChatBar::onInputEditorFocusLost(LLFocusableElement* caller, void*)
  982. {
  983. // Stop typing animation
  984. gAgent.stopTyping();
  985. }
  986. //static
  987. void LLChatBar::onInputEditorGainFocus(LLFocusableElement* caller, void*)
  988. {
  989. LLFloaterChat::setHistoryCursorAndScrollToEnd();
  990. }
  991. //static
  992. void LLChatBar::onClickSay(LLUICtrl* ctrl, void* userdata)
  993. {
  994. e_chat_type chat_type = CHAT_TYPE_NORMAL;
  995. if (ctrl->getValue().asString() == "shout")
  996. {
  997. chat_type = CHAT_TYPE_SHOUT;
  998. }
  999. else if (ctrl->getValue().asString() == "whisper")
  1000. {
  1001. chat_type = CHAT_TYPE_WHISPER;
  1002. }
  1003. LLChatBar* self = (LLChatBar*) userdata;
  1004. self->sendChat(chat_type);
  1005. }
  1006. //static
  1007. void LLChatBar::onClickOpenTextEditor(void* userdata)
  1008. {
  1009. LLChatBar* self = (LLChatBar*)userdata;
  1010. if (self && self->mInputEditor)
  1011. {
  1012. self->mHasScrolledOnce = true;
  1013. HBFloaterTextInput::show(self->mInputEditor);
  1014. }
  1015. }
  1016. void LLChatBar::sendChatFromViewer(const std::string& utf8text, EChatType type,
  1017. bool animate, bool lua_propagate)
  1018. {
  1019. sendChatFromViewer(utf8str_to_wstring(utf8text), type, animate,
  1020. lua_propagate);
  1021. }
  1022. void LLChatBar::sendChatFromViewer(LLWString wtext, EChatType type,
  1023. bool animate, bool lua_propagate)
  1024. {
  1025. if (type != CHAT_TYPE_START && type != CHAT_TYPE_STOP)
  1026. {
  1027. std::string text = wstring_to_utf8str(wtext);
  1028. static LLCachedControl<std::string> lua_prefix(gSavedSettings,
  1029. "LuaCommandPrefix");
  1030. if (text.find(lua_prefix) == 0)
  1031. {
  1032. HBViewerAutomation::eval(text.substr(std::string(lua_prefix).size()));
  1033. return;
  1034. }
  1035. if (gAutomationp && gAutomationp->onSendChat(text))
  1036. {
  1037. if (text.empty()) return;
  1038. wtext = utf8str_to_wstring(text);
  1039. }
  1040. }
  1041. // Look for "/20 foo" channel chats.
  1042. S32 channel = 0;
  1043. LLWString out_text = stripChannelNumber(wtext, &channel);
  1044. std::string utf8_out_text = wstring_to_utf8str(out_text);
  1045. if (!utf8_out_text.empty())
  1046. {
  1047. utf8_out_text = utf8str_truncate(utf8_out_text, MAX_MSG_STR_LEN);
  1048. }
  1049. std::string utf8_text = wstring_to_utf8str(wtext);
  1050. utf8_text = utf8str_trim(utf8_text);
  1051. if (!utf8_text.empty())
  1052. {
  1053. utf8_text = utf8str_truncate(utf8_text, MAX_STRING - 1);
  1054. }
  1055. //MK
  1056. if (gRLenabled && channel == 0)
  1057. {
  1058. // Transform the type according to chatshout, chatnormal and
  1059. // chatwhisper restrictions
  1060. if (type == CHAT_TYPE_WHISPER && gRLInterface.contains("chatwhisper"))
  1061. {
  1062. type = CHAT_TYPE_NORMAL;
  1063. }
  1064. if (type == CHAT_TYPE_SHOUT && gRLInterface.contains("chatshout"))
  1065. {
  1066. type = CHAT_TYPE_NORMAL;
  1067. }
  1068. if ((type == CHAT_TYPE_SHOUT || type == CHAT_TYPE_NORMAL) &&
  1069. gRLInterface.contains("chatnormal"))
  1070. {
  1071. type = CHAT_TYPE_WHISPER;
  1072. }
  1073. if (gRLInterface.containsSubstr("redirchat:"))
  1074. {
  1075. animate = false;
  1076. }
  1077. }
  1078. else if (gRLenabled && channel != 0)
  1079. {
  1080. std::string chan_str = llformat("%d", channel);
  1081. if (gRLInterface.contains("sendchannel_except:" + chan_str) ||
  1082. gRLInterface.containsWithoutException("sendchannel", chan_str))
  1083. {
  1084. return;
  1085. }
  1086. }
  1087. //mk
  1088. // Do not animate for chats people cannot hear (chat to scripts)
  1089. if (animate && channel == 0)
  1090. {
  1091. LLUUID anim;
  1092. if (type == CHAT_TYPE_WHISPER)
  1093. {
  1094. LL_DEBUGS("SendChat") << "You whisper " << utf8_text << LL_ENDL;
  1095. anim = ANIM_AGENT_WHISPER;
  1096. }
  1097. else if (type == CHAT_TYPE_NORMAL)
  1098. {
  1099. LL_DEBUGS("SendChat") << "You say " << utf8_text << LL_ENDL;
  1100. anim = ANIM_AGENT_TALK;
  1101. }
  1102. else if (type == CHAT_TYPE_SHOUT)
  1103. {
  1104. LL_DEBUGS("SendChat") << "You shout " << utf8_text << LL_ENDL;
  1105. anim = ANIM_AGENT_SHOUT;
  1106. }
  1107. else
  1108. {
  1109. llwarns << "Invalid volume" << llendl;
  1110. return;
  1111. }
  1112. U32 play_anim = gSavedSettings.getU32("PlayChatAnims");
  1113. if (play_anim == 0)
  1114. {
  1115. animate = false;
  1116. }
  1117. else if (play_anim == 1)
  1118. {
  1119. if ((utf8_out_text.find("/me ") == 0 ||
  1120. utf8_out_text.find("/me'") == 0) &&
  1121. utf8_out_text.find('"') == std::string::npos)
  1122. {
  1123. // Do not animate for pure emotes
  1124. animate = false;
  1125. }
  1126. }
  1127. if (animate)
  1128. {
  1129. gAgent.sendAnimationRequest(anim, ANIM_REQUEST_START);
  1130. }
  1131. }
  1132. if (channel != 0 && type != CHAT_TYPE_START && type != CHAT_TYPE_STOP)
  1133. {
  1134. LL_DEBUGS("SendChat") << "Chat channel: " << channel << " - Text: "
  1135. << utf8_text << LL_ENDL;
  1136. }
  1137. send_chat_from_viewer(utf8_out_text, type, channel);
  1138. }
  1139. //static
  1140. void LLChatBar::onCommitGesture(LLUICtrl* ctrl, void* data)
  1141. {
  1142. LLChatBar* self = (LLChatBar*)data;
  1143. if (self->mGestureCombo)
  1144. {
  1145. S32 index = self->mGestureCombo->getFirstSelectedIndex();
  1146. if (index == 0)
  1147. {
  1148. return;
  1149. }
  1150. const std::string& trigger =
  1151. self->mGestureCombo->getSelectedValue().asString();
  1152. //MK
  1153. if (!gRLenabled || !gRLInterface.contains("sendchat"))
  1154. {
  1155. //mk
  1156. // Pretend the user chatted the trigger string, to invoke
  1157. // substitution and logging.
  1158. std::string text(trigger);
  1159. std::string revised_text;
  1160. gGestureManager.triggerAndReviseString(text, &revised_text);
  1161. revised_text = utf8str_trim(revised_text);
  1162. if (!revised_text.empty())
  1163. {
  1164. // Do not play the nodding animation
  1165. self->sendChatFromViewer(revised_text, CHAT_TYPE_NORMAL,
  1166. false);
  1167. }
  1168. //MK
  1169. }
  1170. //mk
  1171. }
  1172. self->mGestureLabelTimer.start();
  1173. if (self->mGestureCombo != NULL)
  1174. {
  1175. // Free focus back to chat bar
  1176. self->mGestureCombo->setFocus(false);
  1177. }
  1178. }
  1179. // Helper function
  1180. static void capitalize(std::string& str)
  1181. {
  1182. if (str.empty())
  1183. {
  1184. return; // Nothing to do.
  1185. }
  1186. // Do not attempt to capitalize non-ASCII characters, of characters which
  1187. // are already capitalized. HB
  1188. char& c = str.front();
  1189. if (c >= 'a' && c <= 'z')
  1190. {
  1191. c -= 'a' - 'A';
  1192. }
  1193. }
  1194. //static
  1195. std::string LLChatBar::getMatchingAvatarName(const std::string& match)
  1196. {
  1197. std::string suggestion = match;
  1198. //MK
  1199. if (gRLenabled && gRLInterface.mContainsShownames)
  1200. {
  1201. return suggestion;
  1202. }
  1203. //mk
  1204. bool add_to_ignore = LLSpellCheck::getInstance()->getSpellCheck() &&
  1205. gSavedSettings.getBool("AddAvatarNamesToIgnore");
  1206. uuid_vec_t avatars;
  1207. std::vector<LLVector3d> positions;
  1208. gWorld.getAvatars(avatars, &positions, NULL, gAgent.getPositionGlobal(),
  1209. gSavedSettings.getF32("NearMeRange"));
  1210. av_names_list_t matches;
  1211. std::string longest_match;
  1212. size_t len = 0;
  1213. std::string first_name, last_name, display_name, name, part_name;
  1214. std::string pattern = match;
  1215. LLStringUtil::toLower(pattern);
  1216. LLAvatarName avatar_name;
  1217. for (uuid_vec_t::iterator it = avatars.begin(); it != avatars.end(); ++it)
  1218. {
  1219. const LLUUID id = *it;
  1220. if (gCacheNamep && gCacheNamep->getName(id, first_name, last_name))
  1221. {
  1222. name = first_name;
  1223. LLStringUtil::toLower(name);
  1224. if (name.length() && name.find(pattern) == 0)
  1225. {
  1226. LL_DEBUGS("NameAutoCompletion") << "Inserting matching first name: "
  1227. << first_name << LL_ENDL;
  1228. matches.emplace(first_name);
  1229. if (name.length() > len)
  1230. {
  1231. len = name.length();
  1232. longest_match = first_name;
  1233. }
  1234. }
  1235. if (last_name.length() && last_name != "Resident")
  1236. {
  1237. name = last_name;
  1238. LLStringUtil::toLower(name);
  1239. if (name.find(pattern) == 0)
  1240. {
  1241. LL_DEBUGS("NameAutoCompletion") << "Inserting matching last name: "
  1242. << last_name << LL_ENDL;
  1243. matches.emplace(last_name);
  1244. if (name.length() > len)
  1245. {
  1246. len = name.length();
  1247. longest_match = last_name;
  1248. }
  1249. }
  1250. }
  1251. else
  1252. {
  1253. last_name.clear();
  1254. }
  1255. }
  1256. else
  1257. {
  1258. first_name.clear();
  1259. last_name.clear();
  1260. }
  1261. display_name.clear();
  1262. part_name.clear();
  1263. if (LLAvatarNameCache::useDisplayNames() &&
  1264. LLAvatarNameCache::get(id, &avatar_name))
  1265. {
  1266. display_name = avatar_name.mDisplayName;
  1267. if (display_name != first_name &&
  1268. display_name != first_name + " " + last_name)
  1269. {
  1270. name = display_name;
  1271. LLStringUtil::toLower(name);
  1272. size_t i = name.find(' ');
  1273. if (i > 0)
  1274. {
  1275. part_name = display_name.substr(0, i);
  1276. name = name.substr(0, i);
  1277. if (name.length() && name.find(pattern) == 0)
  1278. {
  1279. LL_DEBUGS("NameAutoCompletion") << "Inserting matching first part of display name: "
  1280. << part_name
  1281. << LL_ENDL;
  1282. matches.emplace(part_name);
  1283. if (name.length() > len)
  1284. {
  1285. len = name.length();
  1286. longest_match = part_name;
  1287. }
  1288. }
  1289. display_name = display_name.substr(i + 1);
  1290. name = display_name;
  1291. LLStringUtil::toLower(name);
  1292. }
  1293. if (name.length() && name.find(pattern) == 0)
  1294. {
  1295. LL_DEBUGS("NameAutoCompletion") << "Inserting matching display name: "
  1296. << display_name << LL_ENDL;
  1297. matches.emplace(display_name);
  1298. if (name.length() > len)
  1299. {
  1300. len = name.length();
  1301. longest_match = display_name;
  1302. }
  1303. }
  1304. }
  1305. else
  1306. {
  1307. display_name.clear();
  1308. }
  1309. }
  1310. if (add_to_ignore)
  1311. {
  1312. name = first_name + " " + last_name + " " + part_name + " " +
  1313. display_name;
  1314. // Display names can change, so don't rely on avatar UUIDs
  1315. if (!sIgnoredNames.count(name))
  1316. {
  1317. LL_DEBUGS("NameAutoCompletion") << "Adding names to the ignore list: "
  1318. << name << LL_ENDL;
  1319. sIgnoredNames.emplace(name);
  1320. LLSpellCheck::getInstance()->addWordsToIgnoreList(name);
  1321. }
  1322. }
  1323. }
  1324. if (matches.size() == 1)
  1325. {
  1326. suggestion = *(matches.begin());
  1327. LL_DEBUGS("NameAutoCompletion") << "Only one match found: "
  1328. << suggestion << LL_ENDL;
  1329. }
  1330. else if (matches.size() > 1)
  1331. {
  1332. // Find the first common letters for all matches.
  1333. for (size_t i = match.length(); i <= len; ++i)
  1334. {
  1335. pattern = utf8str_truncate(longest_match, (S32)i);
  1336. LLStringUtil::toLower(pattern);
  1337. for (av_names_list_t::iterator it = matches.begin();
  1338. it != matches.end(); ++it)
  1339. {
  1340. name = *it;
  1341. LLStringUtil::toLower(name);
  1342. if (name.find(pattern) != 0)
  1343. {
  1344. capitalize(suggestion);
  1345. return suggestion;
  1346. }
  1347. }
  1348. suggestion = utf8str_truncate(longest_match, (S32)i);
  1349. }
  1350. LL_DEBUGS("NameAutoCompletion") << "Several matches found, returning the common letters: "
  1351. << suggestion << LL_ENDL;
  1352. }
  1353. else
  1354. {
  1355. LL_DEBUGS("NameAutoCompletion") << "No match found, returning the search string: "
  1356. << suggestion << LL_ENDL;
  1357. }
  1358. capitalize(suggestion);
  1359. return suggestion;
  1360. }