ServerCredentials.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using MediaBrowser.Model.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace MediaBrowser.Model.ApiClient
  5. {
  6. public class ServerCredentials
  7. {
  8. public List<ServerInfo> Servers { get; set; }
  9. public string ConnectUserId { get; set; }
  10. public string ConnectAccessToken { get; set; }
  11. public ServerCredentials()
  12. {
  13. Servers = new List<ServerInfo>();
  14. }
  15. public void AddOrUpdateServer(ServerInfo server)
  16. {
  17. if (server == null)
  18. {
  19. throw new ArgumentNullException("server");
  20. }
  21. // Clone the existing list of servers
  22. var list = new List<ServerInfo>();
  23. foreach (ServerInfo serverInfo in Servers)
  24. {
  25. list.Add(serverInfo);
  26. }
  27. var index = FindIndex(list, server.Id);
  28. if (index != -1)
  29. {
  30. var existing = list[index];
  31. // Take the most recent DateLastAccessed
  32. if (server.DateLastAccessed > existing.DateLastAccessed)
  33. {
  34. existing.DateLastAccessed = server.DateLastAccessed;
  35. }
  36. existing.UserLinkType = server.UserLinkType;
  37. if (!string.IsNullOrEmpty(server.AccessToken))
  38. {
  39. existing.AccessToken = server.AccessToken;
  40. existing.UserId = server.UserId;
  41. }
  42. if (!string.IsNullOrEmpty(server.ExchangeToken))
  43. {
  44. existing.ExchangeToken = server.ExchangeToken;
  45. }
  46. if (!string.IsNullOrEmpty(server.RemoteAddress))
  47. {
  48. existing.RemoteAddress = server.RemoteAddress;
  49. }
  50. if (!string.IsNullOrEmpty(server.ConnectServerId))
  51. {
  52. existing.ConnectServerId = server.ConnectServerId;
  53. }
  54. if (!string.IsNullOrEmpty(server.LocalAddress))
  55. {
  56. existing.LocalAddress = server.LocalAddress;
  57. }
  58. if (!string.IsNullOrEmpty(server.ManualAddress))
  59. {
  60. existing.ManualAddress = server.ManualAddress;
  61. }
  62. if (!string.IsNullOrEmpty(server.Name))
  63. {
  64. existing.Name = server.Name;
  65. }
  66. if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
  67. {
  68. existing.WakeOnLanInfos = new List<WakeOnLanInfo>();
  69. foreach (WakeOnLanInfo info in server.WakeOnLanInfos)
  70. {
  71. existing.WakeOnLanInfos.Add(info);
  72. }
  73. }
  74. if (server.LastConnectionMode.HasValue)
  75. {
  76. existing.LastConnectionMode = server.LastConnectionMode;
  77. }
  78. foreach (ServerUserInfo user in server.Users)
  79. {
  80. existing.AddOrUpdate(user);
  81. }
  82. }
  83. else
  84. {
  85. list.Add(server);
  86. }
  87. Servers = list;
  88. }
  89. private int FindIndex(List<ServerInfo> servers, string id)
  90. {
  91. var index = 0;
  92. foreach (ServerInfo server in servers)
  93. {
  94. if (StringHelper.EqualsIgnoreCase(id, server.Id))
  95. {
  96. return index;
  97. }
  98. index++;
  99. }
  100. return -1;
  101. }
  102. public ServerInfo GetServer(string id)
  103. {
  104. foreach (ServerInfo server in Servers)
  105. {
  106. if (StringHelper.EqualsIgnoreCase(id, server.Id))
  107. {
  108. return server;
  109. }
  110. }
  111. return null;
  112. }
  113. }
  114. }