CygwinLoginDialog.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:15k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: CygwinLoginDialog.cs,v 1.2 2005/04/20 08:45:44 okajima Exp $
  4. */
  5. using System;
  6. using System.Drawing;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. using System.ComponentModel;
  10. using System.Windows.Forms;
  11. using Poderosa.Connection;
  12. using Poderosa.ConnectionParam;
  13. using Poderosa.Toolkit;
  14. using EnumDescAttributeT = Poderosa.Toolkit.EnumDescAttribute;
  15. using Poderosa.Communication;
  16. using Poderosa.LocalShell;
  17. namespace Poderosa.Forms
  18. {
  19. internal class LocalShellLoginDialog : System.Windows.Forms.Form, ISocketWithTimeoutClient
  20. {
  21. private string _errorMessage;
  22. private LocalShellTerminalParam _param;
  23. private ConnectionTag _result;
  24. private LocalShellUtil.Connector _connector;
  25. private IntPtr _savedHWND;
  26. private System.Windows.Forms.Label _logTypeLabel;
  27. private ComboBox _logTypeBox;
  28. private System.Windows.Forms.Label _logFileLabel;
  29. private ComboBox _logFileBox;
  30. private Button _selectlogButton;
  31. private CheckBox _advancedOptionCheck;
  32. private GroupBox _advancedOptionGroup;
  33. private System.Windows.Forms.Label _homeDirectoryLabel;
  34. private System.Windows.Forms.Label _shellLabel;
  35. private System.Windows.Forms.TextBox _homeDirectoryBox;
  36. private System.Windows.Forms.TextBox _shellBox;
  37. private System.Windows.Forms.Label _lMessage;
  38. private System.Windows.Forms.Button _okButton;
  39. private System.Windows.Forms.Button _cancelButton;
  40. /// <summary>
  41. /// 昁梫側僨僓僀僫曄悢偱偡丅
  42. /// </summary>
  43. private System.ComponentModel.Container components = null;
  44. public LocalShellLoginDialog()
  45. {
  46. //
  47. // Windows 僼僅乕儉 僨僓僀僫 僒億乕僩偵昁梫偱偡丅
  48. //
  49. InitializeComponent();
  50. //
  51. // TODO: InitializeComponent 屇傃弌偟偺屻偵丄僐儞僗僩儔僋僞 僐乕僪傪捛壛偟偰偔偩偝偄丅
  52. //
  53. this._okButton.Text = GApp.Strings.GetString("Common.OK");
  54. this._cancelButton.Text = GApp.Strings.GetString("Common.Cancel");
  55. this._homeDirectoryLabel.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._homeDirectoryLabel");
  56. this._lMessage.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._lMessage");
  57. this._advancedOptionCheck.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._advancedOptionCheck");
  58. this._shellLabel.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._shellLabel");
  59. this._logFileLabel.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._logFileLabel");
  60. this._logTypeLabel.Text = GApp.Strings.GetString("Form.CygwinLoginDialog._logTypeLabel");
  61. }
  62. public ConnectionTag Result {
  63. get {
  64. return _result;
  65. }
  66. }
  67. /// <summary>
  68. /// 巊梡偝傟偰偄傞儕僜乕僗偵屻張棟傪幚峴偟傑偡丅
  69. /// </summary>
  70. protected override void Dispose( bool disposing )
  71. {
  72. if( disposing )
  73. {
  74. if(components != null)
  75. {
  76. components.Dispose();
  77. }
  78. }
  79. base.Dispose( disposing );
  80. }
  81. #region Windows 僼僅乕儉 僨僓僀僫偱惗惉偝傟偨僐乕僪 
  82. /// <summary>
  83. /// 僨僓僀僫 僒億乕僩偵昁梫側儊僜僢僪偱偡丅偙偺儊僜僢僪偺撪梕傪
  84. /// 僐乕僪 僄僨傿僞偱曄峏偟側偄偱偔偩偝偄丅
  85. /// </summary>
  86. private void InitializeComponent()
  87. {
  88. this._okButton = new System.Windows.Forms.Button();
  89. this._cancelButton = new System.Windows.Forms.Button();
  90. this._logTypeBox = new ComboBox();
  91. this._logTypeLabel = new System.Windows.Forms.Label();
  92. this._logFileBox = new ComboBox();
  93. this._logFileLabel = new System.Windows.Forms.Label();
  94. this._selectlogButton = new Button();
  95. this._advancedOptionCheck = new CheckBox();
  96. this._advancedOptionGroup = new GroupBox();
  97. this._homeDirectoryLabel = new System.Windows.Forms.Label();
  98. this._homeDirectoryBox = new System.Windows.Forms.TextBox();
  99. this._shellLabel = new System.Windows.Forms.Label();
  100. this._shellBox = new System.Windows.Forms.TextBox();
  101. this._lMessage = new System.Windows.Forms.Label();
  102. this.SuspendLayout();
  103. // 
  104. // _okButton
  105. // 
  106. this._okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  107. this._okButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
  108. this._okButton.Location = new System.Drawing.Point(144, 176);
  109. this._okButton.Name = "_okButton";
  110. this._okButton.TabIndex = 0;
  111. this._okButton.Click += new System.EventHandler(this.OnOK);
  112. // 
  113. // _cancelButton
  114. // 
  115. this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  116. this._cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
  117. this._cancelButton.Location = new System.Drawing.Point(232, 176);
  118. this._cancelButton.Name = "_cancelButton";
  119. this._cancelButton.TabIndex = 1;
  120. // 
  121. // _logTypeLabel
  122. // 
  123. this._logTypeLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  124. this._logTypeLabel.Location = new System.Drawing.Point(8, 8);
  125. this._logTypeLabel.Name = "_logTypeLabel";
  126. this._logTypeLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  127. this._logTypeLabel.Size = new System.Drawing.Size(80, 16);
  128. this._logTypeLabel.TabIndex = 2;
  129. this._logTypeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  130. // 
  131. // _logTypeBox
  132. // 
  133. this._logTypeBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  134. this._logTypeBox.Items.AddRange(EnumDescAttributeT.For(typeof(LogType)).DescriptionCollection());
  135. this._logTypeBox.Location = new System.Drawing.Point(104, 8);
  136. this._logTypeBox.Name = "_logTypeBox";
  137. this._logTypeBox.Size = new System.Drawing.Size(96, 20);
  138. this._logTypeBox.TabIndex = 3;
  139. this._logTypeBox.SelectionChangeCommitted += new System.EventHandler(this.OnLogTypeChanged);
  140. // 
  141. // _logFileLabel
  142. // 
  143. this._logFileLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  144. this._logFileLabel.Location = new System.Drawing.Point(8, 32);
  145. this._logFileLabel.Name = "_logFileLabel";
  146. this._logFileLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  147. this._logFileLabel.Size = new System.Drawing.Size(88, 16);
  148. this._logFileLabel.TabIndex = 4;
  149. this._logFileLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  150. // 
  151. // _logFileBox
  152. // 
  153. this._logFileBox.Location = new System.Drawing.Point(104, 32);
  154. this._logFileBox.Name = "_logFileBox";
  155. this._logFileBox.Size = new System.Drawing.Size(160, 20);
  156. this._logFileBox.TabIndex = 5;
  157. // 
  158. // _selectlogButton
  159. // 
  160. this._selectlogButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
  161. this._selectlogButton.ImageIndex = 0;
  162. this._selectlogButton.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  163. this._selectlogButton.Location = new System.Drawing.Point(272, 32);
  164. this._selectlogButton.Name = "_selectlogButton";
  165. this._selectlogButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
  166. this._selectlogButton.Size = new System.Drawing.Size(19, 19);
  167. this._selectlogButton.TabIndex = 6;
  168. this._selectlogButton.Text = "...";
  169. this._selectlogButton.Click += new System.EventHandler(this.SelectLog);
  170. // 
  171. // _advancedOptionCheck
  172. // 
  173. this._advancedOptionCheck.Location = new System.Drawing.Point(20, 56);
  174. this._advancedOptionCheck.Size = new System.Drawing.Size(152, 20);
  175. this._advancedOptionCheck.TabIndex = 7;
  176. this._advancedOptionCheck.FlatStyle = FlatStyle.System;
  177. this._advancedOptionCheck.CheckedChanged += new EventHandler(OnAdvancedOptionCheckedChanged);
  178. // 
  179. // _advancedOptionGroup
  180. // 
  181. this._advancedOptionGroup.Location = new System.Drawing.Point(8, 58);
  182. this._advancedOptionGroup.Size = new System.Drawing.Size(300, 110);
  183. this._advancedOptionGroup.TabIndex = 8;
  184. this._advancedOptionGroup.Enabled = false;
  185. this._advancedOptionGroup.FlatStyle = FlatStyle.System;
  186. // 
  187. // _homeDirectoryLabel
  188. // 
  189. this._homeDirectoryLabel.Location = new System.Drawing.Point(8, 24);
  190. this._homeDirectoryLabel.Name = "_homeDirectoryLabel";
  191. this._homeDirectoryLabel.Size = new System.Drawing.Size(112, 23);
  192. this._homeDirectoryLabel.TabIndex = 9;
  193. this._homeDirectoryLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  194. // 
  195. // _homeDirectoryBox
  196. // 
  197. this._homeDirectoryBox.Location = new System.Drawing.Point(120, 24);
  198. this._homeDirectoryBox.Name = "_homeDirectoryBox";
  199. this._homeDirectoryBox.Size = new System.Drawing.Size(172, 19);
  200. this._homeDirectoryBox.TabIndex = 10;
  201. this._homeDirectoryBox.Text = "";
  202. // 
  203. // _shellLabel
  204. // 
  205. this._shellLabel.Location = new System.Drawing.Point(8, 48);
  206. this._shellLabel.Name = "_shellLabel";
  207. this._shellLabel.TabIndex = 11;
  208. this._shellLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  209. // 
  210. // _shellBox
  211. // 
  212. this._shellBox.Location = new System.Drawing.Point(120, 48);
  213. this._shellBox.Name = "_shellBox";
  214. this._shellBox.Size = new System.Drawing.Size(172, 19);
  215. this._shellBox.TabIndex = 12;
  216. this._shellBox.Text = "";
  217. // 
  218. // _lMessage
  219. // 
  220. this._lMessage.Location = new System.Drawing.Point(8, 72);
  221. this._lMessage.Name = "_lMessage";
  222. this._lMessage.Size = new System.Drawing.Size(288, 32);
  223. this._lMessage.TabIndex = 6;
  224. this._lMessage.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  225. // 
  226. // CygwinLoginDialog
  227. // 
  228. _advancedOptionGroup.Controls.Add(this._lMessage);
  229. _advancedOptionGroup.Controls.Add(this._shellBox);
  230. _advancedOptionGroup.Controls.Add(this._shellLabel);
  231. _advancedOptionGroup.Controls.Add(this._homeDirectoryBox);
  232. _advancedOptionGroup.Controls.Add(this._homeDirectoryLabel);
  233. this.AcceptButton = this._okButton;
  234. this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
  235. this.CancelButton = this._cancelButton;
  236. this.ClientSize = new System.Drawing.Size(314, 208);
  237. this.Controls.Add(this._cancelButton);
  238. this.Controls.Add(this._okButton);
  239. this.Controls.Add(this._logTypeLabel);
  240. this.Controls.Add(this._logTypeBox);
  241. this.Controls.Add(this._logFileLabel);
  242. this.Controls.Add(this._logFileBox);
  243. this.Controls.Add(this._selectlogButton);
  244. this.Controls.Add(this._advancedOptionCheck);
  245. this.Controls.Add(this._advancedOptionGroup);
  246. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  247. this.MaximizeBox = false;
  248. this.MinimizeBox = false;
  249. this.Name = "CygwinLoginDialog";
  250. this.ShowInTaskbar = false;
  251. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  252. this.ResumeLayout(false);
  253. }
  254. #endregion
  255. public void ApplyParam(LocalShellTerminalParam param) {
  256. _param = param;
  257. if(param is CygwinTerminalParam)
  258. this.Text = GApp.Strings.GetString("Form.CygwinLoginDialog.TextCygwin");
  259. else
  260. this.Text = GApp.Strings.GetString("Form.CygwinLoginDialog.TextSFU");
  261. }
  262. protected override void OnLoad(EventArgs e) {
  263. base.OnLoad (e);
  264. if(_param==null)
  265. _param = new CygwinTerminalParam(); //僨僼僅儖僩抣偱杽傔傞
  266. else
  267. _param = (LocalShellTerminalParam)_param.Clone();
  268. _param.RenderProfile = null;
  269. _param.Caption = null;
  270. _homeDirectoryBox.Text = _param.Home;
  271. _shellBox.Text = _param.Shell;
  272. StringCollection c = GApp.ConnectionHistory.LogPaths;
  273. foreach(string p in c) _logFileBox.Items.Add(p);
  274. if(GApp.Options.DefaultLogType!=LogType.None) {
  275. _logTypeBox.SelectedIndex = (int)GApp.Options.DefaultLogType;
  276. string t = GUtil.CreateLogFileName("cygwin");
  277. _logFileBox.Items.Add(t);
  278. _logFileBox.Text = t;
  279. }
  280. else
  281. _logTypeBox.SelectedIndex = 0;
  282. AdjustUI();
  283. }
  284. private void OnOK(object sender, EventArgs args) {
  285. this.DialogResult = DialogResult.None;
  286. if(_homeDirectoryBox.Text.Length==0)
  287. GUtil.Warning(this, GApp.Strings.GetString("Message.CygwinLoginDialog.EmptyHomeDirectory"));
  288. else if(_shellBox.Text.Length==0)
  289. GUtil.Warning(this, GApp.Strings.GetString("Message.CygwinLoginDialog.EmptyShell"));
  290. _param.LogType = (LogType)EnumDescAttributeT.For(typeof(LogType)).FromDescription(_logTypeBox.Text, LogType.None);
  291. if(_param.LogType!=LogType.None) {
  292. _param.LogPath = _logFileBox.Text;
  293. LogFileCheckResult r = GCUtil.CheckLogFileName(_param.LogPath, this);
  294. if(r==LogFileCheckResult.Cancel || r==LogFileCheckResult.Error) return;
  295. _param.LogAppend = (r==LogFileCheckResult.Append);
  296. }
  297. _param.Home = _homeDirectoryBox.Text;
  298. _param.Shell = _shellBox.Text;
  299. _okButton.Enabled = false;
  300. _cancelButton.Enabled = false;
  301. this.Cursor = Cursors.WaitCursor;
  302. _savedHWND = this.Handle;
  303. if(_param is CygwinTerminalParam)
  304. this.Text = GApp.Strings.GetString("Caption.CygwinLoginDialog.ConnectingCygwin");
  305. else
  306. this.Text = GApp.Strings.GetString("Caption.CygwinLoginDialog.ConnectingSFU");
  307. _connector = CygwinUtil.AsyncPrepareSocket(this, _param);
  308. if(_connector==null) ClearConnectingState();
  309. }
  310. private void OnAdvancedOptionCheckedChanged(object sender, EventArgs args) {
  311. _advancedOptionGroup.Enabled = _advancedOptionCheck.Checked;
  312. }
  313. private void OnLogTypeChanged(object sender, System.EventArgs args) {
  314. AdjustUI();
  315. }
  316. private void AdjustUI() {
  317. bool e = _logTypeBox.SelectedIndex!=(int)LogType.None;
  318. _logFileBox.Enabled = e;
  319. _selectlogButton.Enabled = e;
  320. }
  321. private void SelectLog(object sender, System.EventArgs e) {
  322. string fn = GCUtil.SelectLogFileByDialog(this);
  323. if(fn!=null) _logFileBox.Text = fn;
  324. }
  325. private void ShowError(string msg) {
  326. GUtil.Warning(this, msg, GApp.Strings.GetString("Message.CygwinLoginDialog.ConnectionError"));
  327. }
  328. protected override bool ProcessDialogKey(Keys key) {
  329. if(_connector!=null && key==(Keys.Control | Keys.C)) {
  330. _connector.Interrupt();
  331. ClearConnectingState();
  332. return true;
  333. }
  334. else
  335. return base.ProcessDialogKey(key);
  336. }
  337. private void ClearConnectingState() {
  338. _okButton.Enabled = true;
  339. _cancelButton.Enabled = true;
  340. this.Cursor = Cursors.Default;
  341. this.Text = GApp.Strings.GetString("Form.CygwinLoginDialog.Text");
  342. _connector = null;
  343. }
  344. protected override void WndProc(ref Message msg) {
  345. base.WndProc(ref msg);
  346. if(msg.Msg==GConst.WMG_ASYNCCONNECT) {
  347. if(msg.LParam.ToInt32()==1) {
  348. this.DialogResult = DialogResult.OK;
  349. Close();
  350. }
  351. else {
  352. ClearConnectingState();
  353. ShowError(_errorMessage);
  354. }
  355. }
  356. }
  357. //ISocketWithTimeoutClient 偙傟傜偼偙偺僂傿儞僪僂偲偼暿偺僗儗僢僪偱幚峴偝傟傞偺偱怲廳偵
  358. public void SuccessfullyExit(object result) {
  359. _result = (ConnectionTag)result;
  360. //_result.SetServerInfo(((TCPTerminalParam)_result.Param).Host, swt.IPAddress);
  361. Win32.SendMessage(_savedHWND, GConst.WMG_ASYNCCONNECT, IntPtr.Zero, new IntPtr(1));
  362. }
  363. public void ConnectionFailed(string message) {
  364. _errorMessage = message;
  365. Win32.SendMessage(_savedHWND, GConst.WMG_ASYNCCONNECT, IntPtr.Zero, IntPtr.Zero);
  366. }
  367. public void CancelTimer() {
  368. }
  369. public IWin32Window GetWindow() {
  370. return this;
  371. }
  372. }
  373. }