Widow – Blame information for rev 22

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