using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.BL.Entities; using Tango.Logging; using Tango.FSE.Common.Modules; using Tango.FSE.Common; using System.Windows.Data; using Tango.Core.DI; using Tango.FSE.Common.Authentication; using System.Windows; namespace Tango.FSE.UI.Modules { /// /// Represents the default PPC . /// /// /// public class DefaultFSEModuleLoader : ExtendedObject, IFSEModuleLoader { private static object _syncObject = new object(); private IAuthenticationProvider _authenticationProvider; private bool _loaded; /// /// Occurs when the user has logged in and user modules are loaded. /// public event EventHandler ModulesLoaded; /// /// Initializes a new instance of the class. /// /// The authentication provider. public DefaultFSEModuleLoader(IAuthenticationProvider authenticationProvider) { _authenticationProvider = authenticationProvider; AllModules = new ObservableCollection(); UserModules = new ObservableCollection(); BindingOperations.EnableCollectionSynchronization(UserModules, _syncObject); _authenticationProvider.CurrentUserChanged += _authenticationProvider_CurrentUserChanged; } /// /// Handles the authentication provider user changed event. /// /// The sender. /// The e. private void _authenticationProvider_CurrentUserChanged(object sender, User user) { if (user != null) { LoadModules(); } else { UnloadModules(); } } private ObservableCollection _allModules; /// /// Gets all loaded modules. /// public ObservableCollection AllModules { get { return _allModules; } private set { _allModules = value; RaisePropertyChangedAuto(); } } private ObservableCollection _userModules; /// /// Gets all the user permitted modules. /// public ObservableCollection UserModules { get { return _userModules; } private set { _userModules = value; RaisePropertyChangedAuto(); } } /// /// Loads all available PPC modules. /// public void LoadModules() { if (!_loaded) { AllModules.Clear(); string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); foreach (var file in Directory.GetFiles(assemblyFolder, "*.dll").Where(x => x.Contains("Tango.FSE"))) { try { Assembly moduleAssembly = null; moduleAssembly = Assembly.LoadFrom(file); if (moduleAssembly != null) { foreach (var moduleType in moduleAssembly.GetLoadableTypes().Where(x => !x.IsInterface && typeof(IFSEModule).IsAssignableFrom(x) && !x.IsAbstract)) { if (!AllModules.ToList().Exists(x => x.GetType() == moduleType)) { try { LogManager.Log(String.Format("Loading module '{0}'...", moduleType.Name)); var module = Activator.CreateInstance(moduleType) as IFSEModule; AllModules.Add(module); } catch (Exception ex) { LogManager.Log(ex, "Could not load module " + moduleType.Name); } } } try { var assemblyName = moduleAssembly.GetName().Name; if (assemblyName != "Tango.FSE.Common") { LogManager.Log("Loading module resource generic theme dictionary..."); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri($"/{assemblyName};component/Themes/Generic.xaml", UriKind.Relative) }); } } catch { } } } catch { } } _loaded = true; } AllModules = AllModules.OrderBy(x => x.GetType().GetCustomAttribute().Index).ToObservableCollection(); UserModules.Clear(); if (_authenticationProvider.CurrentUser != null) { UserModules = AllModules.Where(x => _authenticationProvider.CurrentUser.HasAnyPermission(x.Permission)).ToObservableCollection(); } ModulesLoaded?.Invoke(this, new EventArgs()); } private void UnloadModules() { UserModules.Clear(); } /// /// Gets the PPC module of type T if loaded. /// /// /// public T GetFSEModule() where T : IFSEModule { return UserModules.OfType().FirstOrDefault(); } } }