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