示例#1
0
        /// <summary>
        /// 根据文件创建一个节点;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <returns></returns>
        private static INavNodeModel CreateNodeModelWithFileCollection(IHaveFileCollection haveFileCollection)
        {
            var node = NavNodeFactory.CreateNew();

            node.Name = haveFileCollection.Name;
            return(node);
        }
        public static string[] GetUrlArgsByFile(this IHaveFileCollection haveFileCollection, IFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (haveFileCollection == null)
            {
                throw new ArgumentNullException(nameof(haveFileCollection));
            }

            var fileStack = haveFileCollection.GetParentFiles(file, true);

            if (fileStack == null)
            {
                return(null);
            }

            var stackSize = fileStack.Count();
            var array     = new string[stackSize];
            var index     = 0;

            foreach (var cell in fileStack)
            {
                array[stackSize - index - 1] = cell.Name;

                index++;
            }
            return(array);
        }
        /// <summary>
        /// 得到内部所有文件的迭代;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <param name="backOrBackUpDirIncluded">文件夹是否包含</param>
        /// <returns></returns>
        public static IEnumerable <IFile> GetInnerFiles(this IHaveFileCollection haveFileCollection, bool isDirIncluded = false)
        {
            if (haveFileCollection == null)
            {
                throw new ArgumentNullException(nameof(haveFileCollection));
            }

            foreach (var file in haveFileCollection.Children)
            {
                if (file is IHaveFileCollection innerCollection)
                {
                    if (file is IDirectory dir)
                    {
                        if (!dir.IsBack && !dir.IsLocalBackUp && isDirIncluded)
                        {
                            yield return(file);
                        }
                    }

                    foreach (var innerFile in innerCollection.GetInnerFiles(isDirIncluded))
                    {
                        yield return(innerFile);
                    }
                }
                else
                {
                    yield return(file);
                }
            }
        }
        /// <summary>
        /// //得到内部文件的总大小;
        /// </summary>
        /// <param name="file">目标文件</param>
        /// <param name="subDicIncluded">是否包含子文件夹</param>
        /// <returns></returns>
        public static long GetSubSize(this IHaveFileCollection file, SearchOption searchOption = SearchOption.AllDirectories)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (file.Children == null)
            {
                return(0);
            }

            long sumSize = 0;

            foreach (var child in file.Children)
            {
                if (searchOption == SearchOption.AllDirectories && child is IHaveFileCollection eumFile)
                {
                    sumSize += GetSubSize(eumFile);
                }

                sumSize += child.Size;
            }

            return(sumSize);
        }
        /// <summary>
        /// 得到文件总数;
        /// </summary>
        /// <param name="file"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        private static long GetSubFileNum(IHaveFileCollection file, SearchOption searchOption = SearchOption.AllDirectories)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (file.Children == null)
            {
                return(0);
            }

            long sumNum = 0;

            foreach (var child in file.Children)
            {
                if (searchOption == SearchOption.AllDirectories && child is IHaveFileCollection enumFile)
                {
                    sumNum += GetSubFileNum(enumFile);
                }
                sumNum++;
            }


            return(sumNum);
        }
        /// <summary>
        /// 根据文件得到路径;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetUrlByFile(this IHaveFileCollection haveFileCollection, IFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (haveFileCollection == null)
            {
                throw new ArgumentNullException(nameof(haveFileCollection));
            }

            var sb          = new StringBuilder();
            var parentFiles = haveFileCollection.GetParentFiles(file, true);

            if (parentFiles == null)
            {
                return(null);
            }
            var index = 0;

            foreach (var pFile in parentFiles)
            {
                if (index == 0)
                {
                    sb.Insert(0, $"{pFile.Name}");
                }
                else
                {
                    sb.Insert(0, $"{pFile.Name}{Constants.Path_SplitChar}");
                }
                index++;
            }
            return(sb.ToString());
        }
示例#7
0
 /// <summary>
 /// 填充行;
 /// </summary>
 ///<param name="haveFileCollection">母文件</param>
 public static void FillWithCollection(this IFolderBrowserViewModel vm, IHaveFileCollection haveFileCollection)
 {
     if (haveFileCollection == null)
     {
         return;
     }
     vm.FillRows(haveFileCollection.Children);
 }
        public FolderBrowserDataContext(IHaveFileCollection haveFileCollection)
        {
            FolderBrowserViewModel = new FolderBrowserViewModel(haveFileCollection);
            //var vm = FileExplorerDataContextFactory.CreateFolderBrowserDataContext(haveFileCollection);

            var folderBrowser         = ViewProvider.CreateView(Constants.FolderBrowserView, FolderBrowserViewModel);
            var folderBrowserUIObject = UIObjectProviderFactory.CreateNew(folderBrowser);

            StackGrid.AddChild(folderBrowserUIObject,
                               new GridChildLength(new System.Windows.GridLength(1, System.Windows.GridUnitType.Star)));
        }
        /// <summary>
        /// 通过Url找到子文件;
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static IFile GetFileByUrl(this IHaveFileCollection part, string url)
        {
            try {
                url = url.Replace('/', Constants.Path_SplitChar);
                var urlArgs = url.Split(Constants.Path_SplitChar);
                return(GetFileByUrlArgs(part, urlArgs));
            }
            catch (Exception ex) {
                LoggerService.Current?.WriteCallerLine(ex.Message);
            }

            return(null);
        }
