Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / SelectiveUris.cs @ cbefd298

History | View | Annotate | Download (4.5 kB)

1
using System;
2
using System.Collections.Concurrent;
3
using System.Collections.Generic;
4
using System.ComponentModel.Composition;
5
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using Pithos.Interfaces;
9
using Pithos.Network;
10

    
11
namespace Pithos.Core.Agents
12
{
13
    [Export(typeof(Selectives))]
14
    public class Selectives
15
    {
16

    
17
        public ConcurrentDictionary<Uri, List<Uri>> SelectiveUris { get; private set; }
18

    
19
        private ConcurrentDictionary<Uri, List<string>> SelectivePaths { get; set; }
20

    
21
        public Selectives()
22
        {
23
            SelectiveUris = new ConcurrentDictionary<Uri, List<Uri>>();
24
            SelectivePaths = new ConcurrentDictionary<Uri, List<string>>();
25
        }
26

    
27
        readonly Dictionary<Uri, bool> _selectiveEnabled = new Dictionary<Uri, bool>();
28

    
29
/*
30
        public void SetIsSelectiveEnabled(AccountInfo account,bool value)
31
        {
32
            _selectiveEnabled[account.AccountKey]=value;
33
        }
34
*/
35

    
36
        public void SetIsSelectiveEnabled(Uri accountKey,bool value)
37
        {
38
            _selectiveEnabled[accountKey]=value;
39
        }
40
/*
41

    
42
        public bool IsSelectiveEnabled(AccountInfo account)
43
        {
44
            bool isEnabled;
45
            _selectiveEnabled.TryGetValue(account.AccountKey, out isEnabled);
46
            return isEnabled;
47
        }
48
*/
49

    
50
        public bool IsSelectiveEnabled(Uri accountKey)
51
        {
52
            bool isEnabled;
53
            _selectiveEnabled.TryGetValue(accountKey, out isEnabled);
54
            return isEnabled;
55
        }
56

    
57

    
58
        public void SetSelectedUris(AccountInfo account,List<Uri> uris)
59
        {
60
            SelectiveUris[account.AccountKey] = uris;
61
            SelectivePaths[account.AccountKey] = UrisToFilePaths(account,uris);
62
        }
63

    
64
        public bool IsSelected(ObjectInfo info)
65
        {
66
            //Shared folders should NOT be synced if selective syncing is disabled
67
            var isShared = info.IsShared;
68
            var selectiveEnabled = IsSelectiveEnabled(info.AccountKey);
69
            if (!selectiveEnabled)
70
                return !isShared;
71

    
72
            List<Uri> filterUris;
73
            return !SelectiveUris.TryGetValue(info.AccountKey, out filterUris) 
74
                || filterUris.Count ==0
75
                || filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f));
76
        }
77

    
78
        public bool IsSelected(AccountInfo account,FileSystemInfo info)
79
        {
80
            if (info is DirectoryInfo)
81
                return true;
82
            return IsSelected(account,info.FullName);
83
        }
84

    
85
        public bool IsSelected(AccountInfo account, string fullPath)
86
        {
87
            if (Directory.Exists(fullPath))
88
                return true;
89

    
90
            //Shared folders should NOT be synced if selective syncing is disabled
91
            var isShared = fullPath.IsSharedTo(account);
92
            var selectiveEnabled = IsSelectiveEnabled(account.AccountKey);
93
            if (!selectiveEnabled)
94
                return !isShared;
95

    
96
            List<string> paths;
97
            return !SelectivePaths.TryGetValue(account.AccountKey, out paths)
98
                || paths.Count == 0
99
                || paths.Any(fullPath.IsAtOrDirectlyBelow);
100
        }
101

    
102
        /// <summary>
103
        /// Return a list of absolute filepaths from a list of Uris
104
        /// </summary>
105
        /// <param name="uris"></param>
106
        /// <returns></returns>
107
        private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)
108
        {
109
            if (uris == null)
110
                return new List<string>();
111

    
112
            var accountPath = account.AccountPath;
113
            var storageUrl = account.StorageUri.ToString();
114
            var own = (from uri in uris
115
                       where uri.ToString().StartsWith(storageUrl)
116
                       let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
117
                       //Trim the account name
118
                       select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList();
119
            var others = (from uri in uris
120
                          where !uri.ToString().StartsWith(storageUrl)
121
                          let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
122
                          //Trim the account name
123
                          select Path.Combine(accountPath, "others-shared", relativePath)).ToList();
124
            return own.Union(others).ToList();
125
        }
126

    
127
    }
128
}