% UPDATEPREFS updates the Repository Manager depending on the preferences.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% description: updates the Repository Manager depending on the preferences.
%
% call:        rm = updatePrefs(rm, pl)
%
% version:     $Id: updatePrefs.m,v 1.6 2011/03/28 12:45:45 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function rm = updatePrefs(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    rm = getInfo(varargin{3});
    return
  end
    
  utils.helper.msg(utils.const.msg.PROC1, 'Update expiry login time.');  
  rm = varargin{1};

  % Get timer for login expiry password
  t = rm.timerClearPass;
  
  % Get pointer to the preferences
  prefs = getappdata(0, 'LTPDApreferences');
  expiryTime = double(prefs.getRepoPrefs.getExpiry());
  
  % Set the new expiry time to the repository manager
  if isa(t, 'timer')
    
    % Check if the user have changed the expire time
    if (t.Period ~= expiryTime)
    
      if strcmp(get(t,'Running'), 'on')
        stop(t);
      end
      set(t, 'Period', expiryTime);
      set(t, 'StartDelay', expiryTime);
      start(t);
      
    else
      % Do nothing if the user didn'T change the exire time
    end
      
  else
    t = LTPDARepositoryManager.startTimer('LTPDA_clearPassword', @LTPDARepositoryManager.cb_clearPassord, expiryTime);
    rm.timerClearPass = t;
  end
  
  % Set the host-names to the repository manager
  jhosts = prefs.getRepoPrefs.getHostnames;
  rm.manager.setRepoHosts(jhosts);
  
  % Resets the timers if a user make some changes to the preferences.
  conns = rm.manager.getConnections;
  for ii=1:conns.size
    conn = conns.get(ii-1);
    LTPDARepositoryManager.resetTimer(rm.timerClearPass, conn);
    LTPDARepositoryManager.resetTimer(rm.timerDisconnect, conn);
  end
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'LTPDARepositoryManager', 'ltpda', utils.const.categories.internal, '$Id: updatePrefs.m,v 1.6 2011/03/28 12:45:45 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  pl = plist();
end