示例#10
0
 private static IEnumerable <IHaveFileCollection> CreateChildrenCollection(IHaveFileCollection haveFileCollection)
 {
     foreach (var file in haveFileCollection.Children)
     {
         if (file is IHaveFileCollection cCollection)
         {
             if (cCollection is IDirectory direct && (direct.IsBack || direct.IsLocalBackUp))
             {
                 continue;
             }
             yield return(cCollection);
         }
     }
 }
        /// <summary>
        /// 得到指定文件节点以上所有父节点;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static IEnumerable <IFile> GetParentFiles(this IHaveFileCollection haveFileCollection, IFile file, bool selfIncluded = false)
        {
            if (haveFileCollection == null)
            {
                throw new ArgumentNullException(nameof(haveFileCollection));
            }
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (!CheckOwn(haveFileCollection, file))
            {
                throw new InvalidOperationException($"{nameof(haveFileCollection)}({haveFileCollection.Name}) doesn't own the file ({file.Name})");
            }

            return(file.GetParentEntities(haveFileCollection, f => f.Parent, selfIncluded));
        }
        /// <summary>
        /// 查找索引号;
        /// </summary>
        /// <param name="itrFile"></param>
        /// <returns></returns>
        public static int IndexOf(this IHaveFileCollection itrFile, IFile file)
        {
            if (itrFile == null && itrFile.Children == null)
            {
                return(-1);
            }
            var idx = 0;

            foreach (var item in itrFile.Children)
            {
                if (item == file)
                {
                    return(idx);
                }
                idx++;
            }
            return(-1);
        }
        private void HandleOnFileCollection(IHaveFileCollection haveFileCollection)
        {
            IFile docFile = null;

            if (haveFileCollection is IStreamFile)
            {
                docFile = haveFileCollection as IStreamFile;
            }
            else
            {
                docFile = haveFileCollection.GetParent <IStreamFile>() ?? haveFileCollection.GetRoot();
            }

            if (docFile == null)
            {
                LoggerService.WriteCallerLine($"{nameof(docFile)} can't be null.");
                return;
            }

            var doc = FileExplorerUIHelper.GetOrAddFileDocument(docFile);

            if (doc == null)
            {
                LoggerService.WriteCallerLine($"{nameof(doc)} can't be null.");
                return;
            }

            var folderBrowseDataContext = doc.GetInstance <IFolderBrowserDataContext>(Contracts.FileExplorer.Constants.DocumentTag_FolderBrowserDataContext);

            if (folderBrowseDataContext == null)
            {
                LoggerService.WriteCallerLine($"{nameof(folderBrowseDataContext)} can't be null.");
                return;
            }
            var folderBrowseViewModel = folderBrowseDataContext.FolderBrowserViewModel;

            if (folderBrowseViewModel == null)
            {
                LoggerService.WriteCallerLine($"{nameof(folderBrowseViewModel)} can't be null.");
                return;
            }

            folderBrowseViewModel.FillRows(haveFileCollection.GetInnerFiles());
        }
        public IFolderBrowserDataContext CreateFolderBrowserDataContext(IHaveFileCollection haveFileCollection)
        {
            var dataContext = new FolderBrowserDataContext(haveFileCollection);

            try {
                CommonEventHelper.PublishEventToHandlers(dataContext as IFolderBrowserDataContext, _folderBrowserViewModelCreatedEventHandlers);
                CommonEventHelper.GetEvent <FolderBrowserDataContextCreatedEvent>().Publish(dataContext);
            }
            catch (Exception ex) {
                LoggerService.WriteException(ex);
            }

            //try {
            //    dataContext.Initialize();
            //}
            //catch(Exception ex) {
            //    LoggerService.WriteException(ex);
            //}

            return(dataContext);
        }
        /// <summary>
        /// 通过Url参数获得子文件;
        /// </summary>
        /// <param name="enumFile"></param>
        /// <param name="urlArgs"></param>
        /// <returns></returns>
        public static IFile GetFileByUrlArgs(this IHaveFileCollection enumFile, string[] urlArgs)
        {
            if (urlArgs == null || urlArgs.Length == 0)
            {
                return(null);
            }
            if (enumFile == null)
            {
                return(null);
            }

            return(enumFile.GetEntityFromParams <IFile, string>(
                       urlArgs,
                       f => {
                if (f is IHaveFileCollection haveFileCollection)
                {
                    return haveFileCollection.Children;
                }
                return Enumerable.Empty <IFile>();
            },
                       (f, param) => f.Name == param
                       ));
        }
        //public static TAggregate GetAggregate<TAggregate,TEntity>(Func<TAggregate> aggreateFactory,IEnumerable<){
        //}

        /// <summary>
        /// 检查某个文件集合中是否包含某个文件;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool CheckOwn(this IHaveFileCollection haveFileCollection, IFile file)
        {
            if (haveFileCollection == null)
            {
                throw new ArgumentNullException(nameof(haveFileCollection));
            }

            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            return(haveFileCollection.CheckOwn(file, f => f.Parent));

            //IFile fileNode = file;
            //while (fileNode != null) {
            //    if (fileNode == haveFileCollection) {
            //        break;
            //    }
            //    fileNode = fileNode.Parent;
            //}

            //return fileNode != null;
        }
        /// <summary>
        /// 处理子文件/目录/其它加载;
        /// </summary>
        /// <param name="haveFileCollection"></param>
        /// <param name="filePtr">非托管文件节点指针</param>
        /// <param name="ntfSzAct"></param>
        /// <param name="isCancel"></param>
        private static void DealWithFileNode(
            IHaveFileCollection haveFileCollection,
            ExtPartInfo partInfo,
            IntPtr filePtr,
            Action <long> ntfSzAct,
            Func <bool> isCancel)
        {
            while (filePtr != IntPtr.Zero)
            {
                IFile           file        = null;
                FileStokenBase2 fileStoken2 = null;
                var             stDirEntry  = filePtr.GetStructure <StDirEntry>();

                //压栈;避免使用continue导致死循环以及冗余代码;
                void Push()
                {
                    if (file != null)
                    {
                        haveFileCollection.Children.Add(file);
                    }
                    filePtr = stDirEntry.Next;
                }

                if (stDirEntry.DirInfo == IntPtr.Zero)
                {
                    Push();
                    continue;
                }

                var stExtDirEntry = stDirEntry.DirInfo.GetStructure <StExt4DirEntry>();

                var stExtINodePtr = ExtX_Get_InodeInfo(partInfo.ExtUnmanagedManager.ExtManagerPtr, stExtDirEntry.inode);
                if (stExtINodePtr == IntPtr.Zero)
                {
                    LoggerService.WriteCallerLine($"{nameof(stExtINodePtr)} can't be nullptr.");
                    Push();
                    continue;
                }
                var stExtINode = stExtINodePtr.GetStructure <StExt4Inode>();

                var stBlockListPtr = ExtX_Get_BlockList(partInfo.ExtUnmanagedManager.ExtManagerPtr, stExtINodePtr);
                if (stBlockListPtr == IntPtr.Zero)
                {
                    //LoggerService.WriteCallerLine($"{nameof(stBlockListPtr)} can't be nullptr.");
                    Push();
                    continue;
                }

                var extFileInfo = new ExtFileInfo {
                    StDirEntry     = stDirEntry,
                    StExt4DirEntry = stExtDirEntry,
                    BlockListPtr   = stBlockListPtr,
                    StExt4Inode    = stExtINode
                };

                if (stExtDirEntry.file_type == Ext4FileType.Directory)
                {
                    var dir = FileFactory.CreateDirectory(Constants.DirectoryKey_Ext);

                    var dirStoken = dir.GetStoken(Constants.DirectoryKey_Ext);
                    dirStoken.TypeGuid = Constants.DirectoryType_Ext;

                    file        = dir;
                    fileStoken2 = dirStoken;

                    EditFileStoken2(fileStoken2, partInfo, extFileInfo);
                    //当目录名为".."或者"."时,跳过加载子文件;
                    //且所加载目录不能被删除;
                    if (stExtDirEntry._name[0] != '.' && !stDirEntry.bDel)
                    {
                        LoadDirectoryContent(dir, partInfo, ntfSzAct, isCancel);
                    }
                    else if (stExtDirEntry._name[1] == '.')
                    {
                        dirStoken.IsBack = true;
                    }
                    else
                    {
                        dirStoken.IsLocalBackUp = true;
                    }
                }
                else if (stExtDirEntry.file_type == Ext4FileType.RegularFile)
                {
                    var regFile       = FileFactory.CreateRegularFile(Constants.RegularFileKey_Ext);
                    var regFileStoken = regFile.GetStoken(Constants.RegularFileKey_Ext);

                    regFileStoken.TypeGuid = Constants.RegularFileType_Ext;

                    file        = regFile;
                    fileStoken2 = regFileStoken;

                    EditFileStoken2(fileStoken2, partInfo, extFileInfo);
                    ntfSzAct?.Invoke(fileStoken2.Size);
                }

                Push();

                if (isCancel?.Invoke() ?? false)
                {
                    return;
                }
            }
        }
 public static IFolderBrowserDataContext CreateFolderBrowserDataContext(IHaveFileCollection haveFileCollection) => Current?.CreateFolderBrowserDataContext(haveFileCollection);
示例#19
0
        /// <summary>
        /// 目录/资源浏览器模型构造方法;
        /// </summary>
        /// <param name="haveFileCollection">模型所属主文件</param>
        public FolderBrowserViewModel(IHaveFileCollection haveFileCollection)
        {
            this.OwnedFileCollection = haveFileCollection ?? throw new ArgumentNullException(nameof(haveFileCollection));

            Initialize();
        }