aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Dto/DtoOptions.cs
blob: 052626355f100c19db0d0cdcba8062dc0fa8edf3 (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
using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;

namespace MediaBrowser.Controller.Dto
{
    /// <summary>
    /// Options that control which fields and images are populated when building a <see cref="MediaBrowser.Model.Dto.BaseItemDto"/>.
    /// </summary>
    public class DtoOptions
    {
        private static readonly ItemFields[] DefaultExcludedFields =
        [
            ItemFields.SeasonUserData,
            ItemFields.RefreshState
        ];

        private static readonly ImageType[] AllImageTypes = Enum.GetValues<ImageType>();

        private static readonly ItemFields[] AllItemFields = Enum.GetValues<ItemFields>()
            .Except(DefaultExcludedFields)
            .ToArray();

        /// <summary>
        /// Initializes a new instance of the <see cref="DtoOptions"/> class with all fields enabled.
        /// </summary>
        public DtoOptions()
            : this(true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DtoOptions"/> class.
        /// </summary>
        /// <param name="allFields">Whether to populate all available fields.</param>
        public DtoOptions(bool allFields)
        {
            ImageTypeLimit = int.MaxValue;
            EnableImages = true;
            EnableUserData = true;
            AddCurrentProgram = true;

            Fields = allFields ? AllItemFields : [];
            ImageTypes = AllImageTypes;
        }

        /// <summary>
        /// Gets or sets the fields to populate on the DTO.
        /// </summary>
        public IReadOnlyList<ItemFields> Fields { get; set; }

        /// <summary>
        /// Gets or sets the image types to populate on the DTO.
        /// </summary>
        public IReadOnlyList<ImageType> ImageTypes { get; set; }

        /// <summary>
        /// Gets or sets the maximum number of images to return per image type.
        /// </summary>
        public int ImageTypeLimit { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether image information is populated.
        /// </summary>
        public bool EnableImages { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether program recording information is populated.
        /// </summary>
        public bool AddProgramRecordingInfo { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether user data is populated.
        /// </summary>
        public bool EnableUserData { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the currently airing program is populated.
        /// </summary>
        public bool AddCurrentProgram { get; set; }

        /// <summary>
        /// Gets a value indicating whether the specified field is populated.
        /// </summary>
        /// <param name="field">The field to check.</param>
        /// <returns><c>true</c> if the field is populated; otherwise, <c>false</c>.</returns>
        public bool ContainsField(ItemFields field)
            => Fields.Contains(field);

        /// <summary>
        /// Gets the number of images to return for the specified image type.
        /// </summary>
        /// <param name="type">The image type.</param>
        /// <returns>The image limit for the type, or 0 if the type is not enabled.</returns>
        public int GetImageLimit(ImageType type)
        {
            if (EnableImages && ImageTypes.Contains(type))
            {
                return ImageTypeLimit;
            }

            return 0;
        }
    }
}