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.Communication;
9 using Korero.Economy;
10 using Korero.Properties;
11 using Korero.Serialization;
12 using Korero.Utilities;
13 using Serilog;
14  
15 namespace Korero.Profile
16 {
17 public partial class AvatarProfileForm : 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 string _firstName;
26  
27 private readonly string _lastName;
28  
29 private readonly MainForm _mainForm;
30  
31 private readonly MqttCommunication _mqttCommunication;
32  
33 private GroupProfileForm _groupProfileForm;
34  
35 private PaymentForm _paymentForm;
36  
37 #endregion
38  
39 #region Constructors, Destructors and Finalizers
40  
41 public AvatarProfileForm()
42 {
43 InitializeComponent();
44  
45 _cancellationTokenSource = new CancellationTokenSource();
46 _cancellationToken = _cancellationTokenSource.Token;
47 }
48  
49 public AvatarProfileForm(MainForm mainForm, string firstName, string lastName,
50 MqttCommunication mqttCommunication) : this()
51 {
52 _mainForm = mainForm;
53 _firstName = firstName;
54 _lastName = lastName;
55  
56 _mqttCommunication = mqttCommunication;
57 }
58  
59 public AvatarProfileForm(MainForm mainForm, string fullName, MqttCommunication mqttCommunication) : this()
60 {
61 _mainForm = mainForm;
62  
63 var split = fullName.Split(' ');
64 _firstName = split[0];
65 _lastName = split[1];
66  
67 _mqttCommunication = mqttCommunication;
68 }
69  
70 /// <summary>
71 /// Clean up any resources being used.
72 /// </summary>
73 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
74 protected override void Dispose(bool disposing)
75 {
76 if (disposing && components != null)
77 {
78 components.Dispose();
79 }
80  
81 base.Dispose(disposing);
82 }
83  
84 #endregion
85  
86 #region Event Handlers
2 office 87 private void AvatarProfileForm_Load(object sender, EventArgs e)
88 {
89 Utilities.WindowState.FormTracker.Track(this);
90 }
1 office 91 private async void AvatarProfileForm_Shown(object sender, EventArgs e)
92 {
93 secondLifeNameTextBox.Text = $"{_firstName} {_lastName}";
94  
95 var data = new Dictionary<string, string>
96 {
97 {"command", "getprofiledata"},
98 {"group", Settings.Default.Group},
99 {"password", Settings.Default.Password},
100 {"entity", "avatar"},
101 {"firstname", _firstName},
102 {"lastname", _lastName},
103 {
104 "data",
105 new CSV(new[]
106 {"BornOn", "AboutText", "ProfileImage", "GroupName", "FirstLifeText", "FirstLifeImage"})
107 }
108 };
109  
110 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
111  
112 if (callback == null || !callback.Success)
113 {
114 if (callback != null)
115 {
116 Log.Warning("Command {Command} has failed with {Error}.",
117 callback.Command, callback.Error);
118 }
119  
120 return;
121 }
122  
123 var bornOnText = callback["BornOn"].FirstOrDefault();
124  
125 if (!string.IsNullOrEmpty(bornOnText) && DateTime.TryParse(bornOnText, out var bornOnDateTime))
126 {
127 secondLifeBornTextBox.Text = $"{bornOnText} ({(DateTime.Now - bornOnDateTime.Date).Days} days)";
128 }
129  
130 foreach (var group in callback["GroupName"])
131 {
132 if (string.IsNullOrEmpty(group))
133 {
134 continue;
135 }
136  
137 if (!secondLifeGroupsListBox.Items.Contains(group))
138 {
139 secondLifeGroupsListBox.Items.Add(group);
140 }
141 }
142  
143 var aboutText = callback["AboutText"].FirstOrDefault();
144  
145 if (!string.IsNullOrEmpty(aboutText))
146 {
147 secondLifeAboutRichTextBox.Text = aboutText;
148 }
149  
150 var profileImageText = callback["ProfileImage"].FirstOrDefault();
151  
152 if (!string.IsNullOrEmpty(profileImageText) &&
153 Guid.TryParse(profileImageText, out var profileImageId) &&
154 profileImageId != Guid.Empty)
155 {
156 var profileImage =
157 await Communication.Utilities.DownloadImage(profileImageId, _mainForm.MqttCommunication,
158 _cancellationToken);
159 if (profileImage != null)
160 {
161 secondLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
162 {
163 if (pictureBox.Image != null)
164 {
165 pictureBox.Image.Dispose();
166 pictureBox.Image = null;
167 }
168  
169 pictureBox.Image = profileImage;
170 });
171 }
172 }
173 else
174 {
175 secondLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
176 {
177 if (pictureBox.Image != null)
178 {
179 pictureBox.Image.Dispose();
180 pictureBox.Image = null;
181 }
182 });
183 }
184  
185 var firstLifeText = callback["FirstLifeText"].FirstOrDefault();
186 if (!string.IsNullOrEmpty(firstLifeText))
187 {
188 firstLifeInfoRichTextBox.Text = firstLifeText;
189 }
190  
191 var firstLifeImageText = callback["FirstLifeImage"].FirstOrDefault();
192 if (!string.IsNullOrEmpty(firstLifeImageText) &&
193 Guid.TryParse(firstLifeImageText, out var firstLifeImageId) &&
194 firstLifeImageId != Guid.Empty)
195 {
196 var firstLifeImage =
197 await Communication.Utilities.DownloadImage(firstLifeImageId, _mainForm.MqttCommunication,
198 _cancellationToken);
199 if (firstLifeImage != null)
200 {
201 firstLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
202 {
203 if (pictureBox.Image != null)
204 {
205 pictureBox.Image.Dispose();
206 pictureBox.Image = null;
207 }
208  
209 pictureBox.Image = firstLifeImage;
210 });
211 }
212 }
213 else
214 {
215 firstLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
216 {
217 if (pictureBox.Image != null)
218 {
219 pictureBox.Image.Dispose();
220 pictureBox.Image = null;
221 }
222 });
223 }
224 }
225  
226 private void SecondLifeAboutRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
227 {
228 Process.Start(e.LinkText);
229 }
230  
231 private void FirstLifeInfoRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
232 {
233 Process.Start(e.LinkText);
234 }
235  
236 private void AvatarProfileForm_FormClosing(object sender, FormClosingEventArgs e)
237 {
238 _cancellationTokenSource.Cancel();
239  
240 if (secondLifeProfilePictureBox.Image != null)
241 {
242 secondLifeProfilePictureBox.Image.Dispose();
243 secondLifeProfilePictureBox.Image = null;
244 }
245  
246 if (firstLifeProfilePictureBox.Image != null)
247 {
248 firstLifeProfilePictureBox.Image.Dispose();
249 firstLifeProfilePictureBox.Image = null;
250 }
251 }
252  
253 private void ToolStripMenuItem1_Click(object sender, EventArgs e)
254 {
255 var index = secondLifeGroupsListBox.SelectedIndex;
256 if (index == -1)
257 {
258 return;
259 }
260  
261 var groupName = (string) secondLifeGroupsListBox.Items[index];
262  
263 if (_groupProfileForm != null)
264 {
265 return;
266 }
267  
268 _groupProfileForm = new GroupProfileForm(_mainForm, groupName, _mqttCommunication);
269 _groupProfileForm.Closing += GroupProfileForm_Closing;
270 _groupProfileForm.Show();
271 }
272  
273 private void GroupProfileForm_Closing(object sender, CancelEventArgs e)
274 {
275 if (_groupProfileForm == null)
276 {
277 return;
278 }
279  
280 _groupProfileForm.Dispose();
281 _groupProfileForm = null;
282 }
283  
284 private void PayToolStripMenuItem_Click(object sender, EventArgs e)
285 {
286 if (_paymentForm != null)
287 {
288 return;
289 }
290  
291 _paymentForm = new PaymentForm(_firstName, _lastName, _mainForm, _mqttCommunication);
292 _paymentForm.Closing += PaymentForm_Closing;
293 _paymentForm.Show();
294 }
295  
296 private void PaymentForm_Closing(object sender, CancelEventArgs e)
297 {
298 if (_paymentForm == null)
299 {
300 return;
301 }
302  
303 _paymentForm.Closing -= PaymentForm_Closing;
304 _paymentForm.Dispose();
305 _paymentForm = null;
306 }
307  
308 #endregion
2 office 309  
310  
1 office 311 }
312 }