Korero – Blame information for rev 2

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