2014-10-17 06:52:41 +02:00
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 21:55:31 +01:00
|
|
|
|
// Clone the existing list of servers
|
|
|
|
|
var list = new List<ServerInfo>();
|
|
|
|
|
foreach (ServerInfo serverInfo in Servers)
|
|
|
|
|
{
|
|
|
|
|
list.Add(serverInfo);
|
|
|
|
|
}
|
2014-10-17 06:52:41 +02:00
|
|
|
|
|
|
|
|
|
var index = FindIndex(list, server.Id);
|
|
|
|
|
|
|
|
|
|
if (index != -1)
|
|
|
|
|
{
|
2014-10-18 21:02:54 +02:00
|
|
|
|
var existing = list[index];
|
|
|
|
|
|
2015-03-27 21:55:31 +01:00
|
|
|
|
// Take the most recent DateLastAccessed
|
|
|
|
|
if (server.DateLastAccessed > existing.DateLastAccessed)
|
|
|
|
|
{
|
|
|
|
|
existing.DateLastAccessed = server.DateLastAccessed;
|
|
|
|
|
}
|
2015-11-19 06:19:54 +01:00
|
|
|
|
|
2014-10-30 02:17:31 +01:00
|
|
|
|
existing.UserLinkType = server.UserLinkType;
|
|
|
|
|
|
2014-10-18 21:02:54 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(server.AccessToken))
|
|
|
|
|
{
|
|
|
|
|
existing.AccessToken = server.AccessToken;
|
|
|
|
|
existing.UserId = server.UserId;
|
|
|
|
|
}
|
2014-10-18 23:25:04 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(server.ExchangeToken))
|
|
|
|
|
{
|
|
|
|
|
existing.ExchangeToken = server.ExchangeToken;
|
|
|
|
|
}
|
2014-10-18 21:02:54 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(server.RemoteAddress))
|
|
|
|
|
{
|
|
|
|
|
existing.RemoteAddress = server.RemoteAddress;
|
|
|
|
|
}
|
2014-12-03 04:13:03 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(server.LocalAddress))
|
2014-10-18 21:02:54 +02:00
|
|
|
|
{
|
|
|
|
|
existing.LocalAddress = server.LocalAddress;
|
|
|
|
|
}
|
2014-12-03 04:13:03 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(server.ManualAddress))
|
|
|
|
|
{
|
|
|
|
|
existing.LocalAddress = server.ManualAddress;
|
|
|
|
|
}
|
2014-10-18 21:02:54 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(server.Name))
|
|
|
|
|
{
|
|
|
|
|
existing.Name = server.Name;
|
|
|
|
|
}
|
|
|
|
|
if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
|
|
|
|
|
{
|
2015-03-27 21:55:31 +01:00
|
|
|
|
existing.WakeOnLanInfos = new List<WakeOnLanInfo>();
|
|
|
|
|
foreach (WakeOnLanInfo info in server.WakeOnLanInfos)
|
|
|
|
|
{
|
|
|
|
|
existing.WakeOnLanInfos.Add(info);
|
|
|
|
|
}
|
2014-10-18 21:02:54 +02:00
|
|
|
|
}
|
2014-12-03 04:13:03 +01:00
|
|
|
|
if (server.LastConnectionMode.HasValue)
|
2014-11-14 07:27:10 +01:00
|
|
|
|
{
|
2014-12-03 04:13:03 +01:00
|
|
|
|
existing.LastConnectionMode = server.LastConnectionMode;
|
2014-11-14 07:27:10 +01:00
|
|
|
|
}
|
2015-01-30 06:18:32 +01:00
|
|
|
|
foreach (ServerUserInfo user in server.Users)
|
|
|
|
|
{
|
|
|
|
|
existing.AddOrUpdate(user);
|
|
|
|
|
}
|
2014-10-17 06:52:41 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
list.Add(server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Servers = list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int FindIndex(List<ServerInfo> servers, string id)
|
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
|
|
|
|
|
2015-04-19 02:05:36 +02:00
|
|
|
|
foreach (ServerInfo server in servers)
|
2014-10-17 06:52:41 +02:00
|
|
|
|
{
|
2015-02-06 16:01:17 +01:00
|
|
|
|
if (StringHelper.EqualsIgnoreCase(id, server.Id))
|
2014-10-17 06:52:41 +02:00
|
|
|
|
{
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-04-19 02:05:36 +02:00
|
|
|
|
|
|
|
|
|
public ServerInfo GetServer(string id)
|
|
|
|
|
{
|
|
|
|
|
foreach (ServerInfo server in Servers)
|
|
|
|
|
{
|
|
|
|
|
if (StringHelper.EqualsIgnoreCase(id, server.Id))
|
|
|
|
|
{
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-10-17 06:52:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|