forked from MediaBrowser/Emby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntryPoint.cs
103 lines (89 loc) · 3.46 KB
/
EntryPoint.cs
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
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.XbmcMetadata.Configuration;
using MediaBrowser.XbmcMetadata.Savers;
using System;
namespace MediaBrowser.XbmcMetadata
{
public class EntryPoint : IServerEntryPoint
{
private readonly IUserDataManager _userDataManager;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly IProviderManager _providerManager;
private readonly IConfigurationManager _config;
public EntryPoint(IUserDataManager userDataManager, ILibraryManager libraryManager, ILogger logger, IProviderManager providerManager, IConfigurationManager config)
{
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_logger = logger;
_providerManager = providerManager;
_config = config;
}
public void Run()
{
_userDataManager.UserDataSaved += _userDataManager_UserDataSaved;
_libraryManager.ItemUpdated += _libraryManager_ItemUpdated;
}
void _libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e)
{
if (e.UpdateReason == ItemUpdateType.ImageUpdate)
{
var person = e.Item as Person;
if (person != null)
{
var config = _config.GetNfoConfiguration();
if (!config.SaveImagePathsInNfo)
{
return;
}
var items = _libraryManager.GetItems(new InternalItemsQuery
{
Person = person.Name
}).Items;
foreach (var item in items)
{
SaveMetadataForItem(item, ItemUpdateType.MetadataEdit);
}
}
}
}
void _userDataManager_UserDataSaved(object sender, UserDataSaveEventArgs e)
{
if (e.SaveReason == UserDataSaveReason.PlaybackFinished || e.SaveReason == UserDataSaveReason.TogglePlayed || e.SaveReason == UserDataSaveReason.UpdateUserRating)
{
var item = e.Item as BaseItem;
if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
{
SaveMetadataForItem(item, ItemUpdateType.MetadataEdit);
}
}
}
public void Dispose()
{
_userDataManager.UserDataSaved -= _userDataManager_UserDataSaved;
}
private async void SaveMetadataForItem(BaseItem item, ItemUpdateType updateReason)
{
var locationType = item.LocationType;
if (locationType == LocationType.Remote ||
locationType == LocationType.Virtual)
{
return;
}
try
{
await _providerManager.SaveMetadata(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error saving metadata for {0}", ex, item.Path ?? item.Name);
}
}
}
}