forked from TeunvandenBroek/C-sharp-clipboardquestionanswer-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.cs
218 lines (186 loc) · 7.29 KB
/
Bootstrap.cs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
namespace it
{
using it.Actions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
/// <summary>
/// The bootstrap class is provided to allow the application to run with out a form.
/// We can use a form however in the future by adding it to here.
/// </summary>
internal sealed class Bootstrap : IDisposable
{
private readonly ClipboardMonitor clipboardMonitor = new ClipboardMonitor();
private readonly ControlContainer container = new ControlContainer();
private readonly NotifyIcon notifyIcon;
private readonly IReadOnlyList<Question> questionList = Questions.LoadQuestions();
// Container to hold the actions
private ServiceProvider serviceProvider;
public Bootstrap()
{
notifyIcon = new NotifyIcon(container)
{
Visible = true,
};
ConfigureDependancies();
clipboardMonitor.ClipboardChanged += ClipboardMonitor_ClipboardChanged;
}
private bool disposed;
public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}
private void Dispose(bool disposing)
{
if (disposed)
{
return;
}
disposed = true;
if (disposing)
{
}
if (serviceProvider != null)
{
(serviceProvider as IDisposable)?.Dispose();
serviceProvider = null;
}
if (notifyIcon != null)
{
notifyIcon?.Dispose();
}
if (container != null)
{
container?.Dispose();
}
if (clipboardMonitor != null)
{
clipboardMonitor?.Dispose();
}
}
private void ConfigureDependancies()
{
// Add configure services
ServiceCollection serviceDescriptors = new ServiceCollection();
_ = serviceDescriptors.AddSingleton<IAction, ConvertActions>();
_ = serviceDescriptors.AddSingleton<IAction, TryRomanActions>();
_ = serviceDescriptors.AddSingleton<IAction, CountdownActions>();
_ = serviceDescriptors.AddSingleton<IAction, DeviceActions>();
_ = serviceDescriptors.AddSingleton<IAction, RandomActions>();
_ = serviceDescriptors.AddSingleton<IAction, StopwatchActions>();
_ = serviceDescriptors.AddSingleton<IAction, TimespanActions>();
_ = serviceDescriptors.AddSingleton<IAction, numberToHex>();
_ = serviceDescriptors.AddSingleton<IAction, TimezoneActions>();
_ = serviceDescriptors.AddSingleton<IAction, BmiActions>();
_ = serviceDescriptors.AddSingleton<IAction, tryBinary>();
_ = serviceDescriptors.AddSingleton<IAction, MathActions>();
(serviceProvider as IDisposable)?.Dispose();
serviceProvider = serviceDescriptors.BuildServiceProvider();
}
internal void Startup(string clipboardText)
{
}
private void ClipboardMonitor_ClipboardChanged(object sender, ClipboardChangedEventArgs e)
{
// retrieve the text from the clipboard
if (e.DataObject.GetData(DataFormats.Text) is string clipboardText)
{
// the data is not a string. bail.
if (string.IsNullOrWhiteSpace(clipboardText))
{
return;
}
// if we get to here, we have text
ProcessClipboardText(clipboardText);
}
}
private void ProcessClipboardText(string clipboardText)
{
if (clipboardText is null)
{
throw new ArgumentNullException(nameof(clipboardText));
}
try
{
IAction service = serviceProvider.GetServices<IAction>().FirstOrDefault(s => s.Matches(clipboardText));
if (service is object)
{
ActionResult actionResult = service.TryExecute(clipboardText);
// re attach the event
if (!string.IsNullOrWhiteSpace(actionResult.Title) || !string.IsNullOrWhiteSpace(actionResult.Description))
{
clipboardMonitor.ClipboardChanged -= ClipboardMonitor_ClipboardChanged;
ProcessResult(actionResult, clipboardText);
clipboardMonitor.ClipboardChanged += ClipboardMonitor_ClipboardChanged;
}
return;
}
if (clipboardText.Length > 2)
{
for (int i = 0; i < questionList.Count; i++)
{
Question question = questionList[i];
if (question.Text.Contains(clipboardText))
{
ProcessResult(new ActionResult(question.Text, question.Answer), clipboardText);
return;
}
}
}
}
catch (Exception ex)
{
_ = MessageBox.Show(ex.ToString());
}
}
private void ProcessResult(ActionResult actionResult, string clipboardText)
{
if (clipboardText is null)
{
throw new ArgumentNullException(nameof(clipboardText));
}
if (string.Equals(Clipboard.GetText(), clipboardText, StringComparison.Ordinal))
{
Clipboard.Clear();
}
notifyIcon.Icon = SystemIcons.Exclamation;
notifyIcon.BalloonTipTitle = actionResult.Title;
notifyIcon.BalloonTipText = actionResult.Description;
notifyIcon.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon.ShowBalloonTip(1000);
}
internal static void EnsureWindowStartup(bool isStartingWithWindows)
{
const string keyName = "Clipboard Assistant";
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (key is null)
{
return;
}
string value = key.GetValue(keyName, null) as string;
if (isStartingWithWindows)
{
// key doesn't exist, add it
if (string.IsNullOrWhiteSpace(value) && string.Equals(value, Assembly.GetExecutingAssembly().Location, StringComparison.Ordinal))
{
key.SetValue(keyName, Assembly.GetExecutingAssembly().Location);
}
}
else if (!string.IsNullOrWhiteSpace(value))
{
key.DeleteValue(keyName);
}
key.Close();
key.Dispose();
}
}
}