2014-10-27 14:45:50 -07:00
|
|
|
|
(function (globalScope, store, JSON) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (!globalScope.MediaBrowser) {
|
|
|
|
|
globalScope.MediaBrowser = {};
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
globalScope.MediaBrowser.CredentialProvider = function () {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
var credentials;
|
|
|
|
|
|
|
|
|
|
function ensure() {
|
|
|
|
|
|
|
|
|
|
credentials = credentials || JSON.parse(store.getItem('servercredentials') || '{}');
|
|
|
|
|
credentials.servers = credentials.servers || [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get() {
|
|
|
|
|
|
|
|
|
|
ensure();
|
|
|
|
|
return credentials;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function set(data) {
|
|
|
|
|
credentials = data;
|
|
|
|
|
store.setItem('servercredentials', JSON.stringify(get()));
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.clear = function () {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
credentials = null;
|
|
|
|
|
store.removeItem('servercredentials');
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.credentials = function (data) {
|
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
set(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return get();
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.addOrUpdateServer = function (list, server) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
var existing = list.filter(function (s) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
return s.Id == server.Id;
|
|
|
|
|
})[0];
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (existing) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
// Merge the data
|
|
|
|
|
existing.DateLastAccessed = Math.max(existing.DateLastAccessed || 0, server.DateLastAccessed || 0, new Date().getTime());
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.AccessToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.AccessToken = server.AccessToken;
|
|
|
|
|
existing.UserId = server.UserId;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.ExchangeToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.ExchangeToken = server.ExchangeToken;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.RemoteAddress) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.RemoteAddress = server.RemoteAddress;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.LocalAddress) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.LocalAddress = server.LocalAddress;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.Name) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.Name = server.Name;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.WakeOnLanInfos && server.WakeOnLanInfos.length) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.WakeOnLanInfos = server.WakeOnLanInfos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
list.push(server);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
})(window, store, window.JSON);
|