RegName – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Drawing; |
||
5 | using System.IO; |
||
6 | using System.Linq; |
||
7 | using System.Net; |
||
8 | using System.Reflection; |
||
9 | using System.Text.RegularExpressions; |
||
10 | using System.Threading.Tasks; |
||
11 | using System.Windows.Forms; |
||
12 | using NetSparkleUpdater.Enums; |
||
13 | using NetSparkleUpdater.SignatureVerifiers; |
||
14 | using NetSparkleUpdater.UI.WinForms; |
||
15 | using NetSparkleUpdater; |
||
16 | using RegName.Utilities; |
||
17 | |||
18 | namespace RegName |
||
19 | { |
||
20 | public partial class MainForm : Form |
||
21 | { |
||
22 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
23 | |||
24 | private AboutForm _aboutForm; |
||
25 | |||
26 | private Regex _expression; |
||
27 | |||
28 | private Helpers.IncrementMode _incrementMode; |
||
29 | |||
30 | private RegexOptions _regexOptions; |
||
31 | private readonly SparkleUpdater _sparkle; |
||
32 | |||
33 | #endregion |
||
34 | |||
35 | #region Constructors, Destructors and Finalizers |
||
36 | |||
37 | public MainForm() |
||
38 | { |
||
39 | InitializeComponent(); |
||
40 | Utilities.WindowState.FormTracker.Track(this); |
||
41 | |||
42 | // Start application update. |
||
43 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
||
44 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
||
45 | |||
46 | _sparkle = new SparkleUpdater("https://regname.grimore.org/update/appcast.xml", |
||
47 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
||
48 | { |
||
49 | UIFactory = new UIFactory(icon), |
||
50 | RelaunchAfterUpdate = true, |
||
51 | SecurityProtocolType = SecurityProtocolType.Tls12 |
||
52 | }; |
||
53 | _sparkle.StartLoop(true, true); |
||
54 | |||
55 | comboBox1.SelectedIndex = 0; |
||
56 | _regexOptions = RegexOptions.Compiled; |
||
57 | } |
||
58 | |||
59 | /// <summary> |
||
60 | /// Clean up any resources being used. |
||
61 | /// </summary> |
||
62 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
63 | protected override void Dispose(bool disposing) |
||
64 | { |
||
65 | if (disposing && components != null) |
||
66 | { |
||
67 | components.Dispose(); |
||
68 | } |
||
69 | |||
70 | base.Dispose(disposing); |
||
71 | } |
||
72 | |||
73 | #endregion |
||
74 | |||
75 | #region Event Handlers |
||
76 | |||
77 | private void DataGridView2_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) |
||
78 | { |
||
79 | //UpdatePreviewDataGridView(); |
||
80 | } |
||
81 | |||
82 | private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) |
||
83 | { |
||
84 | //UpdatePreviewDataGridView(); |
||
85 | } |
||
86 | |||
87 | private void DataGridView2_Scroll(object sender, ScrollEventArgs e) |
||
88 | { |
||
89 | switch (e.ScrollOrientation) |
||
90 | { |
||
91 | case ScrollOrientation.VerticalScroll: |
||
92 | dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView2.FirstDisplayedScrollingRowIndex; |
||
93 | break; |
||
94 | case ScrollOrientation.HorizontalScroll: |
||
95 | if (dataGridView1.Rows.Count > 0 & dataGridView2.Rows.Count > 0) |
||
96 | { |
||
97 | dataGridView1.HorizontalScrollingOffset = e.NewValue; |
||
98 | } |
||
99 | |||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | private void DataGridView1_Scroll(object sender, ScrollEventArgs e) |
||
105 | { |
||
106 | switch (e.ScrollOrientation) |
||
107 | { |
||
108 | case ScrollOrientation.VerticalScroll: |
||
109 | dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex; |
||
110 | break; |
||
111 | case ScrollOrientation.HorizontalScroll: |
||
112 | if (dataGridView2.Rows.Count > 0 & dataGridView1.Rows.Count > 0) |
||
113 | { |
||
114 | dataGridView2.HorizontalScrollingOffset = e.NewValue; |
||
115 | } |
||
116 | |||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | private void TextBox3_TextChanged(object sender, EventArgs e) |
||
122 | { |
||
123 | UpdatePreviewDataGridView(); |
||
124 | } |
||
125 | |||
126 | private void DataGridView1_Sorted(object sender, EventArgs e) |
||
127 | { |
||
128 | UpdatePreviewDataGridView(); |
||
129 | } |
||
130 | |||
131 | private void QuitToolStripMenuItem_Click(object sender, EventArgs e) |
||
132 | { |
||
133 | Application.Exit(); |
||
134 | } |
||
135 | |||
136 | private void AboutToolStripMenuItem1_Click(object sender, EventArgs e) |
||
137 | { |
||
138 | if (_aboutForm != null) |
||
139 | { |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | _aboutForm = new AboutForm(); |
||
144 | _aboutForm.Closing += AboutForm_Closing; |
||
145 | _aboutForm.Show(); |
||
146 | } |
||
147 | |||
148 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
||
149 | { |
||
150 | if (_aboutForm == null) |
||
151 | { |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | _aboutForm.Closing -= AboutForm_Closing; |
||
156 | _aboutForm.Dispose(); |
||
157 | _aboutForm = null; |
||
158 | } |
||
159 | |||
160 | private void RemoveAllToolStripMenuItem_Click(object sender, EventArgs e) |
||
161 | { |
||
162 | dataGridView1.Rows.Clear(); |
||
163 | |||
164 | UpdatePreviewDataGridView(); |
||
165 | } |
||
166 | |||
167 | private async void Button2_Click(object sender, EventArgs e) |
||
168 | { |
||
169 | toolStripStatusLabel1.Text = "Renaming..."; |
||
170 | |||
171 | var rows = dataGridView2.Rows.OfType<DataGridViewRow>(); |
||
172 | var dataGridViewRows = rows as DataGridViewRow[] ?? rows.ToArray(); |
||
173 | |||
174 | var count = dataGridViewRows.Length; |
||
175 | |||
176 | var rename = dataGridViewRows |
||
177 | .Select(row => |
||
178 | new FileRename((string) row.Cells[0].Value, (string) dataGridView1.Rows[row.Index].Cells[0].Tag)) |
||
179 | .ToList(); |
||
180 | |||
181 | toolStripProgressBar1.Minimum = 0; |
||
182 | toolStripProgressBar1.Maximum = count; |
||
183 | |||
184 | var progress = new Progress<RenameProgress>(renameProgress => |
||
185 | { |
||
186 | toolStripProgressBar1.Value = renameProgress.Index; |
||
187 | |||
188 | var previewRow = dataGridView2.Rows[renameProgress.Index]; |
||
189 | var replaceRow = dataGridView1.Rows[renameProgress.Index]; |
||
190 | |||
191 | switch (renameProgress) |
||
192 | { |
||
193 | case RenameProgressSuccess renameSuccess: |
||
194 | |||
195 | previewRow.DefaultCellStyle.BackColor = Color.LightGreen; |
||
196 | |||
197 | replaceRow.Cells[0].Tag = renameSuccess.New.FullName; |
||
198 | replaceRow.Cells[0].Value = renameSuccess.New.Name; |
||
199 | replaceRow.Cells[1].Value = renameSuccess.New.CreationTime; |
||
200 | replaceRow.Cells[2].Value = renameSuccess.New.LastAccessTime; |
||
201 | |||
202 | previewRow.Cells[0].Tag = renameSuccess.New.FullName; |
||
203 | previewRow.Cells[0].Value = renameSuccess.New.Name; |
||
204 | |||
205 | return; |
||
206 | case RenameProgressFailure renameFailure: |
||
207 | toolStripStatusLabel1.Text = $"Could not rename file: {renameFailure.Exception.Message}"; |
||
208 | |||
209 | previewRow.DefaultCellStyle.BackColor = Color.LightPink; |
||
210 | |||
211 | return; |
||
212 | } |
||
213 | }); |
||
214 | |||
215 | await Task.Run(() => RenameFiles(rename, progress)); |
||
216 | |||
217 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
218 | toolStripStatusLabel1.Text = "Done."; |
||
219 | } |
||
220 | |||
221 | private void TextBox1_TextChanged(object sender, EventArgs e) |
||
222 | { |
||
223 | UpdatePreviewDataGridView(); |
||
224 | } |
||
225 | |||
226 | private void TextBox2_TextChanged(object sender, EventArgs e) |
||
227 | { |
||
228 | var textBox = (TextBox) sender; |
||
229 | |||
230 | if (string.IsNullOrEmpty(textBox.Text)) |
||
231 | { |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | try |
||
236 | { |
||
237 | _expression = new Regex(textBox.Text, _regexOptions); |
||
238 | |||
239 | foreach (DataGridViewRow rows in dataGridView1.Rows) |
||
240 | { |
||
241 | if (!_expression.IsMatch((string) rows.Cells[0].Value)) |
||
242 | { |
||
243 | textBox.BackColor = Color.LightYellow; |
||
244 | break; |
||
245 | } |
||
246 | |||
247 | textBox.BackColor = Color.LightGreen; |
||
248 | } |
||
249 | |||
250 | UpdatePreviewDataGridView(); |
||
251 | } |
||
252 | catch |
||
253 | { |
||
254 | _expression = null; |
||
255 | |||
256 | textBox.BackColor = Color.LightPink; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | private void RemoveToolStripMenuItem_Click(object sender, EventArgs e) |
||
261 | { |
||
262 | var rows = dataGridView1.SelectedRows; |
||
263 | |||
264 | foreach (DataGridViewRow row in rows) |
||
265 | { |
||
266 | dataGridView1.Rows.RemoveAt(row.Index); |
||
267 | } |
||
268 | |||
269 | UpdatePreviewDataGridView(); |
||
270 | } |
||
271 | |||
272 | private void DataGridView1_DragEnter(object sender, DragEventArgs e) |
||
273 | { |
||
274 | if (e.Data.GetDataPresent(DataFormats.FileDrop)) |
||
275 | { |
||
276 | e.Effect = DragDropEffects.Copy; |
||
277 | return; |
||
278 | } |
||
279 | |||
280 | e.Effect = DragDropEffects.None; |
||
281 | } |
||
282 | |||
283 | private void DataGridView1_DragDrop(object sender, DragEventArgs e) |
||
284 | { |
||
285 | if (!e.Data.GetDataPresent(DataFormats.FileDrop)) |
||
286 | { |
||
287 | return; |
||
288 | } |
||
289 | |||
290 | var files = (string[]) e.Data.GetData(DataFormats.FileDrop); |
||
291 | if (files.Length == 0) |
||
292 | { |
||
293 | return; |
||
294 | } |
||
295 | |||
296 | var exist = new HashSet<string>( |
||
297 | dataGridView1.Rows.OfType<DataGridViewRow>().Select(row => (string) row.Cells[0].Tag)); |
||
298 | |||
299 | // Create row for each file. |
||
300 | foreach (var file in files) |
||
301 | { |
||
302 | var fileInfo = new FileInfo(file); |
||
303 | |||
304 | if (exist.Contains(fileInfo.FullName)) |
||
305 | { |
||
306 | continue; |
||
307 | } |
||
308 | |||
309 | var replaceRowIndex = dataGridView1.Rows.Add(); |
||
310 | var replaceRow = dataGridView1.Rows[replaceRowIndex]; |
||
311 | |||
312 | replaceRow.Cells[0].Value = fileInfo.Name; |
||
313 | replaceRow.Cells[0].Tag = fileInfo.FullName; |
||
314 | replaceRow.Cells[1].Value = fileInfo.CreationTime; |
||
315 | replaceRow.Cells[2].Value = fileInfo.LastAccessTime; |
||
316 | |||
317 | exist.Add(fileInfo.FullName); |
||
318 | } |
||
319 | |||
320 | UpdatePreviewDataGridView(); |
||
321 | |||
322 | // Sort added files. |
||
323 | dataGridView1.Sort(dataGridView1.Columns["Source"], ListSortDirection.Ascending); |
||
324 | dataGridView2.Sort(dataGridView2.Columns["Target"], ListSortDirection.Ascending); |
||
325 | } |
||
326 | |||
327 | private void DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) |
||
328 | { |
||
329 | } |
||
330 | |||
331 | private void CheckBox1_CheckedChanged(object sender, EventArgs e) |
||
332 | { |
||
333 | var checkBox = (CheckBox) sender; |
||
334 | switch (checkBox.CheckState) |
||
335 | { |
||
336 | case CheckState.Checked: |
||
337 | _regexOptions |= RegexOptions.IgnoreCase; |
||
338 | break; |
||
339 | case CheckState.Unchecked: |
||
340 | _regexOptions &= ~RegexOptions.IgnoreCase; |
||
341 | break; |
||
342 | } |
||
343 | } |
||
344 | |||
345 | private void DataGridView2_SelectionChanged(object sender, EventArgs e) |
||
346 | { |
||
347 | dataGridView2.ClearSelection(); |
||
348 | } |
||
349 | |||
350 | private async void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
||
351 | { |
||
352 | // Manually check for updates, this will not show a ui |
||
353 | var result = await _sparkle.CheckForUpdatesQuietly(); |
||
354 | var updates = result.Updates; |
||
355 | if (result.Status == UpdateStatus.UpdateAvailable) |
||
356 | { |
||
357 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
||
358 | _sparkle.ShowUpdateNeededUI(); |
||
359 | return; |
||
360 | } |
||
361 | |||
362 | MessageBox.Show("No updates available at this time.", "RegName", MessageBoxButtons.OK, |
||
363 | MessageBoxIcon.Asterisk, |
||
364 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
||
365 | } |
||
366 | |||
367 | private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) |
||
368 | { |
||
369 | var comboBox = (ComboBox) sender; |
||
370 | var mode = (string) comboBox.SelectedItem; |
||
371 | switch (mode) |
||
372 | { |
||
373 | case "Alpha Numeric": |
||
374 | _incrementMode = Helpers.IncrementMode.AlphaNumeric; |
||
375 | break; |
||
376 | case "Alpha": |
||
377 | _incrementMode = Helpers.IncrementMode.Alpha; |
||
378 | break; |
||
379 | case "Numeric": |
||
380 | _incrementMode = Helpers.IncrementMode.Numeric; |
||
381 | break; |
||
382 | } |
||
383 | |||
384 | UpdatePreviewDataGridView(); |
||
385 | } |
||
386 | |||
387 | #endregion |
||
388 | |||
389 | #region Private Methods |
||
390 | |||
391 | private static void RenameFiles(IReadOnlyList<FileRename> files, IProgress<RenameProgress> progress) |
||
392 | { |
||
393 | var count = files.Count; |
||
394 | |||
395 | for (var i = 0; i < count; ++i) |
||
396 | { |
||
397 | try |
||
398 | { |
||
399 | var regName = files[i].New; |
||
400 | var oldFileInfo = new FileInfo(files[i].Old); |
||
401 | |||
402 | var newName = Path.Combine(oldFileInfo.DirectoryName, regName); |
||
403 | |||
404 | File.Move(oldFileInfo.FullName, newName); |
||
405 | |||
406 | var newFileInfo = new FileInfo(newName); |
||
407 | |||
408 | progress.Report(new RenameProgressSuccess(i, oldFileInfo, newFileInfo)); |
||
409 | } |
||
410 | catch (Exception ex) |
||
411 | { |
||
412 | progress.Report(new RenameProgressFailure(i, ex)); |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | |||
417 | private void UpdatePreviewDataGridView() |
||
418 | { |
||
419 | // Clear all rows. |
||
420 | dataGridView2.Rows.Clear(); |
||
421 | |||
422 | var sequence = textBox3.Text; |
||
423 | foreach (DataGridViewRow replaceRow in dataGridView1.Rows) |
||
424 | { |
||
425 | var fileName = (string) replaceRow.Cells[0].Value; |
||
426 | |||
427 | // Add the row if it does not exist. |
||
428 | if (replaceRow.Index > dataGridView2.Rows.Count - 1) |
||
429 | { |
||
430 | var previewRowIndex = dataGridView2.Rows.Add(); |
||
431 | var previewRow = dataGridView2.Rows[previewRowIndex]; |
||
432 | |||
433 | previewRow.Cells[0].Value = replaceRow.Cells[0].Value; |
||
434 | previewRow.Cells[0].Tag = replaceRow.Cells[0].Tag; |
||
435 | } |
||
436 | |||
437 | var modifyRow = dataGridView2.Rows[replaceRow.Index]; |
||
438 | |||
439 | if (modifyRow.Cells[0].Value == null) |
||
440 | { |
||
441 | continue; |
||
442 | } |
||
443 | |||
444 | // Perform the regular expression replacement. |
||
445 | if (_expression != null) |
||
446 | { |
||
447 | fileName = _expression.Replace((string) modifyRow.Cells[0].Value, textBox1.Text); |
||
448 | } |
||
449 | |||
450 | if (sequence.Length != 0) |
||
451 | { |
||
452 | fileName = fileName.Replace("{seq}", sequence); |
||
453 | |||
454 | // Increment the sequence. |
||
455 | sequence = Helpers.Increment(sequence, _incrementMode); |
||
456 | } |
||
457 | |||
458 | modifyRow.Cells[0].Value = fileName; |
||
459 | } |
||
460 | } |
||
461 | |||
462 | #endregion |
||
463 | } |
||
464 | } |