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.Drawing;\r
19 using System.IO;\r
20 using System.Windows.Forms;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public partial class ConfigDialog : Form\r
25     {\r
26         private readonly Config _config;\r
27         private readonly MainForm _main;\r
28 \r
29         private readonly Dictionary<string, NotificationSpec> _notificationSettings =\r
30             new Dictionary<string, NotificationSpec>();\r
31 \r
32         private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();\r
33         private const string Home = "http://kancollesniffer.osdn.jp/";\r
34         private Point _prevPosition = new Point(int.MinValue, int.MinValue);\r
35 \r
36         public List<string> RepeatSettingsChanged { get; } = new List<string>();\r
37         public NotificationConfigDialog NotificationConfigDialog { get; }\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                     {NotificationType.Repeat, checkBoxRepeat}\r
55                 });\r
56         }\r
57 \r
58         private void ConfigDialog_Load(object sender, EventArgs e)\r
59         {\r
60             if (_prevPosition.X != int.MinValue)\r
61                 Location = _prevPosition;\r
62             _main.CheckVersionUp((current, latest) =>\r
63             {\r
64                 labelVersion.Text = "バージョン" + current;\r
65                 labelLatest.Text = double.Parse(current) >= double.Parse(latest) ? "最新です" : "最新は" + latest + "です";\r
66             });\r
67             labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
68 \r
69             checkBoxTopMost.Checked = _config.TopMost;\r
70             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
71             checkBoxExitSilently.Checked = _config.ExitSilently;\r
72             checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;\r
73             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
74 \r
75             checkBoxFlash.Checked = (_config.NotificationFlags & NotificationType.FlashWindow) != 0;\r
76             checkBoxBalloon.Checked = (_config.NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
77             checkBoxSound.Checked = (_config.NotificationFlags & NotificationType.PlaySound) != 0;\r
78             checkBoxRepeat.Checked = (_config.NotificationFlags & NotificationType.Repeat) != 0;\r
79             foreach (var name in Config.NotificationNames)\r
80                 _notificationSettings[name] = _config.Notifications[name];\r
81             numericUpDownMarginShips.Value = _config.MarginShips;\r
82             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
83             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
84             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
85 \r
86             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
87             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
88             checkBoxResultRank.Checked = (_config.Spoilers & Spoiler.ResultRank) != 0;\r
89             checkBoxAirBattleResult.Checked = (_config.Spoilers & Spoiler.AirBattleResult) != 0;\r
90             checkBoxBattleResult.Checked = (_config.Spoilers & Spoiler.BattleResult) != 0;\r
91             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
92 \r
93             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
94             foreach (var name in Config.NotificationNames)\r
95                 _soundSettings[name] = _config.Sounds[name];\r
96             listBoxSoundFile.SelectedIndex = -1;\r
97             listBoxSoundFile.SelectedIndex = 0;\r
98 \r
99             LoadProxySettings();\r
100             LoadLogSettings();\r
101             LoadDebugSettings();\r
102 \r
103             checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
104             textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
105             checkBoxPushoverOn.Checked = _config.Pushover.On;\r
106             textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;\r
107             textBoxPushoverUserKey.Text = _config.Pushover.UserKey;\r
108         }\r
109 \r
110         private void LoadProxySettings()\r
111         {\r
112             // 見えていないTabPage上でPerformClickは使えない。\r
113             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
114             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
115             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
116             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
117             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
118             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
119         }\r
120 \r
121         private void LoadLogSettings()\r
122         {\r
123             checkBoxOutput.Checked = _config.Log.On;\r
124             textBoxOutput.Text = _config.Log.OutputDir;\r
125             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
126             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
127             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
128         }\r
129 \r
130         private void LoadDebugSettings()\r
131         {\r
132             checkBoxDebugLog.Checked = _config.DebugLogging;\r
133             textBoxDebugLog.Text = _config.DebugLogFile;\r
134         }\r
135 \r
136         private void buttonOk_Click(object sender, EventArgs e)\r
137         {\r
138             if (!ValidatePorts(out var listen, out var outbound, out _))\r
139                 return;\r
140             DialogResult = DialogResult.OK;\r
141             if (!ApplyProxySettings(listen, outbound))\r
142                 DialogResult = DialogResult.None;\r
143             ApplyLogSettings();\r
144             ApplyDebugSettings();\r
145 \r
146             _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
147             _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
148             _config.Pushover.On = checkBoxPushoverOn.Checked;\r
149             _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;\r
150             _config.Pushover.UserKey = textBoxPushoverUserKey.Text;\r
151 \r
152             _config.TopMost = checkBoxTopMost.Checked;\r
153             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
154             _config.ExitSilently = checkBoxExitSilently.Checked;\r
155             _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;\r
156             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
157             _config.NotificationFlags = (checkBoxFlash.Checked ? NotificationType.FlashWindow : 0) |\r
158                                         (checkBoxBalloon.Checked ? NotificationType.ShowBaloonTip : 0) |\r
159                                         (checkBoxSound.Checked ? NotificationType.PlaySound : 0) |\r
160                                         (checkBoxRepeat.Checked ? NotificationType.Repeat : 0);\r
161             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
162             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
163 \r
164             RepeatSettingsChanged.Clear();\r
165             var repeatOff = (_config.NotificationFlags & NotificationType.Repeat) == 0;\r
166             foreach (var name in Config.NotificationNames)\r
167             {\r
168                 var old = _config.Notifications[name];\r
169                 var cur = _notificationSettings[name];\r
170                 if (repeatOff || old.RepeatInterval != cur.RepeatInterval ||\r
171                     (cur.Flags & NotificationType.Repeat) == 0 ||\r
172                     (cur.Flags & NotificationType.Cont) == 0)\r
173                 {\r
174                     RepeatSettingsChanged.Add(name);\r
175                 }\r
176                 _config.Notifications[name] = cur;\r
177             }\r
178 \r
179             _config.NotifyConditions.Clear();\r
180             if (checkBoxCond40.Checked)\r
181                 _config.NotifyConditions.Add(40);\r
182             if (checkBoxCond49.Checked)\r
183                 _config.NotifyConditions.Add(49);\r
184 \r
185             _config.ResetHours.Clear();\r
186             if (checkBoxReset02.Checked)\r
187                 _config.ResetHours.Add(2);\r
188             if (checkBoxReset14.Checked)\r
189                 _config.ResetHours.Add(14);\r
190 \r
191             _config.Spoilers = (checkBoxResultRank.Checked ? Spoiler.ResultRank : 0) |\r
192                                (checkBoxAirBattleResult.Checked ? Spoiler.AirBattleResult : 0) |\r
193                                (checkBoxBattleResult.Checked ? Spoiler.BattleResult : 0);\r
194             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
195 \r
196             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
197             foreach (var name in Config.NotificationNames)\r
198                 _config.Sounds[name] = _soundSettings[name];\r
199         }\r
200 \r
201         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
202         {\r
203             outbound = -1;\r
204             server = -1;\r
205             if (!ValidatePortNumber(textBoxListen, out listen))\r
206                 return false;\r
207             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
208                 return false;\r
209             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
210             {\r
211                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
212                 return false;\r
213             }\r
214             return true;\r
215         }\r
216 \r
217         private bool ApplyProxySettings(int listen, int port)\r
218         {\r
219             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
220             _config.Proxy.Listen = listen;\r
221             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
222             if (_config.Proxy.UseUpstream)\r
223                 _config.Proxy.UpstreamPort = port;\r
224             if (!_main.ApplyProxySetting())\r
225                 return false;\r
226             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
227             return true;\r
228         }\r
229 \r
230         private void ApplyLogSettings()\r
231         {\r
232             _config.Log.On = checkBoxOutput.Checked;\r
233             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
234             _config.Log.OutputDir = textBoxOutput.Text;\r
235             _main.ApplyLogSetting();\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             _soundSettings[(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 = _soundSettings[(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(_soundSettings[(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 buttonOutputDir_Click(object sender, EventArgs e)\r
291         {\r
292             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
293                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
294             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
295         }\r
296 \r
297         private bool ValidatePortNumber(TextBox textBox, out int result)\r
298         {\r
299             var s = textBox.Text;\r
300             if (!int.TryParse(s, out result))\r
301             {\r
302                 ShowToolTip("数字を入力してください。", textBox);\r
303                 return false;\r
304             }\r
305             if (result <= 0)\r
306             {\r
307                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
308                 return false;\r
309             }\r
310             return true;\r
311         }\r
312 \r
313         private readonly ResizableToolTip _toolTip =\r
314             new ResizableToolTip {AutomaticDelay = 0, ToolTipIcon = ToolTipIcon.Error};\r
315 \r
316         private void ShowToolTip(string message, Control control)\r
317         {\r
318             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
319             _toolTip.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             _toolTip.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                 PushNotification.PushToPushbullet(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         private void buttonPushoverTest_Click(object sender, EventArgs e)\r
365         {\r
366             try\r
367             {\r
368                 PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,\r
369                     "KancolleSniffer", "うまくいったかな?");\r
370             }\r
371             catch (Exception ex)\r
372             {\r
373                 MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
374             }\r
375         }\r
376 \r
377         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)\r
378         {\r
379             base.ScaleControl(factor, specified);\r
380             if (factor.Height > 1)\r
381                 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);\r
382         }\r
383     }\r
384 }