UUID Changes
[pithos-ms-client] / trunk / Pithos.Client.WPF / FileProperties / ConflictsViewModel.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Collections.ObjectModel;\r
4 using System.ComponentModel;\r
5 using System.ComponentModel.Composition;\r
6 using System.Diagnostics;\r
7 using System.IO;\r
8 using System.Linq;\r
9 using System.Text;\r
10 using Caliburn.Micro;\r
11 using Pithos.Client.WPF.Converters;\r
12 using Pithos.Client.WPF.Utils;\r
13 using Pithos.Core;\r
14 using Pithos.Interfaces;\r
15 \r
16 namespace Pithos.Client.WPF.FileProperties\r
17 {\r
18     [TypeConverter(typeof(EnumTypeConverter))]\r
19     public enum ConflictAction\r
20     {\r
21         [Description("Defer Decision")]\r
22         Defer,\r
23         [Description("Keep Local")]\r
24         KeepLocal,\r
25         [Description("Keep Server")]\r
26         KeepServer,\r
27        /* [Description("Keep Both")]\r
28         KeepBoth,*/\r
29         [Description("Clear Record")]\r
30         ClearLocal\r
31     }\r
32 \r
33     public class ConflictFile:PropertyChangedBase\r
34     {\r
35         private string _filePath;\r
36         public string FilePath\r
37         {\r
38             get { return _filePath; }\r
39             set\r
40             {\r
41                 _filePath = value;\r
42                 NotifyOfPropertyChange(()=>FilePath);\r
43             }\r
44         }\r
45 \r
46         private string _reason;\r
47         public string Reason\r
48         {\r
49             get\r
50             {\r
51                 return _reason;\r
52             }\r
53             set\r
54             {\r
55                 _reason = value;\r
56                 NotifyOfPropertyChange(() => Reason);\r
57             }\r
58         }\r
59 \r
60         private ConflictAction _action;\r
61         public ConflictAction Action\r
62         {\r
63             get { return _action; }\r
64             set\r
65             {\r
66                 _action = value;\r
67                 NotifyOfPropertyChange(()=>Action);\r
68             }\r
69         }\r
70 \r
71         public DateTime LocalModified { get; set; }\r
72 \r
73         public DateTime CloudModified { get; set; }\r
74 \r
75         public FileState State { get; set; }\r
76 \r
77         public void GoToFile()\r
78         {\r
79             if (!File.Exists(FilePath) && !Directory.Exists(FilePath))\r
80                 return;\r
81             Process.Start("explorer.exe", "/select, " + FilePath);\r
82         }\r
83     }\r
84     [Export(typeof(ConflictsViewModel))]\r
85     class ConflictsViewModel:Screen\r
86     {\r
87         [Import]\r
88         public IStatusKeeper StatusKeeper { get; set; }\r
89 \r
90         [Import]\r
91         public IConflictResolver Resolver { get; set; }\r
92 \r
93         private readonly ObservableCollection<ConflictFile> _conflicts=new ObservableCollection<ConflictFile>();\r
94 \r
95         public ObservableCollection<ConflictFile> Conflicts\r
96         {\r
97             get { return _conflicts; }\r
98         }\r
99 \r
100         public bool HasConflicts\r
101         {\r
102             get { return Conflicts!=null && Conflicts.Count > 0; }\r
103         }\r
104 \r
105         public bool HasNoConflicts\r
106         {\r
107             get { return !HasConflicts; }\r
108         }\r
109 \r
110         public string[]  Actions\r
111         {\r
112             get { return new[] {"Keep Local", "Keep Server", "Keep Both"}; }\r
113         }\r
114 \r
115         public ConflictsViewModel()\r
116         {\r
117                         this.DisplayName="Conflicts";            \r
118             \r
119         }\r
120 \r
121         protected override void OnViewLoaded(object view)\r
122         {\r
123             base.OnViewLoaded(view);\r
124             LoadConflicts();\r
125         }\r
126 \r
127         private void LoadConflicts()\r
128         {\r
129             var fileStates = StatusKeeper.GetConflictStates();\r
130             var conflicts = from state in fileStates\r
131                             let info = FileInfoExtensions.FromPath(state.FilePath)\r
132                             select new ConflictFile\r
133                                        {\r
134                                            FilePath = state.FilePath,\r
135                                            State = state,\r
136                                            Reason = state.ConflictReason ?? state.FileStatus.Name(),\r
137                                            LocalModified = info.LastWriteTime\r
138                                        };\r
139             Conflicts.Clear();\r
140             foreach (var conflict in conflicts)\r
141             {\r
142                 Conflicts.Add(conflict);\r
143             }\r
144             NotifyOfPropertyChange(() => Conflicts);\r
145             NotifyOfPropertyChange(() => HasConflicts);\r
146             NotifyOfPropertyChange(() => HasNoConflicts);\r
147             StatusKeeper.CleanupOrphanStates();\r
148         }\r
149 \r
150         /// <summary>\r
151         /// Open an explorer window to the target path's directory\r
152         /// and select the file\r
153         /// </summary>\r
154         /// <param name="entry"></param>\r
155         public void GoToFile(string fullPath)\r
156         {\r
157             if (!File.Exists(fullPath) && !Directory.Exists(fullPath))\r
158                 return;\r
159             Process.Start("explorer.exe", "/select, " + fullPath);\r
160         }\r
161 \r
162         public void Reload()\r
163         {\r
164             LoadConflicts();\r
165         }\r
166 \r
167         public void Apply()\r
168         {\r
169            /* var conflicts = from conflict in Conflicts\r
170                             where conflict.Action != ConflictAction.Defer\r
171                             select conflict;\r
172             Resolver.Resolve(conflicts);*/\r
173             \r
174             TryClose();\r
175         }\r
176 \r
177         public void Cancel()\r
178         {\r
179             TryClose();\r
180         }\r
181 \r
182         \r
183     }\r
184 \r
185     internal interface IConflictResolver\r
186     {\r
187         void Resolve(IEnumerable<ConflictFile> conflicts);\r
188     }\r
189 \r
190     public class DummyResolver:IConflictResolver\r
191     {\r
192 \r
193 \r
194         public void Resolve(IEnumerable<ConflictFile> conflicts)\r
195         {\r
196             \r
197         }\r
198     }\r
199 }\r