Added UI for SelectiveSynch
[pithos-ms-client] / trunk / Pithos.Client.WPF / SelectiveSynch / SelectiveSynchViewModel.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Collections.Specialized;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using Caliburn.Micro;
9 using Pithos.Client.WPF.Properties;
10 using Pithos.Interfaces;
11 using Pithos.Network;
12
13 namespace Pithos.Client.WPF.SelectiveSynch
14 {
15     class SelectiveSynchViewModel:Screen
16     {
17         private IEventAggregator _events ;
18
19         private string _rootPath;
20         private string _fragmentsPath;
21         public string RootPath
22         {
23             get { return _rootPath; }
24             set
25             {
26                 _rootPath = value;
27                 _fragmentsPath = Path.Combine(_rootPath, FolderConstants.FragmentsFolder);
28                 _pithosDirectory = new ObservableCollection<DirectoryRecord>{
29                         new DirectoryRecord(_fragmentsPath) {Info = new DirectoryInfo(value)}};
30                 NotifyOfPropertyChange(() => RootPath);
31                 NotifyOfPropertyChange(()=>PithosDirectory);
32             }
33         }
34
35         private string _title;
36         public string Title
37         {
38             get { return _title; }
39             set
40             {
41                 _title = value;
42                 NotifyOfPropertyChange(() => Title);
43             }
44         }
45
46         public AccountSettings Account { get; set; }
47
48         private ObservableCollection<DirectoryRecord> _pithosDirectory;
49         public ObservableCollection<DirectoryRecord> PithosDirectory
50         {
51             get { return _pithosDirectory; }
52         }
53
54         private ObservableCollection<DirectoryInfo> _checks;
55         public ObservableCollection<DirectoryInfo> Checks
56         {
57             get { return _checks; }
58         }
59
60         public void GetChecks()
61         {
62             var root = PithosDirectory[0];            
63             _checks = new ObservableCollection<DirectoryInfo>(
64                 from record in root
65                 where record.IsChecked==true
66                 select record.Info);
67             NotifyOfPropertyChange(() => Checks);
68         }
69
70         public SelectiveSynchViewModel(IEventAggregator events, AccountSettings account)
71         {
72             Account = account;
73             AccountName = account.AccountName;
74             Title = account.AccountName;
75             RootPath = account.RootPath;
76
77             SetInitialSelections(account);
78         }
79
80         private void SetInitialSelections(AccountSettings account)
81         {
82             var selections = account.SelectiveFolders;
83             if (selections.Count == 0)
84                 return;
85             var root = PithosDirectory[0];
86             var selects= from record in root
87                              where selections.Contains(record.Info.FullName)
88                              select record;
89             selects.Apply(record=>record.IsChecked=true);            
90         }
91
92         protected string AccountName { get; set; }
93
94         public void SaveChanges()
95         {
96             var selections = GetSelectedFolderNames();
97
98             SaveSettings(selections);
99             var root = PithosDirectory[0];
100             
101             var added= (from record in root
102                          where record.Added
103                          select record.Info.FullName.ToLower()).ToArray();
104             var removed= (from record in root
105                          where record.Removed
106                          select record.Info.FullName.ToLower()).ToArray();            
107
108             _events.Publish(new SelectiveSynchChanges{Account=Account,Added=added,Removed=removed});
109             
110
111             
112
113             TryClose(true);
114         }
115
116         
117         private void SaveSettings(string[] selections)
118         {
119             Account.SelectiveFolders.Clear();
120             Account.SelectiveFolders.AddRange(selections);
121             Settings.Default.Save();            
122         }
123
124         private string[] GetSelectedFolderNames()
125         {
126             var root = PithosDirectory[0];
127             var selections = from record in root
128                          where record.IsChecked == true
129                          select record.Info.FullName;
130             return selections.ToArray();
131         }
132
133         public void RejectChanges()
134         {
135             TryClose(false);
136         }
137     }
138 }