2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 using System.Windows.Forms;
35 using OpenMetaverse.StructuredData;
36 using Radegast.Netcom;
40 public partial class LoginConsole : UserControl, IRadegastTabControl
42 private RadegastInstance instance;
43 private RadegastNetcom netcom { get { return instance.Netcom; } }
45 public LoginConsole(RadegastInstance instance)
47 InitializeComponent();
48 Disposed += new EventHandler(MainConsole_Disposed);
50 this.instance = instance;
53 if (instance.GlobalSettings["hide_login_graphics"].AsBoolean())
54 pnlSplash.BackgroundImage = null;
56 pnlSplash.BackgroundImage = Properties.Resources.radegast_main_screen2;
58 if (!instance.GlobalSettings.ContainsKey("remember_login"))
60 instance.GlobalSettings["remember_login"] = true;
63 instance.GlobalSettings.OnSettingChanged += new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
65 lblVersion.Text = Properties.Resources.RadegastTitle + "." + RadegastBuild.CurrentRev;
67 Load += new EventHandler(LoginConsole_Load);
71 private void MainConsole_Disposed(object sender, EventArgs e)
73 instance.GlobalSettings.OnSettingChanged -= new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
77 void LoginConsole_Load(object sender, EventArgs e)
79 if (instance.PlainColors)
80 panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(210)))), ((int)(((byte)(210)))), ((int)(((byte)(225)))));
82 cbxLocation.SelectedIndex = 0;
83 cbxUsername.SelectedIndexChanged += cbxUsername_SelectedIndexChanged;
87 private void AddNetcomEvents()
89 netcom.ClientLoggingIn += new EventHandler<OverrideEventArgs>(netcom_ClientLoggingIn);
90 netcom.ClientLoginStatus += new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
91 netcom.ClientLoggingOut += new EventHandler<OverrideEventArgs>(netcom_ClientLoggingOut);
92 netcom.ClientLoggedOut += new EventHandler(netcom_ClientLoggedOut);
95 private void RemoveNetcomEvents()
97 netcom.ClientLoggingIn -= new EventHandler<OverrideEventArgs>(netcom_ClientLoggingIn);
98 netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
99 netcom.ClientLoggingOut -= new EventHandler<OverrideEventArgs>(netcom_ClientLoggingOut);
100 netcom.ClientLoggedOut -= new EventHandler(netcom_ClientLoggedOut);
103 void GlobalSettings_OnSettingChanged(object sender, SettingsEventArgs e)
105 if (e.Key == "hide_login_graphics")
107 if (e.Value.AsBoolean())
108 pnlSplash.BackgroundImage = null;
110 pnlSplash.BackgroundImage = Properties.Resources.radegast_main_screen2;
114 private void SaveConfig()
116 Settings s = instance.GlobalSettings;
117 SavedLogin sl = new SavedLogin();
119 string username = cbxUsername.Text;
121 if (cbxUsername.SelectedIndex > 0 && cbxUsername.SelectedItem is SavedLogin)
123 username = ((SavedLogin)cbxUsername.SelectedItem).Username;
126 if (cbxGrid.SelectedIndex == cbxGrid.Items.Count - 1) // custom login uri
128 sl.GridID = "custom_login_uri";
129 sl.CustomURI = txtCustomLoginUri.Text;
133 sl.GridID = (cbxGrid.SelectedItem as Grid).ID;
134 sl.CustomURI = string.Empty;
137 string savedLoginsKey = string.Format("{0}%{1}", username, sl.GridID);
139 if (!(s["saved_logins"] is OSDMap))
141 s["saved_logins"] = new OSDMap();
144 if (cbRemember.Checked)
147 sl.Username = s["username"] = username;
149 if (LoginOptions.IsPasswordMD5(txtPassword.Text))
151 sl.Password = txtPassword.Text;
152 s["password"] = txtPassword.Text;
156 sl.Password =Utils.MD5(txtPassword.Text);
157 s["password"] = Utils.MD5(txtPassword.Text);
159 if (cbxLocation.SelectedIndex == -1)
161 sl.CustomStartLocation = cbxLocation.Text;
165 sl.CustomStartLocation = string.Empty;
167 sl.StartLocationType = cbxLocation.SelectedIndex;
168 ((OSDMap)s["saved_logins"])[savedLoginsKey] = sl.ToOSD();
170 else if (((OSDMap)s["saved_logins"]).ContainsKey(savedLoginsKey))
172 ((OSDMap)s["saved_logins"]).Remove(savedLoginsKey);
175 s["login_location_type"] = OSD.FromInteger(cbxLocation.SelectedIndex);
176 s["login_location"] = OSD.FromString(cbxLocation.Text);
178 s["login_grid"] = OSD.FromInteger(cbxGrid.SelectedIndex);
179 s["login_uri"] = OSD.FromString(txtCustomLoginUri.Text);
180 s["remember_login"] = cbRemember.Checked;
183 private void ClearConfig()
185 Settings s = instance.GlobalSettings;
186 s["username"] = string.Empty;
187 s["password"] = string.Empty;
190 private void InitializeConfig()
192 // Initilize grid dropdown
195 cbxGrid.Items.Clear();
196 for (int i = 0; i < instance.GridManger.Count; i++)
198 cbxGrid.Items.Add(instance.GridManger[i]);
199 if (MainProgram.CommandLine.Grid == instance.GridManger[i].ID)
202 cbxGrid.Items.Add("Custom");
206 cbxGrid.SelectedIndex = gridIx;
210 Settings s = instance.GlobalSettings;
211 cbRemember.Checked = s["remember_login"];
214 string savedUsername;
216 if (string.IsNullOrEmpty(MainProgram.CommandLine.Username))
218 savedUsername = s["username"];
222 savedUsername = MainProgram.CommandLine.Username;
225 cbxUsername.Items.Add(savedUsername);
229 if (s["saved_logins"] is OSDMap)
231 OSDMap savedLogins = (OSDMap)s["saved_logins"];
232 foreach (string loginKey in savedLogins.Keys)
234 SavedLogin sl = SavedLogin.FromOSD(savedLogins[loginKey]);
235 cbxUsername.Items.Add(sl);
241 cbxUsername.Items.Clear();
242 cbxUsername.Text = string.Empty;
245 cbxUsername.SelectedIndex = 0;
247 // Fill in saved password or use one specified on the command line
248 if (string.IsNullOrEmpty(MainProgram.CommandLine.Password))
250 txtPassword.Text = s["password"].AsString();
254 txtPassword.Text = MainProgram.CommandLine.Password;
258 // Setup login location either from the last used or
259 // override from the command line
260 if (string.IsNullOrEmpty(MainProgram.CommandLine.Location))
262 // Use last location as default
263 if (s["login_location_type"].Type == OSDType.Unknown)
265 cbxLocation.SelectedIndex = 1;
266 s["login_location_type"] = OSD.FromInteger(1);
270 cbxLocation.SelectedIndex = s["login_location_type"].AsInteger();
271 cbxLocation.Text = s["login_location"].AsString();
276 switch (MainProgram.CommandLine.Location)
279 cbxLocation.SelectedIndex = 0;
283 cbxLocation.SelectedIndex = 1;
287 cbxLocation.SelectedIndex = -1;
288 cbxLocation.Text = MainProgram.CommandLine.Location;
294 // Set grid dropdown to last used, or override from command line
295 if (string.IsNullOrEmpty(MainProgram.CommandLine.Grid))
297 cbxGrid.SelectedIndex = s["login_grid"].AsInteger();
299 else if (gridIx == -1) // --grid specified but not found
301 MessageBox.Show(string.Format("Grid specified with --grid {0} not found", MainProgram.CommandLine.Grid),
303 MessageBoxButtons.OK,
304 MessageBoxIcon.Warning
308 // Restore login uri from settings, or command line
309 if (string.IsNullOrEmpty(MainProgram.CommandLine.LoginUri))
311 txtCustomLoginUri.Text = s["login_uri"].AsString();
315 txtCustomLoginUri.Text = MainProgram.CommandLine.LoginUri;
316 cbxGrid.SelectedIndex = cbxGrid.Items.Count - 1;
319 // Start logging in if autologin enabled from command line
320 if (MainProgram.CommandLine.AutoLogin)
326 private void netcom_ClientLoginStatus(object sender, LoginProgressEventArgs e)
330 case LoginStatus.ConnectingToLogin:
331 lblLoginStatus.Text = "Connecting to login server...";
332 lblLoginStatus.ForeColor = Color.Black;
335 case LoginStatus.ConnectingToSim:
336 lblLoginStatus.Text = "Connecting to region...";
337 lblLoginStatus.ForeColor = Color.Black;
340 case LoginStatus.Redirecting:
341 lblLoginStatus.Text = "Redirecting...";
342 lblLoginStatus.ForeColor = Color.Black;
345 case LoginStatus.ReadingResponse:
346 lblLoginStatus.Text = "Reading response...";
347 lblLoginStatus.ForeColor = Color.Black;
350 case LoginStatus.Success:
351 lblLoginStatus.Text = "Logged in as " + netcom.LoginOptions.FullName;
352 lblLoginStatus.ForeColor = Color.FromArgb(0, 128, 128, 255);
353 proLogin.Visible = false;
355 btnLogin.Text = "Logout";
356 btnLogin.Enabled = true;
357 instance.Client.Groups.RequestCurrentGroups();
360 case LoginStatus.Failed:
361 lblLoginStatus.ForeColor = Color.Red;
362 if (e.FailReason == "tos")
364 lblLoginStatus.Text = "Must agree to Terms of Service before logging in";
365 pnlTos.Visible = true;
366 txtTOS.Text = e.Message.Replace("\n", "\r\n");
367 btnLogin.Enabled = false;
371 lblLoginStatus.Text = e.Message;
372 btnLogin.Enabled = true;
374 proLogin.Visible = false;
376 btnLogin.Text = "Retry";
381 private void netcom_ClientLoggedOut(object sender, EventArgs e)
383 pnlLoginPrompt.Visible = true;
384 pnlLoggingIn.Visible = false;
386 btnLogin.Text = "Exit";
387 btnLogin.Enabled = true;
390 private void netcom_ClientLoggingOut(object sender, OverrideEventArgs e)
392 btnLogin.Enabled = false;
394 lblLoginStatus.Text = "Logging out...";
395 lblLoginStatus.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
397 proLogin.Visible = true;
400 private void netcom_ClientLoggingIn(object sender, OverrideEventArgs e)
402 lblLoginStatus.Text = "Logging in...";
403 lblLoginStatus.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
405 proLogin.Visible = true;
406 pnlLoggingIn.Visible = true;
407 pnlLoginPrompt.Visible = false;
409 btnLogin.Enabled = false;
412 private void BeginLogin()
414 string username = cbxUsername.Text;
416 if (cbxUsername.SelectedIndex > 0 && cbxUsername.SelectedItem is SavedLogin)
418 username = ((SavedLogin)cbxUsername.SelectedItem).Username;
421 string[] parts = System.Text.RegularExpressions.Regex.Split(username.Trim(), @"[. ]+");
423 if (parts.Length == 2)
425 netcom.LoginOptions.FirstName = parts[0];
426 netcom.LoginOptions.LastName = parts[1];
430 netcom.LoginOptions.FirstName = username.Trim();
431 netcom.LoginOptions.LastName = "Resident";
434 netcom.LoginOptions.Password = txtPassword.Text;
435 netcom.LoginOptions.Channel = Properties.Resources.ProgramName; // Channel
436 netcom.LoginOptions.Version = Properties.Resources.RadegastTitle; // Version
437 netcom.AgreeToTos = cbTOS.Checked;
439 switch (cbxLocation.SelectedIndex)
442 netcom.LoginOptions.StartLocation = StartLocationType.Custom;
443 netcom.LoginOptions.StartLocationCustom = cbxLocation.Text;
447 netcom.LoginOptions.StartLocation = StartLocationType.Home;
451 netcom.LoginOptions.StartLocation = StartLocationType.Last;
455 if (cbxGrid.SelectedIndex == cbxGrid.Items.Count - 1) // custom login uri
457 if (txtCustomLoginUri.TextLength == 0 || txtCustomLoginUri.Text.Trim().Length == 0)
459 MessageBox.Show("You must specify the Login Uri to connect to a custom grid.", Properties.Resources.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Error);
463 netcom.LoginOptions.Grid = new Grid("custom", "Custom", txtCustomLoginUri.Text);
464 netcom.LoginOptions.GridCustomLoginUri = txtCustomLoginUri.Text;
468 netcom.LoginOptions.Grid = cbxGrid.SelectedItem as Grid;
471 if (netcom.LoginOptions.Grid.Platform != "SecondLife")
473 instance.Client.Settings.MULTIPLE_SIMS = true;
476 instance.Client.Settings.HTTP_INVENTORY = !instance.GlobalSettings["disable_http_inventory"];
481 private void btnLogin_Click(object sender, EventArgs e)
483 switch (btnLogin.Text)
485 case "Login": BeginLogin(); break;
488 pnlLoginPrompt.Visible = true;
489 pnlLoggingIn.Visible = false;
490 btnLogin.Text = "Login";
495 #region IRadegastTabControl Members
497 public void RegisterTab(RadegastTab tab)
499 tab.DefaultControlButton = btnLogin;
504 private void cbxGrid_SelectedIndexChanged(object sender, EventArgs e)
506 if (cbxGrid.SelectedIndex == cbxGrid.Items.Count - 1) //Custom option is selected
508 txtCustomLoginUri.Enabled = true;
509 txtCustomLoginUri.Select();
513 txtCustomLoginUri.Enabled = false;
517 private void cbTOS_CheckedChanged(object sender, EventArgs e)
519 btnLogin.Enabled = cbTOS.Checked;
522 private void cbRemember_CheckedChanged(object sender, EventArgs e)
524 instance.GlobalSettings["remember_login"] = cbRemember.Checked;
525 if (!cbRemember.Checked)
531 private void cbxUsername_SelectedIndexChanged(object sender, EventArgs e)
533 cbxUsername.SelectedIndexChanged -= cbxUsername_SelectedIndexChanged;
535 if (cbxUsername.SelectedIndex > 0
536 && cbxUsername.SelectedItem is SavedLogin)
538 SavedLogin sl = (SavedLogin)cbxUsername.SelectedItem;
539 cbxUsername.Text = sl.Username;
540 cbxUsername.Items[0] = sl.Username;
541 cbxUsername.SelectedIndex = 0;
542 txtPassword.Text = sl.Password;
543 cbxLocation.SelectedIndex = sl.StartLocationType;
544 if (sl.StartLocationType == -1)
546 cbxLocation.Text = sl.CustomStartLocation;
548 if (sl.GridID == "custom_login_uri")
550 cbxGrid.SelectedIndex = cbxGrid.Items.Count - 1;
551 txtCustomLoginUri.Text = sl.CustomURI;
555 foreach (var item in cbxGrid.Items)
557 if (item is Grid && ((Grid)item).ID == sl.GridID)
559 cbxGrid.SelectedItem = item;
566 cbxUsername.SelectedIndexChanged += cbxUsername_SelectedIndexChanged;
570 public class SavedLogin
572 public string Username;
573 public string Password;
574 public string GridID;
575 public string CustomURI;
576 public int StartLocationType;
577 public string CustomStartLocation;
579 public OSDMap ToOSD()
581 OSDMap ret = new OSDMap(4);
582 ret["username"] = Username;
583 ret["password"] = Password;
584 ret["grid"] = GridID;
585 ret["custom_url"] = CustomURI;
586 ret["location_type"] = StartLocationType;
587 ret["custom_location"] = CustomStartLocation;
591 public static SavedLogin FromOSD(OSD data)
593 if (!(data is OSDMap)) return null;
594 OSDMap map = (OSDMap)data;
595 SavedLogin ret = new SavedLogin();
596 ret.Username = map["username"];
597 ret.Password = map["password"];
598 ret.GridID = map["grid"];
599 ret.CustomURI = map["custom_url"];
600 if (map.ContainsKey("location_type"))
602 ret.StartLocationType = map["location_type"];
606 ret.StartLocationType = 1;
608 ret.CustomStartLocation = map["custom_location"];
612 public override string ToString()
614 RadegastInstance instance = RadegastInstance.GlobalInstance;
616 if (GridID == "custom_login_uri")
618 gridName = "Custom Login URI";
620 else if (instance.GridManger.KeyExists(GridID))
622 gridName = instance.GridManger[GridID].Name;
628 return string.Format("{0} -- {1}", Username, gridName);