OSDN Git Service

pbs.twimg.com の画像URLのフォーマット変更に対応
[opentween/open-tween.git] / OpenTween / AppendSettingDialog.cs
index de1a827..b7dc66b 100644 (file)
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
+#nullable enable
+
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
+using System.Net.Http;
 using System.Text;
 using System.Windows.Forms;
 using System.Threading;
@@ -45,12 +48,10 @@ namespace OpenTween
 {
     public partial class AppendSettingDialog : OTBaseForm
     {
-        public event EventHandler<IntervalChangedEventArgs> IntervalChanged;
-
-        internal Twitter tw;
-        internal TwitterApi twitterApi;
+        public event EventHandler<IntervalChangedEventArgs>? IntervalChanged;
 
-        private bool _ValidationError = false;
+        internal Twitter tw = null!;
+        internal TwitterApi twitterApi = null!;
 
         public AppendSettingDialog()
         {
@@ -150,44 +151,6 @@ namespace OpenTween
             }
         }
 
-        private void Save_Click(object sender, EventArgs e)
-        {
-            if (MyCommon.IsNetworkAvailable() &&
-                (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp))
-            {
-                // bit.ly 短縮機能実装のプライバシー問題の暫定対応
-                // bit.ly 使用時はログインIDとAPIキーの指定を必須とする
-                // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
-                if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text))
-                {
-                    MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName);
-                    _ValidationError = true;
-                    TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
-                    TreeViewSetting.Select();
-                    this.ShortUrlPanel.TextBitlyId.Focus();
-                    return;
-                }
-
-                if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
-                {
-                    MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
-                    _ValidationError = true;
-                    TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
-                    TreeViewSetting.Select();
-                    this.ShortUrlPanel.TextBitlyId.Focus();
-                    return;
-                }
-                else
-                {
-                    _ValidationError = false;
-                }
-            }
-            else
-            {
-                _ValidationError = false;
-            }
-        }
-
         private void Setting_FormClosing(object sender, FormClosingEventArgs e)
         {
             if (MyCommon._endingFlag) return;
@@ -199,10 +162,6 @@ namespace OpenTween
                     e.Cancel = true;
                 }
             }
-            if (_ValidationError)
-            {
-                e.Cancel = true;
-            }
             if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
             {
                 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
@@ -262,7 +221,7 @@ namespace OpenTween
                     var authUserCombo = this.BasedPanel.AuthUserCombo;
 
                     var oldAccount = authUserCombo.Items.Cast<UserAccount>()
-                        .FirstOrDefault(x => x.UserId == this.tw.UserId);
+                        .FirstOrDefault(x => x.UserId == newAccount.UserId);
 
                     int idx;
                     if (oldAccount != null)
@@ -310,19 +269,22 @@ namespace OpenTween
             var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
             Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
 
+            var uploadImageTimeout = int.Parse(this.ConnectionPanel.UploadImageTimeout.Text.Trim());
+            Networking.UploadImageTimeout = TimeSpan.FromSeconds(uploadImageTimeout);
+
             Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
 
-            TwitterApiConnection.RestApiBase = new Uri(this.ConnectionPanel.TwitterAPIText.Text.Trim());
+            TwitterApiConnection.RestApiHost = this.ConnectionPanel.TwitterAPIText.Text.Trim();
         }
 
-        private async Task<UserAccount> PinAuth()
+        private async Task<UserAccount?> PinAuth()
         {
             var requestToken = await TwitterApiConnection.GetRequestTokenAsync();
 
             var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);
 
             var pin = AuthDialog.DoAuth(this, pinPageUrl);
-            if (string.IsNullOrEmpty(pin))
+            if (MyCommon.IsNullOrEmpty(pin))
                 return null; // キャンセルされた場合
 
             var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);
@@ -337,9 +299,7 @@ namespace OpenTween
         }
 
         private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
-        {
-            this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
-        }
+            => this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
 
         private void Setting_Shown(object sender, EventArgs e)
         {
@@ -354,59 +314,19 @@ namespace OpenTween
             this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
         }
 
-        private bool BitlyValidation(string id, string apikey)
-        {
-            if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
-            {
-                return false;
-            }
-
-            string req = "http://api.bit.ly/v3/validate";
-            string content = "";
-            Dictionary<string, string> param = new Dictionary<string, string>();
-
-            param.Add("login", ApplicationSettings.BitlyLoginId);
-            param.Add("apiKey", ApplicationSettings.BitlyApiKey);
-            param.Add("x_login", id);
-            param.Add("x_apiKey", apikey);
-            param.Add("format", "txt");
-
-            if (!(new HttpVarious()).PostData(req, param, out content))
-            {
-                return true;             // 通信エラーの場合はとりあえずチェックを通ったことにする
-            }
-            else if (content.Trim() == "1")
-            {
-                return true;             // 検証成功
-            }
-            else if (content.Trim() == "0")
-            {
-                return false;            // 検証失敗 APIキーとIDの組み合わせが違う
-            }
-            else
-            {
-                return true;             // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする
-            }
-        }
-
-        private void Cancel_Click(object sender, EventArgs e)
-        {
-            _ValidationError = false;
-        }
-
         private void OpenUrl(string url)
         {
-            string myPath = url;
-            string path = this.ActionPanel.BrowserPathText.Text;
+            var myPath = url;
+            var path = this.ActionPanel.BrowserPathText.Text;
             try
             {
-                if (!string.IsNullOrEmpty(path))
+                if (!MyCommon.IsNullOrEmpty(path))
                 {
                     if (path.StartsWith("\"", StringComparison.Ordinal) && path.Length > 2 && path.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
                     {
-                        int sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
-                        string browserPath = path.Substring(1, sep - 1);
-                        string arg = "";
+                        var sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
+                        var browserPath = path.Substring(1, sep - 1);
+                        var arg = "";
                         if (sep < path.Length - 1)
                         {
                             arg = path.Substring(sep + 1);
@@ -430,14 +350,10 @@ namespace OpenTween
         }
 
         private void CreateAccountButton_Click(object sender, EventArgs e)
-        {
-            this.OpenUrl("https://twitter.com/signup");
-        }
+            => this.OpenUrl("https://twitter.com/signup");
 
         private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
-        {
-            this.IntervalChanged?.Invoke(sender, e);
-        }
+            => this.IntervalChanged?.Invoke(sender, e);
     }
 
     public class IntervalChangedEventArgs : EventArgs
@@ -450,21 +366,15 @@ namespace OpenTween
         public bool Lists;
         public bool UserTimeline;
 
-        public static IntervalChangedEventArgs ResetAll
+        public static IntervalChangedEventArgs ResetAll => new IntervalChangedEventArgs
         {
-            get
-            {
-                return new IntervalChangedEventArgs()
-                {
-                    UserStream = true,
-                    Timeline = true,
-                    Reply = true,
-                    DirectMessage = true,
-                    PublicSearch = true,
-                    Lists = true,
-                    UserTimeline = true,
-                };
-            }
-        }
+            UserStream = true,
+            Timeline = true,
+            Reply = true,
+            DirectMessage = true,
+            PublicSearch = true,
+            Lists = true,
+            UserTimeline = true,
+        };
     }
 }