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