OSDN Git Service

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