Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / SelectiveUris.cs @ 92740b03

History | View | Annotate | Download (4.6 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??false;
68
            if (info.StorageUri == null)
69
                return true;
70
            var selectiveEnabled = IsSelectiveEnabled(info.AccountKey);
71
            if (!selectiveEnabled)
72
                return !isShared;
73

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

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

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

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

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

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

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

    
129
    }
130
}