Refactoring for CredentialsDialog usage (not finished yet)

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@886 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-09-22 06:18:34 +00:00
commit cf699594f9
5 changed files with 62 additions and 154 deletions

View file

@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
namespace GreenshotJiraPlugin { namespace GreenshotJiraPlugin {
partial class LoginForm { partial class SettingsForm {
/// <summary> /// <summary>
/// Designer variable used to keep track of non-visual components. /// Designer variable used to keep track of non-visual components.
/// </summary> /// </summary>
@ -46,35 +46,12 @@ namespace GreenshotJiraPlugin {
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.label_password = new System.Windows.Forms.Label();
this.buttonOK = new System.Windows.Forms.Button(); this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button();
this.label_user = new System.Windows.Forms.Label();
this.textBoxUser = new System.Windows.Forms.TextBox();
this.label_url = new System.Windows.Forms.Label(); this.label_url = new System.Windows.Forms.Label();
this.textBoxUrl = new System.Windows.Forms.TextBox(); this.textBoxUrl = new System.Windows.Forms.TextBox();
this.checkBoxDoNotStorePassword = new System.Windows.Forms.CheckBox();
this.SuspendLayout(); this.SuspendLayout();
// //
// textBoxPassword
//
this.textBoxPassword.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxPassword.Location = new System.Drawing.Point(102, 73);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.PasswordChar = '*';
this.textBoxPassword.Size = new System.Drawing.Size(276, 20);
this.textBoxPassword.TabIndex = 0;
this.textBoxPassword.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TextBoxPasswordKeyUp);
//
// label_password
//
this.label_password.Location = new System.Drawing.Point(12, 73);
this.label_password.Name = "label_password";
this.label_password.Size = new System.Drawing.Size(84, 20);
this.label_password.TabIndex = 1;
this.label_password.Text = "Password";
//
// buttonOK // buttonOK
// //
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -97,22 +74,6 @@ namespace GreenshotJiraPlugin {
this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancelClick); this.buttonCancel.Click += new System.EventHandler(this.ButtonCancelClick);
// //
// label_user
//
this.label_user.Location = new System.Drawing.Point(12, 47);
this.label_user.Name = "label_user";
this.label_user.Size = new System.Drawing.Size(84, 20);
this.label_user.TabIndex = 5;
this.label_user.Text = "User";
//
// textBoxUser
//
this.textBoxUser.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxUser.Location = new System.Drawing.Point(102, 47);
this.textBoxUser.Name = "textBoxUser";
this.textBoxUser.Size = new System.Drawing.Size(276, 20);
this.textBoxUser.TabIndex = 4;
//
// label_url // label_url
// //
this.label_url.Location = new System.Drawing.Point(12, 21); this.label_url.Location = new System.Drawing.Point(12, 21);
@ -129,44 +90,22 @@ namespace GreenshotJiraPlugin {
this.textBoxUrl.Size = new System.Drawing.Size(276, 20); this.textBoxUrl.Size = new System.Drawing.Size(276, 20);
this.textBoxUrl.TabIndex = 6; this.textBoxUrl.TabIndex = 6;
// //
// checkBoxDoNotStorePassword // SettingsForm
//
this.checkBoxDoNotStorePassword.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.checkBoxDoNotStorePassword.Checked = true;
this.checkBoxDoNotStorePassword.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxDoNotStorePassword.Location = new System.Drawing.Point(102, 99);
this.checkBoxDoNotStorePassword.Name = "checkBoxDoNotStorePassword";
this.checkBoxDoNotStorePassword.Size = new System.Drawing.Size(276, 24);
this.checkBoxDoNotStorePassword.TabIndex = 8;
this.checkBoxDoNotStorePassword.Text = "Do not store the password";
this.checkBoxDoNotStorePassword.UseVisualStyleBackColor = true;
//
// LoginForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(387, 174); this.ClientSize = new System.Drawing.Size(387, 174);
this.Controls.Add(this.checkBoxDoNotStorePassword);
this.Controls.Add(this.label_url); this.Controls.Add(this.label_url);
this.Controls.Add(this.textBoxUrl); this.Controls.Add(this.textBoxUrl);
this.Controls.Add(this.label_user);
this.Controls.Add(this.textBoxUser);
this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK); this.Controls.Add(this.buttonOK);
this.Controls.Add(this.label_password); this.Name = "SettingsForm";
this.Controls.Add(this.textBoxPassword);
this.Name = "LoginForm";
this.Text = "Please enter your Jira data"; this.Text = "Please enter your Jira data";
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
} }
private System.Windows.Forms.CheckBox checkBoxDoNotStorePassword;
private System.Windows.Forms.TextBox textBoxUrl; private System.Windows.Forms.TextBox textBoxUrl;
private System.Windows.Forms.Label label_url; private System.Windows.Forms.Label label_url;
private System.Windows.Forms.TextBox textBoxUser;
private System.Windows.Forms.Label label_password;
private System.Windows.Forms.Label label_user;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.Button buttonCancel; private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.Button buttonOK;
} }

