Modified Selectives.IsSelected(ObjectInfo) to include the user's account, to properly...
[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(AccountInfo account,ObjectInfo info)\r
65         {\r
66             //Shared folders should NOT be synced if selective syncing is disabled\r
67             var isShared = info.IsShared??false;\r
68             if (info.StorageUri == null)\r
69                 return true;\r
70             var selectiveEnabled = IsSelectiveEnabled(account.AccountKey);\r
71             if (!selectiveEnabled)\r
72                 return !isShared;\r
73 \r
74             List<Uri> filterUris;\r
75             return !SelectiveUris.TryGetValue(account.AccountKey, out filterUris) \r
76                 || filterUris.Count ==0\r
77                 || filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f));\r
78         }\r
79 \r
80         public bool IsSelected(AccountInfo account,FileSystemInfo info)\r
81         {\r
82             if (info is DirectoryInfo)\r
83                 return true;\r
84             return IsSelected(account,info.FullName);\r
85         }\r
86 \r
87         public bool IsSelected(AccountInfo account, string fullPath)\r
88         {\r
89             if (Directory.Exists(fullPath))\r
90                 return true;\r
91 \r
92             //Shared folders should NOT be synced if selective syncing is disabled\r
93             var isShared = fullPath.IsSharedTo(account);\r
94             var selectiveEnabled = IsSelectiveEnabled(account.AccountKey);\r
95             if (!selectiveEnabled)\r
96                 return !isShared;\r
97 \r
98             List<string> paths;\r
99             return !SelectivePaths.TryGetValue(account.AccountKey, out paths)\r
100                 || paths.Count == 0\r
101                 || paths.Any(fullPath.IsAtOrDirectlyBelow);\r
102         }\r
103 \r
104         /// <summary>\r
105         /// Return a list of absolute filepaths from a list of Uris\r
106         /// </summary>\r
107         /// <param name="uris"></param>\r
108         /// <returns></returns>\r
109         private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)\r
110         {\r
111             if (uris == null)\r
112                 return new List<string>();\r
113 \r
114             var accountPath = account.AccountPath;\r
115             var storageUrl = account.StorageUri.ToString();\r
116             var own = (from uri in uris\r
117                        where uri.ToString().StartsWith(storageUrl)\r
118                        let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()\r
119                        //Trim the account name\r
120                        select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList();\r
121             var others = (from uri in uris\r
122                           where !uri.ToString().StartsWith(storageUrl)\r
123                           let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()\r
124                           //Trim the account name\r
125                           select Path.Combine(accountPath, "others-shared", relativePath)).ToList();\r
126             return own.Union(others).ToList();\r
127         }\r
128 \r
129     }\r
130 }\r