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
21 using KancolleSniffer.Net;
\r
22 using KancolleSniffer.View;
\r
24 namespace KancolleSniffer
\r
26 public partial class ConfigDialog : Form
\r
28 private readonly MainForm _main;
\r
29 private readonly Config _config;
\r
31 private readonly Dictionary<string, NotificationSpec> _notificationSettings =
\r
32 new Dictionary<string, NotificationSpec>();
\r
34 private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();
\r
35 private const string Home = "http://kancollesniffer.osdn.jp/";
\r
36 private Point _prevPosition = new Point(int.MinValue, int.MinValue);
\r
38 public List<string> RepeatSettingsChanged { get; } = new List<string>();
\r
39 public NotificationConfigDialog NotificationConfigDialog { get; }
\r
41 public ConfigDialog(MainForm main)
\r
43 InitializeComponent();
\r
45 _config = main.Config;
\r
46 // ReSharper disable once CoVariantArrayConversion
\r
47 listBoxSoundFile.Items.AddRange(Config.NotificationNames);
\r
48 numericUpDownMaterialLogInterval.Maximum = 1440;
\r
50 NotificationConfigDialog = new NotificationConfigDialog(_notificationSettings,
\r
51 new Dictionary<NotificationType, CheckBox>
\r
53 {NotificationType.FlashWindow, checkBoxFlash},
\r
54 {NotificationType.ShowBaloonTip, checkBoxBalloon},
\r
55 {NotificationType.PlaySound, checkBoxSound},
\r
56 {NotificationType.Repeat, checkBoxRepeat}
\r
60 private void ConfigDialog_Load(object sender, EventArgs e)
\r
62 if (_prevPosition.X != int.MinValue)
\r
63 Location = _prevPosition;
\r
64 _main.CheckVersionUp((current, latest) =>
\r
66 labelVersion.Text = "バージョン" + current;
\r
67 labelLatest.Text = current == latest ? "最新です" : "最新は" + latest + "です";
\r
69 labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;
\r
71 checkBoxTopMost.Checked = _config.TopMost;
\r
72 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
73 checkBoxExitSilently.Checked = _config.ExitSilently;
\r
74 checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;
\r
75 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\r
76 numericUpDownQuest.Value = _config.QuestLines;
\r
78 checkBoxFlash.Checked = (_config.NotificationFlags & NotificationType.FlashWindow) != 0;
\r
79 checkBoxBalloon.Checked = (_config.NotificationFlags & NotificationType.ShowBaloonTip) != 0;
\r
80 checkBoxSound.Checked = (_config.NotificationFlags & NotificationType.PlaySound) != 0;
\r
81 checkBoxRepeat.Checked = (_config.NotificationFlags & NotificationType.Repeat) != 0;
\r
82 foreach (var name in Config.NotificationNames)
\r
83 _notificationSettings[name] = _config.Notifications[name];
\r
84 numericUpDownMarginShips.Value = _config.MarginShips;
\r
85 numericUpDownMarginEquips.Value = _config.MarginEquips;
\r
86 checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);
\r
87 checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);
\r
89 checkBoxReset02.Checked = _config.ResetHours.Contains(2);
\r
90 checkBoxReset14.Checked = _config.ResetHours.Contains(14);
\r
91 checkBoxResultRank.Checked = (_config.Spoilers & Spoiler.ResultRank) != 0;
\r
92 checkBoxAirBattleResult.Checked = (_config.Spoilers & Spoiler.AirBattleResult) != 0;
\r
93 checkBoxBattleResult.Checked = (_config.Spoilers & Spoiler.BattleResult) != 0;
\r
94 checkBoxNextCell.Checked = (_config.Spoilers & Spoiler.NextCell) != 0;
\r
95 checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;
\r
96 checkBoxWarnBadDamageWithDamecon.Checked = _config.WarnBadDamageWithDameCon;
\r
97 checkBoxAutoBattleResult.Checked = _config.ShipList.AutoBattleResult;
\r
99 numericUpDownSoundVolume.Value = _config.Sounds.Volume;
\r
100 foreach (var name in Config.NotificationNames)
\r
101 _soundSettings[name] = _config.Sounds[name];
\r
102 listBoxSoundFile.SelectedIndex = -1;
\r
103 listBoxSoundFile.SelectedIndex = 0;
\r
105 LoadProxySettings();
\r
107 LoadDebugSettings();
\r
109 checkBoxPushbulletOn.Checked = _config.Pushbullet.On;
\r
110 textBoxPushbulletToken.Text = _config.Pushbullet.Token;
\r
111 checkBoxPushoverOn.Checked = _config.Pushover.On;
\r
112 textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;
\r
113 textBoxPushoverUserKey.Text = _config.Pushover.UserKey;
\r
116 private void LoadProxySettings()
\r
118 // 見えていないTabPage上でPerformClickは使えない。
\r
119 radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;
\r
120 radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;
\r
121 textBoxListen.Text = _config.Proxy.Listen.ToString("D");
\r
122 radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;
\r
123 radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;
\r
124 textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");
\r
127 private void LoadLogSettings()
\r
129 checkBoxOutput.Checked = _config.Log.On;
\r
130 textBoxOutput.Text = _config.Log.OutputDir;
\r
131 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
132 folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;
\r
133 numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;
\r
136 private void LoadDebugSettings()
\r
138 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
139 textBoxDebugLog.Text = _config.DebugLogFile;
\r
142 private void buttonOk_Click(object sender, EventArgs e)
\r
144 if (!ValidatePorts(out var listen, out var outbound, out _))
\r
146 DialogResult = DialogResult.OK;
\r
147 if (!ApplyProxySettings(listen, outbound))
\r
148 DialogResult = DialogResult.None;
\r
149 ApplyLogSettings();
\r
150 ApplyDebugSettings();
\r
152 _config.Pushbullet.On = checkBoxPushbulletOn.Checked;
\r
153 _config.Pushbullet.Token = textBoxPushbulletToken.Text;
\r
154 _config.Pushover.On = checkBoxPushoverOn.Checked;
\r
155 _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;
\r
156 _config.Pushover.UserKey = textBoxPushoverUserKey.Text;
\r
158 _config.TopMost = checkBoxTopMost.Checked;
\r
159 _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;
\r
160 _config.ExitSilently = checkBoxExitSilently.Checked;
\r
161 _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;
\r
162 _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));
\r
163 _config.QuestLines = (int)numericUpDownQuest.Value;
\r
164 _config.NotificationFlags = (checkBoxFlash.Checked ? NotificationType.FlashWindow : 0) |
\r
165 (checkBoxBalloon.Checked ? NotificationType.ShowBaloonTip : 0) |
\r
166 (checkBoxSound.Checked ? NotificationType.PlaySound : 0) |
\r
167 (checkBoxRepeat.Checked ? NotificationType.Repeat : 0);
\r
168 _config.MarginShips = (int)numericUpDownMarginShips.Value;
\r
169 _config.MarginEquips = (int)numericUpDownMarginEquips.Value;
\r
171 RepeatSettingsChanged.Clear();
\r
172 var repeatOff = (_config.NotificationFlags & NotificationType.Repeat) == 0;
\r
173 foreach (var name in Config.NotificationNames)
\r
175 var old = _config.Notifications[name];
\r
176 var cur = _notificationSettings[name];
\r
177 if (repeatOff || old.RepeatInterval != cur.RepeatInterval ||
\r
178 (cur.Flags & NotificationType.Repeat) == 0 ||
\r
179 (cur.Flags & NotificationType.Cont) == 0)
\r
181 RepeatSettingsChanged.Add(name);
\r
183 _config.Notifications[name] = cur;
\r
186 _config.NotifyConditions.Clear();
\r
187 if (checkBoxCond40.Checked)
\r
188 _config.NotifyConditions.Add(40);
\r
189 if (checkBoxCond49.Checked)
\r
190 _config.NotifyConditions.Add(49);
\r
192 _config.ResetHours.Clear();
\r
193 if (checkBoxReset02.Checked)
\r
194 _config.ResetHours.Add(2);
\r
195 if (checkBoxReset14.Checked)
\r
196 _config.ResetHours.Add(14);
\r
198 _config.Spoilers = (checkBoxResultRank.Checked ? Spoiler.ResultRank : 0) |
\r
199 (checkBoxAirBattleResult.Checked ? Spoiler.AirBattleResult : 0) |
\r
200 (checkBoxBattleResult.Checked ? Spoiler.BattleResult : 0) |
\r
201 (checkBoxNextCell.Checked ? Spoiler.NextCell : 0);
\r
202 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
203 _config.WarnBadDamageWithDameCon = checkBoxWarnBadDamageWithDamecon.Checked;
\r
204 _config.ShipList.AutoBattleResult = checkBoxAirBattleResult.Checked;
\r
206 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
207 foreach (var name in Config.NotificationNames)
\r
208 _config.Sounds[name] = MakePathRooted(_soundSettings[name]);
\r
211 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
215 if (!ValidatePortNumber(textBoxListen, out listen))
\r
217 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
219 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
221 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
227 private bool ApplyProxySettings(int listen, int port)
\r
229 _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;
\r
230 _config.Proxy.Listen = listen;
\r
231 _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;
\r
232 if (_config.Proxy.UseUpstream)
\r
233 _config.Proxy.UpstreamPort = port;
\r
234 if (!_main.ApplyProxySetting())
\r
236 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
240 private void ApplyLogSettings()
\r
242 _config.Log.On = checkBoxOutput.Checked;
\r
243 _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;
\r
244 _config.Log.OutputDir = MakePathRooted(textBoxOutput.Text);
\r
245 _main.ApplyLogSetting();
\r
248 private void ApplyDebugSettings()
\r
250 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
251 _config.DebugLogFile = MakePathRooted(textBoxDebugLog.Text);
\r
252 _main.ApplyDebugLogSetting();
\r
255 private string MakePathRooted(string path)
\r
259 return string.IsNullOrWhiteSpace(path)
\r
261 : Path.IsPathRooted(path)
\r
263 : Path.Combine(Config.BaseDir, path);
\r
265 catch (ArgumentException)
\r
271 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
273 _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
276 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
278 if (listBoxSoundFile.SelectedItem == null)
\r
280 textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];
\r
281 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
284 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
286 SetInitialPath(openSoundFileDialog, textBoxSoundFile.Text);
\r
287 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
289 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
290 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
293 private void buttonPlay_Click(object sender, EventArgs e)
\r
295 _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
298 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
300 _main.ResetAchievement();
\r
303 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
305 linkLabelProductName.LinkVisited = true;
\r
306 Process.Start(Home);
\r
309 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
311 var off = ((RadioButton)sender).Checked;
\r
312 textBoxPort.Enabled = !off;
\r
315 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
317 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
318 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
319 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
322 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
324 var s = textBox.Text;
\r
325 if (!int.TryParse(s, out result))
\r
327 ShowToolTip("数字を入力してください。", textBox);
\r
332 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
335 if (result > 65535)
\r
337 ShowToolTip("65535以下の数字を入力してください。", textBox);
\r
343 private readonly ResizableToolTip _toolTip =
\r
344 new ResizableToolTip {AutomaticDelay = 0, ToolTipIcon = ToolTipIcon.Error};
\r
346 private void ShowToolTip(string message, Control control)
\r
348 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
349 _toolTip.Show(message, control, 0, control.Height, 3000);
\r
352 private void textBox_Enter(object sender, EventArgs e)
\r
354 _toolTip.Hide((Control)sender);
\r
357 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\r
359 SetInitialPath(openDebugLogDialog, textBoxDebugLog.Text);
\r
360 if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)
\r
361 textBoxDebugLog.Text = openDebugLogDialog.FileName;
\r
362 textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);
\r
365 private void SetInitialPath(OpenFileDialog dialog, string path)
\r
367 var dir = Config.BaseDir;
\r
369 if (!string.IsNullOrWhiteSpace(path))
\r
371 var res = Path.GetDirectoryName(path);
\r
372 if (res == null) // root
\r
376 else if (res != "") // contain directory
\r
379 file = Path.GetFileName(path);
\r
386 dialog.InitialDirectory = dir;
\r
387 dialog.FileName = file;
\r
390 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
392 _main.SetPlayLog(textBoxDebugLog.Text);
\r
393 DialogResult = DialogResult.Cancel;
\r
396 private void buttonDetailedSettings_Click(object sender, EventArgs e)
\r
398 NotificationConfigDialog.ShowDialog(this);
\r
401 private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)
\r
403 _prevPosition = Location;
\r
406 private void buttonPushbulletTest_Click(object sender, EventArgs e)
\r
410 PushNotification.PushToPushbullet(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");
\r
412 catch (Exception ex)
\r
414 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
418 private void buttonPushoverTest_Click(object sender, EventArgs e)
\r
422 PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,
\r
423 "KancolleSniffer", "うまくいったかな?");
\r
425 catch (Exception ex)
\r
427 MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
431 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
\r
433 base.ScaleControl(factor, specified);
\r
434 if (factor.Height > 1)
\r
435 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);
\r