root / trunk / Pithos.Client.WPF / SelectiveSynch / SelectiveSynchViewModel.cs @ 2115e2a5
History | View | Annotate | Download (13.2 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="SelectiveSynchViewModel.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System; |
43 |
using System.Collections.Generic; |
44 |
using System.Collections.ObjectModel; |
45 |
using System.Linq; |
46 |
using System.Threading.Tasks; |
47 |
using Caliburn.Micro; |
48 |
using Pithos.Client.WPF.Properties; |
49 |
using Pithos.Client.WPF.Utils; |
50 |
using Pithos.Core; |
51 |
using Pithos.Interfaces; |
52 |
using Pithos.Network; |
53 |
|
54 |
namespace Pithos.Client.WPF.SelectiveSynch |
55 |
{ |
56 |
class SelectiveSynchViewModel:Screen |
57 |
{ |
58 |
private readonly IEventAggregator _events ; |
59 |
|
60 |
|
61 |
public AccountSettings Account { get; set; } |
62 |
|
63 |
private readonly ObservableCollection<DirectoryRecord> _rootNodes=new ObservableCollection<DirectoryRecord>(); |
64 |
public ObservableCollection<DirectoryRecord> RootNodes |
65 |
{ |
66 |
get { return _rootNodes; } |
67 |
} |
68 |
|
69 |
private ObservableCollection<ObjectInfo> _checks; |
70 |
//private readonly PithosMonitor _monitor; |
71 |
private bool _isBusy=true; |
72 |
private readonly string _apiKey; |
73 |
|
74 |
public ObservableCollection<ObjectInfo> Checks |
75 |
{ |
76 |
get { return _checks; } |
77 |
} |
78 |
|
79 |
public void GetChecks() |
80 |
{ |
81 |
var root = RootNodes[0]; |
82 |
_checks = new ObservableCollection<ObjectInfo>( |
83 |
from DirectoryRecord record in root |
84 |
where record.IsChecked==true |
85 |
select record.ObjectInfo); |
86 |
NotifyOfPropertyChange(() => Checks); |
87 |
} |
88 |
|
89 |
public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey,bool forSelectiveActivation) |
90 |
{ |
91 |
Account = account; |
92 |
AccountName = account.AccountName; |
93 |
DisplayName = String.Format("Selective folder synchronization for {0}",account.AccountName); |
94 |
//_monitor = monitor; |
95 |
_events = events; |
96 |
_apiKey = apiKey; |
97 |
//IsEnabled = account.SelectiveSyncEnabled; |
98 |
TaskEx.Run(()=>LoadRootNode(forSelectiveActivation)); |
99 |
} |
100 |
|
101 |
private async Task LoadRootNode(bool forSelectiveActivation) |
102 |
{ |
103 |
//TODO: Check this |
104 |
var client = new CloudFilesClient(AccountName,_apiKey){AuthenticationUrl=Account.ServerUrl,UsePithos=true}; |
105 |
await client.Authenticate().ConfigureAwait(false); |
106 |
|
107 |
//NEED to get the local folders here as well, |
108 |
// and combine them with the cloud folders |
109 |
|
110 |
|
111 |
var containerInfos = await client.ListContainers(AccountName).ConfigureAwait(false); |
112 |
var dirs = from container in containerInfos |
113 |
where container.Name.ToUnescapedString() != "trash" |
114 |
select new DirectoryRecord |
115 |
{ |
116 |
DisplayName = container.Name.ToUnescapedString(), |
117 |
Uri=client.StorageUrl.Combine(String.Format(@"{0}/{1}",Account.AccountName, container.Name)), |
118 |
Directories = (from dir in client.ListObjects(AccountName, container.Name) |
119 |
select dir).ToTree() |
120 |
}; |
121 |
var ownFolders = dirs.ToList(); |
122 |
|
123 |
var accountNodes=from account in client.ListSharingAccounts().AsParallel() |
124 |
let containerTask = client.ListContainers(account.name) |
125 |
select new DirectoryRecord |
126 |
{ |
127 |
DisplayName=account.name, |
128 |
Uri=client.StorageUrl.Combine("../"+ account.name), |
129 |
Directories=(from container in containerTask.Result.AsParallel() |
130 |
let objects=client.ListObjects(account.name,container.Name) |
131 |
select new DirectoryRecord |
132 |
{ |
133 |
DisplayName=container.Name.ToUnescapedString(), |
134 |
Uri = client.StorageUrl.Combine("../" + account.name + "/" + container.Name), |
135 |
Directories=objects.ToTree() |
136 |
}).ToList() |
137 |
}; |
138 |
|
139 |
var othersNode = new DirectoryRecord |
140 |
{ |
141 |
DisplayName = "Shared with me", |
142 |
Directories=accountNodes.ToList() |
143 |
}; |
144 |
|
145 |
|
146 |
var rootItem = new DirectoryRecord |
147 |
{ |
148 |
DisplayName = AccountName , |
149 |
Directories = ownFolders |
150 |
}; |
151 |
|
152 |
// Add Local Folders |
153 |
//var localFolders = SelectiveExtensions.LocalFolders(AccountName, Account.RootPath,client.StorageUrl.AbsoluteUri); |
154 |
//AppendToTree(localFolders, rootItem); |
155 |
|
156 |
|
157 |
//For each local folder that doesn't exist in the server nodes |
158 |
//find the best matching parent and add the folder below it. |
159 |
|
160 |
Execute.OnUIThread(() => |
161 |
{ |
162 |
RootNodes.Add(rootItem); |
163 |
RootNodes.Add(othersNode); |
164 |
}); |
165 |
|
166 |
SetInitialSelections(Account,forSelectiveActivation); |
167 |
|
168 |
IsBusy = false; |
169 |
} |
170 |
|
171 |
private static void AppendToTree(IEnumerable<DirectoryRecord> localFolders, DirectoryRecord rootItem) |
172 |
{ |
173 |
foreach (var folder in localFolders) |
174 |
{ |
175 |
var items = from root in rootItem |
176 |
from child in root |
177 |
select child; |
178 |
//If this folder is not included in the server folders |
179 |
if (items.Any(dir => dir.Uri == folder.Uri)) |
180 |
continue; |
181 |
|
182 |
//we need to add it |
183 |
//Find the best parent |
184 |
|
185 |
//One way to do this, is to break the the Uri to its parts |
186 |
//and try to find a parent using progressively fewer parts |
187 |
var parts = folder.Uri.AbsoluteUri.Split('/'); |
188 |
for (var i = parts.Length - 1; i > 2; i--) |
189 |
{ |
190 |
var parentUrl = String.Join("/", parts.Splice(0, i)); |
191 |
var parentUri = new Uri(parentUrl); |
192 |
|
193 |
var parent = items.FirstOrDefault(dir => dir.Uri == parentUri); |
194 |
if (parent != null) |
195 |
{ |
196 |
parent.Directories.Add(folder); |
197 |
break; |
198 |
} |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
public bool IsBusy |
204 |
{ |
205 |
get { |
206 |
return _isBusy; |
207 |
} |
208 |
set { |
209 |
_isBusy = value; |
210 |
NotifyOfPropertyChange(()=>IsBusy); |
211 |
} |
212 |
} |
213 |
|
214 |
private void SetInitialSelections(AccountSettings account,bool forSelectiveActivation) |
215 |
{ |
216 |
var selections = account.SelectiveFolders; |
217 |
|
218 |
|
219 |
|
220 |
//Initially, all nodes are checked |
221 |
//We need to *uncheck* the nodes that are not selected |
222 |
|
223 |
var allNodes = (from DirectoryRecord rootRecord in RootNodes |
224 |
from DirectoryRecord record in rootRecord |
225 |
select record).ToList(); |
226 |
//WARNING: Using IsChecked marks the item as REMOVED |
227 |
allNodes.Apply(record => record.IsExplicitlyChecked = false); |
228 |
|
229 |
if (selections.Count == 0) |
230 |
{ |
231 |
// allNodes.Apply(record => record.IsChecked = false); |
232 |
return; |
233 |
} |
234 |
|
235 |
var selects = (from DirectoryRecord rootRecord in RootNodes |
236 |
from DirectoryRecord record in rootRecord |
237 |
where record.Uri !=null && selections.Contains(record.Uri.ToString()) |
238 |
select record).ToList(); |
239 |
//var shouldBeChecked = allNodes.Except(selects).ToList(); |
240 |
|
241 |
//WARNING: Using IsChecked marks the item as ADDED |
242 |
selects.Apply(record => record.IsExplicitlyChecked = !forSelectiveActivation); |
243 |
|
244 |
//If any of the root nodes has at least one selected child, set it to gray |
245 |
SetRootNodeSelections(forSelectiveActivation); |
246 |
|
247 |
//shouldBeChecked.Apply(record => record.IsChecked = true); |
248 |
|
249 |
|
250 |
|
251 |
} |
252 |
|
253 |
private void SetRootNodeSelections(bool forSelectiveActivation) |
254 |
{ |
255 |
var selectedRoots = from DirectoryRecord rootRecord in RootNodes |
256 |
from DirectoryRecord record in rootRecord |
257 |
where record.IsChecked == true |
258 |
select rootRecord; |
259 |
selectedRoots.Apply(record => record.IsExplicitlyChecked = !forSelectiveActivation); |
260 |
} |
261 |
|
262 |
protected string AccountName { get; set; } |
263 |
|
264 |
public void SaveChanges() |
265 |
{ |
266 |
var uris = (from DirectoryRecord root in RootNodes |
267 |
from DirectoryRecord record in root |
268 |
where record.IsChecked == true && record.Uri != null |
269 |
select record.Uri).ToArray(); |
270 |
|
271 |
SaveSettings(uris); |
272 |
|
273 |
//RootNodes is an ObservableCollection, it can't be enumerated iterativelly |
274 |
|
275 |
var added = (from DirectoryRecord root in RootNodes |
276 |
from DirectoryRecord record in root |
277 |
where record.Added && record.Uri != null |
278 |
select record.Uri).ToArray(); |
279 |
var removed = (from DirectoryRecord root in RootNodes |
280 |
from DirectoryRecord record in root |
281 |
where record.Removed && record.Uri != null |
282 |
select record.Uri).ToArray(); |
283 |
//TODO: Include Uris for the containers as well |
284 |
_events.Publish(new SelectiveSynchChanges{Enabled=true, Account=Account,Uris=uris,Added=added,Removed=removed}); |
285 |
|
286 |
|
287 |
|
288 |
|
289 |
TryClose(true); |
290 |
} |
291 |
|
292 |
/* |
293 |
private bool _isEnabled; |
294 |
public bool IsEnabled |
295 |
{ |
296 |
get { return _isEnabled; } |
297 |
set |
298 |
{ |
299 |
_isEnabled = value; |
300 |
NotifyOfPropertyChange(()=>IsEnabled); |
301 |
} |
302 |
} |
303 |
*/ |
304 |
|
305 |
private void SaveSettings(IEnumerable<Uri> uris) |
306 |
{ |
307 |
var selections = uris.Select(uri => uri.ToString()).ToArray(); |
308 |
//Account.SelectiveSyncEnabled = IsEnabled; |
309 |
lock (Account.SelectiveFolders) |
310 |
{ |
311 |
Account.SelectiveFolders.Clear(); |
312 |
Account.SelectiveFolders.AddRange(selections); |
313 |
} |
314 |
Settings.Default.Save(); |
315 |
} |
316 |
|
317 |
public void RejectChanges() |
318 |
{ |
319 |
TryClose(false); |
320 |
} |
321 |
} |
322 |
} |