summaryrefslogtreecommitdiff
path: root/indra/newview/llpanellogin.cpp
blob: 7ef3685cdb2158aabbedcc7c5efdabd8973d5992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
/** 
 * @file llpanellogin.cpp
 * @brief Login dialog and logo display
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llpanellogin.h"
#include "lllayoutstack.h"

#include "indra_constants.h"		// for key and mask constants
#include "llfloaterreg.h"
#include "llfontgl.h"
#include "llmd5.h"
#include "v4color.h"

#include "llappviewer.h"
#include "llbutton.h"
#include "llcheckboxctrl.h"
#include "llcommandhandler.h"		// for secondlife:///app/login/
#include "llcombobox.h"
#include "llviewercontrol.h"
#include "llfloaterpreference.h"
#include "llfocusmgr.h"
#include "lllineeditor.h"
#include "llnotificationsutil.h"
#include "llsecapi.h"
#include "llstartup.h"
#include "lltextbox.h"
#include "llui.h"
#include "lluiconstants.h"
#include "llslurl.h"
#include "llversioninfo.h"
#include "llviewerhelp.h"
#include "llviewertexturelist.h"
#include "llviewermenu.h"			// for handle_preferences()
#include "llviewernetwork.h"
#include "llviewerwindow.h"			// to link into child list
#include "lluictrlfactory.h"
#include "llweb.h"
#include "llmediactrl.h"
#include "llrootview.h"

#include "llfloatertos.h"
#include "lltrans.h"
#include "llglheaders.h"
#include "llpanelloginlistener.h"

#if LL_WINDOWS
#pragma warning(disable: 4355)      // 'this' used in initializer list
#endif  // LL_WINDOWS

#include "llsdserialize.h"

LLPanelLogin *LLPanelLogin::sInstance = NULL;
BOOL LLPanelLogin::sCapslockDidNotification = FALSE;
BOOL LLPanelLogin::sCredentialSet = FALSE;

// Helper functions

LLPointer<LLCredential> load_user_credentials(std::string &user_key)
{
    if (gSecAPIHandler->hasCredentialMap("login_list", LLGridManager::getInstance()->getGrid()))
    {
        // user_key should be of "name Resident" format
        return gSecAPIHandler->loadFromCredentialMap("login_list", LLGridManager::getInstance()->getGrid(), user_key);
    }
    else
    {
        // legacy (or legacy^2, since it also tries to load from settings)
        return gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
    }
}

// keys are lower case to be case insensitive so they are not always
// identical to names which retain user input, like:
// "AwEsOmE Resident" -> "awesome_resident"
std::string get_user_key_from_name(const std::string &username)
{
    std::string key = username;
    LLStringUtil::trim(key);
    LLStringUtil::toLower(key);
    if (!LLGridManager::getInstance()->isSystemGrid())
    {
        size_t separator_index = username.find_first_of(" ");
        if (separator_index == username.npos)
        {
            // CRED_IDENTIFIER_TYPE_ACCOUNT
            return key;
        }
    }
    // CRED_IDENTIFIER_TYPE_AGENT
    size_t separator_index = username.find_first_of(" ._");
    std::string first = username.substr(0, separator_index);
    std::string last;
    if (separator_index != username.npos)
    {
        last = username.substr(separator_index + 1, username.npos);
        LLStringUtil::trim(last);
    }
    else
    {
        // ...on Linden grids, single username users as considered to have
        // last name "Resident"
        // *TODO: Make login.cgi support "account_name" like above
        last = "resident";
    }

    key = first + "_" + last;
    return key;
}

class LLLoginLocationAutoHandler : public LLCommandHandler
{
public:
	// don't allow from external browsers
	LLLoginLocationAutoHandler() : LLCommandHandler("location_login", UNTRUSTED_BLOCK) { }
	bool handle(const LLSD& tokens, const LLSD& query_map, LLMediaCtrl* web)
	{	
		if (LLStartUp::getStartupState() < STATE_LOGIN_CLEANUP)
		{
			if ( tokens.size() == 0 || tokens.size() > 4 ) 
				return false;

			// unescape is important - uris with spaces are escaped in this code path
			// (e.g. space -> %20) and the code to log into a region doesn't support that.
			const std::string region = LLURI::unescape( tokens[0].asString() );

			// just region name as payload 
			if ( tokens.size() == 1 )
			{
				// region name only - slurl will end up as center of region
				LLSLURL slurl(region);
				LLPanelLogin::autologinToLocation(slurl);
			}
			else
			// region name and x coord as payload 
			if ( tokens.size() == 2 )
			{
				// invalid to only specify region and x coordinate
				// slurl code will revert to same as region only, so do this anyway
				LLSLURL slurl(region);
				LLPanelLogin::autologinToLocation(slurl);
			}
			else
			// region name and x/y coord as payload 
			if ( tokens.size() == 3 )
			{
				// region and x/y specified - default z to 0
				F32 xpos;
				std::istringstream codec(tokens[1].asString());
				codec >> xpos;

				F32 ypos;
				codec.clear();
				codec.str(tokens[2].asString());
				codec >> ypos;

				const LLVector3 location(xpos, ypos, 0.0f);
				LLSLURL slurl(region, location);

				LLPanelLogin::autologinToLocation(slurl);
			}
			else
			// region name and x/y/z coord as payload 
			if ( tokens.size() == 4 )
			{
				// region and x/y/z specified - ok
				F32 xpos;
				std::istringstream codec(tokens[1].asString());
				codec >> xpos;

				F32 ypos;
				codec.clear();
				codec.str(tokens[2].asString());
				codec >> ypos;

				F32 zpos;
				codec.clear();
				codec.str(tokens[3].asString());
				codec >> zpos;

				const LLVector3 location(xpos, ypos, zpos);
				LLSLURL slurl(region, location);

				LLPanelLogin::autologinToLocation(slurl);
			};
		}	
		return true;
	}
};
LLLoginLocationAutoHandler gLoginLocationAutoHandler;

//---------------------------------------------------------------------------
// Public methods
//---------------------------------------------------------------------------
LLPanelLogin::LLPanelLogin(const LLRect &rect,
						 void (*callback)(S32 option, void* user_data),
						 void *cb_data)
:	LLPanel(),
	mCallback(callback),
	mCallbackData(cb_data),
	mListener(new LLPanelLoginListener(this)),
	mFirstLoginThisInstall(gSavedSettings.getBOOL("FirstLoginThisInstall")),
	mUsernameLength(0),
	mPasswordLength(0),
	mLocationLength(0),
	mShowFavorites(false)
{
	setBackgroundVisible(FALSE);
	setBackgroundOpaque(TRUE);

	mPasswordModified = FALSE;

	sInstance = this;

	LLView* login_holder = gViewerWindow->getLoginPanelHolder();
	if (login_holder)
	{
		login_holder->addChild(this);
	}

	if (mFirstLoginThisInstall)
	{
		buildFromFile( "panel_login_first.xml");
	}
	else
	{
		buildFromFile( "panel_login.xml");
	}

	reshape(rect.getWidth(), rect.getHeight());

	LLLineEditor* password_edit(getChild<LLLineEditor>("password_edit"));
	password_edit->setKeystrokeCallback(onPassKey, this);
	// STEAM-14: When user presses Enter with this field in focus, initiate login
	password_edit->setCommitCallback(boost::bind(&LLPanelLogin::onClickConnect, this));

	// change z sort of clickable text to be behind buttons
	sendChildToBack(getChildView("forgot_password_text"));
	sendChildToBack(getChildView("sign_up_text"));

    std::string current_grid = LLGridManager::getInstance()->getGrid();
    if (!mFirstLoginThisInstall)
    {
        LLComboBox* favorites_combo = getChild<LLComboBox>("start_location_combo");
        updateLocationSelectorsVisibility(); // separate so that it can be called from preferences
        favorites_combo->setReturnCallback(boost::bind(&LLPanelLogin::onClickConnect, this));
        favorites_combo->setFocusLostCallback(boost::bind(&LLPanelLogin::onLocationSLURL, this));

        LLComboBox* server_choice_combo = getChild<LLComboBox>("server_combo");
        server_choice_combo->setCommitCallback(boost::bind(&LLPanelLogin::onSelectServer, this));

        // Load all of the grids, sorted, and then add a bar and the current grid at the top
        server_choice_combo->removeall();

        std::map<std::string, std::string> known_grids = LLGridManager::getInstance()->getKnownGrids();
        for (std::map<std::string, std::string>::iterator grid_choice = known_grids.begin();
            grid_choice != known_grids.end();
            grid_choice++)
        {
            if (!grid_choice->first.empty() && current_grid != grid_choice->first)
            {
                LL_DEBUGS("AppInit") << "adding " << grid_choice->first << LL_ENDL;
                server_choice_combo->add(grid_choice->second, grid_choice->first);
            }
        }
        server_choice_combo->sortByName();

        LL_DEBUGS("AppInit") << "adding current " << current_grid << LL_ENDL;
        server_choice_combo->add(LLGridManager::getInstance()->getGridLabel(),
            current_grid,
            ADD_TOP);
        server_choice_combo->selectFirstItem();
    }

	LLSLURL start_slurl(LLStartUp::getStartSLURL());
	// The StartSLURL might have been set either by an explicit command-line
	// argument (CmdLineLoginLocation) or by default.
	// current_grid might have been set either by an explicit command-line
	// argument (CmdLineGridChoice) or by default.
	// If the grid specified by StartSLURL is the same as current_grid, the
	// distinction is moot.
	// If we have an explicit command-line SLURL, use that.
	// If we DON'T have an explicit command-line SLURL but we DO have an
	// explicit command-line grid, which is different from the default SLURL's
	// -- do NOT override the explicit command-line grid with the grid from
	// the default SLURL!
	bool force_grid{ start_slurl.getGrid() != current_grid &&
					 gSavedSettings.getString("CmdLineLoginLocation").empty() &&
				   ! gSavedSettings.getString("CmdLineGridChoice").empty() };
	if ( !start_slurl.isSpatial() ) // has a start been established by the command line or NextLoginLocation ? 
	{
		// no, so get the preference setting
		std::string defaultStartLocation = gSavedSettings.getString("LoginLocation");
		LL_INFOS("AppInit")<<"default LoginLocation '"<<defaultStartLocation<<"'"<<LL_ENDL;
		LLSLURL defaultStart(defaultStartLocation);
		if ( defaultStart.isSpatial() && ! force_grid )
		{
			LLStartUp::setStartSLURL(defaultStart);
		}
		else
		{
			LL_INFOS("AppInit") << (force_grid? "--grid specified" : "no valid LoginLocation")
								<< ", using home" << LL_ENDL;
			LLSLURL homeStart(LLSLURL::SIM_LOCATION_HOME);
			LLStartUp::setStartSLURL(homeStart);
		}
	}
	else if (! force_grid)
	{
		onUpdateStartSLURL(start_slurl); // updates grid if needed
	}
	
	childSetAction("connect_btn", onClickConnect, this);

	LLButton* def_btn = getChild<LLButton>("connect_btn");
	setDefaultBtn(def_btn);

	std::string channel = LLVersionInfo::getChannel();
	std::string version = llformat("%s (%d)",
								   LLVersionInfo::getShortVersion().c_str(),
								   LLVersionInfo::getBuild());
	
	LLTextBox* forgot_password_text = getChild<LLTextBox>("forgot_password_text");
	forgot_password_text->setClickedCallback(onClickForgotPassword, NULL);

	LLTextBox* sign_up_text = getChild<LLTextBox>("sign_up_text");
	sign_up_text->setClickedCallback(onClickSignUp, NULL);

	// get the web browser control
	LLMediaCtrl* web_browser = getChild<LLMediaCtrl>("login_html");
	web_browser->addObserver(this);

	loadLoginPage();

	LLComboBox* username_combo(getChild<LLComboBox>("username_combo"));
	username_combo->setTextChangedCallback(boost::bind(&LLPanelLogin::onUserNameTextEnty, this));
	// STEAM-14: When user presses Enter with this field in focus, initiate login
	username_combo->setCommitCallback(boost::bind(&LLPanelLogin::onUserListCommit, this));
	username_combo->setReturnCallback(boost::bind(&LLPanelLogin::onClickConnect, this));
	username_combo->setKeystrokeOnEsc(TRUE);

    if (!mFirstLoginThisInstall)
    {
        LLCheckBoxCtrl* remember_name = getChild<LLCheckBoxCtrl>("remember_name");
        remember_name->setCommitCallback(boost::bind(&LLPanelLogin::onRememberUserCheck, this));
    }
}

void LLPanelLogin::addFavoritesToStartLocation()
{
    if (mFirstLoginThisInstall)
    {
        // first login panel has no favorites, just update name length and buttons
        std::string user_defined_name = getChild<LLComboBox>("username_combo")->getSimple();
        mUsernameLength = user_defined_name.length();
        updateLoginButtons();
        return;
    }

	// Clear the combo.
	LLComboBox* combo = getChild<LLComboBox>("start_location_combo");
	if (!combo) return;
	int num_items = combo->getItemCount();
	for (int i = num_items - 1; i > 1; i--)
	{
		combo->remove(i);
	}

	// Load favorites into the combo.
	std::string user_defined_name = getChild<LLComboBox>("username_combo")->getSimple();
	LLStringUtil::trim(user_defined_name);
	LLStringUtil::toLower(user_defined_name);
	std::string filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "stored_favorites_" + LLGridManager::getInstance()->getGrid() + ".xml");
	std::string old_filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "stored_favorites.xml");
	mUsernameLength = user_defined_name.length();
	updateLoginButtons();

	std::string::size_type index = user_defined_name.find_first_of(" ._");
	if (index != std::string::npos)
	{
		std::string username = user_defined_name.substr(0, index);
		std::string lastname = user_defined_name.substr(index+1);
		if (lastname == "resident")
		{
			user_defined_name = username;
		}
		else
		{
			user_defined_name = username + " " + lastname;
		}
	}

	LLSD fav_llsd;
	llifstream file;
	file.open(filename.c_str());
	if (!file.is_open())
	{
		file.open(old_filename.c_str());
		if (!file.is_open()) return;
	}
	LLSDSerialize::fromXML(fav_llsd, file);

	for (LLSD::map_const_iterator iter = fav_llsd.beginMap();
		iter != fav_llsd.endMap(); ++iter)
	{
		// The account name in stored_favorites.xml has Resident last name even if user has
		// a single word account name, so it can be compared case-insensitive with the
		// user defined "firstname lastname".
		S32 res = LLStringUtil::compareInsensitive(user_defined_name, iter->first);
		if (res != 0)
		{
			LL_DEBUGS() << "Skipping favorites for " << iter->first << LL_ENDL;
			continue;
		}

		combo->addSeparator();
		LL_DEBUGS() << "Loading favorites for " << iter->first << LL_ENDL;
		LLSD user_llsd = iter->second;
		for (LLSD::array_const_iterator iter1 = user_llsd.beginArray();
			iter1 != user_llsd.endArray(); ++iter1)
		{
			std::string label = (*iter1)["name"].asString();
			std::string value = (*iter1)["slurl"].asString();
			if(label != "" && value != "")
			{
				mShowFavorites = true;
				combo->add(label, value);
				if ( LLStartUp::getStartSLURL().getSLURLString() == value)
				{
					combo->selectByValue(value);
				}
			}
		}
		break;
	}
}

LLPanelLogin::~LLPanelLogin()
{
	LLPanelLogin::sInstance = NULL;

	// Controls having keyboard focus by default
	// must reset it on destroy. (EXT-2748)
	gFocusMgr.setDefaultKeyboardFocus(NULL);
}

// virtual
void LLPanelLogin::setFocus(BOOL b)
{
	if(b != hasFocus())
	{
		if(b)
		{
			giveFocus();
		}
		else
		{
			LLPanel::setFocus(b);
		}
	}
}

// static
void LLPanelLogin::giveFocus()
{
	if( sInstance )
	{
		// Grab focus and move cursor to first blank input field
		std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
		std::string pass = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();

		BOOL have_username = !username.empty();
		BOOL have_pass = !pass.empty();

		LLLineEditor* edit = NULL;
		LLComboBox* combo = NULL;
		if (have_username && !have_pass)
		{
			// User saved his name but not his password.  Move
			// focus to password field.
			edit = sInstance->getChild<LLLineEditor>("password_edit");
		}
		else
		{
			// User doesn't have a name, so start there.
			combo = sInstance->getChild<LLComboBox>("username_combo");
		}

		if (edit)
		{
			edit->setFocus(TRUE);
			edit->selectAll();
		}
		else if (combo)
		{
			combo->setFocus(TRUE);
		}
	}
}

// static
void LLPanelLogin::show(const LLRect &rect,
						void (*callback)(S32 option, void* user_data),
						void* callback_data)
{
    if (!LLPanelLogin::sInstance)
    {
        new LLPanelLogin(rect, callback, callback_data);
    }

	if( !gFocusMgr.getKeyboardFocus() )
	{
		// Grab focus and move cursor to first enabled control
		sInstance->setFocus(TRUE);
	}

	// Make sure that focus always goes here (and use the latest sInstance that was just created)
	gFocusMgr.setDefaultKeyboardFocus(sInstance);
}

//static
void LLPanelLogin::populateFields(LLPointer<LLCredential> credential, bool remember_user, bool remember_psswrd)
{
    if (!sInstance)
    {
        LL_WARNS() << "Attempted fillFields with no login view shown" << LL_ENDL;
        return;
    }
    if (sInstance->mFirstLoginThisInstall)
    {
        LLUICtrl* remember_check = sInstance->getChild<LLUICtrl>("remember_check");
        remember_check->setValue(remember_psswrd);
        // no list to populate
        setFields(credential);
    }
    else
    {
        sInstance->getChild<LLUICtrl>("remember_name")->setValue(remember_user);
        LLUICtrl* remember_password = sInstance->getChild<LLUICtrl>("remember_password");
        remember_password->setValue(remember_psswrd);
        remember_password->setEnabled(remember_user);
        sInstance->populateUserList(credential);
    }
}

//static
void LLPanelLogin::resetFields()
{
    if (!sInstance)
    {
        // class not existing at this point might happen since this
        // function is used to reset list in case of changes by external sources
        return;
    }
    if (sInstance->mFirstLoginThisInstall)
    {
        // no list to populate
        LL_WARNS() << "Shouldn't happen, user should have no ability to modify list on first install" << LL_ENDL;
    }
    else
    {
        LLPointer<LLCredential> cred = gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
        sInstance->populateUserList(cred);
    }
}

// static
void LLPanelLogin::setFields(LLPointer<LLCredential> credential)
{
	if (!sInstance)
	{
		LL_WARNS() << "Attempted fillFields with no login view shown" << LL_ENDL;
		return;
	}
	sCredentialSet = TRUE;
	LL_INFOS("Credentials") << "Setting login fields to " << *credential << LL_ENDL;

	LLSD identifier = credential.notNull() ? credential->getIdentifier() : LLSD();

	if(identifier.has("type") && (std::string)identifier["type"] == "agent") 
	{
		// not nessesary for panel_login.xml, needed for panel_login_first.xml
		std::string firstname = identifier["first_name"].asString();
		std::string lastname = identifier["last_name"].asString();
	    std::string login_id = firstname;
	    if (!lastname.empty() && lastname != "Resident" && lastname != "resident")
	    {
		    // support traditional First Last name SLURLs
		    login_id += " ";
		    login_id += lastname;
	    }
		sInstance->getChild<LLComboBox>("username_combo")->setLabel(login_id);
		sInstance->mUsernameLength = login_id.length();
	}
	else if(identifier.has("type") && (std::string)identifier["type"] == "account")
	{
		std::string login_id = identifier["account_name"].asString();
		sInstance->getChild<LLComboBox>("username_combo")->setLabel(login_id);
		sInstance->mUsernameLength = login_id.length();
	}
	else
	{
		sInstance->getChild<LLComboBox>("username_combo")->setLabel(std::string());
		sInstance->mUsernameLength = 0;
	}

	sInstance->addFavoritesToStartLocation();
	// if the password exists in the credential, set the password field with
	// a filler to get some stars
	LLSD authenticator = credential.notNull() ? credential->getAuthenticator() : LLSD();
	LL_INFOS("Credentials") << "Setting authenticator field " << authenticator["type"].asString() << LL_ENDL;
	if(authenticator.isMap() && 
	   authenticator.has("secret") && 
	   (authenticator["secret"].asString().size() > 0))
	{
		
		// This is a MD5 hex digest of a password.
		// We don't actually use the password input field, 
		// fill it with MAX_PASSWORD characters so we get a 
		// nice row of asterisks.
		const std::string filler("123456789!123456");
		sInstance->getChild<LLUICtrl>("password_edit")->setValue(filler);
		sInstance->mPasswordLength = filler.length();
		sInstance->updateLoginButtons();
	}
	else
	{
		sInstance->getChild<LLUICtrl>("password_edit")->setValue(std::string());
		sInstance->mPasswordLength = 0;
	}
}

// static
void LLPanelLogin::getFields(LLPointer<LLCredential>& credential,
							 bool& remember_user,
							 bool& remember_psswrd)
{
	if (!sInstance)
	{
		LL_WARNS() << "Attempted getFields with no login view shown" << LL_ENDL;
		return;
	}

	LLSD identifier = LLSD::emptyMap();
	LLSD authenticator = LLSD::emptyMap();

	std::string username = sInstance->getChild<LLComboBox>("username_combo")->getSimple();
	std::string password = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
	LLStringUtil::trim(username);

	LL_INFOS("Credentials", "Authentication") << "retrieving username:" << username << LL_ENDL;
	// determine if the username is a first/last form or not.
	size_t separator_index = username.find_first_of(' ');
	if (separator_index == username.npos
		&& !LLGridManager::getInstance()->isSystemGrid())
	{
		LL_INFOS("Credentials", "Authentication") << "account: " << username << LL_ENDL;
		// single username, so this is a 'clear' identifier
		identifier["type"] = CRED_IDENTIFIER_TYPE_ACCOUNT;
		identifier["account_name"] = username;
		
		if (LLPanelLogin::sInstance->mPasswordModified)
		{
			authenticator = LLSD::emptyMap();
			// password is plaintext
			authenticator["type"] = CRED_AUTHENTICATOR_TYPE_CLEAR;
			authenticator["secret"] = password;
		}
        else
        {
            credential = load_user_credentials(username);
            if (credential.notNull())
            {
                authenticator = credential->getAuthenticator();
            }
        }
	}
	else
	{
		// Be lenient in terms of what separators we allow for two-word names
		// and allow legacy users to login with firstname.lastname
		separator_index = username.find_first_of(" ._");
		std::string first = username.substr(0, separator_index);
		std::string last;
		if (separator_index != username.npos)
		{
			last = username.substr(separator_index+1, username.npos);
			LLStringUtil::trim(last);
		}
		else
		{
			// ...on Linden grids, single username users as considered to have
			// last name "Resident"
			// *TODO: Make login.cgi support "account_name" like above
			last = "Resident";
		}
		
		if (last.find_first_of(' ') == last.npos)
		{
			LL_INFOS("Credentials", "Authentication") << "agent: " << username << LL_ENDL;
			// traditional firstname / lastname
			identifier["type"] = CRED_IDENTIFIER_TYPE_AGENT;
			identifier["first_name"] = first;
			identifier["last_name"] = last;
		
			if (LLPanelLogin::sInstance->mPasswordModified)
			{
				authenticator = LLSD::emptyMap();
				authenticator["type"] = CRED_AUTHENTICATOR_TYPE_HASH;
				authenticator["algorithm"] = "md5";
				LLMD5 pass((const U8 *)password.c_str());
				char md5pass[33];               /* Flawfinder: ignore */
				pass.hex_digest(md5pass);
				authenticator["secret"] = md5pass;
			}
            else
            {
                std::string key = first + "_" + last;
                LLStringUtil::toLower(key);
                credential = load_user_credentials(key);
                if (credential.notNull())
                {
                    authenticator = credential->getAuthenticator();
                }
            }
		}
	}
	credential = gSecAPIHandler->createCredential(LLGridManager::getInstance()->getGrid(), identifier, authenticator);
    if (!sInstance->mFirstLoginThisInstall)
    {
        remember_psswrd = sInstance->getChild<LLUICtrl>("remember_password")->getValue();
        remember_user = sInstance->getChild<LLUICtrl>("remember_name")->getValue();
    }
    else
    {
        remember_psswrd = sInstance->getChild<LLUICtrl>("remember_check")->getValue();
        remember_user = remember_psswrd; // on panel_login_first "remember_check" is named as 'remember me'
    }
}


