b5237420f3a3e4558520f51451ca3ec40e8eb6a4
[pithos-ms-client] / trunk%2FPithos.Client.WPF%2FFileProperties%2FConflictsViewModel.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.ComponentModel;
5 using System.ComponentModel.Composition;
6 using System.Diagnostics;
7 using System.IO;
8 using System.Linq;
9 using System.Text;
10 using Caliburn.Micro;
11 using Pithos.Client.WPF.Converters;
12 using Pithos.Core;
13 using Pithos.Interfaces;
14
15 namespace Pithos.Client.WPF.FileProperties
16 {
17     [TypeConverter(typeof(EnumTypeConverter))]
18     public enum ConflictAction
19     {
20         [Description("Defer Decision")]
21         Defer,
22         [Description("Keep Local")]
23         KeepLocal,
24         [Description("Keep Server")]
25         KeepServer,
26         [Description("Keep Both")]
27         KeepBoth,
28         [Description("Clear Record")]
29         ClearLocal
30     }
31
32     public class ConflictFile:PropertyChangedBase
33     {
34         private string _filePath;
35         public string FilePath
36         {
37             get { return _filePath; }
38             set
39             {
40                 _filePath = value;
41                 NotifyOfPropertyChange(()=>FilePath);
42             }
43         }
44
45         private ConflictAction _action;
46
47         public ConflictAction Action
48         {
49             get { return _action; }
50             set
51             {
52                 _action = value;
53                 NotifyOfPropertyChange(()=>Action);
54             }
55         }
56     }
57     [Export(typeof(ConflictsViewModel))]
58     class ConflictsViewModel:Screen
59     {
60         [Import]
61         public IStatusKeeper StatusKeeper { get; set; }
62
63         [Import]
64         public IConflictResolver Resolver { get; set; }
65
66         private readonly ObservableCollection<ConflictFile> _conflicts;
67
68         public ObservableCollection<ConflictFile> Conflicts
69         {
70             get { return _conflicts; }
71         }
72
73         public string[]  Actions
74         {
75             get { return new[] {"Keep Local", "Keep Server", "Keep Both"}; }
76         }
77
78         public ConflictsViewModel()
79         {
80                         this.DisplayName="Conflicts";
81             var conflicts = from state in FileState.Queryable
82                             /*where state.FileStatus == FileStatus.Conflict ||
83                                   state.OverlayStatus == FileOverlayStatus.Conflict*/
84                             select new ConflictFile {FilePath = state.FilePath};          
85             _conflicts = new ObservableCollection<ConflictFile>(conflicts.ToList());
86             
87         }
88
89         /// <summary>
90         /// Open an explorer window to the target path's directory
91         /// and select the file
92         /// </summary>
93         /// <param name="entry"></param>
94         public void GoToFile(string fullPath)
95         {
96             if (!File.Exists(fullPath) && !Directory.Exists(fullPath))
97                 return;
98             Process.Start("explorer.exe", "/select, " + fullPath);
99         }
100
101         public void Apply()
102         {
103             var conflicts = from conflict in Conflicts
104                             where conflict.Action != ConflictAction.Defer
105                             select conflict;
106             Resolver.Resolve(conflicts);
107             
108             TryClose(true);
109         }
110
111         public void Cancel()
112         {
113             TryClose(false);
114         }
115
116         
117     }
118
119     internal interface IConflictResolver
120     {
121         void Resolve(IEnumerable<ConflictFile> conflicts);
122     }
123 }