From 6e0ba923858245b4578d3834f36b155f883bcac5 Mon Sep 17 00:00:00 2001 From: RKrom Date: Wed, 18 Aug 2010 13:30:28 +0000 Subject: [PATCH] Refactored to use Ini Configuration git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@820 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- .../Forms/LoginForm.Designer.cs | 42 +++++--- GreenshotJiraPlugin/Forms/LoginForm.cs | 8 +- .../GreenshotJiraPlugin.csproj | 4 + GreenshotJiraPlugin/Jira.cs | 95 ++++--------------- GreenshotJiraPlugin/JiraConfiguration.cs | 84 ++++++++++++++++ GreenshotJiraPlugin/JiraPluginBase.cs | 13 ++- GreenshotJiraPlugin/LanguageKeys.cs | 1 + .../Languages/language_jiraplugin-de-DE.xml | 5 +- .../Languages/language_jiraplugin-en-US.xml | 3 + .../Languages/language_jiraplugin-nl-NL.xml | 5 +- 10 files changed, 161 insertions(+), 99 deletions(-) create mode 100644 GreenshotJiraPlugin/JiraConfiguration.cs diff --git a/GreenshotJiraPlugin/Forms/LoginForm.Designer.cs b/GreenshotJiraPlugin/Forms/LoginForm.Designer.cs index 89f397d0d..3efefb39e 100644 --- a/GreenshotJiraPlugin/Forms/LoginForm.Designer.cs +++ b/GreenshotJiraPlugin/Forms/LoginForm.Designer.cs @@ -54,14 +54,16 @@ namespace GreenshotJiraPlugin { this.textBoxUser = new System.Windows.Forms.TextBox(); this.label_url = new System.Windows.Forms.Label(); this.textBoxUrl = new System.Windows.Forms.TextBox(); + this.checkBoxDoNotStorePassword = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // textBoxPassword // - this.textBoxPassword.Location = new System.Drawing.Point(118, 73); + 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(190, 20); + this.textBoxPassword.Size = new System.Drawing.Size(276, 20); this.textBoxPassword.TabIndex = 0; this.textBoxPassword.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TextBoxPasswordKeyUp); // @@ -69,13 +71,14 @@ namespace GreenshotJiraPlugin { // this.label_password.Location = new System.Drawing.Point(12, 73); this.label_password.Name = "label_password"; - this.label_password.Size = new System.Drawing.Size(100, 20); + this.label_password.Size = new System.Drawing.Size(84, 20); this.label_password.TabIndex = 1; this.label_password.Text = "Password"; // // buttonOK // - this.buttonOK.Location = new System.Drawing.Point(152, 102); + this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonOK.Location = new System.Drawing.Point(222, 139); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(75, 23); this.buttonOK.TabIndex = 2; @@ -85,7 +88,8 @@ namespace GreenshotJiraPlugin { // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(233, 102); + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(303, 139); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); this.buttonCancel.TabIndex = 3; @@ -97,37 +101,50 @@ namespace GreenshotJiraPlugin { // this.label_user.Location = new System.Drawing.Point(12, 47); this.label_user.Name = "label_user"; - this.label_user.Size = new System.Drawing.Size(100, 20); + this.label_user.Size = new System.Drawing.Size(84, 20); this.label_user.TabIndex = 5; this.label_user.Text = "User"; // // textBoxUser // - this.textBoxUser.Location = new System.Drawing.Point(118, 47); + 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(190, 20); + this.textBoxUser.Size = new System.Drawing.Size(276, 20); this.textBoxUser.TabIndex = 4; // // label_url // this.label_url.Location = new System.Drawing.Point(12, 21); this.label_url.Name = "label_url"; - this.label_url.Size = new System.Drawing.Size(100, 20); + this.label_url.Size = new System.Drawing.Size(84, 20); this.label_url.TabIndex = 7; this.label_url.Text = "Url"; // // textBoxUrl // - this.textBoxUrl.Location = new System.Drawing.Point(118, 21); + this.textBoxUrl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBoxUrl.Location = new System.Drawing.Point(102, 21); this.textBoxUrl.Name = "textBoxUrl"; - this.textBoxUrl.Size = new System.Drawing.Size(190, 20); + this.textBoxUrl.Size = new System.Drawing.Size(276, 20); this.textBoxUrl.TabIndex = 6; // + // checkBoxDoNotStorePassword + // + this.checkBoxDoNotStorePassword.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + 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.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(320, 139); + this.ClientSize = new System.Drawing.Size(387, 174); + this.Controls.Add(this.checkBoxDoNotStorePassword); this.Controls.Add(this.label_url); this.Controls.Add(this.textBoxUrl); this.Controls.Add(this.label_user); @@ -141,6 +158,7 @@ namespace GreenshotJiraPlugin { this.ResumeLayout(false); this.PerformLayout(); } + private System.Windows.Forms.CheckBox checkBoxDoNotStorePassword; private System.Windows.Forms.TextBox textBoxUrl; private System.Windows.Forms.Label label_url; private System.Windows.Forms.TextBox textBoxUser; diff --git a/GreenshotJiraPlugin/Forms/LoginForm.cs b/GreenshotJiraPlugin/Forms/LoginForm.cs index 57fb3d6d6..6ece429a9 100644 --- a/GreenshotJiraPlugin/Forms/LoginForm.cs +++ b/GreenshotJiraPlugin/Forms/LoginForm.cs @@ -45,6 +45,7 @@ namespace GreenshotJiraPlugin { this.label_password.Text = lang.GetString(LangKey.label_password); this.buttonOK.Text = lang.GetString(LangKey.OK); this.buttonCancel.Text = lang.GetString(LangKey.CANCEL); + this.checkBoxDoNotStorePassword.Text = lang.GetString(LangKey.label_no_password_store); this.Text = lang.GetString(LangKey.login_title); } @@ -62,7 +63,12 @@ namespace GreenshotJiraPlugin { 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) { this.DialogResult = DialogResult.OK; } diff --git a/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj b/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj index 224f854dc..3e72a8d0f 100644 --- a/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj +++ b/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj @@ -59,6 +59,7 @@ LoginForm.cs + @@ -83,6 +84,9 @@ MSDiscoCodeGenerator Reference.cs + + LoginForm.cs + diff --git a/GreenshotJiraPlugin/Jira.cs b/GreenshotJiraPlugin/Jira.cs index 6030ad71b..0c23cb284 100644 --- a/GreenshotJiraPlugin/Jira.cs +++ b/GreenshotJiraPlugin/Jira.cs @@ -99,118 +99,59 @@ namespace Jira { private const string JIRA_USER_PROPERTY = "user"; private const string JIRA_PASSWORD_PROPERTY = "password"; private const int DEFAULT_TIMEOUT = 29; - public const string CONFIG_FILENAME = "jira.properties"; - private const string DEFAULT_JIRA_URL = "http://jira/rpc/soap/jirasoapservice-v2?wsdl"; private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.jira.rpc.exception.RemoteAuthenticationException"; - private string configurationPath = null; private string credentials = null; private DateTime loggedInTime = DateTime.Now; private bool loggedIn = false; - private string tmpPassword = null; - private Properties config; + private JiraConfiguration config; private JiraSoapServiceService jira; private Dictionary userMap = new Dictionary(); - public JiraConnector(string configurationPath) { - this.configurationPath = configurationPath; - this.config = LoadConfig(); + public JiraConnector() { + this.config = IniConfig.GetIniSection(); jira = new JiraSoapServiceService(); - jira.Url = config.GetProperty(JIRA_URL_PROPERTY); + jira.Url = config.Url; } ~JiraConnector() { logout(); } - - public bool HasPassword() { - return config.ContainsKey(JIRA_PASSWORD_PROPERTY); - } - - public void SetTmpPassword(string password) { - tmpPassword = password; - } - - private Properties LoadConfig() { - Properties config = null; - string filename = Path.Combine(configurationPath, CONFIG_FILENAME); - if (File.Exists(filename)) { - LOG.Debug("Loading configuration from: " + filename); - config = Properties.read(filename); - } - bool changed = false; - if (config == null) { - config = new Properties(); - changed = true; - } - if (!config.ContainsKey(JIRA_URL_PROPERTY)) { - config.AddProperty(JIRA_URL_PROPERTY, DEFAULT_JIRA_URL); - changed = true; - } - if (!config.ContainsKey(JIRA_USER_PROPERTY)) { - config.AddProperty(JIRA_USER_PROPERTY, Environment.UserName); - changed = true; - } - if (changed) { - SaveConfig(config); - } - return config; - } - - private void SaveConfig(Properties config) { - string filename = Path.Combine(configurationPath, CONFIG_FILENAME); - LOG.Debug("Saving configuration to: " + filename); - StringBuilder comment = new StringBuilder(); - comment.AppendLine("# The configuration file for the JIRA Plugin"); - comment.AppendLine("#"); - comment.AppendLine("# Example settings:"); - comment.AppendLine("# " + JIRA_URL_PROPERTY + "=" + DEFAULT_JIRA_URL); - comment.AppendLine("# " + JIRA_USER_PROPERTY + "=Username"); - config.write(filename, comment.ToString()); - } public void login() { logout(); try { - if (HasPassword()) { - this.credentials = jira.login(config.GetProperty(JIRA_USER_PROPERTY), config.GetProperty(JIRA_PASSWORD_PROPERTY)); - } else if (tmpPassword != null) { - this.credentials = jira.login(config.GetProperty(JIRA_USER_PROPERTY), tmpPassword); + if (config.HasPassword()) { + this.credentials = jira.login(config.User, config.Password); + } else if (config.HasTmpPassword()) { + this.credentials = jira.login(config.User, config.TmpPassword); } else { - LoginForm pwForm = new LoginForm(); - pwForm.User = config.GetProperty(JIRA_USER_PROPERTY); - pwForm.Url = config.GetProperty(JIRA_URL_PROPERTY); - DialogResult result = pwForm.ShowDialog(); - if (result == DialogResult.OK) { - tmpPassword = pwForm.Password; - if (!pwForm.User.Equals(config.GetProperty(JIRA_USER_PROPERTY)) ||!pwForm.Url.Equals(config.GetProperty(JIRA_URL_PROPERTY))) { - config.ChangeProperty(JIRA_USER_PROPERTY, pwForm.User); - config.ChangeProperty(JIRA_URL_PROPERTY, pwForm.Url); - jira.Url = config.GetProperty(JIRA_URL_PROPERTY); - SaveConfig(config); - } - this.credentials = jira.login(config.GetProperty(JIRA_USER_PROPERTY), tmpPassword); + if (config.ShowConfigDialog()) { + if (config.HasPassword()) { + this.credentials = jira.login(config.User, config.Password); + } else if (config.HasTmpPassword()) { + this.credentials = jira.login(config.User, config.TmpPassword); + } } else { throw new Exception("User pressed cancel!"); } - } this.loggedInTime = DateTime.Now; this.loggedIn = true; } catch (Exception e) { this.loggedIn = false; this.credentials = null; - e.Data.Add("user",config.GetProperty(JIRA_USER_PROPERTY)); - e.Data.Add("url",config.GetProperty(JIRA_URL_PROPERTY)); + e.Data.Add("user",config.User); + 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! - this.tmpPassword = null; + config.Password = null; + config.TmpPassword = null; throw new Exception(e.Message.Replace(AUTH_FAILED_EXCEPTION_NAME+ ": ","")); } throw e; } } - public void logout() { if (credentials != null) { jira.logout(credentials); diff --git a/GreenshotJiraPlugin/JiraConfiguration.cs b/GreenshotJiraPlugin/JiraConfiguration.cs new file mode 100644 index 000000000..3e1b71768 --- /dev/null +++ b/GreenshotJiraPlugin/JiraConfiguration.cs @@ -0,0 +1,84 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +using Greenshot.Core; + +namespace GreenshotJiraPlugin { + /// + /// Description of CoreConfiguration. + /// + [IniSection("JIRA", Description="Greenshot JIRA Plugin configuration")] + public class JiraConfiguration : IniSection { + [IniProperty("Url", Description="Url to Jira system, including wsdl.", DefaultValue="http://jira/rpc/soap/jirasoapservice-v2?wsdl")] + public string Url; + [IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")] + 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); + } + + /// + /// A form for username/password + /// + /// bool true if OK was pressed, false if cancel + public bool ShowConfigDialog() { + LoginForm pwForm = new LoginForm(); + if (User == null || User.Length == 0) { + User = Environment.UserName; + } + pwForm.User = User; + pwForm.Url = Url; + DialogResult result = pwForm.ShowDialog(); + if (result == DialogResult.OK) { + if (pwForm.DoNotStorePassword) { + 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; + } + IniConfig.Save(); + return true; + } + return false; + } + } +} diff --git a/GreenshotJiraPlugin/JiraPluginBase.cs b/GreenshotJiraPlugin/JiraPluginBase.cs index 898ced7ec..75b55306f 100644 --- a/GreenshotJiraPlugin/JiraPluginBase.cs +++ b/GreenshotJiraPlugin/JiraPluginBase.cs @@ -58,8 +58,6 @@ namespace GreenshotJiraPlugin { this.captureHost = captureHost; this.myAttributes = myAttributes; host.OnImageEditorOpen += new OnImageEditorOpenHandler(ImageEditorOpened); - - this.jiraConnector = new JiraConnector(host.ConfigurationPath); } public virtual void Shutdown() { @@ -72,12 +70,9 @@ namespace GreenshotJiraPlugin { /// Implementation of the IPlugin.Configure /// public virtual void Configure() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.AppendLine("This plugin doesn't have a configuration screen."); - stringBuilder.AppendLine("Configuration is stored at: " + Path.Combine(host.ConfigurationPath, JiraConnector.CONFIG_FILENAME)); - MessageBox.Show(stringBuilder.ToString()); + IniConfig.GetIniSection().ShowConfigDialog(); } - + /// /// This will be called when Greenshot is shutting down /// @@ -109,6 +104,10 @@ namespace GreenshotJiraPlugin { ToolStripMenuItem item = (ToolStripMenuItem)sender; IImageEditor imageEditor = (IImageEditor)item.Tag; + if (jiraConnector == null) { + this.jiraConnector = new JiraConnector(); + } + JiraForm jiraForm = new JiraForm(jiraConnector); if (jiraConnector.isLoggedIn()) { //jiraForm.setFilename(host.GetFilename("png")); diff --git a/GreenshotJiraPlugin/LanguageKeys.cs b/GreenshotJiraPlugin/LanguageKeys.cs index 319eb8593..73bd26538 100644 --- a/GreenshotJiraPlugin/LanguageKeys.cs +++ b/GreenshotJiraPlugin/LanguageKeys.cs @@ -36,6 +36,7 @@ namespace GreenshotJiraPlugin { label_url, label_user, label_password, + label_no_password_store, OK, CANCEL, upload_success, diff --git a/GreenshotJiraPlugin/Languages/language_jiraplugin-de-DE.xml b/GreenshotJiraPlugin/Languages/language_jiraplugin-de-DE.xml index 0e00d80b1..5cc8fb3cd 100644 --- a/GreenshotJiraPlugin/Languages/language_jiraplugin-de-DE.xml +++ b/GreenshotJiraPlugin/Languages/language_jiraplugin-de-DE.xml @@ -43,7 +43,10 @@ Password - + + Kennwort nicht speichern + + OK diff --git a/GreenshotJiraPlugin/Languages/language_jiraplugin-en-US.xml b/GreenshotJiraPlugin/Languages/language_jiraplugin-en-US.xml index 7bd5ae106..788c8c3f7 100644 --- a/GreenshotJiraPlugin/Languages/language_jiraplugin-en-US.xml +++ b/GreenshotJiraPlugin/Languages/language_jiraplugin-en-US.xml @@ -40,6 +40,9 @@ Password + + Do not store the password + Please enter your Jira login data diff --git a/GreenshotJiraPlugin/Languages/language_jiraplugin-nl-NL.xml b/GreenshotJiraPlugin/Languages/language_jiraplugin-nl-NL.xml index 413cbffdc..044acf009 100644 --- a/GreenshotJiraPlugin/Languages/language_jiraplugin-nl-NL.xml +++ b/GreenshotJiraPlugin/Languages/language_jiraplugin-nl-NL.xml @@ -38,7 +38,10 @@ Gebruiker - Password + Wachtwoord + + + Het wachtwoord niet opslaan Geef uw Jira login data