예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        void untrackFileAndAnyChildren(FileBase file)
        {
            parentCache.Remove(file);

            if (file is Directory dir)
            {
                foreach (var child in dir.Data)
                {
                    untrackFileAndAnyChildren(child);
                }
            }
        }
예제 #5
0
        void trackFileAndAnyChildren(FileBase file, Directory parent)
        {
            parentCache[file] = parent;

            if (file is Directory dir)
            {
                foreach (var child in dir.Data)
                {
                    trackFileAndAnyChildren(child, dir);
                }
            }
        }
예제 #6
0
        // 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
        }
예제 #7
0
        // 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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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}");
            }
        }
예제 #12
0
        // 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);
        }
예제 #13
0
        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;
        }
예제 #14
0
 public bool FileExistsInFilesystem(FileBase file)
 {
     return(parentCache.ContainsKey(file));
 }
예제 #15
0
 public void MoveFile(FileBase file, Directory newParent)
 {
     throwIfWouldCreateCircularStructure(file, newParent);
     RemoveFile(file);
     AddFile(file, newParent);
 }
예제 #16
0
 Window findWindowWithFile(FileBase file)
 {
     return(FindObjectsOfType <Window>().SingleOrDefault(w => w.File == file));
 }
예제 #17
0
        public WindowMetadata GetMetadataForFile(FileBase file)
        {
            string fileTypeName = file.GetType().FullName;

            return(Config.FirstOrDefault(d => d.FullNameOfFileType == fileTypeName)?.Metadata);
        }