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