ServerCredentials.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 (!string.IsNullOrEmpty(server.LocalAddress))
  44. {
  45. existing.LocalAddress = server.LocalAddress;
  46. }
  47. if (!string.IsNullOrEmpty(server.ManualAddress))
  48. {
  49. existing.LocalAddress = server.ManualAddress;
  50. }
  51. if (!string.IsNullOrEmpty(server.Name))
  52. {
  53. existing.Name = server.Name;
  54. }
  55. if (server.WakeOnLanInfos != null && server.WakeOnLanInfos.Count > 0)
  56. {
  57. existing.WakeOnLanInfos = server.WakeOnLanInfos.ToList();
  58. }
  59. if (server.LastConnectionMode.HasValue)
  60. {
  61. existing.LastConnectionMode = server.LastConnectionMode;
  62. }
  63. foreach (ServerUserInfo user in server.Users)
  64. {
  65. existing.AddOrUpdate(user);
  66. }
  67. }
  68. else
  69. {
  70. list.Add(server);
  71. }
  72. Servers = list;
  73. }
  74. private int FindIndex(List<ServerInfo> servers, string id)
  75. {
  76. var index = 0;
  77. foreach (var server in servers)
  78. {
  79. if (StringHelper.EqualsIgnoreCase(id, server.Id))
  80. {
  81. return index;
  82. }
  83. index++;
  84. }
  85. return -1;
  86. }
  87. }
  88. }