aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Entities/Folder.cs
blob: 105151f3153a0952df1bf7cd6a6230f6f0d1286c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;

namespace MediaBrowser.Model.Entities
{
    public class Folder : BaseItem
    {
        public bool IsRoot { get; set; }

        public bool IsVirtualFolder
        {
            get
            {
                return Parent != null && Parent.IsRoot;
            }
        }

        public BaseItem[] Children { get; set; }

        /// <summary>
        /// Gets allowed children of an item
        /// </summary>
        public IEnumerable<BaseItem> GetParentalAllowedChildren(User user)
        {
            return Children.Where(c => c.IsParentalAllowed(user));
        }

        /// <summary>
        /// Gets allowed recursive children of an item
        /// </summary>
        public IEnumerable<BaseItem> GetParentalAllowedRecursiveChildren(User user)
        {
            foreach (var item in GetParentalAllowedChildren(user))
            {
                yield return item;

                var subFolder = item as Folder;

                if (subFolder != null)
                {
                    foreach (var subitem in subFolder.GetParentalAllowedRecursiveChildren(user))
                    {
                        yield return subitem;
                    }
                }
            }
        }

        /// <summary>
        /// Since it can be slow to make all of these calculations at once, this method will provide a way to get them all back together
        /// </summary>
        public ItemSpecialCounts GetSpecialCounts(User user)
        {
            ItemSpecialCounts counts = new ItemSpecialCounts();

            IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);

            counts.RecentlyAddedItemCount = GetRecentlyAddedItems(recursiveChildren, user).Count();
            counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recursiveChildren, user).Count();
            counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count();
            counts.WatchedPercentage = GetWatchedPercentage(recursiveChildren, user);

            return counts;
        }

        /// <summary>
        /// Finds all recursive items within a top-level parent that contain the given genre and are allowed for the current user
        /// </summary>
        public IEnumerable<BaseItem> GetItemsWithGenre(string genre, User user)
        {
            return GetParentalAllowedRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
        }

        /// <summary>
        /// Finds all recursive items within a top-level parent that contain the given year and are allowed for the current user
        /// </summary>
        public IEnumerable<BaseItem> GetItemsWithYear(int year, User user)
        {
            return GetParentalAllowedRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
        }

        /// <summary>
        /// Finds all recursive items within a top-level parent that contain the given studio and are allowed for the current user
        /// </summary>
        public IEnumerable<BaseItem> GetItemsWithStudio(string studio, User user)
        {
            return GetParentalAllowedRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase)));
        }

        /// <summary>
        /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
        /// </summary>
        public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
        {
            return GetParentalAllowedRecursiveChildren(user).Where(c =>
            {
                if (c.People != null)
                {
                    return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase));
                }

                return false;
            });
        }

        /// <summary>
        /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
        /// </summary>
        /// <param name="personType">Specify this to limit results to a specific PersonType</param>
        public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
        {
            return GetParentalAllowedRecursiveChildren(user).Where(c =>
            {
                if (c.People != null)
                {
                    return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase) && p.Type == personType);
                }

                return false;
            });
        }

        /// <summary>
        /// Gets all recently added items (recursive) within a folder, based on configuration and parental settings
        /// </summary>
        public IEnumerable<BaseItem> GetRecentlyAddedItems(User user)
        {
            return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
        }

        /// <summary>
        /// Gets all recently added unplayed items (recursive) within a folder, based on configuration and parental settings
        /// </summary>
        public IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(User user)
        {
            return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
        }

        /// <summary>
        /// Gets all in-progress items (recursive) within a folder
        /// </summary>
        public IEnumerable<BaseItem> GetInProgressItems(User user)
        {
            return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
        }

        private static IEnumerable<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
        {
            DateTime now = DateTime.Now;

            return itemSet.Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < user.RecentItemDays);
        }

        private static IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
        {
            return GetRecentlyAddedItems(itemSet, user).Where(i =>
            {
                var userdata = i.GetUserData(user);

                return userdata == null || userdata.PlayCount == 0;
            });
        }

        private static IEnumerable<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
        {
            return itemSet.Where(i =>
            {
                if (i is Folder)
                {
                    return false;
                }

                var userdata = i.GetUserData(user);

                return userdata != null && userdata.PlaybackPositionTicks > 0;
            });
        }

        private static decimal GetWatchedPercentage(IEnumerable<BaseItem> itemSet, User user)
        {
            itemSet = itemSet.Where(i => !(i is Folder));

            if (!itemSet.Any())
            {
                return 0;
            }

            decimal totalPercent = 0;

            foreach (BaseItem item in itemSet)
            {
                UserItemData data = item.GetUserData(user);

                if (data == null)
                {
                    continue;
                }

                if (data.PlayCount > 0)
                {
                    totalPercent += 100;
                }
                else if (data.PlaybackPositionTicks > 0 && item.RunTimeTicks.HasValue)
                {
                    decimal itemPercent = data.PlaybackPositionTicks;
                    itemPercent /= item.RunTimeTicks.Value;
                    totalPercent += itemPercent;
                }
            }

            return totalPercent / itemSet.Count();
        }
        
        /// <summary>
        /// Finds an item by ID, recursively
        /// </summary>
        public override BaseItem FindItemById(Guid id)
        {
            var result = base.FindItemById(id);

            if (result != null)
            {
                return result;
            }

            foreach (BaseItem item in Children)
            {
                result = item.FindItemById(id);

                if (result != null)
                {
                    return result;
                }
            }

            return null;
        }

        /// <summary>
        /// Finds an item by path, recursively
        /// </summary>
        public BaseItem FindByPath(string path)
        {
            if (Path.Equals(path, StringComparison.OrdinalIgnoreCase))
            {
                return this;
            }

            foreach (BaseItem item in Children)
            {
                var folder = item as Folder;

                if (folder != null)
                {
                    var foundItem = folder.FindByPath(path);

                    if (foundItem != null)
                    {
                        return foundItem;
                    }
                }
                else if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
                {
                    return item;
                }
            }

            return null;
        }
    }
}