// static
BOOL LLPanelLogin::areCredentialFieldsDirty()
{
	if (!sInstance)
	{
		LL_WARNS() << "Attempted getServer with no login view shown" << LL_ENDL;
	}
	else
	{
		LLComboBox* combo = sInstance->getChild<LLComboBox>("username_combo");
		if (combo && combo->getCurrentIndex() == -1 && !combo->getValue().asString().empty())
		{
			return true;
		}
		LLLineEditor* ctrl = sInstance->getChild<LLLineEditor>("password_edit");
		if(ctrl && ctrl->isDirty()) 
		{
			return true;
		}
	}
	return false;	
}


// static
void LLPanelLogin::updateLocationSelectorsVisibility()
{
	if (sInstance) 
	{
		BOOL show_server = gSavedSettings.getBOOL("ForceShowGrid");
		LLComboBox* server_combo = sInstance->getChild<LLComboBox>("server_combo");
		if ( server_combo ) 
		{
			server_combo->setVisible(show_server);
		}
	}	
}

// static - called from LLStartUp::setStartSLURL
void LLPanelLogin::onUpdateStartSLURL(const LLSLURL& new_start_slurl)
{
	if (!sInstance) return;

	LL_DEBUGS("AppInit")<<new_start_slurl.asString()<<LL_ENDL;

	LLComboBox* location_combo = sInstance->getChild<LLComboBox>("start_location_combo");
	/*
	 * Determine whether or not the new_start_slurl modifies the grid.
	 *
	 * Note that some forms that could be in the slurl are grid-agnostic.,
	 * such as "home".  Other forms, such as
	 * https://grid.example.com/region/Party%20Town/20/30/5 
	 * specify a particular grid; in those cases we want to change the grid
	 * and the grid selector to match the new value.
	 */
	enum LLSLURL::SLURL_TYPE new_slurl_type = new_start_slurl.getType();
	switch ( new_slurl_type )
	{
	case LLSLURL::LOCATION:
	  {
		std::string slurl_grid = LLGridManager::getInstance()->getGrid(new_start_slurl.getGrid());
		if ( ! slurl_grid.empty() ) // is that a valid grid?
		{
			if ( slurl_grid != LLGridManager::getInstance()->getGrid() ) // new grid?
			{
				// the slurl changes the grid, so update everything to match
				LLGridManager::getInstance()->setGridChoice(slurl_grid);

				// update the grid selector to match the slurl
				LLComboBox* server_combo = sInstance->getChild<LLComboBox>("server_combo");
				std::string server_label(LLGridManager::getInstance()->getGridLabel(slurl_grid));
				server_combo->setSimple(server_label);

				updateServer(); // to change the links and splash screen
			}
			if ( new_start_slurl.getLocationString().length() )
			{
					
				location_combo->setLabel(new_start_slurl.getLocationString());
				sInstance->mLocationLength = new_start_slurl.getLocationString().length();
				sInstance->updateLoginButtons();
			}
		}
		else
		{
			// the grid specified by the slurl is not known
			LLNotificationsUtil::add("InvalidLocationSLURL");
			LL_WARNS("AppInit")<<"invalid LoginLocation:"<<new_start_slurl.asString()<<LL_ENDL;
			location_combo->setTextEntry(LLStringUtil::null);
		}
	  }
 	break;

	case LLSLURL::HOME_LOCATION:
		//location_combo->setCurrentByIndex(0); // home location
		break;

	default:
		LL_WARNS("AppInit")<<"invalid login slurl, using home"<<LL_ENDL;
		//location_combo->setCurrentByIndex(0); // home location
		break;
	}
}

