X-Git-Url: https://code.grnet.gr/git/pithos-ms-client/blobdiff_plain/255f5f86173e94afd51d8182dac87ae6f8554c29..8f44fd3a5bee9a85d5a33cc288ce2d27228faaab:/trunk/Pithos.Client.WPF/SelectiveSynch/SelectiveSynchViewModel.cs diff --git a/trunk/Pithos.Client.WPF/SelectiveSynch/SelectiveSynchViewModel.cs b/trunk/Pithos.Client.WPF/SelectiveSynch/SelectiveSynchViewModel.cs index 988eff0..f7edd53 100644 --- a/trunk/Pithos.Client.WPF/SelectiveSynch/SelectiveSynchViewModel.cs +++ b/trunk/Pithos.Client.WPF/SelectiveSynch/SelectiveSynchViewModel.cs @@ -42,111 +42,188 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.IO; using System.Linq; -using System.Text; +using System.Threading.Tasks; using Caliburn.Micro; using Pithos.Client.WPF.Properties; +using Pithos.Client.WPF.Utils; +using Pithos.Core; using Pithos.Interfaces; using Pithos.Network; namespace Pithos.Client.WPF.SelectiveSynch { class SelectiveSynchViewModel:Screen - { - private IEventAggregator _events ; + { + private readonly IEventAggregator _events ; - private string _rootPath; - private string _cachePath; - public string RootPath - { - get { return _rootPath; } - set - { - _rootPath = value; - _cachePath = Path.Combine(_rootPath, FolderConstants.CacheFolder); - _pithosDirectory = new ObservableCollection{ - new DirectoryRecord(_cachePath) {Info = new DirectoryInfo(value)}}; - NotifyOfPropertyChange(() => RootPath); - NotifyOfPropertyChange(()=>PithosDirectory); - } - } - - private string _title; - public string Title - { - get { return _title; } - set - { - _title = value; - NotifyOfPropertyChange(() => Title); - } - } public AccountSettings Account { get; set; } - private ObservableCollection _pithosDirectory; - public ObservableCollection PithosDirectory + private readonly ObservableCollection _rootNodes=new ObservableCollection(); + public ObservableCollection RootNodes { - get { return _pithosDirectory; } + get { return _rootNodes; } } - private ObservableCollection _checks; - public ObservableCollection Checks + private ObservableCollection _checks; + //private readonly PithosMonitor _monitor; + private bool _isBusy=true; + private string _apiKey; + + public ObservableCollection Checks { get { return _checks; } } public void GetChecks() { - var root = PithosDirectory[0]; - _checks = new ObservableCollection( - from record in root + var root = RootNodes[0]; + _checks = new ObservableCollection( + from DirectoryRecord record in root where record.IsChecked==true - select record.Info); + select record.ObjectInfo); NotifyOfPropertyChange(() => Checks); } - public SelectiveSynchViewModel(IEnumerable folders, IEventAggregator events, AccountSettings account) + public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey) { Account = account; AccountName = account.AccountName; - Title = account.AccountName; - RootPath = account.RootPath; + DisplayName = String.Format("Selective folder synchronization for {0}",account.AccountName); + //_monitor = monitor; + _events = events; + _apiKey = apiKey; + TaskEx.Run(LoadRootNode); + } + + private void LoadRootNode() + { + //TODO: Check this + var client = new CloudFilesClient(AccountName,_apiKey){AuthenticationUrl=Account.ServerUrl,UsePithos=true}; + client.Authenticate(); + - SetInitialSelections(account); + var dirs = from container in client.ListContainers(AccountName) + select new DirectoryRecord + { + DisplayName = container.Name, + Uri=new Uri(client.StorageUrl,String.Format(@"{0}/{1}",Account.AccountName, container.Name)), + Directories = (from dir in client.ListObjects(AccountName, container.Name) + where dir.IsDirectory + select dir).ToTree() + }; + var ownFolders = dirs.ToList(); + + var accountNodes=from account in client.ListSharingAccounts() + select new DirectoryRecord + { + DisplayName=account.name, + Uri=new Uri(client.StorageUrl,"../"+ account.name), + Directories=(from container in client.ListContainers(account.name) + select new DirectoryRecord + { + DisplayName=container.Name, + Uri = new Uri(client.StorageUrl, "../" + account.name + "/" + container.Name), + Directories=(from folder in client.ListObjects(account.name,container.Name) + where folder.IsDirectory + select folder).ToTree() + }).ToList() + }; + + var othersNode = new DirectoryRecord + { + DisplayName = "Others", + Directories=accountNodes.ToList() + }; + + + var rootItem = new DirectoryRecord + { + DisplayName = AccountName , + Directories = ownFolders.ToList() + }; + + Execute.OnUIThread(() => + { + RootNodes.Add(rootItem); + RootNodes.Add(othersNode); + }); + + SetInitialSelections(Account); + + IsBusy = false; + } + + public bool IsBusy + { + get { + return _isBusy; + } + set { + _isBusy = value; + NotifyOfPropertyChange(()=>IsBusy); + } } private void SetInitialSelections(AccountSettings account) { var selections = account.SelectiveFolders; + + + + //Initially, all nodes are checked + //We need to *uncheck* the nodes that are not selected + + var allNodes = (from DirectoryRecord rootRecord in RootNodes + from DirectoryRecord record in rootRecord + select record).ToList(); + + allNodes.Apply(record => record.IsChecked = false); + if (selections.Count == 0) + { + // allNodes.Apply(record => record.IsChecked = false); return; - var root = PithosDirectory[0]; - var selects= from record in root - where selections.Contains(record.Info.FullName) - select record; - selects.Apply(record=>record.IsChecked=true); + } + + var selects = (from DirectoryRecord rootRecord in RootNodes + from DirectoryRecord record in rootRecord + where record.Uri !=null && selections.Contains(record.Uri.ToString()) + select record).ToList(); + //var shouldBeChecked = allNodes.Except(selects).ToList(); + + selects.Apply(record=>record.IsExplicitlyChecked=true); + + //shouldBeChecked.Apply(record => record.IsChecked = true); + + + } protected string AccountName { get; set; } public void SaveChanges() { - var selections = GetSelectedFolderNames(); + var uris = (from DirectoryRecord root in RootNodes + from DirectoryRecord record in root + where record.IsChecked == true && record.Uri != null + select record.Uri).ToArray(); - SaveSettings(selections); - var root = PithosDirectory[0]; + SaveSettings(uris); - var added= (from record in root - where record.Added - select record.Info.FullName.ToLower()).ToArray(); - var removed= (from record in root - where record.Removed - select record.Info.FullName.ToLower()).ToArray(); - - _events.Publish(new SelectiveSynchChanges{Account=Account,Added=added,Removed=removed}); + //RootNodes is an ObservableCollection, it can't be enumerated iterativelly + + var added = (from DirectoryRecord root in RootNodes + from DirectoryRecord record in root + where record.Added && record.Uri != null + select record.Uri).ToArray(); + var removed = (from DirectoryRecord root in RootNodes + from DirectoryRecord record in root + where record.Removed && record.Uri != null + select record.Uri).ToArray(); + //TODO: Include Uris for the containers as well + _events.Publish(new SelectiveSynchChanges{Account=Account,Uris=uris,Added=added,Removed=removed}); @@ -155,21 +232,14 @@ namespace Pithos.Client.WPF.SelectiveSynch } - private void SaveSettings(string[] selections) + private void SaveSettings(IEnumerable uris) { + var selections = uris.Select(uri => uri.ToString()).ToArray(); + Account.SelectiveFolders.Clear(); Account.SelectiveFolders.AddRange(selections); Settings.Default.Save(); - } - - private string[] GetSelectedFolderNames() - { - var root = PithosDirectory[0]; - var selections = from record in root - where record.IsChecked == true - select record.Info.FullName; - return selections.ToArray(); - } + } public void RejectChanges() {