Refactored ConfluencePlugin to be like the JiraPlugin

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@897 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-09-22 10:02:03 +00:00
parent 0b3a36892d
commit c8184d1a76
9 changed files with 142 additions and 181 deletions

View file

@ -25,8 +25,9 @@ using System.IO;
using System.Text;
using System.Windows.Forms;
using GreenshotConfluencePlugin;
using Greenshot.Core;
using Greenshot.Helpers;
using GreenshotConfluencePlugin;
/// <summary>
/// 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<string, string> userMap = new Dictionary<string, string>();
public ConfluenceConnector() {
this.config = IniConfig.GetIniSection<ConfluenceConfiguration>();
public ConfluenceConnector(string url, int timeout) {
this.url = url;
this.timeout = timeout;
confluence = new ConfluenceSoapServiceService();
confluence.Url = config.Url;
confluence.Url = url;
}
~ConfluenceConnector() {
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 = 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();
}

View file

@ -30,50 +30,33 @@ namespace GreenshotConfluencePlugin {
/// </summary>
[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;
/// <summary>
/// A form for username/password
/// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns>
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;

View file

@ -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<ConfluenceConfiguration>();
config = IniConfig.GetIniSection<ConfluenceConfiguration>();
if(config.IsDirty) {
IniConfig.Save();
}
}
public virtual void Shutdown() {
@ -75,7 +79,7 @@ namespace GreenshotConfluencePlugin {
/// Implementation of the IPlugin.Configure
/// </summary>
public virtual void Configure() {
IniConfig.GetIniSection<ConfluenceConfiguration>().ShowConfigDialog();
config.ShowConfigDialog();
}
/// <summary>
@ -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);
}
}
}
}

View file

@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GreenshotConfluencePlugin {
partial class LoginForm {
partial class SettingsForm {
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
@ -46,35 +46,14 @@ namespace GreenshotConfluencePlugin {
/// </summary>
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;
}

View file

@ -28,25 +28,28 @@ namespace GreenshotConfluencePlugin {
/// <summary>
/// Description of PasswordRequestForm.
/// </summary>
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;
}
}
}
}

View file

@ -56,9 +56,9 @@
<Compile Include="Forms\ConfluenceForm.Designer.cs">
<DependentUpon>ConfluenceForm.cs</DependentUpon>
</Compile>
<Compile Include="Forms\LoginForm.cs" />
<Compile Include="Forms\LoginForm.Designer.cs">
<DependentUpon>LoginForm.cs</DependentUpon>
<Compile Include="Forms\SettingsForm.cs" />
<Compile Include="Forms\SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="Confluence.cs" />
<Compile Include="Language.cs" />

View file

@ -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,

View file

@ -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);