Esempio n. 1
0
        private SerializedCommand CreateDeleteCommand(object obj)
        {
            SerializedCommand cmd = null;

            if (obj is FileInfo)
            {
                var fileInfo = obj as FileInfo;

                cmd = CreateCommand(
                    SerializedCommand.UndoRedo.Do,
                    FileOperation.Recycle.ToString(),
                    fileInfo.FullName);
            }
            else if (obj is DirectoryInfo)
            {
                var directoryInfo = obj as DirectoryInfo;

                cmd = CreateCommand(
                    SerializedCommand.UndoRedo.Do,
                    FileOperation.Recycle.ToString(),
                    directoryInfo.FullName);
            }
            else
            {
                throw new NotSupportedException("\"" + obj.GetType().Name + "\" is not supported");
            }

            return(cmd);
        }
Esempio n. 2
0
        private static void TestUndoFileReader()
        {
            UndoFileReader reader = new UndoFileReader(
                VirtualDrive.VirtualFileName("TestUndoFileWriter.txt"));

            UnitTest.Test(reader.NumCommands() == 4);

            for (int i = 3; i >= 0; i--)
            {
                SerializedCommand cmd = reader.CommandByIndex(i);
                UnitTest.Test(cmd.Target == typeof(TestClass).FullName);
                UnitTest.Test(cmd.Operation == "TestUndoFileWriter");

                UnitTest.Test(cmd.Data.Length == i);
                for (int j = 0; j < i; j++)
                {
                    UnitTest.Test(cmd.Data[j] == "line" + j);
                }
            }

            for (int i = 0; i < 4; i++)
            {
                SerializedCommand cmd = reader.CommandByIndex(i);
                UnitTest.Test(cmd.Target == typeof(TestClass).FullName);
                UnitTest.Test(cmd.Operation == "TestUndoFileWriter");

                UnitTest.Test(cmd.Data.Length == i);
                for (int j = 0; j < i; j++)
                {
                    UnitTest.Test(cmd.Data[j] == "line" + j);
                }
            }

            reader.Close();
        }
Esempio n. 3
0
        public static void ProcessCommand(SerializedCommand command)
        {
            byte[]   tag      = System.Convert.FromBase64String(command.Data[1].ToString());
            FileInfo fileInfo = new FileInfo(command.Data[0]);

            TagUtils.WriteTag(tag, fileInfo);
        }
Esempio n. 4
0
 private SerializedCommand CreateUndoCommand(SerializedCommand doCommand)
 {
     return(new SerializedCommand(
                GetType(),
                SerializedCommand.UndoRedo.Undo,
                doCommand.Operation.ToString(),
                doCommand.Data.Reverse().ToArray()));
 }
Esempio n. 5
0
        private void ProcessCommand(SerializedCommand command)
        {
            Logger.WriteLine(Tokens.Info, command.Direction + " " + command.Data[0]);

            Type       type   = Type.GetType(command.Target);
            MethodInfo method = type.GetMethod("ProcessCommand");

            method.Invoke(null, new object[] { command });
        }
Esempio n. 6
0
        public static void ProcessCommand(SerializedCommand command)
        {
            if (command.OperationEnum <FileOperation>() == FileOperation.Recycle)
            {
                Logger.WriteLine(
                    Tokens.Info,
                    command.OperationEnum <FileOperation>().ToString()
                    + " \"" + command.Data[0]
                    + "\"");
            }
            else
            {
                Logger.WriteLine(
                    Tokens.Info,
                    command.OperationEnum <FileOperation>().ToString()
                    + " \"" + command.Data[0]
                    + "\" to \"" + command.Data[1]
                    + "\"");
            }

            switch (command.Direction)
            {
            case SerializedCommand.UndoRedo.Do:

                switch (command.OperationEnum <FileOperation>())
                {
                case FileOperation.Copy: ProcessCopy(command); break;

                case FileOperation.Move: ProcessMove(command); break;

                case FileOperation.Recycle: ProcessRecycle(command); break;

                default: throw new NotSupportedException("Unknown operation");
                }
                break;

            case SerializedCommand.UndoRedo.Undo:

                switch (command.OperationEnum <FileOperation>())
                {
                case FileOperation.Copy: ProcessDelete(command); break;

                case FileOperation.Move: ProcessMove(command); break;

                case FileOperation.Recycle: ProcessDerecycle(command); break;

                default: throw new NotSupportedException("Unknown operation");
                }
                break;

            default:
                throw new NotSupportedException("Unknown direction");
            }
        }
Esempio n. 7
0
 private static void ProcessDelete(SerializedCommand command)
 {
     if (VirtualDrive.ExistsDirectory(command.Data[0]))
     {
         VirtualDrive.DeleteDirectory(command.Data[0], true);
     }
     else if (VirtualDrive.ExistsFile(command.Data[0]))
     {
         VirtualDrive.DeleteFile(command.Data[0]);
     }
     else
     {
         throw new Exception("\"" + command.Data[0] + "\" does not exist!");
     }
 }
