Added option to disable selective Synchronization
[pithos-ms-client] / trunk / Pithos.Core / Agents / SelectiveUris.cs
1 using System;\r
2 using System.Collections.Concurrent;\r
3 using System.Collections.Generic;\r
4 using System.ComponentModel.Composition;\r
5 using System.IO;\r
6 using System.Linq;\r
7 using System.Text;\r
8 using Pithos.Interfaces;\r
9 using Pithos.Network;\r
10 \r
11 namespace Pithos.Core.Agents\r
12 {\r
13     [Export(typeof(Selectives))]\r
14     public class Selectives\r
15     {\r
16 \r
17         public ConcurrentDictionary<Uri, List<Uri>> SelectiveUris { get; private set; }\r
18 \r
19         private ConcurrentDictionary<Uri, List<string>> SelectivePaths { get; set; }\r
20 \r
21         public Selectives()\r
22         {\r
23             SelectiveUris = new ConcurrentDictionary<Uri, List<Uri>>();\r
24             SelectivePaths = new ConcurrentDictionary<Uri, List<string>>();\r
25         }\r
26 \r
27         readonly Dictionary<Uri, bool> _selectiveEnabled = new Dictionary<Uri, bool>();\r
28 \r
29 /*\r
30         public void SetIsSelectiveEnabled(AccountInfo account,bool value)\r
31         {\r
32             _selectiveEnabled[account.AccountKey]=value;\r
33         }\r
34 */\r
35 \r
36         public void SetIsSelectiveEnabled(Uri accountKey,bool value)\r
37         {\r
38             _selectiveEnabled[accountKey]=value;\r
39         }\r
40 /*\r
41 \r
42         public bool IsSelectiveEnabled(AccountInfo account)\r
43         {\r
44             bool isEnabled;\r
45             _selectiveEnabled.TryGetValue(account.AccountKey, out isEnabled);\r
46             return isEnabled;\r
47         }\r
48 */\r
49 \r
50         public bool IsSelectiveEnabled(Uri accountKey)\r
51         {\r
52             bool isEnabled;\r
53             _selectiveEnabled.TryGetValue(accountKey, out isEnabled);\r
54             return isEnabled;\r
55         }\r
56 \r
57 \r
58         public void SetSelectedUris(AccountInfo account,List<Uri> uris)\r
59         {\r
60             SelectiveUris[account.AccountKey] = uris;\r
61             SelectivePaths[account.AccountKey] = UrisToFilePaths(account,uris);\r
62         }\r
63 \r
64         public bool IsSelected(ObjectInfo info)\r
65         {\r
66             //Shared folders should NOT be synced if selective syncing is disabled\r
67             var isShared = info.IsShared;\r
68             var selectiveEnabled = IsSelectiveEnabled(info.AccountKey);\r
69             if (!selectiveEnabled)\r
70                 return !isShared;\r
71 \r
72             List<Uri> filterUris;\r
73             return !SelectiveUris.TryGetValue(info.AccountKey, out filterUris) \r
74                 || filterUris.Count ==0\r
75                 || filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f));\r
76         }\r
77 \r
78         public bool IsSelected(AccountInfo account,FileSystemInfo info)\r
79         {\r
80             if (info is DirectoryInfo)\r
81                 return true;\r
82             return IsSelected(account,info.FullName);\r
83         }\r
84 \r
85         public bool IsSelected(AccountInfo account, string fullPath)\r
86         {\r
87             if (Directory.Exists(fullPath))\r
88                 return true;\r
89 \r
90             //Shared folders should NOT be synced if selective syncing is disabled\r
91             var isShared = fullPath.IsSharedTo(account);\r
92             var selectiveEnabled = IsSelectiveEnabled(account.AccountKey);\r
93             if (!selectiveEnabled)\r
94                 return !isShared;\r
95 \r
96             List<string> paths;\r
97             return !SelectivePaths.TryGetValue(account.AccountKey, out paths)\r
98                 || paths.Count == 0\r
99                 || paths.Any(fullPath.IsAtOrDirectlyBelow);\r
100         }\r
101 \r
102         /// <summary>\r
103         /// Return a list of absolute filepaths from a list of Uris\r
104         /// </summary>\r
105         /// <param name="uris"></param>\r
106         /// <returns></returns>\r
107         private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)\r
108         {\r
109             if (uris == null)\r
110                 return new List<string>();\r
111 \r
112             var accountPath = account.AccountPath;\r
113             var storageUrl = account.StorageUri.ToString();\r
114             var own = (from uri in uris\r
115                        where uri.ToString().StartsWith(storageUrl)\r
116                        let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()\r
117                        //Trim the account name\r
118                        select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList();\r
119             var others = (from uri in uris\r
120                           where !uri.ToString().StartsWith(storageUrl)\r
121                           let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()\r
122                           //Trim the account name\r
123                           select Path.Combine(accountPath, "others-shared", relativePath)).ToList();\r
124             return own.Union(others).ToList();\r
125         }\r
126 \r
127     }\r
128 }\r