private void DeleteFile([NotNull] ProcessedEventArgs processed, long serverTimestamp, long serverFileSize)
        {
            Debug.ArgumentNotNull(processed, nameof(processed));

            if (IsModified || IsAdded)
            {
                ServerTimestamp    = serverTimestamp;
                ServerFileSize     = serverFileSize;
                ConflictResolution = ConflictResolution.NotResolved;
                processed.Text     = Resources.ProjectFileItem_DeleteFile_conflict;
                processed.Comment  = Resources.ProjectFileItem_DeleteFile_Local_file_has_been_modified;
                return;
            }

            if (IsValid)
            {
                try
                {
                    System.IO.File.Delete(AbsoluteFileName);
                }
                catch (Exception ex)
                {
                    processed.Text    = Resources.ProjectFileItem_DeleteFile_failed;
                    processed.Comment = ex.Message;
                }
            }

            Project.Remove(this);
        }
        public override void Revert(EventHandler <ProcessedEventArgs> callback)
        {
            Assert.ArgumentNotNull(callback, nameof(callback));

            UpdateCompleted process = delegate(string result, ref byte[] file, long serverTimestamp, long serverFileSize)
            {
                var processed = new ProcessedEventArgs(result);

                switch (result)
                {
                case "ok":
                    processed.Text = Resources.ProjectFileItem_Revert_reverted;
                    WriteFile(ref file, serverTimestamp, serverFileSize, processed);
                    ConflictResolution = ConflictResolution.None;
                    break;

                case "deleted":
                    processed.Text = Resources.ProjectFileItem_Revert_deleted;
                    DeleteFile(processed, serverTimestamp, serverFileSize);
                    Project.Remove(this);
                    break;

                case "conflict":
                    ServerTimestamp    = serverTimestamp;
                    ServerFileSize     = serverFileSize;
                    ConflictResolution = ConflictResolution.NotResolved;
                    processed.Text     = Resources.ProjectFileItem_Revert_conflict;
                    break;

                case "unchanged":
                    processed.Ignore = true;
                    break;
                }

                callback(this, processed);
            };

            var site = Project.Site;

            if (site != null)
            {
                site.DataService.Update(File, ServerTimestamp.ToString(CultureInfo.InvariantCulture), ServerFileSize, @"revert", process);
            }
        }
        public override void Commit(ProcessedEventHandler callback)
        {
            Assert.ArgumentNotNull(callback, nameof(callback));

            if (IsAdded && !IsValid)
            {
                Project.Remove(this);
                callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Commit_deleted));
                return;
            }

            var fileInfo    = new FileInfo(AbsoluteFileName);
            var fileContent = string.Empty;
            var action      = string.Empty;

            if (IsValid)
            {
                fileContent = System.Convert.ToBase64String(System.IO.File.ReadAllBytes(AbsoluteFileName));
            }
            else
            {
                action += Resources.ProjectFileItem_Commit__delete;
            }

            switch (ConflictResolution)
            {
            case ConflictResolution.NotResolved:
                callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Commit_skipped, Resources.ProjectFileItem_Commit_Conflict_not_resolved_));
                return;

            case ConflictResolution.UseLocalVersion:
                action += Resources.ProjectFileItem_Commit__overwrite;
                break;

            case ConflictResolution.UseServerVersion:
                callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Commit_skipped, Resources.ProjectFileItem_Commit_Use_Update_command_to_overwrite_local_version_with_server_version));
                return;
            }

            CommitCompleted process = delegate(string result, long serverTimestamp, long serverFileSize)
            {
                var processed = new ProcessedEventArgs(result);

                switch (result)
                {
                case "ok":
                    processed.Text     = IsAdded ? Resources.ProjectFileItem_Commit_added : Resources.ProjectFileItem_Commit_committed;
                    FileSize           = fileInfo.Length;
                    FileTimestamp      = fileInfo.LastWriteTimeUtc.Ticks;
                    ServerTimestamp    = serverTimestamp;
                    ServerFileSize     = serverFileSize;
                    ConflictResolution = ConflictResolution.None;
                    break;

                case "deleted":
                    Project.Remove(this);
                    processed.Text = Resources.ProjectFileItem_Commit_deleted;
                    break;

                case "conflict":
                    ServerTimestamp    = serverTimestamp;
                    ServerFileSize     = serverFileSize;
                    ConflictResolution = ConflictResolution.NotResolved;
                    processed.Text     = Resources.ProjectFileItem_Commit_conflict;
                    break;
                }

                callback(this, processed);
            };

            var site = Project.Site;

            if (site != null)
            {
                site.DataService.Commit(File, fileContent, ServerTimestamp, ServerFileSize, action, false, process);
            }
        }
        private void WriteFile([NotNull] ref byte[] file, long serverTimestamp, long serverFileSize, [NotNull] ProcessedEventArgs processed)
        {
            Debug.ArgumentNotNull(file, nameof(file));
            Debug.ArgumentNotNull(processed, nameof(processed));

            try
            {
                var directoryName = System.IO.Path.GetDirectoryName(AbsoluteFileName) ?? string.Empty;
                Directory.CreateDirectory(directoryName);

                System.IO.File.WriteAllBytes(AbsoluteFileName, file);

                var info = new FileInfo(AbsoluteFileName);

                FileSize        = info.Length;
                FileTimestamp   = info.LastWriteTimeUtc.Ticks;
                ServerTimestamp = serverTimestamp;
                ServerFileSize  = serverFileSize;
            }
            catch (Exception ex)
            {
                processed.Text    = Resources.ProjectFileItem_WriteFile_failed;
                processed.Comment = ex.Message;
            }
        }
        public override void Update(ProcessedEventHandler callback)
        {
            Assert.ArgumentNotNull(callback, nameof(callback));

            var action = string.Empty;

            switch (ConflictResolution)
            {
            case ConflictResolution.NotResolved:
                callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Update_skipped, Resources.ProjectFileItem_Update_Conflict_not_resolved));
                return;

            case ConflictResolution.UseLocalVersion:
                callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Update_skipped, Resources.ProjectFileItem_Update_Use_Commit_command_to_overwrite_server_version_with_local_version));
                return;

            case ConflictResolution.UseServerVersion:
                action += Resources.ProjectFileItem_Update__revert;
                break;
            }

            UpdateCompleted process = delegate(string result, ref byte[] file, long serverTimestamp, long serverFileSize)
            {
                var processed = new ProcessedEventArgs(result);

                switch (result)
                {
                case "ok":
                    if ((IsModified || IsAdded) && action.IndexOf(Resources.ProjectFileItem_Update_revert, StringComparison.InvariantCultureIgnoreCase) < 0)
                    {
                        ServerTimestamp    = serverTimestamp;
                        ServerFileSize     = serverFileSize;
                        ConflictResolution = ConflictResolution.NotResolved;
                        processed.Text     = Resources.ProjectFileItem_Update_conflict;
                        processed.Comment  = Resources.ProjectFileItem_Update_Local_file_has_been_modified;
                    }
                    else
                    {
                        processed.Text = Resources.ProjectFileItem_Update_updated;
                        WriteFile(ref file, serverTimestamp, serverFileSize, processed);
                        ConflictResolution = ConflictResolution.None;
                    }

                    break;

                case "deleted":
                    processed.Text = Resources.ProjectFileItem_Update_deleted;
                    DeleteFile(processed, serverTimestamp, serverFileSize);
                    break;

                case "conflict":
                    ServerTimestamp    = serverTimestamp;
                    ServerFileSize     = serverFileSize;
                    ConflictResolution = ConflictResolution.NotResolved;
                    processed.Text     = Resources.ProjectFileItem_Update_conflict;
                    break;

                case "unchanged":
                    processed.Ignore = true;
                    break;
                }

                callback(this, processed);
            };

            Project.Site.DataService.Update(File, ServerTimestamp.ToString(CultureInfo.InvariantCulture), ServerFileSize, action, process);
        }