Repository navigation
Expand file tree
/
Copy pathfrmMain.cs
More file actions
845 lines (702 loc) · 31.8 KB
/
Copy pathfrmMain.cs
File metadata and controls
845 lines (702 loc) · 31.8 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FileSystemImage.FileSystem;
using FileSystemImage.InputForms;
using FileSystemImage.Utils;
using GeneralToolkitLib.Converters;
using FileSystemImage.DataModels;
using GeneralToolkitLib.DataTypes;
using GeneralToolkitLib.Hashing;
using Serilog;
using GeneralToolkitLib.Storage.Models;
using GeneralToolkitLib.Storage;
namespace FileSystemImage
{
public partial class FrmMain : Form
{
private string _currentFileName;
private FileSystemDrive _currentFileSystemDrive;
private readonly ILogger _logger;
public FrmMain()
{
if (this.DesignMode)
{
}
else
{
InitializeComponent();
}
FolderTreeView.Nodes.Clear();
// Serilog already configured and initialized at this stage in program.cs so
_logger = Log.Logger;
}
private void frmMain_Load(object sender, EventArgs e)
{
this.Text = AppConfigHelper.GetProductAndVersionString();
DirectoryInfoDataLabel.Text = "";
LoadAndSaveProgressInfoLabel.Text = "";
LoadAndSaveProgressBar.Visible = false;
ChecksumFileGenerationLabel.Visible = false;
InitFileListDataGridView();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
//Application.Exit();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form = new FrmCreateSysImage())
{
form.SetMainWindow(this);
form.ShowDialog(this);
}
}
public void LoadFileSystemDrive(FileSystemDrive fileSystemDrive)
{
_currentFileSystemDrive = fileSystemDrive;
FolderTreeView.Nodes.Clear();
if (FileSystemDriveIsAvailable)
{
FolderTreeView.Nodes.Add(_currentFileSystemDrive.DriveLetter);
TreeNode root = FolderTreeView.Nodes[0];
root.ImageIndex = 1;
root.SelectedImageIndex = 1;
root.StateImageIndex = 1;
root.Name = fileSystemDrive.DriveLetter;
root.Tag = new RootIdentifier();
foreach (FileSystemDirectory dir in _currentFileSystemDrive.DirectoryList)
{
if (dir.DirectoryList == null)
root.Nodes.Add(dir.Name);
else
{
var treeNodeChildArr = new TreeNode[dir.DirectoryList.Count];
for (int i = 0; i < treeNodeChildArr.Length; i++)
treeNodeChildArr[i] = new TreeNode(dir.DirectoryList[i].Name);
root.Nodes.Add(new TreeNode(dir.Name, treeNodeChildArr));
}
}
if (fileSystemDrive.RootFileList != null)
FileListDataGridView.DataSource = fileSystemDrive.RootFileList.ConvertAll(FileSystemFileWrapper.ConvertObject);
root.Expand();
}
MemoryHandler.RunGarbageCollect();
}
private void openToolStripMenuItem_Click(object sender, EventArgs eventArgs)
{
var passwordDialog = MemoryHandler.GetForm<FormGetPassword>();
using (var openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "FileSystemImage files (*.fsi)|*.fsi";
if (openFileDialog.ShowDialog() == DialogResult.OK && passwordDialog.ShowDialog(this) == DialogResult.OK)
{
string password = passwordDialog.PasswordString;
_currentFileSystemDrive = null;
_currentFileName = openFileDialog.FileName;
LoadAndSaveProgressInfoLabel.Text = "Opening file: " + openFileDialog.FileName;
LoadFileSystemImageFromFile(_currentFileName, password);
Log.Debug("Successful opened file: {FileName}", openFileDialog.FileName);
}
}
}
private void LoadFileSystemImageFromFile(string path, string password)
{
var storageManagerProgress = new Progress<StorageManagerProgress>();
LoadAndSaveProgressBar.Visible = true;
LoadAndSaveProgressInfoLabel.Visible = true;
openToolStripMenuItem.Enabled = false;
storageManagerProgress.ProgressChanged += (s, e) =>
{
LoadAndSaveProgressBar.Value = e.ProgressPercentage;
LoadAndSaveProgressInfoLabel.Text = e.Text;
};
Task.Run(async () =>
{
var settings = new StorageManagerSettings(true, Environment.ProcessorCount, true, password);
var storageManager = new StorageManager(settings);
_currentFileSystemDrive = await storageManager.DeserializeObjectFromFileAsync<FileSystemDrive>(path, storageManagerProgress);
Invoke(new EventHandler(HandleOpenFileComplete));
});
}
private void HandleOpenFileComplete(object sender, EventArgs eventArgs)
{
LoadAndSaveProgressBar.Visible = false;
LoadAndSaveProgressInfoLabel.Visible = false;
openToolStripMenuItem.Enabled = true;
if (_currentFileSystemDrive?.DirectoryList == null)
MessageBox.Show("Failed to open file!");
else
LoadFileSystemDrive(_currentFileSystemDrive);
MemoryHandler.RunGarbageCollect();
}
private void HandleSaveFileComplete(object sender, OutcomeEventArgs eventArgs)
{
LoadAndSaveProgressBar.Visible = false;
saveAsToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
LoadAndSaveProgressInfoLabel.Text = eventArgs.Successful ? "Successfuly saved file" : "Faild to save file";
Task.Run(async delegate
{
await Task.Delay(5000);
ResetLoadAndSaveProgress();
});
MemoryHandler.RunGarbageCollect();
}
private void ResetLoadAndSaveProgress()
{
LoadAndSaveProgressInfoLabel.Text = "";
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_currentFileSystemDrive == null)
{
MessageBox.Show("Nothing to save!");
return;
}
var setPasswordDialog = MemoryHandler.GetForm<FormSetPassword>();
if (_currentFileName == null)
{
bool fileSelected = false;
using (var saveFileDialog = new SaveFileDialog() { Filter = "FileSystemImage files (*.fsi)|*.fsi" })
{
fileSelected = saveFileDialog.ShowDialog() == DialogResult.OK;
_currentFileName = saveFileDialog.FileName;
}
if (!fileSelected)
{
return;
}
}
if (_currentFileName != null && setPasswordDialog.ShowDialog(this) == DialogResult.OK)
{
string password = setPasswordDialog.VerifiedPassword;
SaveFileSystemImageToFile(_currentFileSystemDrive, _currentFileName, password);
}
MemoryHandler.RunGarbageCollect();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var setPasswordDialog = MemoryHandler.GetForm<FormSetPassword>();
string fileName;
if (_currentFileSystemDrive == null)
{
MessageBox.Show("Nothing to save!");
return;
}
using (var saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "FileSystemImage files (*.fsi)|*.fsi";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog.FileName;
_currentFileName = saveFileDialog.FileName;
}
else
return;
};
if (_currentFileName != null && setPasswordDialog.ShowDialog(this) == DialogResult.OK)
{
string password = setPasswordDialog.VerifiedPassword;
SaveFileSystemImageToFile(_currentFileSystemDrive, _currentFileName, password);
Log.Debug("Successfully Saved file: {FileName}", fileName);
}
MemoryHandler.RunGarbageCollect();
}
private void SaveFileSystemImageToFile(FileSystemDrive fileSystemDrive, string path, string password)
{
var storageManagerProgress = new Progress<StorageManagerProgress>();
LoadAndSaveProgressBar.Visible = true;
storageManagerProgress.ProgressChanged += (s, e) =>
{
LoadAndSaveProgressBar.Value = e.ProgressPercentage;
LoadAndSaveProgressInfoLabel.Text = e.Text;
};
saveAsToolStripMenuItem.Enabled = false;
saveToolStripMenuItem.Enabled = false;
Task.Run(async () =>
{
var settings = new StorageManagerSettings(true, Environment.ProcessorCount, true, password);
var storageManager = new StorageManager(settings);
bool success = await storageManager.SerializeObjectToFileAsync(fileSystemDrive, path, storageManagerProgress);
OutcomeEventArgs outcomeEventArgs = success ? OutcomeEventArgs.Success : OutcomeEventArgs.Failure;
SaveFileEventHandler onSaveComplete = HandleSaveFileComplete;
BeginInvoke(onSaveComplete, this, outcomeEventArgs);
});
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
var aboutBox = MemoryHandler.GetForm<FormAboutBox>();
aboutBox.ShowDialog(this);
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_currentFileSystemDrive == null)
{
MessageBox.Show("Nothing to clear.", "No data loaded! ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
FileSystemDrive current = _currentFileSystemDrive;
FolderTreeView.Nodes.Clear();
FileListDataGridView.DataSource = null;
_currentFileSystemDrive = null;
_currentFileName = null;
DirectoryInfoDataLabel.Text = "";
MemoryHandler.DealocateObjectStructure<FileSystemDrive>(ref current);
MemoryHandler.RunGarbageCollect();
}
private void FolderTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
}
private void FolderTreeView_AfterExpand(object sender, TreeViewEventArgs e)
{
//Update child nodes
TreeNode selectedNode = e.Node;
var nodeTraversalStack = new Stack<string>();
TreeNode rootNode = selectedNode;
var directoryList = _currentFileSystemDrive.DirectoryList;
nodeTraversalStack.Push(rootNode.Text);
while (rootNode.Parent != null)
{
rootNode = rootNode.Parent;
nodeTraversalStack.Push(rootNode.Text);
}
while (nodeTraversalStack.Count > 0)
{
string currentItem = nodeTraversalStack.Pop();
for (int i = 0; i < directoryList.Count; i++)
{
FileSystemDirectory dir = directoryList[i];
if (dir.Name == currentItem)
{
directoryList = dir.DirectoryList;
break;
}
}
}
if (directoryList == null)
return;
TreeNode firstNode = selectedNode.FirstNode;
while (firstNode != null)
{
if (firstNode.Nodes.Count == 0)
{
FileSystemDirectory subDir = directoryList.SingleOrDefault(d => d.Name == firstNode.Text);
if (subDir?.DirectoryList != null)
{
foreach (FileSystemDirectory fsd in subDir.DirectoryList)
firstNode.Nodes.Add(new TreeNode(fsd.Name));
}
}
firstNode = firstNode.NextNode;
}
}
private void FolderTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
//Update file list
TreeNode selectedNode = e.Node;
var fileList = GetFileSystemFileListFromNode(selectedNode, true);
if (fileList == null && _currentFileSystemDrive.RootFileList != null && (_currentFileSystemDrive != null && selectedNode.Parent == null))
fileList = _currentFileSystemDrive.RootFileList.ConvertAll(FileSystemFileWrapper.ConvertObject);
FileListDataGridView.DataSource = fileList;
FileListDataGridView.Refresh();
FileListDataGridView.AutoResizeColumns();
}
private List<FileSystemFileWrapper> GetFileSystemFileListFromNode(TreeNode selectedNode, bool setStatusBarInfo)
{
var directoryList = _currentFileSystemDrive.DirectoryList;
List<FileSystemFileWrapper> fileSystemList = null;
var nodeTraversalStack = new Stack<string>();
TreeNode rootNode = selectedNode;
nodeTraversalStack.Push(rootNode.Text);
//Root node
if (rootNode.Parent == null)
{
DirectoryInfoDataLabel.Text = GetRootDirectoryData(_currentFileSystemDrive);
if (_currentFileSystemDrive.RootFileList == null)
_currentFileSystemDrive.RootFileList = new List<FileSystemFile>();
return _currentFileSystemDrive.RootFileList.ConvertAll(FileSystemFileWrapper.ConvertObject);
}
while (rootNode.Parent != null)
{
rootNode = rootNode.Parent;
nodeTraversalStack.Push(rootNode.Text);
}
nodeTraversalStack.Pop();
while (nodeTraversalStack.Count > 0)
{
string currentItem = nodeTraversalStack.Pop();
for (int i = 0; i < directoryList.Count; i++)
{
FileSystemDirectory dir = directoryList[i];
if (dir.Name == currentItem)
{
if (nodeTraversalStack.Count == 0 && dir.FileList != null)
fileSystemList = dir.FileList.ConvertAll(FileSystemFileWrapper.ConvertObject);
directoryList = dir.DirectoryList;
if (setStatusBarInfo)
DirectoryInfoDataLabel.Text = GetFileSystemDirectoryData(dir);
break;
}
}
}
return fileSystemList;
}
private string GetFileSystemDirectoryData(FileSystemDirectory dir)
{
return $"Sub Directories: {dir.SubDirectoriesTotal} | Files: {dir.FilesTotal} | Total file size: {GeneralConverters.FormatFileSizeToString(dir.FileSizeTotal, 2)}";
}
private string GetRootDirectoryData(FileSystemDrive fileSystemDrive)
{
long directories = 0;
long files = fileSystemDrive.RootFileList?.Count ?? 0;
long fileSizeTotal = fileSystemDrive.RootFileList?.Sum(f => f.FileSize) ?? 0;
foreach (FileSystemDirectory fileSystemDirectory in fileSystemDrive.DirectoryList)
{
directories += fileSystemDirectory.SubDirectoriesTotal + 1;
files += fileSystemDirectory.FilesTotal;
fileSizeTotal += fileSystemDirectory.FileSizeTotal;
}
return $"Directories: {directories} | Files: {files} | Total file size: {GeneralConverters.FormatFileSizeToString(fileSizeTotal, 2)}";
}
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
TreeNode currentExpandedNode = FolderTreeView.SelectedNode;
if (currentExpandedNode == null)
{
MessageBox.Show("No selected path found");
return;
}
if (currentExpandedNode.Tag is RootIdentifier && MessageBox.Show(this, "Update entire drive", "Are you sure you want to update the entire drive?", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string path = BuildPathFromTreeNode(currentExpandedNode);
FileSystemDirectory fileSystemDirectory = GetFileSystemDirectoryFromPath(path);
try
{
DoPartialUpdateOnlyFiles(fileSystemDirectory);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
Log.Error(ex, "DoPartialUpdateOnlyFiles failed");
}
}
private void DoPartialUpdateOnlyFiles(FileSystemDirectory targetDirectory)
{
var storageManagerProgress = new Progress<StorageManagerProgress>();
LoadAndSaveProgressBar.Visible = true;
openToolStripMenuItem.Enabled = false;
storageManagerProgress.ProgressChanged += (s, e) =>
{
LoadAndSaveProgressBar.Value = e.ProgressPercentage;
LoadAndSaveProgressInfoLabel.Text = e.Text;
};
Task.Run(async () =>
{
_currentFileSystemDrive = await FileUtils.UpdateFolderAsync(_currentFileSystemDrive, targetDirectory, storageManagerProgress);
Invoke(new EventHandler(HandleOpenFileComplete));
});
}
private FileSystemDirectory GetFileSystemDirectoryFromPath(string path)
{
FileSystemDirectory fsDirectory = null;
var directoryList = _currentFileSystemDrive.DirectoryList;
var pathSegmentArray = path.Split('\\');
foreach (string dir in pathSegmentArray)
{
if (string.IsNullOrEmpty(dir) || dir.EndsWith(":"))
continue;
fsDirectory = directoryList.FirstOrDefault(d => d.Name == dir);
if (fsDirectory == null)
return null;
directoryList = fsDirectory.DirectoryList;
}
return fsDirectory;
}
private string BuildPathFromTreeNode(TreeNode node)
{
var sb = new StringBuilder();
while (node != null)
{
sb.Append("\\" + node.Text.Trim('\\'));
node = node.Parent;
}
string path = sb.ToString().Trim('\\');
path = StringManipulation.ReverseBySeparator(path, "\\");
return path;
}
private struct RootIdentifier
{
public string Guid => "6F99E272-FD0A-4ED3-B33D-B41716886301";
}
#region FileListDataGridView Custom Render
private const int CustomContentHeight = 4;
private readonly Color _gridViewSelectionBorderColor = ColorTranslator.FromHtml("#7da2ce");
private readonly Color _gridViewGradientBackgroundColorStart = ColorTranslator.FromHtml("#b2e1ff");
private readonly Color _gridViewGradientBackgroundColorStop = ColorTranslator.FromHtml("#66b6fc");
private void InitFileListDataGridView()
{
//Padding newPadding = new Padding(0, 1, 0, CUSTOM_CONTENT_HEIGHT);
//this.FileListDataGridView.RowTemplate.DefaultCellStyle.Padding = newPadding;
FileListDataGridView.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent;
FileListDataGridView.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.Black;
FileListDataGridView.RowTemplate.Height += CustomContentHeight;
FileListDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None;
FileListDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
FileListDataGridView.RowPrePaint += FileListDataGridView_RowPrePaint;
FileListDataGridView.RowPostPaint += FileListDataGridView_RowPostPaint;
}
private void FileListDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts &= ~DataGridViewPaintParts.Focus;
if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
{
// Calculate the bounds of the row.
var rowBounds = new Rectangle(0, e.RowBounds.Top, FileListDataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - FileListDataGridView.HorizontalScrollingOffset + 1, e.RowBounds.Height);
// Paint the custom selection background.
using (Brush backbrush = new LinearGradientBrush(rowBounds, _gridViewGradientBackgroundColorStart, _gridViewGradientBackgroundColorStop, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(backbrush, rowBounds);
var p = new Pen(backbrush, 1) { Color = _gridViewSelectionBorderColor };
e.Graphics.DrawRectangle(p, rowBounds);
}
}
}
private void FileListDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
SolidBrush forebrush = null;
try
{
forebrush = (e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected ? new SolidBrush(e.InheritedRowStyle.SelectionForeColor) : new SolidBrush(e.InheritedRowStyle.ForeColor);
}
finally
{
forebrush?.Dispose();
}
}
#endregion
#region Menu Events
private void driveInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FileSystemDriveIsAvailable)
{
var driveInfoForm = MemoryHandler.GetForm<frmDriveInfo>();
driveInfoForm.SetFileSystemDrive(_currentFileSystemDrive);
driveInfoForm.ShowDialog(this);
}
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!FileSystemDriveIsAvailable)
{
MessageBox.Show("Please load a file system image first");
return;
}
var searchDialog = MemoryHandler.GetForm<FrmSearch>();
searchDialog.SetFileSystemDrive(_currentFileSystemDrive);
searchDialog.ShowDialog(this);
}
private bool FileSystemDriveIsAvailable => _currentFileSystemDrive != null;
private void FileListDataGridView_MouseUp(object sender, MouseEventArgs e)
{
if (FileListDataGridView.DataSource == null)
{
return;
}
if (e.Button == MouseButtons.Right)
{
if (sender is Control control)
{
contextMenuStripFile.Show(control, e.X, e.Y);
}
}
}
private void copyFileMenuItem_Click(object sender, EventArgs e)
{
if (FileListDataGridView.SelectedRows.Count == 0) return;
var paths = new StringCollection();
foreach (DataGridViewRow selectedRow in FileListDataGridView.SelectedRows)
{
if (selectedRow.DataBoundItem is FileSystemFileWrapper selectedFile && selectedFile.FullPath != null)
{
paths.Add(selectedFile.FullPath);
}
}
Clipboard.Clear();
Clipboard.SetFileDropList(paths);
}
private void copyFilePathMenuItem_Click(object sender, EventArgs e)
{
if (FileListDataGridView.SelectedRows.Count == 0) return;
var sb = new StringBuilder();
foreach (DataGridViewRow selectedRow in FileListDataGridView.SelectedRows)
{
if (selectedRow.DataBoundItem is FileSystemFileWrapper selectedFile && selectedFile.FullPath != null)
{
sb.AppendLine(selectedFile.FullPath);
}
}
Clipboard.Clear();
Clipboard.SetText(sb.ToString());
}
private void selectAllMenuItem_Click(object sender, EventArgs e)
{
FileListDataGridView.SelectAll();
}
private void createSFVFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FileListDataGridView.SelectedRows.Count == 0) return;
if (!(FileListDataGridView.SelectedRows[0].DataBoundItem is FileSystemFileWrapper fileSystemFileWrapper))
{
MessageBox.Show("Unexprected error in createSFVFile. DataBoundItem is " + FileListDataGridView.SelectedRows[0].DataBoundItem.GetType());
return;
}
// Root folder?
string sfvFilename = fileSystemFileWrapper.FullPath ?? _currentFileSystemDrive.DriveLetter + fileSystemFileWrapper.Name;
int fileExtensionDelimiter = sfvFilename.LastIndexOf('.');
if (fileExtensionDelimiter > 0)
{
sfvFilename = sfvFilename.Substring(0, fileExtensionDelimiter) + ".sfv";
}
else
{
sfvFilename += ".sfv";
}
LoadAndSaveProgressBar.Visible = true;
ChecksumFileGenerationLabel.Visible = true;
Action<ChecksumProgress> checksumProgressLamdaFunction = progress =>
{
ChecksumFileGenerationLabel.Text = GeneralConverters.GetFileNameFromPath(progress.Text).PadLeft(5);
LoadAndSaveProgressBar.Value = progress.TotalProgress;
if (progress.Completed)
{
LoadAndSaveProgressBar.Visible = false;
ChecksumFileGenerationLabel.Visible = false;
MessageBox.Show("Successfully created SFV file");
}
};
var checksumFileGenerator = new ChecksumFileGenerator(Application.ProductName + " " + Application.ProductVersion, new Progress<ChecksumProgress>(checksumProgressLamdaFunction));
var paths = new StringCollection();
foreach (DataGridViewRow selectedRow in FileListDataGridView.SelectedRows)
{
if (selectedRow.DataBoundItem is FileSystemFileWrapper selectedFile && selectedFile.FullPath != null)
{
paths.Add(selectedFile.FullPath);
}
}
checksumFileGenerator.GenrateSFVFileAsync(paths, sfvFilename);
}
private void createMD5FileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FileListDataGridView.SelectedRows.Count == 0) return;
if (!(FileListDataGridView.SelectedRows[0].DataBoundItem is FileSystemFileWrapper fileSystemFileWrapper))
{
MessageBox.Show("Unexprected error in createMD5File. DataBoundItem is " + FileListDataGridView.SelectedRows[0].DataBoundItem.GetType());
return;
}
// Root folder?
string sfvFilename = fileSystemFileWrapper.FullPath ?? _currentFileSystemDrive.DriveLetter + fileSystemFileWrapper.Name;
int fileExtensionDelimiter = sfvFilename.LastIndexOf('.');
if (fileExtensionDelimiter > 0)
{
sfvFilename = sfvFilename.Substring(0, fileExtensionDelimiter) + ".md5";
}
else
{
sfvFilename += ".md5";
}
LoadAndSaveProgressBar.Visible = true;
ChecksumFileGenerationLabel.Visible = true;
Action<ChecksumProgress> checksumProgressLamdaFunction = progress =>
{
ChecksumFileGenerationLabel.Text = GeneralConverters.GetFileNameFromPath(progress.Text).PadLeft(5);
LoadAndSaveProgressBar.Value = progress.TotalProgress;
if (progress.Completed)
{
LoadAndSaveProgressBar.Visible = false;
ChecksumFileGenerationLabel.Visible = false;
MessageBox.Show("Successfully created MD5 file");
}
};
var checksumFileGenerator = new ChecksumFileGenerator(Application.ProductName + " " + Application.ProductVersion, new Progress<ChecksumProgress>(checksumProgressLamdaFunction));
var paths = new StringCollection();
foreach (DataGridViewRow selectedRow in FileListDataGridView.SelectedRows)
{
if (selectedRow.DataBoundItem is FileSystemFileWrapper selectedFile && selectedFile.FullPath != null)
{
paths.Add(selectedFile.FullPath);
}
}
checksumFileGenerator.GenrateMD5FileAsync(paths, sfvFilename);
}
private void FolderTreeView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && FolderTreeView.Nodes.Count > 0)
{
var selectedItem = FolderTreeView.SelectedNode;
if (selectedItem != null && sender is Control control)
{
contextMenuStripFolder.Show(control, e.X, e.Y);
}
}
}
#endregion
#region Folder Context Menu Events
private void TsMenuItemUpdateFolderRecursive_Click(object sender, EventArgs e)
{
}
private void TsMenuItemUpdateFolder_Click(object sender, EventArgs e)
{
}
private void TsMenuItemShowInExplorer_Click(object sender, EventArgs e)
{
var selectedItem = FolderTreeView.SelectedNode;
if (selectedItem != null)
{
string fullPath = FileSystemHelper.GetCorrectFullPath(selectedItem);
Process.Start("explorer", fullPath);
}
}
private void TsMenuItemFolderProperties_Click(object sender, EventArgs e)
{
var selectedItem = FolderTreeView.SelectedNode;
if (selectedItem != null)
{
FormFolderProperties folderProperties = MemoryHandler.GetForm<FormFolderProperties>();
folderProperties.TreeNodeInit(selectedItem);
folderProperties.ShowDialog(this);
}
}
private void TsMenuItemCopyPath_Click(object sender, EventArgs e)
{
var selectedItem = FolderTreeView.SelectedNode;
if (selectedItem != null)
{
string fullPath = FileSystemHelper.GetCorrectFullPath(selectedItem);
Clipboard.Clear();
Clipboard.SetText(fullPath, TextDataFormat.Text);
}
}
private void TsMenuItemDeleteFolder_Click(object sender, EventArgs e)
{
}
#endregion
private async void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (_currentFileSystemDrive != null)
{
MemoryHandler.DealocateObjectStructure<FileSystemDrive>(ref _currentFileSystemDrive);
}
await MemoryHandler.DisposeAnyActiveForm();
e.Cancel = false;
}
}
}