llfloaterbuycurrency.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * @file llfloaterbuycurrency.cpp
  3. * @brief LLFloaterBuyCurrency class implementation
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewergpl$
  6. *
  7. * Copyright (c) 2005-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 "llfloaterbuycurrency.h"
  34. #include "lltextbox.h"
  35. #include "lluictrlfactory.h"
  36. #include "llappviewer.h"
  37. #include "llcommandhandler.h"
  38. #include "llstatusbar.h"
  39. #include "llweb.h"
  40. #if LL_WINDOWS
  41. // passing 'this' during construction generates a warning. The callee only uses
  42. // the pointer to hold a reference to 'this' which is already valid, so this
  43. // call does the correct thing. Disable the warning so that we can compile
  44. // without generating a warning.
  45. #pragma warning(disable : 4355)
  46. #endif
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // Command handler for SLURLs in the form of:
  49. // secondlife:///app/buycurrencyhtml/{ACTION}/{NEXT_ACTION}/{RETURN_CODE}
  50. // Note: we do not have the HTML floater in v1 viewers (and I do not see any
  51. // point in implementing it), so we always use the XUI-based buy currency
  52. // floater instead. HB
  53. class LLBuyCurrencyHandler final : public LLCommandHandler
  54. {
  55. protected:
  56. LOG_CLASS(LLBuyCurrencyHandler);
  57. public:
  58. LLBuyCurrencyHandler()
  59. : LLCommandHandler("buycurrencyhtml", UNTRUSTED_THROTTLE)
  60. {
  61. }
  62. bool handle(const LLSD& params, const LLSD&, LLMediaCtrl*) override
  63. {
  64. S32 count = params.size();
  65. std::string action;
  66. if (count)
  67. {
  68. action = params[0].asString();
  69. }
  70. // Note: we do not care about "NEXT_ACTION", because it may only be
  71. // "open_legacy", and we always open the legacy floater anyway... HB
  72. S32 result_code = 0;
  73. if (count >= 3)
  74. {
  75. result_code = params[2].asInteger();
  76. if (result_code)
  77. {
  78. llwarns <<" Received non-zero result code: " << result_code
  79. << llendl;
  80. }
  81. }
  82. // If the requested action is anything else than "close" (which would
  83. // apply only to the HTML floater version of the buy currency, in LL's
  84. // viewer), then open the legacy floater. HB
  85. if (action != "close")
  86. {
  87. LLFloaterBuyCurrency::buyCurrency();
  88. }
  89. return true;
  90. }
  91. };
  92. LLBuyCurrencyHandler gBuyCurrencyHandler;
  93. ///////////////////////////////////////////////////////////////////////////////
  94. constexpr S32 STANDARD_BUY_AMOUNT = 2000;
  95. constexpr S32 MINIMUM_BALANCE_AMOUNT = 0;
  96. LLFloaterBuyCurrency::LLFloaterBuyCurrency(const LLSD&)
  97. : mManager(*this)
  98. {
  99. LLUICtrlFactory::getInstance()->buildFloater(this,
  100. "floater_buy_currency.xml",
  101. NULL, false);
  102. }
  103. //virtual
  104. bool LLFloaterBuyCurrency::postBuild()
  105. {
  106. mManager.prepare();
  107. childSetAction("buy_btn", onClickBuy, this);
  108. childSetAction("cancel_btn", onClickCancel, this);
  109. childSetAction("error_web", onClickErrorWeb, this);
  110. updateUI();
  111. center();
  112. return true;
  113. }
  114. //virtual
  115. bool LLFloaterBuyCurrency::canClose()
  116. {
  117. return mManager.canCancel();
  118. }
  119. //virtual
  120. void LLFloaterBuyCurrency::draw()
  121. {
  122. if (mManager.process())
  123. {
  124. if (mManager.bought())
  125. {
  126. close();
  127. return;
  128. }
  129. updateUI();
  130. }
  131. LLFloater::draw();
  132. }
  133. void LLFloaterBuyCurrency::noTarget()
  134. {
  135. mHasTarget = false;
  136. mManager.setAmount(STANDARD_BUY_AMOUNT);
  137. }
  138. void LLFloaterBuyCurrency::target(const std::string& name, S32 price)
  139. {
  140. mHasTarget = true;
  141. mTargetName = name;
  142. mTargetPrice = price;
  143. S32 balance = gStatusBarp->getBalance();
  144. S32 need = price - balance;
  145. if (need < 0)
  146. {
  147. need = 0;
  148. }
  149. mManager.setAmount(need + MINIMUM_BALANCE_AMOUNT);
  150. }
  151. void LLFloaterBuyCurrency::updateUI()
  152. {
  153. bool has_error = mManager.hasError();
  154. mManager.updateUI(!has_error && !mManager.buying());
  155. // Section zero: title area
  156. {
  157. childSetVisible("info_buying", false);
  158. childSetVisible("info_cannot_buy", false);
  159. childSetVisible("info_need_more", false);
  160. if (has_error)
  161. {
  162. childSetVisible("info_cannot_buy", true);
  163. }
  164. else if (mHasTarget)
  165. {
  166. childSetVisible("info_need_more", true);
  167. }
  168. else
  169. {
  170. childSetVisible("info_buying", true);
  171. }
  172. }
  173. // Error section
  174. if (has_error)
  175. {
  176. childSetBadge("step_error", BADGE_ERROR);
  177. LLTextBox* message = getChild<LLTextBox>("error_message");
  178. if (message)
  179. {
  180. message->setVisible(true);
  181. message->setWrappedText(mManager.errorMessage());
  182. }
  183. mErrorURI = mManager.errorURI();
  184. bool has_error_uri = !mErrorURI.empty();
  185. childSetVisible("error_web", has_error_uri);
  186. if (has_error_uri)
  187. {
  188. childHide("getting_data");
  189. }
  190. mManager.clearError();
  191. }
  192. else
  193. {
  194. childHide("step_error");
  195. childHide("error_message");
  196. childHide("error_web");
  197. }
  198. // Currency
  199. childSetVisible("contacting", false);
  200. childSetVisible("buy_action", false);
  201. childSetVisible("buy_action_unknown", false);
  202. if (!has_error)
  203. {
  204. childSetBadge("step_1", BADGE_NOTE);
  205. if (mManager.buying())
  206. {
  207. childSetVisible("contacting", true);
  208. }
  209. else
  210. {
  211. if (mHasTarget)
  212. {
  213. childSetVisible("buy_action", true);
  214. childSetTextArg("buy_action", "[NAME]", mTargetName);
  215. childSetTextArg("buy_action", "[PRICE]",
  216. llformat("%d",mTargetPrice));
  217. }
  218. else
  219. {
  220. childSetVisible("buy_action_unknown", true);
  221. }
  222. }
  223. S32 balance = gStatusBarp->getBalance();
  224. childShow("balance_label");
  225. childShow("balance_amount");
  226. childSetTextArg("balance_amount", "[AMT]", llformat("%d", balance));
  227. S32 buying = mManager.getAmount();
  228. childShow("buying_label");
  229. childShow("buying_amount");
  230. childSetTextArg("buying_amount", "[AMT]", llformat("%d", buying));
  231. S32 total = balance + buying;
  232. childShow("total_label");
  233. childShow("total_amount");
  234. childSetTextArg("total_amount", "[AMT]", llformat("%d", total));
  235. childSetVisible("purchase_warning_repurchase", false);
  236. childSetVisible("purchase_warning_notenough", false);
  237. if (mHasTarget)
  238. {
  239. if (total >= mTargetPrice)
  240. {
  241. childSetVisible("purchase_warning_repurchase", true);
  242. }
  243. else
  244. {
  245. childSetVisible("purchase_warning_notenough", true);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. childHide("step_1");
  252. childHide("balance_label");
  253. childHide("balance_amount");
  254. childHide("buying_label");
  255. childHide("buying_amount");
  256. childHide("total_label");
  257. childHide("total_amount");
  258. childHide("purchase_warning_repurchase");
  259. childHide("purchase_warning_notenough");
  260. }
  261. childSetEnabled("buy_btn", mManager.canBuy());
  262. if (!mManager.canBuy() && !childIsVisible("error_web"))
  263. {
  264. childShow("getting_data");
  265. }
  266. }
  267. //static
  268. void LLFloaterBuyCurrency::onClickBuy(void* data)
  269. {
  270. LLFloaterBuyCurrency* self = (LLFloaterBuyCurrency*)data;
  271. if (self)
  272. {
  273. self->mManager.buy(self->getString("buy_currency"));
  274. self->updateUI();
  275. // JC: updateUI() does not get called again until progress is made
  276. // with transaction processing, so the "Purchase" button would be
  277. // left enabled for some time. Pre-emptively disable.
  278. self->childSetEnabled("buy_btn", false);
  279. }
  280. }
  281. //static
  282. void LLFloaterBuyCurrency::onClickCancel(void* data)
  283. {
  284. LLFloaterBuyCurrency* self = (LLFloaterBuyCurrency*)data;
  285. if (self)
  286. {
  287. self->close();
  288. }
  289. }
  290. //static
  291. void LLFloaterBuyCurrency::onClickErrorWeb(void* data)
  292. {
  293. LLFloaterBuyCurrency* self = (LLFloaterBuyCurrency*)data;
  294. if (self)
  295. {
  296. LLWeb::loadURL(self->mErrorURI);
  297. self->close();
  298. }
  299. }
  300. //static
  301. void LLFloaterBuyCurrency::buyCurrency()
  302. {
  303. LLFloaterBuyCurrency* self = getInstance();
  304. if (self)
  305. {
  306. self->noTarget();
  307. self->updateUI();
  308. self->open();
  309. }
  310. }
  311. //static
  312. void LLFloaterBuyCurrency::buyCurrency(const std::string& name, S32 price)
  313. {
  314. LLFloaterBuyCurrency* self = getInstance();
  315. if (self)
  316. {
  317. self->target(name, price);
  318. self->updateUI();
  319. self->open();
  320. }
  321. }