OSDN Git Service

9d01785e04f14fb7f2f304b2f32364ad309ba08c
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ConfigDialog.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.Collections.Generic;\r
20 using System.Diagnostics;\r
21 using System.IO;\r
22 using System.Linq;\r
23 using System.Net;\r
24 using System.Windows.Forms;\r
25 \r
26 namespace KancolleSniffer\r
27 {\r
28     public partial class ConfigDialog : Form\r
29     {\r
30         private readonly Config _config;\r
31         private readonly MainForm _main;\r
32         private readonly Dictionary<string, string> _soundSetting = new Dictionary<string, string>();\r
33         private const string Home = "http://kancollesniffer.sourceforge.jp/";\r
34 \r
35         public ConfigDialog(Config config, MainForm main)\r
36         {\r
37             InitializeComponent();\r
38             _config = config;\r
39             _main = main;\r
40             listBoxSoundFile.Items.AddRange(new object[]\r
41             {\r
42                 "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
43                 "大破警告", "泊地修理20分経過", "泊地修理進行", "疲労回復"\r
44             });\r
45             numericUpDownMaterialLogInterval.Maximum = 1440;\r
46         }\r
47 \r
48         private void ConfigDialog_Load(object sender, EventArgs e)\r
49         {\r
50             var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));\r
51             labelVersion.Text = "バージョン" + version;\r
52             SetLatestVersion(version);\r
53 \r
54             checkBoxTopMost.Checked = _config.TopMost;\r
55             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
56             checkBoxFlash.Checked = _config.FlashWindow;\r
57             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
58             checkBoxSound.Checked = _config.PlaySound;\r
59             numericUpDownMarginShips.Value = _config.MarginShips;\r
60             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
61             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
62             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
63             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
64             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
65 \r
66             numericUpDownSoundVolume.Value = _config.SoundVolume;\r
67 \r
68             _soundSetting["遠征終了"] = _config.MissionSoundFile;\r
69             _soundSetting["入渠終了"] = _config.NDockSoundFile;\r
70             _soundSetting["建造完了"] = _config.KDockSoundFile;\r
71             _soundSetting["艦娘数超過"] = _config.MaxShipsSoundFile;\r
72             _soundSetting["装備数超過"] = _config.MaxEquipsSoundFile;\r
73             _soundSetting["大破警告"] = _config.DamagedShipSoundFile;\r
74             _soundSetting["泊地修理20分経過"] = _config.Akashi20MinSoundFile;\r
75             _soundSetting["泊地修理進行"] = _config.AkashiProgressSoundFile;\r
76             _soundSetting["疲労回復"] = _config.ConditionSoundFile;\r
77 \r
78             listBoxSoundFile.SelectedIndex = -1;\r
79             listBoxSoundFile.SelectedIndex = 0;\r
80 \r
81             LoadProxySettings();\r
82             LoadLogSettings();\r
83             LoadDebugSettings();\r
84         }\r
85 \r
86         private void LoadProxySettings()\r
87         {\r
88             // 見えていないTabPage上でPerformClickは使えない。\r
89             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
90             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
91             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
92             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
93             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
94             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
95         }\r
96 \r
97         private void LoadLogSettings()\r
98         {\r
99             checkBoxOutput.Checked = _config.Log.On;\r
100             textBoxOutput.Text = _config.Log.OutputDir;\r
101             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
102             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
103             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
104             radioButtonServerOn.Checked = _config.Log.ServerOn;\r
105             radioButtonServerOff.Checked = !_config.Log.ServerOn;\r
106             textBoxServer.Text = _config.Log.Listen.ToString("D");\r
107         }\r
108 \r
109         private void LoadDebugSettings()\r
110         {\r
111             checkBoxDebugLog.Checked = _config.DebugLogging;\r
112             textBoxDebugLog.Text = _config.DebugLogFile;\r
113         }\r
114 \r
115         private async void SetLatestVersion(string version)\r
116         {\r
117             try\r
118             {\r
119                 var req = WebRequest.Create(Home + "version");\r
120                 var response = await req.GetResponseAsync();\r
121                 var stream = response.GetResponseStream();\r
122                 if (stream == null)\r
123                     return;\r
124                 using (var reader = new StreamReader(stream))\r
125                 {\r
126                     var str = await reader.ReadLineAsync();\r
127                     Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
128                 }\r
129             }\r
130             catch (WebException)\r
131             {\r
132             }\r
133         }\r
134 \r
135         private void buttonOk_Click(object sender, EventArgs e)\r
136         {\r
137             int listen, outbound, server;\r
138             if (!ValidateProxyPorts(out listen, out outbound) || !ValidateServerPort(out server))\r
139                 return;\r
140             DialogResult = DialogResult.OK;\r
141 \r
142             ApplyProxySettings(listen, outbound);\r
143             ApplyLogSettings(server);\r
144             ApplyDebugSettings();\r
145 \r
146             _config.TopMost = checkBoxTopMost.Checked;\r
147             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
148             _config.FlashWindow = checkBoxFlash.Checked;\r
149             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
150             _config.PlaySound = checkBoxSound.Checked;\r
151             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
152             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
153 \r
154             _config.NotifyConditions.Clear();\r
155             if (checkBoxCond40.Checked)\r
156                 _config.NotifyConditions.Add(40);\r
157             if (checkBoxCond49.Checked)\r
158                 _config.NotifyConditions.Add(49);\r
159 \r
160             _config.ResetHours.Clear();\r
161             if (checkBoxReset02.Checked)\r
162                 _config.ResetHours.Add(2);\r
163             if (checkBoxReset14.Checked)\r
164                 _config.ResetHours.Add(14);\r
165 \r
166             _config.SoundVolume = (int)numericUpDownSoundVolume.Value;\r
167 \r
168             _config.MissionSoundFile = _soundSetting["遠征終了"];\r
169             _config.NDockSoundFile = _soundSetting["入渠終了"];\r
170             _config.KDockSoundFile = _soundSetting["建造完了"];\r
171             _config.MaxShipsSoundFile = _soundSetting["艦娘数超過"];\r
172             _config.MaxEquipsSoundFile = _soundSetting["装備数超過"];\r
173             _config.DamagedShipSoundFile = _soundSetting["大破警告"];\r
174             _config.Akashi20MinSoundFile = _soundSetting["泊地修理20分経過"];\r
175             _config.AkashiProgressSoundFile = _soundSetting["泊地修理進行"];\r
176             _config.ConditionSoundFile = _soundSetting["疲労回復"];\r
177         }\r
178 \r
179         private bool ValidateProxyPorts(out int listen, out int outbound)\r
180         {\r
181             listen = -1;\r
182             outbound = -1;\r
183             if (radioButtonAutoConfigOff.Checked && !ValidatePortNumber(textBoxListen, out listen))\r
184                 return false;\r
185             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
186                 return false;\r
187             if (radioButtonAutoConfigOff.Checked && radioButtonUpstreamOn.Checked && listen == outbound)\r
188             {\r
189                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
190                 return false;\r
191             }\r
192             return true;\r
193         }\r
194 \r
195         private void ApplyProxySettings(int listen, int port)\r
196         {\r
197             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
198             if (!_config.Proxy.Auto)\r
199                 _config.Proxy.Listen = listen;\r
200             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
201             if (_config.Proxy.UseUpstream)\r
202                 _config.Proxy.UpstreamPort = port;\r
203             _main.ApplyProxySetting();\r
204         }\r
205 \r
206         private bool ValidateServerPort(out int server)\r
207         {\r
208             server = -1;\r
209             return !radioButtonServerOn.Checked || ValidatePortNumber(textBoxServer, out server);\r
210         }\r
211 \r
212         private void ApplyLogSettings(int server)\r
213         {\r
214             _config.Log.On = checkBoxOutput.Checked;\r
215             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
216             _config.Log.OutputDir = textBoxOutput.Text;\r
217             _config.Log.ServerOn = radioButtonServerOn.Checked;\r
218             if (_config.Log.ServerOn)\r
219                 _config.Log.Listen = server;\r
220             _main.ApplyLogSetting();\r
221         }\r
222 \r
223         private void ApplyDebugSettings()\r
224         {\r
225             _config.DebugLogging = checkBoxDebugLog.Checked;\r
226             _config.DebugLogFile = textBoxDebugLog.Text;\r
227             _main.ApplyDebugLogSetting();\r
228         }\r
229 \r
230         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
231         {\r
232             _soundSetting[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
233         }\r
234 \r
235         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
236         {\r
237             if (listBoxSoundFile.SelectedItem == null)\r
238                 return;\r
239             textBoxSoundFile.Text = _soundSetting[(string)listBoxSoundFile.SelectedItem];\r
240             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
241         }\r
242 \r
243         private void buttonOpenFile_Click(object sender, EventArgs e)\r
244         {\r
245             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
246             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
247             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
248                 return;\r
249             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
250             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
251         }\r
252 \r
253         private void buttonPlay_Click(object sender, EventArgs e)\r
254         {\r
255             _main.PlaySound(_soundSetting[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
256         }\r
257 \r
258         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
259         {\r
260             _main.ResetAchievemnt();\r
261         }\r
262 \r
263         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
264         {\r
265             linkLabelProductName.LinkVisited = true;\r
266             Process.Start(Home);\r
267         }\r
268 \r
269         private void radioButtonAutoConfigOn_CheckedChanged(object sender, EventArgs e)\r
270         {\r
271             var on = ((RadioButton)sender).Checked;\r
272             textBoxListen.Enabled = !on;\r
273             labelListen.Enabled = !on;\r
274         }\r
275 \r
276         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
277         {\r
278             var off = ((RadioButton)sender).Checked;\r
279             textBoxPort.Enabled = !off;\r
280         }\r
281 \r
282         private void radioButtonServerOn_CheckedChanged(object sender, EventArgs e)\r
283         {\r
284             var on = ((RadioButton)sender).Checked;\r
285             textBoxListen.Enabled = on;\r
286             labelListen.Enabled = on;\r
287         }\r
288 \r
289         private void buttonOutputDir_Click(object sender, EventArgs e)\r
290         {\r
291             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
292                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
293             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
294         }\r
295 \r
296         private bool ValidatePortNumber(TextBox textBox, out int result)\r
297         {\r
298             var s = textBox.Text;\r
299             if (!int.TryParse(s, out result))\r
300             {\r
301                 ShowToolTip("数字を入力してください。", textBox);\r
302                 return false;\r
303             }\r
304             if (result <= 0)\r
305             {\r
306                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
307                 return false;\r
308             }\r
309             return true;\r
310         }\r
311 \r
312         private void ShowToolTip(string message, Control control)\r
313         {\r
314             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
315             toolTipError.Show(message, control, 0, control.Height, 3000);\r
316         }\r
317 \r
318         private void textBox_Enter(object sender, EventArgs e)\r
319         {\r
320             toolTipError.Hide((Control)sender);\r
321         }\r
322 \r
323         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
324         {\r
325             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
326             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
327             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
328                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
329             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
330         }\r
331 \r
332         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
333         {\r
334             _main.SetPlayLog(textBoxDebugLog.Text);\r
335             DialogResult = DialogResult.Cancel;\r
336         }\r
337     }\r
338 }