OSDN Git Service

52c7a12d50a363994f5b49c246b071ba2436389c
[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         private readonly NotificationConfigDialog _notificationConfigDialog;\r
29 \r
30         private readonly Dictionary<string, NotificationSpec> _notificationSettings =\r
31             new Dictionary<string, NotificationSpec>();\r
32 \r
33         private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();\r
34         private const string Home = "http://kancollesniffer.osdn.jp/";\r
35         private Point _prevPosition = new Point(int.MinValue, int.MinValue);\r
36 \r
37         public List<string> RepeatSettingsChanged { get; } = new List<string>();\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             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
104             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
105             checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
106             textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
107             checkBoxPushoverOn.Checked = _config.Pushover.On;\r
108             textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;\r
109             textBoxPushoverUserKey.Text = _config.Pushover.UserKey;\r
110         }\r
111 \r
112         private void LoadProxySettings()\r
113         {\r
114             // 見えていないTabPage上でPerformClickは使えない。\r
115             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
116             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
117             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
118             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
119             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
120             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
121         }\r
122 \r
123         private void LoadLogSettings()\r
124         {\r
125             checkBoxOutput.Checked = _config.Log.On;\r
126             textBoxOutput.Text = _config.Log.OutputDir;\r
127             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
128             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
129             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
130         }\r
131 \r
132         private void LoadDebugSettings()\r
133         {\r
134             checkBoxDebugLog.Checked = _config.DebugLogging;\r
135             textBoxDebugLog.Text = _config.DebugLogFile;\r
136         }\r
137 \r
138         private void buttonOk_Click(object sender, EventArgs e)\r
139         {\r
140             if (!ValidatePorts(out var listen, out var outbound, out _))\r
141                 return;\r
142             DialogResult = DialogResult.OK;\r
143             if (!ApplyProxySettings(listen, outbound))\r
144                 DialogResult = DialogResult.None;\r
145             ApplyLogSettings();\r
146             ApplyDebugSettings();\r
147 \r
148             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
149             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
150             _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
151             _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
152             _config.Pushover.On = checkBoxPushoverOn.Checked;\r
153             _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;\r
154             _config.Pushover.UserKey = textBoxPushoverUserKey.Text;\r
155 \r
156             _config.TopMost = checkBoxTopMost.Checked;\r
157             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
158             _config.ExitSilently = checkBoxExitSilently.Checked;\r
159             _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;\r
160             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
161             _config.NotificationFlags = (checkBoxFlash.Checked ? NotificationType.FlashWindow : 0) |\r
162                                         (checkBoxBalloon.Checked ? NotificationType.ShowBaloonTip : 0) |\r
163                                         (checkBoxSound.Checked ? NotificationType.PlaySound : 0) |\r
164                                         (checkBoxRepeat.Checked ? NotificationType.Repeat : 0);\r
165             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
166             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
167 \r
168             RepeatSettingsChanged.Clear();\r
169             var repeatOff = (_config.NotificationFlags & NotificationType.Repeat) == 0;\r
170             foreach (var name in Config.NotificationNames)\r
171             {\r
172                 var old = _config.Notifications[name];\r
173                 var cur = _notificationSettings[name];\r
174                 if (repeatOff || old.RepeatInterval != cur.RepeatInterval ||\r
175                     (cur.Flags & NotificationType.Repeat) == 0 ||\r
176                     (cur.Flags & NotificationType.Cont) == 0)\r
177                 {\r
178                     RepeatSettingsChanged.Add(name);\r
179                 }\r
180                 _config.Notifications[name] = cur;\r
181             }\r
182 \r
183             _config.NotifyConditions.Clear();\r
184             if (checkBoxCond40.Checked)\r
185                 _config.NotifyConditions.Add(40);\r
186             if (checkBoxCond49.Checked)\r
187                 _config.NotifyConditions.Add(49);\r
188 \r
189             _config.ResetHours.Clear();\r
190             if (checkBoxReset02.Checked)\r
191                 _config.ResetHours.Add(2);\r
192             if (checkBoxReset14.Checked)\r
193                 _config.ResetHours.Add(14);\r
194 \r
195             _config.Spoilers = (checkBoxResultRank.Checked ? Spoiler.ResultRank : 0) |\r
196                                (checkBoxAirBattleResult.Checked ? Spoiler.AirBattleResult : 0) |\r
197                                (checkBoxBattleResult.Checked ? Spoiler.BattleResult : 0);\r
198             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
199 \r
200             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
201             foreach (var name in Config.NotificationNames)\r
202                 _config.Sounds[name] = _soundSettings[name];\r
203         }\r
204 \r
205         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
206         {\r
207             outbound = -1;\r
208             server = -1;\r
209             if (!ValidatePortNumber(textBoxListen, out listen))\r
210                 return false;\r
211             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
212                 return false;\r
213             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
214             {\r
215                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
216                 return false;\r
217             }\r
218             return true;\r
219         }\r
220 \r
221         private bool ApplyProxySettings(int listen, int port)\r
222         {\r
223             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
224             _config.Proxy.Listen = listen;\r
225             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
226             if (_config.Proxy.UseUpstream)\r
227                 _config.Proxy.UpstreamPort = port;\r
228             if (!_main.ApplyProxySetting())\r
229                 return false;\r
230             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
231             return true;\r
232         }\r
233 \r
234         private void ApplyLogSettings()\r
235         {\r
236             _config.Log.On = checkBoxOutput.Checked;\r
237             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
238             _config.Log.OutputDir = textBoxOutput.Text;\r
239             _main.ApplyLogSetting();\r
240         }\r
241 \r
242         private void ApplyDebugSettings()\r
243         {\r
244             _config.DebugLogging = checkBoxDebugLog.Checked;\r
245             _config.DebugLogFile = textBoxDebugLog.Text;\r
246             _main.ApplyDebugLogSetting();\r
247         }\r
248 \r
249         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
250         {\r
251             _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
252         }\r
253 \r
254         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
255         {\r
256             if (listBoxSoundFile.SelectedItem == null)\r
257                 return;\r
258             textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];\r
259             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
260         }\r
261 \r
262         private void buttonOpenFile_Click(object sender, EventArgs e)\r
263         {\r
264             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
265             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
266             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
267                 return;\r
268             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
269             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
270         }\r
271 \r
272         private void buttonPlay_Click(object sender, EventArgs e)\r
273         {\r
274             _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
275         }\r
276 \r
277         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
278         {\r
279             _main.ResetAchievemnt();\r
280         }\r
281 \r
282         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
283         {\r
284             linkLabelProductName.LinkVisited = true;\r
285             Process.Start(Home);\r
286         }\r
287 \r
288         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
289         {\r
290             var off = ((RadioButton)sender).Checked;\r
291             textBoxPort.Enabled = !off;\r
292         }\r
293 \r
294         private void buttonOutputDir_Click(object sender, EventArgs e)\r
295         {\r
296             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
297                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
298             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
299         }\r
300 \r
301         private bool ValidatePortNumber(TextBox textBox, out int result)\r
302         {\r
303             var s = textBox.Text;\r
304             if (!int.TryParse(s, out result))\r
305             {\r
306                 ShowToolTip("数字を入力してください。", textBox);\r
307                 return false;\r
308             }\r
309             if (result <= 0)\r
310             {\r
311                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
312                 return false;\r
313             }\r
314             return true;\r
315         }\r
316 \r
317         private void ShowToolTip(string message, Control control)\r
318         {\r
319             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
320             toolTipError.Show(message, control, 0, control.Height, 3000);\r
321         }\r
322 \r
323         private void textBox_Enter(object sender, EventArgs e)\r
324         {\r
325             toolTipError.Hide((Control)sender);\r
326         }\r
327 \r
328         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
329         {\r
330             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
331             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
332             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
333                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
334             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
335         }\r
336 \r
337         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
338         {\r
339             _main.SetPlayLog(textBoxDebugLog.Text);\r
340             DialogResult = DialogResult.Cancel;\r
341         }\r
342 \r
343         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
344         {\r
345             _notificationConfigDialog.ShowDialog(this);\r
346         }\r
347 \r
348         private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
349         {\r
350             _prevPosition = Location;\r
351         }\r
352 \r
353         private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
354         {\r
355             try\r
356             {\r
357                 PushNotification.PushToPushbullet(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
358             }\r
359             catch (Exception ex)\r
360             {\r
361                 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
362             }\r
363         }\r
364 \r
365         private void buttonPushoverTest_Click(object sender, EventArgs e)\r
366         {\r
367             try\r
368             {\r
369                 PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,\r
370                     "KancolleSniffer", "うまくいったかな?");\r
371             }\r
372             catch (Exception ex)\r
373             {\r
374                 MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
375             }\r
376         }\r
377     }\r
378 }