ServerCredentials.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using MediaBrowser.Model.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace MediaBrowser.Model.ApiClient
  6. {
  7. public class ServerCredentials
  8. {
  9. public List<ServerInfo> Servers { get; set; }
  10. public string ConnectUserId { get; set; }
  11. public string ConnectAccessToken { get; set; }
  12. public ServerCredentials()
  13. {
  14. Servers = new List<ServerInfo>();
  15. }
  16. public void AddOrUpdateServer(ServerInfo server)
  17. {
  18. if (server == null)
  19. {
  20. throw new ArgumentNullException("server");
  21. }
  22. var list = Servers.ToList();
  23. var index = FindIndex(list, server.Id);
  24. if (index != -1)
  25. {
  26. var existing = list[index];
  27. // Merge the data
  28. existing.DateLastAccessed = new[] { existing.DateLastAccessed, server.DateLastAccessed }.Max();
  29. existing.UserLinkType = server.UserLinkType;
  30. if (!string.IsNullOrEmpty(server.AccessToken))
  31. {
  32. existing.AccessToken = server.AccessToken;
  33. existing.UserId = server.UserId;
  34. }
  35. if (!string.IsNullOrEmpty(server.ExchangeToken))
  36. {
  37. existing.ExchangeToken = server.ExchangeToken;
  38. }
  39. if (!string.IsNullOrEmpty(server.RemoteAddress))
  40. {
  41. existing.RemoteAddress = server.RemoteAddress;
  42. }
  43. if (!existing.IsLocalAddressFixed && !string.IsNullOrEmpty(server.LocalAddress))
  44. {
  45. existing.LocalAddress = server.LocalAddress;
  46. }
  47. if (!string.IsNullOrEmpty(server.Name))
  48. {
  49. existing.Name = server.Name;
  50. }
  51. if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
  52. {
  53. existing.WakeOnLanInfos = server.WakeOnLanInfos.ToList();
  54. }
  55. if (server.IsLocalAddressFixed)
  56. {
  57. existing.IsLocalAddressFixed = true;
  58. }
  59. }
  60. else
  61. {
  62. list.Add(server);
  63. }
  64. Servers = list;
  65. }
  66. private int FindIndex(List<ServerInfo> servers, string id)
  67. {
  68. var index = 0;
  69. foreach (var server in servers)
  70. {
  71. if (StringHelper.Equals(id, server.Id))
  72. {
  73. return index;
  74. }
  75. index++;
  76. }
  77. return -1;
  78. }
  79. }
  80. }