void LLPanelLogin::setLocation(const LLSLURL& slurl)
{
	LL_DEBUGS("AppInit")<<"setting Location "<<slurl.asString()<<LL_ENDL;
	LLStartUp::setStartSLURL(slurl); // calls onUpdateStartSLURL, above
}

void LLPanelLogin::autologinToLocation(const LLSLURL& slurl)
{
	LL_DEBUGS("AppInit")<<"automatically logging into Location "<<slurl.asString()<<LL_ENDL;
	LLStartUp::setStartSLURL(slurl); // calls onUpdateStartSLURL, above

	if ( LLPanelLogin::sInstance != NULL )
	{
		void* unused_parameter = 0;
		LLPanelLogin::sInstance->onClickConnect(unused_parameter);
	}
}


// static
void LLPanelLogin::closePanel()
{
	if (sInstance)
	{
		if (LLPanelLogin::sInstance->getParent())
		{
			LLPanelLogin::sInstance->getParent()->removeChild(LLPanelLogin::sInstance);
		}

		delete sInstance;
		sInstance = NULL;
	}
}

// static
void LLPanelLogin::setAlwaysRefresh(bool refresh)
{
	if (sInstance && LLStartUp::getStartupState() < STATE_LOGIN_CLEANUP)
	{
		LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");

		if (web_browser)
		{
			web_browser->setAlwaysRefresh(refresh);
		}
	}
}



