% A function to properly clear MATLAB memory for LTPDA.
%
% M Hewitson 06-08-08
%
% $Id: mc.m,v 1.11 2011/02/03 13:42:43 ingo Exp $
%

% Remove this object from memory so we can clear classes
% prefs = getappdata(0, 'LTPDApreferences');
% prefs.misc.default_window = '';

% TODO: We need to close all GUIs before doing this - Prefs GUI and Repo Gui

function mc()
  
  % remove preferences
  try
    rmappdata(0, 'LTPDApreferences');
  end
  
  % database connection manager
  cm = getappdata(0, 'LTPDADatabaseConnenctionManager');
  if ~isempty(cm)
    % save cached credentials
    setappdata(0, 'LTPDAcredentials', cm.credentials);
    % remove database connection manager
    rmappdata(0, 'LTPDADatabaseConnenctionManager');
  end
  
  evalin('caller', 'clear');         % delete the timer-objects
  clear classes                      % delete the local variables
  evalin('caller', 'clear classes'); % delete the variables in the caller function
  evalin('base', 'clear classes');   % delete the variables in the 'base' workspace
  
  % load the preferences again
  LTPDAprefs.loadPrefs();
  
  % instantiate database connection manager if required
  credentials = getappdata(0, 'LTPDAcredentials');
  if ~isempty(credentials)
    cm = LTPDAConnectionManager();
    cm.credentials = credentials;
  end
  try
    rmappdata(0, 'LTPDAcredentials');
  end
  
end
