aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Modules/DefaultStudioModuleLoader.cs
blob: 9a8a6bc96fc4439180a5e6e9c266f2ca2cb62cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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.MachineStudio.Common;
using Tango.MachineStudio.Common.Authentication;
using Tango.MachineStudio.Common.Modules;
using Tango.MachineStudio.Stubs;

namespace Tango.MachineStudio.UI.Modules
{
    public class DefaultStudioModuleLoader : ExtendedObject, IStudioModuleLoader
    {
        private IAuthenticationProvider _authenticationProvider;
        private bool _loaded;

        public DefaultStudioModuleLoader(IAuthenticationProvider authenticationProvider)
        {
            _authenticationProvider = authenticationProvider;
            AllModules = new ObservableCollection<IStudioModule>();
            UserModules = new ObservableCollection<IStudioModule>();
            _authenticationProvider.CurrentUserChanged += _authenticationProvider_CurrentUserChanged;
        }

        private void _authenticationProvider_CurrentUserChanged(object sender, DAL.Observables.User e)
        {
            LoadModules();
        }

        private ObservableCollection<IStudioModule> _allModules;
        public ObservableCollection<IStudioModule> AllModules
        {
            get { return _allModules; }
            private set { _allModules = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<IStudioModule> _userModules;
        public ObservableCollection<IStudioModule> UserModules
        {
            get { return _userModules; }
            private set { _userModules = value; RaisePropertyChangedAuto(); }
        }

        public void LoadModules()
        {
            if (!_loaded)
            {
                //Preloaded
                AllModules.Add(new StubsModule());
                //Preloaded

                AllModules.Clear();
                string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                foreach (var file in Directory.GetFiles(assemblyFolder, "*.dll").Where(x => x.Contains("MachineStudio")))
                {
                    try
                    {
                        Assembly moduleAssembly = null;
                        moduleAssembly = Assembly.LoadFrom(file);

                        if (moduleAssembly != null)
                        {
                            foreach (var moduleType in moduleAssembly.GetTypes().Where(x => !x.IsInterface && typeof(IStudioModule).IsAssignableFrom(x)))
                            {
                                if (!AllModules.ToList().Exists(x => x.GetType() == moduleType))
                                {
                                    var module = Activator.CreateInstance(moduleType) as IStudioModule;
                                    AllModules.Add(module);
                                }
                            }
                        }
                    }
                    catch { }
                }

                _loaded = true; 
            }

            UserModules.Clear();

            if (_authenticationProvider.CurrentUser != null)
            {
                UserModules = AllModules.Where(x => _authenticationProvider.CurrentUser.HasPermission(x.Permission)).ToObservableCollection();
            }
        }
    }
}