void LLPanelLogin::loadLoginPage()
{
	if (!sInstance) return;

	LLURI login_page = LLURI(LLGridManager::getInstance()->getLoginPage());
	LLSD params(login_page.queryMap());

	LL_DEBUGS("AppInit") << "login_page: " << login_page << LL_ENDL;

	// allow users (testers really) to specify a different login content URL
	std::string force_login_url = gSavedSettings.getString("ForceLoginURL");
	if ( force_login_url.length() > 0 )
	{
		login_page = LLURI(force_login_url);
	}

	// Language
	params["lang"] = LLUI::getLanguage();

	// First Login?
	if (gSavedSettings.getBOOL("FirstLoginThisInstall"))
	{
		params["firstlogin"] = "TRUE"; // not bool: server expects string TRUE
	}

	// Channel and Version
	params["version"] = llformat("%s (%d)",
								 LLVersionInfo::getShortVersion().c_str(),
								 LLVersionInfo::getBuild());
	params["channel"] = LLVersionInfo::getChannel();

	// Grid
	params["grid"] = LLGridManager::getInstance()->getGridId();

	// add OS info
	params["os"] = LLOSInfo::instance().getOSStringSimple();

	// sourceid
	params["sourceid"] = gSavedSettings.getString("sourceid");

	// login page (web) content version
	params["login_content_version"] = gSavedSettings.getString("LoginContentVersion");

	// Make an LLURI with this augmented info
	std::string url = login_page.scheme().empty()? login_page.authority() : login_page.scheme() + "://" + login_page.authority();
	LLURI login_uri(LLURI::buildHTTP(url,
									 login_page.path(),
									 params));

	gViewerWindow->setMenuBackgroundColor(false, !LLGridManager::getInstance()->isInProductionGrid());

	LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
	if (web_browser->getCurrentNavUrl() != login_uri.asString())
	{
		LL_DEBUGS("AppInit") << "loading:    " << login_uri << LL_ENDL;
		web_browser->navigateTo( login_uri.asString(), "text/html" );
	}
}

