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.ComponentModel;
4 using System.Threading;
5 using System.Windows.Forms;
6 using Korero.Communication;
7 using Korero.Properties;
8 using Korero.Selection;
9 using Korero.Utilities;
10 using Serilog;
11  
12 namespace Korero.Economy
13 {
14 public partial class PaymentForm : Form
15 {
16 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
17  
18 private readonly CancellationToken _cancellationToken;
19  
20 private readonly CancellationTokenSource _cancellationTokenSource;
21  
22 private readonly MainForm _mainForm;
23  
24 private readonly MqttCommunication _mqttCommunication;
25  
26 private AvatarSelectionForm _avatarSelectionForm;
27  
28 private GroupSelectionForm _groupSelectionForm;
29  
30 #endregion
31  
32 #region Constructors, Destructors and Finalizers
33  
34 public PaymentForm()
35 {
36 InitializeComponent();
37  
38 _cancellationTokenSource = new CancellationTokenSource();
39 _cancellationToken = _cancellationTokenSource.Token;
40 }
41  
42 public PaymentForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
43 {
44 _mainForm = mainForm;
45 _mqttCommunication = mqttCommunication;
46  
47 _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
48 }
49  
50 public PaymentForm(string firstName, string lastName, MainForm mainForm, MqttCommunication mqttCommunication) :
51 this(mainForm, mqttCommunication)
52 {
53 textBox2.InvokeIfRequired(firstNameTextBox =>
54 {
55 textBox4.InvokeIfRequired(lastNameTextBox =>
56 {
57 firstNameTextBox.Text = firstName;
58 lastNameTextBox.Text = lastName;
59 });
60 });
61  
62 tabControl1.InvokeIfRequired(tabControl => { tabControl.SelectedTab = tabPage1; });
63 }
64  
65 public PaymentForm(string groupName, MainForm mainForm, MqttCommunication mqttCommunication) : this(mainForm,
66 mqttCommunication)
67 {
68 textBox1.InvokeIfRequired(groupNameTextBox => { groupNameTextBox.Text = groupName; });
69  
70 tabControl1.InvokeIfRequired(tabControl => { tabControl.SelectedTab = tabPage2; });
71 }
72  
73 /// <summary>
74 /// Clean up any resources being used.
75 /// </summary>
76 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
77 protected override void Dispose(bool disposing)
78 {
79 if (disposing && components != null)
80 {
81 components.Dispose();
82 }
83  
84 _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
85 base.Dispose(disposing);
86 }
87  
88 #endregion
89  
90 #region Event Handlers
2 office 91 private void PaymentForm_Load(object sender, EventArgs e)
92 {
93 Utilities.WindowState.FormTracker.Track(this);
94 }
1 office 95 private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
96 {
97 }
98  
99 private void Button1_Click(object sender, EventArgs e)
100 {
101 if (_avatarSelectionForm != null)
102 {
103 return;
104 }
105  
106 _avatarSelectionForm = new AvatarSelectionForm(_mainForm.MqttCommunication);
107 _avatarSelectionForm.AvatarSelected += AvatarSelectionForm_AvatarSelected;
108 _avatarSelectionForm.Closing += AvatarSelectionForm_Closing;
109 _avatarSelectionForm.Show();
110 }
111  
112 private void AvatarSelectionForm_AvatarSelected(object sender, AvatarSelectedEventArgs e)
113 {
114 textBox2.InvokeIfRequired(firstNameTextBox =>
115 {
116 textBox4.InvokeIfRequired(lastNameTextBox =>
117 {
118 firstNameTextBox.Text = e.AvatarSelection.FirstName;
119 lastNameTextBox.Text = e.AvatarSelection.LastName;
120 });
121 });
122 }
123  
124 private void AvatarSelectionForm_Closing(object sender, CancelEventArgs e)
125 {
126 if (_avatarSelectionForm == null)
127 {
128 return;
129 }
130  
131 _avatarSelectionForm.AvatarSelected -= AvatarSelectionForm_AvatarSelected;
132 _avatarSelectionForm.Closing -= AvatarSelectionForm_Closing;
133 _avatarSelectionForm.Dispose();
134 _avatarSelectionForm = null;
135 }
136  
137 private void Button2_Click(object sender, EventArgs e)
138 {
139 textBox2.InvokeIfRequired(firstNameTextBox =>
140 {
141 if (string.IsNullOrEmpty(firstNameTextBox.Text))
142 {
143 return;
144 }
145  
146 textBox4.InvokeIfRequired(lastNameTextBox =>
147 {
148 if (string.IsNullOrEmpty(lastNameTextBox.Text))
149 {
150 return;
151 }
152  
153 textBox3.InvokeIfRequired(async amountBox =>
154 {
155 if (!int.TryParse(amountBox.Text, out var amount))
156 {
157 return;
158 }
159  
160 var data = new Dictionary<string, string>
161 {
162 {"command", "pay"},
163 {"group", Settings.Default.Group},
164 {"password", Settings.Default.Password},
165 {"amount", amount.ToString()},
166 {"firstname", firstNameTextBox.Text},
167 {"lastname", lastNameTextBox.Text},
168 {"entity", "avatar"}
169 };
170  
171 var callback =
172 await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
173  
174 if (callback == null || !callback.Success)
175 {
176 if (callback != null)
177 {
178 Log.Warning("Command {Command} has failed with {Error}.",
179 callback.Command, callback.Error);
180 }
181 }
182 });
183 });
184 });
185 }
186  
187 private void Button3_Click(object sender, EventArgs e)
188 {
189 if (_groupSelectionForm != null)
190 {
191 return;
192 }
193  
194 _groupSelectionForm = new GroupSelectionForm(_mqttCommunication);
195 _groupSelectionForm.GroupSelected += GroupSelectionForm_GroupSelected;
196 _groupSelectionForm.Closing += GroupSelectionForm_Closing;
197 _groupSelectionForm.Show();
198 }
199  
200 private void GroupSelectionForm_GroupSelected(object sender, GroupSelectedEventArgs e)
201 {
202 textBox1.InvokeIfRequired(nameTextBox => { nameTextBox.Text = e.GroupSelection.Name; });
203 }
204  
205 private void GroupSelectionForm_Closing(object sender, CancelEventArgs e)
206 {
207 if (_groupSelectionForm == null)
208 {
209 return;
210 }
211  
212 _groupSelectionForm.Closing -= GroupSelectionForm_Closing;
213 _groupSelectionForm.Dispose();
214 _groupSelectionForm = null;
215 }
216  
217 private void Button4_Click(object sender, EventArgs e)
218 {
219 textBox1.InvokeIfRequired(groupNameTextBox =>
220 {
221 if (string.IsNullOrEmpty(groupNameTextBox.Text))
222 {
223 return;
224 }
225  
226 textBox5.InvokeIfRequired(async amountBox =>
227 {
228 if (!int.TryParse(amountBox.Text, out var amount))
229 {
230 return;
231 }
232  
233 var data = new Dictionary<string, string>
234 {
235 {"command", "pay"},
236 {"group", Settings.Default.Group},
237 {"password", Settings.Default.Password},
238 {"amount", amount.ToString()},
239 {"target", groupNameTextBox.Text},
240 {"entity", "group"}
241 };
242  
243 var callback =
244 await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
245  
246 if (callback == null || !callback.Success)
247 {
248 if (callback != null)
249 {
250 Log.Warning("Command {Command} has failed with {Error}.",
251 callback.Command, callback.Error);
252 }
253 }
254 });
255 });
256 }
257  
258 #endregion
2 office 259  
260  
1 office 261 }
262 }