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