void LLPanelLogin::handleMediaEvent(LLPluginClassMedia* /*self*/, EMediaEvent event)
{
}

//---------------------------------------------------------------------------
// Protected methods
//---------------------------------------------------------------------------
// static
void LLPanelLogin::onClickConnect(void *)
{
	if (sInstance && sInstance->mCallback)
	{
		// JC - Make sure the fields all get committed.
		sInstance->setFocus(FALSE);

		LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
		LLSD combo_val = combo->getSelectedValue();

		// the grid definitions may come from a user-supplied grids.xml, so they may not be good
		LL_DEBUGS("AppInit")<<"grid "<<combo_val.asString()<<LL_ENDL;
		try
		{
			LLGridManager::getInstance()->setGridChoice(combo_val.asString());
		}
		catch (LLInvalidGridName ex)
		{
			LLSD args;
			args["GRID"] = ex.name();
			LLNotificationsUtil::add("InvalidGrid", args);
			return;
		}

		// The start location SLURL has already been sent to LLStartUp::setStartSLURL

		std::string username = sInstance->getChild<LLUICtrl>("username_combo")->getValue().asString();
		std::string password = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
		
		if(username.empty())
		{
			// user must type in something into the username field
			LLNotificationsUtil::add("MustHaveAccountToLogIn");
		}
		else if(password.empty())
		{
		    LLNotificationsUtil::add("MustEnterPasswordToLogIn");
		}
		else
		{
			sCredentialSet = FALSE;
			LLPointer<LLCredential> cred;
			bool remember_1, remember_2;
			getFields(cred, remember_1, remember_2);
			std::string identifier_type;
			cred->identifierType(identifier_type);
			LLSD allowed_credential_types;
			LLGridManager::getInstance()->getLoginIdentifierTypes(allowed_credential_types);
			
			// check the typed in credential type against the credential types expected by the server.
			for(LLSD::array_iterator i = allowed_credential_types.beginArray();
				i != allowed_credential_types.endArray();
				i++)
			{
				
				if(i->asString() == identifier_type)
				{
					// yay correct credential type
					sInstance->mCallback(0, sInstance->mCallbackData);
					return;
				}
			}
			
			// Right now, maingrid is the only thing that is picky about
			// credential format, as it doesn't yet allow account (single username)
			// format creds.  - Rox.  James, we wanna fix the message when we change
			// this.
			LLNotificationsUtil::add("InvalidCredentialFormat");			
		}
	}
}

