-
Notifications
You must be signed in to change notification settings - Fork 10
/
CommandsMenu.cs
341 lines (307 loc) · 11.7 KB
/
CommandsMenu.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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using FreenetTray.Browsers;
using FreenetTray.Properties;
namespace FreenetTray
{
public partial class CommandsMenu : Form
{
// Milliseconds between connection attempts while waiting for startup.
private const int SocketPollInterval = 100;
/*
* Milliseconds to wait for startup before notifying the user that Freenet is starting.
*
* See http://www.nngroup.com/articles/response-times-3-important-limits/
*/
private const int SlowOpenThreshold = 3000;
// Milliseconds to show notification balloons.
private const int SlowOpenTimeout = 5000;
private const int WelcomeTimeout = 10000;
private NodeController _node;
public CommandsMenu()
{
InitializeComponent();
FormClosed += (sender, e) => trayIcon.Visible = false;
Shown += (sender, e) => Hide();
// TODO: Read registry to check if the old tray runs at startup and change settings accordingly. (or offer to?)
/*
* TODO: Will the settings be saved always or only if non-default? If always saved this
* introduces a fingerprint of Freenet on the system even when it doesn't do anything on
* startup.
*/
}
private void CommandsMenu_Load(object sender, EventArgs e)
{
trayIcon.Icon = Resources.Offline;
BeginInvoke(new Action(FindNode));
}
private void NodeStarted(object sender, EventArgs e)
{
RefreshMenu(true);
}
private void NodeStopped(object sender, EventArgs e)
{
RefreshMenu(false);
}
private void NodeCrashed(NodeController.CrashType crashType)
{
RefreshMenu(false);
BeginInvoke(new Action(new CrashDialog(crashType, Start, ViewLogs).Show));
}
private void FindNode()
{
while (true)
{
try
{
_node = new NodeController();
break;
}
catch (FileNotFoundException e)
{
FNLog.ErrorException(e, "Failed to find file");
}
catch (DirectoryNotFoundException e)
{
FNLog.ErrorException(e, "Failed to find directory");
}
catch (NodeController.MissingConfigValueException e)
{
// If the configuration files exist but are missing required
// values it is sufficiently surprising to warrant an error
// dialog.
FNLog.Error(strings.MalformedConfig, e.Filename, e.Value);
MessageBox.Show(String.Format(strings.MalformedConfig, e.Filename, e.Value), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (MissingJRE)
{
// No JRE was found
FNLog.Error(strings.JRENotFound);
MessageBox.Show(strings.JRENotFound, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
/* Cannot continue for now */
Application.Exit();
return;
}
// TODO: Explain what happened to prompt a custom location?
try
{
PreferencesWindow.PromptCustomLocation(this);
}
catch (OperationCanceledException)
{
/* User exited the file browser. */
Application.Exit();
return;
}
}
_node.OnStarted += NodeStarted;
_node.OnStopped += NodeStopped;
_node.OnCrashed += NodeCrashed;
foreach (var menuItem in new[]
{
openFreenetMenuItem,
startFreenetMenuItem,
stopFreenetMenuItem,
downloadsMenuItem,
viewLogsMenuItem,
preferencesMenuItem,
hideIconMenuItem,
})
{
menuItem.Enabled = true;
}
// Set menu up for whether there is an existing node.
RefreshMenu(_node.IsRunning());
ReadCommandLine();
}
private void openFreenetMenuItem_Click(object sender = null, EventArgs e = null)
{
Start();
var pollFproxy = new Thread(() =>
{
var fproxyListening = false;
var showSlowOpen = Settings.Default.ShowSlowOpenTip;
var openArgs = e as OpenArgs;
if (openArgs != null)
{
showSlowOpen = openArgs.ShowSlow;
}
/*
* TODO: Programatic way to get loopback address? This would not support IPv6.
* Use FProxy bind interface?
*/
var loopback = new IPAddress(new byte[] { 127, 0, 0, 1 });
var timer = new Stopwatch();
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
timer.Start();
while (_node.IsRunning())
{
try
{
sock.Connect(loopback, _node.FProxyPort);
fproxyListening = true;
break;
}
catch (SocketException ex)
{
FNLog.Debug("Connecting got error: {0}",
Enum.GetName(typeof (SocketError), ex.SocketErrorCode));
Thread.Sleep(SocketPollInterval);
}
// Show a startup notification if it's taking a while.
if (showSlowOpen && timer.ElapsedMilliseconds > SlowOpenThreshold)
{
BeginInvoke(new Action(() =>
{
trayIcon.BalloonTipText = strings.FreenetStarting;
trayIcon.ShowBalloonTip(SlowOpenTimeout);
}));
showSlowOpen = false;
}
}
timer.Stop();
}
if (fproxyListening)
{
FNLog.Debug("FProxy listening after {0}", timer.Elapsed);
BrowserUtil.Open(new Uri(String.Format("http://localhost:{0:d}", _node.FProxyPort)), true);
}
});
pollFproxy.Start();
}
private void startFreenetMenuItem_Click(object sender = null, EventArgs e = null)
{
Start();
}
private void stopFreenetMenuItem_Click(object sender = null, EventArgs e = null)
{
BeginInvoke(new Action(_node.Stop));
}
private void viewLogsMenuItem_Click(object sender = null, EventArgs e = null)
{
ViewLogs();
}
private void preferencesMenuItem_Click(object sender = null, EventArgs e = null)
{
new PreferencesWindow(BrowserUtil.GetAvailableBrowsers()).Show();
}
private void hideIconMenuItem_Click(object sender = null, EventArgs e = null)
{
// The node will continue running.
Application.Exit();
}
/*
* Stop the node if it is running, wait for it to exit, then quit the tray.
*/
private void exitMenuItem_Click(object sender = null, EventArgs e = null)
{
if (_node != null)
{
/*
* Register an event handler for the exit, but if the node is already
* stopped exit immediately because the event would never fire. This
* order of checking avoids missing a node exit: if the check came
* before the handler was registered, the node could exit between
* the check and the handler registration and the tray would hang.
*/
_node.OnStopped += (o, args) => Application.Exit();
if (!_node.IsRunning())
{
Application.Exit();
}
else
{
_node.Stop();
}
}
else
{
Application.Exit();
}
}
private void trayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
openFreenetMenuItem_Click(sender, e);
}
}
private void Start()
{
BeginInvoke(new Action(_node.Start));
}
private void ViewLogs()
{
// start the standart editor
Process.Start(_node.WrapperLogFilename);
}
private void RefreshMenu(bool running)
{
BeginInvoke(new Action(() =>
{
startFreenetMenuItem.Enabled = !running;
stopFreenetMenuItem.Enabled = running;
hideIconMenuItem.Visible = running;
trayIcon.Icon = running ? Resources.Online : Resources.Offline;
}));
}
private void ReadCommandLine()
{
/*
* TODO: Difficulties with this implementation are ignoring the application name if it is
* present and supporting arguments with parameters.
*/
foreach (var arg in Environment.GetCommandLineArgs())
{
switch (arg)
{
case "-open":
openFreenetMenuItem_Click();
break;
case "-start":
startFreenetMenuItem_Click();
break;
case "-stop":
stopFreenetMenuItem_Click();
break;
case "-downloads":
downloadsMenuItem_Click();
break;
case "-logs":
viewLogsMenuItem_Click();
break;
case "-preferences":
preferencesMenuItem_Click();
break;
case "-hide":
hideIconMenuItem_Click();
break;
case "-exit":
exitMenuItem_Click();
break;
case "-welcome":
trayIcon.BalloonTipIcon = ToolTipIcon.Info;
trayIcon.BalloonTipTitle = strings.FreenetStarting;
trayIcon.BalloonTipText = strings.WelcomeTip;
trayIcon.ShowBalloonTip(WelcomeTimeout);
openFreenetMenuItem_Click(null, new OpenArgs { ShowSlow = false });
break;
}
}
}
private class OpenArgs : EventArgs
{
public bool ShowSlow { get; set; }
}
private void downloadsMenuItem_Click(object sender = null, EventArgs e = null)
{
Process.Start("explorer.exe", _node.DownloadsDir);
}
}
}