diff --git a/GreenshotConfluencePlugin/Confluence.cs b/GreenshotConfluencePlugin/Confluence.cs index 164a6433e..212cf0100 100644 --- a/GreenshotConfluencePlugin/Confluence.cs +++ b/GreenshotConfluencePlugin/Confluence.cs @@ -25,8 +25,9 @@ using System.IO; using System.Text; using System.Windows.Forms; -using GreenshotConfluencePlugin; using Greenshot.Core; +using Greenshot.Helpers; +using GreenshotConfluencePlugin; /// /// For details see the Confluence API site @@ -52,53 +53,75 @@ namespace Confluence { private string credentials = null; private DateTime loggedInTime = DateTime.Now; private bool loggedIn = false; - private ConfluenceConfiguration config; private ConfluenceSoapServiceService confluence; + private int timeout; + private string url; private Dictionary userMap = new Dictionary(); - public ConfluenceConnector() { - this.config = IniConfig.GetIniSection(); + public ConfluenceConnector(string url, int timeout) { + this.url = url; + this.timeout = timeout; confluence = new ConfluenceSoapServiceService(); - confluence.Url = config.Url; + confluence.Url = url; } ~ConfluenceConnector() { logout(); } + /// + /// Internal login which catches the exceptions + /// + /// true if login was done sucessfully + private bool doLogin(string user, string password) { + try { + this.credentials = confluence.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", url); + throw e; + } + return true; + } public void login() { logout(); try { - if (config.HasPassword()) { - this.credentials = confluence.login(config.User, config.Password); - } else if (config.HasTmpPassword()) { - this.credentials = confluence.login(config.User, config.TmpPassword); - } else { - if (config.ShowConfigDialog()) { - if (config.HasPassword()) { - this.credentials = confluence.login(config.User, config.Password); - } else if (config.HasTmpPassword()) { - this.credentials = confluence.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.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! - config.Password = null; - config.TmpPassword = null; - throw new Exception(e.Message.Replace(AUTH_FAILED_EXCEPTION_NAME+ ": ","")); - } - throw e; - } + // Get the system name, so the user knows where to login to + string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX,""); + CredentialsDialog dialog = new CredentialsDialog(systemName); + dialog.Name = null; + while (dialog.Show(dialog.Name) == DialogResult.OK) { + if (doLogin(dialog.Name, dialog.Password)) { + if (dialog.SaveChecked) { + dialog.Confirm(true); + } + return; + } else { + try { + dialog.Confirm(false); + } catch (ApplicationException e) { + // exception handling ... + LOG.Error("Problem using the credentials dialog", e); + } + // For every windows version after XP show an incorrect password baloon + dialog.IncorrectPassword = true; + // Make sure the dialog is display, the password was false! + dialog.AlwaysDisplay = true; + } + } + } catch (ApplicationException e) { + // exception handling ... + LOG.Error("Problem using the credentials dialog", e); + } } public void logout() { @@ -111,7 +134,7 @@ namespace Confluence { private void checkCredentials() { if (loggedIn) { - if (loggedInTime.AddMinutes(config.Timeout-1).CompareTo(DateTime.Now) < 0) { + if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) { logout(); login(); } diff --git a/GreenshotConfluencePlugin/ConfluenceConfiguration.cs b/GreenshotConfluencePlugin/ConfluenceConfiguration.cs index 52dbabbb4..8ed8e7d26 100644 --- a/GreenshotConfluencePlugin/ConfluenceConfiguration.cs +++ b/GreenshotConfluencePlugin/ConfluenceConfiguration.cs @@ -30,50 +30,33 @@ namespace GreenshotConfluencePlugin { /// [IniSection("Confluence", Description="Greenshot Confluence Plugin configuration")] public class ConfluenceConfiguration : IniSection { - [IniProperty("Url", Description="Url to Confluence system, including wsdl.", DefaultValue="http://confluence/rpc/soap-axis/confluenceservice-v1?wsdl")] + public const string DEFAULT_POSTFIX = "/rpc/soap-axis/confluenceservice-v1?wsdl"; + public const string DEFAULT_PREFIX = "http://"; + private const string DEFAULT_URL = DEFAULT_PREFIX + "confluence" + DEFAULT_POSTFIX; + + [IniProperty("Url", Description="Url to Confluence system, including wsdl.", DefaultValue=DEFAULT_URL)] public string Url; [IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")] public int Timeout; - [IniProperty("User", Description="User for the Confluence System")] - public string User; - [IniProperty("Password", Description="Password for the Confluence 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); - } + [IniProperty("UploadFormat", Description="What file type to use for uploading", DefaultValue="Png")] + public OutputFormat UploadFormat; + [IniProperty("UploadJpegQuality", Description="JPEG file save quality in %.", DefaultValue="80")] + public int UploadJpegQuality; /// /// 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(); + SettingsForm settingsForm = new SettingsForm(); + settingsForm.Url = Url; + settingsForm.UploadFormat = UploadFormat.ToString(); + DialogResult result = settingsForm.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; + if (!settingsForm.Url.Equals(Url) || !settingsForm.UploadFormat.Equals(UploadFormat.ToString())) { + Url = settingsForm.Url; + UploadFormat = (OutputFormat)Enum.Parse(typeof(OutputFormat), settingsForm.UploadFormat); } IniConfig.Save(); return true; diff --git a/GreenshotConfluencePlugin/ConfluencePluginBase.cs b/GreenshotConfluencePlugin/ConfluencePluginBase.cs index d90c97acc..009df3dd6 100644 --- a/GreenshotConfluencePlugin/ConfluencePluginBase.cs +++ b/GreenshotConfluencePlugin/ConfluencePluginBase.cs @@ -43,6 +43,7 @@ namespace GreenshotConfluencePlugin { private ICaptureHost captureHost = null; private ConfluenceConnector confluenceConnector = null; private PluginAttribute myAttributes; + private ConfluenceConfiguration config = null; public ConfluencePlugin() { } @@ -60,7 +61,10 @@ namespace GreenshotConfluencePlugin { host.OnImageEditorOpen += new OnImageEditorOpenHandler(ImageEditorOpened); // Register configuration (don't need the configuration itself) - IniConfig.GetIniSection(); + config = IniConfig.GetIniSection(); + if(config.IsDirty) { + IniConfig.Save(); + } } public virtual void Shutdown() { @@ -75,7 +79,7 @@ namespace GreenshotConfluencePlugin { /// Implementation of the IPlugin.Configure /// public virtual void Configure() { - IniConfig.GetIniSection().ShowConfigDialog(); + config.ShowConfigDialog(); } /// @@ -110,21 +114,24 @@ namespace GreenshotConfluencePlugin { IImageEditor imageEditor = (IImageEditor)item.Tag; if (confluenceConnector == null) { - this.confluenceConnector = new ConfluenceConnector(); + confluenceConnector = new ConfluenceConnector(config.Url, config.Timeout); } - ConfluenceForm confluenceForm = new ConfluenceForm(confluenceConnector); - confluenceForm.setFilename(host.GetFilename(OutputFormat.Png, imageEditor.CaptureDetails)); - DialogResult result = confluenceForm.ShowDialog(); - if (result == DialogResult.OK) { - using (MemoryStream stream = new MemoryStream()) { - imageEditor.SaveToStream(stream, OutputFormat.Png, 100); - byte [] buffer = stream.GetBuffer(); - try { - confluenceForm.upload(buffer); - LOG.Debug("Uploaded to Confluence."); - MessageBox.Show(lang.GetString(LangKey.upload_success)); - } catch(Exception e) { - MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message); + + if (confluenceConnector.isLoggedIn()) { + ConfluenceForm confluenceForm = new ConfluenceForm(confluenceConnector); + confluenceForm.setFilename(host.GetFilename(config.UploadFormat, imageEditor.CaptureDetails)); + DialogResult result = confluenceForm.ShowDialog(); + if (result == DialogResult.OK) { + using (MemoryStream stream = new MemoryStream()) { + imageEditor.SaveToStream(stream, config.UploadFormat, config.UploadJpegQuality); + byte [] buffer = stream.GetBuffer(); + try { + confluenceForm.upload(buffer); + LOG.Debug("Uploaded to Confluence."); + MessageBox.Show(lang.GetString(LangKey.upload_success)); + } catch(Exception e) { + MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message); + } } } } diff --git a/GreenshotConfluencePlugin/Forms/LoginForm.Designer.cs b/GreenshotConfluencePlugin/Forms/SettingsForm.Designer.cs similarity index 58% rename from GreenshotConfluencePlugin/Forms/LoginForm.Designer.cs rename to GreenshotConfluencePlugin/Forms/SettingsForm.Designer.cs index 9d793e904..5975ce076 100644 --- a/GreenshotConfluencePlugin/Forms/LoginForm.Designer.cs +++ b/GreenshotConfluencePlugin/Forms/SettingsForm.Designer.cs @@ -19,7 +19,7 @@ * along with this program. If not, see . */ namespace GreenshotConfluencePlugin { - partial class LoginForm { + partial class SettingsForm { /// /// Designer variable used to keep track of non-visual components. /// @@ -46,35 +46,14 @@ namespace GreenshotConfluencePlugin { /// 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.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.textBoxUrl = new System.Windows.Forms.TextBox(); - this.checkBoxDoNotStorePassword = new System.Windows.Forms.CheckBox(); + this.combobox_uploadimageformat = new System.Windows.Forms.ComboBox(); + this.label_upload_format = new System.Windows.Forms.Label(); 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 // this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); @@ -97,22 +76,6 @@ namespace GreenshotConfluencePlugin { this.buttonCancel.UseVisualStyleBackColor = true; 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 // this.label_url.Location = new System.Drawing.Point(12, 21); @@ -129,44 +92,43 @@ namespace GreenshotConfluencePlugin { this.textBoxUrl.Size = new System.Drawing.Size(276, 20); this.textBoxUrl.TabIndex = 6; // - // checkBoxDoNotStorePassword + // combobox_uploadimageformat // - 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; + this.combobox_uploadimageformat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.combobox_uploadimageformat.FormattingEnabled = true; + this.combobox_uploadimageformat.Location = new System.Drawing.Point(102, 47); + this.combobox_uploadimageformat.Name = "combobox_uploadimageformat"; + this.combobox_uploadimageformat.Size = new System.Drawing.Size(276, 21); + this.combobox_uploadimageformat.TabIndex = 8; // - // LoginForm + // label_upload_format + // + this.label_upload_format.Location = new System.Drawing.Point(12, 50); + this.label_upload_format.Name = "label_upload_format"; + this.label_upload_format.Size = new System.Drawing.Size(84, 20); + this.label_upload_format.TabIndex = 9; + this.label_upload_format.Text = "Upload format"; + // + // SettingsForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(387, 174); - this.Controls.Add(this.checkBoxDoNotStorePassword); + this.Controls.Add(this.label_upload_format); + this.Controls.Add(this.combobox_uploadimageformat); this.Controls.Add(this.label_url); 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.buttonOK); - this.Controls.Add(this.label_password); - this.Controls.Add(this.textBoxPassword); - this.Name = "LoginForm"; + this.Name = "SettingsForm"; this.Text = "Please enter your Confluence data"; this.ResumeLayout(false); this.PerformLayout(); } - private System.Windows.Forms.CheckBox checkBoxDoNotStorePassword; + private System.Windows.Forms.ComboBox combobox_uploadimageformat; + private System.Windows.Forms.Label label_upload_format; private System.Windows.Forms.TextBox textBoxUrl; 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 buttonOK; } diff --git a/GreenshotConfluencePlugin/Forms/LoginForm.cs b/GreenshotConfluencePlugin/Forms/SettingsForm.cs similarity index 71% rename from GreenshotConfluencePlugin/Forms/LoginForm.cs rename to GreenshotConfluencePlugin/Forms/SettingsForm.cs index 414ee8df9..f9f6250d2 100644 --- a/GreenshotConfluencePlugin/Forms/LoginForm.cs +++ b/GreenshotConfluencePlugin/Forms/SettingsForm.cs @@ -28,25 +28,28 @@ namespace GreenshotConfluencePlugin { /// /// Description of PasswordRequestForm. /// - public partial class LoginForm : Form { + public partial class SettingsForm : Form { private ILanguage lang = Language.GetInstance(); - public LoginForm() { + public SettingsForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); InitializeTexts(); + + combobox_uploadimageformat.Items.Clear(); + foreach(OutputFormat format in Enum.GetValues(typeof(OutputFormat))) { + combobox_uploadimageformat.Items.Add(format.ToString()); + } } private void InitializeTexts() { 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.buttonCancel.Text = lang.GetString(LangKey.CANCEL); - this.checkBoxDoNotStorePassword.Text = lang.GetString(LangKey.label_no_password_store); this.Text = lang.GetString(LangKey.login_title); + this.label_upload_format.Text = lang.GetString(LangKey.label_upload_format); } public string Url { @@ -54,19 +57,9 @@ namespace GreenshotConfluencePlugin { 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;} + public string UploadFormat { + get {return combobox_uploadimageformat.Text;} + set {combobox_uploadimageformat.Text = value;} } void ButtonOKClick(object sender, EventArgs e) { @@ -76,11 +69,5 @@ namespace GreenshotConfluencePlugin { void ButtonCancelClick(object sender, System.EventArgs e) { this.DialogResult = DialogResult.Cancel; } - - void TextBoxPasswordKeyUp(object sender, KeyEventArgs e) { - if (e.KeyCode == Keys.Enter) { - this.DialogResult = DialogResult.OK; - } - } } } diff --git a/GreenshotConfluencePlugin/GreenshotConfluencePlugin.csproj b/GreenshotConfluencePlugin/GreenshotConfluencePlugin.csproj index 708c0f4ce..cad7cc9e0 100644 --- a/GreenshotConfluencePlugin/GreenshotConfluencePlugin.csproj +++ b/GreenshotConfluencePlugin/GreenshotConfluencePlugin.csproj @@ -56,9 +56,9 @@ ConfluenceForm.cs - - - LoginForm.cs + + + SettingsForm.cs diff --git a/GreenshotConfluencePlugin/LanguageKeys.cs b/GreenshotConfluencePlugin/LanguageKeys.cs index 17ef23bd2..4f3408d9c 100644 --- a/GreenshotConfluencePlugin/LanguageKeys.cs +++ b/GreenshotConfluencePlugin/LanguageKeys.cs @@ -25,9 +25,7 @@ namespace GreenshotConfluencePlugin { login_error, login_title, label_url, - label_user, - label_password, - label_no_password_store, + label_upload_format, OK, CANCEL, upload_success, diff --git a/GreenshotJiraPlugin/JiraConfiguration.cs b/GreenshotJiraPlugin/JiraConfiguration.cs index eefce4485..ff39b71bc 100644 --- a/GreenshotJiraPlugin/JiraConfiguration.cs +++ b/GreenshotJiraPlugin/JiraConfiguration.cs @@ -38,7 +38,7 @@ namespace GreenshotJiraPlugin { public string Url; [IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")] public int Timeout; - + [IniProperty("UploadFormat", Description="What file type to use for uploading", DefaultValue="Png")] public OutputFormat UploadFormat; [IniProperty("UploadJpegQuality", Description="JPEG file save quality in %.", DefaultValue="80")] diff --git a/GreenshotJiraPlugin/JiraPluginBase.cs b/GreenshotJiraPlugin/JiraPluginBase.cs index beed4894f..395015e77 100644 --- a/GreenshotJiraPlugin/JiraPluginBase.cs +++ b/GreenshotJiraPlugin/JiraPluginBase.cs @@ -81,7 +81,7 @@ namespace GreenshotJiraPlugin { public virtual void Configure() { config.ShowConfigDialog(); } - + /// /// This will be called when Greenshot is shutting down /// @@ -114,11 +114,11 @@ namespace GreenshotJiraPlugin { IImageEditor imageEditor = (IImageEditor)item.Tag; if (jiraConnector == null) { - this.jiraConnector = new JiraConnector(config.Url, config.Timeout); + jiraConnector = new JiraConnector(config.Url, config.Timeout); } - JiraForm jiraForm = new JiraForm(jiraConnector); if (jiraConnector.isLoggedIn()) { + JiraForm jiraForm = new JiraForm(jiraConnector); jiraForm.setFilename(host.GetFilename(config.UploadFormat, imageEditor.CaptureDetails)); DialogResult result = jiraForm.ShowDialog(); if (result == DialogResult.OK) { @@ -127,6 +127,7 @@ namespace GreenshotJiraPlugin { byte [] buffer = stream.GetBuffer(); try { jiraForm.upload(buffer); + LOG.Debug("Uploaded to Jira."); MessageBox.Show(lang.GetString(LangKey.upload_success)); } catch(Exception e) { MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);