00001
00032 #include "llviewerprecompiledheaders.h"
00033
00034 #include "llfloaterbuycurrency.h"
00035
00036
00037 #include "llcurrencyuimanager.h"
00038 #include "llfloater.h"
00039 #include "llstatusbar.h"
00040 #include "lltextbox.h"
00041 #include "llviewchildren.h"
00042 #include "llviewerwindow.h"
00043 #include "lluictrlfactory.h"
00044 #include "llweb.h"
00045 #include "llwindow.h"
00046 #include "llappviewer.h"
00047
00048 static const S32 STANDARD_BUY_AMOUNT = 1000;
00049 static const S32 MINIMUM_BALANCE_AMOUNT = 0;
00050
00051 class LLFloaterBuyCurrencyUI
00052 : public LLFloater
00053 {
00054 public:
00055 static LLFloaterBuyCurrencyUI* soleInstance(bool createIfNeeded);
00056
00057 private:
00058 static LLFloaterBuyCurrencyUI* sInstance;
00059
00060 LLFloaterBuyCurrencyUI();
00061 virtual ~LLFloaterBuyCurrencyUI();
00062
00063
00064 public:
00065 LLViewChildren mChildren;
00066 LLCurrencyUIManager mManager;
00067
00068 bool mHasTarget;
00069 std::string mTargetName;
00070 S32 mTargetPrice;
00071
00072 public:
00073 void noTarget();
00074 void target(const std::string& name, S32 price);
00075
00076 virtual BOOL postBuild();
00077
00078 void updateUI();
00079
00080 virtual void draw();
00081 virtual BOOL canClose();
00082 virtual void onClose(bool app_quitting);
00083
00084 static void onClickBuy(void* data);
00085 static void onClickCancel(void* data);
00086 static void onClickErrorWeb(void* data);
00087 };
00088
00089
00090
00091 LLFloaterBuyCurrencyUI* LLFloaterBuyCurrencyUI::sInstance = NULL;
00092
00093
00094 LLFloaterBuyCurrencyUI* LLFloaterBuyCurrencyUI::soleInstance(bool createIfNeeded)
00095 {
00096 if (!sInstance && createIfNeeded)
00097 {
00098 sInstance = new LLFloaterBuyCurrencyUI();
00099
00100 LLUICtrlFactory::getInstance()->buildFloater(sInstance, "floater_buy_currency.xml");
00101 sInstance->center();
00102 }
00103
00104 return sInstance;
00105 }
00106
00107
00108 #if LL_WINDOWS
00109
00110
00111
00112
00113 #pragma warning(disable : 4355)
00114 #endif
00115 LLFloaterBuyCurrencyUI::LLFloaterBuyCurrencyUI()
00116 : LLFloater("Buy Currency"),
00117 mChildren(*this),
00118 mManager(*this)
00119 {
00120 }
00121
00122 LLFloaterBuyCurrencyUI::~LLFloaterBuyCurrencyUI()
00123 {
00124 if (sInstance == this)
00125 {
00126 sInstance = NULL;
00127 }
00128 }
00129
00130
00131 void LLFloaterBuyCurrencyUI::noTarget()
00132 {
00133 mHasTarget = false;
00134 mManager.setAmount(STANDARD_BUY_AMOUNT);
00135 }
00136
00137 void LLFloaterBuyCurrencyUI::target(const std::string& name, S32 price)
00138 {
00139 mHasTarget = true;
00140 mTargetName = name;
00141 mTargetPrice = price;
00142
00143 S32 balance = gStatusBar->getBalance();
00144 S32 need = price - balance;
00145 if (need < 0)
00146 {
00147 need = 0;
00148 }
00149
00150 mManager.setAmount(need + MINIMUM_BALANCE_AMOUNT);
00151 }
00152
00153
00154
00155 BOOL LLFloaterBuyCurrencyUI::postBuild()
00156 {
00157 mManager.prepare();
00158
00159 childSetAction("buy_btn", onClickBuy, this);
00160 childSetAction("cancel_btn", onClickCancel, this);
00161 childSetAction("error_web", onClickErrorWeb, this);
00162
00163 updateUI();
00164
00165 return TRUE;
00166 }
00167
00168 void LLFloaterBuyCurrencyUI::draw()
00169 {
00170 if (mManager.process())
00171 {
00172 if (mManager.bought())
00173 {
00174 close();
00175 return;
00176 }
00177
00178 updateUI();
00179 }
00180
00181 LLFloater::draw();
00182 }
00183
00184 BOOL LLFloaterBuyCurrencyUI::canClose()
00185 {
00186 return mManager.canCancel();
00187 }
00188
00189 void LLFloaterBuyCurrencyUI::onClose(bool app_quitting)
00190 {
00191 LLFloater::onClose(app_quitting);
00192 destroy();
00193 }
00194
00195 void LLFloaterBuyCurrencyUI::updateUI()
00196 {
00197 bool hasError = mManager.hasError();
00198 mManager.updateUI(!hasError && !mManager.buying());
00199
00200
00201 {
00202 childSetVisible("info_buying", false);
00203 childSetVisible("info_cannot_buy", false);
00204 childSetVisible("info_need_more", false);
00205 if (hasError)
00206 {
00207 childSetVisible("info_cannot_buy", true);
00208 }
00209 else if (mHasTarget)
00210 {
00211 childSetVisible("info_need_more", true);
00212 }
00213 else
00214 {
00215 childSetVisible("info_buying", true);
00216 }
00217 }
00218
00219
00220 if (hasError)
00221 {
00222 mChildren.setBadge("step_error", LLViewChildren::BADGE_ERROR);
00223
00224 LLTextBox* message = getChild<LLTextBox>("error_message");
00225 if (message)
00226 {
00227 message->setVisible(true);
00228 message->setWrappedText(mManager.errorMessage());
00229 }
00230
00231 childSetVisible("error_web", !mManager.errorURI().empty());
00232 if (!mManager.errorURI().empty())
00233 {
00234 childHide("getting_data");
00235 }
00236 }
00237 else
00238 {
00239 childHide("step_error");
00240 childHide("error_message");
00241 childHide("error_web");
00242 }
00243
00244
00245
00246 childSetVisible("contacting", false);
00247 childSetVisible("buy_action", false);
00248 childSetVisible("buy_action_unknown", false);
00249
00250 if (!hasError)
00251 {
00252 mChildren.setBadge("step_1", LLViewChildren::BADGE_NOTE);
00253
00254 if (mManager.buying())
00255 {
00256 childSetVisible("contacting", true);
00257 }
00258 else
00259 {
00260 if (mHasTarget)
00261 {
00262 childSetVisible("buy_action", true);
00263 childSetTextArg("buy_action", "[NAME]", mTargetName);
00264 childSetTextArg("buy_action", "[PRICE]", llformat("%d",mTargetPrice));
00265 }
00266 else
00267 {
00268 childSetVisible("buy_action_unknown", true);
00269 }
00270 }
00271
00272 S32 balance = gStatusBar->getBalance();
00273 childShow("balance_label");
00274 childShow("balance_amount");
00275 childSetTextArg("balance_amount", "[AMT]", llformat("%d", balance));
00276
00277 S32 buying = mManager.getAmount();
00278 childShow("buying_label");
00279 childShow("buying_amount");
00280 childSetTextArg("buying_amount", "[AMT]", llformat("%d", buying));
00281
00282 S32 total = balance + buying;
00283 childShow("total_label");
00284 childShow("total_amount");
00285 childSetTextArg("total_amount", "[AMT]", llformat("%d", total));
00286
00287 childSetVisible("purchase_warning_repurchase", false);
00288 childSetVisible("purchase_warning_notenough", false);
00289 if (mHasTarget)
00290 {
00291 if (total >= mTargetPrice)
00292 {
00293 childSetVisible("purchase_warning_repurchase", true);
00294 }
00295 else
00296 {
00297 childSetVisible("purchase_warning_notenough", true);
00298 }
00299 }
00300 }
00301 else
00302 {
00303 childHide("step_1");
00304 childHide("balance_label");
00305 childHide("balance_amount");
00306 childHide("buying_label");
00307 childHide("buying_amount");
00308 childHide("total_label");
00309 childHide("total_amount");
00310 childHide("purchase_warning_repurchase");
00311 childHide("purchase_warning_notenough");
00312 }
00313
00314 childSetEnabled("buy_btn", mManager.canBuy());
00315
00316 if (!mManager.canBuy() && !childIsVisible("error_web"))
00317 {
00318 childShow("getting_data");
00319 }
00320 }
00321
00322
00323 void LLFloaterBuyCurrencyUI::onClickBuy(void* data)
00324 {
00325 LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00326 if (self)
00327 {
00328 self->mManager.buy(self->getString("buy_currency"));
00329 self->updateUI();
00330
00331
00332
00333 self->childSetEnabled("buy_btn", false);
00334 }
00335 }
00336
00337
00338 void LLFloaterBuyCurrencyUI::onClickCancel(void* data)
00339 {
00340 LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00341 if (self)
00342 {
00343 self->close();
00344 }
00345 }
00346
00347
00348 void LLFloaterBuyCurrencyUI::onClickErrorWeb(void* data)
00349 {
00350 LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00351 if (self)
00352 {
00353 LLWeb::loadURLExternal(self->mManager.errorURI());
00354 self->close();
00355 }
00356 }
00357
00358
00359 void LLFloaterBuyCurrency::buyCurrency()
00360 {
00361 if (gHideLinks)
00362 {
00363 return;
00364 }
00365
00366 LLFloaterBuyCurrencyUI* ui = LLFloaterBuyCurrencyUI::soleInstance(true);
00367 ui->noTarget();
00368 ui->updateUI();
00369 ui->open();
00370 }
00371
00372 void LLFloaterBuyCurrency::buyCurrency(const std::string& name, S32 price)
00373 {
00374 if (gHideLinks)
00375 {
00376 LLStringBase<char>::format_map_t args;
00377 args["[NAME]"] = name.c_str();
00378 args["[PRICE]"] = price;
00379 gViewerWindow->alertXml("NotEnoughCurrency", args);
00380 return;
00381 }
00382
00383 LLFloaterBuyCurrencyUI* ui = LLFloaterBuyCurrencyUI::soleInstance(true);
00384 ui->target(name, price);
00385 ui->updateUI();
00386 ui->open();
00387 }
00388
00389