Example #1
0
        private void Paste()
        {
            if (this.ClipBoard == null)
            {
                return;
            }

            ObjectListView listView = this.GetCurrentListView();

            MyFileSystemInfo target = null;

            if (this.IsNavigator(listView))
            {
                target = listView.SelectedObject as MyFileSystemInfo;
            }
            else
            {
                target = listView.Tag as MyFileSystemInfo;
            }

            if (target == null || !target.IsDirectory)
            {
                return;
            }

            this.MoveFiles(this.ClipBoard.SourceListView, listView, this.ClipBoard.SelectedItems, target, this.ClipBoard.Mode);

            this.RefreshListView(this.ClipBoard.SourceListView);
            this.RefreshListView(listView);

            this.ClipBoard = null;
        }
Example #2
0
        private void RefreshListView(ObjectListView listView)
        {
            if (this.IsNavigator(listView))
            {
                if (listView.SelectedObject != null)
                {
                    listView.RefreshObject(listView.SelectedObject);
                }
                else
                {
                    listView.Refresh();
                }
            }
            else
            {
                MyFileSystemInfo info = listView.Tag as MyFileSystemInfo;

                if (info != null)
                {
                    var children = this.GetChildren(info, false);

                    listView.SetObjects(children);

                    listView.Refresh();
                }
            }
        }
Example #3
0
        private void InitFileList()
        {
            this.lvFiles.SelectedBackColor = ColorTranslator.FromHtml("#CCE8FF");

            this.olvSize.AspectGetter = delegate(object x)
            {
                MyFileSystemInfo info = x as MyFileSystemInfo;

                if (info.IsDirectory)
                {
                    return((long)-1);
                }

                try
                {
                    return(info.AsFile.Length);
                }
                catch (System.IO.FileNotFoundException)
                {
                    return((long)-2);
                }
            };

            this.olvSize.AspectToStringConverter = delegate(object x)
            {
                long sizeInBytes = (long)x;

                if (sizeInBytes < 0)
                {
                    return("");
                }
                return(FileHelper.FormatFileSize(sizeInBytes));
            };

            this.olvSize.MakeGroupies(new long[] { 0, 1024 * 1024, 512 * 1024 * 1024 },
                                      new string[] { "Folders", "Small", "Big", "Disk space chewer" });

            this.olvCreated.GroupKeyGetter = delegate(object x)
            {
                DateTime dt = ((MyFileSystemInfo)x).CreationTime;
                return(new DateTime(dt.Year, dt.Month, 1));
            };
            this.olvCreated.GroupKeyToTitleConverter = delegate(object x)
            {
                return(((DateTime)x).ToString("yyyy MM"));
            };

            this.olvModified.GroupKeyGetter = delegate(object x)
            {
                DateTime dt = ((MyFileSystemInfo)x).LastWriteTime;
                return(new DateTime(dt.Year, dt.Month, 1));
            };

            this.olvModified.GroupKeyToTitleConverter = delegate(object x)
            {
                return(((DateTime)x).ToString("yyyy MM"));
            };
        }
Example #4
0
        private void EnterSubFolder()
        {
            MyFileSystemInfo info = this.lvFiles.SelectedObject as MyFileSystemInfo;

            if (info != null && info.IsDirectory)
            {
                this.lvFiles.SetObjects(this.GetChildren(info, false));
                this.lvFiles.Tag = info;

                this.ShowItemCount();
            }
        }
Example #5
0
        private void BackToParent()
        {
            MyFileSystemInfo info = this.lvFiles.Tag as MyFileSystemInfo;

            if (info != null)
            {
                if (info.Parent != null)
                {
                    this.lvFiles.SetObjects(info.Parent.GetFileSystemInfos());
                    this.lvFiles.Tag = info.Parent;

                    this.ShowItemCount();
                }
                else
                {
                    //this.lvFiles.SetObjects(this.GetDrives());
                }
            }
        }
Example #6
0
        private void navigator_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyFileSystemInfo info = this.navigator.SelectedObject as MyFileSystemInfo;

            if (info != null && info.IsDirectory)
            {
                this.lvFiles.SetObjects(info.GetFileSystemInfos());
                this.lvFiles.Tag = info;

                this.ShowItemCount();
            }
            else
            {
                this.lvFiles.Tag = null;
                this.lvFiles.Items.Clear();
            }

            this.currentListView = this.navigator;
        }
