OSDN Git Service

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