1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
17 using System.Diagnostics;
\r
18 using System.Drawing;
\r
22 using System.Windows.Forms;
\r
24 namespace KancolleSniffer
\r
26 public partial class ConfigDialog : Form
\r
28 private readonly Config _config;
\r
29 private readonly MainForm _main;
\r
30 private readonly NotificationConfigDialog _notificationConfigDialog;
\r
32 private readonly Dictionary<string, NotificationType> _notificationSettings =
\r
33 new Dictionary<string, NotificationType>();
\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
39 public ConfigDialog(Config config, MainForm main)
\r
41 InitializeComponent();
\r
44 // ReSharper disable once CoVariantArrayConversion
\r
45 listBoxSoundFile.Items.AddRange(Config.NotificationNames);
\r
46 numericUpDownMaterialLogInterval.Maximum = 1440;
\r
48 _notificationConfigDialog = new NotificationConfigDialog(_notificationSettings,
\r
49 new Dictionary<NotificationType, CheckBox>
\r
51 {NotificationType.FlashWindow, checkBoxFlash},
\r
52 {NotificationType.ShowBaloonTip, checkBoxBalloon},
\r
53 {NotificationType.PlaySound, checkBoxSound}
\r
57 private void ConfigDialog_Load(object sender, EventArgs e)
\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
66 checkBoxTopMost.Checked = _config.TopMost;
\r
67 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
68 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\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
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
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
92 LoadProxySettings();
\r
94 LoadDebugSettings();
\r
95 checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;
\r
96 textBoxKancolleDbToken.Text = _config.KancolleDb.Token;
\r
99 private void LoadProxySettings()
\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
110 private void LoadLogSettings()
\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
119 private void LoadDebugSettings()
\r
121 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
122 textBoxDebugLog.Text = _config.DebugLogFile;
\r
125 private async void SetLatestVersion(string version)
\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
134 using (var reader = new StreamReader(stream))
\r
136 var str = await reader.ReadLineAsync();
\r
139 Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));
\r
141 catch (InvalidOperationException)
\r
146 catch (WebException)
\r
151 private void buttonOk_Click(object sender, EventArgs e)
\r
153 int listen, outbound, server;
\r
154 if (!ValidatePorts(out listen, out outbound, out server))
\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
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
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
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
187 _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;
\r
188 _main.UpdateFighterPower();
\r
189 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
191 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
192 foreach (var name in Config.NotificationNames)
\r
193 _config.Sounds[name] = _soundSettings[name];
\r
196 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
200 if (!ValidatePortNumber(textBoxListen, out listen))
\r
202 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
204 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
206 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
212 private bool ApplyProxySettings(int listen, int port)
\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
221 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
225 private void ApplyLogSettings()
\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
233 private void ApplyDebugSettings()
\r
235 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
236 _config.DebugLogFile = textBoxDebugLog.Text;
\r
237 _main.ApplyDebugLogSetting();
\r
240 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
242 _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
245 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
247 if (listBoxSoundFile.SelectedItem == null)
\r
249 textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];
\r
250 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
253 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
255 openSoundFileDialog.FileName = textBoxSoundFile.Text;
\r
256 openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";
\r
257 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
259 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
260 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
263 private void buttonPlay_Click(object sender, EventArgs e)
\r
265 _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
268 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
270 _main.ResetAchievemnt();
\r
273 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
275 linkLabelProductName.LinkVisited = true;
\r
276 Process.Start(Home);
\r
279 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
281 var off = ((RadioButton)sender).Checked;
\r
282 textBoxPort.Enabled = !off;
\r
285 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
287 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
288 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
289 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
292 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
294 var s = textBox.Text;
\r
295 if (!int.TryParse(s, out result))
\r
297 ShowToolTip("数字を入力してください。", textBox);
\r
302 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
308 private void ShowToolTip(string message, Control control)
\r
310 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
311 toolTipError.Show(message, control, 0, control.Height, 3000);
\r
314 private void textBox_Enter(object sender, EventArgs e)
\r
316 toolTipError.Hide((Control)sender);
\r
319 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\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
328 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
330 _main.SetPlayLog(textBoxDebugLog.Text);
\r
331 DialogResult = DialogResult.Cancel;
\r
334 private void buttonDetailedSettings_Click(object sender, EventArgs e)
\r
336 _notificationConfigDialog.ShowDialog(this);
\r
339 private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)
\r
341 _prevPosition = Location;
\r