Added File/Container properties windows
[pithos-ms-client] / trunk / Pithos.Core / PithosMonitor.cs
index e245deb..fe8f57a 100644 (file)
@@ -53,7 +53,7 @@ namespace Pithos.Core
         public string UserName { get; set; }
         public string ApiKey { get; set; }
 
-        private Network.AccountInfo _accountInfo;
+        private AccountInfo _accountInfo;
 
 
         private static readonly ILog Log = LogManager.GetLogger(typeof(PithosMonitor));
@@ -82,9 +82,11 @@ namespace Pithos.Core
         public string RootPath
         {
             get { return _rootPath; }
-            set
+            set 
             {
-                _rootPath = value.ToLower();
+                _rootPath = String.IsNullOrWhiteSpace(value) 
+                    ? String.Empty 
+                    : value.ToLower();
             }
         }
 
@@ -92,7 +94,7 @@ namespace Pithos.Core
         CancellationTokenSource _cancellationSource;
 
 
-        private bool _isInitialized;
+        private bool _started;
 
         public void Start()
         {            
@@ -105,12 +107,13 @@ namespace Pithos.Core
             Contract.EndContractBlock();
 
             StatusNotification.NotifyChange("Starting");
-            if (_isInitialized)
+            if (_started)
             {
                 if (!_cancellationSource.IsCancellationRequested)
                     return;
             }
             _cancellationSource = new CancellationTokenSource();
+            _started = true;
 
             CloudClient=new CloudFilesClient(UserName,ApiKey);
             var proxyUri = ProxyFromSettings();            
@@ -122,6 +125,15 @@ namespace Pithos.Core
             _accountInfo.AccountPath = RootPath;
 
 
+            var pithosFolder = Path.Combine(RootPath, FolderConstants.PithosContainer);
+            if (!Directory.Exists(pithosFolder))
+                Directory.CreateDirectory(pithosFolder);
+            //Create the cache folder and ensure it is hidden
+            CreateHiddenFolder(RootPath, FolderConstants.CacheFolder);
+
+            var policy=CloudClient.GetAccountPolicies(_accountInfo);
+
+            StatusNotification.NotifyAccount(policy);
             EnsurePithosContainers();
             
             StatusKeeper.BlockHash = _blockHash;
@@ -134,8 +146,7 @@ namespace Pithos.Core
             StartNetworkAgent();
 
             StartWorkflowAgent();
-            WorkflowAgent.RestartInterruptedFiles(_accountInfo);
-            _isInitialized = true;
+            WorkflowAgent.RestartInterruptedFiles(_accountInfo);            
         }
 
         private void EnsurePithosContainers()
@@ -187,11 +198,11 @@ namespace Pithos.Core
                 Log.Info("START");
                 try
                 {
-                    var fragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
+                    var cachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
                     var directory = new DirectoryInfo(RootPath);
                     var files =
                         from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
-                        where !file.FullName.StartsWith(fragmentsPath, StringComparison.InvariantCultureIgnoreCase) &&
+                        where !file.FullName.StartsWith(cachePath, StringComparison.InvariantCultureIgnoreCase) &&
                               !file.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase)
                         select file;
                     StatusKeeper.ProcessExistingFiles(files);
@@ -287,7 +298,7 @@ namespace Pithos.Core
             NetworkAgent.ProcessRemoteFiles();
         }
 
-        //Make sure a hidden fragments folder exists to store partial downloads
+        //Make sure a hidden cache folder exists to store partial downloads
         private static string CreateHiddenFolder(string rootPath, string folderName)
         {
             if (String.IsNullOrWhiteSpace(rootPath))
@@ -304,7 +315,16 @@ namespace Pithos.Core
                 var info = Directory.CreateDirectory(folder);
                 info.Attributes |= FileAttributes.Hidden;
 
-                Log.InfoFormat("Created Fragments Folder: {0}", folder);
+                Log.InfoFormat("Created cache Folder: {0}", folder);
+            }
+            else
+            {
+                var info = new DirectoryInfo(folder);
+                if ((info.Attributes & FileAttributes.Hidden) == 0)
+                {
+                    info.Attributes |= FileAttributes.Hidden;
+                    Log.InfoFormat("Reset cache folder to hidden: {0}", folder);
+                }                                
             }
             return folder;
         }
