OSDN Git Service

コンバート改装可能な艦娘の艦隊晒し用のデータが正しくないのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ConfigDialog.cs
index e8e3042..656bdd6 100644 (file)
@@ -1,24 +1,23 @@
-// 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.IO;\r
 using System.Linq;\r
+using System.Net;\r
 using System.Windows.Forms;\r
 \r
 namespace KancolleSniffer\r
@@ -27,79 +26,202 @@ namespace KancolleSniffer
     {\r
         private readonly Config _config;\r
         private readonly MainForm _main;\r
-        private DebugDialog _debugDialog;\r
-        private ProxyDialog _proxyDialog;\r
         private readonly Dictionary<string, string> _soundSetting = new Dictionary<string, string>();\r
+        private const string Home = "http://kancollesniffer.osdn.jp/";\r
 \r
         public ConfigDialog(Config config, MainForm main)\r
         {\r
             InitializeComponent();\r
             _config = config;\r
             _main = main;\r
-            listBoxSoundFile.Items.AddRange(new object[]\r
-            {\r
-                "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過",\r
-                "大破警告", "泊地修理20分経過", "泊地修理進行"\r
-            });\r
+            // ReSharper disable once CoVariantArrayConversion\r
+            listBoxSoundFile.Items.AddRange(_config.Sounds.SoundNames);\r
+            numericUpDownMaterialLogInterval.Maximum = 1440;\r
         }\r
 \r
         private void ConfigDialog_Load(object sender, EventArgs e)\r
         {\r
+            var version = string.Join(".", Application.ProductVersion.Split('.').Take(2));\r
+            labelVersion.Text = "バージョン" + version;\r
+            SetLatestVersion(version);\r
+            labelCopyright.Text = FileVersionInfo.GetVersionInfo(Application.ExecutablePath).LegalCopyright;\r
+\r
             checkBoxTopMost.Checked = _config.TopMost;\r
             checkBoxHideOnMinimized.Checked = _config.HideOnMinimized;\r
+            comboBoxZoom.SelectedItem = _config.Zoom + "%";\r
             checkBoxFlash.Checked = _config.FlashWindow;\r
             checkBoxBalloon.Checked = _config.ShowBaloonTip;\r
-            groupBoxSound.Enabled = checkBoxSound.Checked = _config.PlaySound;\r
+            checkBoxSound.Checked = _config.PlaySound;\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
+            checkBoxReset02.Checked = _config.ResetHours.Contains(2);\r
+            checkBoxReset14.Checked = _config.ResetHours.Contains(14);\r
+            radioButtonResultRankAlways.Checked = _config.AlwaysShowResultRank;\r
+            radioButtonResultRankWhenClick.Checked = !_config.AlwaysShowResultRank;\r
+            checkBoxPresetAkashi.Checked = _config.UsePresetAkashi;\r
 \r
-            checkBoxReset02.Checked = _config.ResetHours.Any(x => x == 2);\r
-            checkBoxReset14.Checked = _config.ResetHours.Any(x => x == 14);\r
+            numericUpDownSoundVolume.Value = _config.Sounds.Volume;\r
+            foreach (var name in _config.Sounds.SoundNames)\r
+                _soundSetting[name] = _config.Sounds[name];\r
+            listBoxSoundFile.SelectedIndex = -1;\r
+            listBoxSoundFile.SelectedIndex = 0;\r
 \r
-            numericUpDownSoundVolume.Value = _config.SoundVolume;\r
+            LoadProxySettings();\r
+            LoadLogSettings();\r
+            LoadDebugSettings();\r
+            checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
+            textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
+        }\r
 \r
-            _soundSetting["遠征終了"] = _config.MissionSoundFile;\r
-            _soundSetting["入渠終了"] = _config.NDockSoundFile;\r
-            _soundSetting["建造完了"] = _config.KDockSoundFile;\r
-            _soundSetting["艦娘数超過"] = _config.MaxShipsSoundFile;\r
-            _soundSetting["装備数超過"] = _config.MaxEquipsSoundFile;\r
-            _soundSetting["大破警告"] = _config.DamagedShipSoundFile;\r
-            _soundSetting["泊地修理20分経過"] = _config.Akashi20MinSoundFile;\r
-            _soundSetting["泊地修理進行"] = _config.AkashiProgressSoundFile;\r
-            listBoxSoundFile.SelectedIndex = 0;\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
+            radioButtonServerOn.Checked = _config.Log.ServerOn;\r
+            radioButtonServerOff.Checked = !_config.Log.ServerOn;\r
+            textBoxServer.Text = _config.Log.Listen.ToString("D");\r
+        }\r
+\r
+        private void LoadDebugSettings()\r
+        {\r
+            checkBoxDebugLog.Checked = _config.DebugLogging;\r
+            textBoxDebugLog.Text = _config.DebugLogFile;\r
+        }\r
+\r
+        private async void SetLatestVersion(string version)\r
+        {\r
+            try\r
+            {\r
+                var req = WebRequest.Create(Home + "version");\r
+                var response = await req.GetResponseAsync();\r
+                var stream = response.GetResponseStream();\r
+                if (stream == null)\r
+                    return;\r
+                using (var reader = new StreamReader(stream))\r
+                {\r
+                    var str = await reader.ReadLineAsync();\r
+                    Invoke(new Action(() => { labelLatest.Text = version == str ? "最新です" : "最新は" + str + "です"; }));\r
+                }\r
+            }\r
+            catch (WebException)\r
+            {\r
+            }\r
         }\r
 \r
         private void buttonOk_Click(object sender, EventArgs e)\r
         {\r
+            int listen, outbound, server;\r
+            if (!ValidatePorts(out listen, out outbound, out server))\r
+                return;\r
+            DialogResult = DialogResult.OK;\r
+            if (!ApplyProxySettings(listen, outbound))\r
+                DialogResult = DialogResult.None;\r
+            if (!ApplyLogSettings(server))\r
+                DialogResult = DialogResult.None;\r
+            ApplyDebugSettings();\r
+            _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
+            _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
+\r
             _config.TopMost = checkBoxTopMost.Checked;\r
             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
+            _config.Zoom = int.Parse(comboBoxZoom.SelectedItem.ToString().Substring(0, 3));\r
             _config.FlashWindow = checkBoxFlash.Checked;\r
             _config.ShowBaloonTip = checkBoxBalloon.Checked;\r
             _config.PlaySound = checkBoxSound.Checked;\r
             _config.MarginShips = (int)numericUpDownMarginShips.Value;\r
             _config.MarginEquips = (int)numericUpDownMarginEquips.Value;\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
                 _config.ResetHours.Add(2);\r
             if (checkBoxReset14.Checked)\r
                 _config.ResetHours.Add(14);\r
 \r
-            _config.SoundVolume = (int)numericUpDownSoundVolume.Value;\r
+            _config.AlwaysShowResultRank = radioButtonResultRankAlways.Checked;\r
+            _main.UpdateFighterPower();\r
+            _config.UsePresetAkashi = checkBoxPresetAkashi.Checked;\r
 \r
-            _config.MissionSoundFile = _soundSetting["遠征終了"];\r
-            _config.NDockSoundFile = _soundSetting["入渠終了"];\r
-            _config.KDockSoundFile = _soundSetting["建造完了"];\r
-            _config.MaxShipsSoundFile = _soundSetting["艦娘数超過"];\r
-            _config.MaxEquipsSoundFile = _soundSetting["装備数超過"];\r
-            _config.DamagedShipSoundFile = _soundSetting["大破警告"];\r
-            _config.Akashi20MinSoundFile = _soundSetting["泊地修理20分経過"];\r
-            _config.AkashiProgressSoundFile = _soundSetting["泊地修理進行"];\r
+            _config.Sounds.Volume = (int)numericUpDownSoundVolume.Value;\r
+            foreach (var name in _config.Sounds.SoundNames)\r
+                _config.Sounds[name] = _soundSetting[name];\r
         }\r
 \r
