PageRenderTime 657ms CodeModel.GetById 100ms app.highlight 430ms RepoModel.GetById 113ms app.codeStats 1ms

/indra/newview/llpanellogin.cpp

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