// static
void LLPanelLogin::onClickVersion(void*)
{
	LLFloaterReg::showInstance("sl_about"); 
}

//static
void LLPanelLogin::onClickForgotPassword(void*)
{
	if (sInstance )
	{
		LLWeb::loadURLExternal(sInstance->getString( "forgot_password_url" ));
	}
}

//static
void LLPanelLogin::onClickSignUp(void*)
{
	if (sInstance)
	{
		LLWeb::loadURLExternal(sInstance->getString("sign_up_url"));
	}
}

// static
void LLPanelLogin::onUserNameTextEnty(void*)
{
    sInstance->mPasswordModified = true;
    sInstance->getChild<LLUICtrl>("password_edit")->setValue(std::string());
    sInstance->mPasswordLength = 0;
    sInstance->addFavoritesToStartLocation(); //will call updateLoginButtons()
}

// static
void LLPanelLogin::onUserListCommit(void*)
{
    if (sInstance)
    {
        LLComboBox* username_combo(sInstance->getChild<LLComboBox>("username_combo"));
        static S32 ind = -1;
        if (ind != username_combo->getCurrentIndex())
        {
            std::string user_key = username_combo->getSelectedValue();
            LLPointer<LLCredential> cred = gSecAPIHandler->loadFromCredentialMap("login_list", LLGridManager::getInstance()->getGrid(), user_key);
            setFields(cred);
            sInstance->mPasswordModified = false;
        }
        else
        {
           std::string pass = sInstance->getChild<LLUICtrl>("password_edit")->getValue().asString();
           if (pass.empty())
           {
               sInstance->giveFocus();
           }
           else
           {
               onClickConnect(NULL);
           }
        }
    }
}

