/indra/newview/llpanellogin.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 1047 lines · 758 code · 144 blank · 145 comment · 111 complexity · 97efdf08e0114f91cac5916d798aca34 MD5 · raw file

  1. /**
  2. * @file llpanellogin.cpp
  3. * @brief Login dialog and logo display
  4. *
  5. * $LicenseInfo:firstyear=2002&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 "llpanellogin.h"
  28. #include "indra_constants.h" // for key and mask constants
  29. #include "llfloaterreg.h"
  30. #include "llfontgl.h"
  31. #include "llmd5.h"
  32. #include "llsecondlifeurls.h"
  33. #include "v4color.h"
  34. #include "llappviewer.h"
  35. #include "llbutton.h"
  36. #include "llcheckboxctrl.h"
  37. #include "llcommandhandler.h" // for secondlife:///app/login/
  38. #include "llcombobox.h"
  39. #include "llcurl.h"
  40. #include "llviewercontrol.h"
  41. #include "llfloaterpreference.h"
  42. #include "llfocusmgr.h"
  43. #include "lllineeditor.h"
  44. #include "llnotificationsutil.h"
  45. #include "llsecapi.h"
  46. #include "llstartup.h"
  47. #include "lltextbox.h"
  48. #include "llui.h"
  49. #include "lluiconstants.h"
  50. #include "llslurl.h"
  51. #include "llversioninfo.h"
  52. #include "llviewerhelp.h"
  53. #include "llviewertexturelist.h"
  54. #include "llviewermenu.h" // for handle_preferences()
  55. #include "llviewernetwork.h"
  56. #include "llviewerwindow.h" // to link into child list
  57. #include "lluictrlfactory.h"
  58. #include "llhttpclient.h"
  59. #include "llweb.h"
  60. #include "llmediactrl.h"
  61. #include "llrootview.h"
  62. #include "llfloatertos.h"
  63. #include "lltrans.h"
  64. #include "llglheaders.h"
  65. #include "llpanelloginlistener.h"
  66. #if LL_WINDOWS
  67. #pragma warning(disable: 4355) // 'this' used in initializer list
  68. #endif // LL_WINDOWS
  69. #include "llsdserialize.h"
  70. const S32 BLACK_BORDER_HEIGHT = 160;
  71. const S32 MAX_PASSWORD = 16;
  72. LLPanelLogin *LLPanelLogin::sInstance = NULL;
  73. BOOL LLPanelLogin::sCapslockDidNotification = FALSE;
  74. // Helper for converting a user name into the canonical "Firstname Lastname" form.
  75. // For new accounts without a last name "Resident" is added as a last name.
  76. static std::string canonicalize_username(const std::string& name);
  77. class LLLoginRefreshHandler : public LLCommandHandler
  78. {
  79. public:
  80. // don't allow from external browsers
  81. LLLoginRefreshHandler() : LLCommandHandler("login_refresh", UNTRUSTED_BLOCK) { }
  82. bool handle(const LLSD& tokens, const LLSD& query_map, LLMediaCtrl* web)
  83. {
  84. if (LLStartUp::getStartupState() < STATE_LOGIN_CLEANUP)
  85. {
  86. LLPanelLogin::loadLoginPage();
  87. }
  88. return true;
  89. }
  90. };
  91. //---------------------------------------------------------------------------
  92. // Public methods
  93. //---------------------------------------------------------------------------
  94. LLPanelLogin::LLPanelLogin(const LLRect &rect,
  95. BOOL show_server,
  96. void (*callback)(S32 option, void* user_data),
  97. void *cb_data)
  98. : LLPanel(),
  99. mLogoImage(),
  100. mCallback(callback),
  101. mCallbackData(cb_data),
  102. mListener(new LLPanelLoginListener(this))
  103. {
  104. setBackgroundVisible(FALSE);
  105. setBackgroundOpaque(TRUE);
  106. // instance management
  107. if (LLPanelLogin::sInstance)
  108. {
  109. llwarns << "Duplicate instance of login view deleted" << llendl;
  110. // Don't leave bad pointer in gFocusMgr
  111. gFocusMgr.setDefaultKeyboardFocus(NULL);
  112. delete LLPanelLogin::sInstance;
  113. }
  114. mPasswordModified = FALSE;
  115. LLPanelLogin::sInstance = this;
  116. LLView* login_holder = gViewerWindow->getLoginPanelHolder();
  117. if (login_holder)
  118. {
  119. login_holder->addChild(this);
  120. }
  121. // Logo
  122. mLogoImage = LLUI::getUIImage("startup_logo");
  123. buildFromFile( "panel_login.xml");
  124. reshape(rect.getWidth(), rect.getHeight());
  125. getChild<LLLineEditor>("password_edit")->setKeystrokeCallback(onPassKey, this);
  126. // change z sort of clickable text to be behind buttons
  127. sendChildToBack(getChildView("forgot_password_text"));
  128. if(LLStartUp::getStartSLURL().getType() != LLSLURL::LOCATION)
  129. {
  130. LLSLURL slurl(gSavedSettings.getString("LoginLocation"));
  131. LLStartUp::setStartSLURL(slurl);
  132. }
  133. updateLocationCombo(false);
  134. LLComboBox* server_choice_combo = sInstance->getChild<LLComboBox>("server_combo");
  135. server_choice_combo->setCommitCallback(onSelectServer, NULL);
  136. server_choice_combo->setFocusLostCallback(boost::bind(onServerComboLostFocus, _1));
  137. updateServerCombo();
  138. childSetAction("connect_btn", onClickConnect, this);
  139. getChild<LLPanel>("login")->setDefaultBtn("connect_btn");
  140. std::string channel = LLVersionInfo::getChannel();
  141. std::string version = llformat("%s (%d)",
  142. LLVersionInfo::getShortVersion().c_str(),
  143. LLVersionInfo::getBuild());
  144. //LLTextBox* channel_text = getChild<LLTextBox>("channel_text");
  145. //channel_text->setTextArg("[CHANNEL]", channel); // though not displayed
  146. //channel_text->setTextArg("[VERSION]", version);
  147. //channel_text->setClickedCallback(onClickVersion, this);
  148. LLTextBox* forgot_password_text = getChild<LLTextBox>("forgot_password_text");
  149. forgot_password_text->setClickedCallback(onClickForgotPassword, NULL);
  150. LLTextBox* create_new_account_text = getChild<LLTextBox>("create_new_account_text");
  151. create_new_account_text->setClickedCallback(onClickNewAccount, NULL);
  152. LLTextBox* need_help_text = getChild<LLTextBox>("login_help");
  153. need_help_text->setClickedCallback(onClickHelp, NULL);
  154. // get the web browser control
  155. LLMediaCtrl* web_browser = getChild<LLMediaCtrl>("login_html");
  156. web_browser->addObserver(this);
  157. reshapeBrowser();
  158. loadLoginPage();
  159. // Show last logged in user favorites in "Start at" combo.
  160. addUsersWithFavoritesToUsername();
  161. getChild<LLComboBox>("username_combo")->setTextChangedCallback(boost::bind(&LLPanelLogin::addFavoritesToStartLocation, this));
  162. updateLocationCombo(false);
  163. }
  164. void LLPanelLogin::addUsersWithFavoritesToUsername()
  165. {
  166. LLComboBox* combo = getChild<LLComboBox>("username_combo");
  167. if (!combo) return;
  168. std::string filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "stored_favorites.xml");
  169. LLSD fav_llsd;
  170. llifstream file;
  171. file.open(filename);
  172. if (!file.is_open()) return;
  173. LLSDSerialize::fromXML(fav_llsd, file);
  174. for (LLSD::map_const_iterator iter = fav_llsd.beginMap();
  175. iter != fav_llsd.endMap(); ++iter)
  176. {
  177. combo->add(iter->first);
  178. }
  179. }
  180. void LLPanelLogin::addFavoritesToStartLocation()
  181. {
  182. LLComboBox* combo = getChild<LLComboBox>("start_location_combo");
  183. if (!combo) return;
  184. int num_items = combo->getItemCount();
  185. for (int i = num_items - 1; i > 2; i--)
  186. {
  187. combo->remove(i);
  188. }
  189. std::string filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "stored_favorites.xml");
  190. LLSD fav_llsd;
  191. llifstream file;
  192. file.open(filename);
  193. if (!file.is_open()) return;
  194. LLSDSerialize::fromXML(fav_llsd, file);
  195. for (LLSD::map_const_iterator iter = fav_llsd.beginMap();
  196. iter != fav_llsd.endMap(); ++iter)
  197. {
  198. std::string user_defined_name = getChild<LLComboBox>("username_combo")->getSimple();
  199. // The account name in stored_favorites.xml has Resident last name even if user has
  200. // a single word account name, so it can be compared case-insensitive with the
  201. // user defined "firstname lastname".
  202. S32 res = LLStringUtil::compareInsensitive(canonicalize_username(user_defined_name), iter->first);
  203. if (res != 0) continue;
  204. combo->addSeparator();
  205. LLSD user_llsd = iter->second;
  206. for (LLSD::array_const_iterator iter1 = user_llsd.beginArray();
  207. iter1 != user_llsd.endArray(); ++iter1)
  208. {
  209. std::string label = (*iter1)["name"].asString();
  210. std::string value = (*iter1)["slurl"].asString();
  211. if(label != "" && value != "")
  212. {
  213. combo->add(label, value);
  214. }
  215. }
  216. break;
  217. }
  218. }
  219. // force the size to be correct (XML doesn't seem to be sufficient to do this)
  220. // (with some padding so the other login screen doesn't show through)
  221. void LLPanelLogin::reshapeBrowser()
  222. {
  223. LLMediaCtrl* web_browser = getChild<LLMediaCtrl>("login_html");
  224. LLRect rect = gViewerWindow->getWindowRectScaled();
  225. LLRect html_rect;
  226. html_rect.setCenterAndSize(
  227. rect.getCenterX() - 2, rect.getCenterY() + 40,
  228. rect.getWidth() + 6, rect.getHeight() - 78 );
  229. web_browser->setRect( html_rect );
  230. web_browser->reshape( html_rect.getWidth(), html_rect.getHeight(), TRUE );
  231. reshape( rect.getWidth(), rect.getHeight(), 1 );
  232. }
  233. LLPanelLogin::~LLPanelLogin()
  234. {
  235. LLPanelLogin::sInstance = NULL;
  236. // Controls having keyboard focus by default
  237. // must reset it on destroy. (EXT-2748)
  238. gFocusMgr.setDefaultKeyboardFocus(NULL);
  239. }
  240. // virtual
  241. void LLPanelLogin::draw()
  242. {
  243. gGL.pushMatrix();
  244. {
  245. F32 image_aspect = 1.333333f;
  246. F32 view_aspect = (F32)getRect().getWidth() / (F32)getRect().getHeight();
  247. // stretch image to maintain aspect ratio
  248. if (image_aspect > view_aspect)
  249. {
  250. gGL.translatef(-0.5f * (image_aspect / view_aspect - 1.f) * getRect().getWidth(), 0.f, 0.f);
  251. gGL.scalef(image_aspect / view_aspect, 1.f, 1.f);
  252. }
  253. S32 width = getRect().getWidth();
  254. S32 height = getRect().getHeight();
  255. if (getChild<LLView>("login_widgets")->getVisible())
  256. {
  257. // draw a background box in black
  258. gl_rect_2d( 0, height - 264, width, 264, LLColor4::black );
  259. // draw the bottom part of the background image
  260. // just the blue background to the native client UI
  261. mLogoImage->draw(0, -264, width + 8, mLogoImage->getHeight());
  262. };
  263. }
  264. gGL.popMatrix();
  265. LLPanel::draw();
  266. }
  267. // virtual
  268. BOOL LLPanelLogin::handleKeyHere(KEY key, MASK mask)
  269. {
  270. if ( KEY_F1 == key )
  271. {
  272. LLViewerHelp* vhelp = LLViewerHelp::getInstance();
  273. vhelp->showTopic(vhelp->f1HelpTopic());
  274. return TRUE;
  275. }
  276. return LLPanel::handleKeyHere(key, mask);
  277. }
  278. // virtual
  279. void LLPanelLogin::setFocus(BOOL b)
  280. {
  281. if(b != hasFocus())
  282. {
  283. if(b)
  284. {
  285. LLPanelLogin::giveFocus();
  286. }
  287. else
  288. {
  289. LLPanel::setFocus(b);
  290. }
  291. }
  292. }
  293. // static
  294. void LLPanelLogin::giveFocus()
  295. {
  296. if( sInstance )
  297. {
  298. // Grab focus and move cursor to first blank input field
  299. std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
  300. std::string pass = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
  301. BOOL have_username = !username.empty();
  302. BOOL have_pass = !pass.empty();
  303. LLLineEditor* edit = NULL;
  304. LLComboBox* combo = NULL;
  305. if (have_username && !have_pass)
  306. {
  307. // User saved his name but not his password. Move
  308. // focus to password field.
  309. edit = sInstance->getChild<LLLineEditor>("password_edit");
  310. }
  311. else
  312. {
  313. // User doesn't have a name, so start there.
  314. combo = sInstance->getChild<LLComboBox>("username_combo");
  315. }
  316. if (edit)
  317. {
  318. edit->setFocus(TRUE);
  319. edit->selectAll();
  320. }
  321. else if (combo)
  322. {
  323. combo->setFocus(TRUE);
  324. }
  325. }
  326. }
  327. // static
  328. void LLPanelLogin::showLoginWidgets()
  329. {
  330. // *NOTE: Mani - This may or may not be obselete code.
  331. // It seems to be part of the defunct? reg-in-client project.
  332. sInstance->getChildView("login_widgets")->setVisible( true);
  333. LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
  334. sInstance->reshapeBrowser();
  335. // *TODO: Append all the usual login parameters, like first_login=Y etc.
  336. std::string splash_screen_url = LLGridManager::getInstance()->getLoginPage();
  337. web_browser->navigateTo( splash_screen_url, "text/html" );
  338. LLUICtrl* username_combo = sInstance->getChild<LLUICtrl>("username_combo");
  339. username_combo->setFocus(TRUE);
  340. }
  341. // static
  342. void LLPanelLogin::show(const LLRect &rect,
  343. BOOL show_server,
  344. void (*callback)(S32 option, void* user_data),
  345. void* callback_data)
  346. {
  347. new LLPanelLogin(rect, show_server, callback, callback_data);
  348. if( !gFocusMgr.getKeyboardFocus() )
  349. {
  350. // Grab focus and move cursor to first enabled control
  351. sInstance->setFocus(TRUE);
  352. }
  353. // Make sure that focus always goes here (and use the latest sInstance that was just created)
  354. gFocusMgr.setDefaultKeyboardFocus(sInstance);
  355. }
  356. // static
  357. void LLPanelLogin::setFields(LLPointer<LLCredential> credential,
  358. BOOL remember)
  359. {
  360. if (!sInstance)
  361. {
  362. llwarns << "Attempted fillFields with no login view shown" << llendl;
  363. return;
  364. }
  365. LL_INFOS("Credentials") << "Setting login fields to " << *credential << LL_ENDL;
  366. LLSD identifier = credential->getIdentifier();
  367. if((std::string)identifier["type"] == "agent")
  368. {
  369. std::string firstname = identifier["first_name"].asString();
  370. std::string lastname = identifier["last_name"].asString();
  371. std::string login_id = firstname;
  372. if (!lastname.empty() && lastname != "Resident")
  373. {
  374. // support traditional First Last name SLURLs
  375. login_id += " ";
  376. login_id += lastname;
  377. }
  378. sInstance->getChild<LLComboBox>("username_combo")->setLabel(login_id);
  379. }
  380. else if((std::string)identifier["type"] == "account")
  381. {
  382. sInstance->getChild<LLComboBox>("username_combo")->setLabel((std::string)identifier["account_name"]);
  383. }
  384. else
  385. {
  386. sInstance->getChild<LLComboBox>("username_combo")->setLabel(std::string());
  387. }
  388. sInstance->addFavoritesToStartLocation();
  389. // if the password exists in the credential, set the password field with
  390. // a filler to get some stars
  391. LLSD authenticator = credential->getAuthenticator();
  392. LL_INFOS("Credentials") << "Setting authenticator field " << authenticator["type"].asString() << LL_ENDL;
  393. if(authenticator.isMap() &&
  394. authenticator.has("secret") &&
  395. (authenticator["secret"].asString().size() > 0))
  396. {
  397. // This is a MD5 hex digest of a password.
  398. // We don't actually use the password input field,
  399. // fill it with MAX_PASSWORD characters so we get a
  400. // nice row of asterixes.
  401. const std::string filler("123456789!123456");
  402. sInstance->getChild<LLUICtrl>("password_edit")->setValue(std::string("123456789!123456"));
  403. }
  404. else
  405. {
  406. sInstance->getChild<LLUICtrl>("password_edit")->setValue(std::string());
  407. }
  408. sInstance->getChild<LLUICtrl>("remember_check")->setValue(remember);
  409. }
  410. // static
  411. void LLPanelLogin::getFields(LLPointer<LLCredential>& credential,
  412. BOOL& remember)
  413. {
  414. if (!sInstance)
  415. {
  416. llwarns << "Attempted getFields with no login view shown" << llendl;
  417. return;
  418. }
  419. // load the credential so we can pass back the stored password or hash if the user did
  420. // not modify the password field.
  421. credential = gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
  422. LLSD identifier = LLSD::emptyMap();
  423. LLSD authenticator = LLSD::emptyMap();
  424. if(credential.notNull())
  425. {
  426. authenticator = credential->getAuthenticator();
  427. }
  428. std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
  429. LLStringUtil::trim(username);
  430. std::string password = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
  431. LL_INFOS2("Credentials", "Authentication") << "retrieving username:" << username << LL_ENDL;
  432. // determine if the username is a first/last form or not.
  433. size_t separator_index = username.find_first_of(' ');
  434. if (separator_index == username.npos
  435. && !LLGridManager::getInstance()->isSystemGrid())
  436. {
  437. LL_INFOS2("Credentials", "Authentication") << "account: " << username << LL_ENDL;
  438. // single username, so this is a 'clear' identifier
  439. identifier["type"] = CRED_IDENTIFIER_TYPE_ACCOUNT;
  440. identifier["account_name"] = username;
  441. if (LLPanelLogin::sInstance->mPasswordModified)
  442. {
  443. authenticator = LLSD::emptyMap();
  444. // password is plaintext
  445. authenticator["type"] = CRED_AUTHENTICATOR_TYPE_CLEAR;
  446. authenticator["secret"] = password;
  447. }
  448. }
  449. else
  450. {
  451. // Be lenient in terms of what separators we allow for two-word names
  452. // and allow legacy users to login with firstname.lastname
  453. separator_index = username.find_first_of(" ._");
  454. std::string first = username.substr(0, separator_index);
  455. std::string last;
  456. if (separator_index != username.npos)
  457. {
  458. last = username.substr(separator_index+1, username.npos);
  459. LLStringUtil::trim(last);
  460. }
  461. else
  462. {
  463. // ...on Linden grids, single username users as considered to have
  464. // last name "Resident"
  465. // *TODO: Make login.cgi support "account_name" like above
  466. last = "Resident";
  467. }
  468. if (last.find_first_of(' ') == last.npos)
  469. {
  470. LL_INFOS2("Credentials", "Authentication") << "agent: " << username << LL_ENDL;
  471. // traditional firstname / lastname
  472. identifier["type"] = CRED_IDENTIFIER_TYPE_AGENT;
  473. identifier["first_name"] = first;
  474. identifier["last_name"] = last;
  475. if (LLPanelLogin::sInstance->mPasswordModified)
  476. {
  477. authenticator = LLSD::emptyMap();
  478. authenticator["type"] = CRED_AUTHENTICATOR_TYPE_HASH;
  479. authenticator["algorithm"] = "md5";
  480. LLMD5 pass((const U8 *)password.c_str());
  481. char md5pass[33]; /* Flawfinder: ignore */
  482. pass.hex_digest(md5pass);
  483. authenticator["secret"] = md5pass;
  484. }
  485. }
  486. }
  487. credential = gSecAPIHandler->createCredential(LLGridManager::getInstance()->getGrid(), identifier, authenticator);
  488. remember = sInstance->getChild<LLUICtrl>("remember_check")->getValue();
  489. }
  490. // static
  491. BOOL LLPanelLogin::isGridComboDirty()
  492. {
  493. BOOL user_picked = FALSE;
  494. if (!sInstance)
  495. {
  496. llwarns << "Attempted getServer with no login view shown" << llendl;
  497. }
  498. else
  499. {
  500. LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
  501. user_picked = combo->isDirty();
  502. }
  503. return user_picked;
  504. }
  505. // static
  506. BOOL LLPanelLogin::areCredentialFieldsDirty()
  507. {
  508. if (!sInstance)
  509. {
  510. llwarns << "Attempted getServer with no login view shown" << llendl;
  511. }
  512. else
  513. {
  514. std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
  515. LLStringUtil::trim(username);
  516. std::string password = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
  517. LLComboBox* combo = sInstance->getChild<LLComboBox>("username_combo");
  518. if(combo && combo->isDirty())
  519. {
  520. return true;
  521. }
  522. LLLineEditor* ctrl = sInstance->getChild<LLLineEditor>("password_edit");
  523. if(ctrl && ctrl->isDirty())
  524. {
  525. return true;
  526. }
  527. }
  528. return false;
  529. }
  530. // static
  531. void LLPanelLogin::updateLocationCombo( bool force_visible )
  532. {
  533. if (!sInstance)
  534. {
  535. return;
  536. }
  537. LLComboBox* combo = sInstance->getChild<LLComboBox>("start_location_combo");
  538. switch(LLStartUp::getStartSLURL().getType())
  539. {
  540. case LLSLURL::LOCATION:
  541. {
  542. combo->setCurrentByIndex( 2 );
  543. combo->setTextEntry(LLStartUp::getStartSLURL().getLocationString());
  544. break;
  545. }
  546. case LLSLURL::HOME_LOCATION:
  547. combo->setCurrentByIndex(1);
  548. break;
  549. default:
  550. combo->setCurrentByIndex(0);
  551. break;
  552. }
  553. BOOL show_start = TRUE;
  554. if ( ! force_visible )
  555. show_start = gSavedSettings.getBOOL("ShowStartLocation");
  556. sInstance->getChildView("start_location_combo")->setVisible( show_start);
  557. sInstance->getChildView("start_location_text")->setVisible( show_start);
  558. BOOL show_server = gSavedSettings.getBOOL("ForceShowGrid");
  559. sInstance->getChildView("server_combo_text")->setVisible( show_server);
  560. sInstance->getChildView("server_combo")->setVisible( show_server);
  561. }
  562. // static
  563. void LLPanelLogin::updateStartSLURL()
  564. {
  565. if (!sInstance) return;
  566. LLComboBox* combo = sInstance->getChild<LLComboBox>("start_location_combo");
  567. S32 index = combo->getCurrentIndex();
  568. switch (index)
  569. {
  570. case 0:
  571. {
  572. LLStartUp::setStartSLURL(LLSLURL(LLSLURL::SIM_LOCATION_LAST));
  573. break;
  574. }
  575. case 1:
  576. {
  577. LLStartUp::setStartSLURL(LLSLURL(LLSLURL::SIM_LOCATION_HOME));
  578. break;
  579. }
  580. default:
  581. {
  582. LLSLURL slurl = LLSLURL(combo->getValue().asString());
  583. if(slurl.getType() == LLSLURL::LOCATION)
  584. {
  585. // we've changed the grid, so update the grid selection
  586. LLStartUp::setStartSLURL(slurl);
  587. }
  588. break;
  589. }
  590. }
  591. }
  592. void LLPanelLogin::setLocation(const LLSLURL& slurl)
  593. {
  594. LLStartUp::setStartSLURL(slurl);
  595. updateServer();
  596. }
  597. // static
  598. void LLPanelLogin::closePanel()
  599. {
  600. if (sInstance)
  601. {
  602. LLPanelLogin::sInstance->getParent()->removeChild( LLPanelLogin::sInstance );
  603. delete sInstance;
  604. sInstance = NULL;
  605. }
  606. }
  607. // static
  608. void LLPanelLogin::setAlwaysRefresh(bool refresh)
  609. {
  610. if (LLStartUp::getStartupState() >= STATE_LOGIN_CLEANUP) return;
  611. LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
  612. if (web_browser)
  613. {
  614. web_browser->setAlwaysRefresh(refresh);
  615. }
  616. }
  617. void LLPanelLogin::loadLoginPage()
  618. {
  619. if (!sInstance) return;
  620. std::ostringstream oStr;
  621. std::string login_page = LLGridManager::getInstance()->getLoginPage();
  622. oStr << login_page;
  623. // Use the right delimeter depending on how LLURI parses the URL
  624. LLURI login_page_uri = LLURI(login_page);
  625. std::string first_query_delimiter = "&";
  626. if (login_page_uri.queryMap().size() == 0)
  627. {
  628. first_query_delimiter = "?";
  629. }
  630. // Language
  631. std::string language = LLUI::getLanguage();
  632. oStr << first_query_delimiter<<"lang=" << language;
  633. // First Login?
  634. if (gSavedSettings.getBOOL("FirstLoginThisInstall"))
  635. {
  636. oStr << "&firstlogin=TRUE";
  637. }
  638. // Channel and Version
  639. std::string version = llformat("%s (%d)",
  640. LLVersionInfo::getShortVersion().c_str(),
  641. LLVersionInfo::getBuild());
  642. char* curl_channel = curl_escape(LLVersionInfo::getChannel().c_str(), 0);
  643. char* curl_version = curl_escape(version.c_str(), 0);
  644. oStr << "&channel=" << curl_channel;
  645. oStr << "&version=" << curl_version;
  646. curl_free(curl_channel);
  647. curl_free(curl_version);
  648. // Grid
  649. char* curl_grid = curl_escape(LLGridManager::getInstance()->getGridLabel().c_str(), 0);
  650. oStr << "&grid=" << curl_grid;
  651. curl_free(curl_grid);
  652. // add OS info
  653. char * os_info = curl_escape(LLAppViewer::instance()->getOSInfo().getOSStringSimple().c_str(), 0);
  654. oStr << "&os=" << os_info;
  655. curl_free(os_info);
  656. gViewerWindow->setMenuBackgroundColor(false, !LLGridManager::getInstance()->isInProductionGrid());
  657. LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
  658. if (web_browser->getCurrentNavUrl() != oStr.str())
  659. {
  660. web_browser->navigateTo( oStr.str(), "text/html" );
  661. }
  662. }
  663. void LLPanelLogin::handleMediaEvent(LLPluginClassMedia* /*self*/, EMediaEvent event)
  664. {
  665. if(event == MEDIA_EVENT_NAVIGATE_COMPLETE)
  666. {
  667. LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
  668. if (web_browser)
  669. {
  670. // *HACK HACK HACK HACK!
  671. /* Stuff a Tab key into the browser now so that the first field will
  672. ** get the focus! The embedded javascript on the page that properly
  673. ** sets the initial focus in a real web browser is not working inside
  674. ** the viewer, so this is an UGLY HACK WORKAROUND for now.
  675. */
  676. // Commented out as it's not reliable
  677. //web_browser->handleKey(KEY_TAB, MASK_NONE, false);
  678. }
  679. }
  680. }
  681. //---------------------------------------------------------------------------
  682. // Protected methods
  683. //---------------------------------------------------------------------------
  684. // static
  685. void LLPanelLogin::onClickConnect(void *)
  686. {
  687. if (sInstance && sInstance->mCallback)
  688. {
  689. // JC - Make sure the fields all get committed.
  690. sInstance->setFocus(FALSE);
  691. LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
  692. LLSD combo_val = combo->getSelectedValue();
  693. if (combo_val.isUndefined())
  694. {
  695. combo_val = combo->getValue();
  696. }
  697. if(combo_val.isUndefined())
  698. {
  699. LLNotificationsUtil::add("StartRegionEmpty");
  700. return;
  701. }
  702. try
  703. {
  704. LLGridManager::getInstance()->setGridChoice(combo_val.asString());
  705. }
  706. catch (LLInvalidGridName ex)
  707. {
  708. LLSD args;
  709. args["GRID"] = combo_val.asString();
  710. LLNotificationsUtil::add("InvalidGrid", args);
  711. return;
  712. }
  713. updateStartSLURL();
  714. std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
  715. if(username.empty())
  716. {
  717. // user must type in something into the username field
  718. LLNotificationsUtil::add("MustHaveAccountToLogIn");
  719. }
  720. else
  721. {
  722. LLPointer<LLCredential> cred;
  723. BOOL remember;
  724. getFields(cred, remember);
  725. std::string identifier_type;
  726. cred->identifierType(identifier_type);
  727. LLSD allowed_credential_types;
  728. LLGridManager::getInstance()->getLoginIdentifierTypes(allowed_credential_types);
  729. // check the typed in credential type against the credential types expected by the server.
  730. for(LLSD::array_iterator i = allowed_credential_types.beginArray();
  731. i != allowed_credential_types.endArray();
  732. i++)
  733. {
  734. if(i->asString() == identifier_type)
  735. {
  736. // yay correct credential type
  737. sInstance->mCallback(0, sInstance->mCallbackData);
  738. return;
  739. }
  740. }
  741. // Right now, maingrid is the only thing that is picky about
  742. // credential format, as it doesn't yet allow account (single username)
  743. // format creds. - Rox. James, we wanna fix the message when we change
  744. // this.
  745. LLNotificationsUtil::add("InvalidCredentialFormat");
  746. }
  747. }
  748. }
  749. // static
  750. void LLPanelLogin::onClickNewAccount(void*)
  751. {
  752. LLWeb::loadURLExternal(sInstance->getString("create_account_url"));
  753. }
  754. // static
  755. void LLPanelLogin::onClickVersion(void*)
  756. {
  757. LLFloaterReg::showInstance("sl_about");
  758. }
  759. //static
  760. void LLPanelLogin::onClickForgotPassword(void*)
  761. {
  762. if (sInstance )
  763. {
  764. LLWeb::loadURLExternal(sInstance->getString( "forgot_password_url" ));
  765. }
  766. }
  767. //static
  768. void LLPanelLogin::onClickHelp(void*)
  769. {
  770. if (sInstance)
  771. {
  772. LLViewerHelp* vhelp = LLViewerHelp::getInstance();
  773. vhelp->showTopic(vhelp->preLoginTopic());
  774. }
  775. }
  776. // static
  777. void LLPanelLogin::onPassKey(LLLineEditor* caller, void* user_data)
  778. {
  779. LLPanelLogin *This = (LLPanelLogin *) user_data;
  780. This->mPasswordModified = TRUE;
  781. if (gKeyboard->getKeyDown(KEY_CAPSLOCK) && sCapslockDidNotification == FALSE)
  782. {
  783. // *TODO: use another way to notify user about enabled caps lock, see EXT-6858
  784. sCapslockDidNotification = TRUE;
  785. }
  786. }
  787. void LLPanelLogin::updateServer()
  788. {
  789. try
  790. {
  791. updateServerCombo();
  792. // if they've selected another grid, we should load the credentials
  793. // for that grid and set them to the UI.
  794. if(sInstance && !sInstance->areCredentialFieldsDirty())
  795. {
  796. LLPointer<LLCredential> credential = gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
  797. bool remember = sInstance->getChild<LLUICtrl>("remember_check")->getValue();
  798. sInstance->setFields(credential, remember);
  799. }
  800. // grid changed so show new splash screen (possibly)
  801. loadLoginPage();
  802. updateLocationCombo(LLStartUp::getStartSLURL().getType() == LLSLURL::LOCATION);
  803. }
  804. catch (LLInvalidGridName ex)
  805. {
  806. // do nothing
  807. }
  808. }
  809. void LLPanelLogin::updateServerCombo()
  810. {
  811. if (!sInstance)
  812. {
  813. return;
  814. }
  815. // We add all of the possible values, sorted, and then add a bar and the current value at the top
  816. LLComboBox* server_choice_combo = sInstance->getChild<LLComboBox>("server_combo");
  817. server_choice_combo->removeall();
  818. std::map<std::string, std::string> known_grids = LLGridManager::getInstance()->getKnownGrids(!gSavedSettings.getBOOL("ShowBetaGrids"));
  819. for (std::map<std::string, std::string>::iterator grid_choice = known_grids.begin();
  820. grid_choice != known_grids.end();
  821. grid_choice++)
  822. {
  823. if (!grid_choice->first.empty())
  824. {
  825. server_choice_combo->add(grid_choice->second, grid_choice->first);
  826. }
  827. }
  828. server_choice_combo->sortByName();
  829. server_choice_combo->addSeparator(ADD_TOP);
  830. server_choice_combo->add(LLGridManager::getInstance()->getGridLabel(),
  831. LLGridManager::getInstance()->getGrid(), ADD_TOP);
  832. server_choice_combo->selectFirstItem();
  833. }
  834. // static
  835. void LLPanelLogin::onSelectServer(LLUICtrl*, void*)
  836. {
  837. // *NOTE: The paramters for this method are ignored.
  838. // LLPanelLogin::onServerComboLostFocus(LLFocusableElement* fe, void*)
  839. // calls this method.
  840. LL_INFOS("AppInit") << "onSelectServer" << LL_ENDL;
  841. // The user twiddled with the grid choice ui.
  842. // apply the selection to the grid setting.
  843. LLPointer<LLCredential> credential;
  844. LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
  845. LLSD combo_val = combo->getSelectedValue();
  846. if (combo_val.isUndefined())
  847. {
  848. combo_val = combo->getValue();
  849. }
  850. combo = sInstance->getChild<LLComboBox>("start_location_combo");
  851. combo->setCurrentByIndex(1);
  852. LLStartUp::setStartSLURL(LLSLURL(gSavedSettings.getString("LoginLocation")));
  853. LLGridManager::getInstance()->setGridChoice(combo_val.asString());
  854. // This new selection will override preset uris
  855. // from the command line.
  856. updateServer();
  857. updateLocationCombo(false);
  858. updateLoginPanelLinks();
  859. }
  860. void LLPanelLogin::onServerComboLostFocus(LLFocusableElement* fe)
  861. {
  862. if (!sInstance)
  863. {
  864. return;
  865. }
  866. LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
  867. if(fe == combo)
  868. {
  869. onSelectServer(combo, NULL);
  870. }
  871. }
  872. void LLPanelLogin::updateLoginPanelLinks()
  873. {
  874. LLSD grid_data;
  875. LLGridManager::getInstance()->getGridInfo(grid_data);
  876. bool system_grid = grid_data.has(GRID_IS_SYSTEM_GRID_VALUE);
  877. // need to call through sInstance, as it's called from onSelectServer, which
  878. // is static.
  879. sInstance->getChildView("create_new_account_text")->setVisible( system_grid);
  880. sInstance->getChildView("forgot_password_text")->setVisible( system_grid);
  881. }
  882. std::string canonicalize_username(const std::string& name)
  883. {
  884. std::string cname = name;
  885. LLStringUtil::trim(cname);
  886. // determine if the username is a first/last form or not.
  887. size_t separator_index = cname.find_first_of(" ._");
  888. std::string first = cname.substr(0, separator_index);
  889. std::string last;
  890. if (separator_index != cname.npos)
  891. {
  892. last = cname.substr(separator_index+1, cname.npos);
  893. LLStringUtil::trim(last);
  894. }
  895. else
  896. {
  897. // ...on Linden grids, single username users as considered to have
  898. // last name "Resident"
  899. last = "Resident";
  900. }
  901. // Username in traditional "firstname lastname" form.
  902. return first + ' ' + last;
  903. }