function success = user_subscribe_dialog

% user_subscribe_dialog - get library to which to subscribe
% ---------------------------------------------------------
% result = user_subscribe_dialog
%
% output:
% -------
%  result - dialog group output

name = 'Add ...';

active_user = get_active_user;

control(1) = control_create( ...
	'style','separator', ...
	'type','header', ...
	'space', 0.75, ...
	'min',1, ...
	'string',['Library  (', active_user.name, ')'] ...
);

control(end + 1) = control_create( ...
	'style', 'edit', ...
	'name', 'filter_libraries', ...
	'alias', 'search' ...
);


%--
% get library name list
%--

names = library_name_list(get_users);

%--
% library listbox
%--

control(end + 1) = control_create( ...
	'style', 'listbox', ...
	'name', 'library', ...
	'string', names, ...
	'lines', 3, ...
	'min', 0, ...
	'max', 2, ...
	'value', 1, ...
	'space', 0.75 ...
);

%--
% add new library (+) button
%--

control(end + 1) = control_create( ...
	'style', 'buttongroup', ...
	'name', 'find_library', ...
	'alias','Find ...', ...
	'align', 'right', ...
	'lines', 1.75, ...
	'space', -0.25, ...
	'width', 1/3 ...
);

%----------------------------
% Library Info
%----------------------------

%--
% info box
%--

control(end + 1) = control_create( ...
	'name', 'library_info', ...
	'alias', 'info', ...
	'style', 'listbox', ...
	'space', 1.25, ... 
	'min', 0, ...
	'max', 2, ...
	'lines', 3 ...
);

%--
% create dialog group
%--

opt = dialog_group;	

opt.width = 14; opt.top = 1; opt.bottom = 0;

opt.header_color = get_extension_color('root');

opt.text_menu = 1;

result = dialog_group(name, control, opt, @user_subscribe_callback);

%---------------------------------
% SUBSCRIBE USER
%---------------------------------

success = 0;

% NOTE: return empty on cancel

if isempty(result.values)
	return;
end

%--
% get user
%--

user = get_active_user;

%--
% subscribe to all libraries
%--

for k = 1:length(result.values.library)
	
	%--
	% get library from value
	%--

	lib = get_user_library(result.values.library{k});
	
	if isempty(lib)
		continue;
	end
	
	%--
	% subscribe the user to the library
	%--
	
	user = user_subscribe(lib, user);

	success = 1;

end


%-----------------------------------------
% CALLBACK FUNCTION
%-----------------------------------------

function result = user_subscribe_callback(obj, eventdata, varargin) 

%--
% get callback context
%--

result = [];

[control,pal] = get_callback_context(obj); 

user = get_active_user;

%--
% select operation by control name
%--

switch (control.name)
	
	%--
	% filter libraries
	%--
	
	case ('filter_libraries')
		
		%--
		% get list of library names
		%--
		
		string = get_control(pal.handle, control.name, 'value');
		
		libnames = filter_strings(library_name_list(get_users),string);
		
		%--
		% handle no libraries found case
		%--
		
		if isempty(libnames)
			libnames = '(No Libraries Found)'; set_control(pal.handle, 'library', 'enable', 0);
		else
			set_control(pal.handle, 'library', 'enable', 1);
		end
		
		%--
		% set the list
		%--
		
		handles = get_control(pal.handle, 'library', 'handles');
		
		set(handles.uicontrol.listbox, ...
			'string', libnames, ...
			'value', 1 ...
		);
	
	%--
	% select library
	%--
	
	case ('library')
		
		%--
		% parse string to get user and library
		%--
		
		string = get_control(pal.handle, control.name, 'value');
		
		if length(string) > 1
			str = '(Multiple Libraries Selected)';
		else
		
			info = parse_tag(string, filesep, {'author', 'libname'});
		
			user = get_users('name', info.author{1}); lib = get_libraries(user, 'name', info.libname{1});
		
			str = library_info_str(lib);
			
		end
		
		%--
		% put info string in info box
		%--
		
		handles = get_control(pal.handle, 'library_info', 'handles');
		
		set(handles.uicontrol.listbox, 'string', str, 'value', []);
	
	case ('author')
			
		names = library_name_list(user);
		
		handles = get_control(pal.handle, 'library', 'handles');
				
		set(handles.uicontrol.listbox, ...
			'string', names, ...
			'value', 1 ...
		);
		
	case ('find_library')
		
		di = pwd; cd(user_root(get_active_user, 'lib'));
		
		[lib_file, lib_path] = uigetfile({'*.mat', 'MAT-files (*.mat)'},'Please select an existing library file.');
		
		cd(di);

		if isnumeric(lib_file) || ~is_library(lib_path, lib_file) 
			return;
		end
			
		lib = load_library([lib_path filesep lib_file]);
	
		handles = get_control(pal.handle, 'library', 'handles');
				
		names = library_name_list(get_active_user); 
		
		ix = find(strcmp(lib.name, names));
		
		if isempty(ix)
			names = {names{:} lib.name}; ix = numel(names); user_subscribe(lib,get_active_user);
		end
		
		set(handles.uicontrol.listbox, ...
			'string', names, ...
			'value', ix ...
		);
				
end


%-------------------------------------------
% IS_LIBRARY
%-------------------------------------------

function out = is_library(path, file)

out = 0;

fullfile = [path, file];

contents = load(fullfile, 'lib');

if ~isfield(contents, 'lib')
	return
end

lib = contents.lib;

if ~isequal(fieldnames(library_create), fieldnames(lib));
	return
end

out = 1;



