private void MenuAddFile_Click(object sender, RoutedEventArgs e)
        {
            var selected = TreeFolderBrowser.SelectedItem as PathItem;

            if (selected == null)
            {
                // No files/folders
                selected = new PathItem()
                {
                    IsFolder = true,
                    FullPath = FolderPath
                };
                ActivePathItem = selected;
            }

            string path;

            if (!selected.IsFolder || selected.FullPath == "..")
            {
                path = Path.Combine(Path.GetDirectoryName(selected.FullPath), "NewFile.md");
            }
            else
            {
                var treeItem = GetTreeviewItem(selected);
                if (treeItem != null)
                {
                    treeItem.IsExpanded = true;
                }

                path = Path.Combine(selected.FullPath, "NewFile.md");
            }

            var item = new PathItem
            {
                FullPath   = path,
                IsFolder   = false,
                IsFile     = true,
                IsEditing  = true,
                IsSelected = true
            };

            item.SetIcon();

            if (selected.FullPath == "..")
            {
                item.Parent = ActivePathItem; // current path
            }
            else if (!selected.IsFolder)
            {
                item.Parent = selected.Parent;
            }
            else
            {
                item.Parent = selected;
            }

            item.Parent.Files.Insert(0, item);
            SetTreeViewSelectionByItem(item);
        }
