You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Model/ApiClient/ServerCredentials.cs

132 lines
3.9 KiB

using MediaBrowser.Model.Extensions;
using System;
using System.Collections.Generic;
namespace MediaBrowser.Model.ApiClient
{
public class ServerCredentials
{
public List<ServerInfo> Servers { get; set; }
public string ConnectUserId { get; set; }
public string ConnectAccessToken { get; set; }
public ServerCredentials()
{
Servers = new List<ServerInfo>();
}
public void AddOrUpdateServer(ServerInfo server)
{
if (server == null)
{
throw new ArgumentNullException("server");
}
// Clone the existing list of servers
var list = new List<ServerInfo>();
foreach (ServerInfo serverInfo in Servers)
{
list.Add(serverInfo);
}
var index = FindIndex(list, server.Id);
if (index != -1)
{
var existing = list[index];
// Take the most recent DateLastAccessed
if (server.DateLastAccessed > existing.DateLastAccessed)
{
existing.DateLastAccessed = server.DateLastAccessed;
}
existing.UserLinkType = server.UserLinkType;
if (!string.IsNullOrEmpty(server.AccessToken))
{
existing.AccessToken = server.AccessToken;
existing.UserId = server.UserId;
}
10 years ago
if (!string.IsNullOrEmpty(server.ExchangeToken))
{
existing.ExchangeToken = server.ExchangeToken;
}
if (!string.IsNullOrEmpty(server.RemoteAddress))
{
existing.RemoteAddress = server.RemoteAddress;
}
if (!string.IsNullOrEmpty(server.ConnectServerId))
{
existing.ConnectServerId = server.ConnectServerId;
}
10 years ago
if (!string.IsNullOrEmpty(server.LocalAddress))
{
existing.LocalAddress = server.LocalAddress;
}
10 years ago
if (!string.IsNullOrEmpty(server.ManualAddress))
{
existing.ManualAddress = server.ManualAddress;
10 years ago
}
if (!string.IsNullOrEmpty(server.Name))
{
existing.Name = server.Name;
}
if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
{
existing.WakeOnLanInfos = new List<WakeOnLanInfo>();
foreach (WakeOnLanInfo info in server.WakeOnLanInfos)
{
existing.WakeOnLanInfos.Add(info);
}
}
10 years ago
if (server.LastConnectionMode.HasValue)
{
10 years ago
existing.LastConnectionMode = server.LastConnectionMode;
}
10 years ago
foreach (ServerUserInfo user in server.Users)
{
existing.AddOrUpdate(user);
}
}
else
{
list.Add(server);
}
Servers = list;
}
private int FindIndex(List<ServerInfo> servers, string id)
{
var index = 0;
10 years ago
foreach (ServerInfo server in servers)
{
if (StringHelper.EqualsIgnoreCase(id, server.Id))
{
return index;
}
index++;
}
return -1;
}
10 years ago
public ServerInfo GetServer(string id)
{
foreach (ServerInfo server in Servers)
{
if (StringHelper.EqualsIgnoreCase(id, server.Id))
{
return server;
}
}
return null;
}
}
}