OSDN Git Service

終了時に直近の提督経験値を記録する
[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 NotificationConfigDialog _notificationConfigDialog;\r
30 \r
31         private readonly Dictionary<string, NotificationType> _notificationSettings =\r
32             new Dictionary<string, NotificationType>();\r
33 \r
34         private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();\r
35         private const string Home = "http://kancollesniffer.osdn.jp/";\r
36 \r
37         public ConfigDialog(Config config, MainForm main)\r
38         {\r
39             InitializeComponent();\r
40             _config = config;\r
41             _main = main;\r
42             // ReSharper disable once CoVariantArrayConversion\r
43             listBoxSoundFile.Items.AddRange(Config.NotificationNames);\r
44             numericUpDownMaterialLogInterval.Maximum = 1440;\r
45 \r
46             _notificationConfigDialog = new NotificationConfigDialog(_notificationSettings,\r
47                 new Dictionary<NotificationType, CheckBox>\r
48                 {\r
49                     {NotificationType.FlashWindow, checkBoxFlash},\r
50                     {NotificationType.ShowBaloonTip, checkBoxBalloon},\r
51                     {NotificationType.PlaySound, checkBoxSound}\r
52                 });\r
53         }\r
54 \r
55         private void ConfigDialog_Load(object sender, EventArgs e)\r
56         {\r
57             var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));\r
58             labelVersion.Text = "バージョン" + version;\r
59             SetLatestVersion(version);\r
60             labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
61 \r
62             checkBoxTopMost.Checked = _config.TopMost;\r
63             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
64             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
65 \r
66             checkBoxFlash.Checked = _config.FlashWindow;\r
67             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
68             checkBoxSound.Checked = _config.PlaySound;\r
69             foreach (var name in Config.NotificationNames)\r
70                 _notificationSettings[name] = _config.Notifications[name];\r
71             numericUpDownMarginShips.Value = _config.MarginShips;\r
72             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
73             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
74             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
75 \r
76             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
77             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
78             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
79             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
80             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
81 \r
82             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
83             foreach (var name in Config.NotificationNames)\r
84                 _soundSettings[name] = _config.Sounds[name];\r
85             listBoxSoundFile.SelectedIndex = -1;\r
86             listBoxSoundFile.SelectedIndex = 0;\r
87 \r
88             LoadProxySettings();\r
89             LoadLogSettings();\r
90             LoadDebugSettings();\r
91             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
92             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
93         }\r
94 \r
95         private void LoadProxySettings()\r
96         {\r
97             // 見えていないTabPage上でPerformClickは使えない。\r
98             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
99             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
100             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
101             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
102             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
103             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
104         }\r
105 \r
106         private void LoadLogSettings()\r
107         {\r
108             checkBoxOutput.Checked = _config.Log.On;\r
109             textBoxOutput.Text = _config.Log.OutputDir;\r
110             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
111             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
112             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
113         }\r
114 \r
115         private void LoadDebugSettings()\r
116         {\r
117             checkBoxDebugLog.Checked = _config.DebugLogging;\r
118             textBoxDebugLog.Text = _config.DebugLogFile;\r
119         }\r
120 \r
121         private async void SetLatestVersion(string version)\r
122         {\r
123             try\r
124             {\r
125                 var req = WebRequest.Create(Home + "version");\r
126                 var response = await req.GetResponseAsync();\r
127                 var stream = response.GetResponseStream();\r
128                 if (stream == null)\r
129                     return;\r
130                 using (var reader = new StreamReader(stream))\r
131                 {\r
132                     var str = await reader.ReadLineAsync();\r
133                     try\r
134                     {\r
135                         Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
136                     }\r
137                     catch (InvalidOperationException)\r
138                     {\r
139                     }\r
140                 }\r
141             }\r
142             catch (WebException)\r
143             {\r
144             }\r
145         }\r
146 \r
147         private void buttonOk_Click(object sender, EventArgs e)\r
148         {\r
149             int listen, outbound, server;\r
150             if (!ValidatePorts(out listen, out outbound, out server))\r
151                 return;\r
152             DialogResult = DialogResult.OK;\r
153             if (!ApplyProxySettings(listen, outbound))\r
154                 DialogResult = DialogResult.None;\r
155             ApplyLogSettings();\r
156             ApplyDebugSettings();\r
157             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
158             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
159 \r
160             _config.TopMost = checkBoxTopMost.Checked;\r
161             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
162             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
163             _config.FlashWindow = checkBoxFlash.Checked;\r
164             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
165             _config.PlaySound = checkBoxSound.Checked;\r
166             foreach (var name in Config.NotificationNames)\r
167                 _config.Notifications[name] = _notificationSettings[name];\r
168             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
169             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
170 \r
171             _config.NotifyConditions.Clear();\r
172             if (checkBoxCond40.Checked)\r
173                 _config.NotifyConditions.Add(40);\r
174             if (checkBoxCond49.Checked)\r
175                 _config.NotifyConditions.Add(49);\r
176 \r
177             _config.ResetHours.Clear();\r
178             if (checkBoxReset02.Checked)\r
179                 _config.ResetHours.Add(2);\r
180             if (checkBoxReset14.Checked)\r
181                 _config.ResetHours.Add(14);\r
182 \r
183             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
184             _main.UpdateFighterPower();\r
185             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
186 \r
187             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
188             foreach (var name in Config.NotificationNames)\r
189                 _config.Sounds[name] = _soundSettings[name];\r
190         }\r
191 \r
192         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
193         {\r
194             outbound = -1;\r
195             server = -1;\r
196             if (!ValidatePortNumber(textBoxListen, out listen))\r
197                 return false;\r
198             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\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             return true;\r
206         }\r
207 \r
208         private bool ApplyProxySettings(int listen, int port)\r
209         {\r
210             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
211             _config.Proxy.Listen = listen;\r
212             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
213             if (_config.Proxy.UseUpstream)\r
214                 _config.Proxy.UpstreamPort = port;\r
215             if (!_main.ApplyProxySetting())\r
216                 return false;\r
217             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
218             return true;\r
219         }\r
220 \r
221         private void ApplyLogSettings()\r
222         {\r
223             _config.Log.On = checkBoxOutput.Checked;\r
224             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
225             _config.Log.OutputDir = textBoxOutput.Text;\r
226             _main.ApplyLogSetting();\r
227         }\r
228 \r
229         private void ApplyDebugSettings()\r
230         {\r
231             _config.DebugLogging = checkBoxDebugLog.Checked;\r
232             _config.DebugLogFile = textBoxDebugLog.Text;\r
233             _main.ApplyDebugLogSetting();\r
234         }\r
235 \r
236         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
237         {\r
238             _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
239         }\r
240 \r
241         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
242         {\r
243             if (listBoxSoundFile.SelectedItem == null)\r
244                 return;\r
245             textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];\r
246             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
247         }\r
248 \r
249         private void buttonOpenFile_Click(object sender, EventArgs e)\r
250         {\r
251             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
252             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
253             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
254                 return;\r
255             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
256             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
257         }\r
258 \r
259         private void buttonPlay_Click(object sender, EventArgs e)\r
260         {\r
261             _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
262         }\r
263 \r
264         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
265         {\r
266             _main.ResetAchievemnt();\r
267         }\r
268 \r
269         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
270         {\r
271             linkLabelProductName.LinkVisited = true;\r
272             Process.Start(Home);\r
273         }\r
274 \r
275         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
276         {\r
277             var off = ((RadioButton)sender).Checked;\r
278             textBoxPort.Enabled = !off;\r
279         }\r
280 \r
281         private void buttonOutputDir_Click(object sender, EventArgs e)\r
282         {\r
283             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
284                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
285             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
286         }\r
287 \r
288         private bool ValidatePortNumber(TextBox textBox, out int result)\r
289         {\r
290             var s = textBox.Text;\r
291             if (!int.TryParse(s, out result))\r
292             {\r
293                 ShowToolTip("数字を入力してください。", textBox);\r
294                 return false;\r
295             }\r
296             if (result <= 0)\r
297             {\r
298                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
299                 return false;\r
300             }\r
301             return true;\r
302         }\r
303 \r
304         private void ShowToolTip(string message, Control control)\r
305         {\r
306             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
307             toolTipError.Show(message, control, 0, control.Height, 3000);\r
308         }\r
309 \r
310         private void textBox_Enter(object sender, EventArgs e)\r
311         {\r
312             toolTipError.Hide((Control)sender);\r
313         }\r
314 \r
315         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
316         {\r
317             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
318             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
319             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
320                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
321             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
322         }\r
323 \r
324         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
325         {\r
326             _main.SetPlayLog(textBoxDebugLog.Text);\r
327             DialogResult = DialogResult.Cancel;\r
328         }\r
329 \r
330         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
331         {\r
332             _notificationConfigDialog.ShowDialog(this);\r
333         }\r
334     }\r
335 }