Korero – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Drawing; |
||
5 | using System.Linq; |
||
6 | using System.Threading; |
||
7 | using System.Windows.Forms; |
||
8 | using Korero.Chat; |
||
9 | using Korero.Communication; |
||
10 | using Korero.Properties; |
||
11 | using Korero.Serialization; |
||
12 | using Korero.Utilities; |
||
13 | using Serilog; |
||
14 | |||
15 | namespace Korero.Friendship |
||
16 | { |
||
17 | public partial class FriendshipForm : Form |
||
18 | { |
||
19 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
20 | |||
21 | private readonly CancellationToken _cancellationToken; |
||
22 | |||
23 | private readonly CancellationTokenSource _cancellationTokenSource; |
||
24 | |||
25 | private readonly MainForm _mainForm; |
||
26 | |||
27 | private readonly MqttCommunication _mqttCommunication; |
||
28 | |||
29 | #endregion |
||
30 | |||
31 | #region Constructors, Destructors and Finalizers |
||
32 | |||
33 | public FriendshipForm() |
||
34 | { |
||
35 | InitializeComponent(); |
||
36 | Utilities.WindowState.FormTracker.Track(this); |
||
37 | |||
38 | _cancellationTokenSource = new CancellationTokenSource(); |
||
39 | _cancellationToken = _cancellationTokenSource.Token; |
||
40 | } |
||
41 | |||
42 | public FriendshipForm(MainForm mainForm, MqttCommunication mqttCommunication) : this() |
||
43 | { |
||
44 | _mainForm = mainForm; |
||
45 | _mqttCommunication = mqttCommunication; |
||
46 | _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived; |
||
47 | } |
||
48 | |||
49 | /// <summary> |
||
50 | /// Clean up any resources being used. |
||
51 | /// </summary> |
||
52 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
53 | protected override void Dispose(bool disposing) |
||
54 | { |
||
55 | if (disposing && components != null) |
||
56 | { |
||
57 | components.Dispose(); |
||
58 | } |
||
59 | |||
60 | _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived; |
||
61 | base.Dispose(disposing); |
||
62 | } |
||
63 | |||
64 | #endregion |
||
65 | |||
66 | #region Event Handlers |
||
67 | |||
68 | private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e) |
||
69 | { |
||
70 | switch (e.Notification["notification"]) |
||
71 | { |
||
72 | case "friendship": |
||
73 | switch (e.Notification["action"]) |
||
74 | { |
||
75 | case "update": |
||
76 | var firstName = e.Notification["firstname"]; |
||
77 | var lastName = e.Notification["lastname"]; |
||
78 | |||
79 | var rights = new CSV(e.Notification["rights"]).ToArray(); |
||
80 | |||
81 | dataGridView1.InvokeIfRequired(dataGridView => |
||
82 | { |
||
83 | foreach (DataGridViewRow row in dataGridView.Rows) |
||
84 | { |
||
85 | if ((string) row.Cells[0].Value != $"{firstName} {lastName}") |
||
86 | { |
||
87 | continue; |
||
88 | } |
||
89 | |||
90 | row.Cells[4].Value = Array.IndexOf(rights, "CanSeeOnMap") != -1; |
||
91 | row.Cells[5].Value = Array.IndexOf(rights, "CanModifyObjects") != -1; |
||
92 | |||
93 | switch (e.Notification["status"]) |
||
94 | { |
||
95 | case "online": |
||
96 | row.DefaultCellStyle.BackColor = Settings.Default.FriendOnlineHighlight; |
||
97 | break; |
||
98 | case "offline": |
||
99 | row.DefaultCellStyle.BackColor = Color.Empty; |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | }); |
||
104 | |||
105 | |||
106 | break; |
||
107 | } |
||
108 | |||
109 | break; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | private async void FriendshipForm_Shown(object sender, EventArgs e) |
||
114 | { |
||
115 | var data = new Dictionary<string, string> |
||
116 | { |
||
117 | {"command", "getfriendslist"}, |
||
118 | {"group", Settings.Default.Group}, |
||
119 | {"password", Settings.Default.Password} |
||
120 | }; |
||
121 | |||
122 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
123 | |||
124 | if (callback == null || !callback.Success) |
||
125 | { |
||
126 | if (callback != null) |
||
127 | { |
||
128 | Log.Warning("Command {Command} has failed with {Error}.", |
||
129 | callback.Command, callback.Error); |
||
130 | } |
||
131 | |||
132 | return; |
||
133 | } |
||
134 | |||
135 | foreach (var friend in CsvStride(callback.Data, 2)) |
||
136 | { |
||
137 | var name = friend[0]; |
||
138 | var id = friend[1]; |
||
139 | |||
140 | data = new Dictionary<string, string> |
||
141 | { |
||
142 | {"command", "getfrienddata"}, |
||
143 | {"group", Settings.Default.Group}, |
||
144 | {"password", Settings.Default.Password}, |
||
145 | {"agent", id}, |
||
146 | { |
||
147 | "data", new CSV(new[] |
||
148 | { |
||
149 | "CanModifyMyObjects", |
||
150 | "CanModifyTheirObjects", |
||
151 | "CanSeeMeOnline", |
||
152 | "CanSeeMeOnMap", |
||
153 | "CanSeeThemOnline", |
||
154 | "CanSeeThemOnMap", |
||
155 | "IsOnline" |
||
156 | }) |
||
157 | } |
||
158 | }; |
||
159 | |||
160 | callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
161 | |||
162 | if (callback == null || !callback.Success) |
||
163 | { |
||
164 | if (callback != null) |
||
165 | { |
||
166 | Log.Warning("Command {Command} has failed with {Error}.", |
||
167 | callback.Command, callback.Error); |
||
168 | } |
||
169 | |||
170 | return; |
||
171 | } |
||
172 | |||
173 | var canModifyMyObjects = callback["CanModifyMyObjects"].FirstOrDefault(); |
||
174 | var canModifyTheirObjects = callback["CanModifyTheirObjects"].FirstOrDefault(); |
||
175 | var canSeeMeOnline = callback["CanSeeMeOnline"].FirstOrDefault(); |
||
176 | var canSeeMeOnMap = callback["CanSeeMeOnMap"].FirstOrDefault(); |
||
177 | var canSeeThemOnline = callback["CanSeeThemOnline"].FirstOrDefault(); |
||
178 | var canSeeThemOnMap = callback["CanSeeThemOnMap"].FirstOrDefault(); |
||
179 | var isOnline = callback["IsOnline"].FirstOrDefault(); |
||
180 | |||
181 | dataGridView1.InvokeIfRequired(dataGridView => |
||
182 | { |
||
183 | var index = dataGridView.Rows.Add(); |
||
184 | dataGridView.Rows[index].Cells[0].Value = name; |
||
185 | |||
186 | dataGridView.Rows[index].Cells[1].Value = canSeeMeOnline == "True"; |
||
187 | dataGridView.Rows[index].Cells[2].Value = canSeeMeOnMap == "True"; |
||
188 | dataGridView.Rows[index].Cells[3].Value = canModifyMyObjects == "True"; |
||
189 | |||
190 | dataGridView.Rows[index].Cells[4].Value = canSeeThemOnMap == "True"; |
||
191 | dataGridView.Rows[index].Cells[5].Value = canModifyTheirObjects == "True"; |
||
192 | |||
193 | if (isOnline == "True") |
||
194 | { |
||
195 | dataGridView.Rows[index].DefaultCellStyle.BackColor = Settings.Default.FriendOnlineHighlight; |
||
196 | } |
||
197 | }); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | private void ChatToolStripMenuItem_Click(object sender, EventArgs e) |
||
202 | { |
||
203 | var index = dataGridView1.CurrentCell.RowIndex; |
||
204 | if (index == -1) |
||
205 | { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | var selectedItem = (string) dataGridView1.Rows[index].Cells[0].Value; |
||
210 | |||
211 | if (_mainForm.ChatForm != null) |
||
212 | { |
||
213 | _mainForm.ChatForm.CreateOrSelect(selectedItem); |
||
214 | return; |
||
215 | } |
||
216 | |||
217 | _mainForm.ChatForm = new ChatForm(_mainForm, _mqttCommunication); |
||
218 | _mainForm.ChatForm.Closing += ChatForm_Closing; |
||
219 | _mainForm.ChatForm.Show(); |
||
220 | _mainForm.ChatForm.CreateOrSelect(selectedItem); |
||
221 | } |
||
222 | |||
223 | private void ChatForm_Closing(object sender, CancelEventArgs e) |
||
224 | { |
||
225 | if (_mainForm.ChatForm == null) |
||
226 | { |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | _mainForm.ChatForm.Closing -= ChatForm_Closing; |
||
231 | _mainForm.ChatForm.Dispose(); |
||
232 | _mainForm.ChatForm = null; |
||
233 | } |
||
234 | |||
235 | private async void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) |
||
236 | { |
||
237 | if (e.ColumnIndex < 1 || e.ColumnIndex > 3) |
||
238 | { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); |
||
243 | |||
244 | // Apply checkstate. |
||
245 | var value = (bool) dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; |
||
246 | dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !value; |
||
247 | |||
248 | var name = (string) dataGridView1.Rows[e.RowIndex].Cells[0].Value; |
||
249 | var firstName = name.Split(' ')[0]; |
||
250 | var lastName = name.Split(' ')[1]; |
||
251 | |||
252 | var rights = new List<string>(); |
||
253 | for (var i = 1; i < 4; ++i) |
||
254 | { |
||
255 | if (!(bool) dataGridView1.Rows[e.RowIndex].Cells[i].Value) |
||
256 | { |
||
257 | continue; |
||
258 | } |
||
259 | |||
260 | switch (i) |
||
261 | { |
||
262 | case 1: |
||
263 | rights.Add("CanSeeOnline"); |
||
264 | break; |
||
265 | case 2: |
||
266 | rights.Add("CanSeeOnMap"); |
||
267 | break; |
||
268 | case 3: |
||
269 | rights.Add("CanModifyObjects"); |
||
270 | break; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | var data = new Dictionary<string, string> |
||
275 | { |
||
276 | {"command", "grantfriendrights"}, |
||
277 | {"group", Settings.Default.Group}, |
||
278 | {"password", Settings.Default.Password}, |
||
279 | {"firstname", firstName}, |
||
280 | {"lastname", lastName}, |
||
281 | {"rights", new CSV(rights)} |
||
282 | }; |
||
283 | |||
284 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
285 | |||
286 | if (callback == null || !callback.Success) |
||
287 | { |
||
288 | if (callback != null) |
||
289 | { |
||
290 | Log.Warning("Command {Command} has failed with {Error}.", |
||
291 | callback.Command, callback.Error); |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | private void FriendshipForm_FormClosing(object sender, FormClosingEventArgs e) |
||
297 | { |
||
298 | _cancellationTokenSource.Cancel(); |
||
299 | } |
||
300 | |||
301 | #endregion |
||
302 | |||
303 | #region Private Methods |
||
304 | |||
305 | private static IEnumerable<string[]> CsvStride(string input, int stride) |
||
306 | { |
||
307 | var i = 0; |
||
308 | return new CSV(input).Select(s => new {s, num = i++}) |
||
309 | .GroupBy(t => t.num / stride, t => t.s) |
||
310 | .Select(g => g.ToArray()); |
||
311 | } |
||
312 | |||
313 | #endregion |
||
314 | } |
||
315 | } |