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.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             comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
69 \r
70             checkBoxFlash.Checked = _config.FlashWindow;\r
71             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
72             checkBoxSound.Checked = _config.PlaySound;\r
73             foreach (var name in Config.NotificationNames)\r
74                 _notificationSettings[name] = _config.Notifications[name];\r
75             numericUpDownMarginShips.Value = _config.MarginShips;\r
76             numericUpDownMarginEquips.Value = _config.MarginEquips;\r
77             checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
78             checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
79 \r
80             checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
81             checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
82             radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
83             radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
84             checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
85 \r
86             numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
87             foreach (var name in Config.NotificationNames)\r
88                 _soundSettings[name] = _config.Sounds[name];\r
89             listBoxSoundFile.SelectedIndex = -1;\r
90             listBoxSoundFile.SelectedIndex = 0;\r
91 \r
92             LoadProxySettings();\r
93             LoadLogSettings();\r
94             LoadDebugSettings();\r
95 \r
96             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
97             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
98             checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
99             textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
100         }\r
101 \r
102         private void LoadProxySettings()\r
103         {\r
104             // 見えていないTabPage上でPerformClickは使えない。\r
105             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
106             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
107             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
108             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
109             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
110             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
111         }\r
112 \r
113         private void LoadLogSettings()\r
114         {\r
115             checkBoxOutput.Checked = _config.Log.On;\r
116             textBoxOutput.Text = _config.Log.OutputDir;\r
117             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
118             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
119             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
120         }\r
121 \r
122         private void LoadDebugSettings()\r
123         {\r
124             checkBoxDebugLog.Checked = _config.DebugLogging;\r
125             textBoxDebugLog.Text = _config.DebugLogFile;\r
126         }\r
127 \r
128         private async void SetLatestVersion(string version)\r
129         {\r
130             try\r
131             {\r
132                 var req = WebRequest.Create(Home + "version");\r
133                 var response = await req.GetResponseAsync();\r
134                 var stream = response.GetResponseStream();\r
135                 if (stream == null)\r
136                     return;\r
137                 using (var reader = new StreamReader(stream))\r
138                 {\r
139                     var str = await reader.ReadLineAsync();\r
140                     try\r
141                     {\r
142                         Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
143                     }\r
144                     catch (InvalidOperationException)\r
145                     {\r
146                     }\r
147                 }\r
148             }\r
149             catch (WebException)\r
150             {\r
151             }\r
152         }\r
153 \r
154         private void buttonOk_Click(object sender, EventArgs e)\r
155         {\r
156             int listen, outbound, server;\r
157             if (!ValidatePorts(out listen, out outbound, out server))\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.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
173             _config.FlashWindow = checkBoxFlash.Checked;\r
174             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
175             _config.PlaySound = checkBoxSound.Checked;\r
176             foreach (var name in Config.NotificationNames)\r
177                 _config.Notifications[name] = _notificationSettings[name];\r
178             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
179             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
180 \r
181             _config.NotifyConditions.Clear();\r
182             if (checkBoxCond40.Checked)\r
183                 _config.NotifyConditions.Add(40);\r
184             if (checkBoxCond49.Checked)\r
185                 _config.NotifyConditions.Add(49);\r
186 \r
187             _config.ResetHours.Clear();\r
188             if (checkBoxReset02.Checked)\r
189                 _config.ResetHours.Add(2);\r
190             if (checkBoxReset14.Checked)\r
191                 _config.ResetHours.Add(14);\r
192 \r
193             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\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 void ShowToolTip(string message, Control control)\r
314         {\r
315             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
316             toolTipError.Show(message, control, 0, control.Height, 3000);\r
317         }\r
318 \r
319         private void textBox_Enter(object sender, EventArgs e)\r
320         {\r
321             toolTipError.Hide((Control)sender);\r
322         }\r
323 \r
324         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
325         {\r
326             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
327             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
328             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
329                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
330             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
331         }\r
332 \r
333         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
334         {\r
335             _main.SetPlayLog(textBoxDebugLog.Text);\r
336             DialogResult = DialogResult.Cancel;\r
337         }\r
338 \r
339         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
340         {\r
341             _notificationConfigDialog.ShowDialog(this);\r
342         }\r
343 \r
344         private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
345         {\r
346             _prevPosition = Location;\r
347         }\r
348 \r
349         private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
350         {\r
351             try\r
352             {\r
353                 PushBullet.PushNote(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
354             }\r
355             catch (Exception ex)\r
356             {\r
357                 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
358             }\r
359         }\r
360     }\r
361 }