@@ -318,7 +338,7 @@ namespace Pithos.Core
 
             FileAgent.StatusKeeper = StatusKeeper;
             FileAgent.Workflow = Workflow;
-            FileAgent.FragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
+            FileAgent.CachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
             FileAgent.Start(_accountInfo, RootPath);
         }
 
@@ -384,11 +404,114 @@ namespace Pithos.Core
                 Directory.Delete(removedPath,true);
             }
         }
+
+        public IEnumerable<string> GetRootFolders()
+        {
+            var dirs = from container in CloudClient.ListContainers(UserName)
+                       from dir in CloudClient.ListObjects(UserName, container.Name, "")
+                       select dir.Name;
+            return dirs;
+        }
+
+        public ObjectInfo GetObjectInfo(string filePath)
+        {
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            Contract.EndContractBlock();
+
+            var file=new FileInfo(filePath);
+            string relativeUrl;//=file.AsRelativeUrlTo(this.RootPath);
+            var relativePath = file.AsRelativeTo(RootPath);
+            
+            string accountName,container;
+            
+            var parts=relativePath.Split('\\');
+
+            var accountInfo = _accountInfo;
+            if (relativePath.StartsWith(FolderConstants.OthersFolder))
+            {                
+                accountName = parts[1];
+                container = parts[2];
+                relativeUrl = String.Join("/", parts.Splice(3));
+                //Create the root URL for the target account
+                var oldName = UserName;
+                var absoluteUri =  _accountInfo.StorageUri.AbsoluteUri;
+                var nameIndex=absoluteUri.IndexOf(oldName);
+                var root=absoluteUri.Substring(0, nameIndex);
+
+                accountInfo = new AccountInfo
+                {
+                    UserName = accountName,
+                    AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
+                    StorageUri = new Uri(root + accountName),
+                    BlockHash=accountInfo.BlockHash,
+                    BlockSize=accountInfo.BlockSize,
+                    Token=accountInfo.Token
+                };
+            }
+            else
+            {
+                accountName = this.UserName;
+                container = parts[0];
+                relativeUrl = String.Join("/", parts.Splice(1));
+            }
+            
+            var client = new CloudFilesClient(accountInfo);
+            var objectInfo=client.GetObjectInfo(accountName, container, relativeUrl);
+            return objectInfo;
+        }
+        
+        public ContainerInfo GetContainerInfo(string filePath)
+        {
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            Contract.EndContractBlock();
+
+            var file=new FileInfo(filePath);
+            var relativePath = file.AsRelativeTo(RootPath);
+            
+            string accountName,container;
+            
+            var parts=relativePath.Split('\\');
+
+            var accountInfo = _accountInfo;
+            if (relativePath.StartsWith(FolderConstants.OthersFolder))
+            {                
+                accountName = parts[1];
+                container = parts[2];                
+                //Create the root URL for the target account
+                var oldName = UserName;
+                var absoluteUri =  _accountInfo.StorageUri.AbsoluteUri;
+                var nameIndex=absoluteUri.IndexOf(oldName);
+                var root=absoluteUri.Substring(0, nameIndex);
+
+                accountInfo = new AccountInfo
+                {
+                    UserName = accountName,
+                    AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
+                    StorageUri = new Uri(root + accountName),
+                    BlockHash=accountInfo.BlockHash,
+                    BlockSize=accountInfo.BlockSize,
+                    Token=accountInfo.Token
+                };
+            }
+            else
+            {
+                accountName = UserName;
+                container = parts[0];                
+            }
+            
+            var client = new CloudFilesClient(accountInfo);
+            var containerInfo=client.GetContainerInfo(accountName, container);
+            return containerInfo;
+        }
     }
 
+
     public interface IStatusNotification
     {        
         void NotifyChange(string status,TraceLevel level=TraceLevel.Info);
         void NotifyChangedFile(string filePath);
+        void NotifyAccount(AccountInfo policy);
     }
 }