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.Selection
13 {
14 public partial class GroupSelectionForm : Form
15 {
16 #region Public Events & Delegates
17  
18 public event EventHandler<GroupSelectedEventArgs> GroupSelected;
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 GroupSelectionForm()
37 {
38 InitializeComponent();
39  
40 _cancellationTokenSource = new CancellationTokenSource();
41 _cancellationToken = _cancellationTokenSource.Token;
42 }
43  
44 public GroupSelectionForm(MqttCommunication mqttCommunication, dynamic data) : this()
45 {
46 _data = data;
47 _mqttCommunication = mqttCommunication;
48 }
49  
50 public GroupSelectionForm(MqttCommunication mqttCommunication) : this()
51 {
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 GroupSelectionForm_Load(object sender, EventArgs e)
73 {
74 Utilities.WindowState.FormTracker.Track(this);
75 }
1 office 76 private async void GroupSelectionForm_Shown(object sender, EventArgs e)
77 {
78 await RetrieveCurrentGroups();
79 }
80  
81 private void GroupsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
82 {
83 EmitSelection((ListBox) sender, e);
84 }
85  
86 private void GroupSelectionForm_FormClosing(object sender, FormClosingEventArgs e)
87 {
88 _cancellationTokenSource.Cancel();
89 }
90  
91 private async void Button1_Click(object sender, EventArgs e)
92 {
93 var name = textBox1.Text;
94  
95 var data = new Dictionary<string, string>
96 {
97 {"command", "batchgroupnametokey"},
98 {"group", Settings.Default.Group},
99 {"password", Settings.Default.Password},
100 {"groups", new CSV(new[] {$"{name}"})},
101 {"sift", new CSV(new[] {"take", "1"})}
102 };
103  
104 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
105  
106 if (callback == null || !callback.Success)
107 {
108 if (callback != null)
109 {
110 Log.Warning("Command {Command} has failed with {Error}.",
111 callback.Command, callback.Error);
112 }
113  
114 return;
115 }
116  
117 if (!callback.Contains($"{name}"))
118 {
119 return;
120 }
121  
122 var groupSelection = new GroupSelection(name);
123  
124 GroupSelected?.Invoke(this, new GroupSelectedEventArgs(groupSelection, _data));
125  
126 Close();
127 }
128  
129 #endregion
130  
131 #region Public Methods
132  
133 public void EmitSelection(ListBox listBox, MouseEventArgs e)
134 {
135 var index = listBox.IndexFromPoint(e.Location);
136 if (index == ListBox.NoMatches)
137 {
138 return;
139 }
140  
141 var groupSelection = (GroupSelection) listBox.Items[index];
142 GroupSelected?.Invoke(this, new GroupSelectedEventArgs(groupSelection, _data));
143  
144 Close();
145 }
146  
147 #endregion
148  
149 #region Private Methods
150  
151 private async Task RetrieveCurrentGroups()
152 {
153 var data = new Dictionary<string, string>
154 {
155 {"command", "getcurrentgroups"},
156 {"group", Settings.Default.Group},
157 {"password", Settings.Default.Password},
158 {"sift", new CSV(new[] {"each", "2"})}
159 };
160  
161 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
162  
163 if (callback == null || !callback.Success)
164 {
165 if (callback != null)
166 {
167 Log.Warning("Command {Command} has failed with {Error}.",
168 callback.Command, callback.Error);
169 }
170  
171 return;
172 }
173  
174 foreach (var item in callback.Data)
175 {
176 groupsListBox.InvokeIfRequired(listBox => { listBox.Items.Add(new GroupSelection(item)); });
177 }
178 }
179  
180 #endregion
2 office 181  
182  
1 office 183 }
184 }