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(AccountInfo account,ObjectInfo info)
\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
70 var selectiveEnabled = IsSelectiveEnabled(account.AccountKey);
\r
71 if (!selectiveEnabled)
\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
80 public bool IsSelected(AccountInfo account,FileSystemInfo info)
\r
82 if (info is DirectoryInfo)
\r
84 return IsSelected(account,info.FullName);
\r
87 public bool IsSelected(AccountInfo account, string fullPath)
\r
89 if (Directory.Exists(fullPath))
\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
99 return !SelectivePaths.TryGetValue(account.AccountKey, out paths)
\r
100 || paths.Count == 0
\r
101 || paths.Any(fullPath.IsAtOrDirectlyBelow);
\r
105 /// Return a list of absolute filepaths from a list of Uris
\r
107 /// <param name="uris"></param>
\r
108 /// <returns></returns>
\r
109 private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)
\r
112 return new List<string>();
\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