Added Clear Conflict action to resolver
[pithos-ms-client] / trunk / Pithos.Client.WPF / FileProperties / ConflictResolver.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Linq;
5 using System.Text;
6 using Caliburn.Micro;
7 using Pithos.Core;
8 using Pithos.Core.Agents;
9 using Pithos.Interfaces;
10 using Pithos.Network;
11
12 namespace Pithos.Client.WPF.FileProperties
13 {
14     [Export(typeof(IConflictResolver))]
15     public class ConflictResolver : IConflictResolver
16     {
17         [Import]
18         public IStatusKeeper StatusAgent { get; set; }
19
20         [Import]
21         public NetworkAgent NetworkAgent { get; set; }
22
23         public void Resolve(IEnumerable<ConflictFile> conflicts)
24         {
25             KeepServer(conflicts.Where(c => c.Action == ConflictAction.KeepServer));
26             KeepLocal(conflicts.Where(c => c.Action == ConflictAction.KeepLocal));
27             KeepBoth(conflicts.Where(c => c.Action == ConflictAction.KeepBoth));
28             ClearLocal(conflicts.Where(c => c.Action == ConflictAction.ClearLocal));
29         }
30
31
32         //Clearing the local state means that we clear the conflict state of the local file.
33         //This is needed to clear wrong conflicts
34         private void ClearLocal(IEnumerable<ConflictFile> conflicts)
35         {
36             //This can be done simply by changing the local Filestate status to normal
37             conflicts.Apply(clear =>
38                 StatusAgent.SetFileState(clear.FilePath, FileStatus.Unchanged, FileOverlayStatus.Normal, ""));    
39
40         }
41
42
43         //Keeping the server version means that we need to 
44         //download the server's version of the file.
45         private void KeepServer(IEnumerable<ConflictFile> conflicts)
46         {
47             //This can be done either by scheduling the appropriate action, ignoring the hash
48             //Or directly downloading the file.
49
50             
51             foreach (var conflict in conflicts)
52             {
53                 //a.Post(new CloudDownloadAction());    
54                 PithosMonitor monitor;
55 /*
56                 var account = monitor.Account;
57                 var oi=monitor.GetObjectInfo(conflict.FilePath);                                
58                 NetworkAgent.Post(new CloudDownloadAction(account,oi,"Resolver"));
59 */
60             }
61             
62
63         }
64
65         //Keeping the server version means that we need to 
66         //upload the local version of the file to the server
67         private void KeepLocal(IEnumerable<ConflictFile> conflicts)
68         {
69             //This can be done either by scheduling the appropriate action, ignoring the hash
70             //Or directly uploading the file.
71             foreach (var conflict in conflicts)
72             {
73                 var info = FileInfoExtensions.FromPath(conflict.FilePath);
74                 var state=StatusAgent.GetStateByFilePath(conflict.FilePath);
75                 PithosMonitor monitor;
76
77 /*
78                                         var pair=(from monitor in  Monitors
79                                                            where conflict.FilePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
80                                                                    select monitor).FirstOrDefault();
81                         var accountMonitor = pair.Value;
82 */
83 /*
84                 var account = monitor.Account;
85                 //var oi=monitor.GetObjectInfo(conflict.FilePath);                                
86                 NetworkAgent.Post(new CloudUploadAction(account,info,state,account.BlockSize,account.BlockHash,"Resolver"));
87                 //NetworkAgent.Post(new CloudUploadAction(,info,conflict.FileState,NetworkAgent.));
88 */
89             }
90         }
91
92         //Keeping both versions means that we need to copy one of them
93         //somewhere and keep the other 
94         private void KeepBoth(IEnumerable<ConflictFile> conflicts)
95         {
96             //We can copy the rename the local file to another name and download the server file
97             //Or rename the server file and upload the local file
98
99             //Downloading the server file is probably much faster with ADSL connections
100
101             //We could probably use the local file as a source of blocks to download fewer data,
102             //We create a hashmap of the local file, download the differences from the server
103             //and merge common and changed blocks to create the new file.
104         }
105     }
106 }