OSDN Git Service

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