OSDN Git Service

f3af537a48f5c488b91e861a60f971d7b2413be0
[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             checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;\r
70             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
71 \r
72             checkBoxFlash.Checked = _config.FlashWindow;\r
73             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
74             checkBoxSound.Checked = _config.PlaySound;\r
75             foreach (var name in Config.NotificationNames)\r
76                 _notificationSettings[name] = _config.Notifications[name];\r
77             numericUpDownMarginShips.Value = _config.MarginShips;\r
78             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
79             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
80             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
81 \r
82             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
83             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
84             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
85             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
86             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
87 \r
88             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
89             foreach (var name in Config.NotificationNames)\r
90                 _soundSettings[name] = _config.Sounds[name];\r
91             listBoxSoundFile.SelectedIndex = -1;\r
92             listBoxSoundFile.SelectedIndex = 0;\r
93 \r
94             LoadProxySettings();\r
95             LoadLogSettings();\r
96             LoadDebugSettings();\r
97 \r
98             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
99             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
100             checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
101             textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
102         }\r
103 \r
104         private void LoadProxySettings()\r
105         {\r
106             // 見えていないTabPage上でPerformClickは使えない。\r
107             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
108             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
109             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
110             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
111             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
112             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
113         }\r
114 \r
115         private void LoadLogSettings()\r
116         {\r
117             checkBoxOutput.Checked = _config.Log.On;\r
118             textBoxOutput.Text = _config.Log.OutputDir;\r
119             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
120             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
121             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
122         }\r
123 \r
124         private void LoadDebugSettings()\r
125         {\r
126             checkBoxDebugLog.Checked = _config.DebugLogging;\r
127             textBoxDebugLog.Text = _config.DebugLogFile;\r
128         }\r
129 \r
130         private async void SetLatestVersion(string version)\r
131         {\r
132             try\r
133             {\r
134                 var req = WebRequest.Create(Home + "version");\r
135                 var response = await req.GetResponseAsync();\r
136                 var stream = response.GetResponseStream();\r
137                 if (stream == null)\r
138                     return;\r
139                 using (var reader = new StreamReader(stream))\r
140                 {\r
141                     var str = await reader.ReadLineAsync();\r
142                     try\r
143                     {\r
144                         Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
145                     }\r
146                     catch (InvalidOperationException)\r
147                     {\r
148                     }\r
149                 }\r
150             }\r
151             catch (WebException)\r
152             {\r
153             }\r
154         }\r
155 \r
156         private void buttonOk_Click(object sender, EventArgs e)\r
157         {\r
158             if (!ValidatePorts(out var listen, out var outbound, out _))\r
159                 return;\r
160             DialogResult = DialogResult.OK;\r
161             if (!ApplyProxySettings(listen, outbound))\r
162                 DialogResult = DialogResult.None;\r
163             ApplyLogSettings();\r
164             ApplyDebugSettings();\r
165 \r
166             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
167             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
168             _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
169             _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
170 \r
171             _config.TopMost = checkBoxTopMost.Checked;\r
172             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
173             _config.ExitSilently = checkBoxExitSilently.Checked;\r
174             _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;\r
175             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
176             _config.FlashWindow = checkBoxFlash.Checked;\r
177             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
178             _config.PlaySound = checkBoxSound.Checked;\r
179             foreach (var name in Config.NotificationNames)\r
180                 _config.Notifications[name] = _notificationSettings[name];\r
181             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
182             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
183 \r
184             _config.NotifyConditions.Clear();\r
185             if (checkBoxCond40.Checked)\r
186                 _config.NotifyConditions.Add(40);\r
187             if (checkBoxCond49.Checked)\r
188                 _config.NotifyConditions.Add(49);\r
189 \r
190             _config.ResetHours.Clear();\r
191             if (checkBoxReset02.Checked)\r
192                 _config.ResetHours.Add(2);\r
193             if (checkBoxReset14.Checked)\r
194                 _config.ResetHours.Add(14);\r
195 \r
196             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
197             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
198 \r
199             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
200             foreach (var name in Config.NotificationNames)\r
201                 _config.Sounds[name] = _soundSettings[name];\r
202         }\r
203 \r
204         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
205         {\r
206             outbound = -1;\r
207             server = -1;\r
208             if (!ValidatePortNumber(textBoxListen, out listen))\r
209                 return false;\r
210             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
211                 return false;\r
212             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
213             {\r
214                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
215                 return false;\r
216             }\r
217             return true;\r
218         }\r
219 \r
220         private bool ApplyProxySettings(int listen, int port)\r
221         {\r
222             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
223             _config.Proxy.Listen = listen;\r
224             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
225             if (_config.Proxy.UseUpstream)\r
226                 _config.Proxy.UpstreamPort = port;\r
227             if (!_main.ApplyProxySetting())\r
228                 return false;\r
229             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
230             return true;\r
231         }\r
232 \r
233         private void ApplyLogSettings()\r
234         {\r
235             _config.Log.On = checkBoxOutput.Checked;\r
236             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
237             _config.Log.OutputDir = textBoxOutput.Text;\r
238             _main.ApplyLogSetting();\r
239         }\r
240 \r
241         private void ApplyDebugSettings()\r
242         {\r
243             _config.DebugLogging = checkBoxDebugLog.Checked;\r
244             _config.DebugLogFile = textBoxDebugLog.Text;\r
245             _main.ApplyDebugLogSetting();\r
246         }\r
247 \r
248         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
249         {\r
250             _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
251         }\r
252 \r
253         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
254         {\r
255             if (listBoxSoundFile.SelectedItem == null)\r
256                 return;\r
257             textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];\r
258             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
259         }\r
260 \r
261         private void buttonOpenFile_Click(object sender, EventArgs e)\r
262         {\r
263             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
264             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
265             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
266                 return;\r
267             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
268             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
269         }\r
270 \r
271         private void buttonPlay_Click(object sender, EventArgs e)\r
272         {\r
273             _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
274         }\r
275 \r
276         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
277         {\r
278             _main.ResetAchievemnt();\r
279         }\r
280 \r
281         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
282         {\r
283             linkLabelProductName.LinkVisited = true;\r
284             Process.Start(Home);\r
285         }\r
286 \r
287         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
288         {\r
289             var off = ((RadioButton)sender).Checked;\r
290             textBoxPort.Enabled = !off;\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         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
343         {\r
344             _notificationConfigDialog.ShowDialog(this);\r
345         }\r
346 \r
347         private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
348         {\r
349             _prevPosition = Location;\r
350         }\r
351 \r
352         private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
353         {\r
354             try\r
355             {\r
356                 PushBullet.PushNote(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
357             }\r
358             catch (Exception ex)\r
359             {\r
360                 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
361             }\r
362         }\r
363     }\r
364 }