This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.cs
216 lines (193 loc) · 8.01 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
*
* TortoiseMantis Copyright 2010 Andreas Stieger <[email protected]>
*
* This file is part of TortoiseMantis.
*
* TortoiseMantis 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 3 of the License, or
* (at your option) any later version.
*
* TortoiseMantis 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 <http://www.gnu.org/licenses/>.
*
**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ServiceModel;
using System.Windows.Forms;
using TortoiseMantis.MantisConnectReference;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Runtime.Remoting.Messaging;
using Interop.BugTraqProvider;
namespace TortoiseMantis
{
[ComVisible(true),
Guid("F25B6B8F-1BA0-4BCA-A809-0C0B6F4A0CED"),
ClassInterface(ClassInterfaceType.None)]
public sealed class Plugin : IBugTraqProvider
{
private IIssuesDisplay form;
private MantisConnectPortTypeClient client;
private ConnectionSettings cs;
private bool connectionErrorReported;
public bool ValidateParameters(IntPtr hParentWnd, string parameters)
{
ConnectionSettings cs = new ConnectionSettings(parameters);
if (!cs.isValid())
{
MessageBox.Show("invalid parameters\nexample:\nurl:http://bugserver/bugs/api/soap/mantisconnect.php username:foouser password:foopass project:fooproject",
"invalid parameters", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public string GetLinkText(IntPtr hParentWnd, string parameters)
{
// TODO
return "Choose Issue";
}
public string GetCommitMessage(IntPtr hParentWnd, string parameters, string commonRoot, string[] pathList, string originalMessage)
{
connectionErrorReported = false;
cs = new ConnectionSettings(parameters);
IssuesForm form = new IssuesForm(this, cs);
this.form = form;
// WTF does this take so long?
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(cs.URL);
client = new MantisConnectPortTypeClient(binding, endpoint);
client.mc_versionCompleted += new EventHandler<mc_versionCompletedEventArgs>(client_mc_versionCompleted);
client.mc_projects_get_user_accessibleCompleted += new EventHandler<mc_projects_get_user_accessibleCompletedEventArgs>(client_mc_projects_get_user_accessibleCompleted);
client.mc_enum_statusCompleted += new EventHandler<mc_enum_statusCompletedEventArgs>(client_mc_enum_statusCompleted);
client.mc_projects_get_user_accessibleAsync(cs.Username, cs.Password);
client.mc_enum_statusAsync(cs.Username, cs.Password);
if (form.ShowDialog() == DialogResult.OK)
{
IssueHeaderData issue = form.GetSelectedIssue();
if (issue != null)
{
String retMessage = String.Format("BUGFIX: {0}\nissue {1}\n", issue.summary, issue.id);
return retMessage;
}
}
return originalMessage;
}
private void ReportConnectionErrorOnce(String message)
{
if (!connectionErrorReported)
{
connectionErrorReported = true;
MessageBox.Show(message, "communication error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void client_mc_versionCompleted(object sender, mc_versionCompletedEventArgs e)
{
if (e.Error == null)
{
OnStatusUpdated(String.Format("connected to MantisBT v{0}", e.Result));
}
else
{
ReportConnectionErrorOnce(e.Error.Message);
}
}
private void client_mc_enum_statusCompleted(object sender, mc_enum_statusCompletedEventArgs e)
{
if (e.Error == null)
{
OnStatusUpdated(String.Format("loaded {0} status mappings", e.Result.Length));
form.SetStatusMappings(e.Result);
}
else
{
ReportConnectionErrorOnce(e.Error.Message);
}
}
private void client_mc_projects_get_user_accessibleCompleted(object sender, mc_projects_get_user_accessibleCompletedEventArgs e)
{
if (e.Error == null)
{
foreach (ProjectData projectData in e.Result)
{
if (projectData.name == cs.Project)
{
OnStatusUpdated(String.Format("found project \"{0}\" ({1})", projectData.name, projectData.id));
client.mc_project_get_usersCompleted += new EventHandler<mc_project_get_usersCompletedEventArgs>(client_mc_project_get_usersCompleted);
client.mc_project_get_issue_headersCompleted += new EventHandler<mc_project_get_issue_headersCompletedEventArgs>(client_mc_project_get_issue_headersCompleted);
client.mc_project_get_usersAsync(cs.Username, cs.Password, projectData.id, "0");
client.mc_project_get_issue_headersAsync(cs.Username, cs.Password, projectData.id, "0", "0");
return;
}
}
}
else
{
ReportConnectionErrorOnce(e.Error.Message);
}
}
private void client_mc_project_get_usersCompleted(object sender, mc_project_get_usersCompletedEventArgs e)
{
if (e.Error == null)
{
OnStatusUpdated(String.Format("read {0} usernames", e.Result.Length));
form.SetAccountData(e.Result);
OnAccountDataLoaded();
}
else
{
ReportConnectionErrorOnce(e.Error.Message);
}
}
private void client_mc_project_get_issue_headersCompleted(object sender, mc_project_get_issue_headersCompletedEventArgs e)
{
if (e.Error == null)
{
OnStatusUpdated(String.Format("found {0} issues", e.Result.Length));
form.SetIssueHeaderData(e.Result);
OnIssuesLoaded();
}
else
{
ReportConnectionErrorOnce(e.Error.Message);
}
}
public delegate void StatusUpdatedHandler(string status);
public event StatusUpdatedHandler StatusUpdated;
private void OnStatusUpdated(string status)
{
if (StatusUpdated != null)
{
StatusUpdated(status);
}
}
public delegate void AccountDataLoadedHandler();
public event AccountDataLoadedHandler AccountDataLoaded;
private void OnAccountDataLoaded()
{
if (AccountDataLoaded != null)
{
AccountDataLoaded();
}
}
public delegate void IssuesLoadedHandler();
public event IssuesLoadedHandler IssuesLoaded;
private void OnIssuesLoaded()
{
if (IssuesLoaded != null)
{
IssuesLoaded();
}
}
}
}