-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.Methods.cs
311 lines (250 loc) · 10 KB
/
MainWindow.Methods.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
//-----------------------------------------------------------------------
// <copyright file="MainWindow.Methods.cs">
// Copyright (c) 2016, 2020 Kent P. McKinney
// Released under the terms of the MIT License
// </copyright>
//-----------------------------------------------------------------------
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace VehicleInformationLookupTool
{
public partial class MainWindow
{
private static void LaunchWebBrowser(string site)
{
try
{
Process.Start(site);
}
catch (Exception)
{
var message = new StringBuilder("Unable to open the site in a _web browser automatically.");
message.Append(" Please visit the following _web page manually:\n\n" + site);
message.Append("\n\nPossible Causes:\n");
message.Append(" This application cannot access the Internet\n");
message.Append(" The _web page has moved to a different location\n");
message.Append(" Configuration setting or problem with the operating system\n");
MessageBox.Show(message.ToString());
}
}
private static DataTable VinTextToDataTable(ref string textContainingVins)
{
/*
Requirement: the user must only use commas, semicolons, and newlines to delineate VINs (as indicated in the UI)
This application does not attempt to validate VIN numbers
*/
/* Normalize the text so that there is one VIN per line */
textContainingVins = textContainingVins.Replace(";", "\n");
textContainingVins = textContainingVins.Replace(",", "\n");
textContainingVins = textContainingVins.Replace(Environment.NewLine, "\n");
textContainingVins = textContainingVins.Replace("\n\n\n", "\n");
textContainingVins = textContainingVins.Replace("\n\n", "\n");
textContainingVins = textContainingVins.Replace(" ", string.Empty);
/* Convert the text into a list of strings */
var vinList = textContainingVins.Split('\n');
/* Create a DataTable */
var table = new DataTable();
/* Add columns */
table.Columns.Add("VIN");
/* Add rows */
foreach (var vin in vinList)
{
if (!string.IsNullOrWhiteSpace(vin))
{
table.Rows.Add(vin);
}
}
return table;
}
private static bool IsFirstRun()
{
if (ReadRegistryString("FirstRun") == "False")
{
return false;
}
WriteRegistryString("FirstRun", "False");
return true;
}
private static bool UserHasAgreedToEula() =>
ReadRegistryString("AgreedEULA") == true.ToString();
private static void SetUserAgreedToEula(bool state) =>
WriteRegistryString("AgreedEULA", state.ToString());
private static string ReadRegistryString(string valueName)
{
try
{
/* Attempt to open the key HKCU\SOFTWARE\VehicleInformationLookupTool */
var hkcu = Registry.CurrentUser;
var software = hkcu?.OpenSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadWriteSubTree);
var key = software?.OpenSubKey("VehicleInformationLookupTool",
RegistryKeyPermissionCheck.ReadWriteSubTree);
/* Attempt to get the named value */
var valueString = key?.GetValue(valueName) as string ?? string.Empty;
key?.Close();
return valueString;
}
catch (Exception)
{
return string.Empty;
}
}
private static void WriteRegistryString(string valueName, string valueString)
{
try
{
/* Attempt to open the key HKCU\SOFTWARE\VehicleInformationLookupTool */
var hkcu = Registry.CurrentUser;
var software = hkcu?.OpenSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadWriteSubTree);
var key = software?.OpenSubKey("VehicleInformationLookupTool",
RegistryKeyPermissionCheck.ReadWriteSubTree);
/* If the key was not found attempt to create it */
if (key is null)
{
key = software?.CreateSubKey("VehicleInformationLookupTool");
}
if (string.IsNullOrWhiteSpace(key?.GetValue(valueName) as string))
{
key?.SetValue(valueName, valueString);
}
key?.Close();
}
catch (Exception)
{
/* Ignore */
}
}
private static string PromptOpenExcelFileName()
{
var dialog = new OpenFileDialog()
{
DefaultExt = ".xlsx",
Filter = "Excel/CSV files (*.xlsx;*.xls;*.csv)|*.xlsx;*.xls;*.csv|All files (*.*)|*.*"
};
/* Display the dialog and return the text entered by the user */
return dialog?.ShowDialog() == true
? dialog.FileName
: string.Empty;
}
private static string PromptSaveExcelFileName(bool IsExcel)
{
var dialog = IsExcel
? new SaveFileDialog()
{
DefaultExt = ".xlsx",
AddExtension = true,
OverwritePrompt = true,
Filter = "Excel files (*.xlsx)|*.xlsx"
}
: new SaveFileDialog()
{
DefaultExt = ".csv",
AddExtension = true,
OverwritePrompt = true,
Filter = "CSV files (*.csv)|*.csv"
};
/* Display the dialog and return the text entered by the user */
return dialog.ShowDialog() == true
? dialog.FileName
: string.Empty;
}
private static List<string> GetVinList(in DataGrid grid, string columnName)
{
grid.ThrowIfNullOrEmpty();
columnName.ThrowIfNullOrEmpty();
var columnIndex = GetDataGridColumnIndex(grid, columnName);
var rows = grid.ItemsSource;
return (from DataRowView row in rows select row[columnIndex] as string).ToList();
}
private static int GetDataGridColumnIndex(in DataGrid grid, string columnName)
{
grid.ThrowIfNullOrEmpty();
columnName.ThrowIfNullOrEmpty();
for (var i = 0; i < grid.Columns.Count; i++)
{
var columnHeader = grid.Columns[i].Header as string;
if (columnHeader == columnName)
{
return i;
}
}
return 0;
}
private static void AddVinRowToDataTable(in DataTable table, in List<string> dataValues)
{
table.ThrowIfNullOrEmpty();
dataValues.ThrowIfNullOrEmpty();
var row = table.NewRow();
if (row.Table.Columns.Count != dataValues.Count)
{
return;
}
for (var i = 0; i < row.Table.Columns.Count; i++)
{
row[i] = dataValues[i] ?? string.Empty;
}
table.Rows.Add(row);
}
private static List<string> GetDataGridColumnNames(in DataGrid datagrid)
{
datagrid.ThrowIfNullOrEmpty();
return datagrid.Columns.Select(column => column.Header.ToString()).ToList();
}
private static List<string> GetDataGridRowValues(in DataGrid datagrid, int rowNumber)
{
datagrid.ThrowIfNullOrEmpty();
var dataValues = new List<string>();
if (!(datagrid.Items[rowNumber] is DataRowView row))
{
return new List<string>();
}
for (var i = 0; i < datagrid.Columns.Count; i++)
{
dataValues.Add(row[i] as string);
}
return dataValues;
}
private static void OrderGridViewItems(in DataGrid datagrid, string columnToOrder, in List<string> orderedStringList)
{
/* Create a new DataTable */
var orderedData = new DataTable();
/* Add columns */
var columnNames = GetDataGridColumnNames(datagrid);
foreach (var name in columnNames)
{
orderedData.Columns.Add(name);
}
/* Add rows */
var columnIndex = GetDataGridColumnIndex(datagrid, columnToOrder);
foreach (var orderedVinNumber in orderedStringList)
{
if (string.IsNullOrWhiteSpace(orderedVinNumber))
{
continue;
}
foreach (DataRowView row in datagrid.Items)
{
if (row is null)
{
continue;
}
var vinValue = row[columnIndex]?.ToString().ToLower() ?? string.Empty;
var originalVin = row["OriginalVIN"]?.ToString().ToLower() ?? string.Empty;
if (orderedVinNumber.ToLower() == vinValue || orderedVinNumber.ToLower() == originalVin)
{
orderedData.ImportRow(row.Row);
break;
}
}
}
/* Replace the old ItemsSource with the new DataTable */
datagrid.ItemsSource = orderedData.DefaultView;
}
}
}