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 NotificationConfigDialog _notificationConfigDialog;
\r
31 private readonly Dictionary<string, NotificationType> _notificationSettings =
\r
32 new Dictionary<string, NotificationType>();
\r
34 private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();
\r
35 private const string Home = "http://kancollesniffer.osdn.jp/";
\r
37 public ConfigDialog(Config config, MainForm main)
\r
39 InitializeComponent();
\r
42 // ReSharper disable once CoVariantArrayConversion
\r
43 listBoxSoundFile.Items.AddRange(Config.NotificationNames);
\r
44 numericUpDownMaterialLogInterval.Maximum = 1440;
\r
46 _notificationConfigDialog = new NotificationConfigDialog(_notificationSettings,
\r
47 new Dictionary<NotificationType, CheckBox>
\r
49 {NotificationType.FlashWindow, checkBoxFlash},
\r
50 {NotificationType.ShowBaloonTip, checkBoxBalloon},
\r
51 {NotificationType.PlaySound, checkBoxSound}
\r
55 private void ConfigDialog_Load(object sender, EventArgs e)
\r
57 var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));
\r
58 labelVersion.Text = "バージョン" + version;
\r
59 SetLatestVersion(version);
\r
60 labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;
\r
62 checkBoxTopMost.Checked = _config.TopMost;
\r
63 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
64 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\r
66 checkBoxFlash.Checked = _config.FlashWindow;
\r
67 checkBoxBalloon.Checked = _config.ShowBaloonTip;
\r
68 checkBoxSound.Checked = _config.PlaySound;
\r
69 foreach (var name in Config.NotificationNames)
\r
70 _notificationSettings[name] = _config.Notifications[name];
\r
71 numericUpDownMarginShips.Value = _config.MarginShips;
\r
72 numericUpDownMarginEquips.Value = _config.MarginEquips;
\r
73 checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);
\r
74 checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);
\r
76 checkBoxReset02.Checked = _config.ResetHours.Contains(2);
\r
77 checkBoxReset14.Checked = _config.ResetHours.Contains(14);
\r
78 radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;
\r
79 radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;
\r
80 checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;
\r
82 numericUpDownSoundVolume.Value = _config.Sounds.Volume;
\r
83 foreach (var name in Config.NotificationNames)
\r
84 _soundSettings[name] = _config.Sounds[name];
\r
85 listBoxSoundFile.SelectedIndex = -1;
\r
86 listBoxSoundFile.SelectedIndex = 0;
\r
88 LoadProxySettings();
\r
90 LoadDebugSettings();
\r
91 checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;
\r
92 textBoxKancolleDbToken.Text = _config.KancolleDb.Token;
\r
95 private void LoadProxySettings()
\r
97 // 見えていないTabPage上でPerformClickは使えない。
\r
98 radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;
\r
99 radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;
\r
100 textBoxListen.Text = _config.Proxy.Listen.ToString("D");
\r
101 radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;
\r
102 radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;
\r
103 textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");
\r
106 private void LoadLogSettings()
\r
108 checkBoxOutput.Checked = _config.Log.On;
\r
109 textBoxOutput.Text = _config.Log.OutputDir;
\r
110 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
111 folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;
\r
112 numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;
\r
115 private void LoadDebugSettings()
\r
117 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
118 textBoxDebugLog.Text = _config.DebugLogFile;
\r
121 private async void SetLatestVersion(string version)
\r
125 var req = WebRequest.Create(Home + "version");
\r
126 var response = await req.GetResponseAsync();
\r
127 var stream = response.GetResponseStream();
\r
128 if (stream == null)
\r
130 using (var reader = new StreamReader(stream))
\r
132 var str = await reader.ReadLineAsync();
\r
135 Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));
\r
137 catch (InvalidOperationException)
\r
142 catch (WebException)
\r
147 private void buttonOk_Click(object sender, EventArgs e)
\r
149 int listen, outbound, server;
\r
150 if (!ValidatePorts(out listen, out outbound, out server))
\r
152 DialogResult = DialogResult.OK;
\r
153 if (!ApplyProxySettings(listen, outbound))
\r
154 DialogResult = DialogResult.None;
\r
155 ApplyLogSettings();
\r
156 ApplyDebugSettings();
\r
157 _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;
\r
158 _config.KancolleDb.Token = textBoxKancolleDbToken.Text;
\r
160 _config.TopMost = checkBoxTopMost.Checked;
\r
161 _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;
\r
162 _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));
\r
163 _config.FlashWindow = checkBoxFlash.Checked;
\r
164 _config.ShowBaloonTip = checkBoxBalloon.Checked;
\r
165 _config.PlaySound = checkBoxSound.Checked;
\r
166 foreach (var name in Config.NotificationNames)
\r
167 _config.Notifications[name] = _notificationSettings[name];
\r
168 _config.MarginShips = (int)numericUpDownMarginShips.Value;
\r
169 _config.MarginEquips = (int)numericUpDownMarginEquips.Value;
\r
171 _config.NotifyConditions.Clear();
\r
172 if (checkBoxCond40.Checked)
\r
173 _config.NotifyConditions.Add(40);
\r
174 if (checkBoxCond49.Checked)
\r
175 _config.NotifyConditions.Add(49);
\r
177 _config.ResetHours.Clear();
\r
178 if (checkBoxReset02.Checked)
\r
179 _config.ResetHours.Add(2);
\r
180 if (checkBoxReset14.Checked)
\r
181 _config.ResetHours.Add(14);
\r
183 _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;
\r
184 _main.UpdateFighterPower();
\r
185 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
187 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
188 foreach (var name in Config.NotificationNames)
\r
189 _config.Sounds[name] = _soundSettings[name];
\r
192 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
196 if (!ValidatePortNumber(textBoxListen, out listen))
\r
198 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
200 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
202 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
208 private bool ApplyProxySettings(int listen, int port)
\r
210 _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;
\r
211 _config.Proxy.Listen = listen;
\r
212 _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;
\r
213 if (_config.Proxy.UseUpstream)
\r
214 _config.Proxy.UpstreamPort = port;
\r
215 if (!_main.ApplyProxySetting())
\r
217 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
221 private void ApplyLogSettings()
\r
223 _config.Log.On = checkBoxOutput.Checked;
\r
224 _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;
\r
225 _config.Log.OutputDir = textBoxOutput.Text;
\r
226 _main.ApplyLogSetting();
\r
229 private void ApplyDebugSettings()
\r
231 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
232 _config.DebugLogFile = textBoxDebugLog.Text;
\r
233 _main.ApplyDebugLogSetting();
\r
236 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
238 _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
241 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
243 if (listBoxSoundFile.SelectedItem == null)
\r
245 textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];
\r
246 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
249 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
251 openSoundFileDialog.FileName = textBoxSoundFile.Text;
\r
252 openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";
\r
253 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
255 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
256 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
259 private void buttonPlay_Click(object sender, EventArgs e)
\r
261 _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
264 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
266 _main.ResetAchievemnt();
\r
269 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
271 linkLabelProductName.LinkVisited = true;
\r
272 Process.Start(Home);
\r
275 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
277 var off = ((RadioButton)sender).Checked;
\r
278 textBoxPort.Enabled = !off;
\r
281 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
283 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
284 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
285 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
288 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
290 var s = textBox.Text;
\r
291 if (!int.TryParse(s, out result))
\r
293 ShowToolTip("数字を入力してください。", textBox);
\r
298 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
304 private void ShowToolTip(string message, Control control)
\r
306 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
307 toolTipError.Show(message, control, 0, control.Height, 3000);
\r
310 private void textBox_Enter(object sender, EventArgs e)
\r
312 toolTipError.Hide((Control)sender);
\r
315 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\r
317 openDebugLogDialog.FileName = textBoxDebugLog.Text;
\r
318 openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);
\r
319 if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)
\r
320 textBoxDebugLog.Text = openDebugLogDialog.FileName;
\r
321 textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);
\r
324 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
326 _main.SetPlayLog(textBoxDebugLog.Text);
\r
327 DialogResult = DialogResult.Cancel;
\r
330 private void buttonDetailedSettings_Click(object sender, EventArgs e)
\r
332 _notificationConfigDialog.ShowDialog(this);
\r