Code quality changes [skip ci]

This commit is contained in:
Robin 2016-08-16 10:37:55 +02:00
commit 798ca503a5
108 changed files with 1981 additions and 2258 deletions

View file

@ -18,7 +18,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GreenshotJiraPlugin {
namespace GreenshotJiraPlugin.Forms {
partial class JiraForm {
/// <summary>
/// Required designer variable.

View file

@ -18,21 +18,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Globalization;
using System.Windows.Forms;
using Greenshot.IniFile;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
using Jira;
namespace GreenshotJiraPlugin {
namespace GreenshotJiraPlugin.Forms {
public partial class JiraForm : Form {
private readonly JiraConnector jiraConnector;
private JiraIssue selectedIssue;
private readonly GreenshotColumnSorter columnSorter;
private readonly JiraConfiguration config = IniConfig.GetIniSection<JiraConfiguration>();
private readonly JiraConnector _jiraConnector;
private JiraIssue _selectedIssue;
private readonly GreenshotColumnSorter _columnSorter;
private readonly JiraConfiguration _config = IniConfig.GetIniSection<JiraConfiguration>();
public JiraForm(JiraConnector jiraConnector) {
InitializeComponent();
@ -40,90 +39,90 @@ namespace GreenshotJiraPlugin {
AcceptButton = uploadButton;
CancelButton = cancelButton;
initializeComponentText();
InitializeComponentText();
columnSorter = new GreenshotColumnSorter();
jiraListView.ListViewItemSorter = columnSorter;
_columnSorter = new GreenshotColumnSorter();
jiraListView.ListViewItemSorter = _columnSorter;
this.jiraConnector = jiraConnector;
_jiraConnector = jiraConnector;
changeModus(false);
ChangeModus(false);
try {
if (!jiraConnector.isLoggedIn) {
jiraConnector.login();
if (!jiraConnector.IsLoggedIn) {
jiraConnector.Login();
}
} catch (Exception e) {
MessageBox.Show(Language.GetFormattedString("jira", LangKey.login_error, e.Message));
}
uploadButton.Enabled = false;
updateForm();
UpdateForm();
}
private void initializeComponentText() {
private void InitializeComponentText() {
label_jirafilter.Text = Language.GetString("jira", LangKey.label_jirafilter);
label_comment.Text = Language.GetString("jira", LangKey.label_comment);
label_filename.Text = Language.GetString("jira", LangKey.label_filename);
}
private void updateForm() {
if (jiraConnector.isLoggedIn) {
JiraFilter[] filters = jiraConnector.getFilters();
private void UpdateForm() {
if (_jiraConnector.IsLoggedIn) {
JiraFilter[] filters = _jiraConnector.GetFilters();
if (filters.Length > 0) {
foreach (JiraFilter filter in filters) {
jiraFilterBox.Items.Add(filter);
}
jiraFilterBox.SelectedIndex = 0;
}
changeModus(true);
if (config.LastUsedJira != null) {
selectedIssue = jiraConnector.getIssue(config.LastUsedJira);
if (selectedIssue != null) {
jiraKey.Text = config.LastUsedJira;
ChangeModus(true);
if (_config.LastUsedJira != null) {
_selectedIssue = _jiraConnector.GetIssue(_config.LastUsedJira);
if (_selectedIssue != null) {
jiraKey.Text = _config.LastUsedJira;
uploadButton.Enabled = true;
}
}
}
}
private void changeModus(bool enabled) {
private void ChangeModus(bool enabled) {
jiraFilterBox.Enabled = enabled;
jiraListView.Enabled = enabled;
jiraFilenameBox.Enabled = enabled;
jiraCommentBox.Enabled = enabled;
}
public void setFilename(string filename) {
public void SetFilename(string filename) {
jiraFilenameBox.Text = filename;
}
public void setComment(string comment) {
public void SetComment(string comment) {
jiraCommentBox.Text = comment;
}
public JiraIssue getJiraIssue() {
return selectedIssue;
public JiraIssue GetJiraIssue() {
return _selectedIssue;
}
public void upload(IBinaryContainer attachment) {
config.LastUsedJira = selectedIssue.Key;
jiraConnector.addAttachment(selectedIssue.Key, jiraFilenameBox.Text, attachment);
if (jiraCommentBox.Text != null && jiraCommentBox.Text.Length > 0) {
jiraConnector.addComment(selectedIssue.Key, jiraCommentBox.Text);
public void Upload(IBinaryContainer attachment) {
_config.LastUsedJira = _selectedIssue.Key;
_jiraConnector.AddAttachment(_selectedIssue.Key, jiraFilenameBox.Text, attachment);
if (!string.IsNullOrEmpty(jiraCommentBox.Text)) {
_jiraConnector.AddComment(_selectedIssue.Key, jiraCommentBox.Text);
}
}
public void logout() {
jiraConnector.logout();
public void Logout() {
_jiraConnector.Logout();
}
private void selectJiraToolStripMenuItem_Click(object sender, EventArgs e) {
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
selectedIssue = (JiraIssue)clickedItem.Tag;
jiraKey.Text = selectedIssue.Key;
_selectedIssue = (JiraIssue)clickedItem.Tag;
jiraKey.Text = _selectedIssue.Key;
}
private void jiraFilterBox_SelectedIndexChanged(object sender, EventArgs e) {
if (jiraConnector.isLoggedIn) {
if (_jiraConnector.IsLoggedIn) {
JiraIssue[] issues = null;
uploadButton.Enabled = false;
JiraFilter filter = (JiraFilter)jiraFilterBox.SelectedItem;
@ -133,7 +132,7 @@ namespace GreenshotJiraPlugin {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(JiraPlugin.Instance.JiraPluginAttributes.Name, Language.GetString("jira", LangKey.communication_wait),
delegate() {
issues = jiraConnector.getIssuesForFilter(filter.Id);
issues = _jiraConnector.GetIssuesForFilter(filter.Id);
}
);
jiraListView.BeginUpdate();
@ -145,8 +144,10 @@ namespace GreenshotJiraPlugin {
jiraListView.Columns.Add(Language.GetString("jira", column));
}
foreach (JiraIssue issue in issues) {
ListViewItem item = new ListViewItem(issue.Key);
item.Tag = issue;
ListViewItem item = new ListViewItem(issue.Key)
{
Tag = issue
};
item.SubItems.Add(issue.Created.Value.ToString("d", DateTimeFormatInfo.InvariantInfo));
item.SubItems.Add(issue.Assignee);
item.SubItems.Add(issue.Reporter);
@ -164,8 +165,8 @@ namespace GreenshotJiraPlugin {
private void jiraListView_SelectedIndexChanged(object sender, EventArgs e) {
if (jiraListView.SelectedItems != null && jiraListView.SelectedItems.Count > 0) {
selectedIssue = (JiraIssue)jiraListView.SelectedItems[0].Tag;
jiraKey.Text = selectedIssue.Key;
_selectedIssue = (JiraIssue)jiraListView.SelectedItems[0].Tag;
jiraKey.Text = _selectedIssue.Key;
uploadButton.Enabled = true;
} else {
uploadButton.Enabled = false;
@ -174,17 +175,17 @@ namespace GreenshotJiraPlugin {
private void jiraListView_ColumnClick(object sender, ColumnClickEventArgs e) {
// Determine if clicked column is already the column that is being sorted.
if (e.Column == columnSorter.SortColumn) {
if (e.Column == _columnSorter.SortColumn) {
// Reverse the current sort direction for this column.
if (columnSorter.Order == SortOrder.Ascending) {
columnSorter.Order = SortOrder.Descending;
if (_columnSorter.Order == SortOrder.Ascending) {
_columnSorter.Order = SortOrder.Descending;
} else {
columnSorter.Order = SortOrder.Ascending;
_columnSorter.Order = SortOrder.Ascending;
}
} else {
// Set the column number that is to be sorted; default to ascending.
columnSorter.SortColumn = e.Column;
columnSorter.Order = SortOrder.Ascending;
_columnSorter.SortColumn = e.Column;
_columnSorter.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
@ -196,8 +197,8 @@ namespace GreenshotJiraPlugin {
uploadButton.Enabled = false;
int dashIndex = jiranumber.IndexOf('-');
if (dashIndex > 0 && jiranumber.Length > dashIndex+1) {
selectedIssue = jiraConnector.getIssue(jiraKey.Text);
if (selectedIssue != null) {
_selectedIssue = _jiraConnector.GetIssue(jiraKey.Text);
if (_selectedIssue != null) {
uploadButton.Enabled = true;
}
}

View file

@ -21,7 +21,7 @@
using GreenshotPlugin.Controls;
namespace GreenshotJiraPlugin {
namespace GreenshotJiraPlugin.Forms {
public class JiraFormBase : GreenshotForm {
}
}

View file

@ -18,7 +18,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GreenshotJiraPlugin {
namespace GreenshotJiraPlugin.Forms {
partial class SettingsForm {
/// <summary>
/// Designer variable used to keep track of non-visual components.

View file

@ -19,12 +19,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GreenshotJiraPlugin {
namespace GreenshotJiraPlugin.Forms {
/// <summary>
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : JiraFormBase {
public SettingsForm(JiraConfiguration config) :base () {
public SettingsForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//

View file

@ -20,16 +20,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Greenshot.IniFile;
using GreenshotJiraPlugin;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Greenshot.IniFile;
using GreenshotJiraPlugin.Web_References.JiraSoap;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
namespace Jira {
namespace GreenshotJiraPlugin {
#region transport classes
public class JiraFilter {
public JiraFilter(string name, string id) {
@ -98,34 +98,34 @@ namespace Jira {
#endregion
public class JiraConnector : IDisposable {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraConnector));
private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.jira.rpc.exception.RemoteAuthenticationException";
private static readonly JiraConfiguration config = IniConfig.GetIniSection<JiraConfiguration>();
public const string DEFAULT_POSTFIX = "/rpc/soap/jirasoapservice-v2?wsdl";
private string credentials;
private DateTime loggedInTime = DateTime.Now;
private bool loggedIn;
private JiraSoapServiceService jira;
private readonly int timeout;
private string url;
private readonly Cache<string, JiraIssue> jiraCache = new Cache<string, JiraIssue>(60 * config.Timeout);
private readonly Cache<string, RemoteUser> userCache = new Cache<string, RemoteUser>(60 * config.Timeout);
private readonly bool suppressBackgroundForm = false;
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(JiraConnector));
private const string AuthFailedExceptionName = "com.atlassian.jira.rpc.exception.RemoteAuthenticationException";
private static readonly JiraConfiguration Config = IniConfig.GetIniSection<JiraConfiguration>();
public const string DefaultPostfix = "/rpc/soap/jirasoapservice-v2?wsdl";
private string _credentials;
private DateTime _loggedInTime = DateTime.Now;
private bool _loggedIn;
private JiraSoapServiceService _jira;
private readonly int _timeout;
private string _url;
private readonly Cache<string, JiraIssue> _jiraCache = new Cache<string, JiraIssue>(60 * Config.Timeout);
private readonly Cache<string, RemoteUser> _userCache = new Cache<string, RemoteUser>(60 * Config.Timeout);
private readonly bool _suppressBackgroundForm;
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (jira != null) {
logout();
protected void Dispose(bool disposing) {
if (_jira != null) {
Logout();
}
if (disposing) {
if (jira != null) {
jira.Dispose();
jira = null;
if (_jira != null) {
_jira.Dispose();
_jira = null;
}
}
}
@ -134,27 +134,27 @@ namespace Jira {
}
public JiraConnector(bool suppressBackgroundForm) {
url = config.Url;
timeout = config.Timeout;
this.suppressBackgroundForm = suppressBackgroundForm;
createService();
_url = Config.Url;
_timeout = Config.Timeout;
_suppressBackgroundForm = suppressBackgroundForm;
CreateService();
}
private void createService() {
if (!suppressBackgroundForm) {
private void CreateService() {
if (!_suppressBackgroundForm) {
new PleaseWaitForm().ShowAndWait(JiraPlugin.Instance.JiraPluginAttributes.Name, Language.GetString("jira", LangKey.communication_wait),
delegate() {
jira = new JiraSoapServiceService();
delegate {
_jira = new JiraSoapServiceService();
}
);
} else {
jira = new JiraSoapServiceService();
_jira = new JiraSoapServiceService();
}
jira.Url = url;
jira.Proxy = NetworkHelper.CreateProxy(new Uri(url));
_jira.Url = _url;
_jira.Proxy = NetworkHelper.CreateProxy(new Uri(_url));
// Do not use:
//jira.AllowAutoRedirect = true;
jira.UserAgent = "Greenshot";
_jira.UserAgent = "Greenshot";
}
~JiraConnector() {
@ -165,30 +165,30 @@ namespace Jira {
/// Internal login which catches the exceptions
/// </summary>
/// <returns>true if login was done sucessfully</returns>
private bool doLogin(string user, string password, bool suppressBackgroundForm) {
private bool DoLogin(string user, string password, bool suppressBackgroundForm) {
// This is what needs to be done
ThreadStart jiraLogin = delegate {
LOG.DebugFormat("Loggin in");
Log.DebugFormat("Loggin in");
try {
credentials = jira.login(user, password);
_credentials = _jira.login(user, password);
} catch (Exception) {
if (!url.EndsWith("wsdl")) {
url = url + "/rpc/soap/jirasoapservice-v2?wsdl";
if (!_url.EndsWith("wsdl")) {
_url = _url + "/rpc/soap/jirasoapservice-v2?wsdl";
// recreate the service with the new url
createService();
credentials = jira.login(user, password);
CreateService();
_credentials = _jira.login(user, password);
// Worked, store the url in the configuration
config.Url = url;
Config.Url = _url;
IniConfig.Save();
} else {
throw;
}
}
LOG.DebugFormat("Logged in");
loggedInTime = DateTime.Now;
loggedIn = true;
Log.DebugFormat("Logged in");
_loggedInTime = DateTime.Now;
_loggedIn = true;
};
// Here we do it
@ -200,125 +200,126 @@ namespace Jira {
}
} catch (Exception e) {
// check if auth failed
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
if (e.Message.Contains(AuthFailedExceptionName)) {
return false;
}
// Not an authentication issue
loggedIn = false;
credentials = null;
_loggedIn = false;
_credentials = null;
e.Data.Add("user", user);
e.Data.Add("url", url);
e.Data.Add("url", _url);
throw;
}
return true;
}
public void login() {
login(false);
public void Login() {
Login(false);
}
public void login(bool suppressBackgroundForm) {
logout();
public void Login(bool suppressBackgroundForm) {
Logout();
try {
// Get the system name, so the user knows where to login to
string systemName = url.Replace(DEFAULT_POSTFIX,"");
CredentialsDialog dialog = new CredentialsDialog(systemName);
dialog.Name = null;
string systemName = _url.Replace(DefaultPostfix,"");
CredentialsDialog dialog = new CredentialsDialog(systemName)
{
Name = null
};
while (dialog.Show(dialog.Name) == DialogResult.OK) {
if (doLogin(dialog.Name, dialog.Password, suppressBackgroundForm)) {
if (DoLogin(dialog.Name, dialog.Password, suppressBackgroundForm)) {
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;
}
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);
Log.Error("Problem using the credentials dialog", e);
}
}
public void logout() {
if (credentials != null) {
jira.logout(credentials);
credentials = null;
loggedIn = false;
public void Logout() {
if (_credentials != null) {
_jira.logout(_credentials);
_credentials = null;
_loggedIn = false;
}
}
private void checkCredentials() {
if (loggedIn) {
if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) {
logout();
login();
private void CheckCredentials() {
if (_loggedIn) {
if (_loggedInTime.AddMinutes(_timeout-1).CompareTo(DateTime.Now) < 0) {
Logout();
Login();
}
} else {
login();
Login();
}
}
public bool isLoggedIn {
public bool IsLoggedIn {
get {
return loggedIn;
return _loggedIn;
}
}
public JiraFilter[] getFilters() {
public JiraFilter[] GetFilters() {
List<JiraFilter> filters = new List<JiraFilter>();
checkCredentials();
RemoteFilter[] remoteFilters = jira.getSavedFilters(credentials);
CheckCredentials();
RemoteFilter[] remoteFilters = _jira.getSavedFilters(_credentials);
foreach (RemoteFilter remoteFilter in remoteFilters) {
filters.Add(new JiraFilter(remoteFilter.name, remoteFilter.id));
}
return filters.ToArray();
}
private JiraIssue createDummyErrorIssue(Exception e) {
private JiraIssue CreateDummyErrorIssue(Exception e) {
// Creating bogus jira to indicate a problem
return new JiraIssue("error", DateTime.Now, "error", "error", "error", e.Message, "error", "error", null);
}
public JiraIssue getIssue(string key) {
public JiraIssue GetIssue(string key) {
JiraIssue jiraIssue = null;
if (jiraCache.Contains(key)) {
jiraIssue = jiraCache[key];
if (_jiraCache.Contains(key)) {
jiraIssue = _jiraCache[key];
}
if (jiraIssue == null) {
checkCredentials();
CheckCredentials();
try {
RemoteIssue issue = jira.getIssue(credentials, key);
jiraIssue = new JiraIssue(issue.key, issue.created, getUserFullName(issue.reporter), getUserFullName(issue.assignee), issue.project, issue.summary, issue.description, issue.environment, issue.attachmentNames);
jiraCache.Add(key, jiraIssue);
RemoteIssue issue = _jira.getIssue(_credentials, key);
jiraIssue = new JiraIssue(issue.key, issue.created, GetUserFullName(issue.reporter), GetUserFullName(issue.assignee), issue.project, issue.summary, issue.description, issue.environment, issue.attachmentNames);
_jiraCache.Add(key, jiraIssue);
} catch (Exception e) {
LOG.Error("Problem retrieving Jira: " + key, e);
Log.Error("Problem retrieving Jira: " + key, e);
}
}
return jiraIssue;
}
public JiraIssue[] getIssuesForFilter(string filterId) {
public JiraIssue[] GetIssuesForFilter(string filterId) {
List<JiraIssue> issuesToReturn = new List<JiraIssue>();
checkCredentials();
CheckCredentials();
try {
RemoteIssue[] issues = jira.getIssuesFromFilter(credentials, filterId);
RemoteIssue[] issues = _jira.getIssuesFromFilter(_credentials, filterId);
#region Username cache update
List<string> users = new List<string>();
foreach (RemoteIssue issue in issues) {
if (issue.reporter != null && !hasUser(issue.reporter) && !users.Contains(issue.reporter)) {
if (issue.reporter != null && !HasUser(issue.reporter) && !users.Contains(issue.reporter)) {
users.Add(issue.reporter);
}
if (issue.assignee != null && !hasUser(issue.assignee) && !users.Contains(issue.assignee)) {
if (issue.assignee != null && !HasUser(issue.assignee) && !users.Contains(issue.assignee)) {
users.Add(issue.assignee);
}
}
@ -327,8 +328,8 @@ namespace Jira {
ManualResetEvent doneEvent = new ManualResetEvent(false);
for (int i = 0; i < users.Count; i++) {
ThreadPool.QueueUserWorkItem(delegate(object name) {
LOG.InfoFormat("Retrieving {0}", name);
getUserFullName((string)name);
Log.InfoFormat("Retrieving {0}", name);
GetUserFullName((string)name);
if (Interlocked.Decrement(ref taskCount) == 0) {
doneEvent.Set();
}
@ -340,65 +341,67 @@ namespace Jira {
foreach (RemoteIssue issue in issues) {
try {
JiraIssue jiraIssue = new JiraIssue(issue.key, issue.created, getUserFullName(issue.reporter), getUserFullName(issue.assignee), issue.project, issue.summary, issue.description, "", issue.attachmentNames);
JiraIssue jiraIssue = new JiraIssue(issue.key, issue.created, GetUserFullName(issue.reporter), GetUserFullName(issue.assignee), issue.project, issue.summary, issue.description, "", issue.attachmentNames);
issuesToReturn.Add(jiraIssue);
} catch (Exception e) {
LOG.Error("Problem retrieving Jira: " + issue.key, e);
JiraIssue jiraIssue = createDummyErrorIssue(e);
Log.Error("Problem retrieving Jira: " + issue.key, e);
JiraIssue jiraIssue = CreateDummyErrorIssue(e);
jiraIssue.Key = issue.key;
issuesToReturn.Add(jiraIssue);
}
}
} catch (Exception e) {
LOG.Error("Problem retrieving Jiras for Filter: " + filterId, e);
issuesToReturn.Add(createDummyErrorIssue(e));
Log.Error("Problem retrieving Jiras for Filter: " + filterId, e);
issuesToReturn.Add(CreateDummyErrorIssue(e));
}
return issuesToReturn.ToArray(); ;
}
public string getURL(string issueKey) {
return url.Replace(DEFAULT_POSTFIX,"") + "/browse/" + issueKey;
public string GetUrl(string issueKey) {
return _url.Replace(DefaultPostfix,"") + "/browse/" + issueKey;
}
public void addAttachment(string issueKey, string filename, IBinaryContainer attachment) {
checkCredentials();
public void AddAttachment(string issueKey, string filename, IBinaryContainer attachment) {
CheckCredentials();
try {
jira.addBase64EncodedAttachmentsToIssue(credentials, issueKey, new string[] { filename }, new string[] { attachment.ToBase64String(Base64FormattingOptions.InsertLineBreaks) });
_jira.addBase64EncodedAttachmentsToIssue(_credentials, issueKey, new[] { filename }, new[] { attachment.ToBase64String(Base64FormattingOptions.InsertLineBreaks) });
} catch (Exception ex1) {
LOG.WarnFormat("Failed to upload by using method addBase64EncodedAttachmentsToIssue, error was {0}", ex1.Message);
Log.WarnFormat("Failed to upload by using method addBase64EncodedAttachmentsToIssue, error was {0}", ex1.Message);
try {
LOG.Warn("Trying addAttachmentsToIssue instead");
jira.addAttachmentsToIssue(credentials, issueKey, new string[] { filename }, (sbyte[])(Array)attachment.ToByteArray());
Log.Warn("Trying addAttachmentsToIssue instead");
_jira.addAttachmentsToIssue(_credentials, issueKey, new[] { filename }, (sbyte[])(Array)attachment.ToByteArray());
} catch (Exception ex2) {
LOG.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message);
Log.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message);
throw;
}
}
}
public void addComment(string issueKey, string commentString) {
RemoteComment comment = new RemoteComment();
comment.body = commentString;
checkCredentials();
jira.addComment(credentials, issueKey, comment);
public void AddComment(string issueKey, string commentString) {
RemoteComment comment = new RemoteComment
{
body = commentString
};
CheckCredentials();
_jira.addComment(_credentials, issueKey, comment);
}
private bool hasUser(string user) {
private bool HasUser(string user) {
if (user != null) {
return userCache.Contains(user);
return _userCache.Contains(user);
}
return false;
}
private string getUserFullName(string user) {
string fullname = null;
private string GetUserFullName(string user) {
string fullname;
if (user != null) {
if (userCache.Contains(user)) {
fullname = userCache[user].fullname;
if (_userCache.Contains(user)) {
fullname = _userCache[user].fullname;
} else {
checkCredentials();
RemoteUser remoteUser = jira.getUser(credentials, user);
userCache.Add(user, remoteUser);
CheckCredentials();
RemoteUser remoteUser = _jira.getUser(_credentials, user);
_userCache.Add(user, remoteUser);
fullname = remoteUser.fullname;
}
} else {

View file

@ -18,7 +18,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Windows.Forms;
using Greenshot.IniFile;
using GreenshotPlugin.Core;
@ -28,35 +28,22 @@ namespace GreenshotJiraPlugin {
/// </summary>
[IniSection("Jira", Description="Greenshot Jira Plugin configuration")]
public class JiraConfiguration : IniSection {
public const string DEFAULT_PREFIX = "http://";
private const string DEFAULT_URL = DEFAULT_PREFIX + "jira" + Jira.JiraConnector.DEFAULT_POSTFIX;
public const string DefaultPrefix = "http://";
private const string DefaultUrl = DefaultPrefix + "jira" + JiraConnector.DefaultPostfix;
[IniProperty("Url", Description="Url to Jira system, including wsdl.", DefaultValue=DEFAULT_URL)]
public string Url;
[IniProperty("Url", Description="Url to Jira system, including wsdl.", DefaultValue=DefaultUrl)]
public string Url { get; set; }
[IniProperty("Timeout", Description="Session timeout in minutes", DefaultValue="30")]
public int Timeout;
public int Timeout { get; set; }
[IniProperty("LastUsedJira", Description="Last used Jira")]
public string LastUsedJira;
public string LastUsedJira { get; set; }
[IniProperty("UploadFormat", Description="What file type to use for uploading", DefaultValue="png")]
public OutputFormat UploadFormat;
public OutputFormat UploadFormat { get; set; }
[IniProperty("UploadJpegQuality", Description="JPEG file save quality in %.", DefaultValue="80")]
public int UploadJpegQuality;
public int UploadJpegQuality { get; set; }
[IniProperty("UploadReduceColors", Description="Reduce color amount of the uploaded image to 256", DefaultValue="False")]
public bool UploadReduceColors;
/// <summary>
/// A form for username/password
/// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns>
public bool ShowConfigDialog() {
SettingsForm settingsForm = new SettingsForm(this);
DialogResult result = settingsForm.ShowDialog();
if (result == DialogResult.OK) {
return true;
}
return false;
}
public bool UploadReduceColors { get; set; }
}
}

View file

@ -18,6 +18,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -26,27 +27,27 @@ using System.IO;
using System.Windows.Forms;
using Greenshot.IniFile;
using Greenshot.Plugin;
using GreenshotJiraPlugin.Forms;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
using Jira;
namespace GreenshotJiraPlugin {
/// <summary>
/// Description of JiraDestination.
/// </summary>
public class JiraDestination : AbstractDestination {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraDestination));
private static readonly JiraConfiguration config = IniConfig.GetIniSection<JiraConfiguration>();
private readonly JiraPlugin jiraPlugin = null;
private readonly JiraIssue jira = null;
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(JiraDestination));
private static readonly JiraConfiguration Config = IniConfig.GetIniSection<JiraConfiguration>();
private readonly JiraPlugin _jiraPlugin;
private readonly JiraIssue _jira;
public JiraDestination(JiraPlugin jiraPlugin) {
this.jiraPlugin = jiraPlugin;
_jiraPlugin = jiraPlugin;
}
public JiraDestination(JiraPlugin jiraPlugin, JiraIssue jira) {
this.jiraPlugin = jiraPlugin;
this.jira = jira;
_jiraPlugin = jiraPlugin;
_jira = jira;
}
public override string Designation {
@ -60,18 +61,18 @@ namespace GreenshotJiraPlugin {
}
public override string Description {
get {
if (jira == null) {
get
{
if (_jira == null) {
return Language.GetString("jira", LangKey.upload_menu_item);
} else {
return FormatUpload(jira);
}
return FormatUpload(_jira);
}
}
public override bool isActive {
get {
return base.isActive && !string.IsNullOrEmpty(config.Url);
return base.isActive && !string.IsNullOrEmpty(Config.Url);
}
}
@ -82,36 +83,36 @@ namespace GreenshotJiraPlugin {
}
public override Image DisplayIcon {
get {
ComponentResourceManager resources = new ComponentResourceManager(typeof(JiraPlugin));
var resources = new ComponentResourceManager(typeof(JiraPlugin));
return (Image)resources.GetObject("Jira");
}
}
public override IEnumerable<IDestination> DynamicDestinations() {
if (JiraPlugin.Instance.CurrentJiraConnector == null || !JiraPlugin.Instance.CurrentJiraConnector.isLoggedIn) {
if (JiraPlugin.Instance.CurrentJiraConnector == null || !JiraPlugin.Instance.CurrentJiraConnector.IsLoggedIn) {
yield break;
}
List<JiraIssue> issues = JiraUtils.GetCurrentJiras();
if (issues != null) {
foreach(JiraIssue jiraIssue in issues) {
yield return new JiraDestination(jiraPlugin, jiraIssue);
foreach(var jiraIssue in issues) {
yield return new JiraDestination(_jiraPlugin, jiraIssue);
}
}
}
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surfaceToUpload, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
if (jira != null) {
string filename = Path.GetFileName(FilenameHelper.GetFilename(Config.UploadFormat, captureDetails));
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(Config.UploadFormat, Config.UploadJpegQuality, Config.UploadReduceColors);
if (_jira != null) {
try {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
delegate() {
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
delegate {
_jiraPlugin.JiraConnector.AddAttachment(_jira.Key, filename, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
}
);
LOG.Debug("Uploaded to Jira.");
Log.Debug("Uploaded to Jira.");
exportInformation.ExportMade = true;
// TODO: This can't work:
exportInformation.Uri = surfaceToUpload.UploadURL;
@ -119,19 +120,19 @@ namespace GreenshotJiraPlugin {
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
}
} else {
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
if (jiraPlugin.JiraConnector.isLoggedIn) {
jiraForm.setFilename(filename);
JiraForm jiraForm = new JiraForm(_jiraPlugin.JiraConnector);
if (_jiraPlugin.JiraConnector.IsLoggedIn) {
jiraForm.SetFilename(filename);
DialogResult result = jiraForm.ShowDialog();
if (result == DialogResult.OK) {
try {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
delegate() {
jiraForm.upload(new SurfaceContainer(surfaceToUpload, outputSettings, filename));
delegate {
jiraForm.Upload(new SurfaceContainer(surfaceToUpload, outputSettings, filename));
}
);
LOG.Debug("Uploaded to Jira.");
Log.Debug("Uploaded to Jira.");
exportInformation.ExportMade = true;
// TODO: This can't work:
exportInformation.Uri = surfaceToUpload.UploadURL;

View file

@ -23,21 +23,19 @@ using System.ComponentModel;
using System.Windows.Forms;
using Greenshot.IniFile;
using Greenshot.Plugin;
using Jira;
using System;
using GreenshotJiraPlugin.Forms;
namespace GreenshotJiraPlugin {
/// <summary>
/// This is the JiraPlugin base code
/// </summary>
public class JiraPlugin : IGreenshotPlugin {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraPlugin));
private PluginAttribute jiraPluginAttributes;
private IGreenshotHost host;
private JiraConnector jiraConnector = null;
private JiraConfiguration config = null;
private ComponentResourceManager resources;
private static JiraPlugin instance = null;
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(JiraPlugin));
private PluginAttribute _jiraPluginAttributes;
private JiraConnector _jiraConnector;
private JiraConfiguration _config;
private static JiraPlugin _instance;
public void Dispose() {
Dispose(true);
@ -46,26 +44,26 @@ namespace GreenshotJiraPlugin {
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (jiraConnector != null) {
jiraConnector.Dispose();
jiraConnector = null;
if (_jiraConnector != null) {
_jiraConnector.Dispose();
_jiraConnector = null;
}
}
}
public static JiraPlugin Instance {
get {
return instance;
return _instance;
}
}
public JiraPlugin() {
instance = this;
_instance = this;
}
public PluginAttribute JiraPluginAttributes {
get {
return jiraPluginAttributes;
return _jiraPluginAttributes;
}
}
@ -80,40 +78,38 @@ namespace GreenshotJiraPlugin {
//Needed for a fail-fast
public JiraConnector CurrentJiraConnector {
get {
return jiraConnector;
return _jiraConnector;
}
}
public JiraConnector JiraConnector {
get {
if (jiraConnector == null) {
jiraConnector = new JiraConnector(true);
if (_jiraConnector == null) {
_jiraConnector = new JiraConnector(true);
}
return jiraConnector;
return _jiraConnector;
}
}
/// <summary>
/// Implementation of the IGreenshotPlugin.Initialize
/// </summary>
/// <param name="host">Use the IGreenshotPluginHost interface to register events</param>
/// <param name="captureHost">Use the ICaptureHost interface to register in the MainContextMenu</param>
/// <param name="pluginAttribute">My own attributes</param>
/// <param name="pluginHost">Use the IGreenshotPluginHost interface to register events</param>
/// <param name="myAttributes">My own attributes</param>
/// <returns>true if plugin is initialized, false if not (doesn't show)</returns>
public virtual bool Initialize(IGreenshotHost pluginHost, PluginAttribute myAttributes) {
host = (IGreenshotHost)pluginHost;
jiraPluginAttributes = myAttributes;
_jiraPluginAttributes = myAttributes;
// Register configuration (don't need the configuration itself)
config = IniConfig.GetIniSection<JiraConfiguration>();
resources = new ComponentResourceManager(typeof(JiraPlugin));
_config = IniConfig.GetIniSection<JiraConfiguration>();
new ComponentResourceManager(typeof(JiraPlugin));
return true;
}
public virtual void Shutdown() {
LOG.Debug("Jira Plugin shutdown.");
if (jiraConnector != null) {
jiraConnector.logout();
Log.Debug("Jira Plugin shutdown.");
if (_jiraConnector != null) {
_jiraConnector.Logout();
}
}
@ -121,25 +117,40 @@ namespace GreenshotJiraPlugin {
/// Implementation of the IPlugin.Configure
/// </summary>
public virtual void Configure() {
string url = config.Url;
if (config.ShowConfigDialog()) {
string url = _config.Url;
if (ShowConfigDialog()) {
// check for re-login
if (jiraConnector != null && jiraConnector.isLoggedIn && !string.IsNullOrEmpty(url)) {
if (!url.Equals(config.Url)) {
jiraConnector.logout();
jiraConnector.login();
if (_jiraConnector != null && _jiraConnector.IsLoggedIn && !string.IsNullOrEmpty(url)) {
if (!url.Equals(_config.Url)) {
_jiraConnector.Logout();
_jiraConnector.Login();
}
}
}
}
/// <summary>
/// A form for username/password
/// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns>
private bool ShowConfigDialog()
{
var settingsForm = new SettingsForm();
var result = settingsForm.ShowDialog();
if (result == DialogResult.OK)
{
return true;
}
return false;
}
/// <summary>
/// This will be called when Greenshot is shutting down
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Closing(object sender, FormClosingEventArgs e) {
LOG.Debug("Application closing, calling logout of jira!");
Log.Debug("Application closing, calling logout of jira!");
Shutdown();
}
}

View file

@ -22,15 +22,14 @@ using System.Collections.Generic;
using System.Text.RegularExpressions;
using Greenshot.IniFile;
using GreenshotPlugin.Core;
using Jira;
namespace GreenshotJiraPlugin {
/// <summary>
/// Description of JiraUtils.
/// </summary>
public class JiraUtils {
private static readonly Regex JIRA_KEY_REGEX = new Regex(@"/browse/([A-Z0-9]+\-[0-9]+)");
private static readonly JiraConfiguration config = IniConfig.GetIniSection<JiraConfiguration>();
public static class JiraUtils {
private static readonly Regex JiraKeyRegex = new Regex(@"/browse/([A-Z0-9]+\-[0-9]+)");
private static readonly JiraConfiguration Config = IniConfig.GetIniSection<JiraConfiguration>();
public static List<JiraIssue> GetCurrentJiras() {
// Make sure we suppress the login
@ -39,24 +38,30 @@ namespace GreenshotJiraPlugin {
if (url == null) {
continue;
}
MatchCollection jiraKeyMatch = JIRA_KEY_REGEX.Matches(url);
if (jiraKeyMatch != null && jiraKeyMatch.Count > 0) {
MatchCollection jiraKeyMatch = JiraKeyRegex.Matches(url);
if (jiraKeyMatch.Count > 0) {
string jiraKey = jiraKeyMatch[0].Groups[1].Value;
jirakeys.Add(jiraKey);
}
}
if (!string.IsNullOrEmpty(config.LastUsedJira) && !jirakeys.Contains(config.LastUsedJira)) {
jirakeys.Add(config.LastUsedJira);
if (!string.IsNullOrEmpty(Config.LastUsedJira) && !jirakeys.Contains(Config.LastUsedJira)) {
jirakeys.Add(Config.LastUsedJira);
}
if (jirakeys.Count > 0) {
List<JiraIssue> jiraIssues = new List<JiraIssue>();
foreach(string jiraKey in jirakeys) {
try {
JiraIssue issue = JiraPlugin.Instance.JiraConnector.getIssue(jiraKey);
if (issue != null) {
try
{
JiraIssue issue = JiraPlugin.Instance.JiraConnector.GetIssue(jiraKey);
if (issue != null)
{
jiraIssues.Add(issue);
}
} catch {}
}
catch
{
}
}
if (jiraIssues.Count > 0) {
return jiraIssues;

View file

@ -35,7 +35,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The PluginAttribute describes the "entryType" and if the plugin is configurable
[assembly: PluginAttribute("GreenshotJiraPlugin.JiraPlugin", true)]
[assembly: Plugin("GreenshotJiraPlugin.JiraPlugin", true)]
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.

File diff suppressed because it is too large Load diff