root / trunk / Pithos.Core / Agents / SelectiveUris.cs @ 1cc1e8c5
History | View | Annotate | Download (5.9 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 void AddUri(AccountInfo account,Uri uri) |
69 |
{ |
70 |
var accountPath = account.AccountPath; |
71 |
var storageUrl = account.StorageUri.ToString(); |
72 |
var isShared=!uri.ToString().StartsWith(storageUrl); |
73 |
var relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath(); |
74 |
|
75 |
var fullPath=isShared |
76 |
?Path.Combine(accountPath, FolderConstants.OthersFolder, relativePath) |
77 |
:Path.Combine(accountPath, relativePath.After(account.UserName + '\\')); |
78 |
|
79 |
SelectiveUris[account.AccountKey].Add(uri); |
80 |
SelectivePaths[account.AccountKey].Add(fullPath); |
81 |
|
82 |
} |
83 |
|
84 |
public bool IsSelected(AccountInfo account,ObjectInfo info) |
85 |
{ |
86 |
//Shared folders should NOT be synced if selective syncing is disabled |
87 |
var isShared = info.IsShared??false; |
88 |
if (info.StorageUri == null) |
89 |
return true; |
90 |
var selectiveEnabled = IsSelectiveEnabled(account.AccountKey); |
91 |
if (!selectiveEnabled) |
92 |
return !isShared; |
93 |
|
94 |
List<Uri> filterUris; |
95 |
return !SelectiveUris.TryGetValue(account.AccountKey, out filterUris) |
96 |
|| filterUris.Count ==0 |
97 |
|| filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f)); |
98 |
} |
99 |
|
100 |
public bool IsSelected(AccountInfo account,FileSystemInfo info) |
101 |
{ |
102 |
/*if (info is DirectoryInfo) |
103 |
return true;*/ |
104 |
return IsSelected(account,info.FullName); |
105 |
} |
106 |
|
107 |
public bool IsSelected(AccountInfo account, string fullPath) |
108 |
{ |
109 |
//Shared folders should NOT be synced if selective syncing is disabled |
110 |
var isShared = fullPath.IsSharedTo(account); |
111 |
var selectiveEnabled = IsSelectiveEnabled(account.AccountKey); |
112 |
if (!selectiveEnabled) |
113 |
return !isShared; |
114 |
|
115 |
List<string> paths; |
116 |
var hasSelectives = SelectivePaths.TryGetValue(account.AccountKey, out paths); |
117 |
var isSelected = !hasSelectives || paths.Count == 0 || paths.Any(fullPath.IsAtOrDirectlyBelow); |
118 |
return isSelected; |
119 |
} |
120 |
|
121 |
/// <summary> |
122 |
/// Return a list of absolute filepaths from a list of Uris |
123 |
/// </summary> |
124 |
/// <param name="uris"></param> |
125 |
/// <returns></returns> |
126 |
private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris) |
127 |
{ |
128 |
if (uris == null) |
129 |
return new List<string>(); |
130 |
|
131 |
var accountPath = account.AccountPath; |
132 |
var storageUrl = account.StorageUri.ToString(); |
133 |
var own = (from uri in uris |
134 |
where uri.ToString().StartsWith(storageUrl) |
135 |
let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath() |
136 |
//Trim the account name |
137 |
select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList(); |
138 |
var others = (from uri in uris |
139 |
where !uri.ToString().StartsWith(storageUrl) |
140 |
let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath() |
141 |
//Trim the account name |
142 |
select Path.Combine(accountPath, FolderConstants.OthersFolder, relativePath)).ToList(); |
143 |
return own.Union(others).ToList(); |
144 |
} |
145 |
|
146 |
public void Save(AccountInfo account) |
147 |
{ |
148 |
Contract.Requires(account!=null); |
149 |
Contract.EndContractBlock(); |
150 |
|
151 |
var selections = SelectiveUris[account.AccountKey]; |
152 |
var accountSettings= Settings.Accounts.First(acc => acc.AccountKey == account.AccountKey); |
153 |
accountSettings.SelectiveFolders.Clear(); |
154 |
accountSettings.SelectiveFolders.AddRange(selections.Select(uri => uri.ToString()).ToArray()); |
155 |
Settings.Save(); |
156 |
|
157 |
} |
158 |
|
159 |
} |
160 |
} |