Shared File Fix for Upload
[pithos-ms-client] / trunk / Pithos.Core / PithosMonitor.cs
index 76a15ea..b5c250e 100644 (file)
@@ -80,6 +80,8 @@ namespace Pithos.Core
         }
 
 
+
+
         private IPithosWorkflow _workflow;
 
         [Import]
@@ -116,7 +118,20 @@ namespace Pithos.Core
         [Import]
         public NetworkAgent NetworkAgent { get; set; }
         [Import]
-        public PollAgent PollAgent { get; set; }       
+        public PollAgent PollAgent { get; set; }
+
+        private Selectives _selectives;
+
+        [Import]
+        public Selectives Selectives
+        {
+            get { return _selectives; }
+            set
+            {
+                _selectives = value;
+                FileAgent.Selectives = value;
+            }
+        }
 
         public string UserName { get; set; }
         private string _apiKey;
@@ -164,8 +179,7 @@ namespace Pithos.Core
 
         public PithosMonitor()
         {
-            FileAgent = new FileAgent((int)Settings.FileIdleTimeout.TotalMilliseconds);
-
+            FileAgent = new FileAgent();
         }
         private bool _started;
 
@@ -194,12 +208,12 @@ namespace Pithos.Core
             }
             _cancellationSource = new CancellationTokenSource();
 
-
-            CloudClient = new CloudFilesClient(UserName, ApiKey)
-                              {UsePithos = true, AuthenticationUrl = AuthenticationUrl};
-
-
-            _accountInfo = CloudClient.Authenticate();            
+            lock (this)
+            {
+                CloudClient = new CloudFilesClient(UserName, ApiKey)
+                                  {UsePithos = true, AuthenticationUrl = AuthenticationUrl};
+                _accountInfo = CloudClient.Authenticate();
+            }
             _accountInfo.SiteUri = AuthenticationUrl;
             _accountInfo.AccountPath = RootPath;
 
@@ -221,7 +235,7 @@ namespace Pithos.Core
             StatusKeeper.StartProcessing(_cancellationSource.Token);
             IndexLocalFiles();
             //Extract the URIs from the string collection
-            var settings = Settings.Accounts.First(s => s.AccountName == _accountInfo.UserName);
+            var settings = Settings.Accounts.First(s => s.AccountKey == _accountInfo.AccountKey );
             var selectiveUrls=settings.SelectiveFolders.Cast<string>().Select(url => new Uri(url)).ToArray();
 
             SetSelectivePaths(selectiveUrls,null,null);
@@ -318,42 +332,6 @@ namespace Pithos.Core
             }
         }*/
 
-        internal class LocalFileComparer:EqualityComparer<CloudAction>
-        {
-            public override bool Equals(CloudAction x, CloudAction y)
-            {
-                if (x.Action != y.Action)
-                    return false;
-                if (x.LocalFile != null && y.LocalFile != null && !x.LocalFile.FullName.Equals(y.LocalFile.FullName))
-                    return false;
-                if (x.CloudFile != null && y.CloudFile != null )
-                {
-                    if (x.CloudFile.Hash == null & y.CloudFile.Hash != null)
-                        return false;
-                    if (x.CloudFile.Hash != null & y.CloudFile.Hash == null)
-                        return false;
-                    if (x.CloudFile.Hash == null & y.CloudFile.Hash == null)
-                        return (x.CloudFile.Name == y.CloudFile.Name);
-                    if (!x.CloudFile.Hash.Equals(y.CloudFile.Hash))
-                        return false;
-                }
-                if (x.CloudFile == null ^ y.CloudFile == null ||
-                    x.LocalFile == null ^ y.LocalFile == null)
-                    return false;
-                return true;
-            }
-
-            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();
-                return hash1 ^ hash2 & hash3;
-            }
-        }        
-
 
         private void StartNetworkAgent()
         {
@@ -409,7 +387,8 @@ namespace Pithos.Core
                 Log.DebugFormat("Start Folder Monitoring [{0}]",RootPath);
 
             AgentLocator<FileAgent>.Register(FileAgent,RootPath);
-
+            
+            FileAgent.IdleTimeout = Settings.FileIdleTimeout;
             FileAgent.StatusKeeper = StatusKeeper;
             FileAgent.StatusNotification = StatusNotification;
             FileAgent.Workflow = Workflow;
@@ -467,9 +446,9 @@ namespace Pithos.Core
             //Convert the uris to paths
             var selectivePaths = UrisToFilePaths(uris);
             
-            FileAgent.SelectivePaths=selectivePaths;
-            PollAgent.SetSyncUris(uris);
-            
+            var selectiveUri = uris.ToList();
+            this.Selectives.SetSelectedUris(_accountInfo,selectiveUri);
+
             var removedPaths = UrisToFilePaths(removed);
             UnversionSelectivePaths(removedPaths);
 
@@ -499,12 +478,17 @@ namespace Pithos.Core
             if (uris == null)
                 return new List<string>();
 
-            return (from uri in uris
-                    let relativePath = _accountInfo.StorageUri
-                        .MakeRelativeUri(uri)
-                        .RelativeUriToFilePath()
-                        //Trim the account name
-                    select Path.Combine(RootPath, relativePath.After(_accountInfo.UserName + '\\'))).ToList();            
+            var own = (from uri in uris
+                       where uri.ToString().StartsWith(_accountInfo.StorageUri.ToString())
+                                   let relativePath = _accountInfo.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
+                                   //Trim the account name
+                                   select Path.Combine(RootPath, relativePath.After(_accountInfo.UserName + '\\'))).ToList();
+            var others= (from uri in uris
+                         where !uri.ToString().StartsWith(_accountInfo.StorageUri.ToString())
+                                   let relativePath = _accountInfo.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
+                                   //Trim the account name
+                                   select Path.Combine(RootPath,"others-shared", relativePath)).ToList();
+            return own.Union(others).ToList();            
         }