OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / Setting / Panel / ProxyPanel.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2014      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.ComponentModel;
32 using System.Drawing;
33 using System.Data;
34 using System.Linq;
35 using System.Text;
36 using System.Windows.Forms;
37 using OpenTween.Connection;
38
39 namespace OpenTween.Setting.Panel
40 {
41     public partial class ProxyPanel : SettingPanelBase
42     {
43         public ProxyPanel()
44             => this.InitializeComponent();
45
46         public void LoadConfig(SettingLocal settingLocal)
47         {
48             switch (settingLocal.ProxyType)
49             {
50                 case ProxyType.None:
51                     this.RadioProxyNone.Checked = true;
52                     break;
53                 case ProxyType.IE:
54                     this.RadioProxyIE.Checked = true;
55                     break;
56                 default:
57                     this.RadioProxySpecified.Checked = true;
58                     break;
59             }
60
61             var chk = this.RadioProxySpecified.Checked;
62             this.LabelProxyAddress.Enabled = chk;
63             this.TextProxyAddress.Enabled = chk;
64             this.LabelProxyPort.Enabled = chk;
65             this.TextProxyPort.Enabled = chk;
66             this.LabelProxyUser.Enabled = chk;
67             this.TextProxyUser.Enabled = chk;
68             this.LabelProxyPassword.Enabled = chk;
69             this.TextProxyPassword.Enabled = chk;
70
71             this.TextProxyAddress.Text = settingLocal.ProxyAddress;
72             this.TextProxyPort.Text = settingLocal.ProxyPort.ToString();
73             this.TextProxyUser.Text = settingLocal.ProxyUser;
74             this.TextProxyPassword.Text = settingLocal.ProxyPassword;
75         }
76
77         public void SaveConfig(SettingLocal settingLocal)
78         {
79             if (this.RadioProxyNone.Checked)
80             {
81                 settingLocal.ProxyType = ProxyType.None;
82             }
83             else if (this.RadioProxyIE.Checked)
84             {
85                 settingLocal.ProxyType = ProxyType.IE;
86             }
87             else
88             {
89                 settingLocal.ProxyType = ProxyType.Specified;
90             }
91
92             settingLocal.ProxyAddress = this.TextProxyAddress.Text.Trim();
93             settingLocal.ProxyPort = int.Parse(this.TextProxyPort.Text.Trim());
94             settingLocal.ProxyUser = this.TextProxyUser.Text.Trim();
95             settingLocal.ProxyPassword = this.TextProxyPassword.Text.Trim();
96         }
97
98         private void RadioProxySpecified_CheckedChanged(object sender, EventArgs e)
99         {
100             var chk = RadioProxySpecified.Checked;
101             LabelProxyAddress.Enabled = chk;
102             TextProxyAddress.Enabled = chk;
103             LabelProxyPort.Enabled = chk;
104             TextProxyPort.Enabled = chk;
105             LabelProxyUser.Enabled = chk;
106             TextProxyUser.Enabled = chk;
107             LabelProxyPassword.Enabled = chk;
108             TextProxyPassword.Enabled = chk;
109         }
110
111         private void TextProxyPort_Validating(object sender, CancelEventArgs e)
112         {
113             if (string.IsNullOrWhiteSpace(TextProxyPort.Text)) TextProxyPort.Text = "0";
114             if (int.TryParse(TextProxyPort.Text.Trim(), out var port) == false)
115             {
116                 MessageBox.Show(Properties.Resources.TextProxyPort_ValidatingText1);
117                 e.Cancel = true;
118                 return;
119             }
120             if (port < 0 || port > 65535)
121             {
122                 MessageBox.Show(Properties.Resources.TextProxyPort_ValidatingText2);
123                 e.Cancel = true;
124                 return;
125             }
126         }
127     }
128 }