% LTPDARepositoryManager creates the LTPDA Repository Manager.
%
% CALL:
%         >> LTPDARepositoryManager
%
% M Hewitson 08-01-10
%
% $Id: LTPDARepositoryManager.m,v 1.14 2011/02/03 13:46:23 ingo Exp $
%


classdef LTPDARepositoryManager < handle
  %------------------------------------------------
  %---------- Private read-only Properties --------
  %------------------------------------------------
  properties (SetAccess = private, GetAccess = public)
    manager         = [];
    gui             = [];
    selector        = [];
    timerDisconnect = [];
    timerClearPass  = [];
    h1;
    h2;
    h3;
    h4;
    h5;
    h6;
    h7;
    h8;
    h9;
  end
  
  properties (Constant = true, GetAccess = private)
    DISCONNECT = 60;
  end
  
  %-----------------------------------------
  %  Public methods
  %-----------------------------------------
  methods
    
    %-----------------------------------------
    %  Constructor
    %-----------------------------------------
    function wb = LTPDARepositoryManager(varargin)
      
      twb = getappdata(0, 'LTPDARepositoryManager');
      
      if isa(twb, mfilename)
        
        wb = twb;
        
      else
        
        switch nargin
          case 0
            % Create a new repository manager
            wb.manager = mpipeline.repository.RepositoryManager();
            % Create timer which disconnects the open connections
            wb.timerDisconnect = LTPDARepositoryManager.startTimer('LTPDA_disconnectConnections', @LTPDARepositoryManager.cb_timerDisconnect, wb.DISCONNECT);
            % Create timer which clears the password
            prefs = getappdata(0, 'LTPDApreferences');
            expiry = double(prefs.getRepoPrefs.getExpiry);
            wb.timerClearPass = LTPDARepositoryManager.startTimer('LTPDA_clearPassword', @LTPDARepositoryManager.cb_timerClearPassord, expiry);
            pause(0.1);
        end
        
        % Store the manager in the application workspace
        setappdata(0, 'LTPDARepositoryManager', wb);
        
        wb.updatePrefs();
        
      end
    end % End constructor
    
    %-----------------------------------------
    %  Destructor
    %-----------------------------------------
    function delete(h)
      disp('*** Deleting repository manager')
      if ~isempty(h)
        disp('  - closing repository connections')
        h.manager.closeAllConnections();
        h.deleteTimer('timerClearPass');
        h.deleteTimer('timerDisconnect');
        % close gui
        if ~isempty(h.gui)
          h.gui.dispose();
        end
      end
    end
    
    
    function initGUI(wb)
      
      % Create a new GUI
      wb.gui     = mpipeline.repository.RepositoryManagerGUI([], wb.manager);
      % Create new selector
      wb.selector = mpipeline.repository.ConnectionSelector([], wb.manager);
      
      %--- called when window is closed
      wb.h1 = handle(wb.gui, 'callbackproperties');
      wb.h1.WindowClosedCallback = @LTPDARepositoryManager.cb_guiClosed;
      
      
    end
    
  end % End public methods
  
  methods
    varargout = display(varargin)
    varargout = updatePrefs(varargin)
    varargout = restartTimers(varargin)
    varargout = showGui(varargin)
    varargout = listConnections(varargin)
    
    varargout = deleteTimer(varargin)
  end
  
  methods (Static=true)
    
    % Callback methods
    varargout = cb_timerDisconnect(varargin)
    varargout = cb_timerClearPassord(varargin)
    varargout = cb_newConnection(varargin)
    varargout = cb_guiClosed(varargin)
    
    % Timer methods
    varargout = startTimer(varargin)
    varargout = resetTimer(varargin)
    
    % Helper Methods
    varargout = getSinfo(varargin)
    varargout = executeQuery(varargin)
    varargout = copyObjects(varargin)
    
    function ii = getInfo(varargin)
      ii = utils.helper.generic_getInfo(varargin{:}, 'LTPDARepositoryManager');
    end
    
    % Return the plist for a particular parameter set
    function out = getDefaultPlist(set)
      out = plist();
    end
    
    function out = VEROUT()
      out = '$Id: LTPDARepositoryManager.m,v 1.14 2011/02/03 13:46:23 ingo Exp $';
    end
    
    function out = SETS()
      out = {'Default'};
    end
    
    function obj = initObjectWithSize(n,m)
      obj = LTPDARepositoryManager.newarray([n m]);
    end
    
  end
  %------------------------------------------------
  % Private static methods
  %------------------------------------------------
  methods(Access=private, Static=true)
  end
  
end

