OSDN Git Service

ReSharperの指摘に従って直す
[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.Drawing;\r
19 using System.IO;\r
20 using System.Linq;\r
21 using System.Net;\r
22 using System.Windows.Forms;\r
23 \r
24 namespace KancolleSniffer\r
25 {\r
26     public partial class ConfigDialog : Form\r
27     {\r
28         private readonly Config _config;\r
29         private readonly MainForm _main;\r
30         private readonly NotificationConfigDialog _notificationConfigDialog;\r
31 \r
32         private readonly Dictionary<string, NotificationType> _notificationSettings =\r
33             new Dictionary<string, NotificationType>();\r
34 \r
35         private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();\r
36         private const string Home = "http://kancollesniffer.osdn.jp/";\r
37         private Point _prevPosition = new Point(int.MinValue, int.MinValue);\r
38 \r
39         public ConfigDialog(Config config, MainForm main)\r
40         {\r
41             InitializeComponent();\r
42             _config = config;\r
43             _main = main;\r
44             // ReSharper disable once CoVariantArrayConversion\r
45             listBoxSoundFile.Items.AddRange(Config.NotificationNames);\r
46             numericUpDownMaterialLogInterval.Maximum = 1440;\r
47 \r
48             _notificationConfigDialog = new NotificationConfigDialog(_notificationSettings,\r
49                 new Dictionary<NotificationType, CheckBox>\r
50                 {\r
51                     {NotificationType.FlashWindow, checkBoxFlash},\r
52                     {NotificationType.ShowBaloonTip, checkBoxBalloon},\r
53                     {NotificationType.PlaySound, checkBoxSound}\r
54                 });\r
55         }\r
56 \r
57         private void ConfigDialog_Load(object sender, EventArgs e)\r
58         {\r
59             if (_prevPosition.X != int.MinValue)\r
60                 Location = _prevPosition;\r
61             var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));\r
62             labelVersion.Text = "バージョン" + version;\r
63             SetLatestVersion(version);\r
64             labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
65 \r
66             checkBoxTopMost.Checked = _config.TopMost;\r
67             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
68             checkBoxExitSilently.Checked = _config.ExitSilently;\r
69             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
70 \r
71             checkBoxFlash.Checked = _config.FlashWindow;\r
72             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
73             checkBoxSound.Checked = _config.PlaySound;\r
74             foreach (var name in Config.NotificationNames)\r
75                 _notificationSettings[name] = _config.Notifications[name];\r
76             numericUpDownMarginShips.Value = _config.MarginShips;\r
77             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
78             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
79             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
80 \r
81             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
82             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
83             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
84             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
85             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
86 \r
87             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
88             foreach (var name in Config.NotificationNames)\r
89                 _soundSettings[name] = _config.Sounds[name];\r
90             listBoxSoundFile.SelectedIndex = -1;\r
91             listBoxSoundFile.SelectedIndex = 0;\r
92 \r
93             LoadProxySettings();\r
94             LoadLogSettings();\r
95             LoadDebugSettings();\r
96 \r
97             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
98             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
99             checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
100             textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
101         }\r
102 \r
103         private void LoadProxySettings()\r
104         {\r
105             // 見えていないTabPage上でPerformClickは使えない。\r
106             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
107             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
108             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
109             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
110             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
111             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
112         }\r
113 \r
114         private void LoadLogSettings()\r
115         {\r
116             checkBoxOutput.Checked = _config.Log.On;\r
117             textBoxOutput.Text = _config.Log.OutputDir;\r
118             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
119             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
120             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
121         }\r
122 \r
123         private void LoadDebugSettings()\r
124         {\r
125             checkBoxDebugLog.Checked = _config.DebugLogging;\r
126             textBoxDebugLog.Text = _config.DebugLogFile;\r
127         }\r
128 \r
129         private async void SetLatestVersion(string version)\r
130         {\r
131             try\r
132             {\r
133                 var req = WebRequest.Create(Home + "version");\r
134                 var response = await req.GetResponseAsync();\r
135                 var stream = response.GetResponseStream();\r
136                 if (stream == null)\r
137                     return;\r
138                 using (var reader = new StreamReader(stream))\r
139                 {\r
140                     var str = await reader.ReadLineAsync();\r
141                     try\r
142                     {\r
143                         Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
144                     }\r
145                     catch (InvalidOperationException)\r
146                     {\r
147                     }\r
148                 }\r
149             }\r
150             catch (WebException)\r
151             {\r
152             }\r
153         }\r
154 \r
155         private void buttonOk_Click(object sender, EventArgs e)\r
156         {\r
157             if (!ValidatePorts(out var listen, out var outbound, out _))\r
158                 return;\r
159             DialogResult = DialogResult.OK;\r
160             if (!ApplyProxySettings(listen, outbound))\r
161                 DialogResult = DialogResult.None;\r
162             ApplyLogSettings();\r
163             ApplyDebugSettings();\r
164 \r
165             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
166             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
167             _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
168             _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
169 \r
170             _config.TopMost = checkBoxTopMost.Checked;\r
171             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
172             _config.ExitSilently = checkBoxExitSilently.Checked;\r
173             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
174             _config.FlashWindow = checkBoxFlash.Checked;\r
175             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
176             _config.PlaySound = checkBoxSound.Checked;\r
177             foreach (var name in Config.NotificationNames)\r
178                 _config.Notifications[name] = _notificationSettings[name];\r
179             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
180             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
181 \r
182             _config.NotifyConditions.Clear();\r
183             if (checkBoxCond40.Checked)\r
184                 _config.NotifyConditions.Add(40);\r
185             if (checkBoxCond49.Checked)\r
186                 _config.NotifyConditions.Add(49);\r
187 \r
188             _config.ResetHours.Clear();\r
189             if (checkBoxReset02.Checked)\r
190                 _config.ResetHours.Add(2);\r
191             if (checkBoxReset14.Checked)\r
192                 _config.ResetHours.Add(14);\r
193 \r
194             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
195             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
196 \r
197             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
198             foreach (var name in Config.NotificationNames)\r
199                 _config.Sounds[name] = _soundSettings[name];\r
200         }\r
201 \r
202         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
203         {\r
204             outbound = -1;\r
205             server = -1;\r
206             if (!ValidatePortNumber(textBoxListen, out listen))\r
207                 return false;\r
208             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
209                 return false;\r
210             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
211             {\r
212                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
213                 return false;\r
214             }\r
215             return true;\r
216         }\r
217 \r
218         private bool ApplyProxySettings(int listen, int port)\r
219         {\r
220             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
221             _config.Proxy.Listen = listen;\r
222             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
223             if (_config.Proxy.UseUpstream)\r
224                 _config.Proxy.UpstreamPort = port;\r
225             if (!_main.ApplyProxySetting())\r
226                 return false;\r
227             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
228             return true;\r
229         }\r
230 \r
231         private void ApplyLogSettings()\r
232         {\r
233             _config.Log.On = checkBoxOutput.Checked;\r
234             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
235             _config.Log.OutputDir = textBoxOutput.Text;\r
236             _main.ApplyLogSetting();\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             _soundSettings[(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 = _soundSettings[(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(_soundSettings[(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 buttonOutputDir_Click(object sender, EventArgs e)\r
292         {\r
293             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
294                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
295             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
296         }\r
297 \r
298         private bool ValidatePortNumber(TextBox textBox, out int result)\r
299         {\r
300             var s = textBox.Text;\r
301             if (!int.TryParse(s, out result))\r
302             {\r
303                 ShowToolTip("数字を入力してください。", textBox);\r
304                 return false;\r
305             }\r
306             if (result <= 0)\r
307             {\r
308                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
309                 return false;\r
310             }\r
311             return true;\r
312         }\r
313 \r
314         private void ShowToolTip(string message, Control control)\r
315         {\r
316             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
317             toolTipError.Show(message, control, 0, control.Height, 3000);\r
318         }\r
319 \r
320         private void textBox_Enter(object sender, EventArgs e)\r
321         {\r
322             toolTipError.Hide((Control)sender);\r
323         }\r
324 \r
325         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
326         {\r
327             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
328             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
329             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
330                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
331             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
332         }\r
333 \r
334         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
335         {\r
336             _main.SetPlayLog(textBoxDebugLog.Text);\r
337             DialogResult = DialogResult.Cancel;\r
338         }\r
339 \r
340         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
341         {\r
342             _notificationConfigDialog.ShowDialog(this);\r
343         }\r
344 \r
345         private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
346         {\r
347             _prevPosition = Location;\r
348         }\r
349 \r
350         private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
351         {\r
352             try\r
353             {\r
354                 PushBullet.PushNote(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
355             }\r
356             catch (Exception ex)\r
357             {\r
358                 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
359             }\r
360         }\r
361     }\r
362 }