// static
// At the moment only happens if !mFirstLoginThisInstall
void LLPanelLogin::onRememberUserCheck(void*)
{
    if (sInstance && !sInstance->mFirstLoginThisInstall)
    {
        LLCheckBoxCtrl* remember_name(sInstance->getChild<LLCheckBoxCtrl>("remember_name"));
        LLCheckBoxCtrl* remember_psswrd(sInstance->getChild<LLCheckBoxCtrl>("remember_password"));
        LLComboBox* user_combo(sInstance->getChild<LLComboBox>("username_combo"));

        bool remember = remember_name->getValue().asBoolean();
        if (user_combo->getCurrentIndex() != -1 && !remember)
        {
            remember = true;
            remember_name->setValue(true);
            LLNotificationsUtil::add("LoginCantRemoveUsername");
        }
        remember_psswrd->setEnabled(remember);
    }
}

// static
void LLPanelLogin::onPassKey(LLLineEditor* caller, void* user_data)
{
	LLPanelLogin *self = (LLPanelLogin *)user_data;
	self->mPasswordModified = TRUE;
	if (gKeyboard->getKeyDown(KEY_CAPSLOCK) && sCapslockDidNotification == FALSE)
	{
		// *TODO: use another way to notify user about enabled caps lock, see EXT-6858
		sCapslockDidNotification = TRUE;
	}

	LLLineEditor* password_edit(self->getChild<LLLineEditor>("password_edit"));
	self->mPasswordLength = password_edit->getText().length();
	self->updateLoginButtons();
}


void LLPanelLogin::updateServer()
{
	if (sInstance)
	{
		try 
		{
			// if they've selected another grid, we should load the credentials
			// for that grid and set them to the UI. But if there were any modifications to
			// fields, modifications should carry over.
			// Not sure if it should carry over password but it worked like this before login changes
			// Example: you started typing in and found that your are under wrong grid,
			// you switch yet don't lose anything
			if (sInstance->areCredentialFieldsDirty())
			{
				// save modified creds
				LLComboBox* user_combo = sInstance->getChild<LLComboBox>("username_combo");
				LLLineEditor* pswd_edit = sInstance->getChild<LLLineEditor>("password_edit");
				std::string username = user_combo->getSimple();
				LLStringUtil::trim(username);
				std::string password = pswd_edit->getValue().asString();

				// populate dropbox and setFields
				// Note: following call is related to initializeLoginInfo()
				LLPointer<LLCredential> credential = gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
				sInstance->populateUserList(credential);

				// restore creds
				user_combo->setTextEntry(username);
				pswd_edit->setValue(password);
				sInstance->mUsernameLength = username.length();
				sInstance->mPasswordLength = password.length();
			}
			else
			{
				// populate dropbox and setFields
				// Note: following call is related to initializeLoginInfo()
				LLPointer<LLCredential> credential = gSecAPIHandler->loadCredential(LLGridManager::getInstance()->getGrid());
				sInstance->populateUserList(credential);
			}

			// update the login panel links 
			bool system_grid = LLGridManager::getInstance()->isSystemGrid();

			// Want to vanish not only create_new_account_btn, but also the
			// title text over it, so turn on/off the whole layout_panel element.
			sInstance->getChild<LLLayoutPanel>("links")->setVisible(system_grid);
			sInstance->getChildView("forgot_password_text")->setVisible(system_grid);

			// grid changed so show new splash screen (possibly)
			loadLoginPage();
		}
		catch (LLInvalidGridName ex)
		{
			LL_WARNS("AppInit")<<"server '"<<ex.name()<<"' selection failed"<<LL_ENDL;
			LLSD args;
			args["GRID"] = ex.name();
			LLNotificationsUtil::add("InvalidGrid", args);	
			return;
		}
	}
}

