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.Chat
13 {
14 public partial class NewConversationForm : Form
15 {
16 #region Public Events & Delegates
17  
18 public event EventHandler<NewConversationSelectedEventArgs> NewConversationSelected;
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 MqttCommunication _mqttCommunication;
29  
30 #endregion
31  
32 #region Constructors, Destructors and Finalizers
33  
34 public NewConversationForm()
35 {
36 InitializeComponent();
37 Utilities.WindowState.FormTracker.Track(this);
38  
39 _cancellationTokenSource = new CancellationTokenSource();
40 _cancellationToken = _cancellationTokenSource.Token;
41 }
42  
43 public NewConversationForm(MqttCommunication mqttCommunication) : this()
44 {
45 _mqttCommunication = mqttCommunication;
46 }
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 _cancellationTokenSource.Cancel();
61 base.Dispose(disposing);
62 }
63  
64 #endregion
65  
66 #region Event Handlers
67  
68 private void RegionListBox_SelectedIndexChanged(object sender, EventArgs e)
69 {
70 if (regionListBox.SelectedIndex > -1)
71 {
72 groupsListBox.ClearSelected();
73 friendsListBox.ClearSelected();
74 }
75 }
76  
77 private void GroupsListBox_SelectedIndexChanged(object sender, EventArgs e)
78 {
79 if (groupsListBox.SelectedIndex > -1)
80 {
81 regionListBox.ClearSelected();
82 friendsListBox.ClearSelected();
83 }
84 }
85  
86 private void FriendsListBox_SelectedIndexChanged(object sender, EventArgs e)
87 {
88 if (friendsListBox.SelectedIndex > -1)
89 {
90 regionListBox.ClearSelected();
91 groupsListBox.ClearSelected();
92 }
93 }
94  
95 private void FriendsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
96 {
97 EmitSelection((ListBox) sender, e);
98 }
99  
100 private void GroupsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
101 {
102 EmitSelection((ListBox) sender, e);
103 }
104  
105 private void RegionListBox_MouseDoubleClick(object sender, MouseEventArgs e)
106 {
107 EmitSelection((ListBox) sender, e);
108 }
109  
110 private async void Button1_Click(object sender, EventArgs e)
111 {
112 var firstName = textBox1.Text;
113 var lastName = textBox2.Text;
114  
115 var data = new Dictionary<string, string>
116 {
117 {"command", "batchavatarnametokey"},
118 {"group", Settings.Default.Group},
119 {"password", Settings.Default.Password},
120 {"avatars", new CSV(new[] {$"{firstName} {lastName}"})},
121 {"sift", new CSV(new[] {"take", "1"})}
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 if (!callback.Contains($"{firstName} {lastName}"))
138 {
139 return;
140 }
141  
142 var conversation = new ConversationAvatar(firstName, lastName);
143  
144 NewConversationSelected?.Invoke(this, new NewConversationSelectedEventArgs(conversation));
145  
146 Close();
147 }
148  
149 private async void NewConversationForm_Shown(object sender, EventArgs e)
150 {
151 await Task.WhenAll(RetrieveFriendsList(), RetrieveCurrentGroups(), RetrieveRegionAvatars());
152 }
153  
154 #endregion
155  
156 #region Public Methods
157  
158 public void EmitSelection(ListBox listBox, MouseEventArgs e)
159 {
160 var index = listBox.IndexFromPoint(e.Location);
161 if (index == ListBox.NoMatches)
162 {
163 return;
164 }
165  
166 var newConversation = (Conversation) listBox.Items[index];
167 NewConversationSelected?.Invoke(this, new NewConversationSelectedEventArgs(newConversation));
168  
169 Close();
170 }
171  
172 #endregion
173  
174 #region Private Methods
175  
176 private async Task RetrieveRegionAvatars()
177 {
178 var data = new Dictionary<string, string>
179 {
180 {"command", "getavatarpositions"},
181 {"group", Settings.Default.Group},
182 {"password", Settings.Default.Password},
183 {"entity", "region"},
184 {"sift", new CSV(new[] {"each", "3"})}
185 };
186  
187 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
188  
189 if (callback == null || !callback.Success)
190 {
191 if (callback != null)
192 {
193 Log.Warning("Command {Command} has failed with {Error}.",
194 callback.Command, callback.Error);
195 }
196  
197 return;
198 }
199  
200 foreach (var item in callback.Data)
201 {
202 regionListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new ConversationAvatar(item)); });
203 }
204 }
205  
206 private async Task RetrieveCurrentGroups()
207 {
208 var data = new Dictionary<string, string>
209 {
210 {"command", "getcurrentgroups"},
211 {"group", Settings.Default.Group},
212 {"password", Settings.Default.Password},
213 {"sift", new CSV(new[] {"each", "2"})}
214 };
215  
216 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
217  
218 if (callback == null || !callback.Success)
219 {
220 if (callback != null)
221 {
222 Log.Warning("Command {Command} has failed with {Error}.",
223 callback.Command, callback.Error);
224 }
225  
226 return;
227 }
228  
229 foreach (var item in callback.Data)
230 {
231 groupsListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new ConversationGroup(item)); });
232 }
233 }
234  
235 private async Task RetrieveFriendsList()
236 {
237 var data = new Dictionary<string, string>
238 {
239 {"command", "getfriendslist"},
240 {"group", Settings.Default.Group},
241 {"password", Settings.Default.Password},
242 {"sift", new CSV(new[] {"each", "2"})}
243 };
244  
245 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
246  
247 if (callback == null || !callback.Success)
248 {
249 if (callback != null)
250 {
251 Log.Warning("Command {Command} has failed with {Error}.",
252 callback.Command, callback.Error);
253 }
254  
255 return;
256 }
257  
258 foreach (var item in callback.Data)
259 {
260 friendsListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new ConversationAvatar(item)); });
261 }
262 }
263  
264 #endregion
265 }
266 }