예제 #1
0
        private void DisplayDirectory(SynchronizedDirectory parentDirectory, string depthIndentation = "")
        {
            const int width = 80;

            foreach (var file in parentDirectory.Files.Where(x => x.Operation != SynchronizationOperation.Nothing))
            {
                if (file.Operation == SynchronizationOperation.Keep)
                {
                    continue;
                }

                _userInterface.ReportInformation(string.Format("{0} {1} {2,-6} {3,11} {4,11}",
                                                               depthIndentation,
                                                               file.Name.PadRight(width - depthIndentation.Length),
                                                               file.Operation.ToString(),
                                                               FormatFileSize(file.SizeOnComputer),
                                                               FormatFileSize(file.SizeOnDevice)
                                                               ));
            }

            foreach (var directory in parentDirectory.Directories.Where(x => x.Operation != SynchronizationOperation.Nothing))
            {
                //if (directory.Operation == SynchronizationOperation.Delete)
                //    File.WriteAllText(Path.Combine(Path.Combine(RootComputerPath, directory.Path), SynchronizationMarkerFileName), "");

                _userInterface.ReportInformation(string.Format("{0} {1} {2,-6}",
                                                               depthIndentation,
                                                               directory.Name.PadRight(width - depthIndentation.Length),
                                                               directory.Operation.ToString()
                                                               ));
                DisplayDirectory(directory, depthIndentation + "  ");
            }
        }
예제 #2
0
        private void SynchronizeDirectory(SynchronizedDirectory parentDirectory, StorageServicesAdapter storageServiceAdapter)
        {
            var operation = parentDirectory.Operation;

            if (operation == SynchronizationOperation.Copy)
            {
                _userInterface.ReportInformation(string.Format("Create directory {0}", parentDirectory.Path));
                DoOperation(storageServiceAdapter, parentDirectory);
            }

            foreach (var file in parentDirectory.Files.Where(file => file.Operation == SynchronizationOperation.Delete))
            {
                _userInterface.ReportInformation(string.Format("Delete file      {0}", file.Path));
                DoOperation(storageServiceAdapter, file);
            }

            foreach (var directory in parentDirectory.Directories)
            {
                SynchronizeDirectory(directory, storageServiceAdapter);
            }

            foreach (var file in parentDirectory.Files.Where(file => file.Operation == SynchronizationOperation.Copy))
            {
                _userInterface.ReportInformation(string.Format("Copy file        {0}", file.Path));
                DoOperation(storageServiceAdapter, file);
            }

            if (operation == SynchronizationOperation.Delete)
            {
                _userInterface.ReportInformation(string.Format("Delete directory {0}", parentDirectory.Path));
                DoOperation(storageServiceAdapter, parentDirectory);
            }
        }
예제 #3
0
        private void ExploreDeviceDirectory(PortableDeviceContainerObject container, SynchronizedDirectory parentDirectory, int remaingDepthToIgnore)
        {
            if (remaingDepthToIgnore == 0)
            {
                parentDirectory.ObjectOnDevice = container;
            }

            foreach (var child in container.Childs)
            {
                var fileObject = child as PortableDeviceFileObject;
                if (fileObject != null)
                {
                    if (remaingDepthToIgnore > 0)
                    {
                        continue;
                    }

                    var file = new SynchronizedFile();
                    file.Parent = parentDirectory;
                    file.Name   = fileObject.FileName;
                    file.ComputePath();
                    file.SizeOnDevice   = fileObject.Size;
                    file.IsOnDevice     = true;
                    file.ObjectOnDevice = fileObject;

                    parentDirectory.Files.Add(file);
                }
                else
                {
                    var containerObject = child as PortableDeviceContainerObject;
                    if (containerObject != null)
                    {
                        if (remaingDepthToIgnore > 0)
                        {
                            ExploreDeviceDirectory(containerObject, parentDirectory, remaingDepthToIgnore - 1);
                        }
                        else
                        {
                            var directory = new SynchronizedDirectory();
                            directory.Parent = parentDirectory;
                            directory.Name   = containerObject.Name;
                            directory.ComputePath();
                            directory.IsOnDevice     = true;
                            directory.ObjectOnDevice = containerObject;

                            ExploreDeviceDirectory(containerObject, directory, remaingDepthToIgnore - 1);

                            parentDirectory.Directories.Add(directory);
                        }
                    }
                }
            }
        }
예제 #4
0
        private bool ExploreComputerDirectory(DirectoryInfo container, SynchronizedDirectory parentDirectory)
        {
            var shouldBeSynchronized = container.GetFiles(_options.SynchronizationMarkerFileName).Length > 0;

            if (shouldBeSynchronized)
            {
                foreach (var child in container.GetFiles())
                {
                    if (child.Name == _options.SynchronizationMarkerFileName)
                    {
                        continue;
                    }
                    var file = parentDirectory.Files.FirstOrDefault(x => x.Name.ToLower() == child.Name.ToLower());
                    if (file == null)
                    {
                        file        = new SynchronizedFile();
                        file.Parent = parentDirectory;
                        file.Name   = child.Name;
                        file.ComputePath();
                        parentDirectory.Files.Add(file);
                    }
                    file.IsOnComputer     = true;
                    file.SizeOnComputer   = child.Length;
                    file.ObjectOnComputer = child;
                }
            }

            foreach (var child in container.GetDirectories())
            {
                var directory = parentDirectory.Directories.FirstOrDefault(x => x.Name.ToLower() == child.Name.ToLower());
                if (directory == null)
                {
                    directory        = new SynchronizedDirectory();
                    directory.Parent = parentDirectory;
                    directory.Name   = child.Name;
                    directory.ComputePath();
                    parentDirectory.Directories.Add(directory);
                }

                var childShouldBeSynchronized = ExploreComputerDirectory(child, directory);
                shouldBeSynchronized = shouldBeSynchronized || childShouldBeSynchronized;
            }

            parentDirectory.IsOnComputer     = shouldBeSynchronized;
            parentDirectory.ObjectOnComputer = container;
            return(shouldBeSynchronized);
        }
예제 #5
0
        private void DoOperation(StorageServicesAdapter storageServiceAdapter, SynchronizedDirectory directory)
        {
            try
            {
                var operation = directory.Operation;
                if (operation == SynchronizationOperation.Delete)
                {
                    storageServiceAdapter.Delete(directory.ObjectOnDevice);
                    directory.IsOnDevice     = false;
                    directory.ObjectOnDevice = null;
                }

                if (operation == SynchronizationOperation.Copy)
                {
                    directory.ObjectOnDevice = (PortableDeviceContainerObject)storageServiceAdapter.Mkdir(directory.Parent.ObjectOnDevice, directory.Name);
                    directory.IsOnDevice     = true;
                }
            }
            catch (Exception e)
            {
                _userInterface.ReportError(string.Format("Exception: {0}", e.Message));
            }
        }