llfloaterdisplayname.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @file llfloaterdisplayname.cpp
  3. * @author Leyla Farazha
  4. * @brief Implementation of the LLFloaterDisplayName class.
  5. *
  6. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include "llviewerprecompiledheaders.h"
  28. #include "llfloaterdisplayname.h"
  29. #include "llavatarnamecache.h"
  30. #include "llnotifications.h"
  31. #include "lluictrlfactory.h"
  32. #include "llagent.h"
  33. #include "llviewercontrol.h"
  34. #include "llviewerdisplayname.h"
  35. #include "llviewermessage.h" // For formatted_time()
  36. LLFloaterDisplayName::LLFloaterDisplayName(const LLSD&)
  37. {
  38. LLUICtrlFactory::getInstance()->buildFloater(this,
  39. "floater_display_name.xml");
  40. }
  41. //virtual
  42. bool LLFloaterDisplayName::postBuild()
  43. {
  44. childSetAction("reset_btn", onReset, this);
  45. childSetAction("cancel_btn", onCancel, this);
  46. childSetAction("save_btn", onSave, this);
  47. center();
  48. return true;
  49. }
  50. //virtual
  51. void LLFloaterDisplayName::onOpen()
  52. {
  53. getChild<LLUICtrl>("display_name_editor")->clear();
  54. getChild<LLUICtrl>("display_name_confirm")->clear();
  55. LLAvatarName av_name;
  56. LLAvatarNameCache::get(gAgentID, &av_name);
  57. F64 now_secs = LLTimer::getEpochSeconds();
  58. if (now_secs < av_name.mNextUpdate)
  59. {
  60. // ... cannot update until some time in the future
  61. F64 next_update_local_secs = av_name.mNextUpdate;
  62. std::string next_update_string =
  63. formatted_time((time_t)next_update_local_secs);
  64. getChild<LLUICtrl>("lockout_text")->setTextArg("[TIME]",
  65. next_update_string);
  66. getChild<LLUICtrl>("lockout_text")->setVisible(true);
  67. getChild<LLUICtrl>("now_ok_text")->setVisible(false);
  68. getChild<LLUICtrl>("save_btn")->setEnabled(false);
  69. getChild<LLUICtrl>("display_name_editor")->setEnabled(false);
  70. getChild<LLUICtrl>("display_name_confirm")->setEnabled(false);
  71. getChild<LLUICtrl>("cancel_btn")->setFocus(true);
  72. }
  73. else
  74. {
  75. getChild<LLUICtrl>("lockout_text")->setVisible(false);
  76. getChild<LLUICtrl>("now_ok_text")->setVisible(true);
  77. getChild<LLUICtrl>("save_btn")->setEnabled(true);
  78. getChild<LLUICtrl>("display_name_editor")->setEnabled(true);
  79. getChild<LLUICtrl>("display_name_confirm")->setEnabled(true);
  80. }
  81. }
  82. //static
  83. void LLFloaterDisplayName::onCacheSetName(bool success,
  84. const std::string& reason,
  85. const LLSD& content)
  86. {
  87. if (success)
  88. {
  89. // Inform the user that the change took place, but will take a while
  90. // to percolate.
  91. LLSD args;
  92. args["DISPLAY_NAME"] = content["display_name"];
  93. gNotifications.add("SetDisplayNameSuccess", args);
  94. return;
  95. }
  96. // Request failed, notify the user
  97. std::string error_tag = content["error_tag"].asString();
  98. llwarns << "Set name failure error_tag: " << error_tag << llendl;
  99. // We might have a localized string for this message; error_args will
  100. // usually be empty from the server.
  101. if (!error_tag.empty() && gNotifications.templateExists(error_tag))
  102. {
  103. gNotifications.add(error_tag);
  104. return;
  105. }
  106. // The server error might have a localized message for us
  107. std::string lang_code = LLUI::getLanguage();
  108. LLSD error_desc = content["error_description"];
  109. if (error_desc.has(lang_code))
  110. {
  111. LLSD args;
  112. args["MESSAGE"] = error_desc[lang_code].asString();
  113. gNotifications.add("GenericAlert", args);
  114. return;
  115. }
  116. // No specific error, throw a generic one
  117. gNotifications.add("SetDisplayNameFailedGeneric");
  118. }
  119. //static
  120. void LLFloaterDisplayName::onCancel(void* data)
  121. {
  122. LLFloaterDisplayName* self = (LLFloaterDisplayName*)data;
  123. if (self)
  124. {
  125. self->close();
  126. }
  127. }
  128. //static
  129. void LLFloaterDisplayName::onReset(void* data)
  130. {
  131. LLFloaterDisplayName* self = (LLFloaterDisplayName*)data;
  132. if (!self) return;
  133. if (LLAvatarNameCache::useDisplayNames())
  134. {
  135. LLViewerDisplayName::set("",
  136. boost::bind(&LLFloaterDisplayName::onCacheSetName,
  137. _1, _2, _3));
  138. }
  139. else
  140. {
  141. gNotifications.add("SetDisplayNameFailedGeneric");
  142. }
  143. self->close();
  144. }
  145. //static
  146. void LLFloaterDisplayName::onSave(void* data)
  147. {
  148. LLFloaterDisplayName* self = (LLFloaterDisplayName*)data;
  149. if (!self) return;
  150. std::string display_name_utf8;
  151. display_name_utf8 = self->getChild<LLUICtrl>("display_name_editor")->getValue().asString();
  152. std::string display_name_confirm;
  153. display_name_confirm = self->getChild<LLUICtrl>("display_name_confirm")->getValue().asString();
  154. if (display_name_utf8.compare(display_name_confirm))
  155. {
  156. gNotifications.add("SetDisplayNameMismatch");
  157. return;
  158. }
  159. constexpr U32 DISPLAY_NAME_MAX_LENGTH = 31; // Characters, not bytes
  160. LLWString display_name_wstr = utf8str_to_wstring(display_name_utf8);
  161. if (display_name_wstr.size() > DISPLAY_NAME_MAX_LENGTH)
  162. {
  163. LLSD args;
  164. args["LENGTH"] = llformat("%d", DISPLAY_NAME_MAX_LENGTH);
  165. gNotifications.add("SetDisplayNameFailedLength", args);
  166. return;
  167. }
  168. if (LLAvatarNameCache::useDisplayNames())
  169. {
  170. LLViewerDisplayName::set(display_name_utf8,
  171. boost::bind(&LLFloaterDisplayName::onCacheSetName,
  172. _1, _2, _3));
  173. }
  174. else
  175. {
  176. gNotifications.add("SetDisplayNameFailedGeneric");
  177. }
  178. self->close();
  179. }