ServerCredentials.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.LocalAddress))
  51. {
  52. existing.LocalAddress = server.LocalAddress;
  53. }
  54. if (!string.IsNullOrEmpty(server.ManualAddress))
  55. {
  56. existing.LocalAddress = server.ManualAddress;
  57. }
  58. if (!string.IsNullOrEmpty(server.Name))
  59. {
  60. existing.Name = server.Name;
  61. }
  62. if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
  63. {
  64. existing.WakeOnLanInfos = new List<WakeOnLanInfo>();
  65. foreach (WakeOnLanInfo info in server.WakeOnLanInfos)
  66. {
  67. existing.WakeOnLanInfos.Add(info);
  68. }
  69. }
  70. if (server.LastConnectionMode.HasValue)
  71. {
  72. existing.LastConnectionMode = server.LastConnectionMode;
  73. }
  74. foreach (ServerUserInfo user in server.Users)
  75. {
  76. existing.AddOrUpdate(user);
  77. }
  78. }
  79. else
  80. {
  81. list.Add(server);
  82. }
  83. Servers = list;
  84. }
  85. private int FindIndex(List<ServerInfo> servers, string id)
  86. {
  87. var index = 0;
  88. foreach (var server in servers)
  89. {
  90. if (StringHelper.EqualsIgnoreCase(id, server.Id))
  91. {
  92. return index;
  93. }
  94. index++;
  95. }
  96. return -1;
  97. }
  98. }
  99. }