jellyfin-server/MediaBrowser.Api/StartupWizardService.cs

240 lines
8.4 KiB
C#
Raw Normal View History

2014-11-15 02:31:03 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Connect;
using MediaBrowser.Controller.Library;
2015-08-20 20:54:00 +00:00
using MediaBrowser.Controller.LiveTv;
2014-11-15 02:31:03 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Configuration;
2015-08-20 20:54:00 +00:00
using MediaBrowser.Model.LiveTv;
2014-11-15 02:31:03 +00:00
using ServiceStack;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
[Route("/Startup/Complete", "POST", Summary = "Reports that the startup wizard has been completed")]
public class ReportStartupWizardComplete : IReturnVoid
{
}
[Route("/Startup/Info", "GET", Summary = "Gets initial server info")]
public class GetStartupInfo : IReturn<StartupInfo>
{
}
[Route("/Startup/Configuration", "GET", Summary = "Gets initial server configuration")]
public class GetStartupConfiguration : IReturn<StartupConfiguration>
{
}
[Route("/Startup/Configuration", "POST", Summary = "Updates initial server configuration")]
public class UpdateStartupConfiguration : StartupConfiguration, IReturnVoid
{
}
[Route("/Startup/User", "GET", Summary = "Gets initial user info")]
public class GetStartupUser : IReturn<StartupUser>
{
}
[Route("/Startup/User", "POST", Summary = "Updates initial user info")]
public class UpdateStartupUser : StartupUser, IReturn<UpdateStartupUserResult>
{
}
[Authenticated(AllowBeforeStartupWizard = true, Roles = "Admin")]
public class StartupWizardService : BaseApiService
{
private readonly IServerConfigurationManager _config;
private readonly IServerApplicationHost _appHost;
private readonly IUserManager _userManager;
private readonly IConnectManager _connectManager;
2015-08-22 16:55:23 +00:00
private readonly ILiveTvManager _liveTvManager;
2014-11-15 02:31:03 +00:00
2015-08-22 16:55:23 +00:00
public StartupWizardService(IServerConfigurationManager config, IServerApplicationHost appHost, IUserManager userManager, IConnectManager connectManager, ILiveTvManager liveTvManager)
2014-11-15 02:31:03 +00:00
{
_config = config;
_appHost = appHost;
_userManager = userManager;
_connectManager = connectManager;
2015-08-22 16:55:23 +00:00
_liveTvManager = liveTvManager;
2014-11-15 02:31:03 +00:00
}
public void Post(ReportStartupWizardComplete request)
{
_config.Configuration.IsStartupWizardCompleted = true;
2014-11-30 19:01:33 +00:00
_config.Configuration.EnableLocalizedGuids = true;
2015-04-24 02:15:29 +00:00
_config.Configuration.EnableCustomPathSubFolders = true;
2015-10-14 05:02:30 +00:00
_config.Configuration.EnableDateLastRefresh = true;
2016-03-14 01:34:24 +00:00
_config.Configuration.EnableStandaloneMusicKeys = true;
2014-11-15 02:31:03 +00:00
_config.SaveConfiguration();
}
public object Get(GetStartupInfo request)
{
var info = _appHost.GetSystemInfo();
return new StartupInfo
{
SupportsRunningAsService = info.SupportsRunningAsService
};
}
public object Get(GetStartupConfiguration request)
{
2015-08-20 20:57:29 +00:00
var result = new StartupConfiguration
2014-11-15 02:31:03 +00:00
{
UICulture = _config.Configuration.UICulture,
EnableInternetProviders = _config.Configuration.EnableInternetProviders,
SaveLocalMeta = _config.Configuration.SaveLocalMeta,
MetadataCountryCode = _config.Configuration.MetadataCountryCode,
PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage
};
2015-08-20 20:57:29 +00:00
var tvConfig = GetLiveTVConfiguration();
if (tvConfig.TunerHosts.Count > 0)
{
result.LiveTvTunerPath = tvConfig.TunerHosts[0].Url;
result.LiveTvTunerType = tvConfig.TunerHosts[0].Type;
}
2015-08-22 16:55:23 +00:00
if (tvConfig.ListingProviders.Count > 0)
{
result.LiveTvGuideProviderId = tvConfig.ListingProviders[0].Id;
result.LiveTvGuideProviderType = tvConfig.ListingProviders[0].Type;
}
2015-08-20 20:57:29 +00:00
return result;
2014-11-15 02:31:03 +00:00
}
public void Post(UpdateStartupConfiguration request)
{
_config.Configuration.UICulture = request.UICulture;
_config.Configuration.EnableInternetProviders = request.EnableInternetProviders;
_config.Configuration.SaveLocalMeta = request.SaveLocalMeta;
_config.Configuration.MetadataCountryCode = request.MetadataCountryCode;
_config.Configuration.PreferredMetadataLanguage = request.PreferredMetadataLanguage;
_config.SaveConfiguration();
2015-08-20 20:54:00 +00:00
var task = UpdateTuners(request);
Task.WaitAll(task);
2014-11-15 02:31:03 +00:00
}
public object Get(GetStartupUser request)
{
var user = _userManager.Users.First();
return new StartupUser
{
Name = user.Name,
ConnectUserName = user.ConnectUserName
};
}
public async Task<object> Post(UpdateStartupUser request)
{
var user = _userManager.Users.First();
// TODO: This should be handled internally by xbmc metadata
const string metadataKey = "xbmcmetadata";
var metadata = _config.GetConfiguration<XbmcMetadataOptions>(metadataKey);
metadata.UserId = user.Id.ToString("N");
_config.SaveConfiguration(metadataKey, metadata);
user.Name = request.Name;
await _userManager.UpdateUser(user).ConfigureAwait(false);
var result = new UpdateStartupUserResult();
if (!string.IsNullOrWhiteSpace(user.ConnectUserName) &&
string.IsNullOrWhiteSpace(request.ConnectUserName))
{
await _connectManager.RemoveConnect(user.Id.ToString("N")).ConfigureAwait(false);
}
else if (!string.Equals(user.ConnectUserName, request.ConnectUserName, StringComparison.OrdinalIgnoreCase))
{
result.UserLinkResult = await _connectManager.LinkUser(user.Id.ToString("N"), request.ConnectUserName).ConfigureAwait(false);
}
return result;
}
2015-08-20 20:54:00 +00:00
private async Task UpdateTuners(UpdateStartupConfiguration request)
{
var config = GetLiveTVConfiguration();
var save = false;
if (string.IsNullOrWhiteSpace(request.LiveTvTunerPath) ||
string.IsNullOrWhiteSpace(request.LiveTvTunerType))
{
if (config.TunerHosts.Count > 0)
{
config.TunerHosts.Clear();
save = true;
}
}
else
{
if (!config.TunerHosts.Any(i => string.Equals(i.Type, request.LiveTvTunerType, StringComparison.OrdinalIgnoreCase) && string.Equals(i.Url, request.LiveTvTunerPath, StringComparison.OrdinalIgnoreCase)))
{
// Add tuner
await _liveTvManager.SaveTunerHost(new TunerHostInfo
{
IsEnabled = true,
Type = request.LiveTvTunerType,
Url = request.LiveTvTunerPath
}).ConfigureAwait(false);
}
}
if (save)
{
SaveLiveTVConfiguration(config);
}
}
private void SaveLiveTVConfiguration(LiveTvOptions config)
{
_config.SaveConfiguration("livetv", config);
}
private LiveTvOptions GetLiveTVConfiguration()
{
return _config.GetConfiguration<LiveTvOptions>("livetv");
}
2014-11-15 02:31:03 +00:00
}
public class StartupConfiguration
{
public string UICulture { get; set; }
public bool EnableInternetProviders { get; set; }
public bool SaveLocalMeta { get; set; }
public string MetadataCountryCode { get; set; }
public string PreferredMetadataLanguage { get; set; }
2015-08-20 20:54:00 +00:00
public string LiveTvTunerType { get; set; }
public string LiveTvTunerPath { get; set; }
2015-08-22 16:55:23 +00:00
public string LiveTvGuideProviderId { get; set; }
public string LiveTvGuideProviderType { get; set; }
2014-11-15 02:31:03 +00:00
}
public class StartupInfo
{
public bool SupportsRunningAsService { get; set; }
}
public class StartupUser
{
public string Name { get; set; }
public string ConnectUserName { get; set; }
}
public class UpdateStartupUserResult
{
public UserLinkResult UserLinkResult { get; set; }
}
}