void validateFileToBeAdded(FileBase file) { if (FileExistsInFilesystem(file)) { throw new FileAlreadyExistsException($"cannot add file {file.Name} because it already exists in this filesystem"); } validateFileName(file.Name); }
public void MoveFile(FileBase file, string parentDirectoryPath, bool deep = false) { if (GetFileAtPath(parentDirectoryPath) is Directory dir) { throwIfWouldCreateCircularStructure(file, dir); } RemoveFile(file); AddFile(file, parentDirectoryPath, deep); }
public string GetPathOfFile(FileBase file) { if (!FileExistsInFilesystem(file)) { throw new FileDoesNotExistException($"file {file.Name} does not exist in this filesystem"); } var ancestorNames = traverseAncestors(file).Reverse().Select(f => f.Name); var pathSuffix = file is Directory ? PathSeparator : ""; return(string.Join(PathSeparator, ancestorNames) + pathSuffix); }
void untrackFileAndAnyChildren(FileBase file) { parentCache.Remove(file); if (file is Directory dir) { foreach (var child in dir.Data) { untrackFileAndAnyChildren(child); } } }
void trackFileAndAnyChildren(FileBase file, Directory parent) { parentCache[file] = parent; if (file is Directory dir) { foreach (var child in dir.Data) { trackFileAndAnyChildren(child, dir); } } }
// assumes file is in filesystem IEnumerable <FileBase> traverseAncestors(FileBase file) { FileBase currentFile = file; while (currentFile != RootDirectory) { yield return(currentFile); currentFile = parentCache[currentFile]; } yield return(currentFile); // this should always be the root }
// assumes file existed to begin with public void RemoveFile(FileBase file) { if (!FileExistsInFilesystem(file)) { throw new FileDoesNotExistException($"file {file.Name} does not exist in this filesystem"); } if (file == RootDirectory) { throw new AttemptedRootDirectoryDeletionException("cannot delete the root directory"); } parentCache[file].Data.Remove(file); untrackFileAndAnyChildren(file); }
void onClick() { entry.Read = true; FileBase file = entry.Contents is Order order ? new OrderFile { Data = order } as FileBase : new EmailFile { Data = entry.Contents }; file.Name = entry.Contents.AnnotatedSubject; WindowFactory.Instance.OpenWindowWithFile(file); }
public void AddFile(FileBase file, Directory parent) { throwIfWouldCreateCircularStructure(file, parent); validateFileToBeAdded(file); if (!FileExistsInFilesystem(parent)) { throw new FileDoesNotExistException($"cannot add file {file.Name} to directory {parent.Name} because that directory does not exist in the filesystem"); } if (parent.Data.Any(f => f.Name == file.Name)) { throw new PathAlreadyExistsException($"cannot add file {file.Name} to directory {parent.Name} because it already contains a file with that name"); } parent.Data.Add(file); trackFileAndAnyChildren(file, parent); }
public Window OpenWindowWithFile(FileBase file) { var windowMetadata = FileAssociationConfig.GetMetadataForFile(file); Window window; switch (windowMetadata.NewWindowMode) { case WindowMetadata.NewWindowBehavior.FocusOldWindow: window = findWindowWithMetadata(windowMetadata); break; case WindowMetadata.NewWindowBehavior.OpenOneWindowPerFile: window = findWindowWithFile(file); break; case WindowMetadata.NewWindowBehavior.AlwaysOpenNewWindow: window = null; break; default: throw new InvalidOperationException($"file association config {FileAssociationConfig.name} has unsupported window mode {windowMetadata.NewWindowMode} configured for file type {file.GetType().Name}"); } if (window == null) { window = Instantiate(windowMetadata.WindowPrefab, WindowParent); window.SetFile(file); if (windowMetadata.AddButtonToTaskbar) { var button = Instantiate(TaskBarButtonPrefab); window.SetTaskBarButton(button); button.SetWindow(window); TaskBar.Instance.AddButton(button); } } window.Focus(); return(window); }
void throwIfWouldCreateCircularStructure(FileBase file, Directory targetParent) { // wish I could use pattern matching here but Unity won't let me :( var directory = file as Directory; if (directory == null) { return; } if (directory == targetParent) { throw new CircularDirectoryStructureException($"cannot add directory {directory.Name} to itself"); } if (traverseAncestors(targetParent).Contains(directory)) { throw new CircularDirectoryStructureException($"cannot add {directory.Name} to directory {targetParent.Name} because {targetParent.Name} is a descendant of {directory.Name}"); } }
// deep parameter acts like the 'p' flag in mkdir. if it's set and you want to add path '/a/b/c' and directory 'a' or 'b' do not exist, they will be created public void AddFile(FileBase file, string parentDirectoryPath, bool deep = false) { validateFileToBeAdded(file); validatePath(parentDirectoryPath); Directory parent = GetDirectoryAtPath(parentDirectoryPath); if (parent == null) { if (deep) { parent = convertDeepPathToDirectories(parentDirectoryPath); } else { throw new PathDoesNotExistException($"one or more directories on the path {parentDirectoryPath} do not exist. either add those directories, or set the deep flag and try again"); } } AddFile(file, parent); }
public void RenameFile(FileBase file, string name) { validateFileName(name); if (!FileExistsInFilesystem(file)) { throw new FileDoesNotExistException("cannot rename file that does not exist"); } if (file.Name == name) { return; } if (file != RootDirectory && parentCache[file].Data.Any(f => f != file && f.Name == name)) { throw new PathAlreadyExistsException($"cannot rename {file.Name} to {name} because its parent directory ({parentCache[file].Name}{PathSeparator}) already contains a file with that name"); } file.Name = name; }
public bool FileExistsInFilesystem(FileBase file) { return(parentCache.ContainsKey(file)); }
public void MoveFile(FileBase file, Directory newParent) { throwIfWouldCreateCircularStructure(file, newParent); RemoveFile(file); AddFile(file, newParent); }
Window findWindowWithFile(FileBase file) { return(FindObjectsOfType <Window>().SingleOrDefault(w => w.File == file)); }
public WindowMetadata GetMetadataForFile(FileBase file) { string fileTypeName = file.GetType().FullName; return(Config.FirstOrDefault(d => d.FullNameOfFileType == fileTypeName)?.Metadata); }