Skip to content
Snippets Groups Projects
tsg_preferences.m 3.58 KiB
Newer Older
function tsg_preferences(hTsgGUI, app_name, DEFAULT_PATH_FILE)
% tsg_preferences(S1,S2) returns structure tsg saved in S1.mat file
% and with S2 internal version number.
% If S1.mat dosn't exist, call tsg_initialisation to create it in
% prefdir directory
%
% Input
% -----
% hTsgGUI    ............ Handle to the main user interface
% app_name   ............ application name
%
% Output
% ------
% none
%
% $Id$
%

global VERSION

% init empty tsg structure
% ------------------------
tsg = [];

% Construct config file path
% --------------------------
config_file = [prefdir, filesep, app_name, '.mat'];

% Open config file
% ----------------
Yves Gouriou's avatar
Yves Gouriou committed
% config = fopen( config_file, 'r' );
config = -1;
% test fopen return value
% -----------------------
if config == -1

  % ---------------------------------
  new_config;

else
  
  % Pointer set to watch during loading preference file 
  % ---------------------------------------------------
  set( hTsgGUI, 'Pointer', 'watch' );
  % Initialize loading preference file waitbar
  % ------------------------------------------
  wb = waitbar(0, 'Loading préference file. Please  wait...');
  
  % display waitbar
  % ---------------
  waitbar( 1/10, wb, 'loading structure root' );

  try

    load( config_file, 'root', 'tsg' );
      'Can''t load preference file ''%s''', config_file);

    % quit program
    % ------------

  end

  % display waitbar
  % ---------------
  waitbar( 1/2, wb, 'loading structure tsg' );

  % Close waitbar
  % -------------
  close(wb)
  
  % cursor back to normal
  % ---------------------
  set(hTsgGUI,'Pointer','arrow');

  try

    % set current preference autoload in submenu option/preference
    % ------------------------------------------------------------
    set(findobj('tag', 'TAG_UIMENU_OPTION_PREFERENCES', ...
    warning('tsgqc_GUI:tsg_preferences', ...
      ['Preference file ''%s''\nis malformed\n' ...
      'delete it.'], config_file);
    quitProgram(hTsgGUI, DEFAULT_PATH_FILE);

  end

  % If there no field version or wrong version, initialize
  % -------------------------------------------------------
  if ~isfield( root, 'version') || ~strcmp(root.version, VERSION)

    % call new config private function
    % --------------------------------
    new_config;

  end

end

% Save structure tsg
% ------------------
setappdata( hTsgGUI, 'tsg_data', tsg);

% save root in userdata property
% ------------------------------

% ----------------------------------------------------------------------
% ----------------------------------------------------------------------
  function new_config
 
    % call default tsg_initialisation
    % -------------------------------
    tsg = tsg_initialisation;
  
    % init root structure to default values
    % -------------------------------------
    root.preference.autoload = 'off';
    root.version             = VERSION;

    % save root and tsg to 'prefdir.mat' file
    % ---------------------------------------
    save(config_file, 'root', 'tsg');

    % save root in root object for next use
    % -------------------------------------
    set(0, 'userdata', root);

  end

end