using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Linq; using System.Text; using Pithos.Interfaces; using Pithos.Network; namespace Pithos.Core.Agents { [Export(typeof(Selectives))] public class Selectives { public Dictionary> SelectiveUris { get; private set; } private Dictionary> SelectivePaths { get; set; } public Selectives() { SelectiveUris = new Dictionary>(); SelectivePaths = new Dictionary>(); } public void SetSelectedUris(AccountInfo account,List uris) { SelectiveUris[account.AccountKey] = uris; SelectivePaths[account.AccountKey] = UrisToFilePaths(account,uris); } public bool IsSelected(ObjectInfo info) { List filterUris; return !SelectiveUris.TryGetValue(info.AccountKey, out filterUris) || filterUris.Count ==0 || filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f)); } public bool IsSelected(AccountInfo account,FileSystemInfo info) { return IsSelected(account,info.FullName); } public bool IsSelected(AccountInfo account, string fullPath) { List paths; return !SelectivePaths.TryGetValue(account.AccountKey, out paths) || paths.Count == 0 || paths.Any(fullPath.IsAtOrDirectlyBelow); } /// /// Return a list of absolute filepaths from a list of Uris /// /// /// private List UrisToFilePaths(AccountInfo account,IEnumerable uris) { if (uris == null) return new List(); var accountPath = account.AccountPath; var storageUrl = account.StorageUri.ToString(); var own = (from uri in uris where uri.ToString().StartsWith(storageUrl) let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath() //Trim the account name select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList(); var others = (from uri in uris where !uri.ToString().StartsWith(storageUrl) let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath() //Trim the account name select Path.Combine(accountPath, "others-shared", relativePath)).ToList(); return own.Union(others).ToList(); } } }