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
21 using System.Windows.Forms;
\r
23 namespace KancolleSniffer
\r
25 public partial class ConfigDialog : Form
\r
27 private readonly Config _config;
\r
28 private readonly MainForm _main;
\r
29 private readonly Dictionary<string, string> _soundSetting = new Dictionary<string, string>();
\r
30 private const string Home = "http://kancollesniffer.osdn.jp/";
\r
32 public ConfigDialog(Config config, MainForm main)
\r
34 InitializeComponent();
\r
37 // ReSharper disable once CoVariantArrayConversion
\r
38 listBoxSoundFile.Items.AddRange(_config.Sounds.SoundNames);
\r
39 numericUpDownMaterialLogInterval.Maximum = 1440;
\r
42 private void ConfigDialog_Load(object sender, EventArgs e)
\r
44 var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));
\r
45 labelVersion.Text = "バージョン" + version;
\r
46 SetLatestVersion(version);
\r
47 labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;
\r
49 checkBoxTopMost.Checked = _config.TopMost;
\r
50 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
51 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\r
52 checkBoxFlash.Checked = _config.FlashWindow;
\r
53 checkBoxBalloon.Checked = _config.ShowBaloonTip;
\r
54 checkBoxSound.Checked = _config.PlaySound;
\r
55 numericUpDownMarginShips.Value = _config.MarginShips;
\r
56 numericUpDownMarginEquips.Value = _config.MarginEquips;
\r
57 checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);
\r
58 checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);
\r
59 checkBoxReset02.Checked = _config.ResetHours.Contains(2);
\r
60 checkBoxReset14.Checked = _config.ResetHours.Contains(14);
\r
61 radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;
\r
62 radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;
\r
63 checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;
\r
65 numericUpDownSoundVolume.Value = _config.Sounds.Volume;
\r
66 foreach (var name in _config.Sounds.SoundNames)
\r
67 _soundSetting[name] = _config.Sounds[name];
\r
68 listBoxSoundFile.SelectedIndex = -1;
\r
69 listBoxSoundFile.SelectedIndex = 0;
\r
71 LoadProxySettings();
\r
73 LoadDebugSettings();
\r
74 checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;
\r
75 textBoxKancolleDbToken.Text = _config.KancolleDb.Token;
\r
78 private void LoadProxySettings()
\r
80 // 見えていないTabPage上でPerformClickは使えない。
\r
81 radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;
\r
82 radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;
\r
83 textBoxListen.Text = _config.Proxy.Listen.ToString("D");
\r
84 radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;
\r
85 radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;
\r
86 textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");
\r
89 private void LoadLogSettings()
\r
91 checkBoxOutput.Checked = _config.Log.On;
\r
92 textBoxOutput.Text = _config.Log.OutputDir;
\r
93 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
94 folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;
\r
95 numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;
\r
98 private void LoadDebugSettings()
\r
100 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
101 textBoxDebugLog.Text = _config.DebugLogFile;
\r
104 private async void SetLatestVersion(string version)
\r
108 var req = WebRequest.Create(Home + "version");
\r
109 var response = await req.GetResponseAsync();
\r
110 var stream = response.GetResponseStream();
\r
111 if (stream == null)
\r
113 using (var reader = new StreamReader(stream))
\r
115 var str = await reader.ReadLineAsync();
\r
116 Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));
\r
119 catch (WebException)
\r
124 private void buttonOk_Click(object sender, EventArgs e)
\r
126 int listen, outbound, server;
\r
127 if (!ValidatePorts(out listen, out outbound, out server))
\r
129 DialogResult = DialogResult.OK;
\r
130 if (!ApplyProxySettings(listen, outbound))
\r
131 DialogResult = DialogResult.None;
\r
132 ApplyLogSettings();
\r
133 ApplyDebugSettings();
\r
134 _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;
\r
135 _config.KancolleDb.Token = textBoxKancolleDbToken.Text;
\r
137 _config.TopMost = checkBoxTopMost.Checked;
\r
138 _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;
\r
139 _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));
\r
140 _config.FlashWindow = checkBoxFlash.Checked;
\r
141 _config.ShowBaloonTip = checkBoxBalloon.Checked;
\r
142 _config.PlaySound = checkBoxSound.Checked;
\r
143 _config.MarginShips = (int)numericUpDownMarginShips.Value;
\r
144 _config.MarginEquips = (int)numericUpDownMarginEquips.Value;
\r
146 _config.NotifyConditions.Clear();
\r
147 if (checkBoxCond40.Checked)
\r
148 _config.NotifyConditions.Add(40);
\r
149 if (checkBoxCond49.Checked)
\r
150 _config.NotifyConditions.Add(49);
\r
152 _config.ResetHours.Clear();
\r
153 if (checkBoxReset02.Checked)
\r
154 _config.ResetHours.Add(2);
\r
155 if (checkBoxReset14.Checked)
\r
156 _config.ResetHours.Add(14);
\r
158 _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;
\r
159 _main.UpdateFighterPower();
\r
160 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
162 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
163 foreach (var name in _config.Sounds.SoundNames)
\r
164 _config.Sounds[name] = _soundSetting[name];
\r
167 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
171 if (!ValidatePortNumber(textBoxListen, out listen))
\r
173 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
175 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
177 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
183 private bool ApplyProxySettings(int listen, int port)
\r
185 _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;
\r
186 _config.Proxy.Listen = listen;
\r
187 _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;
\r
188 if (_config.Proxy.UseUpstream)
\r
189 _config.Proxy.UpstreamPort = port;
\r
190 if (!_main.ApplyProxySetting())
\r
192 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
196 private void ApplyLogSettings()
\r
198 _config.Log.On = checkBoxOutput.Checked;
\r
199 _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;
\r
200 _config.Log.OutputDir = textBoxOutput.Text;
\r
201 _main.ApplyLogSetting();
\r
204 private void ApplyDebugSettings()
\r
206 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
207 _config.DebugLogFile = textBoxDebugLog.Text;
\r
208 _main.ApplyDebugLogSetting();
\r
211 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
213 _soundSetting[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
216 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
218 if (listBoxSoundFile.SelectedItem == null)
\r
220 textBoxSoundFile.Text = _soundSetting[(string)listBoxSoundFile.SelectedItem];
\r
221 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
224 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
226 openSoundFileDialog.FileName = textBoxSoundFile.Text;
\r
227 openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";
\r
228 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
230 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
231 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
234 private void buttonPlay_Click(object sender, EventArgs e)
\r
236 _main.PlaySound(_soundSetting[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
239 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
241 _main.ResetAchievemnt();
\r
244 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
246 linkLabelProductName.LinkVisited = true;
\r
247 Process.Start(Home);
\r
250 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
252 var off = ((RadioButton)sender).Checked;
\r
253 textBoxPort.Enabled = !off;
\r
256 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
258 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
259 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
260 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
263 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
265 var s = textBox.Text;
\r
266 if (!int.TryParse(s, out result))
\r
268 ShowToolTip("数字を入力してください。", textBox);
\r
273 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
279 private void ShowToolTip(string message, Control control)
\r
281 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
282 toolTipError.Show(message, control, 0, control.Height, 3000);
\r
285 private void textBox_Enter(object sender, EventArgs e)
\r
287 toolTipError.Hide((Control)sender);
\r
290 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\r
292 openDebugLogDialog.FileName = textBoxDebugLog.Text;
\r
293 openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);
\r
294 if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)
\r
295 textBoxDebugLog.Text = openDebugLogDialog.FileName;
\r
296 textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);
\r
299 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
301 _main.SetPlayLog(textBoxDebugLog.Text);
\r
302 DialogResult = DialogResult.Cancel;
\r