aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/BaseItem.cs
blob: 4c9008b22c7571b24e76507c42671ca72b06dcd2 (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
using MediaBrowser.Model.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.IO;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

namespace MediaBrowser.Controller.Entities
{
    public abstract class BaseItem : BaseEntity, IHasProviderIds
    {

        public IEnumerable<string> PhysicalLocations
        {
            get
            {
                return _resolveArgs.PhysicalLocations;
            }
        }

        public string SortName { get; set; }

        /// <summary>
        /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
        /// </summary>
        public DateTime? PremiereDate { get; set; }

        public string LogoImagePath { get; set; }

        public string ArtImagePath { get; set; }

        public string ThumbnailImagePath { get; set; }

        public string BannerImagePath { get; set; }

        public IEnumerable<string> BackdropImagePaths { get; set; }

        public string OfficialRating { get; set; }
        
        public string CustomRating { get; set; }
        public string CustomPin { get; set; }

        public string Language { get; set; }
        public string Overview { get; set; }
        public List<string> Taglines { get; set; }

        /// <summary>
        /// Using a Dictionary to prevent duplicates
        /// </summary>
        public Dictionary<string,PersonInfo> People { get; set; }

        public List<string> Studios { get; set; }

        public List<string> Genres { get; set; }

        public string DisplayMediaType { get; set; }

        public float? CommunityRating { get; set; }
        public long? RunTimeTicks { get; set; }

        public string AspectRatio { get; set; }
        public int? ProductionYear { get; set; }

        /// <summary>
        /// If the item is part of a series, this is it's number in the series.
        /// This could be episode number, album track number, etc.
        /// </summary>
        public int? IndexNumber { get; set; }

        /// <summary>
        /// For an episode this could be the season number, or for a song this could be the disc number.
        /// </summary>
        public int? ParentIndexNumber { get; set; }

        public IEnumerable<Video> LocalTrailers { get; set; }

        public string TrailerUrl { get; set; }

        public Dictionary<string, string> ProviderIds { get; set; }

        public Dictionary<Guid, UserItemData> UserData { get; set; }

        public UserItemData GetUserData(User user, bool createIfNull)
        {
            if (UserData == null || !UserData.ContainsKey(user.Id))
            {
                if (createIfNull)
                {
                    AddUserData(user, new UserItemData());
                }
                else
                {
                    return null;
                }
            }

            return UserData[user.Id];
        }

        private void AddUserData(User user, UserItemData data)
        {
            if (UserData == null)
            {
                UserData = new Dictionary<Guid, UserItemData>();
            }

            UserData[user.Id] = data;
        }

        /// <summary>
        /// Determines if a given user has access to this item
        /// </summary>
        internal bool IsParentalAllowed(User user)
        {
            return true;
        }

        /// <summary>
        /// Finds an item by ID, recursively
        /// </summary>
        public virtual BaseItem FindItemById(Guid id)
        {
            if (Id == id)
            {
                return this;
            }

            if (LocalTrailers != null)
            {
                return LocalTrailers.FirstOrDefault(i => i.Id == id);
            }

            return null;
        }

        public virtual bool IsFolder
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Determine if we have changed vs the passed in copy
        /// </summary>
        /// <param name="copy"></param>
        /// <returns></returns>
        public virtual bool IsChanged(BaseItem copy)
        {
            bool changed = copy.DateModified != this.DateModified;
            if (changed) MediaBrowser.Common.Logging.Logger.LogDebugInfo(this.Name + " changed - original creation: " + this.DateCreated + " new creation: " + copy.DateCreated + " original modified: " + this.DateModified + " new modified: " + copy.DateModified);
            return changed;
        }

        /// <summary>
        /// Determines if the item is considered new based on user settings
        /// </summary>
        public bool IsRecentlyAdded(User user)
        {
            return (DateTime.UtcNow - DateCreated).TotalDays < user.RecentItemDays;
        }

        public void AddPerson(PersonInfo person)
        {
            if (People == null)
            {
                People = new Dictionary<string, PersonInfo>(StringComparer.OrdinalIgnoreCase);
            }

            People[person.Name] = person;
        }

        /// <summary>
        /// Marks the item as either played or unplayed
        /// </summary>
        public virtual void SetPlayedStatus(User user, bool wasPlayed)
        {
            UserItemData data = GetUserData(user, true);

            if (wasPlayed)
            {
                data.PlayCount = Math.Max(data.PlayCount, 1);
            }
            else
            {
                data.PlayCount = 0;
                data.PlaybackPositionTicks = 0;
            }
        }

        /// <summary>
        /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed.
        /// </summary>
        /// <returns></returns>
        public virtual Task ChangedExternally()
        {
            return Task.Run(() => RefreshMetadata());
        }
    }
}