-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContextMenus.cs
407 lines (375 loc) · 15.5 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// 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.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace TrayApp
{
/// <summary>
///
/// </summary>
internal class ContextMenus
{
/// <summary>
/// Is the About box displayed?
/// </summary>
private bool isAboutLoaded = false;
/// <summary>
/// Creates this instance.
/// </summary>
/// <returns>ContextMenuStrip</returns>
public ContextMenuStrip CreateFeedsMenu(bool allow_notifications = true)
{
// Add the default menu options.
// TODO: Cache the feeds results to avoid heavy rebuilding every time a checkbox value changes :(
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem item;
if (Helper.NeedUpgrade)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Save();
Helper.NeedUpgrade = false;
}
Properties.Settings.Default.Reload();
bool show_notifications = Properties.Settings.Default.ShowNotifications;
if (!allow_notifications)
{
show_notifications = false;
}
bool debug = Properties.Settings.Default.Debug;
List<string> FeedList = Helper.Convert(Properties.Settings.Default.SettingFeedList);
List<string> feed_title_list = new List<string>();
if (FeedList != null)
{
short max_entry_per_site = Properties.Settings.Default.EntriesPerFeed;
StringCollection loaded_urls = new StringCollection();
string temporaryRssFile = System.IO.Path.GetTempFileName();
foreach (var url_iter in FeedList)
{
string tip_title = "";
if (show_notifications)
{
tip_title = "Loading";
}
if (loaded_urls.Contains(url_iter))
{
if (show_notifications)
{
tip_title += "... (Ignored)";
}
}
else if (!Helper.ValidateInput(url_iter))
{
if (show_notifications)
{
tip_title += "... (Invalid)";
}
}
else
{
// Hack to handle invalid RSS 2.0 dates
// https://stackoverflow.com/a/3936714
XmlReader r = new MyXmlReader(url_iter);
SyndicationFeed feed = SyndicationFeed.Load(r);
Rss20FeedFormatter rssFormatter = feed.GetRss20Formatter();
XmlTextWriter rssWriter = new XmlTextWriter(temporaryRssFile, Encoding.UTF8);
rssWriter.Formatting = Formatting.Indented;
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();
item = new ToolStripMenuItem
{
Text = rssFormatter.Feed.Title.Text,
Image = Properties.Resources.Rss
};
//if (show_notifications)
//{
// showToolTip(tip_title + " " + item.Text + ((debug) ? saved_url : ""));
//}
feed_title_list.Add(item.Text);
{// I cannot find how to obtain the base url within the first <link>blah</link> element, so I'll do a dirty split
string main_website_url = "";
foreach (string baseurl in url_iter.Split('/').ToList().GetRange(0, 3))
{
main_website_url += baseurl + "/";
}
item.Click += delegate (object sender, EventArgs e) { FeedEntry_Click(sender, e, main_website_url); };
}
// TODO: get image
//foreach (SyndicationElementExtension extension in f.ElementExtensions)
//{
// XElement element = extension.GetObject<XElement>();
//
// if (element.HasAttributes)
// {
// foreach (var attribute in element.Attributes())
// {
// string value = attribute.Value.ToLower();
// if (value.StartsWith("http://") && (value.EndsWith(".jpg") || value.EndsWith(".png") || value.EndsWith(".gif")))
// {
// rssItem.ImageLinks.Add(value); // Add here the image link to some array
// }
// }
// }
//}
menu.Items.Add(item); // Add menu entry with the feed name
for (int i = 0; i < max_entry_per_site; i++) // Add elements from feed
{
var syndicationItem = rssFormatter.Feed.Items.ElementAt(i);
item = new ToolStripMenuItem()
{
Text = syndicationItem.Title.Text
};
item.Click += delegate (object sender, EventArgs e) { FeedEntry_Click(sender, e, syndicationItem.Links.ToList().First().Uri.ToString()); };
menu.Items.Add(item);
//TODO: Get "Info" field and set as tooltip
}
loaded_urls.Add(url_iter);
if (url_iter != FeedList.Last())
{
menu.Items.Add(new ToolStripSeparator()); // Separator.
}
}
}
// Must be saved after the foreach loop to prevent overwriting the working data
Properties.Settings.Default.SettingFeedList.Clear();
Properties.Settings.Default.SettingFeedList = loaded_urls;
Properties.Settings.Default.Save();
if (File.Exists(temporaryRssFile))
{
try
{
File.Delete(temporaryRssFile);
}
catch (Exception ex)
{
Program.ExceptionHandler(ex);
}
}
}
if (show_notifications)
{
string titles = "";
foreach (string title in feed_title_list)
{
titles += title + System.Environment.NewLine;
}
if (titles != "")
{
showToolTip(titles, "Loaded Feeds");
}
}
return menu;
}
public ContextMenuStrip CreateLoadingMenu()
{
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem item;
// Add a feed.
item = new ToolStripMenuItem()
{
Text = "Loading..."
};
item.Enabled = false;
menu.Items.Add(item);
return menu;
}
public ContextMenuStrip CreateOptionsMenu()
{
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem item;
// Add a feed.
item = new ToolStripMenuItem()
{
Text = "Add Feed",
Image = Properties.Resources.Rss
};
item.Click += new EventHandler(AddFeed_Click);
menu.Items.Add(item);
// About box
item = new ToolStripMenuItem()
{
Text = "About",
Image = Properties.Resources.About
};
item.Click += new EventHandler(About_Click);
menu.Items.Add(item);
// Notifications On/Off
item = new ToolStripMenuItem()
{
Text = "Notifications",
Checked = Properties.Settings.Default.ShowNotifications,
};
if (item.Checked)
{
item.Image = Properties.Resources.checkmark;
}
item.Click += new EventHandler(Notification_Setting_Click);
menu.Items.Add(item);
// Add to Startup
item = new ToolStripMenuItem()
{
Text = "Run at Login",
Checked = Properties.Settings.Default.AutomaticStartup,
};
if (item.Checked)
{
item.Image = Properties.Resources.checkmark;
}
item.Click += new EventHandler(Startup_Click);
menu.Items.Add(item);
// Separator.
menu.Items.Add(new ToolStripSeparator());
// Exit.
item = new ToolStripMenuItem()
{
Text = "Exit",
Image = Properties.Resources.Exit
};
item.Click += new System.EventHandler(Exit_Click);
menu.Items.Add(item);
System.GC.Collect(3, System.GCCollectionMode.Forced);
System.GC.WaitForFullGCComplete();
return menu;
}
/// <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 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 void AddFeed_Click(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 void Exit_Click(object sender, EventArgs e)
{
// Quit without further ado.
TrayApp.ProcessIcon.ni.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 void Explorer_Click(object sender, EventArgs e)
{
Process.Start("explorer", null);
}
private void FeedEntry_Click(object sender, EventArgs e, string u)
{
try
{
Process.Start(u);
}
catch (Exception ex)
{
Program.ExceptionHandler(ex);
}
}
/// <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 void Notification_Setting_Click(object sender, EventArgs e)
{
Properties.Settings.Default.ShowNotifications = !Properties.Settings.Default.ShowNotifications;
Properties.Settings.Default.Save();
TrayApp.ProcessIcon.ni.ContextMenuStrip = new ContextMenus().CreateFeedsMenu(false);
}
private void showToolTip(string text, string title = "")
{
if (title == "")
{
title = Program.ProductName;
}
ProcessIcon.ni.BalloonTipTitle = title;
ProcessIcon.ni.BalloonTipText = text;
ProcessIcon.ni.ShowBalloonTip(1);
}
/// <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 void Startup_Click(object sender, EventArgs e)
{
bool startUp = !Properties.Settings.Default.AutomaticStartup;
Integration.AddToStartup(startUp);
Properties.Settings.Default.AutomaticStartup = startUp;
Properties.Settings.Default.Save();
TrayApp.ProcessIcon.ni.ContextMenuStrip = new ContextMenus().CreateFeedsMenu(false);
}
private 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();
}
}
}
}
}