blob: d0f6484abb540b576b9331194faa01f1af0bd26d (
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
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.PPC.Common.Authentication;
using Tango.PPC.Common.Modules;
using Tango.PPC.Common;
using Tango.PPC.Jobs;
using System.Windows.Data;
using Tango.Core.DI;
using Tango.Settings;
namespace Tango.PPC.UI.Modules
{
/// <summary>
/// Represents the default PPC <see cref="IPPCModuleLoader"></see>.
/// </summary>
/// <seealso cref="Tango.Core.ExtendedObject" />
/// <seealso cref="Tango.PPC.Common.Modules.IPPCModuleLoader" />
public class DefaultPPCModuleLoader : ExtendedObject, IPPCModuleLoader
{
private static object _syncObject = new object();
private IAuthenticationProvider _authenticationProvider;
private bool _loaded;
/// <summary>
/// Occurs when the user has logged in and user modules are loaded.
/// </summary>
public event EventHandler ModulesLoaded;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultPPCModuleLoader"/> class.
/// </summary>
/// <param name="authenticationProvider">The authentication provider.</param>
public DefaultPPCModuleLoader(IAuthenticationProvider authenticationProvider)
{
_authenticationProvider = authenticationProvider;
AllModules = new ObservableCollection<IPPCModule>();
UserModules = new ObservableCollection<IPPCModule>();
BindingOperations.EnableCollectionSynchronization(UserModules, _syncObject);
_authenticationProvider.CurrentUserChanged += _authenticationProvider_CurrentUserChanged;
}
/// <summary>
/// Handles the authentication provider user changed event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void _authenticationProvider_CurrentUserChanged(object sender, User e)
{
LoadModules();
}
private ObservableCollection<IPPCModule> _allModules;
/// <summary>
/// Gets all loaded modules.
/// </summary>
public ObservableCollection<IPPCModule> AllModules
{
get { return _allModules; }
private set { _allModules = value; RaisePropertyChangedAuto(); }
}
private ObservableCollection<IPPCModule> _userModules;
/// <summary>
/// Gets all the user permitted modules.
/// </summary>
public ObservableCollection<IPPCModule> UserModules
{
get { return _userModules; }
private set { _userModules = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Loads all available PPC modules.
/// </summary>
public void LoadModules()
{
var settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
if (!_loaded)
{
//Preloaded
if (settings.UseJobsModuleV2)
{
LogManager.Log(String.Format("Loading module '{0}'...", nameof(JobsV2Module)));
AllModules.Add(new JobsV2Module());
}
else
{
LogManager.Log(String.Format("Loading module '{0}'...", nameof(JobsModule)));
AllModules.Add(new JobsModule());
}
//Preloaded
AllModules.Clear();
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (var file in Directory.GetFiles(assemblyFolder, "*.dll").Where(x => x.Contains("Tango.PPC")))
{
try
{
Assembly moduleAssembly = null;
moduleAssembly = Assembly.LoadFrom(file);
if (moduleAssembly != null)
{
foreach (var moduleType in moduleAssembly.GetLoadableTypes().Where(x => !x.IsInterface && typeof(IPPCModule).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 IPPCModule;
AllModules.Add(module);
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not load module " + moduleType.Name);
}
}
}
}
}
catch { }
}
_loaded = true;
}
AllModules = AllModules.OrderBy(x => x.GetType().GetCustomAttribute<PPCModuleAttribute>().Index).ToObservableCollection();
if (settings.UseJobsModuleV2)
{
var legacyJobsModule = AllModules.SingleOrDefault(x => x.GetType() == typeof(JobsModule));
AllModules.Remove(legacyJobsModule);
}
else
{
var newJobsModule = AllModules.SingleOrDefault(x => x.GetType() == typeof(JobsV2Module));
AllModules.Remove(newJobsModule);
}
UserModules.Clear();
if (_authenticationProvider.AuthenticationRequired && _authenticationProvider.CurrentUser != null)
{
UserModules = AllModules.Where(x => _authenticationProvider.CurrentUser.HasPermission(x.Permission)).ToObservableCollection();
}
else if (!_authenticationProvider.AuthenticationRequired)
{
UserModules = AllModules.ToObservableCollection();
}
ModulesLoaded?.Invoke(this, new EventArgs());
}
/// <summary>
/// Gets the PPC module of type T if loaded.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetPPCModule<T>() where T : IPPCModule
{
return UserModules.OfType<T>().FirstOrDefault();
}
}
}
|