aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/Controller/UIKernel.cs
blob: ca24b7852a7f76c76c96f6255cfbd258caec2fe9 (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
using MediaBrowser.ApiInteraction;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Progress;
using MediaBrowser.UI.Configuration;
using System;
using System.Threading.Tasks;

namespace MediaBrowser.UI.Controller
{
    /// <summary>
    /// This controls application logic as well as server interaction within the UI.
    /// </summary>
    public class UIKernel : BaseKernel<UIApplicationConfiguration, UIApplicationPaths>
    {
        public static UIKernel Instance { get; private set; }

        public ApiClient ApiClient { get; private set; }
        public DtoUser CurrentUser { get; set; }
        public ServerConfiguration ServerConfiguration { get; set; }

        public UIKernel()
            : base()
        {
            Instance = this;
        }

        public override KernelContext KernelContext
        {
            get { return KernelContext.Ui; }
        }

        /// <summary>
        /// Give the UI a different url prefix so that they can share the same port, in case they are installed on the same machine.
        /// </summary>
        protected override string HttpServerUrlPrefix
        {
            get
            {
                return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/ui/";
            }
        }

        /// <summary>
        /// Performs initializations that can be reloaded at anytime
        /// </summary>
        protected override async Task ReloadInternal(IProgress<TaskProgress> progress)
        {
            ReloadApiClient();

            await new PluginUpdater().UpdatePlugins().ConfigureAwait(false);

            await base.ReloadInternal(progress).ConfigureAwait(false);
        }

        /// <summary>
        /// Updates and installs new plugin assemblies and configurations from the server
        /// </summary>
        protected async Task<PluginUpdateResult> UpdatePlugins()
        {
            return await new PluginUpdater().UpdatePlugins().ConfigureAwait(false);
        }

        /// <summary>
        /// Disposes the current ApiClient and creates a new one
        /// </summary>
        private void ReloadApiClient()
        {
            DisposeApiClient();

            ApiClient = new ApiClient
            {
                ServerHostName = Configuration.ServerHostName,
                ServerApiPort = Configuration.ServerApiPort
            };
        }

        /// <summary>
        /// Disposes the current ApiClient
        /// </summary>
        private void DisposeApiClient()
        {
            if (ApiClient != null)
            {
                ApiClient.Dispose();
            }
        }

        public override void Dispose()
        {
            base.Dispose();

            DisposeApiClient();
        }
    }
}