Esempio n. 8
0
 private static void ProcessCopy(SerializedCommand command)
 {
     if (VirtualDrive.ExistsDirectory(command.Data[0]))
     {
         //CoreVirtualDrive.FileSystemOperations.SafeOperations.CopyDirectory(command.Data[0], command.Data[1]);
         VirtualDrive.CopyDirectory(command.Data[0], command.Data[1]);
     }
     else if (VirtualDrive.ExistsFile(command.Data[0]))
     {
         //CoreVirtualDrive.FileSystemOperations.SafeOperations.CopyFile(command.Data[0], command.Data[1]);
         VirtualDrive.CopyFile(command.Data[0], command.Data[1]);
     }
     else
     {
         throw new Exception("\"" + command.Data[0] + "\" does not exist!");
     }
 }
Esempio n. 9
0
        public virtual void Process(object obj)
        {
            FileInfo fileInfo = obj as FileInfo;

            Logger.WriteLine(Tokens.Info, "File \"" + fileInfo + "\"");

            byte[][] before = TagUtils.ReadTagsRaw(fileInfo);
            byte[][] after  = Do(fileInfo, before);

            SerializedCommand cmdUndo = CreateCommand(fileInfo.FullName, before, SerializedCommand.UndoRedo.Undo);
            SerializedCommand cmdDo   = CreateCommand(fileInfo.FullName, after, SerializedCommand.UndoRedo.Do);

            ProcessCommand(cmdDo);

            if (!Object.ReferenceEquals(UndoFile, null))
            {
                UndoFile.Write(cmdUndo);
                UndoFile.Write(cmdDo);
            }
        }
Esempio n. 10
0
        public virtual void Process(object obj)
        {
            SerializedCommand cmd = CreateDoCommand(obj);

            if (Operation == FileOperation.Copy || Operation == FileOperation.Move)
            {
                if (String.IsNullOrEmpty(NewName))
                {
                    throw new Exception(Operation.ToString() + " invalid filename: \"" + NewName + "\"");
                }

                string src = cmd.Data[0];
                string dst = cmd.Data[1];

                if (src.Equals(dst))
                {
                    return;
                }

                if (AlreadyExists(src, dst))
                {
                    if (Conflicts == ConflictSolving.Skip)
                    {
                        Logger.WriteLine(Tokens.Warning, Operation.ToString() + " already exists: \"" + dst + "\"");
                        return;
                    }
                    else if (Conflicts == ConflictSolving.Overwrite)
                    {
                        MoveToRecycleBin(dst);
                    }
                }
            }

            ProcessCommand(cmd);

            if (!Object.ReferenceEquals(UndoFile, null))
            {
                UndoFile.Write(CreateUndoCommand(cmd));
                UndoFile.Write(cmd);
            }
        }
Esempio n. 11
0
        public static void ProcessCommand(SerializedCommand cmd)
        {
            FileInfo fileInfo = new FileInfo(cmd.Data[0]);

            byte[][] tags = (from item in cmd.Data.Skip(1)
                             select System.Convert.FromBase64String(item)).ToArray();

            if (tags.Length == 1 && TagUtils.HasTagV1(tags[0]) && TagUtils.HasTagV2(fileInfo))
            {
                TagUtils.StripTagV2(fileInfo, 0);
            }
            else if (tags.Length == 1 && TagUtils.HasTagV2(tags[0]) && TagUtils.HasTagV1(fileInfo))
            {
                TagUtils.StripTagV1(fileInfo);
            }

            foreach (var tag in tags)
            {
                TagUtils.WriteTag(tag, fileInfo, WriteStrategy);
            }
        }
Esempio n. 12
0
        private SerializedCommand CreateDoCommand(object obj)
        {
            SerializedCommand cmd = null;

            if (Operation == FileOperation.Copy || Operation == FileOperation.Move)
            {
                if (obj is FileInfo)
                {
                    var fileInfo = obj as FileInfo;

                    cmd = CreateCommand(
                        SerializedCommand.UndoRedo.Do,
                        Operation.ToString(),
                        fileInfo.FullName,
                        Path.Combine(fileInfo.DirectoryName, NewName));
                }
                else if (obj is DirectoryInfo)
                {
                    var directoryInfo = obj as DirectoryInfo;

                    cmd = CreateCommand(
                        SerializedCommand.UndoRedo.Do,
                        Operation.ToString(),
                        directoryInfo.FullName,
                        NewName);
                }
                else
                {
                    throw new NotSupportedException("Unknown type");
                }
            }
            else if (Operation == FileOperation.Recycle)
            {
                cmd = CreateDeleteCommand(obj);
            }

            return(cmd);
        }
Esempio n. 13
0
 private static void ProcessDerecycle(SerializedCommand command)
 {
     CoreVirtualDrive.RecycleBin.Instance.Restore(command.Data[0]);
 }
Esempio n. 14
0
 public void Write(SerializedCommand command)
 {
     command.Write(Writer);
     Writer.Flush();
 }