From f53b93e803ad6b542690b931b7e135e7c182c2a4 Mon Sep 17 00:00:00 2001 From: Latif Khalifa Date: Tue, 16 Aug 2011 04:28:43 +0000 Subject: [PATCH] RAD-279: Added ability to save more than one login git-svn-id: https://radegast.googlecode.com/svn/trunk@1094 f7a694da-4d33-11de-9ad6-1127a62b9fcd --- Radegast/Core/GridManager.cs | 23 +++- Radegast/GUI/Consoles/LoginConsole.Designer.cs | 61 ++++----- Radegast/GUI/Consoles/LoginConsole.cs | 169 ++++++++++++++++++++++--- Radegast/Netcom/LoginOptions.cs | 12 +- Radegast/Netcom/RadegastNetcom/SLNetCom.cs | 2 +- 5 files changed, 204 insertions(+), 63 deletions(-) diff --git a/Radegast/Core/GridManager.cs b/Radegast/Core/GridManager.cs index 193e88a..3460ab9 100644 --- a/Radegast/Core/GridManager.cs +++ b/Radegast/Core/GridManager.cs @@ -139,11 +139,32 @@ namespace Radegast } } - public Grid this [int ix] + public Grid this[int ix] { get { return Grids[ix]; } } + public Grid this[string gridID] + { + get + { + foreach (Grid grid in Grids) + { + if (grid.ID == gridID) return grid; + } + throw new KeyNotFoundException(); + } + } + + public bool KeyExists(string gridID) + { + foreach (Grid grid in Grids) + { + if (grid.ID == gridID) return true; + } + return false; + } + public int Count { get { return Grids.Count; } diff --git a/Radegast/GUI/Consoles/LoginConsole.Designer.cs b/Radegast/GUI/Consoles/LoginConsole.Designer.cs index 283d989..0b8a705 100644 --- a/Radegast/GUI/Consoles/LoginConsole.Designer.cs +++ b/Radegast/GUI/Consoles/LoginConsole.Designer.cs @@ -59,12 +59,13 @@ namespace Radegast private void InitializeComponent() { this.pnlLoginPrompt = new System.Windows.Forms.Panel(); + this.cbRemember = new System.Windows.Forms.CheckBox(); this.label6 = new System.Windows.Forms.Label(); this.txtCustomLoginUri = new System.Windows.Forms.TextBox(); this.cbxGrid = new System.Windows.Forms.ComboBox(); this.label5 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); - this.txtUsername = new System.Windows.Forms.TextBox(); + this.cbxUsername = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.txtPassword = new System.Windows.Forms.TextBox(); this.cbxLocation = new System.Windows.Forms.ComboBox(); @@ -74,13 +75,11 @@ namespace Radegast this.proLogin = new System.Windows.Forms.ProgressBar(); this.lblLoginStatus = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); - this.btnGridManager = new System.Windows.Forms.Button(); this.lblVersion = new System.Windows.Forms.Label(); this.pnlSplash = new System.Windows.Forms.Panel(); this.pnlTos = new System.Windows.Forms.Panel(); this.cbTOS = new System.Windows.Forms.CheckBox(); this.txtTOS = new System.Windows.Forms.TextBox(); - this.cbRemember = new System.Windows.Forms.CheckBox(); this.pnlLoginPrompt.SuspendLayout(); this.pnlLoggingIn.SuspendLayout(); this.panel1.SuspendLayout(); @@ -97,7 +96,7 @@ namespace Radegast this.pnlLoginPrompt.Controls.Add(this.cbxGrid); this.pnlLoginPrompt.Controls.Add(this.label5); this.pnlLoginPrompt.Controls.Add(this.label1); - this.pnlLoginPrompt.Controls.Add(this.txtUsername); + this.pnlLoginPrompt.Controls.Add(this.cbxUsername); this.pnlLoginPrompt.Controls.Add(this.label2); this.pnlLoginPrompt.Controls.Add(this.txtPassword); this.pnlLoginPrompt.Controls.Add(this.cbxLocation); @@ -107,6 +106,19 @@ namespace Radegast this.pnlLoginPrompt.Size = new System.Drawing.Size(554, 94); this.pnlLoginPrompt.TabIndex = 13; // + // cbRemember + // + this.cbRemember.AutoSize = true; + this.cbRemember.Checked = true; + this.cbRemember.CheckState = System.Windows.Forms.CheckState.Checked; + this.cbRemember.Location = new System.Drawing.Point(6, 45); + this.cbRemember.Name = "cbRemember"; + this.cbRemember.Size = new System.Drawing.Size(197, 17); + this.cbRemember.TabIndex = 5; + this.cbRemember.Text = "Remember username and password"; + this.cbRemember.UseVisualStyleBackColor = true; + this.cbRemember.CheckedChanged += new System.EventHandler(this.cbRemember_CheckedChanged); + // // label6 // this.label6.AutoSize = true; @@ -158,13 +170,13 @@ namespace Radegast this.label1.TabIndex = 0; this.label1.Text = "Username"; // - // txtUsername + // cbxUsername // - this.txtUsername.AccessibleName = "Username"; - this.txtUsername.Location = new System.Drawing.Point(3, 16); - this.txtUsername.Name = "txtUsername"; - this.txtUsername.Size = new System.Drawing.Size(263, 21); - this.txtUsername.TabIndex = 1; + this.cbxUsername.AccessibleName = "Username"; + this.cbxUsername.Location = new System.Drawing.Point(3, 16); + this.cbxUsername.Name = "cbxUsername"; + this.cbxUsername.Size = new System.Drawing.Size(263, 21); + this.cbxUsername.TabIndex = 1; // // label2 // @@ -184,7 +196,6 @@ namespace Radegast this.txtPassword.Size = new System.Drawing.Size(275, 21); this.txtPassword.TabIndex = 4; this.txtPassword.UseSystemPasswordChar = true; - this.txtPassword.TextChanged += new System.EventHandler(this.txtPassword_TextChanged); // // cbxLocation // @@ -256,7 +267,6 @@ namespace Radegast // panel1 // this.panel1.BackColor = System.Drawing.SystemColors.Control; - this.panel1.Controls.Add(this.btnGridManager); this.panel1.Controls.Add(this.lblVersion); this.panel1.Controls.Add(this.pnlLoginPrompt); this.panel1.Controls.Add(this.btnLogin); @@ -267,17 +277,6 @@ namespace Radegast this.panel1.Size = new System.Drawing.Size(682, 97); this.panel1.TabIndex = 18; // - // btnGridManager - // - this.btnGridManager.ForeColor = System.Drawing.SystemColors.ControlText; - this.btnGridManager.Location = new System.Drawing.Point(6, 44); - this.btnGridManager.Name = "btnGridManager"; - this.btnGridManager.Size = new System.Drawing.Size(116, 23); - this.btnGridManager.TabIndex = 16; - this.btnGridManager.Text = "Grid Manager"; - this.btnGridManager.UseVisualStyleBackColor = true; - this.btnGridManager.Visible = false; - // // lblVersion // this.lblVersion.AutoSize = true; @@ -331,19 +330,6 @@ namespace Radegast this.txtTOS.Size = new System.Drawing.Size(682, 302); this.txtTOS.TabIndex = 0; // - // cbRemember - // - this.cbRemember.AutoSize = true; - this.cbRemember.Checked = true; - this.cbRemember.CheckState = System.Windows.Forms.CheckState.Checked; - this.cbRemember.Location = new System.Drawing.Point(6, 45); - this.cbRemember.Name = "cbRemember"; - this.cbRemember.Size = new System.Drawing.Size(197, 17); - this.cbRemember.TabIndex = 5; - this.cbRemember.Text = "Remember username and password"; - this.cbRemember.UseVisualStyleBackColor = true; - this.cbRemember.CheckedChanged += new System.EventHandler(this.cbRemember_CheckedChanged); - // // LoginConsole // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -370,7 +356,7 @@ namespace Radegast public System.Windows.Forms.Panel pnlLoginPrompt; public System.Windows.Forms.Label label1; - public System.Windows.Forms.TextBox txtUsername; + public System.Windows.Forms.ComboBox cbxUsername; public System.Windows.Forms.Label label2; public System.Windows.Forms.TextBox txtPassword; public System.Windows.Forms.ComboBox cbxLocation; @@ -385,7 +371,6 @@ namespace Radegast public System.Windows.Forms.Label label6; public System.Windows.Forms.Panel panel1; public System.Windows.Forms.Panel pnlSplash; - public System.Windows.Forms.Button btnGridManager; public System.Windows.Forms.Panel pnlTos; public System.Windows.Forms.TextBox txtTOS; public System.Windows.Forms.CheckBox cbTOS; diff --git a/Radegast/GUI/Consoles/LoginConsole.cs b/Radegast/GUI/Consoles/LoginConsole.cs index 10256e1..dd737a2 100644 --- a/Radegast/GUI/Consoles/LoginConsole.cs +++ b/Radegast/GUI/Consoles/LoginConsole.cs @@ -80,6 +80,7 @@ namespace Radegast panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(210)))), ((int)(((byte)(210)))), ((int)(((byte)(225))))); cbxLocation.SelectedIndex = 0; + cbxUsername.SelectedIndexChanged += cbxUsername_SelectedIndexChanged; InitializeConfig(); } @@ -116,12 +117,43 @@ namespace Radegast if (cbRemember.Checked) { - s["username"] = txtUsername.Text; + SavedLogin sl = new SavedLogin(); - if (netcom.LoginOptions.IsPasswordMD5) - s["password"] = OSD.FromString(txtPassword.Text); + string username = cbxUsername.Text; + + if (cbxUsername.SelectedIndex > 0 && cbxUsername.SelectedItem is SavedLogin) + { + username = ((SavedLogin)cbxUsername.SelectedItem).Username; + } + + sl.Username = s["username"] = username; + + if (LoginOptions.IsPasswordMD5(txtPassword.Text)) + { + sl.Password = txtPassword.Text; + s["password"] = txtPassword.Text; + } + else + { + sl.Password =Utils.MD5(txtPassword.Text); + s["password"] = Utils.MD5(txtPassword.Text); + } + + if (cbxGrid.SelectedIndex == cbxGrid.Items.Count - 1) // custom login uri + { + sl.GridID = "custom_login_uri"; + sl.CustomURI = txtCustomLoginUri.Text; + } else - s["password"] = OSD.FromString(Utils.MD5(txtPassword.Text)); + { + sl.GridID = (cbxGrid.SelectedItem as Grid).ID; + sl.CustomURI = string.Empty; + } + if (!(s["saved_logins"] is OSDMap)) + { + s["saved_logins"] = new OSDMap(); + } + ((OSDMap)s["saved_logins"])[string.Format("{0}%{1}", sl.Username, sl.GridID)] = sl.ToOSD(); } s["login_location_type"] = OSD.FromInteger(cbxLocation.SelectedIndex); @@ -162,26 +194,47 @@ namespace Radegast Settings s = instance.GlobalSettings; // Setup login name + string savedUsername; + if (string.IsNullOrEmpty(MainProgram.CommandLine.Username)) { - txtUsername.Text = s["username"]; + savedUsername = s["username"]; } else { - txtUsername.Text = MainProgram.CommandLine.Username; + savedUsername = MainProgram.CommandLine.Username; } + cbxUsername.Items.Add(savedUsername); + + try + { + if (s["saved_logins"] is OSDMap) + { + OSDMap savedLogins = (OSDMap)s["saved_logins"]; + foreach (string loginKey in savedLogins.Keys) + { + SavedLogin sl = SavedLogin.FromOSD(savedLogins[loginKey]); + cbxUsername.Items.Add(sl); + } + } + } + catch + { + cbxUsername.Items.Clear(); + cbxUsername.Text = string.Empty; + } + + cbxUsername.SelectedIndex = 0; // Fill in saved password or use one specified on the command line if (string.IsNullOrEmpty(MainProgram.CommandLine.Password)) { txtPassword.Text = s["password"].AsString(); - netcom.LoginOptions.IsPasswordMD5 = true; } else { txtPassword.Text = MainProgram.CommandLine.Password; - netcom.LoginOptions.IsPasswordMD5 = false; } @@ -343,7 +396,15 @@ namespace Radegast private void BeginLogin() { - var parts = System.Text.RegularExpressions.Regex.Split(txtUsername.Text.Trim(), @"[. ]+"); + string username = cbxUsername.Text; + + if (cbxUsername.SelectedIndex > 0 && cbxUsername.SelectedItem is SavedLogin) + { + username = ((SavedLogin)cbxUsername.SelectedItem).Username; + } + + string[] parts = System.Text.RegularExpressions.Regex.Split(username.Trim(), @"[. ]+"); + if (parts.Length == 2) { netcom.LoginOptions.FirstName = parts[0]; @@ -351,7 +412,7 @@ namespace Radegast } else { - netcom.LoginOptions.FirstName = txtUsername.Text.Trim(); + netcom.LoginOptions.FirstName = username.Trim(); netcom.LoginOptions.LastName = "Resident"; } @@ -393,11 +454,11 @@ namespace Radegast } netcom.Login(); + SaveConfig(); } private void btnLogin_Click(object sender, EventArgs e) { - SaveConfig(); switch (btnLogin.Text) { case "Login": BeginLogin(); break; @@ -432,11 +493,6 @@ namespace Radegast } } - private void txtPassword_TextChanged(object sender, EventArgs e) - { - netcom.LoginOptions.IsPasswordMD5 = false; - } - private void cbTOS_CheckedChanged(object sender, EventArgs e) { btnLogin.Enabled = cbTOS.Checked; @@ -451,5 +507,86 @@ namespace Radegast } } + private void cbxUsername_SelectedIndexChanged(object sender, EventArgs e) + { + cbxUsername.SelectedIndexChanged -= cbxUsername_SelectedIndexChanged; + + if (cbxUsername.SelectedIndex > 0 + && cbxUsername.SelectedItem is SavedLogin) + { + SavedLogin sl = (SavedLogin)cbxUsername.SelectedItem; + cbxUsername.Text = sl.Username; + cbxUsername.Items[0] = sl.Username; + cbxUsername.SelectedIndex = 0; + txtPassword.Text = sl.Password; + if (sl.GridID == "custom_login_uri") + { + cbxGrid.SelectedIndex = cbxGrid.Items.Count - 1; + txtCustomLoginUri.Text = sl.CustomURI; + } + else + { + foreach (var item in cbxGrid.Items) + { + if (item is Grid && ((Grid)item).ID == sl.GridID) + { + cbxGrid.SelectedItem = item; + break; + } + } + } + } + + cbxUsername.SelectedIndexChanged += cbxUsername_SelectedIndexChanged; + } + } + + public class SavedLogin + { + public string Username; + public string Password; + public string GridID; + public string CustomURI; + + public OSDMap ToOSD() + { + OSDMap ret = new OSDMap(4); + ret["username"] = Username; + ret["password"] = Password; + ret["grid"] = GridID; + ret["custom_url"] = CustomURI; + return ret; + } + + public static SavedLogin FromOSD(OSD data) + { + if (!(data is OSDMap)) return null; + OSDMap map = (OSDMap)data; + SavedLogin ret = new SavedLogin(); + ret.Username = map["username"]; + ret.Password = map["password"]; + ret.GridID = map["grid"]; + ret.CustomURI = map["custom_url"]; + return ret; + } + + public override string ToString() + { + RadegastInstance instance = RadegastInstance.GlobalInstance; + string gridName; + if (GridID == "custom_login_uri") + { + gridName = "Custom Login URI"; + } + else if (instance.GridManger.KeyExists(GridID)) + { + gridName = instance.GridManger[GridID].Name; + } + else + { + gridName = GridID; + } + return string.Format("{0} -- {1}", Username, gridName); + } } } diff --git a/Radegast/Netcom/LoginOptions.cs b/Radegast/Netcom/LoginOptions.cs index 7d176d3..d44b8a0 100644 --- a/Radegast/Netcom/LoginOptions.cs +++ b/Radegast/Netcom/LoginOptions.cs @@ -36,7 +36,6 @@ namespace Radegast.Netcom private string firstName; private string lastName; private string password; - private bool isPasswordMD5 = false; private string version = string.Empty; private string channel = string.Empty; @@ -51,6 +50,11 @@ namespace Radegast.Netcom } + public static bool IsPasswordMD5(string pass) + { + return pass.Length == 35 && pass.StartsWith("$1$"); + } + public string FirstName { get { return firstName; } @@ -80,12 +84,6 @@ namespace Radegast.Netcom set { password = value; } } - public bool IsPasswordMD5 - { - get { return isPasswordMD5; } - set { isPasswordMD5 = value; } - } - public StartLocationType StartLocation { get { return startLocation; } diff --git a/Radegast/Netcom/RadegastNetcom/SLNetCom.cs b/Radegast/Netcom/RadegastNetcom/SLNetCom.cs index d7a6e2f..bf1d619 100644 --- a/Radegast/Netcom/RadegastNetcom/SLNetCom.cs +++ b/Radegast/Netcom/RadegastNetcom/SLNetCom.cs @@ -270,7 +270,7 @@ namespace Radegast.Netcom string password; - if (loginOptions.IsPasswordMD5) + if (LoginOptions.IsPasswordMD5(loginOptions.Password)) { password = loginOptions.Password; } -- 2.11.0