Modified loggers to use their enclosing class
[pithos-ms-client] / trunk / Pithos.Core / PithosMonitor.cs
index 4e8e393..567be26 100644 (file)
  */
 #endregion
 using System;
-using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
-using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.IO;
 using System.Linq;
-using System.Net;
-using System.Net.NetworkInformation;
-using System.Security.Cryptography;
-using System.ServiceModel.Description;
-using System.Text;
+using System.Reflection;
 using System.Threading;
 using System.Threading.Tasks;
-using Castle.ActiveRecord.Queries;
-using Microsoft.WindowsAPICodePack.Net;
 using Pithos.Core.Agents;
 using Pithos.Interfaces;
-using System.ServiceModel;
 using Pithos.Network;
 using log4net;
 
@@ -67,6 +58,8 @@ namespace Pithos.Core
     [Export(typeof(PithosMonitor))]
     public class PithosMonitor:IDisposable
     {
+        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
         private int _blockSize;
         private string _blockHash;
 
@@ -139,11 +132,10 @@ namespace Pithos.Core
         }
 
         private AccountInfo _accountInfo;
-        
-       
 
 
-        private static readonly ILog Log = LogManager.GetLogger(typeof(PithosMonitor));
+
+
 
 
         public bool Pause
@@ -211,11 +203,11 @@ namespace Pithos.Core
                     return;
             }
             _cancellationSource = new CancellationTokenSource();
-            
 
-            CloudClient=new CloudFilesClient(UserName,ApiKey);
-            CloudClient.UsePithos = true;
-            CloudClient.AuthenticationUrl = this.AuthenticationUrl;            
+
+            CloudClient = new CloudFilesClient(UserName, ApiKey)
+                              {UsePithos = true, AuthenticationUrl = AuthenticationUrl};
+
 
             _accountInfo = CloudClient.Authenticate();            
             _accountInfo.SiteUri = AuthenticationUrl;
@@ -259,11 +251,11 @@ namespace Pithos.Core
             var pithosContainers = new List<string>{ FolderConstants.TrashContainer,FolderConstants.PithosContainer};
             foreach (var container in pithosContainers)
             {                
-                var info=CloudClient.GetContainerInfo(this.UserName, container);
+                var info=CloudClient.GetContainerInfo(UserName, container);
                 if (info == ContainerInfo.Empty)
                 {
-                    CloudClient.CreateContainer(this.UserName, container);
-                    info = CloudClient.GetContainerInfo(this.UserName, container);
+                    CloudClient.CreateContainer(UserName, container);
+                    info = CloudClient.GetContainerInfo(UserName, container);
                 }
                 _blockSize = info.BlockSize;
                 _blockHash = info.BlockHash;
@@ -276,8 +268,8 @@ namespace Pithos.Core
 
         private void IndexLocalFiles()
         {
-            StatusNotification.NotifyChange("Indexing Local Files",TraceLevel.Info);
-            using (log4net.ThreadContext.Stacks["Monitor"].Push("Indexing local files"))
+            StatusNotification.NotifyChange("Indexing Local Files");
+            using (ThreadContext.Stacks["Monitor"].Push("Indexing local files"))
             {
                 Log.Info("START");
                 try
@@ -359,6 +351,8 @@ namespace Pithos.Core
 
             public override int GetHashCode(CloudAction obj)
             {
+                if (obj == null)
+                    return 0;
                 var hash1 = (obj.LocalFile == null) ? int.MaxValue : obj.LocalFile.FullName.GetHashCode();
                 var hash2 = (obj.CloudFile == null) ? int.MaxValue : (obj.CloudFile.Hash ?? obj.CloudFile.Name??"").GetHashCode();
                 var hash3 = obj.Action.GetHashCode();
@@ -366,13 +360,9 @@ namespace Pithos.Core
             }
         }        
 
-        private Timer timer;
-
         private void StartNetworkAgent()
         {
 
-            NetworkAgent.AddAccount(_accountInfo);
-
             NetworkAgent.StatusNotification = StatusNotification;
                         
             NetworkAgent.Start();
@@ -385,7 +375,7 @@ namespace Pithos.Core
         }
 
         //Make sure a hidden cache folder exists to store partial downloads
-        private static string CreateHiddenFolder(string rootPath, string folderName)
+        private static void CreateHiddenFolder(string rootPath, string folderName)
         {
             if (String.IsNullOrWhiteSpace(rootPath))
                 throw new ArgumentNullException("rootPath");
@@ -412,7 +402,6 @@ namespace Pithos.Core
                     Log.InfoFormat("Reset cache folder to hidden: {0}", folder);
                 }                                
             }
-            return folder;
         }
 
        
