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
57 private void ConfigDialog_Load(object sender, EventArgs e)
\r
59 if (_prevPosition.X != int.MinValue)
\r
60 Location = _prevPosition;
\r
61 _main.CheckVersionUp((current, latest) =>
\r
63 labelVersion.Text = "バージョン" + current;
\r
64 labelLatest.Text = double.Parse(current) >= double.Parse(latest) ? "最新です" : "最新は" + latest + "です";
\r
66 labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;
\r
68 checkBoxTopMost.Checked = _config.TopMost;
\r
69 checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;
\r
70 checkBoxExitSilently.Checked = _config.ExitSilently;
\r
71 checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;
\r
72 comboBoxZoom.SelectedItem = _config.Zoom + "%";
\r
74 checkBoxFlash.Checked = _config.FlashWindow;
\r
75 checkBoxBalloon.Checked = _config.ShowBaloonTip;
\r
76 checkBoxSound.Checked = _config.PlaySound;
\r
77 foreach (var name in Config.NotificationNames)
\r
78 _notificationSettings[name] = _config.Notifications[name];
\r
79 numericUpDownMarginShips.Value = _config.MarginShips;
\r
80 numericUpDownMarginEquips.Value = _config.MarginEquips;
\r
81 checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);
\r
82 checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);
\r
84 checkBoxReset02.Checked = _config.ResetHours.Contains(2);
\r
85 checkBoxReset14.Checked = _config.ResetHours.Contains(14);
\r
86 radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;
\r
87 radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;
\r
88 checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;
\r
90 numericUpDownSoundVolume.Value = _config.Sounds.Volume;
\r
91 foreach (var name in Config.NotificationNames)
\r
92 _soundSettings[name] = _config.Sounds[name];
\r
93 listBoxSoundFile.SelectedIndex = -1;
\r
94 listBoxSoundFile.SelectedIndex = 0;
\r
96 LoadProxySettings();
\r
98 LoadDebugSettings();
\r
100 checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;
\r
101 textBoxKancolleDbToken.Text = _config.KancolleDb.Token;
\r
102 checkBoxPushbulletOn.Checked = _config.Pushbullet.On;
\r
103 textBoxPushbulletToken.Text = _config.Pushbullet.Token;
\r
104 checkBoxPushoverOn.Checked = _config.Pushover.On;
\r
105 textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;
\r
106 textBoxPushoverUserKey.Text = _config.Pushover.UserKey;
\r
109 private void LoadProxySettings()
\r
111 // 見えていないTabPage上でPerformClickは使えない。
\r
112 radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;
\r
113 radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;
\r
114 textBoxListen.Text = _config.Proxy.Listen.ToString("D");
\r
115 radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;
\r
116 radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;
\r
117 textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");
\r
120 private void LoadLogSettings()
\r
122 checkBoxOutput.Checked = _config.Log.On;
\r
123 textBoxOutput.Text = _config.Log.OutputDir;
\r
124 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
125 folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;
\r
126 numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;
\r
129 private void LoadDebugSettings()
\r
131 checkBoxDebugLog.Checked = _config.DebugLogging;
\r
132 textBoxDebugLog.Text = _config.DebugLogFile;
\r
135 private void buttonOk_Click(object sender, EventArgs e)
\r
137 if (!ValidatePorts(out var listen, out var outbound, out _))
\r
139 DialogResult = DialogResult.OK;
\r
140 if (!ApplyProxySettings(listen, outbound))
\r
141 DialogResult = DialogResult.None;
\r
142 ApplyLogSettings();
\r
143 ApplyDebugSettings();
\r
145 _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;
\r
146 _config.KancolleDb.Token = textBoxKancolleDbToken.Text;
\r
147 _config.Pushbullet.On = checkBoxPushbulletOn.Checked;
\r
148 _config.Pushbullet.Token = textBoxPushbulletToken.Text;
\r
149 _config.Pushover.On = checkBoxPushoverOn.Checked;
\r
150 _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;
\r
151 _config.Pushover.UserKey = textBoxPushoverUserKey.Text;
\r
153 _config.TopMost = checkBoxTopMost.Checked;
\r
154 _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;
\r
155 _config.ExitSilently = checkBoxExitSilently.Checked;
\r
156 _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;
\r
157 _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));
\r
158 _config.FlashWindow = checkBoxFlash.Checked;
\r
159 _config.ShowBaloonTip = checkBoxBalloon.Checked;
\r
160 _config.PlaySound = checkBoxSound.Checked;
\r
161 _config.MarginShips = (int)numericUpDownMarginShips.Value;
\r
162 _config.MarginEquips = (int)numericUpDownMarginEquips.Value;
\r
164 RepeatSettingsChanged.Clear();
\r
165 foreach (var name in Config.NotificationNames)
\r
167 var old = _config.Notifications[name];
\r
168 var cur = _notificationSettings[name];
\r
169 if (old.RepeatInterval != cur.RepeatInterval ||
\r
170 (old.Flags & NotificationType.Repeat) != (cur.Flags & NotificationType.Repeat))
\r
172 RepeatSettingsChanged.Add(name);
\r
174 _config.Notifications[name] = cur;
\r
177 _config.NotifyConditions.Clear();
\r
178 if (checkBoxCond40.Checked)
\r
179 _config.NotifyConditions.Add(40);
\r
180 if (checkBoxCond49.Checked)
\r
181 _config.NotifyConditions.Add(49);
\r
183 _config.ResetHours.Clear();
\r
184 if (checkBoxReset02.Checked)
\r
185 _config.ResetHours.Add(2);
\r
186 if (checkBoxReset14.Checked)
\r
187 _config.ResetHours.Add(14);
\r
189 _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;
\r
190 _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;
\r
192 _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;
\r
193 foreach (var name in Config.NotificationNames)
\r
194 _config.Sounds[name] = _soundSettings[name];
\r
197 private bool ValidatePorts(out int listen, out int outbound, out int server)
\r
201 if (!ValidatePortNumber(textBoxListen, out listen))
\r
203 if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))
\r
205 if (radioButtonUpstreamOn.Checked && listen == outbound)
\r
207 ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);
\r
213 private bool ApplyProxySettings(int listen, int port)
\r
215 _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;
\r
216 _config.Proxy.Listen = listen;
\r
217 _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;
\r
218 if (_config.Proxy.UseUpstream)
\r
219 _config.Proxy.UpstreamPort = port;
\r
220 if (!_main.ApplyProxySetting())
\r
222 textBoxListen.Text = _config.Proxy.Listen.ToString();
\r
226 private void ApplyLogSettings()
\r
228 _config.Log.On = checkBoxOutput.Checked;
\r
229 _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;
\r
230 _config.Log.OutputDir = textBoxOutput.Text;
\r
231 _main.ApplyLogSetting();
\r
234 private void ApplyDebugSettings()
\r
236 _config.DebugLogging = checkBoxDebugLog.Checked;
\r
237 _config.DebugLogFile = textBoxDebugLog.Text;
\r
238 _main.ApplyDebugLogSetting();
\r
241 private void textBoxSoundFile_TextChanged(object sender, EventArgs e)
\r
243 _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;
\r
246 private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)
\r
248 if (listBoxSoundFile.SelectedItem == null)
\r
250 textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];
\r
251 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
254 private void buttonOpenFile_Click(object sender, EventArgs e)
\r
256 openSoundFileDialog.FileName = textBoxSoundFile.Text;
\r
257 openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";
\r
258 if (openSoundFileDialog.ShowDialog() != DialogResult.OK)
\r
260 textBoxSoundFile.Text = openSoundFileDialog.FileName;
\r
261 textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);
\r
264 private void buttonPlay_Click(object sender, EventArgs e)
\r
266 _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);
\r
269 private void buttonResetAchievement_Click(object sender, EventArgs e)
\r
271 _main.ResetAchievemnt();
\r
274 private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
\r
276 linkLabelProductName.LinkVisited = true;
\r
277 Process.Start(Home);
\r
280 private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)
\r
282 var off = ((RadioButton)sender).Checked;
\r
283 textBoxPort.Enabled = !off;
\r
286 private void buttonOutputDir_Click(object sender, EventArgs e)
\r
288 if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)
\r
289 textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;
\r
290 textBoxOutput.Select(textBoxOutput.Text.Length, 0);
\r
293 private bool ValidatePortNumber(TextBox textBox, out int result)
\r
295 var s = textBox.Text;
\r
296 if (!int.TryParse(s, out result))
\r
298 ShowToolTip("数字を入力してください。", textBox);
\r
303 ShowToolTip("0より大きい数字を入力してください。", textBox);
\r
309 private void ShowToolTip(string message, Control control)
\r
311 tabControl.SelectedTab = (TabPage)control.Parent.Parent;
\r
312 toolTipError.Show(message, control, 0, control.Height, 3000);
\r
315 private void textBox_Enter(object sender, EventArgs e)
\r
317 toolTipError.Hide((Control)sender);
\r
320 private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)
\r
322 openDebugLogDialog.FileName = textBoxDebugLog.Text;
\r
323 openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(textBoxDebugLog.Text);
\r
324 if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)
\r
325 textBoxDebugLog.Text = openDebugLogDialog.FileName;
\r
326 textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);
\r
329 private void buttonPlayDebugLog_Click(object sender, EventArgs e)
\r
331 _main.SetPlayLog(textBoxDebugLog.Text);
\r
332 DialogResult = DialogResult.Cancel;
\r
335 private void buttonDetailedSettings_Click(object sender, EventArgs e)
\r
337 _notificationConfigDialog.ShowDialog(this);
\r
340 private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)
\r
342 _prevPosition = Location;
\r
345 private void buttonPushbulletTest_Click(object sender, EventArgs e)
\r
349 PushNotification.PushToPushbullet(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");
\r
351 catch (Exception ex)
\r
353 MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
357 private void buttonPushoverTest_Click(object sender, EventArgs e)
\r
361 PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,
\r
362 "KancolleSniffer", "うまくいったかな?");
\r
364 catch (Exception ex)
\r
366 MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r