2014-12-01 19:42:07 +01:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-07-04 04:22:57 +02:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-09-01 22:10:54 +02:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2016-06-19 08:18:29 +02:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2016-08-16 19:08:37 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-08-08 20:14:05 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-03-19 06:04:38 +01:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Entities
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class Folder
|
|
|
|
|
/// </summary>
|
2016-10-09 09:18:43 +02:00
|
|
|
|
public class Folder : BaseItem
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-10-06 03:04:41 +02:00
|
|
|
|
public static IUserManager UserManager { get; set; }
|
2014-09-01 22:10:54 +02:00
|
|
|
|
public static IUserViewManager UserViewManager { get; set; }
|
2013-10-06 03:04:41 +02:00
|
|
|
|
|
2016-10-09 09:18:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether this instance is root.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
|
|
|
|
|
public bool IsRoot { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual List<LinkedChild> LinkedChildren { get; set; }
|
2014-02-06 05:39:16 +01:00
|
|
|
|
|
2016-05-09 05:13:38 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public DateTime? DateLastMediaAdded { get; set; }
|
|
|
|
|
|
2013-07-05 15:47:10 +02:00
|
|
|
|
public Folder()
|
|
|
|
|
{
|
|
|
|
|
LinkedChildren = new List<LinkedChild>();
|
2016-10-09 09:18:43 +02:00
|
|
|
|
}
|
2013-12-05 17:50:21 +01:00
|
|
|
|
|
2016-10-09 09:18:43 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public override bool SupportsThemeMedia
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
2013-07-05 15:47:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-03 04:16:37 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public virtual bool IsPreSorted
|
|
|
|
|
{
|
2015-09-03 06:16:31 +02:00
|
|
|
|
get { return false; }
|
2014-08-03 04:16:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 09:18:43 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public virtual bool IsPhysicalRoot
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-11 08:46:59 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public override bool SupportsPlayedStatus
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance is folder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public override bool IsFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 05:13:38 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public virtual bool SupportsCumulativeRunTimeTicks
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public virtual bool SupportsDateLastMediaAdded
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 21:52:05 +01:00
|
|
|
|
public override bool CanDelete()
|
|
|
|
|
{
|
|
|
|
|
if (IsRoot)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.CanDelete();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 05:13:38 +02:00
|
|
|
|
public override bool RequiresRefresh()
|
|
|
|
|
{
|
|
|
|
|
var baseResult = base.RequiresRefresh();
|
|
|
|
|
|
|
|
|
|
if (SupportsCumulativeRunTimeTicks && !RunTimeTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
baseResult = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseResult;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 05:57:18 +01:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public override string FileNameWithoutExtension
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (LocationType == LocationType.FileSystem)
|
|
|
|
|
{
|
|
|
|
|
return System.IO.Path.GetFileName(Path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-09 07:56:45 +01:00
|
|
|
|
protected override bool IsAllowTagFilterEnforced()
|
2015-02-09 07:17:11 +01:00
|
|
|
|
{
|
|
|
|
|
if (this is ICollectionFolder)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this is UserView)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 22:29:02 +01:00
|
|
|
|
[IgnoreDataMember]
|
2013-07-06 02:19:44 +02:00
|
|
|
|
protected virtual bool SupportsShortcutChildren
|
2013-07-05 15:47:10 +02:00
|
|
|
|
{
|
2016-04-02 23:08:35 +02:00
|
|
|
|
get { return false; }
|
2013-07-05 15:47:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 19:46:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the child.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.InvalidOperationException">Unable to add + item.Name</exception>
|
|
|
|
|
public async Task AddChild(BaseItem item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-07-09 07:52:25 +02:00
|
|
|
|
item.SetParent(this);
|
2013-05-12 15:13:57 +02:00
|
|
|
|
|
|
|
|
|
if (item.Id == Guid.Empty)
|
|
|
|
|
{
|
2014-11-30 20:01:33 +01:00
|
|
|
|
item.Id = LibraryManager.GetNewItemId(item.Path, item.GetType());
|
2013-05-12 15:13:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 22:11:09 +01:00
|
|
|
|
if (ActualChildren.Any(i => i.Id == item.Id))
|
2013-10-06 03:04:41 +02:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("A child with the Id {0} already exists.", item.Id));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-12 15:13:57 +02:00
|
|
|
|
if (item.DateCreated == DateTime.MinValue)
|
|
|
|
|
{
|
2013-07-17 17:19:28 +02:00
|
|
|
|
item.DateCreated = DateTime.UtcNow;
|
2013-05-12 15:13:57 +02:00
|
|
|
|
}
|
|
|
|
|
if (item.DateModified == DateTime.MinValue)
|
|
|
|
|
{
|
2013-07-17 17:19:28 +02:00
|
|
|
|
item.DateModified = DateTime.UtcNow;
|
2013-05-12 15:13:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 19:46:35 +02:00
|
|
|
|
await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
|
2013-09-19 23:23:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 19:46:35 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the child.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2016-03-21 21:15:18 +01:00
|
|
|
|
public void RemoveChild(BaseItem item)
|
2013-05-03 19:46:35 +02:00
|
|
|
|
{
|
2015-07-09 07:52:25 +02:00
|
|
|
|
item.SetParent(null);
|
2013-05-03 19:46:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the valid set of index by options for this folder type.
|
|
|
|
|
/// Override or extend to modify.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Dictionary{System.StringFunc{UserIEnumerable{BaseItem}}}.</returns>
|
2013-12-11 03:51:26 +01:00
|
|
|
|
protected virtual IEnumerable<string> GetIndexByOptions()
|
|
|
|
|
{
|
2016-05-07 04:11:22 +02:00
|
|
|
|
return new List<string> {
|
|
|
|
|
{"None"},
|
2015-02-05 06:29:37 +01:00
|
|
|
|
{"Performer"},
|
|
|
|
|
{"Genre"},
|
|
|
|
|
{"Director"},
|
|
|
|
|
{"Year"},
|
|
|
|
|
{"Studio"}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the list of indexy by choices for this folder (localized).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The index by option strings.</value>
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public IEnumerable<string> IndexByOptionStrings
|
|
|
|
|
{
|
2013-12-11 03:51:26 +01:00
|
|
|
|
get { return GetIndexByOptions(); }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 22:32:26 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the actual children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The actual children.</value>
|
2016-05-19 21:06:58 +02:00
|
|
|
|
[IgnoreDataMember]
|
2016-03-20 22:32:26 +01:00
|
|
|
|
protected virtual IEnumerable<BaseItem> ActualChildren
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-06-17 15:06:13 +02:00
|
|
|
|
return LoadChildren();
|
2016-03-20 22:32:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// thread-safe access to the actual children of this folder - without regard to user
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The children.</value>
|
|
|
|
|
[IgnoreDataMember]
|
2013-05-03 19:46:35 +02:00
|
|
|
|
public IEnumerable<BaseItem> Children
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-10-11 02:39:30 +02:00
|
|
|
|
get { return ActualChildren.ToList(); }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// thread-safe access to all recursive children of this folder - without regard to user
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The recursive children.</value>
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public IEnumerable<BaseItem> RecursiveChildren
|
|
|
|
|
{
|
2013-09-27 14:24:28 +02:00
|
|
|
|
get { return GetRecursiveChildren(); }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 06:04:11 +01:00
|
|
|
|
public override bool IsVisible(User user)
|
|
|
|
|
{
|
2015-01-22 17:41:34 +01:00
|
|
|
|
if (this is ICollectionFolder && !(this is BasePluginFolder))
|
2014-02-21 06:04:11 +01:00
|
|
|
|
{
|
2015-04-15 05:41:29 +02:00
|
|
|
|
if (user.Policy.BlockedMediaFolders != null)
|
2014-02-21 06:04:11 +01:00
|
|
|
|
{
|
2015-04-15 05:41:29 +02:00
|
|
|
|
if (user.Policy.BlockedMediaFolders.Contains(Id.ToString("N"), StringComparer.OrdinalIgnoreCase) ||
|
|
|
|
|
|
|
|
|
|
// Backwards compatibility
|
|
|
|
|
user.Policy.BlockedMediaFolders.Contains(Name, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!user.Policy.EnableAllFolders && !user.Policy.EnabledFolders.Contains(Id.ToString("N"), StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-21 06:04:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.IsVisible(user);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads our children. Validation will occur externally.
|
|
|
|
|
/// We want this sychronous.
|
|
|
|
|
/// </summary>
|
2016-06-17 15:06:13 +02:00
|
|
|
|
protected virtual IEnumerable<BaseItem> LoadChildren()
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2016-08-13 21:53:20 +02:00
|
|
|
|
//Logger.Debug("Loading children from {0} {1} {2}", GetType().Name, Id, Path);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
//just load our children from the repo - the library will be validated and maintained in other processes
|
2013-09-19 01:33:21 +02:00
|
|
|
|
return GetCachedChildren();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 08:27:44 +01:00
|
|
|
|
public Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-08-06 06:48:00 +02:00
|
|
|
|
return ValidateChildren(progress, cancellationToken, new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem)));
|
2014-02-09 08:27:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validates that the children of the folder still exist
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2014-02-09 08:27:44 +01:00
|
|
|
|
/// <param name="metadataRefreshOptions">The metadata refresh options.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <param name="recursive">if set to <c>true</c> [recursive].</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-02-09 08:27:44 +01:00
|
|
|
|
public Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken, MetadataRefreshOptions metadataRefreshOptions, bool recursive = true)
|
2014-02-06 23:22:03 +01:00
|
|
|
|
{
|
2015-01-27 07:50:40 +01:00
|
|
|
|
return ValidateChildrenInternal(progress, cancellationToken, recursive, true, metadataRefreshOptions, metadataRefreshOptions.DirectoryService);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-23 21:35:58 +01:00
|
|
|
|
private Dictionary<Guid, BaseItem> GetActualChildrenDictionary()
|
|
|
|
|
{
|
|
|
|
|
var dictionary = new Dictionary<Guid, BaseItem>();
|
|
|
|
|
|
|
|
|
|
foreach (var child in ActualChildren)
|
|
|
|
|
{
|
|
|
|
|
var id = child.Id;
|
|
|
|
|
if (dictionary.ContainsKey(id))
|
|
|
|
|
{
|
2014-02-26 05:38:21 +01:00
|
|
|
|
Logger.Error("Found folder containing items with duplicate id. Path: {0}, Child Name: {1}",
|
2014-02-23 21:35:58 +01:00
|
|
|
|
Path ?? Name,
|
|
|
|
|
child.Path ?? child.Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dictionary[id] = child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dictionary;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-03 06:11:03 +01:00
|
|
|
|
private bool IsValidFromResolver(BaseItem current, BaseItem newItem)
|
|
|
|
|
{
|
2014-12-03 04:13:03 +01:00
|
|
|
|
return current.IsValidFromResolver(newItem);
|
2014-03-03 06:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
2014-02-06 23:22:03 +01:00
|
|
|
|
/// Validates the children internal.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <param name="recursive">if set to <c>true</c> [recursive].</param>
|
2014-02-06 23:22:03 +01:00
|
|
|
|
/// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
|
|
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
2014-02-08 23:38:02 +01:00
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <returns>Task.</returns>
|
2014-02-10 19:39:41 +01:00
|
|
|
|
protected async virtual Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-07-05 18:17:32 +02:00
|
|
|
|
var locationType = LocationType;
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-02-06 17:46:26 +01:00
|
|
|
|
var validChildren = new List<BaseItem>();
|
2013-07-07 17:53:38 +02:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
if (locationType != LocationType.Remote && locationType != LocationType.Virtual)
|
2013-07-07 17:53:38 +02:00
|
|
|
|
{
|
2013-12-27 01:52:36 +01:00
|
|
|
|
IEnumerable<BaseItem> nonCachedChildren;
|
2013-07-07 17:53:38 +02:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-02-08 23:38:02 +01:00
|
|
|
|
nonCachedChildren = GetNonCachedChildren(directoryService);
|
2013-12-27 01:52:36 +01:00
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
|
nonCachedChildren = new BaseItem[] { };
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
Logger.ErrorException("Error getting file system entries for {0}", ex, Path);
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
if (nonCachedChildren == null) return; //nothing to validate
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
progress.Report(5);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
//build a dictionary of the current children we have now by Id so we can compare quickly and easily
|
2014-02-23 21:35:58 +01:00
|
|
|
|
var currentChildren = GetActualChildrenDictionary();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
//create a list for our validated children
|
|
|
|
|
var newItems = new List<BaseItem>();
|
2013-04-12 20:22:40 +02:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
foreach (var child in nonCachedChildren)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-12-27 01:52:36 +01:00
|
|
|
|
BaseItem currentChild;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-11-23 17:04:57 +01:00
|
|
|
|
if (currentChildren.TryGetValue(child.Id, out currentChild) && IsValidFromResolver(currentChild, child))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2016-08-24 08:13:15 +02:00
|
|
|
|
await currentChild.UpdateIsOffline(false).ConfigureAwait(false);
|
2015-11-23 17:04:57 +01:00
|
|
|
|
validChildren.Add(currentChild);
|
|
|
|
|
|
|
|
|
|
continue;
|
2015-11-23 17:01:42 +01:00
|
|
|
|
}
|
2015-11-23 17:04:57 +01:00
|
|
|
|
|
|
|
|
|
// Brand new item - needs to be added
|
|
|
|
|
child.SetParent(this);
|
|
|
|
|
newItems.Add(child);
|
|
|
|
|
validChildren.Add(child);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-12-27 01:52:36 +01:00
|
|
|
|
|
|
|
|
|
// If any items were added or removed....
|
|
|
|
|
if (newItems.Count > 0 || currentChildren.Count != validChildren.Count)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-12-27 01:52:36 +01:00
|
|
|
|
// That's all the new and changed ones - now see if there are any that are missing
|
2014-02-06 17:46:26 +01:00
|
|
|
|
var itemsRemoved = currentChildren.Values.Except(validChildren).ToList();
|
2013-12-27 01:52:36 +01:00
|
|
|
|
var actualRemovals = new List<BaseItem>();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
foreach (var item in itemsRemoved)
|
|
|
|
|
{
|
2016-05-25 04:06:56 +02:00
|
|
|
|
var itemLocationType = item.LocationType;
|
|
|
|
|
if (itemLocationType == LocationType.Virtual ||
|
|
|
|
|
itemLocationType == LocationType.Remote)
|
2013-12-27 01:52:36 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
else if (!string.IsNullOrEmpty(item.Path) && IsPathOffline(item.Path))
|
|
|
|
|
{
|
2016-08-24 08:13:15 +02:00
|
|
|
|
await item.UpdateIsOffline(true).ConfigureAwait(false);
|
2013-12-27 01:52:36 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
actualRemovals.Add(item);
|
|
|
|
|
}
|
2013-10-17 01:35:11 +02:00
|
|
|
|
}
|
2013-07-05 20:23:41 +02:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
if (actualRemovals.Count > 0)
|
2013-10-06 03:04:41 +02:00
|
|
|
|
{
|
2013-12-27 01:52:36 +01:00
|
|
|
|
foreach (var item in actualRemovals)
|
|
|
|
|
{
|
2015-11-23 17:04:57 +01:00
|
|
|
|
Logger.Debug("Removed item: " + item.Path);
|
|
|
|
|
|
|
|
|
|
item.SetParent(null);
|
|
|
|
|
item.IsOffline = false;
|
|
|
|
|
await LibraryManager.DeleteItem(item, new DeleteOptions { DeleteFileLocation = false }).ConfigureAwait(false);
|
2013-12-27 01:52:36 +01:00
|
|
|
|
LibraryManager.ReportItemRemoved(item);
|
|
|
|
|
}
|
2013-09-23 20:28:07 +02:00
|
|
|
|
}
|
2013-09-19 02:37:01 +02:00
|
|
|
|
|
2013-12-27 01:52:36 +01:00
|
|
|
|
await LibraryManager.CreateItems(newItems, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-03-28 20:25:36 +01:00
|
|
|
|
progress.Report(10);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
if (recursive)
|
|
|
|
|
{
|
2014-02-08 23:38:02 +01:00
|
|
|
|
await ValidateSubFolders(ActualChildren.OfType<Folder>().ToList(), directoryService, progress, cancellationToken).ConfigureAwait(false);
|
2014-02-06 23:22:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(20);
|
|
|
|
|
|
|
|
|
|
if (refreshChildMetadata)
|
|
|
|
|
{
|
|
|
|
|
var container = this as IMetadataContainer;
|
|
|
|
|
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
2016-03-27 23:11:27 +02:00
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(.80 * p + 20));
|
2014-02-06 23:22:03 +01:00
|
|
|
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
{
|
|
|
|
|
await container.RefreshAllMetadata(refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await RefreshMetadataRecursive(refreshOptions, recursive, innerProgress, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-02-22 05:23:06 +01:00
|
|
|
|
progress.Report(100);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
private async Task RefreshMetadataRecursive(MetadataRefreshOptions refreshOptions, bool recursive, IProgress<double> progress, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-06 23:22:03 +01:00
|
|
|
|
var children = ActualChildren.ToList();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
var percentages = new Dictionary<Guid, double>(children.Count);
|
2015-01-29 04:58:39 +01:00
|
|
|
|
var numComplete = 0;
|
|
|
|
|
var count = children.Count;
|
2013-04-15 17:10:12 +02:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
foreach (var child in children)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-06 23:22:03 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2014-02-09 08:27:44 +01:00
|
|
|
|
|
2015-01-29 04:58:39 +01:00
|
|
|
|
if (child.IsFolder)
|
2014-02-06 23:22:03 +01:00
|
|
|
|
{
|
2015-01-29 04:58:39 +01:00
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentChild = child;
|
|
|
|
|
innerProgress.RegisterAction(p =>
|
2014-02-06 23:22:03 +01:00
|
|
|
|
{
|
2015-01-29 04:58:39 +01:00
|
|
|
|
lock (percentages)
|
|
|
|
|
{
|
|
|
|
|
percentages[currentChild.Id] = p / 100;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-01-29 04:58:39 +01:00
|
|
|
|
var innerPercent = percentages.Values.Sum();
|
|
|
|
|
innerPercent /= count;
|
|
|
|
|
innerPercent *= 100;
|
|
|
|
|
progress.Report(innerPercent);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-02-06 23:22:03 +01:00
|
|
|
|
|
|
|
|
|
await RefreshChildMetadata(child, refreshOptions, recursive, innerProgress, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-29 04:58:39 +01:00
|
|
|
|
await RefreshChildMetadata(child, refreshOptions, false, new Progress<double>(), cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-02-06 23:22:03 +01:00
|
|
|
|
}
|
2015-01-29 04:58:39 +01:00
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= count;
|
|
|
|
|
percent *= 100;
|
|
|
|
|
|
|
|
|
|
progress.Report(percent);
|
2014-02-06 23:22:03 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
progress.Report(100);
|
2014-01-01 19:26:31 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
private async Task RefreshChildMetadata(BaseItem child, MetadataRefreshOptions refreshOptions, bool recursive, IProgress<double> progress, CancellationToken cancellationToken)
|
2014-01-01 19:26:31 +01:00
|
|
|
|
{
|
2014-02-06 23:22:03 +01:00
|
|
|
|
var container = child as IMetadataContainer;
|
2013-04-15 17:10:12 +02:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
if (container != null)
|
2014-01-01 19:26:31 +01:00
|
|
|
|
{
|
2014-02-06 23:22:03 +01:00
|
|
|
|
await container.RefreshAllMetadata(refreshOptions, progress, cancellationToken).ConfigureAwait(false);
|
2014-01-01 19:26:31 +01:00
|
|
|
|
}
|
2014-02-06 23:22:03 +01:00
|
|
|
|
else
|
2014-01-01 19:26:31 +01:00
|
|
|
|
{
|
2014-02-06 23:22:03 +01:00
|
|
|
|
await child.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
|
{
|
|
|
|
|
var folder = child as Folder;
|
|
|
|
|
|
|
|
|
|
if (folder != null)
|
|
|
|
|
{
|
|
|
|
|
await folder.RefreshMetadataRecursive(refreshOptions, true, progress, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-01 19:26:31 +01:00
|
|
|
|
}
|
2014-02-06 23:22:03 +01:00
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes the children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="children">The children.</param>
|
2014-02-08 23:38:02 +01:00
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
2014-02-06 23:22:03 +01:00
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-02-10 19:39:41 +01:00
|
|
|
|
private async Task ValidateSubFolders(IList<Folder> children, IDirectoryService directoryService, IProgress<double> progress, CancellationToken cancellationToken)
|
2014-02-06 23:22:03 +01:00
|
|
|
|
{
|
|
|
|
|
var list = children;
|
|
|
|
|
var childCount = list.Count;
|
2013-04-15 17:10:12 +02:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
var percentages = new Dictionary<Guid, double>(list.Count);
|
|
|
|
|
|
|
|
|
|
foreach (var item in list)
|
2014-01-01 19:26:31 +01:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-04-15 17:10:12 +02:00
|
|
|
|
|
2014-02-06 23:22:03 +01:00
|
|
|
|
var child = item;
|
|
|
|
|
|
2014-01-01 19:26:31 +01:00
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
2013-06-28 22:25:58 +02:00
|
|
|
|
|
2014-01-01 19:26:31 +01:00
|
|
|
|
innerProgress.RegisterAction(p =>
|
|
|
|
|
{
|
|
|
|
|
lock (percentages)
|
2013-03-28 20:25:36 +01:00
|
|
|
|
{
|
2014-01-01 19:26:31 +01:00
|
|
|
|
percentages[child.Id] = p / 100;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-01-01 19:26:31 +01:00
|
|
|
|
var percent = percentages.Values.Sum();
|
|
|
|
|
percent /= childCount;
|
2013-03-28 20:25:36 +01:00
|
|
|
|
|
2016-03-27 23:11:27 +02:00
|
|
|
|
progress.Report(10 * percent + 10);
|
2013-04-15 17:10:12 +02:00
|
|
|
|
}
|
2014-01-01 19:26:31 +01:00
|
|
|
|
});
|
|
|
|
|
|
2015-01-27 07:50:40 +01:00
|
|
|
|
await child.ValidateChildrenInternal(innerProgress, cancellationToken, true, false, null, directoryService)
|
2014-02-08 21:22:40 +01:00
|
|
|
|
.ConfigureAwait(false);
|
2013-04-15 17:10:12 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-05 16:54:14 +02:00
|
|
|
|
/// <summary>
|
2013-10-06 03:04:41 +02:00
|
|
|
|
/// Determines whether the specified path is offline.
|
2013-07-05 16:54:14 +02:00
|
|
|
|
/// </summary>
|
2013-10-06 03:04:41 +02:00
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if the specified path is offline; otherwise, <c>false</c>.</returns>
|
2015-09-21 17:43:10 +02:00
|
|
|
|
public static bool IsPathOffline(string path)
|
2013-07-05 16:54:14 +02:00
|
|
|
|
{
|
2015-09-17 03:33:46 +02:00
|
|
|
|
if (FileSystem.FileExists(path))
|
2013-07-07 17:53:38 +02:00
|
|
|
|
{
|
2013-10-06 03:04:41 +02:00
|
|
|
|
return false;
|
2013-07-07 17:53:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 03:04:41 +02:00
|
|
|
|
var originalPath = path;
|
|
|
|
|
|
2013-07-06 02:19:44 +02:00
|
|
|
|
// Depending on whether the path is local or unc, it may return either null or '\' at the top
|
2013-07-07 03:41:04 +02:00
|
|
|
|
while (!string.IsNullOrEmpty(path) && path.Length > 1)
|
2013-07-05 16:54:14 +02:00
|
|
|
|
{
|
2015-09-17 03:33:46 +02:00
|
|
|
|
if (FileSystem.DirectoryExists(path))
|
2013-07-05 16:54:14 +02:00
|
|
|
|
{
|
2013-10-06 03:04:41 +02:00
|
|
|
|
return false;
|
2013-07-05 16:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-07 03:41:04 +02:00
|
|
|
|
path = System.IO.Path.GetDirectoryName(path);
|
2013-07-05 16:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 04:54:45 +01:00
|
|
|
|
if (ContainsPath(LibraryManager.GetVirtualFolders(), originalPath))
|
2013-10-06 03:04:41 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 17:04:57 +01:00
|
|
|
|
return false;
|
2013-10-06 03:04:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the specified folders contains path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="folders">The folders.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if the specified folders contains path; otherwise, <c>false</c>.</returns>
|
2015-09-21 17:43:10 +02:00
|
|
|
|
private static bool ContainsPath(IEnumerable<VirtualFolderInfo> folders, string path)
|
2013-10-06 03:04:41 +02:00
|
|
|
|
{
|
|
|
|
|
return folders.SelectMany(i => i.Locations).Any(i => ContainsPath(i, path));
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 17:43:10 +02:00
|
|
|
|
private static bool ContainsPath(string parent, string path)
|
2013-10-06 03:04:41 +02:00
|
|
|
|
{
|
2014-01-01 19:26:31 +01:00
|
|
|
|
return string.Equals(parent, path, StringComparison.OrdinalIgnoreCase) || FileSystem.ContainsSubPath(parent, path);
|
2013-07-05 16:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the children of this folder from the actual file system
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
2014-02-10 19:39:41 +01:00
|
|
|
|
protected virtual IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-12-21 19:58:17 +01:00
|
|
|
|
var collectionType = LibraryManager.GetContentType(this);
|
2016-08-13 07:49:00 +02:00
|
|
|
|
var libraryOptions = LibraryManager.GetLibraryOptions(this);
|
2014-10-23 06:26:01 +02:00
|
|
|
|
|
2016-08-13 07:49:00 +02:00
|
|
|
|
return LibraryManager.ResolvePaths(GetFileSystemChildren(directoryService), directoryService, this, libraryOptions, collectionType);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get our children from the repo - stubbed for now
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
2016-06-17 15:06:13 +02:00
|
|
|
|
protected IEnumerable<BaseItem> GetCachedChildren()
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2016-06-17 15:06:13 +02:00
|
|
|
|
return ItemRepository.GetItemList(new InternalItemsQuery
|
2015-09-24 04:31:40 +02:00
|
|
|
|
{
|
2016-05-06 06:50:39 +02:00
|
|
|
|
ParentId = Id,
|
|
|
|
|
GroupByPresentationUniqueKey = false
|
2016-03-21 21:15:18 +01:00
|
|
|
|
});
|
2014-10-23 06:26:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-16 15:24:12 +02:00
|
|
|
|
public virtual int GetChildCount(User user)
|
|
|
|
|
{
|
|
|
|
|
if (LinkedChildren.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (!(this is ICollectionFolder))
|
|
|
|
|
{
|
|
|
|
|
return GetChildren(user, true).Count();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = GetItems(new InternalItemsQuery(user)
|
|
|
|
|
{
|
|
|
|
|
Recursive = false,
|
|
|
|
|
Limit = 0,
|
|
|
|
|
ParentId = Id
|
|
|
|
|
|
|
|
|
|
}).Result;
|
|
|
|
|
|
|
|
|
|
return result.TotalRecordCount;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 18:36:00 +01:00
|
|
|
|
public virtual int GetRecursiveChildCount(User user)
|
|
|
|
|
{
|
|
|
|
|
return GetItems(new InternalItemsQuery(user)
|
|
|
|
|
{
|
|
|
|
|
Recursive = true,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsVirtualItem = false,
|
|
|
|
|
EnableTotalRecordCount = true,
|
|
|
|
|
Limit = 0
|
|
|
|
|
|
|
|
|
|
}).Result.TotalRecordCount;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 07:46:51 +01:00
|
|
|
|
public QueryResult<BaseItem> QueryRecursive(InternalItemsQuery query)
|
|
|
|
|
{
|
|
|
|
|
var user = query.User;
|
|
|
|
|
|
2016-05-18 07:34:10 +02:00
|
|
|
|
if (!query.ForceDirect && RequiresPostFiltering(query))
|
2016-03-20 07:46:51 +01:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<BaseItem> items;
|
|
|
|
|
Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager);
|
|
|
|
|
|
|
|
|
|
if (query.User == null)
|
|
|
|
|
{
|
|
|
|
|
items = GetRecursiveChildren(filter);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
items = GetRecursiveChildren(user, query);
|
2016-03-20 07:46:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-17 22:52:16 +02:00
|
|
|
|
return PostFilterAndSort(items, query, true, true);
|
2016-03-20 07:46:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(this is UserRootFolder) && !(this is AggregateFolder))
|
|
|
|
|
{
|
|
|
|
|
query.ParentId = query.ParentId ?? Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LibraryManager.GetItemsResult(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool RequiresPostFiltering(InternalItemsQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (LinkedChildren.Count > 0)
|
|
|
|
|
{
|
2016-03-20 19:39:20 +01:00
|
|
|
|
if (!(this is ICollectionFolder))
|
|
|
|
|
{
|
2016-06-04 02:15:14 +02:00
|
|
|
|
Logger.Debug("Query requires post-filtering due to LinkedChildren. Type: " + GetType().Name);
|
2016-03-20 19:39:20 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-03-20 07:46:51 +01:00
|
|
|
|
}
|
2016-05-20 23:18:48 +02:00
|
|
|
|
|
2016-03-20 07:46:51 +01:00
|
|
|
|
if (query.SortBy != null && query.SortBy.Length > 0)
|
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.AiredEpisodeOrder, StringComparer.OrdinalIgnoreCase))
|
2016-03-20 07:46:51 +01:00
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.AiredEpisodeOrder");
|
2016-03-20 07:46:51 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.Budget, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.Budget");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.GameSystem, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.GameSystem");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.Metascore, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.Metascore");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.Players, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.Players");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.Revenue, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.Revenue");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (query.SortBy.Contains(ItemSortBy.VideoBitRate, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemSortBy.VideoBitRate");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 22:32:26 +01:00
|
|
|
|
if (query.ItemIds.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemIds");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 07:46:51 +01:00
|
|
|
|
if (query.IsInBoxSet.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to IsInBoxSet");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter by Video3DFormat
|
|
|
|
|
if (query.Is3D.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to Is3D");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.HasOfficialRating.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to HasOfficialRating");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IsPlaceHolder.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to IsPlaceHolder");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.HasSpecialFeature.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to HasSpecialFeature");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.HasSubtitles.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to HasSubtitles");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.HasTrailer.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to HasTrailer");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter by VideoType
|
|
|
|
|
if (query.VideoTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to VideoTypes");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply person filter
|
|
|
|
|
if (query.ItemIdsFromPersonFilters != null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to ItemIdsFromPersonFilters");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.MinPlayers.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to MinPlayers");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.MaxPlayers.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to MaxPlayers");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-05 21:44:55 +02:00
|
|
|
|
if (UserViewBuilder.CollapseBoxSetItems(query, this, query.User, ConfigurationManager))
|
2016-03-20 07:46:51 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to CollapseBoxSetItems");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AdjacentTo))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to AdjacentTo");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 20:53:22 +01:00
|
|
|
|
if (query.AirDays.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to AirDays");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.SeriesStatuses.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to SeriesStatuses");
|
|
|
|
|
return true;
|
2016-03-20 21:04:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.AiredDuringSeason.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to AiredDuringSeason");
|
|
|
|
|
return true;
|
2016-03-21 01:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AlbumArtistStartsWithOrGreater))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to AlbumArtistStartsWithOrGreater");
|
|
|
|
|
return true;
|
2016-03-21 17:50:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 20:14:05 +02:00
|
|
|
|
if (query.IsPlayed.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (query.IncludeItemTypes.Length == 1 && query.IncludeItemTypes.Contains(typeof(Series).Name))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Query requires post-filtering due to IsPlayed");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 07:46:51 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 23:08:35 +02:00
|
|
|
|
public Task<QueryResult<BaseItem>> GetItems(InternalItemsQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (query.ItemIds.Length > 0)
|
|
|
|
|
{
|
2016-08-25 18:55:57 +02:00
|
|
|
|
var result = LibraryManager.GetItemsResult(query);
|
2016-08-18 17:13:18 +02:00
|
|
|
|
|
|
|
|
|
if (query.SortBy.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
var ids = query.ItemIds.ToList();
|
|
|
|
|
|
|
|
|
|
// Try to preserve order
|
2016-08-25 18:55:57 +02:00
|
|
|
|
result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id.ToString("N"))).ToArray();
|
2016-08-18 17:13:18 +02:00
|
|
|
|
}
|
2016-08-25 18:55:57 +02:00
|
|
|
|
return Task.FromResult(result);
|
2016-04-02 23:08:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetItemsInternal(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual async Task<QueryResult<BaseItem>> GetItemsInternal(InternalItemsQuery query)
|
2014-09-01 22:10:54 +02:00
|
|
|
|
{
|
2016-03-19 06:04:38 +01:00
|
|
|
|
if (SourceType == SourceType.Channel)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Don't blow up here because it could cause parent screens with other content to fail
|
|
|
|
|
return await ChannelManager.GetChannelItemsInternal(new ChannelItemQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelId = ChannelId,
|
|
|
|
|
FolderId = Id.ToString("N"),
|
|
|
|
|
Limit = query.Limit,
|
|
|
|
|
StartIndex = query.StartIndex,
|
|
|
|
|
UserId = query.User.Id.ToString("N"),
|
|
|
|
|
SortBy = query.SortBy,
|
|
|
|
|
SortOrder = query.SortOrder
|
|
|
|
|
|
|
|
|
|
}, new Progress<double>(), CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Already logged at lower levels
|
2016-07-29 21:18:03 +02:00
|
|
|
|
return new QueryResult<BaseItem>();
|
2016-03-19 06:04:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-20 07:46:51 +01:00
|
|
|
|
|
|
|
|
|
if (query.Recursive)
|
|
|
|
|
{
|
|
|
|
|
return QueryRecursive(query);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 22:10:54 +02:00
|
|
|
|
var user = query.User;
|
|
|
|
|
|
2015-01-25 07:34:50 +01:00
|
|
|
|
Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager);
|
|
|
|
|
|
2015-02-07 04:25:23 +01:00
|
|
|
|
IEnumerable<BaseItem> items;
|
|
|
|
|
|
|
|
|
|
if (query.User == null)
|
|
|
|
|
{
|
|
|
|
|
items = query.Recursive
|
|
|
|
|
? GetRecursiveChildren(filter)
|
|
|
|
|
: Children.Where(filter);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
items = query.Recursive
|
2016-05-18 07:34:10 +02:00
|
|
|
|
? GetRecursiveChildren(user, query)
|
2015-02-07 04:25:23 +01:00
|
|
|
|
: GetChildren(user, true).Where(filter);
|
|
|
|
|
}
|
2014-09-01 22:10:54 +02:00
|
|
|
|
|
2016-08-17 22:52:16 +02:00
|
|
|
|
return PostFilterAndSort(items, query, true, true);
|
2014-09-01 22:10:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-17 22:52:16 +02:00
|
|
|
|
protected QueryResult<BaseItem> PostFilterAndSort(IEnumerable<BaseItem> items, InternalItemsQuery query, bool collapseBoxSetItems, bool enableSorting)
|
2014-09-01 22:10:54 +02:00
|
|
|
|
{
|
2016-08-17 22:52:16 +02:00
|
|
|
|
return UserViewBuilder.PostFilterAndSort(items, this, null, query, LibraryManager, ConfigurationManager, collapseBoxSetItems, enableSorting);
|
2014-09-01 22:10:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 03:51:26 +01:00
|
|
|
|
public virtual IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//the true root should return our users root folder children
|
2013-12-11 03:51:26 +01:00
|
|
|
|
if (IsPhysicalRoot) return user.RootFolder.GetChildren(user, includeLinkedChildren);
|
2013-07-05 15:47:10 +02:00
|
|
|
|
|
2015-01-25 07:34:50 +01:00
|
|
|
|
var result = new Dictionary<Guid, BaseItem>();
|
2013-09-26 20:45:43 +02:00
|
|
|
|
|
2015-11-23 17:04:57 +01:00
|
|
|
|
AddChildren(user, includeLinkedChildren, result, false, null);
|
2013-09-26 20:45:43 +02:00
|
|
|
|
|
2015-01-25 07:34:50 +01:00
|
|
|
|
return result.Values;
|
2013-09-20 04:03:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 04:32:40 +02:00
|
|
|
|
protected virtual IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
|
|
|
|
|
{
|
|
|
|
|
return Children;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 04:03:37 +02:00
|
|
|
|
/// <summary>
|
2013-09-26 20:45:43 +02:00
|
|
|
|
/// Adds the children to list.
|
2013-09-20 04:03:37 +02:00
|
|
|
|
/// </summary>
|
2013-09-26 23:20:26 +02:00
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
2016-05-18 07:34:10 +02:00
|
|
|
|
private void AddChildren(User user, bool includeLinkedChildren, Dictionary<Guid, BaseItem> result, bool recursive, InternalItemsQuery query)
|
2013-09-20 04:03:37 +02:00
|
|
|
|
{
|
2014-06-05 04:32:40 +02:00
|
|
|
|
foreach (var child in GetEligibleChildrenForRecursiveChildren(user))
|
2013-09-20 04:03:37 +02:00
|
|
|
|
{
|
|
|
|
|
if (child.IsVisible(user))
|
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
if (query == null || UserViewBuilder.FilterItem(child, query))
|
2013-09-26 23:20:26 +02:00
|
|
|
|
{
|
2015-11-23 17:04:57 +01:00
|
|
|
|
result[child.Id] = child;
|
2013-09-26 23:20:26 +02:00
|
|
|
|
}
|
2013-09-26 20:45:43 +02:00
|
|
|
|
|
2014-02-21 06:04:11 +01:00
|
|
|
|
if (recursive && child.IsFolder)
|
2013-09-26 20:45:43 +02:00
|
|
|
|
{
|
2014-02-21 06:04:11 +01:00
|
|
|
|
var folder = (Folder)child;
|
|
|
|
|
|
2016-05-18 07:34:10 +02:00
|
|
|
|
folder.AddChildren(user, includeLinkedChildren, result, true, query);
|
2013-09-26 20:45:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-20 04:03:37 +02:00
|
|
|
|
}
|
2013-07-05 15:47:10 +02:00
|
|
|
|
|
|
|
|
|
if (includeLinkedChildren)
|
|
|
|
|
{
|
2014-08-14 15:24:30 +02:00
|
|
|
|
foreach (var child in GetLinkedChildren(user))
|
2013-09-20 04:03:37 +02:00
|
|
|
|
{
|
|
|
|
|
if (child.IsVisible(user))
|
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
if (query == null || UserViewBuilder.FilterItem(child, query))
|
2015-01-25 07:34:50 +01:00
|
|
|
|
{
|
|
|
|
|
result[child.Id] = child;
|
|
|
|
|
}
|
2013-09-20 04:03:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-05 15:47:10 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets allowed recursive children of an item
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="user">The user.</param>
|
2013-07-05 15:47:10 +02:00
|
|
|
|
/// <param name="includeLinkedChildren">if set to <c>true</c> [include linked children].</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException"></exception>
|
2015-01-25 07:34:50 +01:00
|
|
|
|
public IEnumerable<BaseItem> GetRecursiveChildren(User user, bool includeLinkedChildren = true)
|
|
|
|
|
{
|
2016-05-18 07:34:10 +02:00
|
|
|
|
return GetRecursiveChildren(user, null);
|
2015-01-25 07:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 07:34:10 +02:00
|
|
|
|
public virtual IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
2013-10-04 21:48:31 +02:00
|
|
|
|
throw new ArgumentNullException("user");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-25 07:34:50 +01:00
|
|
|
|
var result = new Dictionary<Guid, BaseItem>();
|
2013-09-20 04:03:37 +02:00
|
|
|
|
|
2016-05-18 07:34:10 +02:00
|
|
|
|
AddChildren(user, true, result, true, query);
|
2013-08-24 00:13:18 +02:00
|
|
|
|
|
2015-01-25 07:34:50 +01:00
|
|
|
|
return result.Values;
|
2013-08-24 00:13:18 +02:00
|
|
|
|
}
|
2013-09-27 14:24:28 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the recursive children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IList{BaseItem}.</returns>
|
|
|
|
|
public IList<BaseItem> GetRecursiveChildren()
|
2015-01-25 07:34:50 +01:00
|
|
|
|
{
|
2016-09-07 19:17:26 +02:00
|
|
|
|
return GetRecursiveChildren(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<BaseItem> GetRecursiveChildren(bool includeLinkedChildren)
|
|
|
|
|
{
|
|
|
|
|
return GetRecursiveChildren(i => true, includeLinkedChildren);
|
2015-01-25 07:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter)
|
2016-09-07 19:17:26 +02:00
|
|
|
|
{
|
|
|
|
|
return GetRecursiveChildren(filter, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter, bool includeLinkedChildren)
|
2013-09-27 14:24:28 +02:00
|
|
|
|
{
|
2016-05-09 06:56:41 +02:00
|
|
|
|
var result = new Dictionary<Guid, BaseItem>();
|
2013-09-27 14:24:28 +02:00
|
|
|
|
|
2016-10-13 20:43:47 +02:00
|
|
|
|
AddChildrenToList(result, includeLinkedChildren, true, filter);
|
2013-09-27 14:24:28 +02:00
|
|
|
|
|
2016-05-09 06:56:41 +02:00
|
|
|
|
return result.Values.ToList();
|
2013-09-27 14:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the children to list.
|
|
|
|
|
/// </summary>
|
2016-05-20 23:18:48 +02:00
|
|
|
|
private void AddChildrenToList(Dictionary<Guid, BaseItem> result, bool includeLinkedChildren, bool recursive, Func<BaseItem, bool> filter)
|
2013-09-27 14:24:28 +02:00
|
|
|
|
{
|
|
|
|
|
foreach (var child in Children)
|
|
|
|
|
{
|
|
|
|
|
if (filter == null || filter(child))
|
|
|
|
|
{
|
2016-05-09 06:56:41 +02:00
|
|
|
|
result[child.Id] = child;
|
2013-09-27 14:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 21:48:26 +01:00
|
|
|
|
if (recursive && child.IsFolder)
|
2013-09-27 14:24:28 +02:00
|
|
|
|
{
|
2013-11-21 21:48:26 +01:00
|
|
|
|
var folder = (Folder)child;
|
2013-09-27 14:24:28 +02:00
|
|
|
|
|
2016-05-11 16:36:28 +02:00
|
|
|
|
// We can only support includeLinkedChildren for the first folder, or we might end up stuck in a loop of linked items
|
|
|
|
|
folder.AddChildrenToList(result, false, true, filter);
|
2016-05-09 06:56:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includeLinkedChildren)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in GetLinkedChildren())
|
|
|
|
|
{
|
|
|
|
|
if (filter == null || filter(child))
|
|
|
|
|
{
|
|
|
|
|
result[child.Id] = child;
|
|
|
|
|
}
|
2013-09-27 14:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-07-05 15:47:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the linked children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
|
|
|
|
public IEnumerable<BaseItem> GetLinkedChildren()
|
|
|
|
|
{
|
|
|
|
|
return LinkedChildren
|
2013-07-06 02:19:44 +02:00
|
|
|
|
.Select(GetLinkedChild)
|
2013-07-05 15:47:10 +02:00
|
|
|
|
.Where(i => i != null);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 15:24:30 +02:00
|
|
|
|
protected virtual bool FilterLinkedChildrenPerUser
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<BaseItem> GetLinkedChildren(User user)
|
|
|
|
|
{
|
2014-08-15 18:35:41 +02:00
|
|
|
|
if (!FilterLinkedChildrenPerUser || user == null)
|
2014-08-14 15:24:30 +02:00
|
|
|
|
{
|
|
|
|
|
return GetLinkedChildren();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var locations = user.RootFolder
|
2015-04-05 17:01:57 +02:00
|
|
|
|
.Children
|
2014-08-14 15:24:30 +02:00
|
|
|
|
.OfType<CollectionFolder>()
|
2015-04-05 17:01:57 +02:00
|
|
|
|
.Where(i => i.IsVisible(user))
|
2014-08-14 15:24:30 +02:00
|
|
|
|
.SelectMany(i => i.PhysicalLocations)
|
|
|
|
|
.ToList();
|
2014-09-01 22:10:54 +02:00
|
|
|
|
|
2014-08-15 18:35:41 +02:00
|
|
|
|
return LinkedChildren
|
|
|
|
|
.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var child = GetLinkedChild(i);
|
|
|
|
|
|
2016-10-01 22:29:24 +02:00
|
|
|
|
if (child != null)
|
2014-08-15 18:35:41 +02:00
|
|
|
|
{
|
2016-10-01 22:29:24 +02:00
|
|
|
|
var childLocationType = child.LocationType;
|
|
|
|
|
if (childLocationType == LocationType.Remote || childLocationType == LocationType.Virtual)
|
2014-08-15 18:35:41 +02:00
|
|
|
|
{
|
2016-10-01 22:29:24 +02:00
|
|
|
|
if (!child.IsVisibleStandalone(user))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-08-15 18:35:41 +02:00
|
|
|
|
}
|
2016-10-01 22:29:24 +02:00
|
|
|
|
else if (childLocationType == LocationType.FileSystem && !locations.Any(l => FileSystem.ContainsSubPath(l, child.Path)))
|
2014-08-15 18:35:41 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
})
|
2014-08-14 15:24:30 +02:00
|
|
|
|
.Where(i => i != null);
|
|
|
|
|
}
|
2014-08-12 01:41:11 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the linked children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
2014-09-01 22:10:54 +02:00
|
|
|
|
public IEnumerable<Tuple<LinkedChild, BaseItem>> GetLinkedChildrenInfos()
|
2014-08-12 01:41:11 +02:00
|
|
|
|
{
|
|
|
|
|
return LinkedChildren
|
2014-09-01 22:10:54 +02:00
|
|
|
|
.Select(i => new Tuple<LinkedChild, BaseItem>(i, GetLinkedChild(i)))
|
2014-08-12 01:41:11 +02:00
|
|
|
|
.Where(i => i.Item2 != null);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 22:29:02 +01:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
protected override bool SupportsOwnedItems
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return base.SupportsOwnedItems || SupportsShortcutChildren;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 05:38:46 +02:00
|
|
|
|
protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
|
2013-07-05 15:47:10 +02:00
|
|
|
|
{
|
2014-02-10 19:39:41 +01:00
|
|
|
|
var changesFound = false;
|
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
if (LocationType == LocationType.FileSystem)
|
2014-02-03 06:35:43 +01:00
|
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
|
if (RefreshLinkedChildren(fileSystemChildren))
|
|
|
|
|
{
|
2014-02-10 19:39:41 +01:00
|
|
|
|
changesFound = true;
|
2014-02-06 05:39:16 +01:00
|
|
|
|
}
|
2014-02-03 06:35:43 +01:00
|
|
|
|
}
|
2013-07-05 15:47:10 +02:00
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
var baseHasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return baseHasChanges || changesFound;
|
2013-07-05 15:47:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes the linked children.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
2016-12-13 08:36:30 +01:00
|
|
|
|
protected virtual bool RefreshLinkedChildren(IEnumerable<FileSystemMetadata> fileSystemChildren)
|
2013-07-05 15:47:10 +02:00
|
|
|
|
{
|
|
|
|
|
var currentManualLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Manual).ToList();
|
|
|
|
|
var currentShortcutLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Shortcut).ToList();
|
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
List<LinkedChild> newShortcutLinks;
|
|
|
|
|
|
|
|
|
|
if (SupportsShortcutChildren)
|
|
|
|
|
{
|
|
|
|
|
newShortcutLinks = fileSystemChildren
|
2016-10-25 21:02:04 +02:00
|
|
|
|
.Where(i => !i.IsDirectory && FileSystem.IsShortcut(i.FullName))
|
2015-08-15 03:44:30 +02:00
|
|
|
|
.Select(i =>
|
2013-07-05 15:47:10 +02:00
|
|
|
|
{
|
2015-08-15 03:44:30 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Found shortcut at {0}", i.FullName);
|
2013-09-08 19:55:37 +02:00
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
var resolvedPath = FileSystem.ResolveShortcut(i.FullName);
|
2013-09-08 19:55:37 +02:00
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(resolvedPath))
|
2013-09-08 19:55:37 +02:00
|
|
|
|
{
|
2015-08-15 03:44:30 +02:00
|
|
|
|
return new LinkedChild
|
|
|
|
|
{
|
|
|
|
|
Path = resolvedPath,
|
|
|
|
|
Type = LinkedChildType.Shortcut
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-09-08 19:55:37 +02:00
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
Logger.Error("Error resolving shortcut {0}", i.FullName);
|
2013-09-08 19:55:37 +02:00
|
|
|
|
|
2015-08-15 03:44:30 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error resolving shortcut {0}", ex, i.FullName);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Where(i => i != null)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else { newShortcutLinks = new List<LinkedChild>(); }
|
2013-07-05 15:47:10 +02:00
|
|
|
|
|
2013-10-03 17:24:32 +02:00
|
|
|
|
if (!newShortcutLinks.SequenceEqual(currentShortcutLinks, new LinkedChildComparer()))
|
2013-07-05 15:47:10 +02:00
|
|
|
|
{
|
2013-07-06 02:19:44 +02:00
|
|
|
|
Logger.Info("Shortcut links have changed for {0}", Path);
|
2013-08-04 02:59:23 +02:00
|
|
|
|
|
2013-07-05 15:47:10 +02:00
|
|
|
|
newShortcutLinks.AddRange(currentManualLinks);
|
|
|
|
|
LinkedChildren = newShortcutLinks;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-27 03:44:16 +01:00
|
|
|
|
foreach (var child in LinkedChildren)
|
|
|
|
|
{
|
|
|
|
|
// Reset the cached value
|
2015-03-05 03:49:08 +01:00
|
|
|
|
child.ItemId = null;
|
2014-02-27 03:44:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-05 15:47:10 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Folders need to validate and refresh
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public override async Task ChangedExternally()
|
|
|
|
|
{
|
2013-04-10 17:56:36 +02:00
|
|
|
|
var progress = new Progress<double>();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
await ValidateChildren(progress, CancellationToken.None).ConfigureAwait(false);
|
2014-02-06 23:22:03 +01:00
|
|
|
|
|
|
|
|
|
await base.ChangedExternally().ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-09-21 21:24:50 +02:00
|
|
|
|
/// Marks the played.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="user">The user.</param>
|
2013-09-21 21:24:50 +02:00
|
|
|
|
/// <param name="datePlayed">The date played.</param>
|
2014-10-25 20:32:58 +02:00
|
|
|
|
/// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <returns>Task.</returns>
|
2014-10-25 20:32:58 +02:00
|
|
|
|
public override async Task MarkPlayed(User user,
|
|
|
|
|
DateTime? datePlayed,
|
|
|
|
|
bool resetPosition)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-11-22 06:15:00 +01:00
|
|
|
|
var query = new InternalItemsQuery
|
2015-10-02 22:27:05 +02:00
|
|
|
|
{
|
|
|
|
|
User = user,
|
|
|
|
|
Recursive = true,
|
2015-11-06 16:02:22 +01:00
|
|
|
|
IsFolder = false,
|
2016-05-18 07:34:10 +02:00
|
|
|
|
EnableTotalRecordCount = false
|
2015-11-22 06:15:00 +01:00
|
|
|
|
};
|
|
|
|
|
|
2016-05-20 23:18:48 +02:00
|
|
|
|
if (!user.Configuration.DisplayMissingEpisodes || !user.Configuration.DisplayUnairedEpisodes)
|
2015-11-22 06:15:00 +01:00
|
|
|
|
{
|
2016-05-20 23:18:48 +02:00
|
|
|
|
query.ExcludeLocationTypes = new[] { LocationType.Virtual };
|
2015-11-22 06:15:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var itemsResult = await GetItems(query).ConfigureAwait(false);
|
2015-10-02 22:27:05 +02:00
|
|
|
|
|
2013-04-17 05:37:14 +02:00
|
|
|
|
// Sweep through recursively and update status
|
2015-10-02 22:27:05 +02:00
|
|
|
|
var tasks = itemsResult.Items.Select(c => c.MarkPlayed(user, datePlayed, resetPosition));
|
2013-09-21 21:24:50 +02:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Marks the unplayed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-10-25 20:32:58 +02:00
|
|
|
|
public override async Task MarkUnplayed(User user)
|
2013-09-21 21:24:50 +02:00
|
|
|
|
{
|
2015-10-02 22:28:44 +02:00
|
|
|
|
var itemsResult = await GetItems(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
User = user,
|
|
|
|
|
Recursive = true,
|
2016-05-18 07:34:10 +02:00
|
|
|
|
IsFolder = false,
|
|
|
|
|
EnableTotalRecordCount = false
|
2015-10-02 22:28:44 +02:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
2013-09-21 21:24:50 +02:00
|
|
|
|
// Sweep through recursively and update status
|
2015-10-02 22:28:44 +02:00
|
|
|
|
var tasks = itemsResult.Items.Select(c => c.MarkUnplayed(user));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-15 23:19:37 +01:00
|
|
|
|
public override bool IsPlayed(User user)
|
|
|
|
|
{
|
2016-05-07 04:11:22 +02:00
|
|
|
|
var itemsResult = GetItems(new InternalItemsQuery(user)
|
|
|
|
|
{
|
|
|
|
|
Recursive = true,
|
|
|
|
|
IsFolder = false,
|
2016-05-18 07:34:10 +02:00
|
|
|
|
ExcludeLocationTypes = new[] { LocationType.Virtual },
|
|
|
|
|
EnableTotalRecordCount = false
|
2016-05-07 04:11:22 +02:00
|
|
|
|
|
|
|
|
|
}).Result;
|
|
|
|
|
|
|
|
|
|
return itemsResult.Items
|
2014-01-17 19:23:00 +01:00
|
|
|
|
.All(i => i.IsPlayed(user));
|
2014-01-15 23:19:37 +01:00
|
|
|
|
}
|
2014-01-18 06:55:21 +01:00
|
|
|
|
|
|
|
|
|
public override bool IsUnplayed(User user)
|
|
|
|
|
{
|
2014-10-25 20:32:58 +02:00
|
|
|
|
return !IsPlayed(user);
|
2014-01-18 06:55:21 +01:00
|
|
|
|
}
|
2014-07-04 04:22:57 +02:00
|
|
|
|
|
2016-05-02 19:11:45 +02:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
public virtual bool SupportsUserDataFromChildren
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// These are just far too slow.
|
|
|
|
|
if (this is ICollectionFolder)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this is UserView)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this is UserRootFolder)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-19 08:18:29 +02:00
|
|
|
|
if (this is Channel)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (SourceType != SourceType.Library)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-21 09:54:53 +01:00
|
|
|
|
var iItemByName = this as IItemByName;
|
|
|
|
|
if (iItemByName != null)
|
|
|
|
|
{
|
|
|
|
|
var hasDualAccess = this as IHasDualAccess;
|
|
|
|
|
if (hasDualAccess == null || hasDualAccess.IsAccessedByName)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-02 19:11:45 +02:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 08:36:30 +01:00
|
|
|
|
public override async Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, List<ItemFields> itemFields)
|
2014-07-04 04:22:57 +02:00
|
|
|
|
{
|
2016-05-02 19:11:45 +02:00
|
|
|
|
if (!SupportsUserDataFromChildren)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-19 08:18:29 +02:00
|
|
|
|
if (itemDto != null)
|
|
|
|
|
{
|
2016-12-13 08:36:30 +01:00
|
|
|
|
if (itemFields.Contains(ItemFields.RecursiveItemCount))
|
|
|
|
|
{
|
|
|
|
|
itemDto.RecursiveItemCount = GetRecursiveChildCount(user);
|
|
|
|
|
}
|
2016-06-19 08:18:29 +02:00
|
|
|
|
}
|
2014-07-04 04:22:57 +02:00
|
|
|
|
|
2016-12-13 08:36:30 +01:00
|
|
|
|
if (SupportsPlayedStatus)
|
2014-07-04 04:22:57 +02:00
|
|
|
|
{
|
2016-12-13 08:36:30 +01:00
|
|
|
|
var unplayedQueryResult = await GetItems(new InternalItemsQuery(user)
|
2016-11-21 09:54:53 +01:00
|
|
|
|
{
|
|
|
|
|
Recursive = true,
|
|
|
|
|
IsFolder = false,
|
|
|
|
|
IsVirtualItem = false,
|
|
|
|
|
EnableTotalRecordCount = true,
|
|
|
|
|
Limit = 0,
|
|
|
|
|
IsPlayed = false
|
|
|
|
|
|
2016-12-13 08:36:30 +01:00
|
|
|
|
}).ConfigureAwait(false);
|
2016-11-21 09:54:53 +01:00
|
|
|
|
|
|
|
|
|
double unplayedCount = unplayedQueryResult.TotalRecordCount;
|
|
|
|
|
|
2016-06-15 20:56:37 +02:00
|
|
|
|
dto.UnplayedItemCount = unplayedQueryResult.TotalRecordCount;
|
2016-08-16 19:08:37 +02:00
|
|
|
|
|
2016-12-13 08:36:30 +01:00
|
|
|
|
if (itemDto != null && itemDto.RecursiveItemCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (itemDto.RecursiveItemCount.Value > 0)
|
|
|
|
|
{
|
|
|
|
|
var unplayedPercentage = (unplayedCount/itemDto.RecursiveItemCount.Value)*100;
|
|
|
|
|
dto.PlayedPercentage = 100 - unplayedPercentage;
|
|
|
|
|
dto.Played = dto.PlayedPercentage.Value >= 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-08-16 19:08:37 +02:00
|
|
|
|
{
|
2016-12-13 08:36:30 +01:00
|
|
|
|
dto.Played = (dto.UnplayedItemCount ?? 0) == 0;
|
2016-08-16 19:08:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-04 04:22:57 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2015-11-23 17:04:57 +01:00
|
|
|
|
}
|