aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/Folder.cs
blob: 07529c80f67000dc5d596c880f34235390e78a28 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
using MediaBrowser.Model.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Common.Logging;
using MediaBrowser.Controller.Resolvers;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

namespace MediaBrowser.Controller.Entities
{
    public class Folder : BaseItem
    {
        #region Events
        /// <summary>
        /// Fires whenever a validation routine updates our children.  The added and removed children are properties of the args.
        /// *** Will fire asynchronously. ***
        /// </summary>
        public event EventHandler<ChildrenChangedEventArgs> ChildrenChanged;
        protected void OnChildrenChanged(ChildrenChangedEventArgs args)
        {
            if (ChildrenChanged != null)
            {
                Task.Run( () => 
                    {
                        ChildrenChanged(this, args);
                        Kernel.Instance.OnLibraryChanged(args);
                    });
            }
        }

        #endregion

        public override bool IsFolder
        {
            get
            {
                return true;
            }
        }

        public bool IsRoot { get; set; }

        public bool IsVirtualFolder
        {
            get
            {
                return Parent != null && Parent.IsRoot;
            }
        }
        protected object childLock = new object();
        protected List<BaseItem> children;
        protected virtual List<BaseItem> ActualChildren
        {
            get
            {
                if (children == null)
                {
                    LoadChildren();
                }
                return children;
            }

            set
            {
                children = value;
            }
        }

        /// <summary>
        /// thread-safe access to the actual children of this folder - without regard to user
        /// </summary>
        public IEnumerable<BaseItem> Children
        {
            get
            {
                lock (childLock)
                    return ActualChildren.ToList();
            }
        }

