forked from XenHat/TrayReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextMenus.cs
364 lines (323 loc) · 14.3 KB
/
ContextMenus.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace TrayApp
{
public class TrackedToolStripMenuItem
{
private Lazy<ToolStripMenuItem> _TemplateToolStripMenuItemList = new Lazy<ToolStripMenuItem>(() => _CreateTemplateToolStripMenuItem());
public ToolStripMenuItem Value
{
get
{
return _TemplateToolStripMenuItemList.Value;
}
}
private static ToolStripMenuItem _CreateTemplateToolStripMenuItem()
{
ToolStripMenuItem instance = new ToolStripMenuItem();
return instance;
}
}
/// <summary>
/// </summary>
internal class MenuGenerator
{
// This method handles the Closing event. The ToolStripDropDown control is not allowed to
// close unless the Done menu item is clicked or the Close method is called explicitly. The
// Done menu item is enabled only after both of the other menu items have been selected.
private void ContextMenuStrip_Closing(
object sender,
ToolStripDropDownClosingEventArgs e)
{
}
public static class ContextMenus
{
private static readonly List<EventHandler> menu_events_handlers = new List<EventHandler>()
{
Notification_Setting_Click,
Startup_Click,
KillOnIdle_Click,
KillDropbox_Click,
Set_Power_Plan_Click,
};
private static readonly List<string> menu_names = new List<string>()
{
"Notifications",
"Run at Login",
"Kill on Idle",
"Kill Dropbox",
"Auto. Power Plan",
};
/// <summary>
/// Is the About box displayed?
/// </summary>
private static bool isAboutLoaded = false;
private static ToolStripSeparator sSeparator = new ToolStripSeparator();
/// <summary>
/// Creates this instance.
/// </summary>
/// <returns>ContextMenuStrip</returns>
public static void CreateFeedsMenu()
{
// Warning: This leaves a window in which the menu doesn't exist. I'll fix that later
// when the leak is gone.
if (Program.sTrayIcon.ContextMenuStrip != null)
{
Program.sTrayIcon.ContextMenuStrip.Dispose();
}
// Add the default menu options.
Program.sTrayIcon.ContextMenuStrip = new ContextMenuStrip();
Program.sTrayIcon.ContextMenuStrip.ShowImageMargin = true;
// DEBUG
//TrackedToolStripMenuItem item = new TrackedToolStripMenuItem();
//ProcessIcon.ni.ContextMenuStrip.Items.Add(item);
// Regenerate a new list with the current settings.
List<bool> menu_items_enabled = new List<bool>()
{
TrayApp.Properties.Settings.Default.ShowNotifications,
TrayApp.Properties.Settings.Default.AutomaticStartup,
TrayApp.Properties.Settings.Default.KillOnIdle,
TrayApp.Properties.Settings.Default.KillDropbox,
TrayApp.Properties.Settings.Default.ForceOnDemandPowerPlan,
};
for (int i = 0; i < menu_names.Count; i++)
{
var LoopItem = new TrackedToolStripMenuItem().Value;
//ToolStripMenuItem watt = new ToolStripMenuItem();
LoopItem.Text = menu_names[i];
LoopItem.Click += menu_events_handlers[i];
// TODO: Still need to avoid rebuilding the menu every time the menu changes
if (menu_items_enabled[i])
{
LoopItem.Image = TrayApp.Properties.Resources.checkmark;
}
Program.sTrayIcon.ContextMenuStrip.Items.Add(LoopItem);
};
/// ----------------------------------------------------------------------------------
/// Always present or static menu items
// WOAH, this creates a memory leak!
//sTrayIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator()); // Separator.
Program.sTrayIcon.ContextMenuStrip.Items.Add(sSeparator);
// Add one entry to this menu to kill everything
var item = new TrackedToolStripMenuItem();
item.Value.Text = "Force Idle Now!";
item.Value.Image = TrayApp.Properties.Resources.Rss;
item.Value.Click += delegate (object sender, EventArgs e) { MainAction_Click(sender, e); };
Program.sTrayIcon.ContextMenuStrip.Items.Add(item.Value);
item = new TrackedToolStripMenuItem();
item.Value.Text = "Add Executable";
item.Value.Image = TrayApp.Properties.Resources.Plus;
item.Value.Click += delegate (object sender, EventArgs e) { AddItem(sender, e); };
Program.sTrayIcon.ContextMenuStrip.Items.Add(item.Value);
item = new TrackedToolStripMenuItem();
item.Value.Text = "About";
item.Value.Image = TrayApp.Properties.Resources.About;
item.Value.Click += delegate (object sender, EventArgs e) { About_Click(sender, e); };
Program.sTrayIcon.ContextMenuStrip.Items.Add(item.Value);
item = new TrackedToolStripMenuItem();
item.Value.Text = "Exit";
item.Value.Image = TrayApp.Properties.Resources.Exit;
item.Value.Click += delegate (object sender, EventArgs e) { Exit_Click(sender, e); };
Program.sTrayIcon.ContextMenuStrip.Items.Add(item.Value);
/// end ----------------------------------------------------------------------------------
//if (sTrayIcon.ContextMenuStrip != null)
//{
// sTrayIcon.ContextMenuStrip.Dispose();
//}
//item.Dispose();
GC.Collect();
GC.WaitForFullGCComplete();
}
/// <summary>
/// Handles the Click event of the Kill Background Processes control.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
public static void MainAction_Click(object sender, EventArgs e)
{
Program.DoIdleTasks();
}
public static void RegenerateMenu()
{
CreateFeedsMenu();
}
/// <summary>
/// Handles the Click event of the About control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void About_Click(object sender, EventArgs e)
{
if (!isAboutLoaded)
{
isAboutLoaded = true;
new AboutBox().ShowDialog();
isAboutLoaded = false;
}
}
/// <summary>
/// Handles the Click event of the Add Feed control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void AddItem(object sender, EventArgs e)
{
new AddFeed().ShowDialog();
}
/// <summary>
/// Handles the Click event of the Exit control.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void Exit_Click(object sender, EventArgs e)
{
// Quit without further ado.
Program.sTrayIcon.Visible = false;
Application.Exit();
}
/// <summary>
/// Handles the Click event of the Explorer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void Explorer_Click(object sender, EventArgs e)
{
Process.Start("explorer", null);
}
private static void FeedEntry_Click(object sender, EventArgs e, string u)
{
try
{
ProcessDestroyer.KillProcessByName(new List<string>() { u });
}
catch (Exception ex)
{
Program.ExceptionHandler(ex);
}
}
private static void KillDropbox_Click(object sender, EventArgs e)
{
// TODO: Shouldn't we use the event data?
TrayApp.Properties.Settings.Default.KillDropbox = !TrayApp.Properties.Settings.Default.KillDropbox;
TrayApp.Properties.Settings.Default.Save();
RegenerateMenu();
}
/// <summary>
/// Handles the Click event of the KillOnIdle control.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void KillOnIdle_Click(object sender, EventArgs e)
{
// TODO: Shouldn't we use the event data?
TrayApp.Properties.Settings.Default.KillOnIdle = !TrayApp.Properties.Settings.Default.KillOnIdle;
TrayApp.Properties.Settings.Default.Save();
RegenerateMenu();
}
/// <summary>
/// Handles the Click event of the Notification Setting control.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void Notification_Setting_Click(object sender, EventArgs e)
{
// Flipping here can cause bugs, be more explicit so that the value is always right.
TrayApp.Properties.Settings.Default.ShowNotifications = !TrayApp.Properties.Settings.Default.ShowNotifications;
TrayApp.Properties.Settings.Default.Save();
RegenerateMenu();
}
/// <summary>
/// Handles the Click event of the Add Feed control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void Set_Power_Plan_Click(object sender, EventArgs e)
{
TrayApp.Properties.Settings.Default.ForceOnDemandPowerPlan = !TrayApp.Properties.Settings.Default.ForceOnDemandPowerPlan;
TrayApp.Properties.Settings.Default.Save();
RegenerateMenu();
}
/// <summary>
/// Handles the Click event of the Startup control.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event data.
/// </param>
private static void Startup_Click(object sender, EventArgs e)
{
TrayApp.Properties.Settings.Default.AutomaticStartup = !TrayApp.Properties.Settings.Default.AutomaticStartup;
TrayApp.Properties.Settings.Default.Save();
RegenerateMenu();
Integration.AddToStartup();
}
}
}
internal class MyXmlReader : XmlTextReader
{
private const string CustomUtcDateTimeFormat = "ddd MMM dd HH:mm:ss Z yyyy";
private bool readingDate = false;
// Wed Oct 07 08:00:07 GMT 2009
public MyXmlReader(Stream s) : base(s)
{
}
public MyXmlReader(string inputUri) : base(inputUri)
{
}
public override void ReadEndElement()
{
if (readingDate)
{
readingDate = false;
}
base.ReadEndElement();
}
public override void ReadStartElement()
{
if (string.Equals(base.NamespaceURI, string.Empty, StringComparison.InvariantCultureIgnoreCase) &&
(string.Equals(base.LocalName, "lastBuildDate", StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(base.LocalName, "pubDate", StringComparison.InvariantCultureIgnoreCase)))
{
readingDate = true;
}
base.ReadStartElement();
}
public override string ReadString()
{
if (readingDate)
{
string dateString = base.ReadString();
DateTime dt;
if (!DateTime.TryParse(dateString, out dt))
dt = DateTime.ParseExact(dateString, CustomUtcDateTimeFormat, System.Globalization.CultureInfo.InvariantCulture);
return dt.ToUniversalTime().ToString("R", System.Globalization.CultureInfo.InvariantCulture);
}
else
{
return base.ReadString();
}
}
}
}