-        private void checkBoxSound_CheckedChanged(object sender, EventArgs e)\r
+        private bool ValidatePorts(out int listen, out int outbound, out int server)\r
         {\r
-            groupBoxSound.Enabled = checkBoxSound.Checked;\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 (radioButtonServerOn.Checked && !ValidatePortNumber(textBoxServer, out server))\r
+                return false;\r
+            if (radioButtonUpstreamOn.Checked && listen == outbound)\r
+            {\r
+                ShowToolTip("受信と送信に同じポートは使えません。", textBoxPort);\r
+                return false;\r
+            }\r
+            if (radioButtonServerOn.Checked && server == listen)\r
+            {\r
+                ShowToolTip("プロキシの受信ポートと同じポートは使えません。", textBoxServer);\r
+                return false;\r
+            }\r
+            return true;\r
+        }\r
+\r
+        private bool ApplyProxySettings(int listen, int port)\r
+        {\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 bool ApplyLogSettings(int server)\r
+        {\r
+            _config.Log.On = checkBoxOutput.Checked;\r
+            _config.Log.MaterialLogInterval = (int)numericUpDownMaterialLogInterval.Value;\r
+            _config.Log.OutputDir = textBoxOutput.Text;\r
+            _config.Log.ServerOn = radioButtonServerOn.Checked;\r
+            _config.Log.Listen = server;\r
+            if (!_main.ApplyLogSetting())\r
+                return false;\r
+            textBoxServer.Text = _config.Log.Listen.ToString();\r
+            return true;\r
+        }\r
+\r
+        private void ApplyDebugSettings()\r
+        {\r
+            _config.DebugLogging = checkBoxDebugLog.Checked;\r
+            _config.DebugLogFile = textBoxDebugLog.Text;\r
+            _main.ApplyDebugLogSetting();\r
         }\r
 \r
         private void textBoxSoundFile_TextChanged(object sender, EventArgs e)\r
@@ -109,43 +231,97 @@ namespace KancolleSniffer
 \r
         private void listBoxSoundFile_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
+            if (listBoxSoundFile.SelectedItem == null)\r
+                return;\r
             textBoxSoundFile.Text = _soundSetting[(string)listBoxSoundFile.SelectedItem];\r
             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
         }\r
 \r
         private void buttonOpenFile_Click(object sender, EventArgs e)\r
         {\r
-            openFileDialog.FileName = textBoxSoundFile.Text;\r
-            openFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
-            if (openFileDialog.ShowDialog() != DialogResult.OK)\r
+            openSoundFileDialog.FileName = textBoxSoundFile.Text;\r
+            openSoundFileDialog.InitialDirectory = Path.GetDirectoryName(textBoxSoundFile.Text) ?? "";\r
+            if (openSoundFileDialog.ShowDialog() != DialogResult.OK)\r
                 return;\r
-            textBoxSoundFile.Text = openFileDialog.FileName;\r
+            textBoxSoundFile.Text = openSoundFileDialog.FileName;\r
             textBoxSoundFile.Select(textBoxSoundFile.Text.Length, 0);\r
         }\r
 \r