View file

@ -28,10 +28,10 @@ namespace GreenshotJiraPlugin {
/// <summary> /// <summary>
/// Description of PasswordRequestForm. /// Description of PasswordRequestForm.
/// </summary> /// </summary>
public partial class LoginForm : Form { public partial class SettingsForm : Form {
private ILanguage lang = Language.GetInstance(); private ILanguage lang = Language.GetInstance();
public LoginForm() { public SettingsForm() {
// //
// The InitializeComponent() call is required for Windows Forms designer support. // The InitializeComponent() call is required for Windows Forms designer support.
// //
@ -41,11 +41,8 @@ namespace GreenshotJiraPlugin {
private void InitializeTexts() { private void InitializeTexts() {
this.label_url.Text = lang.GetString(LangKey.label_url); this.label_url.Text = lang.GetString(LangKey.label_url);
this.label_user.Text = lang.GetString(LangKey.label_user);
this.label_password.Text = lang.GetString(LangKey.label_password);
this.buttonOK.Text = lang.GetString(LangKey.OK); this.buttonOK.Text = lang.GetString(LangKey.OK);
this.buttonCancel.Text = lang.GetString(LangKey.CANCEL); this.buttonCancel.Text = lang.GetString(LangKey.CANCEL);
this.checkBoxDoNotStorePassword.Text = lang.GetString(LangKey.label_no_password_store);
this.Text = lang.GetString(LangKey.login_title); this.Text = lang.GetString(LangKey.login_title);
} }
@ -54,21 +51,6 @@ namespace GreenshotJiraPlugin {
set {textBoxUrl.Text = value;} set {textBoxUrl.Text = value;}
} }
public string User {
get {return textBoxUser.Text;}
set {textBoxUser.Text = value;}
}
public string Password {
get {return textBoxPassword.Text;}
set {textBoxPassword.Text = value;}
}
public bool DoNotStorePassword {
get {return checkBoxDoNotStorePassword.Checked;}
set {checkBoxDoNotStorePassword.Checked = value;}
}
void ButtonOKClick(object sender, EventArgs e) { void ButtonOKClick(object sender, EventArgs e) {
this.DialogResult = DialogResult.OK; this.DialogResult = DialogResult.OK;
} }
@ -76,11 +58,5 @@ namespace GreenshotJiraPlugin {
void ButtonCancelClick(object sender, System.EventArgs e) { void ButtonCancelClick(object sender, System.EventArgs e) {
this.DialogResult = DialogResult.Cancel; this.DialogResult = DialogResult.Cancel;
} }
void TextBoxPasswordKeyUp(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
this.DialogResult = DialogResult.OK;
}
}
} }
} }

View file

@ -54,9 +54,9 @@
<Compile Include="Forms\JiraForm.cs" /> <Compile Include="Forms\JiraForm.cs" />
<Compile Include="Forms\JiraForm.Designer.cs" /> <Compile Include="Forms\JiraForm.Designer.cs" />
<Compile Include="Forms\ListViewColumnSorter.cs" /> <Compile Include="Forms\ListViewColumnSorter.cs" />
<Compile Include="Forms\LoginForm.cs" /> <Compile Include="Forms\SettingsForm.cs" />
<Compile Include="Forms\LoginForm.Designer.cs"> <Compile Include="Forms\SettingsForm.Designer.cs">
<DependentUpon>LoginForm.cs</DependentUpon> <DependentUpon>SettingsForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Jira.cs" /> <Compile Include="Jira.cs" />
<Compile Include="JiraConfiguration.cs" /> <Compile Include="JiraConfiguration.cs" />

View file

@ -25,8 +25,9 @@ using System.IO;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using GreenshotJiraPlugin;
using Greenshot.Core; using Greenshot.Core;
using Greenshot.Helpers;
using GreenshotJiraPlugin;
namespace Jira { namespace Jira {
#region transport classes #region transport classes
@ -113,39 +114,55 @@ namespace Jira {
logout(); logout();
} }
/// <summary>
/// Internal login which catches the exceptions
/// </summary>
/// <returns>true if login was done sucessfully</returns>
private bool doLogin(string user, string password) {
try {
this.credentials = jira.login(user, password);
this.loggedInTime = DateTime.Now;
this.loggedIn = true;
} catch (Exception e) {
// check if auth failed
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
return false;
}
// Not an authentication issue
this.loggedIn = false;
this.credentials = null;
e.Data.Add("user", user);
e.Data.Add("url", config.Url);
throw e;
}
return true;
}
public void login() { public void login() {
logout(); logout();
try { try {
if (config.HasPassword()) { CredentialsDialog dialog = new CredentialsDialog(config.Url.Replace(JiraConfiguration.DEFAULT_PREFIX,"").Replace(JiraConfiguration.DEFAULT_POSTFIX,""));
this.credentials = jira.login(config.User, config.Password); dialog.Name = null;
} else if (config.HasTmpPassword()) { while (dialog.Show(dialog.Name) == DialogResult.OK) {
this.credentials = jira.login(config.User, config.TmpPassword); if (doLogin(dialog.Name, dialog.Password)) {
} else { if (dialog.SaveChecked) {
if (config.ShowConfigDialog()) { dialog.Confirm(true);
if (config.HasPassword()) { }
this.credentials = jira.login(config.User, config.Password); return;
} else if (config.HasTmpPassword()) { } else {
this.credentials = jira.login(config.User, config.TmpPassword); try {
} dialog.Confirm(false);
} else { } catch (ApplicationException e) {
throw new Exception("User pressed cancel!"); // exception handling ...
} LOG.Error("Problem using the credentials dialog", e);
} }
this.loggedInTime = DateTime.Now; dialog.IncorrectPassword = true;
this.loggedIn = true; }
} catch (Exception e) { }
this.loggedIn = false; } catch (ApplicationException e) {
this.credentials = null; // exception handling ...
e.Data.Add("user",config.User); LOG.Error("Problem using the credentials dialog", e);
e.Data.Add("url",config.Url); }
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
// Login failed due to wrong user or password, password should be removed!
config.Password = null;
config.TmpPassword = null;
throw new Exception(e.Message.Replace(AUTH_FAILED_EXCEPTION_NAME+ ": ",""));
}
throw e;
}
} }
public void logout() { public void logout() {

View file

@ -30,49 +30,25 @@ namespace GreenshotJiraPlugin {
/// </summary> /// </summary>
[IniSection("Jira", Description="Greenshot Jira Plugin configuration")] [IniSection("Jira", Description="Greenshot Jira Plugin configuration")]
public class JiraConfiguration : IniSection { public class JiraConfiguration : IniSection {
[IniProperty("Url", Description="Url to Jira system, including wsdl.", DefaultValue="http://jira/rpc/soap/jirasoapservice-v2?wsdl")] public const string DEFAULT_POSTFIX = "/rpc/soap/jirasoapservice-v2?wsdl";
public const string DEFAULT_PREFIX = "http://";
private const string DEFAULT_URL = DEFAULT_PREFIX + "jira" + DEFAULT_POSTFIX;
[IniProperty("Url", Description="Url to Jira system, including wsdl.", DefaultValue=DEFAULT_URL)]
public string Url; public string Url;
[IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")] [IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")]
public int Timeout; public int Timeout;
[IniProperty("User", Description="User for the Jira System")]
public string User;
[IniProperty("Password", Description="Password for the Jira System, belonging to user.")]
public string Password;
// This will not be stored
public string TmpPassword;
public bool HasPassword() {
return (Password != null && Password.Length > 0);
}
public bool HasTmpPassword() {
return (TmpPassword != null && TmpPassword.Length > 0);
}
/// <summary> /// <summary>
/// A form for username/password /// A form for username/password
/// </summary> /// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns> /// <returns>bool true if OK was pressed, false if cancel</returns>
public bool ShowConfigDialog() { public bool ShowConfigDialog() {
LoginForm pwForm = new LoginForm(); SettingsForm pwForm = new SettingsForm();
if (User == null || User.Length == 0) {
User = Environment.UserName;
}
pwForm.User = User;
pwForm.Url = Url; pwForm.Url = Url;
DialogResult result = pwForm.ShowDialog(); DialogResult result = pwForm.ShowDialog();
if (result == DialogResult.OK) { if (result == DialogResult.OK) {
if (pwForm.DoNotStorePassword) { if (!pwForm.Url.Equals(Url)) {
TmpPassword = pwForm.Password;
Password = null;
} else {
Password = pwForm.Password;
TmpPassword = null;
}
if (!pwForm.User.Equals(User) ||!pwForm.Url.Equals(Url)) {
User = pwForm.User;
Url = pwForm.Url; Url = pwForm.Url;
} }
IniConfig.Save(); IniConfig.Save();