@@ -435,9 +424,6 @@ namespace Pithos.Core
             if (FileAgent!=null)
                 FileAgent.Stop();
             FileAgent = null;
-            if (timer != null)
-                timer.Dispose();
-            timer = null;            
         }
 
 
@@ -483,12 +469,26 @@ namespace Pithos.Core
             
             FileAgent.SelectivePaths=selectivePaths;
             PollAgent.SetSyncUris(uris);
-
+            
             var removedPaths = UrisToFilePaths(removed);
-            RemoveSelectivePaths(removedPaths);
+            UnversionSelectivePaths(removedPaths);
+
+        }
+
+        /// <summary>
+        /// Mark all unselected paths as Unversioned
+        /// </summary>
+        /// <param name="removed"></param>
+        private void UnversionSelectivePaths(List<string> removed)
+        {
+            if (removed == null)
+                return;
 
+            //Ensure we remove any file state below the deleted folders
+            FileState.UnversionPaths(removed);
         }
 
+
         /// <summary>
         /// Return a list of absolute filepaths from a list of Uris
         /// </summary>
@@ -503,26 +503,10 @@ namespace Pithos.Core
                     let relativePath = _accountInfo.StorageUri
                         .MakeRelativeUri(uri)
                         .RelativeUriToFilePath()
-                    select Path.Combine(RootPath, relativePath)).ToList();
+                        //Trim the account name
+                    select Path.Combine(RootPath, relativePath.After(_accountInfo.UserName + '\\'))).ToList();            
         }
 
-        /// <summary>
-        /// Delete the folders that were removed from synchronization
-        /// </summary>
-        /// <param name="removed"></param>
-        private void RemoveSelectivePaths(List<string> removed)
-        {
-            if (removed == null)
-                return;
-
-            foreach (var removedPath in removed.Where(Directory.Exists))
-            {
-                Directory.Delete(removedPath,true);
-            }
-
-            //Ensure we remove any file state below the deleted folders
-            FileState.RemovePaths(removed);
-        }
 
         public ObjectInfo GetObjectInfo(string filePath)
         {
@@ -547,7 +531,7 @@ namespace Pithos.Core
                 //Create the root URL for the target account
                 var oldName = UserName;
                 var absoluteUri =  _accountInfo.StorageUri.AbsoluteUri;
-                var nameIndex=absoluteUri.IndexOf(oldName);
+                var nameIndex=absoluteUri.IndexOf(oldName, StringComparison.Ordinal);
                 var root=absoluteUri.Substring(0, nameIndex);
 
                 accountInfo = new AccountInfo
@@ -562,7 +546,7 @@ namespace Pithos.Core
             }
             else
             {
-                accountName = this.UserName;
+                accountName = UserName;
                 container = parts[0];
                 relativeUrl = String.Join("/", parts.Splice(1));
             }
@@ -593,7 +577,7 @@ namespace Pithos.Core
                 //Create the root URL for the target account
                 var oldName = UserName;
                 var absoluteUri =  _accountInfo.StorageUri.AbsoluteUri;
-                var nameIndex=absoluteUri.IndexOf(oldName);
+                var nameIndex=absoluteUri.IndexOf(oldName, StringComparison.Ordinal);
                 var root=absoluteUri.Substring(0, nameIndex);
 
                 accountInfo = new AccountInfo