private List<FileWithTags> getOtherFilesInDirectory(List<string> filesAndDirectoriesNames) { if (this.database == null || filesAndDirectoriesNames == null) return new List<FileWithTags>(); if (filesAndDirectoriesNames.Count == 0) return new List<FileWithTags>(); System.IO.DirectoryInfo parentDirectory = null; // Take the first item, Assume it is a file System.IO.FileInfo item = new System.IO.FileInfo(filesAndDirectoriesNames[0]); // Check if the item is a file if (item.Exists /* isFile*/ ) { parentDirectory = item.Directory; } else /* Maybe a directory*/ { // Assume it is a directory System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(filesAndDirectoriesNames[0]); // Check that in fact the name represents a directory if (dir.Exists) { parentDirectory = dir.Parent; } } if (parentDirectory != null) { System.IO.FileInfo[] directoryFiles = parentDirectory.GetFiles(); List<FileWithTags> listOfFiles = new List<FileWithTags>(); // Convert list to lower-case for comparison for (int i = 0; i < filesAndDirectoriesNames.Count; i++) filesAndDirectoriesNames[i] = filesAndDirectoriesNames[i].ToLower(); // Create list of all files within the directory that exists in the database FileWithTags file; foreach (System.IO.FileInfo fi in directoryFiles) { if (!filesAndDirectoriesNames.Contains(fi.FullName.ToLower())) { file = this.database[fi.FullName]; if (file != null) listOfFiles.Add(file); } } return listOfFiles; } return new List<FileWithTags>(); }
private List<FileTag> getUnionOfTags(List<FileWithTags> files) { FileWithTags tempFile = new FileWithTags(); // Check the input if (files != null && files.Count > 0) { // Add to the file tags from all other files. // The file object will be in charge of removing duplicated tags for (int i = 0; i < files.Count; i++) { tempFile.Tags.AddRange(files[i].Tags); } } return tempFile.Tags; }
/// <summary> /// Sets the tags list of the form to show the tags of the input parameter /// </summary> /// <param name="file"></param> public void SetFile(FileWithTags file) { if (this.files == null) this.files = new List<FileWithTags>(); this.files.Clear(); this.files.Add(file); this.file = file; this.Text = file.FileName; // Set list box of tags this.listTags.Items.Clear(); foreach (FileTag tag in file.Tags) { this.listTags.Items.Add(tag); this.originalTagsList.Add(tag); } // Set list box of proposed tags this.listProposedTags.Items.Clear(); foreach (FileTag tag in this.proposedTags) { if (!file.Tags.Exists(tag.Compare)) this.listProposedTags.Items.Add(tag); } this.listFiles.Items.Clear(); this.listFiles.Items.Add(file.FileName); }
/// <summary> /// This method gets a list of all selected files and directories names /// and sets the form to show all mutual tags between each and every file /// within that list and sub-directories of selected directories in the list /// </summary> /// <param name="filesAndDirectoriesNames"></param> public void SetFiles(List<string> filesAndDirectoriesNames) { // Exit if no database is present if (this.database == null) return; // Get the list of all file names (instead of directories names) List<string> fileNames = this.extractAllFilesNames(filesAndDirectoriesNames); // Prepare a list of FileWithTags List<FileWithTags> files = new List<FileWithTags>(); // Search each file name in the database foreach (string fileName in fileNames) { // Find file in database FileWithTags file = this.database[fileName]; if (file == null) /* If not found, create the file and add to database */ { file = new FileWithTags(); file.FileName = fileName; this.database.Files.Add(file); } // Add file to the list files.Add(file); } // Use method to set the visualization this.proposedTags = this.getUnionOfTags(this.getOtherFilesInDirectory(filesAndDirectoriesNames)); this.SetFiles(files); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static TagFilesDatabase DeSerialize(string fileName) { TagFilesDatabase ret = new TagFilesDatabase(); if (File.Exists(fileName)) { string line = ""; FileStream fileStream = new FileStream(fileName, FileMode.Open); StreamReader reader = new StreamReader(fileStream); while (!reader.EndOfStream) { line = reader.ReadLine(); // Read the files if (line == fileBeginString) { // Read the file name FileWithTags file = new FileWithTags(); file.FileName = reader.ReadLine(); // Read the tags line = reader.ReadLine(); while (line != fileEndString) { FileTag tag = new FileTag(); tag.Value = line; file.Tags.Add(tag); line = reader.ReadLine(); } ret.Files.Add(file); } // Read the groups else if (line == groupsBeginString) { line = reader.ReadLine(); while (line != groupsEndString) { string name = line; string key = reader.ReadLine(); ret.CreateGroup(name, key); line = reader.ReadLine(); } } // Read the mapping between tags and groups else if (line == mappingBeginString) { line = reader.ReadLine(); int index = 0; while (line != mappingEndString) { // read the tag line FileTag tag = new FileTag(line); // read the group index line int groupIndex = int.Parse(reader.ReadLine()); // find the tag in the list index = ret.tags.FindIndex(tag.Compare); if (index >= 0) { ret.tagGroupMapping[index] = groupIndex; } line = reader.ReadLine(); } } } reader.Dispose(); fileStream.Close(); } return ret; }
public TagFilesDatabaseEventArgs(FileWithTags file, FileTag tag) { this.file = file; this.tag = tag; }
private void raiseEvent(DatabaseAction action, FileWithTags file, FileTag tag) { TagFilesDatabaseEventArgs e = new TagFilesDatabaseEventArgs(file, tag); if (action == DatabaseAction.AddingTag) { if (this.TagAdded != null) this.TagAdded(this, e); } else if (action == DatabaseAction.RemovingTag) { if (this.TagRemoved != null) this.TagRemoved(this, e); } else if (action == DatabaseAction.RemovingFile) { if (this.FileRemoved != null) this.FileRemoved(this, e); } else if (action == DatabaseAction.AddingFile) { if (this.FileAdded != null) this.FileAdded(this, e); } }
private void file_TagAdded(FileWithTags sender, FileWithTags.FileWithTagsEventArgs e) { // Set the action of the database this.currentAction = DatabaseAction.AddingTag; // If it's the first tag of the file, remove the Empty tag from the database if (sender.Tags.Count == 1) // After the tag was added this.removeTag(""); // Add the tag this.addTag(e.Tag.Value); // Set the ending of the action this.currentAction = DatabaseAction.Idle; // Raise the DatabaseChange event this.raiseEvent(DatabaseAction.AddingTag, sender, e.Tag); }
// A file from the list of files notifies that one of its tags has been removed or that a new tag has been added private void file_TagRemoved(FileWithTags sender, FileWithTags.FileWithTagsEventArgs e) { // Set the action of the database this.currentAction = DatabaseAction.RemovingTag; // Remove the tag this.removeTag(e.Tag.Value); // If no more tags remains, add to the database the Empty tag if (sender.Tags.Count == 0) { this.addTag(""); } // Set the ending of the action this.currentAction = DatabaseAction.Idle; // Raise the DatabaseChange event this.raiseEvent(DatabaseAction.RemovingTag, sender, e.Tag); }