Korero – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.Threading;
4 using System.Threading.Tasks;
5 using System.Windows.Forms;
6 using Korero.Communication;
7 using Korero.Properties;
8 using Korero.Serialization;
9 using Korero.Utilities;
10 using Serilog;
11  
12 namespace Korero.Selection
13 {
14 public partial class AvatarSelectionForm : Form
15 {
16 #region Public Events & Delegates
17  
18 public event EventHandler<AvatarSelectedEventArgs> AvatarSelected;
19  
20 #endregion
21  
22 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
23  
24 private readonly CancellationToken _cancellationToken;
25  
26 private readonly CancellationTokenSource _cancellationTokenSource;
27  
28 private readonly dynamic _data;
29  
30 private readonly MqttCommunication _mqttCommunication;
31  
32 #endregion
33  
34 #region Constructors, Destructors and Finalizers
35  
36 public AvatarSelectionForm()
37 {
38 InitializeComponent();
39 Utilities.WindowState.FormTracker.Track(this);
40  
41 _cancellationTokenSource = new CancellationTokenSource();
42 _cancellationToken = _cancellationTokenSource.Token;
43 }
44  
45 public AvatarSelectionForm(MqttCommunication mqttCommunication, dynamic data) : this()
46 {
47 _data = data;
48 _mqttCommunication = mqttCommunication;
49 }
50  
51 public AvatarSelectionForm(MqttCommunication mqttCommunication) : this()
52 {
53 _mqttCommunication = mqttCommunication;
54 }
55  
56 /// <summary>
57 /// Clean up any resources being used.
58 /// </summary>
59 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
60 protected override void Dispose(bool disposing)
61 {
62 if (disposing && components != null)
63 {
64 components.Dispose();
65 }
66  
67 base.Dispose(disposing);
68 }
69  
70 #endregion
71  
72 #region Event Handlers
73  
74 private async void Button1_Click(object sender, EventArgs e)
75 {
76 var firstName = textBox1.Text;
77 var lastName = textBox2.Text;
78  
79 var data = new Dictionary<string, string>
80 {
81 {"command", "batchavatarnametokey"},
82 {"group", Settings.Default.Group},
83 {"password", Settings.Default.Password},
84 {"avatars", new CSV(new[] {$"{firstName} {lastName}"})},
85 {"sift", new CSV(new[] {"take", "1"})}
86 };
87  
88 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
89  
90 if (callback == null || !callback.Success)
91 {
92 if (callback != null)
93 {
94 Log.Warning("Command {Command} has failed with {Error}.",
95 callback.Command, callback.Error);
96 }
97  
98 return;
99 }
100  
101 if (!callback.Contains($"{firstName} {lastName}"))
102 {
103 return;
104 }
105  
106 var avatarSelection = new AvatarSelection(firstName, lastName);
107  
108 AvatarSelected?.Invoke(this, new AvatarSelectedEventArgs(avatarSelection, _data));
109  
110 Close();
111 }
112  
113 private void RegionListBox_SelectedIndexChanged(object sender, EventArgs e)
114 {
115 if (regionListBox.SelectedIndex > -1)
116 {
117 friendsListBox.ClearSelected();
118 }
119 }
120  
121 private void FriendsListBox_SelectedIndexChanged(object sender, EventArgs e)
122 {
123 if (friendsListBox.SelectedIndex > -1)
124 {
125 regionListBox.ClearSelected();
126 }
127 }
128  
129 private void FriendsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
130 {
131 EmitSelection((ListBox) sender, e);
132 }
133  
134 private void RegionListBox_MouseDoubleClick(object sender, MouseEventArgs e)
135 {
136 EmitSelection((ListBox) sender, e);
137 }
138  
139 private async void AvatarSelectionForm_Shown(object sender, EventArgs e)
140 {
141 await Task.WhenAll(RetrieveFriendsList(), RetrieveRegionAvatars());
142 }
143  
144 private void AvatarSelectionForm_FormClosing(object sender, FormClosingEventArgs e)
145 {
146 _cancellationTokenSource.Cancel();
147 }
148  
149 #endregion
150  
151 #region Public Methods
152  
153 public void EmitSelection(ListBox listBox, MouseEventArgs e)
154 {
155 var index = listBox.IndexFromPoint(e.Location);
156 if (index == ListBox.NoMatches)
157 {
158 return;
159 }
160  
161 var avatarSelection = (AvatarSelection) listBox.Items[index];
162 AvatarSelected?.Invoke(this, new AvatarSelectedEventArgs(avatarSelection, _data));
163  
164 Close();
165 }
166  
167 #endregion
168  
169 #region Private Methods
170  
171 private async Task RetrieveFriendsList()
172 {
173 var data = new Dictionary<string, string>
174 {
175 {"command", "getfriendslist"},
176 {"group", Settings.Default.Group},
177 {"password", Settings.Default.Password},
178 {"sift", new CSV(new[] {"each", "2"})}
179 };
180  
181 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
182  
183 if (callback == null || !callback.Success)
184 {
185 if (callback != null)
186 {
187 Log.Warning("Command {Command} has failed with {Error}.",
188 callback.Command, callback.Error);
189 }
190  
191 return;
192 }
193  
194 foreach (var item in callback.Data)
195 {
196 friendsListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new AvatarSelection(item)); });
197 }
198 }
199  
200 private async Task RetrieveRegionAvatars()
201 {
202 var data = new Dictionary<string, string>
203 {
204 {"command", "getavatarpositions"},
205 {"group", Settings.Default.Group},
206 {"password", Settings.Default.Password},
207 {"entity", "region"},
208 {"sift", new CSV(new[] {"each", "3"})}
209 };
210  
211 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
212  
213 if (callback == null || !callback.Success)
214 {
215 if (callback != null)
216 {
217 Log.Warning("Command {Command} has failed with {Error}.",
218 callback.Command, callback.Error);
219 }
220  
221 return;
222 }
223  
224 foreach (var item in callback.Data)
225 {
226 regionListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new AvatarSelection(item)); });
227 }
228 }
229  
230 #endregion
231 }
232 }