-\r
         private void buttonPlay_Click(object sender, EventArgs e)\r
         {\r
             _main.PlaySound(_soundSetting[(string)listBoxSoundFile.SelectedItem], (int)numericUpDownSoundVolume.Value);\r
         }\r
 \r
-        private void DebugToolStripMenuItem_Click(object sender, EventArgs e)\r
+        private void buttonResetAchievement_Click(object sender, EventArgs e)\r
         {\r
-            if (_debugDialog == null)\r
-                _debugDialog = new DebugDialog(_config, _main);\r
-            _debugDialog.ShowDialog(this);\r
+            _main.ResetAchievemnt();\r
         }\r
 \r
-        private void buttonResetAchievement_Click(object sender, EventArgs e)\r
+        private void linkLabelProductName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
         {\r
-            _main.ResetAchievemnt();\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 radioButtonServerOff_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            var off = ((RadioButton)sender).Checked;\r
+            textBoxServer.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
+            return true;\r
+        }\r
+\r
+        private void ShowToolTip(string message, Control control)\r
+        {\r
+            tabControl.SelectedTab = (TabPage)control.Parent.Parent;\r
+            toolTipError.Show(message, control, 0, control.Height, 3000);\r
+        }\r
+\r
+        private void textBox_Enter(object sender, EventArgs e)\r
+        {\r
+            toolTipError.Hide((Control)sender);\r
+        }\r
+\r
+        private void buttonDebugLogOpenFile_Click(object sender, EventArgs e)\r
+        {\r
+            openDebugLogDialog.FileName = textBoxDebugLog.Text;\r
+            openDebugLogDialog.InitialDirectory = Path.GetDirectoryName(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 ProxyToolStripMenuItem_Click(object sender, EventArgs e)\r
+        private void buttonPlayDebugLog_Click(object sender, EventArgs e)\r
         {\r
-            if (_proxyDialog == null)\r
-                _proxyDialog = new ProxyDialog(_config.Proxy, _main);\r
-            _proxyDialog.ShowDialog(this);\r
+            _main.SetPlayLog(textBoxDebugLog.Text);\r
+            DialogResult = DialogResult.Cancel;\r
         }\r
     }\r
 }
\ No newline at end of file