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.Diagnostics;
5 using System.Linq;
6 using System.Threading;
7 using System.Windows.Forms;
8 using Korero.Chat;
9 using Korero.Communication;
10 using Korero.Economy;
11 using Korero.Properties;
12 using Korero.Serialization;
13 using Korero.Utilities;
14 using Serilog;
15  
16 namespace Korero.Profile
17 {
18 public partial class GroupProfileForm : Form
19 {
20 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
21  
22 private readonly CancellationToken _cancellationToken;
23  
24 private readonly CancellationTokenSource _cancellationTokenSource;
25  
26 private readonly string _groupName;
27  
28 private readonly MainForm _mainForm;
29  
30 private readonly MqttCommunication _mqttCommunication;
31  
32 private AvatarProfileForm _avatarProfileForm;
33  
34 private PaymentForm _paymentForm;
35  
36 #endregion
37  
38 #region Constructors, Destructors and Finalizers
39  
40 public GroupProfileForm()
41 {
42 InitializeComponent();
43  
44 _cancellationTokenSource = new CancellationTokenSource();
45 _cancellationToken = _cancellationTokenSource.Token;
46 }
47  
48 public GroupProfileForm(MainForm mainForm, string groupName, MqttCommunication mqttCommunication) : this()
49 {
50 _mainForm = mainForm;
51 _groupName = groupName;
52 _mqttCommunication = mqttCommunication;
53 }
54  
55 /// <summary>
56 /// Clean up any resources being used.
57 /// </summary>
58 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
59 protected override void Dispose(bool disposing)
60 {
61 if (disposing && components != null)
62 {
63 components.Dispose();
64 }
65  
66 base.Dispose(disposing);
67 }
68  
69 #endregion
70  
71 #region Event Handlers
2 office 72 private void GroupProfileForm_Load(object sender, EventArgs e)
73 {
74 Utilities.WindowState.FormTracker.Track(this);
75 }
1 office 76 private async void GroupProfileForm_Shown(object sender, EventArgs e)
77 {
78 try
79 {
80 var data = new Dictionary<string, string>
81 {
82 {"command", "getgroupdata"},
83 {"group", Settings.Default.Group},
84 {"password", Settings.Default.Password},
85 {"target", _groupName},
86 {
87 "data",
88 new CSV(new[]
89 {"Charter", "InsigniaID"})
90 }
91 };
92  
93 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
94  
95 if (callback == null || !callback.Success)
96 {
97 if (callback != null)
98 {
99 Log.Warning("Command {Command} has failed with {Error}.",
100 callback.Command, callback.Error);
101 }
102  
103 return;
104 }
105  
106 var insigniaId = callback["InsigniaID"].FirstOrDefault();
107  
108 if (!string.IsNullOrEmpty(insigniaId) &&
109 Guid.TryParse(insigniaId, out var insigniaImageId) &&
110 insigniaImageId != Guid.Empty)
111 {
112 var insigniaImage =
113 await Communication.Utilities.DownloadImage(insigniaImageId, _mainForm.MqttCommunication,
114 _cancellationToken);
115 if (insigniaImage != null)
116 {
117 pictureBox1.InvokeIfRequired(pictureBox =>
118 {
119 if (pictureBox.Image != null)
120 {
121 pictureBox.Image.Dispose();
122 pictureBox.Image = null;
123 }
124  
125 pictureBox.Image = insigniaImage;
126 });
127 }
128 }
129 else
130 {
131 pictureBox1.InvokeIfRequired(pictureBox =>
132 {
133 if (pictureBox.Image != null)
134 {
135 pictureBox.Image.Dispose();
136 pictureBox.Image = null;
137 }
138 });
139 }
140  
141 var charterText = callback["Charter"].FirstOrDefault();
142 if (!string.IsNullOrEmpty(charterText))
143 {
144 richTextBox1.Text = charterText;
145 }
146  
147 data = new Dictionary<string, string>
148 {
149 {"command", "getgroupmembersdata"},
150 {"group", Settings.Default.Group},
151 {"password", Settings.Default.Password},
152 {"target", _groupName},
153 {
154 "data",
155 new CSV(new[]
156 {"ID", "Title", "OnlineStatus"})
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 groupMembersData = callback.Data;
174  
175 data = new Dictionary<string, string>
176 {
177 {"command", "batchavatarkeytoname"},
178 {"group", Settings.Default.Group},
179 {"password", Settings.Default.Password},
180 {
181 "avatars",
182 new CSV(callback["ID"])
183 }
184 };
185  
186 callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
187  
188 if (callback == null || !callback.Success)
189 {
190 if (callback != null)
191 {
192 Log.Warning("Command {Command} has failed with {Error}.",
193 callback.Command, callback.Error);
194 }
195  
196 return;
197 }
198  
199 var nameToKey = new Dictionary<Guid, string>();
200 foreach (var avatar in CsvStride(callback.Data, 2))
201 {
202 if (!Guid.TryParse(avatar[0], out var id))
203 {
204 continue;
205 }
206  
207 nameToKey.Add(id, avatar[1]);
208 }
209  
210 foreach (var member in CsvStride(groupMembersData, 6))
211 {
212 var index = Array.IndexOf(member, "ID");
213  
214 if (!Guid.TryParse(member[index + 1], out var id))
215 {
216 continue;
217 }
218  
219 if (!nameToKey.TryGetValue(id, out var name))
220 {
221 continue;
222 }
223  
224 index = Array.IndexOf(member, "Title");
225 var title = member[index + 1];
226 index = Array.IndexOf(member, "OnlineStatus");
227 var status = member[index + 1];
228  
229 dataGridView1.InvokeIfRequired(dataGridView =>
230 {
231 index = dataGridView.Rows.Add();
232 dataGridView.Rows[index].Cells[0].Value = name;
233 dataGridView.Rows[index].Cells[1].Value = title;
234 dataGridView.Rows[index].Cells[2].Value = status;
235 });
236 }
237 }
238 catch (Exception ex)
239 {
240 Log.Warning(ex, "Error while retrieving group data.");
241 }
242 }
243  
244 private void GroupProfileForm_FormClosing(object sender, FormClosingEventArgs e)
245 {
246 _cancellationTokenSource.Cancel();
247  
248 if (pictureBox1.Image != null)
249 {
250 pictureBox1.Image.Dispose();
251 pictureBox1.Image = null;
252 }
253 }
254  
255 private void ProfileToolStripMenuItem_Click(object sender, EventArgs e)
256 {
257 if (_avatarProfileForm != null)
258 {
259 return;
260 }
261  
262 var index = dataGridView1.CurrentCell.RowIndex;
263 if (index == -1)
264 {
265 return;
266 }
267  
268 var name = (string) dataGridView1.Rows[index].Cells[0].Value;
269  
270 _avatarProfileForm = new AvatarProfileForm(_mainForm, name, _mqttCommunication);
271 _avatarProfileForm.Closing += AvatarProfileForm_Closing;
272 _avatarProfileForm.Show();
273 }
274  
275 private void AvatarProfileForm_Closing(object sender, CancelEventArgs e)
276 {
277 if (_avatarProfileForm == null)
278 {
279 return;
280 }
281  
282 _avatarProfileForm.Closing -= AvatarProfileForm_Closing;
283 _avatarProfileForm.Dispose();
284 _avatarProfileForm = null;
285 }
286  
287 private void RichTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
288 {
289 Process.Start(e.LinkText);
290 }
291  
292 private void ChatToolStripMenuItem_Click(object sender, EventArgs e)
293 {
294 var index = dataGridView1.CurrentCell.RowIndex;
295 if (index == -1)
296 {
297 return;
298 }
299  
300 var name = (string) dataGridView1.Rows[index].Cells[0].Value;
301  
302 if (_mainForm.ChatForm != null)
303 {
304 _mainForm.ChatForm.CreateOrSelect(name);
305 return;
306 }
307  
308 _mainForm.ChatForm = new ChatForm(_mainForm, _mqttCommunication);
309 _mainForm.ChatForm.Closing += ChatForm_Closing;
310 _mainForm.ChatForm.Show();
311 _mainForm.ChatForm.CreateOrSelect(name);
312 }
313  
314 private void ChatForm_Closing(object sender, CancelEventArgs e)
315 {
316 if (_mainForm.ChatForm == null)
317 {
318 return;
319 }
320  
321 _mainForm.ChatForm.Closing -= ChatForm_Closing;
322 _mainForm.ChatForm.Dispose();
323 _mainForm.ChatForm = null;
324 }
325  
326 private void PayToolStripMenuItem_Click(object sender, EventArgs e)
327 {
328 if (_paymentForm != null)
329 {
330 return;
331 }
332  
333 _paymentForm = new PaymentForm(_groupName, _mainForm, _mqttCommunication);
334 _paymentForm.Closing += PaymentForm_Closing;
335 _paymentForm.Show();
336 }
337  
338 private void PaymentForm_Closing(object sender, CancelEventArgs e)
339 {
340 if (_paymentForm == null)
341 {
342 return;
343 }
344  
345 _paymentForm.Closing -= PaymentForm_Closing;
346 _paymentForm.Dispose();
347 _paymentForm = null;
348 }
349  
350 #endregion
351  
352 #region Private Methods
353  
354 private static IEnumerable<string[]> CsvStride(string input, int stride)
355 {
356 var i = 0;
357 return new CSV(input).Select(s => new {s, num = i++})
358 .GroupBy(t => t.num / stride, t => t.s)
359 .Select(g => g.ToArray());
360 }
361  
362 #endregion
2 office 363  
364  
1 office 365 }
366 }