Example #2
0
        private void FileWatcher_CreateOrDelete(object sender, FileSystemEventArgs e)
        {
            var file = e.FullPath;

            if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                mmApp.Model.Window.Dispatcher.Invoke(() =>
                {
                    var pi = FolderStructure.FindPathItemByFilename(ActivePathItem, file);
                    if (pi == null)
                    {
                        return;
                    }

                    pi.Parent.Files.Remove(pi);

                    //Debug.WriteLine("After: " + pi.Parent.Files.Count + " " + file);
                }, DispatcherPriority.ApplicationIdle);
            }

            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                mmApp.Model.Window.Dispatcher.Invoke(() =>
                {
                    var pi = FolderStructure.FindPathItemByFilename(ActivePathItem, file);
                    if (pi != null) // Already exists in the tree
                    {
                        return;
                    }

                    // does the path exist?
                    var parentPathItem =
                        FolderStructure.FindPathItemByFilename(ActivePathItem, Path.GetDirectoryName(file));
                    if (parentPathItem == null) // path is not expanced yet
                    {
                        return;
                    }

                    bool isFolder = Directory.Exists(file);
                    pi            = new PathItem()
                    {
                        FullPath = file,
                        IsFolder = isFolder,
                        IsFile   = !isFolder,
                        Parent   = parentPathItem
                    };
                    pi.SetIcon();

                    FolderStructure.InsertPathItemInOrder(pi, parentPathItem);
                }, DispatcherPriority.ApplicationIdle);
            }
        }
        /// <summary>
        /// Gets a folder hierarchy
        /// </summary>
        /// <param name="baseFolder"></param>
        /// <param name="parentPathItem"></param>
        /// <param name="skipFolders"></param>
        /// <returns></returns>
        public PathItem GetFilesAndFolders(string baseFolder, PathItem parentPathItem = null, string skipFolders = ".git,node_modules,bower_components,packages,testresults,bin,obj", bool nonRecursive = false)
        {
            if (string.IsNullOrEmpty(baseFolder) || !Directory.Exists(baseFolder))
            {
                return(new PathItem());
            }

            baseFolder = ExpandPathEnvironmentVars(baseFolder);

            PathItem activeItem;
            bool     isRootFolder = false;

            if (parentPathItem == null)
            {
                isRootFolder = true;
                activeItem   = new PathItem
                {
                    FullPath = baseFolder,
                    IsFolder = true
                };
                if (mmApp.Configuration.FolderBrowser.ShowIcons)
                {
                    activeItem.SetIcon();
                }
            }
            else
            {
                activeItem = new PathItem {
                    FullPath = baseFolder, IsFolder = true, Parent = parentPathItem
                };
                if (mmApp.Configuration.FolderBrowser.ShowIcons)
                {
                    activeItem.SetIcon();
                }

                parentPathItem.Files.Add(activeItem);
            }


            string[] folders = null;

            try
            {
                folders = Directory.GetDirectories(baseFolder);
            }
            catch { }

            if (folders != null)
            {
                foreach (var folder in folders)
                {
                    var name = Path.GetFileName(folder);
                    if (!string.IsNullOrEmpty(name))
                    {
                        if (name.StartsWith("."))
                        {
                            continue;
                        }
                        // skip folders
                        if (("," + skipFolders + ",").Contains("," + name.ToLower() + ","))
                        {
                            continue;
                        }
                    }

                    if (!nonRecursive)
                    {
                        GetFilesAndFolders(folder, activeItem, skipFolders);
                    }
                    else
                    {
                        var folderPath = new PathItem
                        {
                            FullPath = folder,
                            IsFolder = true,
                            Parent   = activeItem
                        };
                        if (mmApp.Configuration.FolderBrowser.ShowIcons)
                        {
                            folderPath.SetIcon();
                        }
                        folderPath.Files.Add(PathItem.Empty);

                        activeItem.Files.Add(folderPath);
                    }
                }
            }

            string[] files = null;
            try
            {
                files = Directory.GetFiles(baseFolder);
            }
            catch { }

            if (files != null)
            {
                foreach (var file in files)
                {
                    var item = new PathItem {
                        FullPath = file, Parent = activeItem, IsFolder = false, IsFile = true
                    };
                    if (mmApp.Configuration.FolderBrowser.ShowIcons)
                    {
                        item.Icon = IconList.GetIconFromFile(file);
                    }

                    activeItem.Files.Add(item);
                }
            }

            if (activeItem.FullPath.Length > 5 && isRootFolder)
            {
                var parentFolder = new PathItem
                {
                    IsFolder = true,
                    FullPath = ".."
                };
                parentFolder.SetIcon();
                activeItem.Files.Insert(0, parentFolder);
            }

            return(activeItem);
        }
        /// <summary>
        /// Gets a folder hierarchy and attach to an existing folder item or
        /// </summary>
        /// <param name="baseFolder">The folder for which to retrieve files for</param>
        /// <param name="parentPathItem">The parent item to which the retrieved files are attached</param>
        /// <param name="ignoredFolders"></param>
        /// <returns></returns>
        public PathItem GetFilesAndFolders(string baseFolder,
                                           PathItem parentPathItem      = null,
                                           string ignoredFolders        = null,
                                           string ignoredFileExtensions = null,
                                           bool nonRecursive            = false)
        {
            if (string.IsNullOrEmpty(baseFolder) || !Directory.Exists(baseFolder))
            {
                return(new PathItem());
            }

            baseFolder = ExpandPathEnvironmentVars(baseFolder);

            PathItem activeItem;
            bool     isRootFolder = false;

            if (ignoredFolders == null)
            {
                ignoredFolders = mmApp.Configuration.FolderBrowser.IgnoredFolders;
            }
            if (ignoredFileExtensions == null)
            {
                ignoredFileExtensions = mmApp.Configuration.FolderBrowser.IgnoredFileExtensions;
            }

            if (parentPathItem == null)
            {
                isRootFolder = true;
                activeItem   = new PathItem
                {
                    FullPath = baseFolder,
                    IsFolder = true
                };
                if (mmApp.Configuration.FolderBrowser.ShowIcons)
                {
                    activeItem.SetIcon();
                }
            }
            else
            {
                activeItem = parentPathItem; //new PathItem { FullPath=baseFolder, IsFolder = true, Parent = parentPathItem};
                activeItem.Files.Clear();    // remove all items
            }

            string[] folders = null;

            try
            {
                folders = Directory.GetDirectories(baseFolder);
            }
            catch { }

            if (folders != null)
            {
                foreach (var folder in folders.OrderBy(f => f.ToLower()))
                {
                    var name = Path.GetFileName(folder);
                    if (!string.IsNullOrEmpty(name))
                    {
                        if (name.StartsWith("."))
                        {
                            continue;
                        }
                        // skip folders
                        if (("," + ignoredFolders + ",").Contains("," + name.ToLower() + ","))
                        {
                            continue;
                        }
                    }

                    var folderPath = new PathItem
                    {
                        FullPath = folder,
                        IsFolder = true,
                        Parent   = activeItem
                    };
                    if (mmApp.Configuration.FolderBrowser.ShowIcons)
                    {
                        folderPath.SetIcon();
                    }
                    activeItem.Files.Add(folderPath);

                    if (!nonRecursive)
                    {
                        GetFilesAndFolders(folder, folderPath, ignoredFolders);
                    }
                    else
                    {
                        folderPath.Files.Add(PathItem.Empty);
                    }
                }
            }

            string[] files = null;
            try
            {
                files = Directory.GetFiles(baseFolder);
            }
            catch { }

            if (files != null)
            {
                // Skip Extensions
                string[] extensions = null;
                if (!string.IsNullOrEmpty(ignoredFileExtensions))
                {
                    extensions = ignoredFileExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }

                foreach (var file in files.OrderBy(f => f.ToLower()))
                {
                    if (extensions != null &&
                        extensions.Any(ext => file.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        continue;
                    }

                    var item = new PathItem {
                        FullPath = file, Parent = activeItem, IsFolder = false, IsFile = true
                    };
                    if (mmApp.Configuration.FolderBrowser.ShowIcons)
                    {
                        item.Icon = IconList.GetIconFromFile(file);
                    }

                    activeItem.Files.Add(item);
                }
            }

            if (activeItem.FullPath.Length > 5 && isRootFolder)
            {
                var parentFolder = new PathItem
                {
                    IsFolder = true,
                    FullPath = ".."
                };
                parentFolder.SetIcon();
                activeItem.Files.Insert(0, parentFolder);
            }

            return(activeItem);
        }
        private void FileWatcher_CreateOrDelete(object sender, FileSystemEventArgs e)
        {
            var file = e.FullPath;

            if (string.IsNullOrEmpty(file))
            {
                return;
            }

            if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                mmApp.Model.Window.Dispatcher.Invoke(() =>
                {
                    var pi = FolderStructure.FindPathItemByFilename(ActivePathItem, file);
                    if (pi == null)
                    {
                        return;
                    }

                    pi.Parent.Files.Remove(pi);

                    //Debug.WriteLine("After: " + pi.Parent.Files.Count + " " + file);
                }, DispatcherPriority.ApplicationIdle);
            }

            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                mmApp.Model.Window.Dispatcher.Invoke(() =>
                {
                    // Skip ignored Extensions
                    string[] extensions = null;
                    if (!string.IsNullOrEmpty(mmApp.Model.Configuration.FolderBrowser.IgnoredFileExtensions))
                    {
                        extensions = mmApp.Model.Configuration.FolderBrowser.IgnoredFileExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    }

                    if (extensions != null && extensions.Any(ext => file.ToLowerInvariant().EndsWith(ext)))
                    {
                        return;
                    }

                    var pi = FolderStructure.FindPathItemByFilename(ActivePathItem, file);
                    if (pi != null) // Already exists in the tree
                    {
                        return;
                    }

                    // does the path exist?
                    var parentPathItem =
                        FolderStructure.FindPathItemByFilename(ActivePathItem, Path.GetDirectoryName(file));
                    if (parentPathItem == null) // path is not expanced yet
                    {
                        return;
                    }

                    bool isFolder = Directory.Exists(file);
                    pi            = new PathItem()
                    {
                        FullPath = file,
                        IsFolder = isFolder,
                        IsFile   = !isFolder,
                        Parent   = parentPathItem
                    };
                    pi.SetIcon();

                    FolderStructure.InsertPathItemInOrder(pi, parentPathItem);
                }, DispatcherPriority.ApplicationIdle);
            }
        }