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             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
96             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
97         }\r
98 \r
99         private void LoadProxySettings()\r
100         {\r
101             // 見えていないTabPage上でPerformClickは使えない。\r
102             radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
103             radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
104             textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
105             radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
106             radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
107             textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
108         }\r
109 \r
110         private void LoadLogSettings()\r
111         {\r
112             checkBoxOutput.Checked = _config.Log.On;\r
113             textBoxOutput.Text = _config.Log.OutputDir;\r
114             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
115             folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
116             numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
117         }\r
118 \r
119         private void LoadDebugSettings()\r
120         {\r
121             checkBoxDebugLog.Checked = _config.DebugLogging;\r
122             textBoxDebugLog.Text = _config.DebugLogFile;\r
123         }\r
124 \r
125         private async void SetLatestVersion(string version)\r
126         {\r
127             try\r
128             {\r
129                 var req = WebRequest.Create(Home + "version");\r
130                 var response = await req.GetResponseAsync();\r
131                 var stream = response.GetResponseStream();\r
132                 if (stream == null)\r
133                     return;\r
134                 using (var reader = new StreamReader(stream))\r
135                 {\r
136                     var str = await reader.ReadLineAsync();\r
137                     try\r
138                     {\r
139                         Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
140                     }\r
141                     catch (InvalidOperationException)\r
142                     {\r
143                     }\r
144                 }\r
145             }\r
146             catch (WebException)\r
147             {\r
148             }\r
149         }\r
150 \r
151         private void buttonOk_Click(object sender, EventArgs e)\r
152         {\r
153             int listen, outbound, server;\r
154             if (!ValidatePorts(out listen, out outbound, out server))\r
155                 return;\r
156             DialogResult = DialogResult.OK;\r
157             if (!ApplyProxySettings(listen, outbound))\r
158                 DialogResult = DialogResult.None;\r
159             ApplyLogSettings();\r
160             ApplyDebugSettings();\r
161             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
162             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
163 \r
164             _config.TopMost = checkBoxTopMost.Checked;\r
165             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
166             _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
167             _config.FlashWindow = checkBoxFlash.Checked;\r
168             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
169             _config.PlaySound = checkBoxSound.Checked;\r
170             foreach (var name in Config.NotificationNames)\r
171                 _config.Notifications[name] = _notificationSettings[name];\r
172             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
173             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
174 \r
175             _config.NotifyConditions.Clear();\r
176             if (checkBoxCond40.Checked)\r
177                 _config.NotifyConditions.Add(40);\r
178             if (checkBoxCond49.Checked)\r
179                 _config.NotifyConditions.Add(49);\r
180 \r
181             _config.ResetHours.Clear();\r
182             if (checkBoxReset02.Checked)\r
183                 _config.ResetHours.Add(2);\r
184             if (checkBoxReset14.Checked)\r
185                 _config.ResetHours.Add(14);\r
186 \r
187             _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
188             _main.UpdateFighterPower();\r
189             _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
190 \r
191             _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
192             foreach (var name in Config.NotificationNames)\r
193                 _config.Sounds[name] = _soundSettings[name];\r
194         }\r
195 \r
196         private bool ValidatePorts(out int listen, out int outbound, out int server)\r
197         {\r
198             outbound = -1;\r
199             server = -1;\r
200             if (!ValidatePortNumber(textBoxListen, out listen))\r
201                 return false;\r
202             if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
203                 return false;\r
204             if (radioButtonUpstreamOn.Checked && listen == outbound)\r
205             {\r
206                 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
207                 return false;\r
208             }\r
209             return true;\r
210         }\r
211 \r
212         private bool ApplyProxySettings(int listen, int port)\r
213         {\r
214             _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
215             _config.Proxy.Listen = listen;\r
216             _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
217             if (_config.Proxy.UseUpstream)\r
218                 _config.Proxy.UpstreamPort = port;\r
219             if (!_main.ApplyProxySetting())\r
220                 return false;\r
221             textBoxListen.Text = _config.Proxy.Listen.ToString();\r
222             return true;\r
223         }\r
224 \r
225         private void ApplyLogSettings()\r
226         {\r
227             _config.Log.On = checkBoxOutput.Checked;\r
228             _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
229             _config.Log.OutputDir = textBoxOutput.Text;\r
230             _main.ApplyLogSetting();\r
231         }\r
232 \r
233         private void ApplyDebugSettings()\r
234         {\r
235             _config.DebugLogging = checkBoxDebugLog.Checked;\r
236             _config.DebugLogFile = textBoxDebugLog.Text;\r
237             _main.ApplyDebugLogSetting();\r
238         }\r
239 \r
240         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
241         {\r
242             _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
243         }\r
244 \r
245         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
246         {\r
247             if (listBoxSoundFile.SelectedItem == null)\r
248                 return;\r
249             textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];\r
250             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
251         }\r
252 \r
253         private void buttonOpenFile_Click(object sender, EventArgs e)\r
254         {\r
255             openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
256             openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
257             if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
258                 return;\r
259             textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
260             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
261         }\r
262 \r
263         private void buttonPlay_Click(object sender, EventArgs e)\r
264         {\r
265             _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
266         }\r
267 \r
268         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
269         {\r
270             _main.ResetAchievemnt();\r
271         }\r
272 \r
273         private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
274         {\r
275             linkLabelProductName.LinkVisited = true;\r
276             Process.Start(Home);\r
277         }\r
278 \r
279         private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
280         {\r
281             var off = ((RadioButton)sender).Checked;\r
282             textBoxPort.Enabled = !off;\r
283         }\r
284 \r
285         private void buttonOutputDir_Click(object sender, EventArgs e)\r
286         {\r
287             if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
288                 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
289             textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
290         }\r
291 \r
292         private bool ValidatePortNumber(TextBox textBox, out int result)\r
293         {\r
294             var s = textBox.Text;\r
295             if (!int.TryParse(s, out result))\r
296             {\r
297                 ShowToolTip("数字を入力してください。", textBox);\r
298                 return false;\r
299             }\r
300             if (result <= 0)\r
301             {\r
302                 ShowToolTip("0より大きい数字を入力してください。", textBox);\r
303                 return false;\r
304             }\r
305             return true;\r
306         }\r
307 \r
308         private void ShowToolTip(string message, Control control)\r
309         {\r
310             tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
311             toolTipError.Show(message, control, 0, control.Height, 3000);\r
312         }\r
313 \r
314         private void textBox_Enter(object sender, EventArgs e)\r
315         {\r
316             toolTipError.Hide((Control)sender);\r
317         }\r
318 \r
319         private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
320         {\r
321             openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
322             openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);\r
323             if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
324                 textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
325             textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
326         }\r
327 \r
328         private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
329         {\r
330             _main.SetPlayLog(textBoxDebugLog.Text);\r
331             DialogResult = DialogResult.Cancel;\r
332         }\r
333 \r
334         private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
335         {\r
336             _notificationConfigDialog.ShowDialog(this);\r
337         }\r
338 \r
339         private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
340         {\r
341             _prevPosition = Location;\r
342         }\r
343     }\r
344 }