OSDN Git Service

バージョン8.5の準備
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / SystemProxy.cs
index d72476a..660aebb 100644 (file)
@@ -1,19 +1,16 @@
 // Copyright (C) 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.Runtime.InteropServices;\r
@@ -27,6 +24,7 @@ namespace KancolleSniffer
     public class SystemProxy\r
     {\r
         private InternetPerConnOptionList orgList;\r
+        private string _currentUrl;\r
 \r
         private void SaveSettings()\r
         {\r
@@ -57,6 +55,7 @@ namespace KancolleSniffer
             SaveSettings();\r
             var flagValue = new InternetPerConnOptionValue {dwValue = (int)PerConnFlags.PROXY_TYPE_AUTO_PROXY_URL};\r
             var urlValue = new InternetPerConnOptionValue {pszValue = Marshal.StringToHGlobalAuto(url)};\r
+            _currentUrl = url;\r
             var opts = new[]\r
             {\r
                 new InternetPerConnOption {dwOption = PerConnOption.INTERNET_PER_CONN_FLAGS, Value = flagValue},\r
@@ -84,11 +83,18 @@ namespace KancolleSniffer
         {\r
             if (orgList.dwSize == 0)\r
                 return;\r
-            // Always unselect the Use automatic configuration script check box.\r
-            var flagsOpt =\r
-                (InternetPerConnOption)Marshal.PtrToStructure(orgList.pOptions, typeof(InternetPerConnOption));\r
-            flagsOpt.Value.dwValue &= ~(int)PerConnFlags.PROXY_TYPE_AUTO_PROXY_URL;\r
-            Marshal.StructureToPtr(flagsOpt, orgList.pOptions, false);\r
+            var size = Marshal.SizeOf(typeof(InternetPerConnOption));\r
+            var urlOpt = (InternetPerConnOption)\r
+                Marshal.PtrToStructure((IntPtr)((long)orgList.pOptions + size), typeof(InternetPerConnOption));\r
+            var orgUrl = Marshal.PtrToStringAuto(urlOpt.Value.pszValue);\r
+            if (orgUrl == _currentUrl) // The restoration was sikipped or failed at last time.\r
+            {\r
+                // Unselect the Use automatic configration script check box.\r
+                var flagsOpt =\r
+                    (InternetPerConnOption)Marshal.PtrToStructure(orgList.pOptions, typeof(InternetPerConnOption));\r
+                flagsOpt.Value.dwValue &= ~(int)PerConnFlags.PROXY_TYPE_AUTO_PROXY_URL;\r
+                Marshal.StructureToPtr(flagsOpt, orgList.pOptions, false);\r
+            }\r
             var listSize = orgList.dwSize;\r
             var listBuff = Marshal.AllocCoTaskMem(listSize);\r
             Marshal.StructureToPtr(orgList, listBuff, false);\r
@@ -96,9 +102,6 @@ namespace KancolleSniffer
             Refresh();\r
 \r
             Marshal.FreeCoTaskMem(listBuff);\r
-            var size = Marshal.SizeOf(typeof(InternetPerConnOption));\r
-            var urlOpt = (InternetPerConnOption)\r
-                Marshal.PtrToStructure((IntPtr)((long)orgList.pOptions + size), typeof(InternetPerConnOption));\r
             Marshal.FreeHGlobal(urlOpt.Value.pszValue);\r
             Marshal.FreeCoTaskMem(orgList.pOptions);\r
             orgList.dwSize = 0;\r