2 using System.Collections.Concurrent;
\r
3 using System.Collections.Generic;
\r
4 using System.ComponentModel.Composition;
\r
8 using Pithos.Interfaces;
\r
9 using Pithos.Network;
\r
11 namespace Pithos.Core.Agents
\r
13 [Export(typeof(Selectives))]
\r
14 public class Selectives
\r
17 public ConcurrentDictionary<Uri, List<Uri>> SelectiveUris { get; private set; }
\r
19 private ConcurrentDictionary<Uri, List<string>> SelectivePaths { get; set; }
\r
23 SelectiveUris = new ConcurrentDictionary<Uri, List<Uri>>();
\r
24 SelectivePaths = new ConcurrentDictionary<Uri, List<string>>();
\r
27 readonly Dictionary<Uri, bool> _selectiveEnabled = new Dictionary<Uri, bool>();
\r
30 public void SetIsSelectiveEnabled(AccountInfo account,bool value)
\r
32 _selectiveEnabled[account.AccountKey]=value;
\r
36 public void SetIsSelectiveEnabled(Uri accountKey,bool value)
\r
38 _selectiveEnabled[accountKey]=value;
\r
42 public bool IsSelectiveEnabled(AccountInfo account)
\r
45 _selectiveEnabled.TryGetValue(account.AccountKey, out isEnabled);
\r
50 public bool IsSelectiveEnabled(Uri accountKey)
\r
53 _selectiveEnabled.TryGetValue(accountKey, out isEnabled);
\r
58 public void SetSelectedUris(AccountInfo account,List<Uri> uris)
\r
60 SelectiveUris[account.AccountKey] = uris;
\r
61 SelectivePaths[account.AccountKey] = UrisToFilePaths(account,uris);
\r
64 public bool IsSelected(ObjectInfo info)
\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
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
78 public bool IsSelected(AccountInfo account,FileSystemInfo info)
\r
80 if (info is DirectoryInfo)
\r
82 return IsSelected(account,info.FullName);
\r
85 public bool IsSelected(AccountInfo account, string fullPath)
\r
87 if (Directory.Exists(fullPath))
\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
97 return !SelectivePaths.TryGetValue(account.AccountKey, out paths)
\r
99 || paths.Any(fullPath.IsAtOrDirectlyBelow);
\r
103 /// Return a list of absolute filepaths from a list of Uris
\r
105 /// <param name="uris"></param>
\r
106 /// <returns></returns>
\r
107 private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)
\r
110 return new List<string>();
\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