hbfloateruserauth.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @file hbfloateruserauth.cpp
  3. * @brief The HBFloaterUserAuth class definition
  4. *
  5. * $LicenseInfo:firstyear=2015&license=viewergpl$
  6. *
  7. * Copyright (c) 2015, Henri Beauchamp
  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 "linden_common.h"
  33. #include "hbfloateruserauth.h"
  34. #include "llapp.h" // For isQuitting()
  35. #include "llbutton.h"
  36. #include "llcheckboxctrl.h"
  37. #include "lllineeditor.h"
  38. #include "lluictrlfactory.h"
  39. // NOTE: we allow an empty password field, since it might be a valid login.
  40. #define ALLOW_EMPTY_PASSWORD 1
  41. //static
  42. uuid_list_t HBFloaterUserAuth::sAuthList;
  43. //static
  44. void HBFloaterUserAuth::request(const std::string& host,
  45. const std::string& realm,
  46. const LLUUID& auth_id,
  47. HBFloaterUserAuthCallback callback)
  48. {
  49. if (!sAuthList.count(auth_id))
  50. {
  51. (void)new HBFloaterUserAuth(host, realm, auth_id, callback);
  52. }
  53. }
  54. HBFloaterUserAuth::HBFloaterUserAuth(const std::string& host,
  55. const std::string& realm,
  56. const LLUUID& auth_id,
  57. HBFloaterUserAuthCallback callback)
  58. : mUserAuthCallback(callback),
  59. mAuthId(auth_id),
  60. mHost(host),
  61. mRealm(realm),
  62. mMustClose(false),
  63. mCallbackDone(false)
  64. {
  65. sAuthList.emplace(auth_id);
  66. LLUICtrlFactory::getInstance()->buildFloater(this,
  67. "floater_user_auth.xml");
  68. }
  69. //virtual
  70. HBFloaterUserAuth::~HBFloaterUserAuth()
  71. {
  72. if (!mCallbackDone && !LLApp::isQuitting())
  73. {
  74. doCallback(false);
  75. }
  76. sAuthList.erase(mAuthId);
  77. }
  78. //virtual
  79. bool HBFloaterUserAuth::postBuild()
  80. {
  81. mUserNameInputLine = getChild<LLLineEditor>("user_name");
  82. mUserNameInputLine->setOnHandleKeyCallback(onHandleKeyCallback, this);
  83. mUserNameInputLine->setKeystrokeCallback(onKeystrokeCallback);
  84. mUserNameInputLine->setCallbackUserData(this);
  85. mPasswordInputLine = getChild<LLLineEditor>("password");
  86. mPasswordInputLine->setOnHandleKeyCallback(onHandleKeyCallback, this);
  87. mPasswordInputLine->setCallbackUserData(this);
  88. mPasswordInputLine->setDrawAsterixes(true);
  89. childSetCommitCallback("show_password", onCommitCheckBox, this);
  90. mOKBtn = getChild<LLButton>("ok");
  91. mOKBtn->setClickedCallback(onButtonOK, this);
  92. mOKBtn->setEnabled(false);
  93. childSetAction("cancel", onButtonCancel, this);
  94. setTitle(getTitle() + " " + mHost);
  95. childSetTextArg("prompt_text", "[REALM]", mRealm);
  96. center();
  97. return true;
  98. }
  99. //virtual
  100. void HBFloaterUserAuth::draw()
  101. {
  102. if (mMustClose)
  103. {
  104. onButtonOK(this);
  105. }
  106. else
  107. {
  108. LLFloater::draw();
  109. }
  110. }
  111. void HBFloaterUserAuth::doCallback(bool validated)
  112. {
  113. if (!mCallbackDone)
  114. {
  115. if (mUserAuthCallback)
  116. {
  117. mUserAuthCallback(mAuthId, mUserNameInputLine->getText(),
  118. mPasswordInputLine->getText(), validated);
  119. }
  120. mCallbackDone = true;
  121. }
  122. }
  123. //static
  124. void HBFloaterUserAuth::onButtonOK(void* user_data)
  125. {
  126. HBFloaterUserAuth* self = (HBFloaterUserAuth*)user_data;
  127. if (self)
  128. {
  129. self->doCallback(true);
  130. self->close();
  131. }
  132. }
  133. //static
  134. void HBFloaterUserAuth::onButtonCancel(void* user_data)
  135. {
  136. HBFloaterUserAuth* self = (HBFloaterUserAuth*)user_data;
  137. if (self)
  138. {
  139. self->close();
  140. }
  141. }
  142. //static
  143. void HBFloaterUserAuth::onCommitCheckBox(LLUICtrl* ctrl, void* user_data)
  144. {
  145. HBFloaterUserAuth* self = (HBFloaterUserAuth*)user_data;
  146. if (self && ctrl)
  147. {
  148. LLCheckBoxCtrl* check = (LLCheckBoxCtrl*)ctrl;
  149. self->mPasswordInputLine->setDrawAsterixes(!check->get());
  150. #if 0 // Does not work...
  151. self->mPasswordInputLine->setFocus(true);
  152. #endif
  153. }
  154. }
  155. //static
  156. bool HBFloaterUserAuth::onHandleKeyCallback(KEY key, MASK mask,
  157. LLLineEditor* caller,
  158. void* user_data)
  159. {
  160. bool handled = false;
  161. HBFloaterUserAuth* self = (HBFloaterUserAuth*)user_data;
  162. if (self && key == KEY_RETURN && mask == MASK_NONE &&
  163. !self->mUserNameInputLine->getText().empty())
  164. {
  165. if (caller == self->mUserNameInputLine)
  166. {
  167. self->mPasswordInputLine->setFocus(true);
  168. handled = true;
  169. }
  170. #if ALLOW_EMPTY_PASSWORD
  171. else
  172. #else
  173. else if (!self->mPasswordInputLine->getText().empty())
  174. #endif
  175. {
  176. self->mMustClose = true;
  177. handled = true;
  178. }
  179. }
  180. return handled;
  181. }
  182. //static
  183. void HBFloaterUserAuth::onKeystrokeCallback(LLLineEditor*, void* user_data)
  184. {
  185. HBFloaterUserAuth* self = (HBFloaterUserAuth*)user_data;
  186. if (self)
  187. {
  188. bool cant_validate =
  189. #if !ALLOW_EMPTY_PASSWORD
  190. self->mPasswordInputLine->getText().empty()) ||
  191. #endif
  192. self->mUserNameInputLine->getText().empty();
  193. self->mOKBtn->setEnabled(!cant_validate);
  194. }
  195. }