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
20 using System.Windows.Forms;
\r
22 namespace KancolleSniffer
\r
24 public partial class ConfigDialog : Form
\r
26 private readonly Config _config;
\r
27 private readonly MainForm _main;
\r
28 private readonly NotificationConfigDialog _notificationConfigDialog;
\r
30 private readonly Dictionary<string, NotificationSpec> _notificationSettings =
\r
31 new Dictionary<string, NotificationSpec>();
\r
33 private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();
\r
34 private const string Home = "http://kancollesniffer.osdn.jp/";
\r
35 private Point _prevPosition = new Point(int.MinValue, int.MinValue);
\r
37 public List<string> RepeatSettingsChanged { get; } = new List<string>();
\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
54 {NotificationType.Repeat, checkBoxRepeat}
\r
58 private void ConfigDialog_Load(object sender, EventArgs e)
\r
60 if (_prevPosition.X != int.MinValue)
\r
61 Location = _prevPosition;
\r
62 _main.CheckVersionUp((current, latest) =>
\r
64 labelVersion.Text = "バージョン" + current;
\r
65 labelLatest.Text = double.Parse(current) >= double.Parse(latest) ? "最新です" : "最新は" + latest + "です";
\r
67 labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;
\r
69 checkBoxTopMost.Checked = _config.TopMost;
\r
70 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
71 checkBoxExitSilently.Checked = _config.ExitSilently;
\r
72 checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;
\r
73 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\r
75 checkBoxFlash.Checked = (_config.NotificationFlags & NotificationType.FlashWindow) != 0;
\r
76 checkBoxBalloon.Checked = (_config.NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
77 checkBoxSound.Checked = (_config.NotificationFlags & NotificationType.PlaySound) != 0;
\r
78 checkBoxRepeat.Checked = (_config.NotificationFlags & NotificationType.Repeat) != 0;
\r
79 foreach (var name in Config.NotificationNames)
\r
80 _notificationSettings[name] = _config.Notifications[name];
\r
81 numericUpDownMarginShips.Value = _config.MarginShips;
\r
82 numericUpDownMarginEquips.Value = _config.MarginEquips;
\r
83 checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);
\r
84 checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);
\r
86 checkBoxReset02.Checked = _config.ResetHours.Contains(2);
\r
87 checkBoxReset14.Checked = _config.ResetHours.Contains(14);
\r
88 checkBoxResultRank.Checked = (_config.Spoilers & Spoiler.ResultRank) != 0;
\r
89 checkBoxAirBattleResult.Checked = (_config.Spoilers & Spoiler.AirBattleResult) != 0;
\r
90 checkBoxBattleResult.Checked = (_config.Spoilers & Spoiler.BattleResult) != 0;
\r
91 checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;
\r
93 numericUpDownSoundVolume.Value = _config.Sounds.Volume;
\r
94 foreach (var name in Config.NotificationNames)
\r
95 _soundSettings[name] = _config.Sounds[name];
\r
96 listBoxSoundFile.SelectedIndex = -1;
\r
97 listBoxSoundFile.SelectedIndex = 0;
\r
99 LoadProxySettings();
\r
101 LoadDebugSettings();
\r
103 checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;
\r
104 textBoxKancolleDbToken.Text = _config.KancolleDb.Token;
\r
105 checkBoxPushbulletOn.Checked = _config.Pushbullet.On;
\r
106 textBoxPushbulletToken.Text = _config.Pushbullet.Token;
\r
107 checkBoxPushoverOn.Checked = _config.Pushover.On;
\r
108 textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;
\r
109 textBoxPushoverUserKey.Text = _config.Pushover.UserKey;
\r
112 private void LoadProxySettings()
\r
114 // 見えていないTabPage上でPerformClickは使えない。
\r
115 radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;
\r
116 radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;
\r
117 textBoxListen.Text = _config.Proxy.Listen.ToString("D");
\r
118 radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;
\r
119 radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;
\r
120 textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");
\r
123 private void LoadLogSettings()
\r
125 checkBoxOutput.Checked = _config.Log.On;
\r
126 textBoxOutput.Text = _config.Log.OutputDir;
\r
127 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
128 folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;
\r
129 numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;
\r
132 private void LoadDebugSettings()
\r
134 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
135 textBoxDebugLog.Text = _config.DebugLogFile;
\r
138 private void buttonOk_Click(object sender, EventArgs e)
\r
140 if (!ValidatePorts(out var listen, out var outbound, out _))
\r
142 DialogResult = DialogResult.OK;
\r
143 if (!ApplyProxySettings(listen, outbound))
\r
144 DialogResult = DialogResult.None;
\r
145 ApplyLogSettings();
\r
146 ApplyDebugSettings();
\r
148 _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;
\r
149 _config.KancolleDb.Token = textBoxKancolleDbToken.Text;
\r
150 _config.Pushbullet.On = checkBoxPushbulletOn.Checked;
\r
151 _config.Pushbullet.Token = textBoxPushbulletToken.Text;
\r
152 _config.Pushover.On = checkBoxPushoverOn.Checked;
\r
153 _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;
\r
154 _config.Pushover.UserKey = textBoxPushoverUserKey.Text;
\r
156 _config.TopMost = checkBoxTopMost.Checked;
\r
157 _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;
\r
158 _config.ExitSilently = checkBoxExitSilently.Checked;
\r
159 _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;
\r
160 _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));
\r
161 _config.NotificationFlags = (checkBoxFlash.Checked ? NotificationType.FlashWindow : 0) |
\r
162 (checkBoxBalloon.Checked ? NotificationType.ShowBaloonTip : 0) |
\r
163 (checkBoxSound.Checked ? NotificationType.PlaySound : 0) |
\r
164 (checkBoxRepeat.Checked ? NotificationType.Repeat : 0);
\r
165 _config.MarginShips = (int)numericUpDownMarginShips.Value;
\r
166 _config.MarginEquips = (int)numericUpDownMarginEquips.Value;
\r
168 RepeatSettingsChanged.Clear();
\r
169 var repeatOff = (_config.NotificationFlags & NotificationType.Repeat) == 0;
\r
170 foreach (var name in Config.NotificationNames)
\r
172 var old = _config.Notifications[name];
\r
173 var cur = _notificationSettings[name];
\r
174 if (repeatOff || old.RepeatInterval != cur.RepeatInterval ||
\r
175 (cur.Flags & NotificationType.Repeat) == 0 ||
\r
176 (cur.Flags & NotificationType.Cont) == 0)
\r
178 RepeatSettingsChanged.Add(name);
\r
180 _config.Notifications[name] = cur;
\r
183 _config.NotifyConditions.Clear();
\r
184 if (checkBoxCond40.Checked)
\r
185 _config.NotifyConditions.Add(40);
\r
186 if (checkBoxCond49.Checked)
\r
187 _config.NotifyConditions.Add(49);
\r
189 _config.ResetHours.Clear();
\r
190 if (checkBoxReset02.Checked)
\r
191 _config.ResetHours.Add(2);
\r
192 if (checkBoxReset14.Checked)
\r
193 _config.ResetHours.Add(14);
\r
195 _config.Spoilers = (checkBoxResultRank.Checked ? Spoiler.ResultRank : 0) |
\r
196 (checkBoxAirBattleResult.Checked ? Spoiler.AirBattleResult : 0) |
\r
197 (checkBoxBattleResult.Checked ? Spoiler.BattleResult : 0);
\r
198 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
200 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
201 foreach (var name in Config.NotificationNames)
\r
202 _config.Sounds[name] = _soundSettings[name];
\r
205 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
209 if (!ValidatePortNumber(textBoxListen, out listen))
\r
211 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
213 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
215 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
221 private bool ApplyProxySettings(int listen, int port)
\r
223 _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;
\r
224 _config.Proxy.Listen = listen;
\r
225 _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;
\r
226 if (_config.Proxy.UseUpstream)
\r
227 _config.Proxy.UpstreamPort = port;
\r
228 if (!_main.ApplyProxySetting())
\r
230 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
234 private void ApplyLogSettings()
\r
236 _config.Log.On = checkBoxOutput.Checked;
\r
237 _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;
\r
238 _config.Log.OutputDir = textBoxOutput.Text;
\r
239 _main.ApplyLogSetting();
\r
242 private void ApplyDebugSettings()
\r
244 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
245 _config.DebugLogFile = textBoxDebugLog.Text;
\r
246 _main.ApplyDebugLogSetting();
\r
249 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
251 _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
254 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
256 if (listBoxSoundFile.SelectedItem == null)
\r
258 textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];
\r
259 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
262 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
264 openSoundFileDialog.FileName = textBoxSoundFile.Text;
\r
265 openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";
\r
266 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
268 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
269 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
272 private void buttonPlay_Click(object sender, EventArgs e)
\r
274 _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
277 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
279 _main.ResetAchievemnt();
\r
282 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
284 linkLabelProductName.LinkVisited = true;
\r
285 Process.Start(Home);
\r
288 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
290 var off = ((RadioButton)sender).Checked;
\r
291 textBoxPort.Enabled = !off;
\r
294 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
296 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
297 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
298 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
301 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
303 var s = textBox.Text;
\r
304 if (!int.TryParse(s, out result))
\r
306 ShowToolTip("数字を入力してください。", textBox);
\r
311 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
317 private void ShowToolTip(string message, Control control)
\r
319 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
320 toolTipError.Show(message, control, 0, control.Height, 3000);
\r
323 private void textBox_Enter(object sender, EventArgs e)
\r
325 toolTipError.Hide((Control)sender);
\r
328 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\r
330 openDebugLogDialog.FileName = textBoxDebugLog.Text;
\r
331 openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);
\r
332 if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)
\r
333 textBoxDebugLog.Text = openDebugLogDialog.FileName;
\r
334 textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);
\r
337 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
339 _main.SetPlayLog(textBoxDebugLog.Text);
\r
340 DialogResult = DialogResult.Cancel;
\r
343 private void buttonDetailedSettings_Click(object sender, EventArgs e)
\r
345 _notificationConfigDialog.ShowDialog(this);
\r
348 private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)
\r
350 _prevPosition = Location;
\r
353 private void buttonPushbulletTest_Click(object sender, EventArgs e)
\r
357 PushNotification.PushToPushbullet(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");
\r
359 catch (Exception ex)
\r
361 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
365 private void buttonPushoverTest_Click(object sender, EventArgs e)
\r
369 PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,
\r
370 "KancolleSniffer", "うまくいったかな?");
\r
372 catch (Exception ex)
\r
374 MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r