From 2d52886e8728681f3b0724d0032bd78c232f12c8 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 3 Dec 2020 20:28:24 -0600 Subject: [PATCH] Added some helper functions to make removing files and folders in a Snapshot object easier. --- .vs/DataOrganizer/v16/.suo | Bin 95744 -> 145920 bytes DataOrganizer/DB.cs | 29 ++- DataOrganizer/FileViewer.cs | 140 +++++++----- DataOrganizer/MainForm.Designer.cs | 44 ++-- DataOrganizer/MainForm.cs | 6 + DataOrganizer/Snapshot.cs | 332 ++++++++++++++++++++++------- 6 files changed, 404 insertions(+), 147 deletions(-) diff --git a/.vs/DataOrganizer/v16/.suo b/.vs/DataOrganizer/v16/.suo index d009185a03b12d9523ca60492277ac0b8d57887c..2e11118071106317164bbdf59704dd84444f8aab 100644 GIT binary patch delta 5642 zcmZp8!`kqLV}gMu69W_oFfhn6Fff4l|Nj5~4-#c&U|`T?U|`T=U|`Ui9LOrZvEUCQ z?}Gf+swXF}F)D$SOs-_AW}H3wD^oqA*5p#=dR`9(28IvpASnh0uE~rn()Cgd3=AL> z|NsC0Uxa~yL5_iefuDhafsKKIfro*CL70JofrEj8L5P8YL5z`sL7ahsfscWKL4bjQ z0i;KPfq~)g|Ns9%Sek)>K@=(uk^@-;qPam9GBPlzBgsiHg7tI#pAQu@oXp4~KCys9 z!~`k;(rL=Tz+lF}z%cm+i;kod0|SFI0|SE#0|SFA0|SE_0|SFQ0|SG{WJXr$%@(XC zjJyqd-}Zrg?+LOI-S>vbz6Y5O!kkd|gTg=`$z~8=0~D$g3phkUp67x%7^DY`jX;vI z$_xw)EKqGSP?`a%QVkk(Jd6wsI#4-zC=CiO6$S7 zdR9A7nw|VpxEhz-=AW|FjEu83m&*TVWYnBos#wpd!N9-(N{I}U3&n&d3y29!ZV+Rc ztRiN@C=L=)1qCxGFMv`iC^$hWP?3Rw0hHoF(E$op)yaio;vAsB1O@!$4Pqi9px^?9 zHi$+_Rx*=I!~|quNed(iOIjc{2v1fJe=td^gwbeosnmZ)M)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uSAYr~;pyx7nFOZ`1~S@A|B%SYI{l#%)1m1N0gSfO1etmiKW_PHwCiK+gdG8S z@mI4iOQ`?QTIUq7n7bp!{ny1+GkCy8EI%N~#5h^eMSA)LUq*-NyAv5r8F{w*1v2_G zGH#vT7|U2cJwJwV!S(~9Obv|FFUBxRPS=QKbQVkk2Lvc^lnzvb*jpW#gc-LVkYH+H znl2d4=rnzLG^4YiB}fGrs~reoU|_JAUK7ihHQhmm>A-Zx|BNbN3#Z#BFy0je)#qT9 z3_tegGB7YGPuERkyuAG%C({%r5eA0h%=EliS7)o3(Bjmh;uuFphUta>8Fi+gNnjLO zmchsfj_v7&v5fN5^Ld!Wr$10)V&jN759$UmFiZ_(vY38=pGjltMka}AnT!sTC;8@0 zpOeqX!&o=nkd-NWvbL|#_8-hl98A+4ESL=C8A=(77?K%M8DbcU7*ZK_OB&e4sqD>r zmT~g`?#X-Yr#td98BEJ(lws_dTo{@F$M$JEt)*c_?o5%$O-C_$A8f;`HicGqRnUJFbeZ;Z(P`Svy7OyY=M-;JDmg!^Gsm iI6aAlNoBf00V9`C8-p{06+;X|C_^zrDnk)NF#`Z6RRmxF diff --git a/DataOrganizer/DB.cs b/DataOrganizer/DB.cs index 7117f8f..c59793a 100644 --- a/DataOrganizer/DB.cs +++ b/DataOrganizer/DB.cs @@ -24,13 +24,18 @@ namespace DataOrganizer ConnectionString = $"Data Source={DatabasePath}"; - if (!Directory.Exists(new FileInfo(DatabasePath).Directory.FullName)) + if (!File.Exists(DatabasePath)) { Directory.CreateDirectory(new FileInfo(DatabasePath).Directory.FullName); CreateDatabase(); } } + public void DeleteDatabase() + { + if (File.Exists(DatabasePath)) File.Delete(DatabasePath); + } + private void CreateDatabase() { //TODO: this is tmp @@ -345,6 +350,26 @@ namespace DataOrganizer } } + public string GetRootPathById(int id) + { + var query = @"SELECT path FROM folder_root WHERE root_id = @ID"; + + using (var connection = new SqliteConnection(ConnectionString)) + { + using (var command = new SqliteCommand(query, connection)) + { + command.Parameters.AddWithValue("ID", id); + connection.Open(); + + var reader = command.ExecuteReader(); + + reader.Read(); + + return reader["path"].ToString(); + } + } + } + public bool UpdateNamingPatterById(int id, string newPattern) { var query = @"UPDATE main.naming_pattern SET regex = @pattern WHERE pattern_id = @ID"; @@ -417,7 +442,7 @@ namespace DataOrganizer { foreach (var file in files) { - command.Parameters.AddWithValue("FullPath", file.PathNoRoot.Remove(0, rootLength)); + command.Parameters.AddWithValue("FullPath", file.PathNoRoot.Remove(0, rootLength + 1)); //Remove the root and the trailing slash that the root folder would have. command.Parameters.AddWithValue("Name", file.Name); command.Parameters.AddWithValue("Size", file.Size); command.Parameters.AddWithValue("DateCreated", file.CreationDate); diff --git a/DataOrganizer/FileViewer.cs b/DataOrganizer/FileViewer.cs index 4306e82..3787f83 100644 --- a/DataOrganizer/FileViewer.cs +++ b/DataOrganizer/FileViewer.cs @@ -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(); + var files = new List(); 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(); + + 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 files) + { + foreach(var file in files) + { - PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue }); } } private List ScanDirectory(DirectoryInfo dir) { - var newFiles = new List(); + var files = new List(); - 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) { diff --git a/DataOrganizer/MainForm.Designer.cs b/DataOrganizer/MainForm.Designer.cs index b893689..f6833c8 100644 --- a/DataOrganizer/MainForm.Designer.cs +++ b/DataOrganizer/MainForm.Designer.cs @@ -30,9 +30,11 @@ { this.MainMenu = new System.Windows.Forms.MenuStrip(); this.FileMainMenu = new System.Windows.Forms.ToolStripMenuItem(); - this.MainLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); this.ManageProfilesFileMainMenu = new System.Windows.Forms.ToolStripMenuItem(); this.FileManagerFileMainMenu = new System.Windows.Forms.ToolStripMenuItem(); + this.MainLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); + this.debugToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.deleteDBToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.MainMenu.SuspendLayout(); this.SuspendLayout(); // @@ -41,7 +43,8 @@ this.MainMenu.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2); this.MainMenu.ImageScalingSize = new System.Drawing.Size(24, 24); this.MainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.FileMainMenu}); + this.FileMainMenu, + this.debugToolStripMenuItem}); this.MainMenu.Location = new System.Drawing.Point(0, 0); this.MainMenu.Name = "MainMenu"; this.MainMenu.Size = new System.Drawing.Size(800, 33); @@ -57,6 +60,20 @@ this.FileMainMenu.Size = new System.Drawing.Size(54, 29); this.FileMainMenu.Text = "&File"; // + // ManageProfilesFileMainMenu + // + this.ManageProfilesFileMainMenu.Name = "ManageProfilesFileMainMenu"; + this.ManageProfilesFileMainMenu.Size = new System.Drawing.Size(270, 34); + this.ManageProfilesFileMainMenu.Text = "Manage &Profiles"; + this.ManageProfilesFileMainMenu.Click += new System.EventHandler(this.ManageProfilesFileMainMenu_Click); + // + // FileManagerFileMainMenu + // + this.FileManagerFileMainMenu.Name = "FileManagerFileMainMenu"; + this.FileManagerFileMainMenu.Size = new System.Drawing.Size(270, 34); + this.FileManagerFileMainMenu.Text = "&File Manager"; + this.FileManagerFileMainMenu.Click += new System.EventHandler(this.FileManagerFileMainMenu_Click); + // // MainLayoutPanel // this.MainLayoutPanel.ColumnCount = 2; @@ -71,19 +88,20 @@ this.MainLayoutPanel.Size = new System.Drawing.Size(800, 791); this.MainLayoutPanel.TabIndex = 1; // - // ManageProfilesFileMainMenu + // debugToolStripMenuItem // - this.ManageProfilesFileMainMenu.Name = "ManageProfilesFileMainMenu"; - this.ManageProfilesFileMainMenu.Size = new System.Drawing.Size(270, 34); - this.ManageProfilesFileMainMenu.Text = "Manage &Profiles"; - this.ManageProfilesFileMainMenu.Click += new System.EventHandler(this.ManageProfilesFileMainMenu_Click); + this.debugToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.deleteDBToolStripMenuItem}); + this.debugToolStripMenuItem.Name = "debugToolStripMenuItem"; + this.debugToolStripMenuItem.Size = new System.Drawing.Size(82, 29); + this.debugToolStripMenuItem.Text = "&Debug"; // - // FileManagerFileMainMenu + // deleteDBToolStripMenuItem // - this.FileManagerFileMainMenu.Name = "FileManagerFileMainMenu"; - this.FileManagerFileMainMenu.Size = new System.Drawing.Size(270, 34); - this.FileManagerFileMainMenu.Text = "&File Manager"; - this.FileManagerFileMainMenu.Click += new System.EventHandler(this.FileManagerFileMainMenu_Click); + this.deleteDBToolStripMenuItem.Name = "deleteDBToolStripMenuItem"; + this.deleteDBToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.deleteDBToolStripMenuItem.Text = "&Delete DB"; + this.deleteDBToolStripMenuItem.Click += new System.EventHandler(this.deleteDBToolStripMenuItem_Click); // // MainForm // @@ -110,6 +128,8 @@ private System.Windows.Forms.TableLayoutPanel MainLayoutPanel; private System.Windows.Forms.ToolStripMenuItem ManageProfilesFileMainMenu; private System.Windows.Forms.ToolStripMenuItem FileManagerFileMainMenu; + private System.Windows.Forms.ToolStripMenuItem debugToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem deleteDBToolStripMenuItem; } } diff --git a/DataOrganizer/MainForm.cs b/DataOrganizer/MainForm.cs index 86e3728..13e08a1 100644 --- a/DataOrganizer/MainForm.cs +++ b/DataOrganizer/MainForm.cs @@ -28,5 +28,11 @@ namespace DataOrganizer var form = new FileViewer(); form.ShowDialog(); } + + private void deleteDBToolStripMenuItem_Click(object sender, EventArgs e) + { + var db = new DB(); + db.DeleteDatabase(); + } } } diff --git a/DataOrganizer/Snapshot.cs b/DataOrganizer/Snapshot.cs index 88413b4..d66b1e3 100644 --- a/DataOrganizer/Snapshot.cs +++ b/DataOrganizer/Snapshot.cs @@ -11,60 +11,32 @@ namespace DataOrganizer class Snapshot { public Folder Root { get; private set; } - public Dictionary Files { get; private set; } = new Dictionary(); + public Dictionary Files { get; private set; } = new Dictionary(); public Dictionary Directories { get; private set; } = new Dictionary(); public Dictionary FileDuplicates { get; private set; } = new Dictionary(); //Contains files associated with particular, first level folders. //So if the root folder group is X:\Anime and we have a folder path of x:\Anime\Death Note, then indexing this dictonary //with the text "Death Note" would return all files in that folder. - public Dictionary> OrganizationFolders { get; private set; } = new Dictionary>(); + public Dictionary> OrganizationFolders { get; private set; } = new Dictionary>(); - public Snapshot(List fileRecords, DB.Profile profile, DB.FolderGroup folderGroup) + public Snapshot(List fileRecords, DB.FolderGroup folderGroup) { - Root = new Folder(folderGroup.Name, folderGroup.Path); + Root = new Folder(folderGroup.Name, folderGroup.Path, null); Directories.Add(Root.FullPath, Root); - if(folderGroup.Type == DB.OrganizationType.SingleLevel) + if (folderGroup.Type == DB.OrganizationType.SingleLevel) ProcessFilesAsOneLevel(fileRecords, folderGroup); + //else if (folderGroup.Type == DB.OrganizationType.Flat) + // ProcessFilesAsFlat(fileRecords, folderGroup); + } - //var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length; + public Snapshot(List files, DB.FolderGroup folderGroup) + { + Root = new Folder(folderGroup.Name, folderGroup.Path, null); + Directories.Add(Root.FullPath, Root); - //foreach (var file in fileRecords) - //{ - // var folders = file.PathNoRoot.Split(new char[] { Path.DirectorySeparatorChar }); - // var currentPath = folderGroup.Path; - - // if (folders.Length == rootDepth + 1) - // { - // Root.AddFile(file); - // Files.Add(file.PathNoRoot, file); - // continue; - // } - - // for (var i = rootDepth; i < folders.Length - 1; i++) - // { - // string previousPath = currentPath; - // currentPath = Path.Combine(currentPath, folders[i]); - - // if (!Directories.ContainsKey(currentPath)) - // { - // var newFolder = new Folder(folders[i], currentPath); - // Directories[previousPath].AddDirectory(newFolder); - // Directories.Add(currentPath, newFolder); - // } - // } - - // Directories[currentPath].AddFile(file); - - // var duplicates = Files.Values.Where(x => x.Size == file.Size && x.Id != file.Id); - - // Files.Add(file.PathNoRoot, file); - - // foreach (var duplicate in duplicates) - // { - // FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List { duplicate } }); - // } - //} + if (folderGroup.Type == DB.OrganizationType.SingleLevel) + ProcessFilesAsOneLevel(files, folderGroup); } private void ProcessFilesAsOneLevel(List files, DB.FolderGroup folderGroup) @@ -79,9 +51,9 @@ namespace DataOrganizer if(firstLevelFolder == null || firstLevelFolder.Name != folders[0]) { - firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0])); + firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]), Root); Directories.Add(orgFolder, firstLevelFolder); - OrganizationFolders.Add(orgFolder, new List()); + OrganizationFolders.Add(orgFolder, new List()); Root.AddDirectory(firstLevelFolder); } @@ -91,33 +63,117 @@ namespace DataOrganizer currentPath = Path.Combine(currentPath, folders[i]); if(!Directories.ContainsKey(currentPath)) { - var newFolder = new Folder(folders[i], currentPath); + var newFolder = new Folder(folders[i], currentPath, Directories[parentFolder]); Directories.Add(currentPath, newFolder); Directories[parentFolder].AddDirectory(newFolder); } } - var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id); + //var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id); - foreach(var dup in duplicates) + //foreach(var dup in duplicates) + //{ + // if(FileDuplicates.ContainsKey(file.PathNoRoot)) + // { + // FileDuplicates[file.PathNoRoot].Duplicates.Add(dup); + // } + // else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List() { dup } }); + //} + var snap = new FileSnapshot(file, firstLevelFolder, Directories[currentPath], folderGroup.Path); + + OrganizationFolders[orgFolder].Add(snap); + Directories[currentPath].AddFile(snap); + Files.Add(Path.Combine(folderGroup.Path, file.PathNoRoot), snap); + } + } + + private void ProcessFilesAsOneLevel(List files, DB.FolderGroup folderGroup) + { + Folder firstLevelFolder = null; + + foreach (var file in files) + { + //Add one to the length to remove the trailing slash in the root path. + var pathNoRoot = file.FullName.Remove(0, folderGroup.Path.Length + 1); + var folders = pathNoRoot.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + var orgFolder = Path.Combine(folderGroup.Path, folders[0]); + var currentPath = Path.Combine(folderGroup.Path, folders[0]); + + if (firstLevelFolder == null || firstLevelFolder.Name != folders[0]) { - if(FileDuplicates.ContainsKey(file.PathNoRoot)) - { - FileDuplicates[file.PathNoRoot].Duplicates.Add(dup); - } - else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List() { dup } }); + firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0]), Root); + Directories.Add(orgFolder, firstLevelFolder); + OrganizationFolders.Add(orgFolder, new List()); + Root.AddDirectory(firstLevelFolder); } - OrganizationFolders[orgFolder].Add(file); - Directories[currentPath].AddFile(file); - Files.Add(file.PathNoRoot, file); + for (var i = 1; i < folders.Length - 1; i++) + { + var parentFolder = currentPath; + currentPath = Path.Combine(currentPath, folders[i]); + if (!Directories.ContainsKey(currentPath)) + { + var newFolder = new Folder(folders[i], currentPath, Directories[parentFolder]); + Directories.Add(currentPath, newFolder); + Directories[parentFolder].AddDirectory(newFolder); + } + } + + //var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id); + + //foreach(var dup in duplicates) + //{ + // if(FileDuplicates.ContainsKey(file.PathNoRoot)) + // { + // FileDuplicates[file.PathNoRoot].Duplicates.Add(dup); + // } + // else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List() { dup } }); + //} + var snap = new FileSnapshot(file, firstLevelFolder, Directories[currentPath], folderGroup.Path); + + OrganizationFolders[orgFolder].Add(snap); + Directories[currentPath].AddFile(snap); + Files.Add(snap.FullPath, snap); } } + private void ProcessFilesAsFlat(List files, DB.FolderGroup folderGroup) + { + + } + + public void DeleteFile(FileSnapshot file) + { + if (Files.ContainsKey(file.FullPath)) Files.Remove(file.FullPath); + Root.DeleteFile(file); + file.ParentFolder.DeleteFile(file); + } + + public void DeleteFolder(Folder folder) + { + ClearSubFolders(folder); + } + + private void ClearSubFolders(Folder folder) + { + if (Directories.ContainsKey(folder.FullPath)) Directories.Remove(folder.FullPath); + if (OrganizationFolders.ContainsKey(folder.FullPath)) OrganizationFolders.Remove(folder.FullPath); + Root.DeleteFolder(folder); + folder.Parent.DeleteFolder(folder); + + foreach (var file in folder.Files) + { + var f = Files.Remove(file.Key); + } + + foreach (var dir in folder.Folders) + ClearSubFolders(dir.Value); + } + public struct Duplicate { public DB.FileSnapshot File; - public List Duplicates; + public List Duplicates; } } @@ -125,28 +181,28 @@ namespace DataOrganizer { public string Name { get; private set; } public string FullPath { get; private set; } - public Dictionary Files { get; private set; } = new Dictionary(); + public Folder Parent { get; private set; } + public Dictionary Files { get; private set; } = new Dictionary(); public Dictionary Folders { get; private set; } = new Dictionary(); - public Folder OrganizationalFolder { get; private set; } + public bool IsEmpty + { + get { return !(Files.Count > 0 || Folders.Count > 0); } + } + public bool IsRootFolder + { + get { return Parent == null; } + } - public Folder(string name, string fullPath) + public Folder(string name, string fullPath, Folder parent) { Name = name; FullPath = fullPath; + Parent = parent; } - public void AddFile(DB.FileSnapshot file) + public void AddFile(FileSnapshot file) { - //var possibleDuplicates = Files.Values.Where(x => x.Size == file.Size); - - Files.Add(file.PathNoRoot, file); - - //foreach (var f in possibleDuplicates) - //{ - // if (f.Id == file.Id) continue; - - // Duplicates.Add(new Duplicate() { FileA = file, FileB = f }); - //} + Files.Add(file.FullPath, file); } public void AddDirectory(Folder folder) @@ -154,16 +210,142 @@ namespace DataOrganizer Folders.Add(folder.FullPath, folder); } - //public struct Duplicate - //{ - // public DB.FileSnapshot FileA; - // public DB.FileSnapshot FileB; - //} + public List GetAllPendingChanges() + { + var files = new List(); + + files.AddRange(Files.Values.Where(x => x.Status != FileSnapshot.FileStatus.None)); + + foreach (var folder in Folders.Values) + files.AddRange(folder.GetAllPendingChanges()); + + return files; + } + + public void DeleteFile(FileSnapshot file) + { + if (Files.ContainsKey(file.FullPath)) Files.Remove(file.FullPath); + } + + public void DeleteFolder(Folder folder) + { + if (Folders.ContainsKey(folder.FullPath)) Folders.Remove(folder.FullPath); + } } public class FileSnapshot { + public Folder OrganizationalFolder { get; } + public Folder ParentFolder { get; } + public string PathNoRoot { get; } + private string Root { get; } + private int _FileId { get; set; } + private string _Name { get; set; } + private DateTime _ModifiedDate { get; set; } + public DateTime CreatedDate { get; } + public long Size { get; } + private string _NamingPattern { get; set; } + public FileStatus Status { get; set; } + public int FileId + { + get { return _FileId; } + set + { + //Since this file is getting an ID, that means we've been commited to the database, so change the Status to reflect this. + Status = FileStatus.None; + FileId = value; + } + } + + public DateTime ModifiedDate + { + get { return _ModifiedDate; } + set + { + if (Status != FileStatus.PendingInsert) Status = FileStatus.PendingUpdate; + _ModifiedDate = value; + } + } + + public string NamingPattern + { + get { return _NamingPattern; } + set + { + if (Status != FileStatus.PendingInsert) Status = FileStatus.PendingUpdate; + _NamingPattern = value; + } + } + + public string Name + { + get { return _Name; } + } + + public FileSnapshot(DB.FileSnapshot file, Folder organizationalFolder, Folder parentFolder, string rootPath) + { + OrganizationalFolder = organizationalFolder; + ParentFolder = parentFolder; + + var db = new DB(); + + if (file.PatternId != 0) _NamingPattern = db.GetNamingPatternById(file.PatternId); + + PathNoRoot = file.PathNoRoot; + Root = rootPath; + _Name = file.Name; + _ModifiedDate = file.ModifiedDate; + CreatedDate = file.CreationDate; + Size = file.Size; + _FileId = file.Id; + Status = FileStatus.None; + } + + public FileSnapshot(FileInfo file, Folder organizationalFolder, Folder parentFolder, string rootPath) + { + OrganizationalFolder = organizationalFolder; + ParentFolder = parentFolder; + + _Name = file.Name; + //Add one to the length to remove the trailing slash in the root path. + PathNoRoot = file.FullName.Remove(0, rootPath.Length + 1); + Root = rootPath; + _ModifiedDate = file.LastWriteTimeUtc; + CreatedDate = file.CreationTimeUtc; + Size = file.Length; + _FileId = 0; + Status = FileStatus.PendingInsert; + } + + public bool HasOwnNamingPattern + { + get + { + return !string.IsNullOrEmpty(NamingPattern); + } + } + + public string FullPath + { + get + { + return Path.Combine(Root, PathNoRoot); + } + } + + public bool HasPendingChanges + { + get { return Status != FileStatus.None; } + } + + public enum FileStatus + { + PendingInsert, + PendingUpdate, + PendingDelete, + None + } } public class SnapshotComparsion