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