1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using Autofac ;
3
5
using PassWinmenu . Configuration ;
4
6
using PassWinmenu . WinApi ;
5
7
@@ -8,35 +10,28 @@ namespace PassWinmenu.Actions
8
10
{
9
11
internal class ActionDispatcher
10
12
{
11
- private readonly DecryptPasswordAction decryptPasswordAction ;
12
- private readonly GenerateTotpAction generateTotpAction ;
13
- private readonly DecryptMetadataAction decryptMetadataAction ;
14
- private readonly GetKeyAction getKeyAction ;
13
+ private readonly ILifetimeScope container ;
15
14
private readonly IDialogService dialogService ;
16
- private readonly Dictionary < HotkeyAction , IAction > actions ;
17
15
18
16
public ActionDispatcher (
19
- DecryptPasswordAction decryptPasswordAction ,
20
- GenerateTotpAction generateTotpAction ,
21
- DecryptMetadataAction decryptMetadataAction ,
22
- GetKeyAction getKeyAction ,
23
- IDialogService dialogService ,
24
- Dictionary < HotkeyAction , IAction > actions )
17
+ ILifetimeScope container ,
18
+ IDialogService dialogService )
25
19
{
26
- this . decryptPasswordAction = decryptPasswordAction ;
27
- this . generateTotpAction = generateTotpAction ;
28
- this . decryptMetadataAction = decryptMetadataAction ;
29
- this . getKeyAction = getKeyAction ;
20
+ this . container = container ;
30
21
this . dialogService = dialogService ;
31
- this . actions = actions ;
32
22
}
33
23
34
24
/// <summary>
35
25
/// Asks the user to choose a password file, decrypts it, and copies the resulting value to the clipboard.
36
26
/// </summary>
37
27
public void DecryptPassword ( bool copyToClipboard , bool typeUsername , bool typePassword )
38
28
{
39
- Try ( ( ) => decryptPasswordAction . Execute ( copyToClipboard , typeUsername , typePassword ) , "Unable to decrypt password" ) ;
29
+ using var scope = container . BeginLifetimeScope ( ) ;
30
+ var decryptPasswordAction = scope . Resolve < DecryptPasswordAction > ( ) ;
31
+
32
+ Try (
33
+ ( ) => decryptPasswordAction . Execute ( copyToClipboard , typeUsername , typePassword ) ,
34
+ "Unable to decrypt password" ) ;
40
35
}
41
36
42
37
/// <summary>
@@ -45,21 +40,35 @@ public void DecryptPassword(bool copyToClipboard, bool typeUsername, bool typePa
45
40
/// </summary>
46
41
public void GenerateTotpCode ( bool copyToClipboard , bool typeTotpCode )
47
42
{
48
- Try ( ( ) => generateTotpAction . GenerateTotpCode ( copyToClipboard , typeTotpCode ) , "Unable to generate TOTP code" ) ;
43
+ using var scope = container . BeginLifetimeScope ( ) ;
44
+ var generateTotpAction = scope . Resolve < GenerateTotpAction > ( ) ;
45
+
46
+ Try (
47
+ ( ) => generateTotpAction . GenerateTotpCode ( copyToClipboard , typeTotpCode ) ,
48
+ "Unable to generate TOTP code" ) ;
49
49
}
50
50
51
51
public void DecryptMetadata ( bool copyToClipboard , bool type )
52
52
{
53
+ using var scope = container . BeginLifetimeScope ( ) ;
54
+ var decryptMetadataAction = scope . Resolve < DecryptMetadataAction > ( ) ;
55
+
53
56
Try ( ( ) => decryptMetadataAction . DecryptMetadata ( copyToClipboard , type ) , "Unable to decrypt metadata" ) ;
54
57
}
55
58
56
59
public void DecryptPasswordField ( bool copyToClipboard , bool type , string ? fieldName = null )
57
60
{
61
+ using var scope = container . BeginLifetimeScope ( ) ;
62
+ var getKeyAction = scope . Resolve < GetKeyAction > ( ) ;
63
+
58
64
Try ( ( ) => getKeyAction . GetKey ( copyToClipboard , type , fieldName ) , "Unable to decrypt password field" ) ;
59
65
}
60
66
61
67
public void Dispatch ( HotkeyAction hotkeyAction )
62
68
{
69
+ using var scope = container . BeginLifetimeScope ( ) ;
70
+ var actions = scope . Resolve < IEnumerable < IAction > > ( ) . ToDictionary ( a => a . ActionType ) ;
71
+
63
72
if ( actions . TryGetValue ( hotkeyAction , out var action ) )
64
73
{
65
74
Try ( ( ) => action . Execute ( ) , $ "Action '{ hotkeyAction } ' failed") ;
0 commit comments