Widow – Blame information for rev 29
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.ComponentModel; |
||
3 | using System.Configuration; |
||
8 | office | 4 | using System.Globalization; |
28 | office | 5 | using System.IO; |
1 | office | 6 | using System.Threading.Tasks; |
7 | using System.Windows.Forms; |
||
15 | office | 8 | using AutoUpdaterDotNET; |
1 | office | 9 | using Widow.Properties; |
10 | using Widow.Serialization; |
||
11 | |||
12 | namespace Widow |
||
13 | { |
||
6 | office | 14 | public partial class MainForm : Form |
1 | office | 15 | { |
16 | #region Public Events & Delegates |
||
17 | |||
18 | public event EventHandler<WindowCreatedEventArgs> WindowCreated; |
||
19 | |||
20 | public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed; |
||
21 | |||
22 | #endregion |
||
23 | |||
24 | #region Public Enums, Properties and Fields |
||
25 | |||
26 | public Windows.Windows Windows { get; set; } |
||
27 | |||
28 | public RuleEditForm RuleEditForm { get; set; } |
||
29 | |||
9 | office | 30 | public WindowManipulation WindowManipulation { get; set; } |
1 | office | 31 | |
5 | office | 32 | public AboutForm AboutForm { get; set; } |
22 | office | 33 | |
21 | office | 34 | public LogForm LogForm { get; private set; } |
5 | office | 35 | |
1 | office | 36 | #endregion |
37 | |||
38 | #region Constructors, Destructors and Finalizers |
||
39 | |||
6 | office | 40 | public MainForm() |
1 | office | 41 | { |
42 | InitializeComponent(); |
||
15 | office | 43 | AutoUpdater.Start("http://widow.grimore.org/update/update.xml"); |
6 | office | 44 | |
23 | office | 45 | // Upgrade settings if required. |
46 | if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
||
47 | { |
||
48 | Settings.Default.Upgrade(); |
||
49 | } |
||
50 | |||
1 | office | 51 | // Bind to settings changed event. |
52 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
||
53 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
||
54 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
||
55 | |||
56 | Natives.RegisterShellHookWindow(Handle); |
||
57 | |||
18 | office | 58 | WindowManipulation = new WindowManipulation(Settings.Default, this); |
59 | |||
22 | office | 60 | LoadWindows().ContinueWith(task => { WindowManipulation.AddWindows(Windows); }); |
1 | office | 61 | } |
62 | |||
63 | /// <summary> |
||
64 | /// Clean up any resources being used. |
||
65 | /// </summary> |
||
66 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
67 | protected override void Dispose(bool disposing) |
||
68 | { |
||
69 | if (disposing && components != null) |
||
70 | { |
||
71 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
||
72 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
||
73 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
||
74 | |||
75 | components.Dispose(); |
||
76 | } |
||
77 | |||
78 | base.Dispose(disposing); |
||
79 | } |
||
80 | |||
81 | #endregion |
||
82 | |||
83 | #region Private Overrides |
||
84 | |||
85 | protected override void WndProc(ref Message m) |
||
86 | { |
||
87 | var handle = m.LParam; |
||
88 | var windowName = Helpers.GetWindowTitle(handle); |
||
89 | |||
90 | switch (m.WParam.ToInt32()) |
||
91 | { |
||
92 | case (int) Natives.WM.HSHELL_WINDOWCREATED: |
||
93 | WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle)); |
||
94 | break; |
||
95 | case (int) Natives.WM.HSHELL_WINDOWDESTROYED: |
||
96 | WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle)); |
||
97 | break; |
||
98 | } |
||
99 | |||
100 | base.WndProc(ref m); |
||
101 | } |
||
102 | |||
103 | #endregion |
||
104 | |||
105 | #region Event Handlers |
||
106 | |||
8 | office | 107 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
1 | office | 108 | { |
109 | Settings.Default.Save(); |
||
110 | } |
||
111 | |||
8 | office | 112 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
1 | office | 113 | { |
114 | } |
||
115 | |||
8 | office | 116 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
1 | office | 117 | { |
118 | } |
||
119 | |||
120 | private void NewToolStripMenuItem_Click(object sender, EventArgs e) |
||
121 | { |
||
11 | office | 122 | if (RuleEditForm != null) |
123 | { |
||
124 | return; |
||
125 | } |
||
126 | |||
1 | office | 127 | RuleEditForm = new RuleEditForm(this, Windows); |
128 | RuleEditForm.Closed += RuleEditForm_Closed; |
||
129 | RuleEditForm.Show(); |
||
130 | } |
||
131 | |||
132 | private async void RuleEditForm_Closed(object sender, EventArgs e) |
||
133 | { |
||
134 | RuleEditForm.Closed -= RuleEditForm_Closed; |
||
135 | RuleEditForm.Dispose(); |
||
136 | RuleEditForm = null; |
||
137 | |||
28 | office | 138 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
139 | { |
||
140 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
141 | } |
||
142 | |||
23 | office | 143 | switch (await WindowsSerialization.Serialize(Windows, Constants.WindowsSettingsFile)) |
1 | office | 144 | { |
145 | case SerializationSuccess serializationSuccess: |
||
21 | office | 146 | if (LogForm != null) |
147 | { |
||
148 | LogForm.InvokeIfRequired(form => |
||
149 | { |
||
150 | form.logTextBox.Text += "Windows saved." + Environment.NewLine; |
||
151 | }); |
||
152 | } |
||
22 | office | 153 | |
1 | office | 154 | break; |
155 | case SerializationFailure serializationFailure: |
||
21 | office | 156 | if (LogForm != null) |
157 | { |
||
158 | LogForm.InvokeIfRequired(form => |
||
159 | { |
||
22 | office | 160 | form.logTextBox.Text += "Failed to save windows: " + |
161 | serializationFailure.Exception.Message + Environment.NewLine; |
||
21 | office | 162 | }); |
163 | } |
||
22 | office | 164 | |
1 | office | 165 | break; |
166 | } |
||
167 | } |
||
168 | |||
169 | private void ExitToolStripMenuItem_Click(object sender, EventArgs e) |
||
170 | { |
||
29 | office | 171 | Close(); |
172 | |||
173 | Environment.Exit(0); |
||
1 | office | 174 | } |
175 | |||
176 | private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
||
177 | { |
||
178 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
||
179 | |||
180 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
||
181 | } |
||
182 | |||
5 | office | 183 | private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
184 | { |
||
185 | Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked; |
||
186 | |||
9 | office | 187 | WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate; |
5 | office | 188 | } |
189 | |||
190 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
191 | { |
||
192 | // Show the about form. |
||
193 | AboutForm = new AboutForm(); |
||
194 | AboutForm.Show(); |
||
195 | } |
||
196 | |||
11 | office | 197 | private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e) |
7 | office | 198 | { |
11 | office | 199 | await WindowManipulation.Apply(); |
7 | office | 200 | } |
201 | |||
8 | office | 202 | private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
203 | { |
||
204 | var toolStripMenuItem = (ToolStripMenuItem) sender; |
||
205 | |||
206 | if (!int.TryParse(toolStripTextBox1.Text, out var time)) |
||
207 | { |
||
208 | return; |
||
209 | } |
||
210 | |||
211 | Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
||
212 | Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture); |
||
213 | |||
9 | office | 214 | WindowManipulation.ApplyEveryTime = time; |
215 | WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
||
8 | office | 216 | } |
217 | |||
13 | office | 218 | private void NotifyIcon1_DoubleClick(object sender, EventArgs e) |
219 | { |
||
220 | if (RuleEditForm != null) |
||
221 | { |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | RuleEditForm = new RuleEditForm(this, Windows); |
||
226 | RuleEditForm.Closed += RuleEditForm_Closed; |
||
227 | RuleEditForm.Show(); |
||
228 | } |
||
229 | |||
230 | private async void NotifyIcon1_Click(object sender, EventArgs e) |
||
231 | { |
||
232 | await WindowManipulation.Apply(); |
||
233 | } |
||
234 | |||
22 | office | 235 | private void ShowLogToolStripMenuItem_Click(object sender, EventArgs e) |
236 | { |
||
237 | if (LogForm != null) |
||
238 | { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | LogForm = new LogForm(); |
||
243 | LogForm.Closed += LogForm_Closed; |
||
244 | LogForm.Show(); |
||
245 | } |
||
246 | |||
247 | private void LogForm_Closed(object sender, EventArgs e) |
||
248 | { |
||
249 | LogForm.Closed -= LogForm_Closed; |
||
250 | LogForm.Dispose(); |
||
251 | LogForm = null; |
||
252 | } |
||
253 | |||
1 | office | 254 | #endregion |
255 | |||
256 | #region Private Methods |
||
257 | |||
258 | private async Task LoadWindows() |
||
259 | { |
||
28 | office | 260 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
261 | { |
||
262 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
263 | } |
||
264 | |||
23 | office | 265 | switch (await WindowsSerialization.Deserialize(Constants.WindowsSettingsFile)) |
1 | office | 266 | { |
267 | case SerializationSuccess serializationSuccess: |
||
268 | Windows = serializationSuccess.Windows; |
||
21 | office | 269 | if (LogForm != null) |
270 | { |
||
271 | LogForm.InvokeIfRequired(form => |
||
272 | { |
||
273 | form.logTextBox.Text += "Windows loaded." + Environment.NewLine; |
||
274 | }); |
||
275 | } |
||
22 | office | 276 | |
1 | office | 277 | break; |
278 | case SerializationFailure serializationFailure: |
||
279 | Windows = new Windows.Windows(); |
||
21 | office | 280 | if (LogForm != null) |
281 | { |
||
282 | LogForm.InvokeIfRequired(form => |
||
283 | { |
||
22 | office | 284 | form.logTextBox.Text += "Failed to load windows: " + |
285 | serializationFailure.Exception.Message + Environment.NewLine; |
||
21 | office | 286 | }); |
287 | } |
||
22 | office | 288 | |
1 | office | 289 | break; |
290 | } |
||
291 | } |
||
292 | |||
293 | #endregion |
||
294 | } |
||
295 | } |