-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallLookupPanel.cs
315 lines (276 loc) · 10.3 KB
/
CallLookupPanel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Unclassified.Util;
using W6OP.CallParser;
namespace W6OP
{
public partial class CallLookupPanel : UserControl
{
// prefix file parser and call lookup class instances
private readonly PrefixFileParser PrefixFileParser;
private CallLookUp CallLookUp;
Settings settings;
// plugin instance
public CallLookupPlugin Plugin;
// these must be initialized or GetSettings() fails
public string QRZLogonId = "";
public string QRZPassword = "";
public string Grid = "";
// needed because the button clicks on control closing
private string LastCallLookedUp = "";
/// <summary>
/// Constuctor.
/// </summary>
public CallLookupPanel()
{
InitializeComponent();
PrefixFileParser = new PrefixFileParser();
PrefixFileParser.ParsePrefixFile("");
CallLookUp = new CallLookUp(PrefixFileParser);
}
/// <summary>
/// Initialization after window loads.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CallLookupPanel_Load(object sender, EventArgs e)
{
settings = (Settings)Plugin.Settings;
TextBoxCallSign.Focus();
}
/// <summary>
/// Captures the Enter key when in the Call Sign textbox.
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
ButtonLookupCall.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// Look up a call sign.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCallLookup_Click(object sender, EventArgs e)
{
IEnumerable<Hit> hitCollection = new List<Hit>();
string heading;
string distance;
string grid = "";
string homeLocation;
// why does this get hit before control is loaded???
if (!ButtonLookupCall.Enabled)
{
return;
}
try
{
if (!string.IsNullOrEmpty(TextBoxCallSign.Text) && TextBoxCallSign.Text != LastCallLookedUp)
{
homeLocation = settings.Grid;
ListViewResults.Items.Clear();
LastCallLookedUp = TextBoxCallSign.Text;
if (CheckBoxQRZ.Checked)
{
if (!string.IsNullOrEmpty(settings.QRZLogonId) && !string.IsNullOrEmpty(settings.QRZLogonId))
{
try
{
hitCollection = CallLookUp.LookUpCall(TextBoxCallSign.Text, settings.QRZLogonId, settings.QRZPassword);
}
catch (Exception ex)
{
MessageBox.Show("There has been an error querying QRZ. \n" +
"Do you have an XML Logbook subscription? \n " + ex.Message, "QRZ Lookup Failure",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please go to Plugin Settings and add your QRZ.com credentials", "Missing Credentials", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
else
{
hitCollection = CallLookUp.LookUpCall(TextBoxCallSign.Text);
}
if (hitCollection != null)
{
var hitList = hitCollection.ToList();
foreach (Hit hit in hitList)
{
if (!string.IsNullOrEmpty(homeLocation))
{
grid = GetGridFromLatLong(hit.Latitude,hit.Longitude);
heading = GetHeading(homeLocation, grid);
distance = GetDistance(homeLocation, grid);
} else
{
heading = "0";
distance = "0";
}
UpdateListViewResults(hit, heading, distance, grid);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "An Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Get the heading from one grid to another.
/// </summary>
/// <param name="destination"></param>
/// <returns></returns>
private string GetHeading(string homeLocation, string destination)
{
double azimuth = MaidenheadLocator.Azimuth(homeLocation.ToUpper(), destination.ToUpper());
return string.Format("{0:0.##}", azimuth);
}
/// <summary>
/// Get the distance from one grid to another.
/// </summary>
/// <param name="destination"></param>
/// <returns></returns>
private string GetDistance(string homeLocation, string destination)
{
double distance = MaidenheadLocator.Distance(homeLocation.ToUpper(), destination.ToUpper());
if (settings.Unit == Units.Miles)
{
distance /= 1.6;
}
return string.Format("{0:0.##}", distance);
}
/// <summary>
/// Get the maidenhead grid form the latitude and longitude.
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <returns></returns>
private string GetGridFromLatLong(string latitude, string longitude)
{
double lat = Convert.ToDouble(latitude);
double lon = Convert.ToDouble(longitude);
string grid = MaidenheadLocator.LatLngToLocator(lat, lon);
return grid;
}
/// <summary>
/// Display the results in a ListView
/// Prevent cross thread calls.
/// </summary>
/// <param name="call"></param>
/// <param name="clear"></param>
private void UpdateListViewResults(Hit hit, string heading, string distance, string grid)
{
if (!InvokeRequired)
{
ListViewItem item;
if (hit.Kind == PrefixKind.DXCC)
{
item = new ListViewItem(hit.CallSign);
item.BackColor = Color.Thistle;
}
else
{
item = new ListViewItem(hit.CallSign);
item.BackColor = Color.LavenderBlush;
}
item.SubItems.Add(hit.Kind.ToString());
item.SubItems.Add(hit.Country);
item.SubItems.Add(hit.Province ?? "");
item.SubItems.Add(hit.DXCC.ToString());
if (hit.IsQRZInformation)
{
item.SubItems.Add("QRZ");
item.Tag = hit;
}
else
{
item.SubItems.Add("");
}
//heading = String.Format("{0:0.##}", heading);
//distance = String.Format("{0:0.##}", distance);
item.SubItems.Add(heading);
item.SubItems.Add(distance);
item.SubItems.Add(grid);
ListViewResults.Items.Add(item);
}
else
{
BeginInvoke(new Action<Hit, string, string, string>(UpdateListViewResults), hit, heading, distance, grid);
return;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxCallSign_KeyDown(object sender, KeyEventArgs e)
{
if(!CheckBoxQRZ.Checked)
{
if (e.KeyCode == Keys.Enter)
{
ButtonCallLookup_Click(this, new EventArgs());
}
}
else
{
if (TextBoxCallSign.Text.Length > 2)
{
if (e.KeyCode == Keys.Enter)
{
ButtonCallLookup_Click(this, new EventArgs());
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListViewResults_SelectedIndexChanged(object sender, EventArgs e)
{
QRZDetailForm detail;
if (ListViewResults.SelectedItems.Count > 0)
{
ListViewItem item = ListViewResults.SelectedItems[0];
if (item.Tag != null)
{
detail = new QRZDetailForm((Hit)item.Tag)
{
StartPosition = FormStartPosition.CenterParent
};
detail.Show(this);
}
}
}
private void CallLookupPanel_Leave(object sender, EventArgs e)
{
ButtonLookupCall.Enabled = false;
}
private void TextBoxCallSign_Enter(object sender, EventArgs e)
{
ButtonLookupCall.Enabled = true;
}
private void CheckBoxQRZ_CheckedChanged(object sender, EventArgs e)
{
LastCallLookedUp = "";
}
} // end class
}