-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSessDlg.cs
653 lines (609 loc) · 20.8 KB
/
SessDlg.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright (C) 2004-2005, 2007 Rocky Lo. All Rights Reserved.
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
// This file is part of the VNC system.
//
// The VNC system is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the VNC system is not available from the place
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
// the authors on [email protected] for information on obtaining it.
using System;
using System.IO;
using System.Xml;
using System.Windows.Forms;
namespace Vnc.Viewer
{
/// <remarks>Prompts the user for connection details.</remarks>
internal abstract class SessDlg : Form
{
/// <summary>This is default port to connect to.</summary>
private const UInt16 VncPort = 5900;
/// <summary>This is default port to listen on.</summary>
private const UInt16 VncListenPort = 5500;
private ConnMode connMode = ConnMode.Active;
private string host = null;
private int port = -1;
private string passwd = null;
protected ViewOpts viewOpts = null;
protected Label servLbl = new Label();
protected TextBox remoteEndPt = new TextBox();
protected Label remoteEndPtLbl = new Label();
protected Label passwdLbl = new Label();
protected TextBox passwdBox = new TextBox();
protected Label recentLbl = new Label();
protected CheckBox listenBox = new CheckBox();
protected Label listenPortLbl = new Label();
protected TextBox listenPortBox = new TextBox();
protected CheckBox fullScrnBox = new CheckBox();
protected Label rotateLbl = new Label();
protected ComboBox rotateBox = new ComboBox();
protected Label pixelSizeLbl = new Label();
protected ComboBox pixelSizeBox = new ComboBox();
protected Label cliScalingLbl = new Label();
protected ComboBox cliScalingBox = new ComboBox();
protected Label cliScalingWidthLbl = new Label();
protected TextBox cliScalingWidthBox = new TextBox();
protected Label cliScalingHeightLbl = new Label();
protected TextBox cliScalingHeightBox = new TextBox();
protected Label servScalingLbl = new Label();
protected ComboBox servScalingBox = new ComboBox();
protected CheckBox viewOnlyBox = new CheckBox();
protected CheckBox shareServBox = new CheckBox();
protected CheckBox scrnUpdAlgoBox = new CheckBox();
protected EventHandler okHdr = null;
protected EventHandler cancelHdr = null;
protected EventHandler aboutHdr = null;
protected EventHandler saveDefsHdr = null;
protected EventHandler restoreDefsHdr = null;
internal SessDlg() : base()
{
viewOpts = new ViewOpts();
SetupHdrs();
}
internal SessDlg(ViewOpts viewOpts) : base()
{
this.viewOpts = viewOpts;
SetupHdrs();
}
private void SetupHdrs()
{
okHdr = new EventHandler(OkClicked);
cancelHdr = new EventHandler(CancelClicked);
aboutHdr = new EventHandler(AboutClicked);
saveDefsHdr = new EventHandler(SaveDefsClicked);
restoreDefsHdr = new EventHandler(RestoreDefsClicked);
}
internal ConnOpts ConnOpts
{
get
{
if(connMode == ConnMode.Active)
return new ConnOpts(host, port, passwd, viewOpts);
else
return new ConnOpts(port, passwd, viewOpts);
}
}
protected ConnMode ConnMode
{
get
{
return listenBox.Checked? ConnMode.Passive : ConnMode.Active;
}
}
private void AboutClicked(object sender, EventArgs e)
{
App.AboutBox();
}
protected void Ok()
{
if(!ValidateCliScaling())
return;
GetConnMode();
if(connMode == ConnMode.Active)
{
if(!ValidateHostPort())
return;
}
else
{
if(!ValidateListenPort())
return;
}
GetPasswd();
GetOptions();
if(connMode == ConnMode.Active)
SaveConnHist();
DialogResult = DialogResult.OK;
}
private void OkClicked(object sender, EventArgs e)
{
Ok();
}
protected void Cancel()
{
DialogResult = DialogResult.Cancel;
}
private void CancelClicked(object sender, EventArgs e)
{
Cancel();
}
protected bool ValidateCliScaling()
{
if(cliScalingBox.Text != App.GetStr("Custom"))
return true;
try
{
if(UInt16.Parse(cliScalingWidthBox.Text) > 0 && UInt16.Parse(cliScalingHeightBox.Text) > 0)
return true;
else
return false;
}
catch(FormatException)
{
MessageBox.Show(App.GetStr("Customized client-side scaling width or height is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
catch(OverflowException)
{
// TODO: Something more descriptive.
MessageBox.Show(App.GetStr("Customized client-side scaling width or height is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
}
protected void GetConnMode()
{
connMode = listenBox.Checked? ConnMode.Passive : ConnMode.Active;
}
protected bool ValidateHostPort()
{
int indexOfColon = remoteEndPt.Text.IndexOf(':');
if(indexOfColon < 0) // Colon not found.
{
if(remoteEndPt.Text.Length > 0)
{
host = remoteEndPt.Text;
port = VncPort;
}
else // Empty string.
{
MessageBox.Show(App.GetStr("Please specify the VNC server to connect to!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
}
// Colon at the beginning or at the end.
else if(indexOfColon == 0 || indexOfColon == remoteEndPt.Text.Length - 1)
{
MessageBox.Show(App.GetStr("Please specify the host and/or the display/port to connect to!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
else
{
// Double colon at the end.
if(indexOfColon == remoteEndPt.Text.Length - 2 && remoteEndPt.Text[indexOfColon + 1] == ':')
{
MessageBox.Show(App.GetStr("Please specify the port to connect to!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
else
{
int portOffset = VncPort;
int portStart = indexOfColon + 1;
if(remoteEndPt.Text[indexOfColon + 1] == ':') // Double colon.
{
portOffset = 0;
portStart = indexOfColon + 2;
}
string portStr = remoteEndPt.Text.Substring(portStart);
try
{
port = Int32.Parse(portStr);
if(port < 0)
throw new OverflowException();
}
catch(FormatException)
{
MessageBox.Show(App.GetStr("The port is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
catch(OverflowException)
{
MessageBox.Show(App.GetStr("The port is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
port += portOffset;
// TODO: Compare this with min/max port numbers.
host = remoteEndPt.Text.Substring(0, indexOfColon);
}
}
return true;
}
protected bool ValidateListenPort()
{
if(listenPortBox.Text.Length > 0)
{
try
{
port = Int32.Parse(listenPortBox.Text);
if(port < 0)
throw new OverflowException();
}
catch(FormatException)
{
MessageBox.Show(App.GetStr("The listening port is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
catch(OverflowException)
{
MessageBox.Show(App.GetStr("The listening port is invalid!"),
Text,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return false;
}
}
else
port = VncListenPort;
return true;
}
protected void GetPasswd()
{
passwd = passwdBox.Text;
}
protected virtual void GetOptions()
{
viewOpts.IsFullScrn = fullScrnBox.Checked;
switch(rotateBox.SelectedIndex)
{
case 0:
viewOpts.Orientation = Orientation.Portrait;
break;
case 1:
viewOpts.Orientation = Orientation.Landscape90;
break;
case 2:
viewOpts.Orientation = Orientation.Landscape270;
break;
case 3:
viewOpts.Orientation = Orientation.Portrait180;
break;
}
switch(pixelSizeBox.SelectedIndex)
{
case 0:
viewOpts.PixelSize = PixelSize.Unspec;
break;
case 1:
viewOpts.PixelSize = PixelSize.Force8Bit;
break;
case 2:
viewOpts.PixelSize = PixelSize.Force16Bit;
break;
}
viewOpts.CliScalingWidth = 0;
viewOpts.CliScalingHeight = 0;
switch(cliScalingBox.SelectedIndex)
{
case 0:
viewOpts.CliScaling = CliScaling.None;
break;
case 1:
viewOpts.CliScaling = CliScaling.Auto;
break;
case 2:
viewOpts.CliScaling = CliScaling.OneHalf;
break;
case 3:
viewOpts.CliScaling = CliScaling.OneThird;
break;
case 4:
viewOpts.CliScaling = CliScaling.OneFourth;
break;
case 5:
viewOpts.CliScaling = CliScaling.OneFifth;
break;
case 6:
viewOpts.CliScaling = CliScaling.Double;
break;
case 7:
viewOpts.CliScaling = CliScaling.Custom;
// Assuming we have validated the input.
viewOpts.CliScalingWidth = UInt16.Parse(cliScalingWidthBox.Text);
viewOpts.CliScalingHeight = UInt16.Parse(cliScalingHeightBox.Text);
break;
}
viewOpts.ServScaling = (ServScaling)servScalingBox.SelectedIndex;
viewOpts.ViewOnly = viewOnlyBox.Checked;
viewOpts.ShareServ = !shareServBox.Checked;
viewOpts.ScrnUpdAlgo = scrnUpdAlgoBox.Checked? ScrnUpdAlgo.Asap : ScrnUpdAlgo.Batch;
}
protected virtual void SetOptions(ViewOpts viewOpts)
{
this.viewOpts = viewOpts;
fullScrnBox.Checked = viewOpts.IsFullScrn;
switch(viewOpts.Orientation)
{
case Orientation.Portrait:
rotateBox.SelectedIndex = 0;
break;
case Orientation.Landscape90:
rotateBox.SelectedIndex = 1;
break;
case Orientation.Portrait180:
rotateBox.SelectedIndex = 3;
break;
case Orientation.Landscape270:
rotateBox.SelectedIndex = 2;
break;
}
switch(viewOpts.PixelSize)
{
case PixelSize.Unspec:
pixelSizeBox.SelectedIndex = 0;
break;
case PixelSize.Force8Bit:
pixelSizeBox.SelectedIndex = 1;
break;
case PixelSize.Force16Bit:
pixelSizeBox.SelectedIndex = 2;
break;
}
cliScalingWidthBox.Text = String.Empty;
cliScalingHeightBox.Text = String.Empty;
switch(viewOpts.CliScaling)
{
case CliScaling.None:
cliScalingBox.SelectedIndex = 0;
break;
case CliScaling.Auto:
cliScalingBox.SelectedIndex = 1;
break;
case CliScaling.OneHalf:
cliScalingBox.SelectedIndex = 2;
break;
case CliScaling.OneThird:
cliScalingBox.SelectedIndex = 3;
break;
case CliScaling.OneFourth:
cliScalingBox.SelectedIndex = 4;
break;
case CliScaling.OneFifth:
cliScalingBox.SelectedIndex = 5;
break;
case CliScaling.Double:
cliScalingBox.SelectedIndex = 6;
break;
case CliScaling.Custom:
cliScalingBox.SelectedIndex = 7;
cliScalingWidthBox.Text = viewOpts.CliScalingWidth.ToString();
cliScalingHeightBox.Text = viewOpts.CliScalingHeight.ToString();
break;
}
servScalingBox.SelectedIndex = (int)viewOpts.ServScaling;
viewOnlyBox.Checked = viewOpts.ViewOnly;
shareServBox.Checked = !viewOpts.ShareServ;
scrnUpdAlgoBox.Checked = viewOpts.ScrnUpdAlgo == ScrnUpdAlgo.Asap;
}
private void SaveConnHist()
{
try
{
ConnHist connHist = new ConnHist(App.ConnHistFileName);
connHist.Add(host + "::" + port);
connHist.Save(App.ConnHistFileName);
}
catch(IOException)
{
MessageBox.Show(App.GetStr("Unable to save the history file!"),
App.GetStr("Error!"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
catch(XmlException)
{
MessageBox.Show(App.GetStr("The history file is corrupted!"),
App.GetStr("Error!"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
protected abstract void AddConnHistEntry(string entry);
private void LoadConnHist()
{
try
{
ConnHist connHist = new ConnHist(App.ConnHistFileName);
for(byte i = 0; i < connHist.Count; i++)
AddConnHistEntry(connHist[i]);
}
catch(XmlException)
{
MessageBox.Show(App.GetStr("The history file is corrupted."),
App.GetStr("Error"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
private void SaveDefsClicked(object sender, EventArgs e)
{
if(!ValidateCliScaling())
return;
GetOptions();
try
{
viewOpts.Save(App.SettingsFileName);
}
catch(IOException)
{
MessageBox.Show(App.GetStr("Unable to save defaults!"),
App.GetStr("Error"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
private void RestoreDefsClicked(object sender, EventArgs e)
{
try
{
viewOpts.Load(App.SettingsFileName);
}
catch(FileNotFoundException)
{
viewOpts = new ViewOpts();
}
catch(IOException)
{
MessageBox.Show(App.GetStr("Unable to read from the setting file!"),
App.GetStr("Error"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return;
}
catch(XmlException)
{
MessageBox.Show(App.GetStr("The setting file is corrupted!"),
App.GetStr("Error"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return;
}
catch(FormatException)
{
MessageBox.Show(App.GetStr("The setting file is corrupted!"),
App.GetStr("Error"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return;
}
SetOptions(viewOpts);
}
private void ListenBoxChanged(object sender, EventArgs e)
{
if(listenBox.Checked)
remoteEndPt.Text = "";
else
listenPortBox.Text = "";
remoteEndPt.Enabled = !listenBox.Checked;
listenPortBox.Enabled = listenBox.Checked;
}
private void CliScalingBoxChanged(object sender, EventArgs e)
{
cliScalingWidthBox.Enabled = cliScalingBox.Text == App.GetStr("Custom");
cliScalingHeightBox.Enabled = cliScalingBox.Text == App.GetStr("Custom");
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ControlBox = false;
MinimizeBox = false;
MaximizeBox = false;
Text = App.GetStr("New VNC Connection");
Menu = new MainMenu();
servLbl.Text = App.GetStr("Server:");
remoteEndPtLbl.Text = App.GetStr("(host:display or host::port)");
passwdLbl.Text = App.GetStr("Password:");
passwdBox.PasswordChar = '*';
recentLbl.Text = App.GetStr("Recent:");
listenBox.Text = App.GetStr("Listen mode");
listenBox.CheckStateChanged += new EventHandler(ListenBoxChanged);
listenPortLbl.Text = App.GetStr("Port:");
listenPortBox.Enabled = false;
fullScrnBox.Text = App.GetStr("Full screen mode");
rotateLbl.Text = App.GetStr("Orientation:");
rotateBox.DropDownStyle = ComboBoxStyle.DropDownList;
rotateBox.Items.Add(App.GetStr("Portrait"));
rotateBox.Items.Add(App.GetStr("Screen rotated clockwise"));
rotateBox.Items.Add(App.GetStr("Screen rotated counter-clockwise"));
rotateBox.Items.Add(App.GetStr("Upside down"));
pixelSizeLbl.Text = App.GetStr("Pixel size:");
pixelSizeBox.DropDownStyle = ComboBoxStyle.DropDownList;
pixelSizeBox.Items.Add(App.GetStr("Server decides"));
pixelSizeBox.Items.Add(App.GetStr("Force 8-bit"));
pixelSizeBox.Items.Add(App.GetStr("Force 16-bit"));
cliScalingLbl.Text = App.GetStr("Client-side scaling:");
cliScalingBox.DropDownStyle = ComboBoxStyle.DropDownList;
cliScalingBox.Items.Add(App.GetStr("None"));
cliScalingBox.Items.Add(App.GetStr("Auto"));
cliScalingBox.Items.Add(App.GetStr("1/2 of server"));
cliScalingBox.Items.Add(App.GetStr("1/3 of server"));
cliScalingBox.Items.Add(App.GetStr("1/4 of server"));
cliScalingBox.Items.Add(App.GetStr("1/5 of server"));
cliScalingBox.Items.Add(App.GetStr("2 of server"));
cliScalingBox.Items.Add(App.GetStr("Custom"));
cliScalingBox.SelectedIndexChanged += new EventHandler(CliScalingBoxChanged);
cliScalingWidthLbl.Text = App.GetStr("Width (pixel):");
cliScalingHeightLbl.Text = App.GetStr("Height (pixel):");
servScalingLbl.Text = App.GetStr("Server-side scaling:");
servScalingBox.DropDownStyle = ComboBoxStyle.DropDownList;
servScalingBox.Items.Add(App.GetStr("Default"));
servScalingBox.Items.Add(App.GetStr("None"));
servScalingBox.Items.Add(App.GetStr("1/2"));
servScalingBox.Items.Add(App.GetStr("1/3"));
servScalingBox.Items.Add(App.GetStr("1/4"));
servScalingBox.Items.Add(App.GetStr("1/5"));
servScalingBox.Items.Add(App.GetStr("1/6"));
servScalingBox.Items.Add(App.GetStr("1/7"));
servScalingBox.Items.Add(App.GetStr("1/8"));
servScalingBox.Items.Add(App.GetStr("1/9"));
viewOnlyBox.Text = App.GetStr("View only (ignore input)");
shareServBox.Text = App.GetStr("Disconnect other viewers upon connect");
scrnUpdAlgoBox.Text = App.GetStr("Update screen ASAP");
SetOptions(viewOpts);
LoadConnHist();
}
}
}