OSDN Git Service

ラベルをクリックしたときだけ勝利判定を表示できるようにする
[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             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
66             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
67 \r
68             numericUpDownSoundVolume.Value = _config.SoundVolume;\r
69 \r
70             _soundSetting["遠征終了"] = _config.MissionSoundFile;\r
71             _soundSetting["入渠終了"] = _config.NDockSoundFile;\r
72             _soundSetting["建造完了"] = _config.KDockSoundFile;\r
73             _soundSetting["艦娘数超過"] = _config.MaxShipsSoundFile;\r
74             _soundSetting["装備数超過"] = _config.MaxEquipsSoundFile;\r
75             _soundSetting["大破警告"] = _config.DamagedShipSoundFile;\r
76             _soundSetting["泊地修理20分経過"] = _config.Akashi20MinSoundFile;\r
77             _soundSetting["泊地修理進行"] = _config.AkashiProgressSoundFile;\r
78             _soundSetting["疲労回復"] = _config.ConditionSoundFile;\r
79 \r
80             listBoxSoundFile.SelectedIndex = -1;\r
81             listBoxSoundFile.SelectedIndex = 0;\r
82 \r
83             LoadProxySettings();\r
84             LoadLogSettings();\r
85             LoadDebugSettings();\r
86         }\r
87 \r
88         private void LoadProxySettings()\r
89         {\r
90             // 見えていないTabPage上でPerformClickは使えない。\r
91             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
92             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
93             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
94             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
95             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
96             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
97         }\r
98 \r
99         private void LoadLogSettings()\r
100         {\r
101             checkBoxOutput.Checked = _config.Log.On;\r
102             textBoxOutput.Text = _config.Log.OutputDir;\r
103             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
104             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
105             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
106             radioButtonServerOn.Checked = _config.Log.ServerOn;\r
107             radioButtonServerOff.Checked = !_config.Log.ServerOn;\r
108             textBoxServer.Text = _config.Log.Listen.ToString("D");\r
109         }\r
110 \r
111         private void LoadDebugSettings()\r
112         {\r
113             checkBoxDebugLog.Checked = _config.DebugLogging;\r
114             textBoxDebugLog.Text = _config.DebugLogFile;\r
115         }\r
116 \r
117         private async void SetLatestVersion(string version)\r
118         {\r
119             try\r
120             {\r
121                 var req = WebRequest.Create(Home + "version");\r
122                 var response = await req.GetResponseAsync();\r
123                 var stream = response.GetResponseStream();\r
124                 if (stream == null)\r
125                     return;\r
126                 using (var reader = new StreamReader(stream))\r
127                 {\r
128                     var str = await reader.ReadLineAsync();\r
129                     Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
130                 }\r
131             }\r
132             catch (WebException)\r
133             {\r
134             }\r
135         }\r
136 \r
137         private void buttonOk_Click(object sender, EventArgs e)\r
138         {\r
139             int listen, outbound, server;\r
140             if (!ValidateProxyPorts(out listen, out outbound) || !ValidateServerPort(out server))\r
141                 return;\r
142             DialogResult = DialogResult.OK;\r
143 \r
144             ApplyProxySettings(listen, outbound);\r
145             ApplyLogSettings(server);\r
146             ApplyDebugSettings();\r
147 \r
148             _config.TopMost = checkBoxTopMost.Checked;\r
149             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
150             _config.FlashWindow = checkBoxFlash.Checked;\r
151             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
152             _config.PlaySound = checkBoxSound.Checked;\r
153             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
154             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
155 \r
156             _config.NotifyConditions.Clear();\r
157             if (checkBoxCond40.Checked)\r
158                 _config.NotifyConditions.Add(40);\r
159             if (checkBoxCond49.Checked)\r
160                 _config.NotifyConditions.Add(49);\r
161 \r
162             _config.ResetHours.Clear();\r
163             if (checkBoxReset02.Checked)\r
164                 _config.ResetHours.Add(2);\r
165             if (checkBoxReset14.Checked)\r
166                 _config.ResetHours.Add(14);\r
167 \r
168             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
169 \r
170             _config.SoundVolume = (int)numericUpDownSoundVolume.Value;\r
171 \r
172             _config.MissionSoundFile = _soundSetting["遠征終了"];\r
173             _config.NDockSoundFile = _soundSetting["入渠終了"];\r
174             _config.KDockSoundFile = _soundSetting["建造完了"];\r
175             _config.MaxShipsSoundFile = _soundSetting["艦娘数超過"];\r
176             _config.MaxEquipsSoundFile = _soundSetting["装備数超過"];\r
177             _config.DamagedShipSoundFile = _soundSetting["大破警告"];\r
178             _config.Akashi20MinSoundFile = _soundSetting["泊地修理20分経過"];\r
179             _config.AkashiProgressSoundFile = _soundSetting["泊地修理進行"];\r
180             _config.ConditionSoundFile = _soundSetting["疲労回復"];\r
181         }\r
182 \r
183         private bool ValidateProxyPorts(out int listen, out int outbound)\r
184         {\r
185             listen = -1;\r
186             outbound = -1;\r
187             if (radioButtonAutoConfigOff.Checked && !ValidatePortNumber(textBoxListen, out listen))\r
188                 return false;\r
189             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
190                 return false;\r
191             if (radioButtonAutoConfigOff.Checked && radioButtonUpstreamOn.Checked && listen == outbound)\r
192             {\r
193                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
194                 return false;\r
195             }\r
196             return true;\r
197         }\r
198 \r
199         private void ApplyProxySettings(int listen, int port)\r
200         {\r
201             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
202             if (!_config.Proxy.Auto)\r
203                 _config.Proxy.Listen = listen;\r
204             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
205             if (_config.Proxy.UseUpstream)\r
206                 _config.Proxy.UpstreamPort = port;\r
207             _main.ApplyProxySetting();\r
208         }\r
209 \r
210         private bool ValidateServerPort(out int server)\r
211         {\r
212             server = -1;\r
213             return !radioButtonServerOn.Checked || ValidatePortNumber(textBoxServer, out server);\r
214         }\r
215 \r
216         private void ApplyLogSettings(int server)\r
217         {\r
218             _config.Log.On = checkBoxOutput.Checked;\r
219             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
220             _config.Log.OutputDir = textBoxOutput.Text;\r
221             _config.Log.ServerOn = radioButtonServerOn.Checked;\r
222             if (_config.Log.ServerOn)\r
223                 _config.Log.Listen = server;\r
224             _main.ApplyLogSetting();\r
225         }\r
226 \r
227         private void ApplyDebugSettings()\r
228         {\r
229             _config.DebugLogging = checkBoxDebugLog.Checked;\r
230             _config.DebugLogFile = textBoxDebugLog.Text;\r
231             _main.ApplyDebugLogSetting();\r
232         }\r
233 \r
234         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
235         {\r
236             _soundSetting[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
237         }\r
238 \r
239         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
240         {\r
241             if (listBoxSoundFile.SelectedItem == null)\r
242                 return;\r
243             textBoxSoundFile.Text = _soundSetting[(string)listBoxSoundFile.SelectedItem];\r
244             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
245         }\r
246 \r
247         private void buttonOpenFile_Click(object sender, EventArgs e)\r
248         {\r
249             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
250             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
251             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
252                 return;\r
253             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
254             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
255         }\r
256 \r
257         private void buttonPlay_Click(object sender, EventArgs e)\r
258         {\r
259             _main.PlaySound(_soundSetting[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
260         }\r
261 \r
262         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
263         {\r
264             _main.ResetAchievemnt();\r
265         }\r
266 \r
267         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
268         {\r
269             linkLabelProductName.LinkVisited = true;\r
270             Process.Start(Home);\r
271         }\r
272 \r
273         private void radioButtonAutoConfigOn_CheckedChanged(object sender, EventArgs e)\r
274         {\r
275             var on = ((RadioButton)sender).Checked;\r
276             textBoxListen.Enabled = !on;\r
277             labelListen.Enabled = !on;\r
278         }\r
279 \r
280         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
281         {\r
282             var off = ((RadioButton)sender).Checked;\r
283             textBoxPort.Enabled = !off;\r
284         }\r
285 \r
286         private void radioButtonServerOn_CheckedChanged(object sender, EventArgs e)\r
287         {\r
288             var on = ((RadioButton)sender).Checked;\r
289             textBoxListen.Enabled = on;\r
290             labelListen.Enabled = on;\r
291         }\r
292 \r
293         private void buttonOutputDir_Click(object sender, EventArgs e)\r
294         {\r
295             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
296                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
297             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
298         }\r
299 \r
300         private bool ValidatePortNumber(TextBox textBox, out int result)\r
301         {\r
302             var s = textBox.Text;\r
303             if (!int.TryParse(s, out result))\r
304             {\r
305                 ShowToolTip("数字を入力してください。", textBox);\r
306                 return false;\r
307             }\r
308             if (result <= 0)\r
309             {\r
310                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
311                 return false;\r
312             }\r
313             return true;\r
314         }\r
315 \r
316         private void ShowToolTip(string message, Control control)\r
317         {\r
318             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
319             toolTipError.Show(message, control, 0, control.Height, 3000);\r
320         }\r
321 \r
322         private void textBox_Enter(object sender, EventArgs e)\r
323         {\r
324             toolTipError.Hide((Control)sender);\r
325         }\r
326 \r
327         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
328         {\r
329             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
330             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
331             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
332                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
333             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
334         }\r
335 \r
336         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
337         {\r
338             _main.SetPlayLog(textBoxDebugLog.Text);\r
339             DialogResult = DialogResult.Cancel;\r
340         }\r
341     }\r
342 }