PageRenderTime 62ms CodeModel.GetById 20ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 1ms

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