using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.ComponentModel.Composition; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using Caliburn.Micro; using Pithos.Client.WPF.Converters; using Pithos.Core; using Pithos.Interfaces; namespace Pithos.Client.WPF.FileProperties { [TypeConverter(typeof(EnumTypeConverter))] public enum ConflictAction { [Description("Defer Decision")] Defer, [Description("Keep Local")] KeepLocal, [Description("Keep Server")] KeepServer, [Description("Keep Both")] KeepBoth, [Description("Clear Record")] ClearLocal } public class ConflictFile:PropertyChangedBase { private string _filePath; public string FilePath { get { return _filePath; } set { _filePath = value; NotifyOfPropertyChange(()=>FilePath); } } private string _reason; public string Reason { get { return _reason; } set { _reason = value; NotifyOfPropertyChange(() => Reason); } } private ConflictAction _action; public ConflictAction Action { get { return _action; } set { _action = value; NotifyOfPropertyChange(()=>Action); } } public DateTime LocalModified { get; set; } public DateTime CloudModified { get; set; } public void GoToFile() { if (!File.Exists(FilePath) && !Directory.Exists(FilePath)) return; Process.Start("explorer.exe", "/select, " + FilePath); } } [Export(typeof(ConflictsViewModel))] class ConflictsViewModel:Screen { [Import] public IStatusKeeper StatusKeeper { get; set; } [Import] public IConflictResolver Resolver { get; set; } private readonly ObservableCollection _conflicts; public ObservableCollection Conflicts { get { return _conflicts; } } public string[] Actions { get { return new[] {"Keep Local", "Keep Server", "Keep Both"}; } } public ConflictsViewModel() { this.DisplayName="Conflicts"; var conflicts = from state in FileState.Queryable where state.FileStatus == FileStatus.Conflict || state.OverlayStatus == FileOverlayStatus.Conflict let info=FileInfoExtensions.FromPath(state.FilePath) select new ConflictFile {FilePath = state.FilePath,Reason=state.ConflictReason,LocalModified = info.LastWriteTime}; _conflicts = new ObservableCollection(conflicts.ToList()); } /// /// Open an explorer window to the target path's directory /// and select the file /// /// public void GoToFile(string fullPath) { if (!File.Exists(fullPath) && !Directory.Exists(fullPath)) return; Process.Start("explorer.exe", "/select, " + fullPath); } public void Apply() { var conflicts = from conflict in Conflicts where conflict.Action != ConflictAction.Defer select conflict; Resolver.Resolve(conflicts); TryClose(true); } public void Cancel() { TryClose(false); } } internal interface IConflictResolver { void Resolve(IEnumerable conflicts); } }