OSDN Git Service

ズーム125%で戦果が100超のときに表示があふれるのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ConfigDialog.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Diagnostics;\r
18 using System.IO;\r
19 using System.Linq;\r
20 using System.Net;\r
21 using System.Windows.Forms;\r
22 \r
23 namespace KancolleSniffer\r
24 {\r
25     public partial class ConfigDialog : Form\r
26     {\r
27         private readonly Config _config;\r
28         private readonly MainForm _main;\r
29         private readonly Dictionary<string, string> _soundSetting = new Dictionary<string, string>();\r
30         private const string Home = "http://kancollesniffer.osdn.jp/";\r
31 \r
32         public ConfigDialog(Config config, MainForm main)\r
33         {\r
34             InitializeComponent();\r
35             _config = config;\r
36             _main = main;\r
37             // ReSharper disable once CoVariantArrayConversion\r
38             listBoxSoundFile.Items.AddRange(_config.Sounds.SoundNames);\r
39             numericUpDownMaterialLogInterval.Maximum = 1440;\r
40         }\r
41 \r
42         private void ConfigDialog_Load(object sender, EventArgs e)\r
43         {\r
44             var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));\r
45             labelVersion.Text = "バージョン" + version;\r
46             SetLatestVersion(version);\r
47             labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
48 \r
49             checkBoxTopMost.Checked = _config.TopMost;\r
50             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
51             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
52             checkBoxFlash.Checked = _config.FlashWindow;\r
53             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
54             checkBoxSound.Checked = _config.PlaySound;\r
55             numericUpDownMarginShips.Value = _config.MarginShips;\r
56             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
57             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
58             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
59             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
60             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
61             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
62             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
63             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
64 \r
65             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
66             foreach (var name in _config.Sounds.SoundNames)\r
67                 _soundSetting[name] = _config.Sounds[name];\r
68             listBoxSoundFile.SelectedIndex = -1;\r
69             listBoxSoundFile.SelectedIndex = 0;\r
70 \r
71             LoadProxySettings();\r
72             LoadLogSettings();\r
73             LoadDebugSettings();\r
74             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
75             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
76         }\r
77 \r
78         private void LoadProxySettings()\r
79         {\r
80             // 見えていないTabPage上でPerformClickは使えない。\r
81             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
82             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
83             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
84             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
85             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
86             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
87         }\r
88 \r
89         private void LoadLogSettings()\r
90         {\r
91             checkBoxOutput.Checked = _config.Log.On;\r
92             textBoxOutput.Text = _config.Log.OutputDir;\r
93             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
94             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
95             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
96         }\r
97 \r
98         private void LoadDebugSettings()\r
99         {\r
100             checkBoxDebugLog.Checked = _config.DebugLogging;\r
101             textBoxDebugLog.Text = _config.DebugLogFile;\r
102         }\r
103 \r
104         private async void SetLatestVersion(string version)\r
105         {\r
106             try\r
107             {\r
108                 var req = WebRequest.Create(Home + "version");\r
109                 var response = await req.GetResponseAsync();\r
110                 var stream = response.GetResponseStream();\r
111                 if (stream == null)\r
112                     return;\r
113                 using (var reader = new StreamReader(stream))\r
114                 {\r
115                     var str = await reader.ReadLineAsync();\r
116                     Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
117                 }\r
118             }\r
119             catch (WebException)\r
120             {\r
121             }\r
122         }\r
123 \r
124         private void buttonOk_Click(object sender, EventArgs e)\r
125         {\r
126             int listen, outbound, server;\r
127             if (!ValidatePorts(out listen, out outbound, out server))\r
128                 return;\r
129             DialogResult = DialogResult.OK;\r
130             if (!ApplyProxySettings(listen, outbound))\r
131                 DialogResult = DialogResult.None;\r
132             ApplyLogSettings();\r
133             ApplyDebugSettings();\r
134             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
135             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
136 \r
137             _config.TopMost = checkBoxTopMost.Checked;\r
138             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
139             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
140             _config.FlashWindow = checkBoxFlash.Checked;\r
141             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
142             _config.PlaySound = checkBoxSound.Checked;\r
143             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
144             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
145 \r
146             _config.NotifyConditions.Clear();\r
147             if (checkBoxCond40.Checked)\r
148                 _config.NotifyConditions.Add(40);\r
149             if (checkBoxCond49.Checked)\r
150                 _config.NotifyConditions.Add(49);\r
151 \r
152             _config.ResetHours.Clear();\r
153             if (checkBoxReset02.Checked)\r
154                 _config.ResetHours.Add(2);\r
155             if (checkBoxReset14.Checked)\r
156                 _config.ResetHours.Add(14);\r
157 \r
158             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
159             _main.UpdateFighterPower();\r
160             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
161 \r
162             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
163             foreach (var name in _config.Sounds.SoundNames)\r
164                 _config.Sounds[name] = _soundSetting[name];\r
165         }\r
166 \r
167         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
168         {\r
169             outbound = -1;\r
170             server = -1;\r
171             if (!ValidatePortNumber(textBoxListen, out listen))\r
172                 return false;\r
173             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
174                 return false;\r
175             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
176             {\r
177                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
178                 return false;\r
179             }\r
180             return true;\r
181         }\r
182 \r
183         private bool ApplyProxySettings(int listen, int port)\r
184         {\r
185             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
186             _config.Proxy.Listen = listen;\r
187             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
188             if (_config.Proxy.UseUpstream)\r
189                 _config.Proxy.UpstreamPort = port;\r
190             if (!_main.ApplyProxySetting())\r
191                 return false;\r
192             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
193             return true;\r
194         }\r
195 \r
196         private void ApplyLogSettings()\r
197         {\r
198             _config.Log.On = checkBoxOutput.Checked;\r
199             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
200             _config.Log.OutputDir = textBoxOutput.Text;\r
201             _main.ApplyLogSetting();\r
202         }\r
203 \r
204         private void ApplyDebugSettings()\r
205         {\r
206             _config.DebugLogging = checkBoxDebugLog.Checked;\r
207             _config.DebugLogFile = textBoxDebugLog.Text;\r
208             _main.ApplyDebugLogSetting();\r
209         }\r
210 \r
211         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
212         {\r
213             _soundSetting[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
214         }\r
215 \r
216         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
217         {\r
218             if (listBoxSoundFile.SelectedItem == null)\r
219                 return;\r
220             textBoxSoundFile.Text = _soundSetting[(string)listBoxSoundFile.SelectedItem];\r
221             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
222         }\r
223 \r
224         private void buttonOpenFile_Click(object sender, EventArgs e)\r
225         {\r
226             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
227             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
228             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
229                 return;\r
230             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
231             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
232         }\r
233 \r
234         private void buttonPlay_Click(object sender, EventArgs e)\r
235         {\r
236             _main.PlaySound(_soundSetting[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
237         }\r
238 \r
239         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
240         {\r
241             _main.ResetAchievemnt();\r
242         }\r
243 \r
244         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
245         {\r
246             linkLabelProductName.LinkVisited = true;\r
247             Process.Start(Home);\r
248         }\r
249 \r
250         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
251         {\r
252             var off = ((RadioButton)sender).Checked;\r
253             textBoxPort.Enabled = !off;\r
254         }\r
255 \r
256         private void buttonOutputDir_Click(object sender, EventArgs e)\r
257         {\r
258             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
259                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
260             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
261         }\r
262 \r
263         private bool ValidatePortNumber(TextBox textBox, out int result)\r
264         {\r
265             var s = textBox.Text;\r
266             if (!int.TryParse(s, out result))\r
267             {\r
268                 ShowToolTip("数字を入力してください。", textBox);\r
269                 return false;\r
270             }\r
271             if (result <= 0)\r
272             {\r
273                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
274                 return false;\r
275             }\r
276             return true;\r
277         }\r
278 \r
279         private void ShowToolTip(string message, Control control)\r
280         {\r
281             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
282             toolTipError.Show(message, control, 0, control.Height, 3000);\r
283         }\r
284 \r
285         private void textBox_Enter(object sender, EventArgs e)\r
286         {\r
287             toolTipError.Hide((Control)sender);\r
288         }\r
289 \r
290         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
291         {\r
292             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
293             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
294             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
295                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
296             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
297         }\r
298 \r
299         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
300         {\r
301             _main.SetPlayLog(textBoxDebugLog.Text);\r
302             DialogResult = DialogResult.Cancel;\r
303         }\r
304     }\r
305 }