void LLPanelLogin::updateLoginButtons()
{
	LLButton* login_btn = getChild<LLButton>("connect_btn");

	login_btn->setEnabled(mUsernameLength != 0 && mPasswordLength != 0);

	if (!mFirstLoginThisInstall)
	{
		LLComboBox* user_combo = getChild<LLComboBox>("username_combo");
		LLCheckBoxCtrl* remember_name = getChild<LLCheckBoxCtrl>("remember_name");
		if (user_combo->getCurrentIndex() != -1)
		{
			remember_name->setValue(true);
		} // Note: might be good idea to do "else remember_name->setValue(mRememberedState)" but it might behave 'weird' to user
	}
}

void LLPanelLogin::populateUserList(LLPointer<LLCredential> credential)
{
    LLComboBox* user_combo = getChild<LLComboBox>("username_combo");
    user_combo->removeall();
    user_combo->clear();
    mUsernameLength = 0;
    mPasswordLength = 0;

    if (gSecAPIHandler->hasCredentialMap("login_list", LLGridManager::getInstance()->getGrid()))
    {
        LLSecAPIHandler::credential_map_t credencials;
        gSecAPIHandler->loadCredentialMap("login_list", LLGridManager::getInstance()->getGrid(), credencials);

        LLSecAPIHandler::credential_map_t::iterator cr_iter = credencials.begin();
        LLSecAPIHandler::credential_map_t::iterator cr_end = credencials.end();
        while (cr_iter != cr_end)
        {
            if (cr_iter->second.notNull()) // basic safety in case of future changes
            {
                // cr_iter->first == user_id , to be able to be find it in case we select it
                user_combo->add(LLPanelLogin::getUserName(cr_iter->second), cr_iter->first, ADD_BOTTOM, TRUE);
            }
            cr_iter++;
        }

        if (credential.isNull() || !user_combo->setSelectedByValue(LLSD(credential->userID()), true))
        {
            // selection failed, just deselect whatever might be selected
            user_combo->setValue(std::string());
            getChild<LLUICtrl>("password_edit")->setValue(std::string());
            updateLoginButtons();
        }
        else
        {
            setFields(credential);
        }
    }
    else
    {
        if (credential.notNull())
        {
            const LLSD &ident = credential->getIdentifier();
            if (ident.isMap() && ident.has("type"))
            {
                user_combo->add(LLPanelLogin::getUserName(credential), credential->userID(), ADD_BOTTOM, TRUE);
                setFields(credential);
            }
            else
            {
                updateLoginButtons();
            }
        }
        else
        {
            updateLoginButtons();
        }
    }
}


void LLPanelLogin::onSelectServer()
{
	// The user twiddled with the grid choice ui.
	// apply the selection to the grid setting.
	LLComboBox* server_combo = getChild<LLComboBox>("server_combo");
	LLSD server_combo_val = server_combo->getSelectedValue();
	LL_INFOS("AppInit") << "grid "<<server_combo_val.asString()<< LL_ENDL;
	LLGridManager::getInstance()->setGridChoice(server_combo_val.asString());
	addFavoritesToStartLocation();
	
	/*
	 * Determine whether or not the value in the start_location_combo makes sense
	 * with the new grid value.
	 *
	 * Note that some forms that could be in the location combo are grid-agnostic,
	 * such as "MyRegion/128/128/0".  There could be regions with that name on any
	 * number of grids, so leave them alone.  Other forms, such as
	 * https://grid.example.com/region/Party%20Town/20/30/5 specify a particular
	 * grid; in those cases we want to clear the location.
	 */
	LLComboBox* location_combo = getChild<LLComboBox>("start_location_combo");
	S32 index = location_combo->getCurrentIndex();
	switch (index)
	{
	case 0: // last location
	case 1: // home location
		// do nothing - these are grid-agnostic locations
		break;
		
	default:
		{
			std::string location = location_combo->getValue().asString();
			LLSLURL slurl(location); // generata a slurl from the location combo contents
			if (   slurl.getType() == LLSLURL::LOCATION
				&& slurl.getGrid() != LLGridManager::getInstance()->getGrid()
				)
			{
				// the grid specified by the location is not this one, so clear the combo
				location_combo->setCurrentByIndex(0); // last location on the new grid
				location_combo->setTextEntry(LLStringUtil::null);
			}
		}			
		break;
	}

	updateServer();
}

void LLPanelLogin::onLocationSLURL()
{
	LLComboBox* location_combo = getChild<LLComboBox>("start_location_combo");
	std::string location = location_combo->getValue().asString();
	LL_DEBUGS("AppInit")<<location<<LL_ENDL;

	LLStartUp::setStartSLURL(location); // calls onUpdateStartSLURL, above 
}

// static
bool LLPanelLogin::getShowFavorites()
{
	return gSavedPerAccountSettings.getBOOL("ShowFavoritesOnLogin");
}

// static
std::string LLPanelLogin::getUserName(LLPointer<LLCredential> &cred)
{
    if (cred.isNull())
    {
        return "unknown";
    }
    const LLSD &ident = cred->getIdentifier();

    if (!ident.isMap())
    {
        return "unknown";
    }
    else if ((std::string)ident["type"] == "agent")
    {
        std::string second_name = ident["last_name"];
        if (second_name == "resident" || second_name == "Resident")
        {
            return (std::string)ident["first_name"];
        }
        return (std::string)ident["first_name"] + " " + (std::string)ident["last_name"];
    }
    else if ((std::string)ident["type"] == "account")
    {
        return LLCacheName::cleanFullName((std::string)ident["account_name"]);
    }

    return "unknown";
}