OSDN Git Service

バージョン11.11の準備
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ConfigDialog.cs
index 20831d4..f9c8194 100644 (file)
@@ -1,24 +1,25 @@
-// Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
+// Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
 // \r
-// This program is part of KancolleSniffer.\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
 //\r
-// KancolleSniffer is free software: you can redistribute it and/or modify\r
-// it under the terms of the GNU General Public License as published by\r
-// the Free Software Foundation, either version 3 of the License, or\r
-// (at your option) any later version.\r
+//    http://www.apache.org/licenses/LICENSE-2.0\r
 //\r
-// This program is distributed in the hope that it will be useful,\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-// GNU General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU General Public License\r
-// along with this program; if not, see <http://www.gnu.org/licenses/>.\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
 \r
 using System;\r
+using System.Collections.Generic;\r
+using System.Diagnostics;\r
+using System.Drawing;\r
 using System.IO;\r
-using System.Linq;\r
 using System.Windows.Forms;\r
+using KancolleSniffer.Net;\r
+using KancolleSniffer.View;\r
 \r
 namespace KancolleSniffer\r
 {\r
@@ -26,42 +27,163 @@ namespace KancolleSniffer
     {\r
         private readonly Config _config;\r
         private readonly MainForm _main;\r
-        private DebugDialog _debugDialog;\r
-        private ProxyDialog _proxyDialog;\r
+\r
+        private readonly Dictionary<string, NotificationSpec> _notificationSettings =\r
+            new Dictionary<string, NotificationSpec>();\r
+\r
+        private readonly Dictionary<string, string> _soundSettings = new Dictionary<string, string>();\r
+        private const string Home = "http://kancollesniffer.osdn.jp/";\r
+        private Point _prevPosition = new Point(int.MinValue, int.MinValue);\r
+\r
+        public List<string> RepeatSettingsChanged { get; } = new List<string>();\r
+        public NotificationConfigDialog NotificationConfigDialog { get; }\r
 \r
         public ConfigDialog(Config config, MainForm main)\r
         {\r
             InitializeComponent();\r
             _config = config;\r
             _main = main;\r
+            // ReSharper disable once CoVariantArrayConversion\r
+            listBoxSoundFile.Items.AddRange(Config.NotificationNames);\r
+            numericUpDownMaterialLogInterval.Maximum = 1440;\r
+\r
+            NotificationConfigDialog = new NotificationConfigDialog(_notificationSettings,\r
+                new Dictionary<NotificationType, CheckBox>\r
+                {\r
+                    {NotificationType.FlashWindow, checkBoxFlash},\r
+                    {NotificationType.ShowBaloonTip, checkBoxBalloon},\r
+                    {NotificationType.PlaySound, checkBoxSound},\r
+                    {NotificationType.Repeat, checkBoxRepeat}\r
+                });\r
         }\r
 \r
         private void ConfigDialog_Load(object sender, EventArgs e)\r
         {\r
+            if (_prevPosition.X != int.MinValue)\r
+                Location = _prevPosition;\r
+            _main.CheckVersionUp((current, latest) =>\r
+            {\r
+                labelVersion.Text = "バージョン" + current;\r
+                labelLatest.Text = current == latest ? "最新です" : "最新は" + latest + "です";\r
+            });\r
+            labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
+\r
             checkBoxTopMost.Checked = _config.TopMost;\r
-            checkBoxFlash.Checked = _config.FlashWindow;\r
-            checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
-            groupBoxSound.Enabled = checkBoxSound.Checked = _config.PlaySound;\r
+            checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
+            checkBoxExitSilently.Checked = _config.ExitSilently;\r
+            checkBoxLocationPerMachine.Checked = _config.SaveLocationPerMachine;\r
+            comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
+\r
+            checkBoxFlash.Checked = (_config.NotificationFlags & NotificationType.FlashWindow) != 0;\r
+            checkBoxBalloon.Checked = (_config.NotificationFlags & NotificationType.ShowBaloonTip) != 0;\r
+            checkBoxSound.Checked = (_config.NotificationFlags & NotificationType.PlaySound) != 0;\r
+            checkBoxRepeat.Checked = (_config.NotificationFlags & NotificationType.Repeat) != 0;\r
+            foreach (var name in Config.NotificationNames)\r
+                _notificationSettings[name] = _config.Notifications[name];\r
             numericUpDownMarginShips.Value = _config.MarginShips;\r
+            numericUpDownMarginEquips.Value = _config.MarginEquips;\r
+            checkBoxCond40.Checked = _config.NotifyConditions.Contains(40);\r
+            checkBoxCond49.Checked = _config.NotifyConditions.Contains(49);\r
+\r
+            checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
+            checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
+            checkBoxResultRank.Checked = (_config.Spoilers & Spoiler.ResultRank) != 0;\r
+            checkBoxAirBattleResult.Checked = (_config.Spoilers & Spoiler.AirBattleResult) != 0;\r
+            checkBoxBattleResult.Checked = (_config.Spoilers & Spoiler.BattleResult) != 0;\r
+            checkBoxNextCell.Checked = (_config.Spoilers & Spoiler.NextCell) != 0;\r
+            checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
+\r
+            numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
+            foreach (var name in Config.NotificationNames)\r
+                _soundSettings[name] = _config.Sounds[name];\r
+            listBoxSoundFile.SelectedIndex = -1;\r
+            listBoxSoundFile.SelectedIndex = 0;\r
 \r
-            checkBoxReset02.Checked = _config.ResetHours.Any(x => x == 2);\r
-            checkBoxReset14.Checked = _config.ResetHours.Any(x => x == 14);\r
+            LoadProxySettings();\r
+            LoadLogSettings();\r
+            LoadDebugSettings();\r
 \r
-            numericUpDownSoundVolume.Value = _config.SoundVolume;\r
-            textBoxMissionSoundFile.Text = _config.MissionSoundFile;\r
-            textBoxNDockSoundFile.Text = _config.NDockSoundFile;\r
-            textBoxKDockSoundFile.Text = _config.KDockSoundFile;\r
-            textBoxMaxShipsSoundFile.Text = _config.MaxShipsSoundFile;\r
-            textBoxDamagedShipSoundFile.Text = _config.DamagedShipSoundFile;\r
+            checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
+            textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
+            checkBoxPushoverOn.Checked = _config.Pushover.On;\r
+            textBoxPushoverApiKey.Text = _config.Pushover.ApiKey;\r
+            textBoxPushoverUserKey.Text = _config.Pushover.UserKey;\r
+        }\r
+\r
+        private void LoadProxySettings()\r
+        {\r
+            // 見えていないTabPage上でPerformClickは使えない。\r
+            radioButtonAutoConfigOn.Checked = _config.Proxy.Auto;\r
+            radioButtonAutoConfigOff.Checked = !_config.Proxy.Auto;\r
+            textBoxListen.Text = _config.Proxy.Listen.ToString("D");\r
+            radioButtonUpstreamOn.Checked = _config.Proxy.UseUpstream;\r
+            radioButtonUpstreamOff.Checked = !_config.Proxy.UseUpstream;\r
+            textBoxPort.Text = _config.Proxy.UpstreamPort.ToString("D");\r
+        }\r
+\r
+        private void LoadLogSettings()\r
+        {\r
+            checkBoxOutput.Checked = _config.Log.On;\r
+            textBoxOutput.Text = _config.Log.OutputDir;\r
+            textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
+            folderBrowserDialogOutputDir.SelectedPath = _config.Log.OutputDir;\r
+            numericUpDownMaterialLogInterval.Value = _config.Log.MaterialLogInterval;\r
+        }\r
+\r
+        private void LoadDebugSettings()\r
+        {\r
+            checkBoxDebugLog.Checked = _config.DebugLogging;\r
+            textBoxDebugLog.Text = _config.DebugLogFile;\r
         }\r
 \r
         private void buttonOk_Click(object sender, EventArgs e)\r
         {\r
+            if (!ValidatePorts(out var listen, out var outbound, out _))\r
+                return;\r
+            DialogResult = DialogResult.OK;\r
+            if (!ApplyProxySettings(listen, outbound))\r
+                DialogResult = DialogResult.None;\r
+            ApplyLogSettings();\r
+            ApplyDebugSettings();\r
+\r
+            _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
+            _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
+            _config.Pushover.On = checkBoxPushoverOn.Checked;\r
+            _config.Pushover.ApiKey = textBoxPushoverApiKey.Text;\r
+            _config.Pushover.UserKey = textBoxPushoverUserKey.Text;\r
+\r
             _config.TopMost = checkBoxTopMost.Checked;\r
-            _config.FlashWindow = checkBoxFlash.Checked;\r
-            _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
-            _config.PlaySound = checkBoxSound.Checked;\r
+            _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
+            _config.ExitSilently = checkBoxExitSilently.Checked;\r
+            _config.SaveLocationPerMachine = checkBoxLocationPerMachine.Checked;\r
+            _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
+            _config.NotificationFlags = (checkBoxFlash.Checked ? NotificationType.FlashWindow : 0) |\r
+                                        (checkBoxBalloon.Checked ? NotificationType.ShowBaloonTip : 0) |\r
+                                        (checkBoxSound.Checked ? NotificationType.PlaySound : 0) |\r
+                                        (checkBoxRepeat.Checked ? NotificationType.Repeat : 0);\r
             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
+            _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\r
+\r
+            RepeatSettingsChanged.Clear();\r
+            var repeatOff = (_config.NotificationFlags & NotificationType.Repeat) == 0;\r
+            foreach (var name in Config.NotificationNames)\r
+            {\r
+                var old = _config.Notifications[name];\r
+                var cur = _notificationSettings[name];\r
+                if (repeatOff || old.RepeatInterval != cur.RepeatInterval ||\r
+                    (cur.Flags & NotificationType.Repeat) == 0 ||\r
+                    (cur.Flags & NotificationType.Cont) == 0)\r
+                {\r
+                    RepeatSettingsChanged.Add(name);\r
+                }\r
+                _config.Notifications[name] = cur;\r
+            }\r
+\r
+            _config.NotifyConditions.Clear();\r
+            if (checkBoxCond40.Checked)\r
+                _config.NotifyConditions.Add(40);\r
+            if (checkBoxCond49.Checked)\r
+                _config.NotifyConditions.Add(49);\r
 \r
             _config.ResetHours.Clear();\r
             if (checkBoxReset02.Checked)\r
@@ -69,69 +191,242 @@ namespace KancolleSniffer
             if (checkBoxReset14.Checked)\r
                 _config.ResetHours.Add(14);\r
 \r
-            _config.SoundVolume = (int)numericUpDownSoundVolume.Value;\r
-            _config.MissionSoundFile = textBoxMissionSoundFile.Text;\r
-            _config.NDockSoundFile = textBoxNDockSoundFile.Text;\r
-            _config.KDockSoundFile = textBoxKDockSoundFile.Text;\r
-            _config.MaxShipsSoundFile = textBoxMaxShipsSoundFile.Text;\r
-            _config.DamagedShipSoundFile = textBoxDamagedShipSoundFile.Text;\r
+            _config.Spoilers = (checkBoxResultRank.Checked ? Spoiler.ResultRank : 0) |\r
+                               (checkBoxAirBattleResult.Checked ? Spoiler.AirBattleResult : 0) |\r
+                               (checkBoxBattleResult.Checked ? Spoiler.BattleResult : 0) |\r
+                               (checkBoxNextCell.Checked ? Spoiler.NextCell : 0);\r
+            _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
+\r
+            _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
+            foreach (var name in Config.NotificationNames)\r
+                _config.Sounds[name] = MakePathRooted(_soundSettings[name]);\r
+        }\r
+\r
+        private bool ValidatePorts(out int listen, out int outbound, out int server)\r
+        {\r
+            outbound = -1;\r
+            server = -1;\r
+            if (!ValidatePortNumber(textBoxListen, out listen))\r
+                return false;\r
+            if (radioButtonUpstreamOn.Checked && !ValidatePortNumber(textBoxPort, out outbound))\r
+                return false;\r
+            if (radioButtonUpstreamOn.Checked && listen == outbound)\r
+            {\r
+                ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
+                return false;\r
+            }\r
+            return true;\r
         }\r
 \r
-        private void checkBoxSound_CheckedChanged(object sender, EventArgs e)\r
+        private bool ApplyProxySettings(int listen, int port)\r
         {\r
-            groupBoxSound.Enabled = checkBoxSound.Checked;\r
+            _config.Proxy.Auto = radioButtonAutoConfigOn.Checked;\r
+            _config.Proxy.Listen = listen;\r
+            _config.Proxy.UseUpstream = radioButtonUpstreamOn.Checked;\r
+            if (_config.Proxy.UseUpstream)\r
+                _config.Proxy.UpstreamPort = port;\r
+            if (!_main.ApplyProxySetting())\r
+                return false;\r
+            textBoxListen.Text = _config.Proxy.Listen.ToString();\r
+            return true;\r
         }\r
 \r
-        private void buttonMissionOpenFile_Click(object sender, EventArgs e)\r
+        private void ApplyLogSettings()\r
         {\r
-            ChooseSoundFile(textBoxMissionSoundFile);\r
+            _config.Log.On = checkBoxOutput.Checked;\r
+            _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
+            _config.Log.OutputDir = MakePathRooted(textBoxOutput.Text);\r
+            _main.ApplyLogSetting();\r
         }\r
 \r
-        private void buttonNDockOpenFile_Click(object sender, EventArgs e)\r
+        private void ApplyDebugSettings()\r
         {\r
-            ChooseSoundFile(textBoxNDockSoundFile);\r
+            _config.DebugLogging = checkBoxDebugLog.Checked;\r
+            _config.DebugLogFile = MakePathRooted(textBoxDebugLog.Text);\r
+            _main.ApplyDebugLogSetting();\r
         }\r
 \r
-        private void buttonKDockOpenFile_Click(object sender, EventArgs e)\r
+        private string MakePathRooted(string path)\r
         {\r
-            ChooseSoundFile(textBoxKDockSoundFile);\r
+            try\r
+            {\r
+                return string.IsNullOrWhiteSpace(path)\r
+                    ? ""\r
+                    : Path.IsPathRooted(path)\r
+                        ? path\r
+                        : Path.Combine(Config.BaseDir, path);\r
+            }\r
+            catch (ArgumentException)\r
+            {\r
+                return "";\r
+            }\r
         }\r
 \r
-        private void buttonMaxShipsOpenFile_Click(object sender, EventArgs e)\r
+        private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
         {\r
-            ChooseSoundFile(textBoxMaxShipsSoundFile);\r
+            _soundSettings[(string)listBoxSoundFile.SelectedItem] = textBoxSoundFile.Text;\r
         }\r
 \r
-        private void buttonDamagedShipOpenFile_Click(object sender, EventArgs e)\r
+        private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            ChooseSoundFile(textBoxDamagedShipSoundFile);\r
+            if (listBoxSoundFile.SelectedItem == null)\r
+                return;\r
+            textBoxSoundFile.Text = _soundSettings[(string)listBoxSoundFile.SelectedItem];\r
+            textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
         }\r
 \r
-        private void ChooseSoundFile(TextBox textBox)\r
+        private void buttonOpenFile_Click(object sender, EventArgs e)\r
         {\r
-            openFileDialog.FileName = textBox.Text;\r
-            openFileDialog.InitialDirectory = Path.GetDirectoryName(textBox.Text) ?? "";\r
-            if (openFileDialog.ShowDialog() == DialogResult.OK)\r
-                textBox.Text = openFileDialog.FileName;\r
+            SetInitialPath(openSoundFileDialog, textBoxSoundFile.Text);\r
+            if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
+                return;\r
+            textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
+            textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
         }\r
 \r
-        private void DebugToolStripMenuItem_Click(object sender, EventArgs e)\r
+        private void buttonPlay_Click(object sender, EventArgs e)\r
         {\r
-            if (_debugDialog == null)\r
-                _debugDialog = new DebugDialog(_config, _main);\r
-            _debugDialog.ShowDialog(this);\r
+            _main.PlaySound(_soundSettings[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
         }\r
 \r
         private void buttonResetAchievement_Click(object sender, EventArgs e)\r
         {\r
-            _main.ResetAchievemnt();\r
+            _main.ResetAchievement();\r
+        }\r
+\r
+        private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
+        {\r
+            linkLabelProductName.LinkVisited = true;\r
+            Process.Start(Home);\r
+        }\r
+\r
+        private void radioButtonUpstreamOff_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            var off = ((RadioButton)sender).Checked;\r
+            textBoxPort.Enabled = !off;\r
+        }\r
+\r
+        private void buttonOutputDir_Click(object sender, EventArgs e)\r
+        {\r
+            if (folderBrowserDialogOutputDir.ShowDialog(this) == DialogResult.OK)\r
+                textBoxOutput.Text = folderBrowserDialogOutputDir.SelectedPath;\r
+            textBoxOutput.Select(textBoxOutput.Text.Length, 0);\r
+        }\r
+\r
+        private bool ValidatePortNumber(TextBox textBox, out int result)\r
+        {\r
+            var s = textBox.Text;\r
+            if (!int.TryParse(s, out result))\r
+            {\r
+                ShowToolTip("数字を入力してください。", textBox);\r
+                return false;\r
+            }\r
+            if (result <= 0)\r
+            {\r
+                ShowToolTip("0より大きい数字を入力してください。", textBox);\r
+                return false;\r
+            }\r
+            if (result > 65535)\r
+            {\r
+                ShowToolTip("65535以下の数字を入力してください。", textBox);\r
+                return false;\r
+            }\r
+            return true;\r
+        }\r
+\r
+        private readonly ResizableToolTip _toolTip =\r
+            new ResizableToolTip {AutomaticDelay = 0, ToolTipIcon = ToolTipIcon.Error};\r
+\r
+        private void ShowToolTip(string message, Control control)\r
+        {\r
+            tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
+            _toolTip.Show(message, control, 0, control.Height, 3000);\r
+        }\r
+\r
+        private void textBox_Enter(object sender, EventArgs e)\r
+        {\r
+            _toolTip.Hide((Control)sender);\r
+        }\r
+\r
+        private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
+        {\r
+            SetInitialPath(openDebugLogDialog, textBoxDebugLog.Text);\r
+            if (openDebugLogDialog.ShowDialog(this) == DialogResult.OK)\r
+                textBoxDebugLog.Text = openDebugLogDialog.FileName;\r
+            textBoxDebugLog.Select(textBoxDebugLog.Text.Length, 0);\r
+        }\r
+\r
+        private void SetInitialPath(OpenFileDialog dialog, string path)\r
+        {\r
+            var dir = Config.BaseDir;\r
+            var file = "";\r
+            if (!string.IsNullOrWhiteSpace(path))\r
+            {\r
+                var res = Path.GetDirectoryName(path);\r
+                if (res == null) // root\r
+                {\r
+                    dir = path;\r
+                }\r
+                else if (res != "") // contain directory\r
+                {\r
+                    dir = res;\r
+                    file = Path.GetFileName(path);\r
+                }\r
+                else\r
+                {\r
+                    file = path;\r
+                }\r
+            }\r
+            dialog.InitialDirectory = dir;\r
+            dialog.FileName = file;\r
+        }\r
+\r
+        private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
+        {\r
+            _main.SetPlayLog(textBoxDebugLog.Text);\r
+            DialogResult = DialogResult.Cancel;\r
+        }\r
+\r
+        private void buttonDetailedSettings_Click(object sender, EventArgs e)\r
+        {\r
+            NotificationConfigDialog.ShowDialog(this);\r
+        }\r
+\r
+        private void ConfigDialog_FormClosing(object sender, FormClosingEventArgs e)\r
+        {\r
+            _prevPosition = Location;\r
+        }\r
+\r
+        private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
+        {\r
+            try\r
+            {\r
+                PushNotification.PushToPushbullet(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
+        }\r
+\r
+        private void buttonPushoverTest_Click(object sender, EventArgs e)\r
+        {\r
+            try\r
+            {\r
+                PushNotification.PushToPushover(textBoxPushoverApiKey.Text, textBoxPushoverUserKey.Text,\r
+                    "KancolleSniffer", "うまくいったかな?");\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(this, ex.Message, "Pushoverエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
         }\r
 \r
-        private void ProxyToolStripMenuItem_Click(object sender, EventArgs e)\r
+        protected override void ScaleControl(SizeF factor, BoundsSpecified specified)\r
         {\r
-            if (_proxyDialog == null)\r
-                _proxyDialog = new ProxyDialog(_config.Proxy, _main);\r
-            _proxyDialog.ShowDialog(this);\r
+            base.ScaleControl(factor, specified);\r
+            if (factor.Height > 1)\r
+                _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);\r
         }\r
     }\r
 }
\ No newline at end of file