OSDN Git Service

Exceptionクラスを直接スローしている箇所を派生型に置き換え (CA2201)
[opentween/open-tween.git] / OpenTween / MyCommon.cs
index 1ff4ea2..426eedb 100644 (file)
@@ -50,7 +50,7 @@ using OpenTween.Api;
 
 namespace OpenTween
 {
-    public sealed class MyCommon
+    public static class MyCommon
     {
         private static readonly object LockObj = new object();
         public static bool _endingFlag;        //終了フラグ
@@ -220,6 +220,19 @@ namespace OpenTween
                    ListUserSubscribed | ListUserUnsubscribed | ListDestroyed),
         }
 
+        public static _Assembly EntryAssembly { get; internal set; }
+        public static string FileVersion { get; internal set; }
+
+        static MyCommon()
+        {
+            var assembly = Assembly.GetExecutingAssembly();
+            MyCommon.EntryAssembly = assembly;
+
+            var fileVersionAttribute = (AssemblyFileVersionAttribute)assembly
+                .GetCustomAttributes(typeof(AssemblyFileVersionAttribute)).First();
+            MyCommon.FileVersion = fileVersionAttribute.Version;
+        }
+
         public static string GetErrorLogPath()
         {
             return Path.Combine(Path.GetDirectoryName(MyCommon.EntryAssembly.Location), "ErrorLogs");
@@ -269,7 +282,7 @@ namespace OpenTween
                     writer.WriteLine(Properties.Resources.TraceOutText3);
                     writer.WriteLine(Properties.Resources.TraceOutText4, Environment.OSVersion.VersionString);
                     writer.WriteLine(Properties.Resources.TraceOutText5, Environment.Version.ToString());
-                    writer.WriteLine(Properties.Resources.TraceOutText6, MyCommon.GetAssemblyName(), fileVersion);
+                    writer.WriteLine(Properties.Resources.TraceOutText6, MyCommon.GetAssemblyName(), FileVersion);
                     writer.WriteLine(Message);
                     writer.WriteLine();
                 }
@@ -391,7 +404,7 @@ namespace OpenTween
                     writer.WriteLine(Properties.Resources.UnhandledExceptionText4);
                     writer.WriteLine(Properties.Resources.UnhandledExceptionText5, Environment.OSVersion.VersionString);
                     writer.WriteLine(Properties.Resources.UnhandledExceptionText6, Environment.Version.ToString());
-                    writer.WriteLine(Properties.Resources.UnhandledExceptionText7, MyCommon.GetAssemblyName(), fileVersion);
+                    writer.WriteLine(Properties.Resources.UnhandledExceptionText7, MyCommon.GetAssemblyName(), FileVersion);
 
                     writer.Write(ExceptionOutMessage(ex, ref IsTerminatePermission));
                     writer.Flush();
@@ -406,9 +419,8 @@ namespace OpenTween
                     case DialogResult.No:
                         return false;
                     case DialogResult.Cancel:
-                        return IsTerminatePermission;
                     default:
-                        throw new Exception("");
+                        return IsTerminatePermission;
                 }
             }
         }
@@ -739,21 +751,6 @@ namespace OpenTween
             //RTByMe
         }
 
-        public static string fileVersion = "";
-
-        public static string GetUserAgentString(bool fakeMSIE = false)
-        {
-            if (string.IsNullOrEmpty(fileVersion))
-            {
-                throw new Exception("fileversion is not Initialized.");
-            }
-
-            if (fakeMSIE)
-                return GetAssemblyName() + "/" + fileVersion + " (compatible; MSIE 10.0)";
-            else
-                return GetAssemblyName() + "/" + fileVersion;
-        }
-
         public static TwitterApiStatus TwitterApiInfo = new TwitterApiStatus();
 
         public static bool IsAnimatedGif(string filename)
@@ -879,8 +876,6 @@ namespace OpenTween
             return MyCommon.EntryAssembly.GetName().Name;
         }
 
-        internal static _Assembly EntryAssembly = Assembly.GetEntryAssembly();
-
         /// <summary>
         /// 文字列中に含まれる %AppName% をアプリケーション名に置換する
         /// </summary>
@@ -913,15 +908,9 @@ namespace OpenTween
         /// </returns>
         public static string GetReadableVersion(string versionStr = null)
         {
-            if (versionStr == null)
-            {
-                if (MyCommon.fileVersion == null)
-                    return null;
-
-                versionStr = MyCommon.fileVersion;
-            }
+            var version = Version.Parse(versionStr ?? MyCommon.FileVersion);
 
-            return GetReadableVersion(Version.Parse(versionStr));
+            return GetReadableVersion(version);
         }
 
         /// <summary>
@@ -978,38 +967,43 @@ namespace OpenTween
         }
 
         /// <summary>
-        /// OpenTween 内で共通して使う設定を施した HttpClient インスタンスを作成します
+        /// 指定された IDictionary を元にクエリ文字列を生成します
         /// </summary>
-        public static HttpClient CreateHttpClient()
+        /// <param name="param">生成するクエリの key-value コレクション</param>
+        public static string BuildQueryString(IDictionary<string, string> param)
         {
-            return CreateHttpClient(new HttpClientHandler());
+            if (param == null || param.Count == 0)
+                return string.Empty;
+
+            var query = param
+                .Where(x => x.Value != null)
+                .Select(x => EscapeQueryString(x.Key) + '=' + EscapeQueryString(x.Value));
+
+            return string.Join("&", query);
         }
 
+        // .NET 4.5+: Reserved characters のうち、Uriクラスによってエスケープ強制解除されてしまうものも最初から Unreserved として扱う
+        private static readonly HashSet<char> UnreservedChars =
+            new HashSet<char>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*:");
+
         /// <summary>
-        /// OpenTween 内で共通して使う設定を施した HttpClient インスタンスを作成します
+        /// 2バイト文字も考慮したクエリ用エンコード
         /// </summary>
-        public static HttpClient CreateHttpClient(HttpClientHandler handler)
+        /// <param name="stringToEncode">エンコードする文字列</param>
+        /// <returns>エンコード結果文字列</returns>
+        public static string EscapeQueryString(string stringToEncode)
         {
-            switch (HttpConnection.proxyKind)
+            var sb = new StringBuilder(stringToEncode.Length * 2);
+
+            foreach (var b in Encoding.UTF8.GetBytes(stringToEncode))
             {
-                case HttpConnection.ProxyType.None:
-                    handler.UseProxy = false;
-                    break;
-                case HttpConnection.ProxyType.Specified:
-                    handler.UseProxy = true;
-                    handler.Proxy = HttpConnection.proxy;
-                    break;
-                case HttpConnection.ProxyType.IE:
-                default:
-                    handler.UseProxy = true;
-                    handler.Proxy = WebRequest.GetSystemWebProxy();
-                    break;
+                if (UnreservedChars.Contains((char)b))
+                    sb.Append((char)b);
+                else
+                    sb.AppendFormat("%{0:X2}", b);
             }
 
-            var client = new HttpClient(handler);
-            client.DefaultRequestHeaders.Add("User-Agent", MyCommon.GetUserAgentString());
-
-            return client;
+            return sb.ToString();
         }
     }
 }