Widow – Blame information for rev 28

Subversion Repositories:
Rev:
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 {
171 Application.Exit();
172 }
173  
174 private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
175 {
176 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
177  
178 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
179 }
180  
5 office 181 private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
182 {
183 Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;
184  
9 office 185 WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate;
5 office 186 }
187  
188 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
189 {
190 // Show the about form.
191 AboutForm = new AboutForm();
192 AboutForm.Show();
193 }
194  
11 office 195 private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e)
7 office 196 {
11 office 197 await WindowManipulation.Apply();
7 office 198 }
199  
8 office 200 private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
201 {
202 var toolStripMenuItem = (ToolStripMenuItem) sender;
203  
204 if (!int.TryParse(toolStripTextBox1.Text, out var time))
205 {
206 return;
207 }
208  
209 Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
210 Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture);
211  
9 office 212 WindowManipulation.ApplyEveryTime = time;
213 WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
8 office 214 }
215  
13 office 216 private void NotifyIcon1_DoubleClick(object sender, EventArgs e)
217 {
218 if (RuleEditForm != null)
219 {
220 return;
221 }
222  
223 RuleEditForm = new RuleEditForm(this, Windows);
224 RuleEditForm.Closed += RuleEditForm_Closed;
225 RuleEditForm.Show();
226 }
227  
228 private async void NotifyIcon1_Click(object sender, EventArgs e)
229 {
230 await WindowManipulation.Apply();
231 }
232  
22 office 233 private void ShowLogToolStripMenuItem_Click(object sender, EventArgs e)
234 {
235 if (LogForm != null)
236 {
237 return;
238 }
239  
240 LogForm = new LogForm();
241 LogForm.Closed += LogForm_Closed;
242 LogForm.Show();
243 }
244  
245 private void LogForm_Closed(object sender, EventArgs e)
246 {
247 LogForm.Closed -= LogForm_Closed;
248 LogForm.Dispose();
249 LogForm = null;
250 }
251  
1 office 252 #endregion
253  
254 #region Private Methods
255  
256 private async Task LoadWindows()
257 {
28 office 258 if (!Directory.Exists(Constants.UserApplicationDirectory))
259 {
260 Directory.CreateDirectory(Constants.UserApplicationDirectory);
261 }
262  
23 office 263 switch (await WindowsSerialization.Deserialize(Constants.WindowsSettingsFile))
1 office 264 {
265 case SerializationSuccess serializationSuccess:
266 Windows = serializationSuccess.Windows;
21 office 267 if (LogForm != null)
268 {
269 LogForm.InvokeIfRequired(form =>
270 {
271 form.logTextBox.Text += "Windows loaded." + Environment.NewLine;
272 });
273 }
22 office 274  
1 office 275 break;
276 case SerializationFailure serializationFailure:
277 Windows = new Windows.Windows();
21 office 278 if (LogForm != null)
279 {
280 LogForm.InvokeIfRequired(form =>
281 {
22 office 282 form.logTextBox.Text += "Failed to load windows: " +
283 serializationFailure.Exception.Message + Environment.NewLine;
21 office 284 });
285 }
22 office 286  
1 office 287 break;
288 }
289 }
290  
291 #endregion
292 }
293 }