PageRenderTime 200ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llfloaterbuycurrency.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 305 lines | 214 code | 51 blank | 40 comment | 12 complexity | a29546a864339a36b8f562b2f9d65ae3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llfloaterbuycurrency.cpp
  3. * @brief LLFloaterBuyCurrency class implementation
  4. *
  5. * $LicenseInfo:firstyear=2005&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 "llfloaterbuycurrency.h"
  28. // viewer includes
  29. #include "llcurrencyuimanager.h"
  30. #include "llfloater.h"
  31. #include "llfloaterreg.h"
  32. #include "llnotificationsutil.h"
  33. #include "llstatusbar.h"
  34. #include "lltextbox.h"
  35. #include "llviewchildren.h"
  36. #include "llviewerwindow.h"
  37. #include "lluictrlfactory.h"
  38. #include "llweb.h"
  39. #include "llwindow.h"
  40. #include "llappviewer.h"
  41. static const S32 STANDARD_BUY_AMOUNT = 2000;
  42. static const S32 MINIMUM_BALANCE_AMOUNT = 0;
  43. class LLFloaterBuyCurrencyUI
  44. : public LLFloater
  45. {
  46. public:
  47. LLFloaterBuyCurrencyUI(const LLSD& key);
  48. virtual ~LLFloaterBuyCurrencyUI();
  49. public:
  50. LLViewChildren mChildren;
  51. LLCurrencyUIManager mManager;
  52. bool mHasTarget;
  53. std::string mTargetName;
  54. S32 mTargetPrice;
  55. public:
  56. void noTarget();
  57. void target(const std::string& name, S32 price);
  58. virtual BOOL postBuild();
  59. void updateUI();
  60. virtual void draw();
  61. virtual BOOL canClose();
  62. void onClickBuy();
  63. void onClickCancel();
  64. void onClickErrorWeb();
  65. };
  66. LLFloater* LLFloaterBuyCurrency::buildFloater(const LLSD& key)
  67. {
  68. LLFloaterBuyCurrencyUI* floater = new LLFloaterBuyCurrencyUI(key);
  69. return floater;
  70. }
  71. #if LL_WINDOWS
  72. // passing 'this' during construction generates a warning. The callee
  73. // only uses the pointer to hold a reference to 'this' which is
  74. // already valid, so this call does the correct thing. Disable the
  75. // warning so that we can compile without generating a warning.
  76. #pragma warning(disable : 4355)
  77. #endif
  78. LLFloaterBuyCurrencyUI::LLFloaterBuyCurrencyUI(const LLSD& key)
  79. : LLFloater(key),
  80. mChildren(*this),
  81. mManager(*this)
  82. {
  83. }
  84. LLFloaterBuyCurrencyUI::~LLFloaterBuyCurrencyUI()
  85. {
  86. }
  87. void LLFloaterBuyCurrencyUI::noTarget()
  88. {
  89. mHasTarget = false;
  90. mManager.setAmount(STANDARD_BUY_AMOUNT);
  91. }
  92. void LLFloaterBuyCurrencyUI::target(const std::string& name, S32 price)
  93. {
  94. mHasTarget = true;
  95. mTargetName = name;
  96. mTargetPrice = price;
  97. S32 balance = gStatusBar->getBalance();
  98. S32 need = price - balance;
  99. if (need < 0)
  100. {
  101. need = 0;
  102. }
  103. mManager.setAmount(need + MINIMUM_BALANCE_AMOUNT);
  104. }
  105. // virtual
  106. BOOL LLFloaterBuyCurrencyUI::postBuild()
  107. {
  108. mManager.prepare();
  109. getChild<LLUICtrl>("buy_btn")->setCommitCallback( boost::bind(&LLFloaterBuyCurrencyUI::onClickBuy, this));
  110. getChild<LLUICtrl>("cancel_btn")->setCommitCallback( boost::bind(&LLFloaterBuyCurrencyUI::onClickCancel, this));
  111. getChild<LLUICtrl>("error_web")->setCommitCallback( boost::bind(&LLFloaterBuyCurrencyUI::onClickErrorWeb, this));
  112. center();
  113. updateUI();
  114. return TRUE;
  115. }
  116. void LLFloaterBuyCurrencyUI::draw()
  117. {
  118. if (mManager.process())
  119. {
  120. if (mManager.bought())
  121. {
  122. LLNotificationsUtil::add("BuyLindenDollarSuccess");
  123. closeFloater();
  124. return;
  125. }
  126. updateUI();
  127. }
  128. // disable the Buy button when we are not able to buy
  129. getChildView("buy_btn")->setEnabled(mManager.canBuy());
  130. LLFloater::draw();
  131. }
  132. BOOL LLFloaterBuyCurrencyUI::canClose()
  133. {
  134. return mManager.canCancel();
  135. }
  136. void LLFloaterBuyCurrencyUI::updateUI()
  137. {
  138. bool hasError = mManager.hasError();
  139. mManager.updateUI(!hasError && !mManager.buying());
  140. // hide most widgets - we'll turn them on as needed next
  141. getChildView("info_buying")->setVisible(FALSE);
  142. getChildView("info_cannot_buy")->setVisible(FALSE);
  143. getChildView("info_need_more")->setVisible(FALSE);
  144. getChildView("purchase_warning_repurchase")->setVisible(FALSE);
  145. getChildView("purchase_warning_notenough")->setVisible(FALSE);
  146. getChildView("contacting")->setVisible(FALSE);
  147. getChildView("buy_action")->setVisible(FALSE);
  148. if (hasError)
  149. {
  150. // display an error from the server
  151. getChildView("normal_background")->setVisible(FALSE);
  152. getChildView("error_background")->setVisible(TRUE);
  153. getChildView("info_cannot_buy")->setVisible(TRUE);
  154. getChildView("cannot_buy_message")->setVisible(TRUE);
  155. getChildView("balance_label")->setVisible(FALSE);
  156. getChildView("balance_amount")->setVisible(FALSE);
  157. getChildView("buying_label")->setVisible(FALSE);
  158. getChildView("buying_amount")->setVisible(FALSE);
  159. getChildView("total_label")->setVisible(FALSE);
  160. getChildView("total_amount")->setVisible(FALSE);
  161. LLTextBox* message = getChild<LLTextBox>("cannot_buy_message");
  162. if (message)
  163. {
  164. message->setText(mManager.errorMessage());
  165. }
  166. getChildView("error_web")->setVisible( !mManager.errorURI().empty());
  167. }
  168. else
  169. {
  170. // display the main Buy L$ interface
  171. getChildView("normal_background")->setVisible(TRUE);
  172. getChildView("error_background")->setVisible(FALSE);
  173. getChildView("cannot_buy_message")->setVisible(FALSE);
  174. getChildView("error_web")->setVisible(FALSE);
  175. if (mHasTarget)
  176. {
  177. getChildView("info_need_more")->setVisible(TRUE);
  178. }
  179. else
  180. {
  181. getChildView("info_buying")->setVisible(TRUE);
  182. }
  183. if (mManager.buying())
  184. {
  185. getChildView("contacting")->setVisible( true);
  186. }
  187. else
  188. {
  189. if (mHasTarget)
  190. {
  191. getChildView("buy_action")->setVisible( true);
  192. getChild<LLUICtrl>("buy_action")->setTextArg("[ACTION]", mTargetName);
  193. }
  194. }
  195. S32 balance = gStatusBar->getBalance();
  196. getChildView("balance_label")->setVisible(TRUE);
  197. getChildView("balance_amount")->setVisible(TRUE);
  198. getChild<LLUICtrl>("balance_amount")->setTextArg("[AMT]", llformat("%d", balance));
  199. S32 buying = mManager.getAmount();
  200. getChildView("buying_label")->setVisible(TRUE);
  201. getChildView("buying_amount")->setVisible(TRUE);
  202. getChild<LLUICtrl>("buying_amount")->setTextArg("[AMT]", llformat("%d", buying));
  203. S32 total = balance + buying;
  204. getChildView("total_label")->setVisible(TRUE);
  205. getChildView("total_amount")->setVisible(TRUE);
  206. getChild<LLUICtrl>("total_amount")->setTextArg("[AMT]", llformat("%d", total));
  207. if (mHasTarget)
  208. {
  209. if (total >= mTargetPrice)
  210. {
  211. getChildView("purchase_warning_repurchase")->setVisible( true);
  212. }
  213. else
  214. {
  215. getChildView("purchase_warning_notenough")->setVisible( true);
  216. }
  217. }
  218. }
  219. getChildView("getting_data")->setVisible( !mManager.canBuy() && !hasError);
  220. }
  221. void LLFloaterBuyCurrencyUI::onClickBuy()
  222. {
  223. mManager.buy(getString("buy_currency"));
  224. updateUI();
  225. // Update L$ balance
  226. LLStatusBar::sendMoneyBalanceRequest();
  227. }
  228. void LLFloaterBuyCurrencyUI::onClickCancel()
  229. {
  230. closeFloater();
  231. // Update L$ balance
  232. LLStatusBar::sendMoneyBalanceRequest();
  233. }
  234. void LLFloaterBuyCurrencyUI::onClickErrorWeb()
  235. {
  236. LLWeb::loadURLExternal(mManager.errorURI());
  237. closeFloater();
  238. // Update L$ balance
  239. LLStatusBar::sendMoneyBalanceRequest();
  240. }
  241. // static
  242. void LLFloaterBuyCurrency::buyCurrency()
  243. {
  244. LLFloaterBuyCurrencyUI* ui = LLFloaterReg::showTypedInstance<LLFloaterBuyCurrencyUI>("buy_currency");
  245. ui->noTarget();
  246. ui->updateUI();
  247. }
  248. // static
  249. void LLFloaterBuyCurrency::buyCurrency(const std::string& name, S32 price)
  250. {
  251. LLFloaterBuyCurrencyUI* ui = LLFloaterReg::showTypedInstance<LLFloaterBuyCurrencyUI>("buy_currency");
  252. ui->target(name, price);
  253. ui->updateUI();
  254. }