aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Plugins.Trailers/Plugin.cs
blob: 2fc2773d3816bfcdd5c71d2c3a19453aae5bda64 (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
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.ScheduledTasks;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Plugins.Trailers.Configuration;
using MediaBrowser.Plugins.Trailers.ScheduledTasks;
using System;
using System.ComponentModel.Composition;
using System.IO;

namespace MediaBrowser.Plugins.Trailers
{
    /// <summary>
    /// Class Plugin
    /// </summary>
    [Export(typeof(IPlugin))]
    public class Plugin : BasePlugin<PluginConfiguration>
    {
        /// <summary>
        /// Gets the name of the plugin
        /// </summary>
        /// <value>The name.</value>
        public override string Name
        {
            get { return "Trailers"; }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public override string Description
        {
            get
            {
                return "Movie trailers for your collection.";
            }
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static Plugin Instance { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Plugin" /> class.
        /// </summary>
        public Plugin()
            : base()
        {
            Instance = this;
        }

        /// <summary>
        /// The _download path
        /// </summary>
        private string _downloadPath;
        /// <summary>
        /// Gets the path to the trailer download directory
        /// </summary>
        /// <value>The download path.</value>
        public string DownloadPath
        {
            get
            {
                if (_downloadPath == null)
                {
                    // Use 
                    _downloadPath = Configuration.DownloadPath;

                    if (string.IsNullOrWhiteSpace(_downloadPath))
                    {
                        _downloadPath = Path.Combine(Controller.Kernel.Instance.ApplicationPaths.DataPath, Name);
                    }

                    if (!Directory.Exists(_downloadPath))
                    {
                        Directory.CreateDirectory(_downloadPath);
                    }
                }
                return _downloadPath;
            }
        }

        /// <summary>
        /// Starts the plugin on the server
        /// </summary>
        /// <param name="isFirstRun">if set to <c>true</c> [is first run].</param>
        protected override void InitializeOnServer(bool isFirstRun)
        {
            base.InitializeOnServer(isFirstRun);

            if (isFirstRun)
            {
                Kernel.TaskManager.QueueScheduledTask<CurrentTrailerDownloadTask>();
            }
        }

        /// <summary>
        /// Completely overwrites the current configuration with a new copy
        /// Returns true or false indicating success or failure
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        public override void UpdateConfiguration(BasePluginConfiguration configuration)
        {
            var config = (PluginConfiguration) configuration;

            var pathChanged = !string.Equals(Configuration.DownloadPath, config.DownloadPath, StringComparison.OrdinalIgnoreCase);

            base.UpdateConfiguration(configuration);

            if (pathChanged)
            {
                _downloadPath = null;
                Kernel.TaskManager.QueueScheduledTask<RefreshMediaLibraryTask>();
            }
        }
    }
}