Added some helper functions to make removing files and folders in a Snapshot object easier.

This commit is contained in:
Admin
2020-12-03 20:28:24 -06:00
parent 826d788d9f
commit 2d52886e87
6 changed files with 404 additions and 147 deletions
+82 -58
View File
@@ -37,10 +37,16 @@ namespace DataOrganizer
ProfileComboBox.SelectedIndexChanged += ProfileComboBox_SelectedIndexChanged;
RootFolderComboBox.SelectedIndexChanged += SelectedFolderComboBox_SelectedIndexChanged;
PrimaryFilesTreeView.NodeMouseClick += PrimaryFilesTreeView_NodeMouseClick;
FolderSelectButton.Click += FolderSelectButton_Click1;
ProfileComboBox.SelectedIndex = 0;
}
private void FolderSelectButton_Click1(object sender, EventArgs e)
{
WriteFilesToDb(GetActiveFolderGroup().Path);
}
private void PrimaryFilesTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(ActiveSnapshotForPrimaryTree.Files.ContainsKey(e.Node.Name))
@@ -48,7 +54,7 @@ namespace DataOrganizer
var file = ActiveSnapshotForPrimaryTree.Files[e.Node.Name];
FileDetailsNameLabel.Text = $"Name: {file.Name}";
FileDetailsFullPathLabel.Text = $"Full Path: {file.PathNoRoot}";
FileDetailsFullPathLabel.Text = $"Full Path: {file.FullPath}";
}
}
@@ -64,7 +70,9 @@ namespace DataOrganizer
PrimaryTreeNodes.Clear();
var files = db.GetFilesForProfileByFolderGroup(profile.Id, folderGroup.Id);
ActiveSnapshotForPrimaryTree = new Snapshot(files, profile, folderGroup);
ActiveSnapshotForPrimaryTree = new Snapshot(files, folderGroup);
if(ActiveSnapshotForPrimaryTree.Directories.ContainsKey(@"W:\Test\Amagi Brilliant Park\Season 1")) ActiveSnapshotForPrimaryTree.DeleteFolder(ActiveSnapshotForPrimaryTree.Directories[@"W:\Test\Amagi Brilliant Park\Season 1"]);
foreach(var folder in ActiveSnapshotForPrimaryTree.Root.Folders)
{
@@ -164,82 +172,98 @@ namespace DataOrganizer
private void ScanRootFolderButton_Click(object sender, EventArgs e)
{
var folderGroup = GetActiveFolderGroup();
var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length;
var newFiles = new List<FileInfo>();
var files = new List<FileInfo>();
var rootFolder = new DirectoryInfo(folderGroup.Path);
var pendingChanges = 0;
foreach (var dir in rootFolder.EnumerateDirectories()) newFiles.AddRange(ScanDirectory(dir));
foreach (var dir in rootFolder.EnumerateDirectories())
files.AddRange(ScanDirectory(dir));
foreach (var file in rootFolder.EnumerateFiles())
var snapShot = new Snapshot(files, folderGroup);
var newFiles = snapShot.Files.Values.Except(ActiveSnapshotForPrimaryTree.Files.Values);
var existingFiles = ActiveSnapshotForPrimaryTree.Files.Values.Except(snapShot.Files.Values);
var deletedFiles = new List<FileSnapshot>();
foreach(var existingFile in existingFiles)
{
if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
if (!snapShot.Files.ContainsKey(existingFile.FullPath))
{
newFiles.Add(file);
existingFile.Status = FileSnapshot.FileStatus.PendingDelete;
deletedFiles.Add(existingFile);
pendingChanges++;
}
else if (existingFile.ModifiedDate != snapShot.Files[existingFile.FullPath].ModifiedDate)
{
existingFile.ModifiedDate = snapShot.Files[existingFile.FullPath].ModifiedDate;
pendingChanges++;
}
}
foreach (var newFile in newFiles)
pendingChanges += newFiles.Count();
foreach(var f in newFiles)
{
var folders = newFile.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar });
var currentPath = folderGroup.Path;
// AddNewFileToPrimaryTreeNode(f);
}
for(var i = rootDepth; i < folders.Length; i++)
{
var previousPath = currentPath;
currentPath = Path.Combine(currentPath, folders[i]);
if(!PrimaryTreeNodes.ContainsKey(currentPath))
{
var node = new TreeNode(folders[i]) { ForeColor = Color.Blue, ToolTipText = currentPath };
PrimaryTreeNodes[currentPath] = node;
if (previousPath == folderGroup.Path)
{
PrimaryFilesTreeView.Nodes.Add(node);
}
else
{
PrimaryTreeNodes[previousPath].Nodes.Add(node);
}
}
}
//foreach (var file in rootFolder.EnumerateFiles())
//{
// if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
// {
// newFiles.Add(file);
// }
//}
//foreach (var newFile in newFiles)
//{
// var folders = newFile.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar });
// var currentPath = folderGroup.Path;
// for(var i = rootDepth; i < folders.Length; i++)
// {
// var previousPath = currentPath;
// currentPath = Path.Combine(currentPath, folders[i]);
// if(!PrimaryTreeNodes.ContainsKey(currentPath))
// {
// var node = new TreeNode(folders[i]) { ForeColor = Color.Blue, ToolTipText = currentPath };
// PrimaryTreeNodes[currentPath] = node;
// if (previousPath == folderGroup.Path)
// {
// PrimaryFilesTreeView.Nodes.Add(node);
// }
// else
// {
// PrimaryTreeNodes[previousPath].Nodes.Add(node);
// }
// }
// }
// PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue });
//}
//WriteFilesToDb(folderGroup.Path);
}
private void AddNewFileToPrimaryTreeNode(List<FileSnapshot> files)
{
foreach(var file in files)
{
PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue });
}
}
private List<FileInfo> ScanDirectory(DirectoryInfo dir)
{
var newFiles = new List<FileInfo>();
var files = new List<FileInfo>();
foreach (var d in dir.EnumerateDirectories())
{
UpdateStatusStrip($"Processing {d.FullName}");
newFiles.AddRange(ScanDirectory(d));
}
files.AddRange(dir.EnumerateFiles());
foreach(var file in dir.EnumerateFiles())
{
if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName))
{
newFiles.Add(file);
}
else
{
//TODO: check for modifications
}
}
foreach (var folder in dir.EnumerateDirectories())
files.AddRange(ScanDirectory(folder));
foreach(var file in ActiveSnapshotForPrimaryTree.Files)
{
UpdateStatusStrip($"Checking for the existance of {file.Value.Name}");
if(File.Exists(file.Key))
{
//TODO: mark this file as deleted. Also scan the folders.
}
}
return newFiles;
return files;
}
private void WriteFilesToDb(string root)
{