Example #7
0
        private IEnumerable <MyFileSystemInfo> GetChildren(MyFileSystemInfo folder, bool isNavigator)
        {
            try
            {
                if (isNavigator)
                {
                    return(folder.GetFileSystemInfos().Where(item => item.IsDirectory || item.Extension.ToLower() == ".zip"));
                }
                else
                {
                    return(folder.GetFileSystemInfos());
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                this.BeginInvoke((MethodInvoker) delegate()
                {
                    this.navigator.Collapse(folder);
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                });

                return(Enumerable.Empty <MyFileSystemInfo>());
            }
        }
Example #8
0
 private void LoadByDriver(MyFileSystemInfo driver)
 {
     this.navigator.Roots = this.GetChildren(driver, true);
 }
Example #9
0
        private void MoveObjects(ObjectListView sourceListView, ObjectListView targetListView, IEnumerable <MyFileSystemInfo> sources, MyFileSystemInfo target, FileMoveMode mode = FileMoveMode.None)
        {
            foreach (MyFileSystemInfo x in sources)
            {
                bool isCopy = mode == FileMoveMode.Copy || (mode == FileMoveMode.Drag && !FileHelper.IsSameDrive(x.FullName, target.FullName));

                if (!isCopy)
                {
                    if (!(sourceListView is TreeListView))
                    {
                        sourceListView.RemoveObject(x);
                    }

                    x.Parent.Children.Remove(x);
                }

                if (!(targetListView is TreeListView))
                {
                    if (!targetListView.Objects.OfType <MyFileSystemInfo>().Any(item => item.Name == x.Name))
                    {
                        targetListView.AddObject(x);
                    }
                }

                x.Parent = target;

                if (!target.Children.Any(item => item.Name == x.Name))
                {
                    target.Children.Add(x);
                }
            }
        }
Example #10
0
        private async void MoveFiles(ObjectListView sourceListView, ObjectListView targetListView, IEnumerable <MyFileSystemInfo> sources, MyFileSystemInfo target, FileMoveMode mode = FileMoveMode.None)
        {
            if (mode == FileMoveMode.None)
            {
                return;
            }

            long totalSize        = 0;
            int  fileCount        = 0;
            long totalTransferred = 0;

            foreach (MyFileSystemInfo source in sources)
            {
                if (source.IsDirectory)
                {
                    fileCount += FileHelper.GetFolderFileCount(source.AsDirectory);
                    totalSize += FileHelper.GetFolderSize(source.AsDirectory);
                }
                else
                {
                    totalSize += source.AsFile.Length;
                    fileCount++;
                }
            }

            this.cancellationTokenSource = new CancellationTokenSource();

            FileCopyInfo info = new FileCopyInfo()
            {
                TotalSize = totalSize, FileCount = fileCount
            };

            Dictionary <string, TransferProgress> fileTransferred = new Dictionary <string, TransferProgress>();

            Action <TransferProgress> fileCopyProgress = (progress) =>
            {
                if (this.isCanceled)
                {
                    return;
                }

                if (!fileTransferred.ContainsKey(progress.SourcePath))
                {
                    fileTransferred.Add(progress.SourcePath, progress);
                }
                else
                {
                    fileTransferred[progress.SourcePath] = progress;
                }

                totalTransferred   = fileTransferred.Sum(item => item.Value.Transferred);
                info.FinishedCount = fileTransferred.Where(item => item.Value.Transferred == item.Value.Total).Count();

                info.TotalTransferred   = totalTransferred;
                info.CurrentSize        = progress.StreamSize;
                info.CurrentTransferred = progress.Transferred;
                info.CurrentPercent     = progress.Fraction;
                info.TotalPercent       = totalTransferred / (totalSize * 1.0);

                this.Feedback($"Progress:({info.FinishedCount}/{info.FileCount}) {FileHelper.FormatFileSize(info.TotalTransferred)}/{FileHelper.FormatFileSize(info.TotalSize)}, {info.TotalPercent.ToString("P")}");
            };

            try
            {
                this.isCanceled = false;
                this.isMoving   = true;

                foreach (MyFileSystemInfo source in sources)
                {
                    if (this.isCanceled)
                    {
                        break;
                    }

                    if (source.IsDirectory)
                    {
                        DirectoryInfo sourceFolder = source.AsDirectory;

                        string targetFolder = Path.Combine(target.FullName, sourceFolder.Name);

                        if (FileHelper.IsSameDrive(source.AsDirectory.FullName, targetFolder) && (mode == FileMoveMode.Cut || mode == FileMoveMode.Drag))
                        {
                            await FileTransferManager.MoveWithProgressAsync(source.AsDirectory.FullName, targetFolder, fileCopyProgress, this.cancellationTokenSource.Token);
                        }
                        else
                        {
                            await FileHelper.CopyFolder(source.AsDirectory.FullName, targetFolder, true, fileCopyProgress, this.cancellationTokenSource.Token);

                            if (mode == FileMoveMode.Cut)
                            {
                                source.AsDirectory.Delete(true);
                            }
                        }
                    }
                    else
                    {
                        FileInfo sourceFile = source.AsFile;

                        string targetFilePath = Path.Combine(target.FullName, sourceFile.Name);

                        if (FileHelper.IsSameDrive(sourceFile.DirectoryName, target.FullName) && (mode == FileMoveMode.Cut || mode == FileMoveMode.Drag))
                        {
                            await FileHelper.MoveFile(sourceFile.FullName, targetFilePath, fileCopyProgress, this.cancellationTokenSource.Token);
                        }
                        else
                        {
                            await FileHelper.CopyFile(sourceFile.FullName, targetFilePath, fileCopyProgress, this.cancellationTokenSource.Token);

                            if (mode == FileMoveMode.Cut)
                            {
                                sourceFile.Delete();
                            }
                        }
                    }
                }

                this.MoveObjects(sourceListView, targetListView, sources, target, mode);
            }
            catch (Exception ex)
            {
                this.Feedback(ex.Message, true);
            }
            finally
            {
                this.isMoving = false;

                if (mode != FileMoveMode.Copy)
                {
                    this.RefreshListView(this.navigator);
                }

                this.RefreshListView(this.lvFiles);
            }
        }
Example #11
0
        private void InitDragDrop(ObjectListView listView)
        {
            listView.IsSimpleDragSource = true;
            listView.IsSimpleDropSink   = true;

            SimpleDropSink dropSink = (SimpleDropSink)listView.DropSink;

            dropSink.AcceptExternal      = true;
            dropSink.CanDropBetween      = true;
            dropSink.CanDropOnBackground = true;

            listView.ModelCanDrop += delegate(object sender, ModelDropEventArgs e)
            {
                bool isNavigator = this.IsNavigator(e.ListView);

                e.Handled = true;
                e.Effect  = DragDropEffects.None;

                if (e.SourceModels.Contains(e.TargetModel))
                {
                    e.InfoMessage = "Cannot drop on self";
                    return;
                }
                else if (e.TargetModel is MyFileSystemInfo info)
                {
                    string targetFolder = info.AsDirectory.FullName;

                    if (info.IsDirectory)
                    {
                        if (e.SourceModels.OfType <MyFileSystemInfo>().Any(item => item.IsDirectory && targetFolder.StartsWith(item.AsDirectory.FullName)))
                        {
                            e.InfoMessage = "Cannot drop on descendant";
                            return;
                        }
                    }
                }
                else
                {
                    if (e.SourceModels.OfType <MyFileSystemInfo>().Any(item => item.IsDrive))
                    {
                        return;
                    }

                    if (isNavigator)
                    {
                        if (e.TargetModel == null)
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (e.ListView.Tag == null)
                        {
                            return;
                        }
                    }
                }

                e.Effect = DragDropEffects.Move;
            };

            listView.ModelDropped += delegate(object sender, ModelDropEventArgs e)
            {
                bool isNavigator = this.IsNavigator(e.ListView);

                MyFileSystemInfo targetModel = null;

                if (isNavigator)
                {
                    targetModel = e.TargetModel as MyFileSystemInfo;
                }
                else
                {
                    targetModel = e.ListView.Tag as MyFileSystemInfo;
                }

                if (targetModel == null)
                {
                    return;
                }

                this.MoveFiles(e.SourceListView, e.ListView, e.SourceModels.OfType <MyFileSystemInfo>(), targetModel, FileMoveMode.Drag);

                e.RefreshObjects();

                e.Effect = DragDropEffects.None;
            };
        }