2014-10-17 04:52:41 +00:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var list = Servers.ToList();
|
|
|
|
|
|
|
|
|
|
var index = FindIndex(list, server.Id);
|
|
|
|
|
|
|
|
|
|
if (index != -1)
|
|
|
|
|
{
|
2014-10-18 19:02:54 +00:00
|
|
|
|
var existing = list[index];
|
|
|
|
|
|
|
|
|
|
// Merge the data
|
|
|
|
|
existing.DateLastAccessed = new[] { existing.DateLastAccessed, server.DateLastAccessed }.Max();
|
|
|
|
|
|
2014-10-30 01:17:31 +00:00
|
|
|
|
existing.UserLinkType = server.UserLinkType;
|
|
|
|
|
|
2014-10-18 19:02:54 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(server.AccessToken))
|
|
|
|
|
{
|
|
|
|
|
existing.AccessToken = server.AccessToken;
|
|
|
|
|
existing.UserId = server.UserId;
|
|
|
|
|
}
|
2014-10-18 21:25:04 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(server.ExchangeToken))
|
|
|
|
|
{
|
|
|
|
|
existing.ExchangeToken = server.ExchangeToken;
|
|
|
|
|
}
|
2014-10-18 19:02:54 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(server.RemoteAddress))
|
|
|
|
|
{
|
|
|
|
|
existing.RemoteAddress = server.RemoteAddress;
|
|
|
|
|
}
|
2014-11-14 06:27:10 +00:00
|
|
|
|
if (!existing.IsLocalAddressFixed && !string.IsNullOrEmpty(server.LocalAddress))
|
2014-10-18 19:02:54 +00:00
|
|
|
|
{
|
|
|
|
|
existing.LocalAddress = server.LocalAddress;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(server.Name))
|
|
|
|
|
{
|
|
|
|
|
existing.Name = server.Name;
|
|
|
|
|
}
|
|
|
|
|
if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
existing.WakeOnLanInfos = server.WakeOnLanInfos.ToList();
|
|
|
|
|
}
|
2014-11-14 06:27:10 +00:00
|
|
|
|
if (server.IsLocalAddressFixed)
|
|
|
|
|
{
|
|
|
|
|
existing.IsLocalAddressFixed = true;
|
|
|
|
|
}
|
2014-10-17 04:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
list.Add(server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Servers = list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int FindIndex(List<ServerInfo> servers, string id)
|
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var server in servers)
|
|
|
|
|
{
|
|
|
|
|
if (StringHelper.Equals(id, server.Id))
|
|
|
|
|
{
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|