Merge branch 'master' of https://code.grnet.gr/git/pithos-ms-client
[pithos-ms-client] / trunk / Pithos.Client.WPF / SelectiveSynch / SelectiveSynchViewModel.cs
index a801923..8520ca1 100644 (file)
 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.Core.Agents;
 using Pithos.Interfaces;
 using Pithos.Network;
 
 namespace Pithos.Client.WPF.SelectiveSynch
 {
     class SelectiveSynchViewModel:Screen
-    {
-        private const string DirectoryType = "application/directory";
-        private IEventAggregator _events ;
+    {        
+        private readonly IEventAggregator _events ;
 
-        private string _title;
-        public string Title
-        {
-            get { return _title; }
-            set
-            {
-                _title = value;
-                NotifyOfPropertyChange(() => Title);
-            }
-        }
 
         public AccountSettings Account { get; set; }
 
@@ -81,8 +67,9 @@ namespace Pithos.Client.WPF.SelectiveSynch
         }
 
         private ObservableCollection<ObjectInfo> _checks;
-        private PithosMonitor _monitor;
+        //private readonly PithosMonitor _monitor;
         private bool _isBusy=true;
+        private string _apiKey;
 
         public ObservableCollection<ObjectInfo> Checks
         {
@@ -93,56 +80,60 @@ namespace Pithos.Client.WPF.SelectiveSynch
         {
             var root = RootNodes[0];            
             _checks = new ObservableCollection<ObjectInfo>(
-                from record in root
+                from DirectoryRecord record in root
                 where record.IsChecked==true
                 select record.ObjectInfo);
             NotifyOfPropertyChange(() => Checks);
         }
 
-        public SelectiveSynchViewModel(PithosMonitor monitor, IEventAggregator events, AccountSettings account)
+        public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey)
         {
             Account = account;
             AccountName = account.AccountName;
-            Title = account.AccountName;
-            _monitor = monitor;
+            DisplayName = String.Format("Selective folder synchronization for {0}",account.AccountName);
+            //_monitor = monitor;
             _events = events;
+            _apiKey = apiKey;
             TaskEx.Run(LoadRootNode);
         }
 
         private void LoadRootNode()
-        {
-            var client = _monitor.CloudClient;
+        {            
+            //TODO: Check this
+            var client = new CloudFilesClient(AccountName,_apiKey){AuthenticationUrl=Account.ServerUrl,UsePithos=true};
+            client.Authenticate();
+            
 
-            var dirs = from container in client.ListContainers(_monitor.UserName)
+            var dirs = from container in client.ListContainers(AccountName)                       
                        select new DirectoryRecord
                                   {
                                       DisplayName = container.Name,
-                                      Uri=new Uri(client.StorageUrl,container.Name),
-                                      Directories = (from dir in client.ListObjects(_monitor.UserName, container.Name, "")
-                                                     where dir.Content_Type == DirectoryType
-                                                     select new DirectoryRecord { DisplayName = dir.Name, ObjectInfo = dir }).ToList()
+                                      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 accounts = client.ListSharingAccounts();
-
             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,
-                                                            Directories=(from folder in client.ListObjects(account.name,container.Name,"")
-                                                                        where folder.Content_Type==DirectoryType
-                                                                        select new DirectoryRecord{DisplayName=folder.Name,ObjectInfo=folder}).ToList()
+                                                            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",
+                                     DisplayName = "Shared to me",
                                      Directories=accountNodes.ToList()
                                  };
 
@@ -155,8 +146,8 @@ namespace Pithos.Client.WPF.SelectiveSynch
 
             Execute.OnUIThread(() =>
                                    {
-                                       this.RootNodes.Add(rootItem);
-                                       this.RootNodes.Add(othersNode);
+                                       RootNodes.Add(rootItem);
+                                       RootNodes.Add(othersNode);
                                    });
 
             SetInitialSelections(Account);
@@ -179,38 +170,56 @@ namespace Pithos.Client.WPF.SelectiveSynch
         {
             var selections = account.SelectiveFolders;
 
-            if (selections.Count == 0)
-                return;
+
+                
             //Initially, all nodes are checked
             //We need to *uncheck* the nodes that are not selected
 
-            var selects = from rootRecord in RootNodes
-                          from record in rootRecord
-                          where record.Uri !=null &&  !selections.Contains(record.Uri.ToString())
-                          select record;
+            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 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);
+            
+            
 
-            selects.Apply(record=>record.IsChecked=false);
         }
 
         protected string AccountName { get; set; }
 
         public void SaveChanges()
         {
-            var uris = (from root in RootNodes
-                        from record in root
+            var uris = (from DirectoryRecord root in RootNodes
+                        from DirectoryRecord record in root
                         where record.IsChecked == true && record.Uri != null
                         select record.Uri).ToArray();            
 
             SaveSettings(uris);
             
             //RootNodes is an ObservableCollection, it can't be enumerated iterativelly
-            
-            var added= (from root in RootNodes
-                        from record in root
+
+            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 root in RootNodes
-                            from record in root
+            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