        /// <summary>
        /// thread-safe access to all recursive children of this folder - without regard to user
        /// </summary>
        public IEnumerable<BaseItem> RecursiveChildren
        {
            get
            {
                foreach (var item in Children)
                {
                    yield return item;

                    var subFolder = item as Folder;

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

        /// <summary>
        /// Loads and validates our children
        /// </summary>
        protected virtual void LoadChildren()
        {
            //first - load our children from the repo
            lock (childLock)
                children = GetCachedChildren();

            //then kick off a validation against the actual file system
            Task.Run(() => ValidateChildren());
        }

        protected bool ChildrenValidating = false;

        /// <summary>
        /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
        /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
        /// </summary>
        /// <returns></returns>
        protected async virtual void ValidateChildren()
        {
            if (ChildrenValidating) return; //only ever want one of these going at once and don't want them to fire off in sequence so don't use lock
            ChildrenValidating = true;
            bool changed = false; //this will save us a little time at the end if nothing changes
            var changedArgs = new ChildrenChangedEventArgs(this);
            //get the current valid children from filesystem (or wherever)
            var nonCachedChildren = await GetNonCachedChildren();
            if (nonCachedChildren == null) return; //nothing to validate
            //build a dictionary of the current children we have now by Id so we can compare quickly and easily
            Dictionary<Guid, BaseItem> currentChildren;
            lock (childLock)
                currentChildren =  ActualChildren.ToDictionary(i => i.Id);
            
            //create a list for our validated children
            var validChildren = new List<BaseItem>();
            //now traverse the valid children and find any changed or new items
            foreach (var child in nonCachedChildren)
            {
                BaseItem currentChild;
                currentChildren.TryGetValue(child.Id, out currentChild);
                if (currentChild == null)
                {
                    //brand new item - needs to be added
                    changed = true;
                    changedArgs.ItemsAdded.Add(child);
                    //refresh it
                    child.RefreshMetadata();
                    Logger.LogInfo("New Item Added to Library: ("+child.GetType().Name+") "+ child.Name + " (" + child.Path + ")");
                    //save it in repo...

                    //and add it to our valid children
                    validChildren.Add(child);
                    //fire an added event...?
                    //if it is a folder we need to validate its children as well
                    Folder folder = child as Folder;
                    if (folder != null)
                    {
                        folder.ValidateChildren();
                        //probably need to refresh too...
                    }
                }
                else
                {
                    //existing item - check if it has changed
                    if (currentChild.IsChanged(child))
                    {
                        changed = true;
                        //update resolve args and refresh meta
                        //  Note - we are refreshing the existing child instead of the newly found one so the "Except" operation below
                        //  will identify this item as the same one
                        currentChild.ResolveArgs = child.ResolveArgs;
                        currentChild.RefreshMetadata();
                        Logger.LogInfo("Item Changed: ("+currentChild.GetType().Name+") "+ currentChild.Name + " (" + currentChild.Path + ")");
                        //save it in repo...
                        validChildren.Add(currentChild);
                    }
                    else
                    {
                        //current child that didn't change - just put it in the valid children
                        validChildren.Add(currentChild);
                    }
                }
            }

            //that's all the new and changed ones - now see if there are any that are missing
            changedArgs.ItemsRemoved = currentChildren.Values.Except(validChildren);
            changed |= changedArgs.ItemsRemoved != null;

            //now, if anything changed - replace our children
            if (changed)
            {
                if (changedArgs.ItemsRemoved != null) foreach (var item in changedArgs.ItemsRemoved) Logger.LogDebugInfo("** " + item.Name + " Removed from library.");

                lock (childLock)
                    ActualChildren = validChildren;
                //and save children in repo...

                //and fire event
                this.OnChildrenChanged(changedArgs);
            }
            ChildrenValidating = false;

        }

        /// <summary>
        /// Get the children of this folder from the actual file system
        /// </summary>
        /// <returns></returns>
        protected async virtual Task<IEnumerable<BaseItem>> GetNonCachedChildren()
        {
            ItemResolveEventArgs args = new ItemResolveEventArgs()
            {
                FileInfo = FileData.GetFileData(this.Path),
                Parent = this.Parent,
                Cancel = false,
                Path = this.Path
            };

            // Gather child folder and files
            if (args.IsDirectory)
            {
                args.FileSystemChildren = FileData.GetFileSystemEntries(this.Path, "*").ToArray();

                bool isVirtualFolder = Parent != null && Parent.IsRoot;
                args = FileSystemHelper.FilterChildFileSystemEntries(args, isVirtualFolder);
            }
            else
            {
                Logger.LogError("Folder has a path that is not a directory: " + this.Path);
                return null;
            }

            if (!EntityResolutionHelper.ShouldResolvePathContents(args))
            {
                return null;
            }
            return (await Task.WhenAll<BaseItem>(GetChildren(args.FileSystemChildren)).ConfigureAwait(false))
                        .Where(i => i != null).OrderBy(f =>
                        {
                            return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;

                        });

        }

        /// <summary>
        /// Resolves a path into a BaseItem
        /// </summary>
        protected async Task<BaseItem> GetChild(string path,  WIN32_FIND_DATA? fileInfo = null)
        {
            ItemResolveEventArgs args = new ItemResolveEventArgs()
            {
                FileInfo = fileInfo ?? FileData.GetFileData(path),
                Parent = this,
                Cancel = false,
                Path = path
            };

            args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
            args = FileSystemHelper.FilterChildFileSystemEntries(args, false);

            return Kernel.Instance.ResolveItem(args);

        }

        /// <summary>
        /// Finds child BaseItems for us
        /// </summary>
        protected Task<BaseItem>[] GetChildren(WIN32_FIND_DATA[] fileSystemChildren)
        {
            Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];

            for (int i = 0; i < fileSystemChildren.Length; i++)
            {
                var child = fileSystemChildren[i];

                tasks[i] = GetChild(child.Path, child);
            }

            return tasks;
        }


        /// <summary>
        /// Get our children from the repo - stubbed for now
        /// </summary>
        /// <returns></returns>
        protected virtual List<BaseItem> GetCachedChildren()
        {
            return new List<BaseItem>();
        }

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

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

                var subFolder = item as Folder;

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

        /// <summary>
        /// Folders need to validate and refresh
        /// </summary>
        /// <returns></returns>
        public override Task ChangedExternally()
        {
            return Task.Run(() =>
                {
                    if (this.IsRoot)
                    {
                        Kernel.Instance.ReloadRoot().ConfigureAwait(false);
                    }
                    else
                    {
                        RefreshMetadata();
                        ValidateChildren();
                    }
                });
        }

        /// <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)
        {
            var counts = new ItemSpecialCounts();

            IEnumerable<BaseItem> recursiveChildren = GetRecursiveChildren(user);

            var recentlyAddedItems = GetRecentlyAddedItems(recursiveChildren, user);

            counts.RecentlyAddedItemCount = recentlyAddedItems.Count;
            counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recentlyAddedItems, user).Count;
            counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count;
            counts.PlayedPercentage = GetPlayedPercentage(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 GetRecursiveChildren(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 GetRecursiveChildren(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 GetRecursiveChildren(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 the user has marked as a favorite
        /// </summary>
        public IEnumerable<BaseItem> GetFavoriteItems(User user)
        {
            return GetRecursiveChildren(user).Where(c =>
            {
                UserItemData data = c.GetUserData(user, false);

                if (data != null)
                {
                    return data.IsFavorite;
                }

                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>
        public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
        {
            return GetRecursiveChildren(user).Where(c =>
            {
                if (c.People != null)
                {
                    return c.People.ContainsKey(person);
                }

                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 GetRecursiveChildren(user).Where(c =>
            {
                if (c.People != null)
                {
                    return c.People.ContainsKey(person) && c.People[person].Type.Equals(personType, StringComparison.OrdinalIgnoreCase);
                }

                return false;
            });
        }

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

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

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

        /// <summary>
        /// Takes a list of items and returns the ones that are recently added
        /// </summary>
        private static List<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
        {
            var list = new List<BaseItem>();

            foreach (var item in itemSet)
            {
                if (!item.IsFolder && item.IsRecentlyAdded(user))
                {
                    list.Add(item);
                }
            }

            return list;
        }

        /// <summary>
        /// Takes a list of items and returns the ones that are recently added and unplayed
        /// </summary>
        private static List<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
        {
            var list = new List<BaseItem>();

            foreach (var item in itemSet)
            {
                if (!item.IsFolder && item.IsRecentlyAdded(user))
                {
                    var userdata = item.GetUserData(user, false);

                    if (userdata == null || userdata.PlayCount == 0)
                    {
                        list.Add(item);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// Takes a list of items and returns the ones that are in progress
        /// </summary>
        private static List<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
        {
            var list = new List<BaseItem>();

            foreach (var item in itemSet)
            {
                if (!item.IsFolder)
                {
                    var userdata = item.GetUserData(user, false);

                    if (userdata != null && userdata.PlaybackPositionTicks > 0)
                    {
                        list.Add(item);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// Gets the total played percentage for a set of items
        /// </summary>
        private static decimal GetPlayedPercentage(IEnumerable<BaseItem> itemSet, User user)
        {
            itemSet = itemSet.Where(i => !(i.IsFolder));

            decimal totalPercent = 0;

            int count = 0;

            foreach (BaseItem item in itemSet)
            {
                count++;
                
                UserItemData data = item.GetUserData(user, false);

                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;
                }
            }

            if (count == 0)
            {
                return 0;
            }

            return totalPercent / count;
        }

        /// <summary>
        /// Marks the item as either played or unplayed
        /// </summary>
        public override void SetPlayedStatus(User user, bool wasPlayed)
        {
            base.SetPlayedStatus(user, wasPlayed);

            // Now sweep through recursively and update status
            foreach (BaseItem item in GetChildren(user))
            {
                item.SetPlayedStatus(user, wasPlayed);
            }
        }

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

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

            //this should be functionally equivilent to what was here since it is IEnum and works on a thread-safe copy
            return RecursiveChildren.FirstOrDefault(i => i.Id == id);
        }

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

            //this should be functionally equivilent to what was here since it is IEnum and works on a thread-safe copy
            return RecursiveChildren.FirstOrDefault(i => i.PhysicalLocations.Contains(path, StringComparer.OrdinalIgnoreCase));
        }
    }
}