OSDN Git Service

BasedPanelに対する設定値の設定・取得を LoadConfing/SaveConfig メソッドに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 31 Jul 2014 13:38:25 +0000 (22:38 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 31 Jul 2014 16:17:19 +0000 (01:17 +0900)
OpenTween/AppendSettingDialog.cs
OpenTween/Setting/Panel/BasedPanel.cs
OpenTween/Tween.cs

index 15e5966..096a565 100644 (file)
@@ -46,11 +46,10 @@ namespace OpenTween
     public partial class AppendSettingDialog : OTBaseForm
     {
         private static AppendSettingDialog _instance = new AppendSettingDialog();
-        private Twitter tw;
+        internal Twitter tw;
 
         private bool _ValidationError = false;
 
-        public List<UserAccount> UserAccounts;
         private long? InitialUserId;
 
         public TwitterConfiguration TwitterConfiguration = TwitterConfiguration.DefaultConfiguration();
@@ -61,6 +60,7 @@ namespace OpenTween
 
         public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
         {
+            this.BasedPanel.LoadConfig(settingCommon);
             this.GetPeriodPanel.LoadConfig(settingCommon);
             this.StartupPanel.LoadConfig(settingCommon);
             this.TweetPrvPanel.LoadConfig(settingCommon);
@@ -75,10 +75,18 @@ namespace OpenTween
             this.CooperatePanel.LoadConfig(settingCommon);
             this.ConnectionPanel.LoadConfig(settingCommon);
             this.NotifyPanel.LoadConfig(settingCommon);
+
+            var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.tw.UserId);
+            if (activeUser != null)
+            {
+                this.BasedPanel.AuthUserCombo.SelectedItem = activeUser;
+                this.InitialUserId = activeUser.UserId;
+            }
         }
 
         public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
         {
+            this.BasedPanel.SaveConfig(settingCommon);
             this.GetPeriodPanel.SaveConfig(settingCommon);
             this.StartupPanel.SaveConfig(settingCommon);
             this.TweetPrvPanel.SaveConfig(settingCommon);
@@ -93,6 +101,24 @@ namespace OpenTween
             this.CooperatePanel.SaveConfig(settingCommon);
             this.ConnectionPanel.SaveConfig(settingCommon);
             this.NotifyPanel.SaveConfig(settingCommon);
+
+            var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
+            if (userAccountIdx != -1)
+            {
+                var u = settingCommon.UserAccounts[userAccountIdx];
+                this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
+
+                if (u.UserId == 0)
+                {
+                    this.tw.VerifyCredentials();
+                    u.UserId = this.tw.UserId;
+                }
+            }
+            else
+            {
+                this.tw.ClearAuthInfo();
+                this.tw.Initialize("", "", "", 0);
+            }
         }
 
         private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
@@ -162,33 +188,6 @@ namespace OpenTween
                 _ValidationError = false;
             }
 
-            this.UserAccounts.Clear();
-            foreach (object u in this.BasedPanel.AuthUserCombo.Items)
-            {
-                this.UserAccounts.Add((UserAccount)u);
-            }
-            if (this.BasedPanel.AuthUserCombo.SelectedIndex > -1)
-            {
-                foreach (UserAccount u in this.UserAccounts)
-                {
-                    if (u.Username.ToLower() == ((UserAccount)this.BasedPanel.AuthUserCombo.SelectedItem).Username.ToLower())
-                    {
-                        tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
-                        if (u.UserId == 0)
-                        {
-                            tw.VerifyCredentials();
-                            u.UserId = tw.UserId;
-                        }
-                        break;
-                    }
-                }
-            }
-            else
-            {
-                tw.ClearAuthInfo();
-                tw.Initialize("", "", "", 0);
-            }
-
 #if UA
             //フォロー
             if (this.FollowCheckBox.Checked)
@@ -222,38 +221,6 @@ namespace OpenTween
         {
             if (MyCommon._endingFlag) return;
 
-            if (this.DialogResult == DialogResult.Cancel)
-            {
-                //キャンセル時は画面表示時のアカウントに戻す
-                //キャンセル時でも認証済みアカウント情報は保存する
-                this.UserAccounts.Clear();
-                foreach (object u in this.BasedPanel.AuthUserCombo.Items)
-                {
-                    this.UserAccounts.Add((UserAccount)u);
-                }
-                //アクティブユーザーを起動時のアカウントに戻す(起動時アカウントなければ何もしない)
-                bool userSet = false;
-                if (this.InitialUserId != null)
-                {
-                    foreach (UserAccount u in this.UserAccounts)
-                    {
-                        if (u.UserId == this.InitialUserId)
-                        {
-                            tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
-                            userSet = true;
-                            break;
-                        }
-                    }
-                }
-                //認証済みアカウントが削除されていた場合、もしくは起動時アカウントがなかった場合は、
-                //アクティブユーザーなしとして初期化
-                if (!userSet)
-                {
-                    tw.ClearAuthInfo();
-                    tw.Initialize("", "", "", 0);
-                }
-            }
-
             if (tw != null && string.IsNullOrEmpty(tw.Username) && e.CloseReason == CloseReason.None)
             {
                 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
@@ -275,46 +242,6 @@ namespace OpenTween
 
         private void Setting_Load(object sender, EventArgs e)
         {
-            tw = ((TweenMain)this.Owner).TwitterInstance;
-            //this.AuthStateLabel.Enabled = true;
-            //this.AuthUserLabel.Enabled = true;
-            this.BasedPanel.AuthClearButton.Enabled = true;
-
-            //if (tw.Username == "")
-            //{
-            //    //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4
-            //    //this.AuthUserLabel.Text = ""
-            //    //this.Save.Enabled = false
-            //}
-            //else
-            //{
-            //    //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click3;
-            //    //if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWrite)
-            //    //{
-            //    //    this.AuthStateLabel.Text += "(xAuth)";
-            //    //}
-            //    //else if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWriteAndDirectMessage)
-            //    //{
-            //    //    this.AuthStateLabel.Text += "(OAuth)";
-            //    //}
-            //    //this.AuthUserLabel.Text = tw.Username;
-            //}
-
-            this.BasedPanel.AuthUserCombo.Items.Clear();
-            if (this.UserAccounts.Count > 0)
-            {
-                this.BasedPanel.AuthUserCombo.Items.AddRange(this.UserAccounts.ToArray());
-                foreach (UserAccount u in this.UserAccounts)
-                {
-                    if (u.UserId == tw.UserId)
-                    {
-                        this.BasedPanel.AuthUserCombo.SelectedItem = u;
-                        this.InitialUserId = u.UserId;
-                        break;
-                    }
-                }
-            }
-
             this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
             this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
             this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
index 6b749d2..658ef3e 100644 (file)
@@ -42,6 +42,20 @@ namespace OpenTween.Setting.Panel
             InitializeComponent();
         }
 
+        public void LoadConfig(SettingCommon settingCommon)
+        {
+            using (ControlTransaction.Update(this.AuthUserCombo))
+            {
+                this.AuthUserCombo.Items.Clear();
+                this.AuthUserCombo.Items.AddRange(settingCommon.UserAccounts.ToArray());
+            }
+        }
+
+        public void SaveConfig(SettingCommon settingCommon)
+        {
+            settingCommon.UserAccounts = this.AuthUserCombo.Items.Cast<UserAccount>().ToList();
+        }
+
         private void AuthClearButton_Click(object sender, EventArgs e)
         {
             //tw.ClearAuthInfo();
index 522e278..e0cc42d 100644 (file)
@@ -757,15 +757,15 @@ namespace OpenTween
             if (this._cfgCommon.CountApiReply < 20 || this._cfgCommon.CountApiReply > 200)
                 this._cfgCommon.CountApiReply = 40;
 
-            //設定画面への反映
-            this.SettingDialog.LoadConfig(this._cfgCommon, this._cfgLocal);
             HttpTwitter.TwitterUrl = _cfgCommon.TwitterUrl;
 
             //認証関連
             if (string.IsNullOrEmpty(_cfgCommon.Token)) _cfgCommon.UserName = "";
             tw.Initialize(_cfgCommon.Token, _cfgCommon.TokenSecret, _cfgCommon.UserName, _cfgCommon.UserId);
 
-            SettingDialog.UserAccounts = _cfgCommon.UserAccounts;
+            //設定画面への反映
+            this.SettingDialog.tw = this.tw;
+            this.SettingDialog.LoadConfig(this._cfgCommon, this._cfgLocal);
 
             //新着取得時のリストスクロールをするか。trueならスクロールしない
             ListLockMenuItem.Checked = _cfgCommon.ListLock;
@@ -1109,18 +1109,9 @@ namespace OpenTween
             if (saveRequired) SaveConfigsAll(false);
 
             if (tw.UserId == 0)
-            {
                 tw.VerifyCredentials();
-                foreach (UserAccount ua in _cfgCommon.UserAccounts)
-                {
-                    if (ua.Username.ToLower() == tw.Username.ToLower())
-                    {
-                        ua.UserId = tw.UserId;
-                        break;
-                    }
-                }
-            }
-            foreach (UserAccount ua in SettingDialog.UserAccounts)
+
+            foreach (var ua in this._cfgCommon.UserAccounts)
             {
                 if (ua.UserId == 0 && ua.Username.ToLower() == tw.Username.ToLower())
                 {
@@ -3769,7 +3760,9 @@ namespace OpenTween
         private void SettingStripMenuItem_Click(object sender, EventArgs e)
         {
             DialogResult result;
-            string uid = tw.Username.ToLower();
+
+            // 設定画面表示前のユーザー情報
+            var oldUser = new { tw.AccessToken, tw.AccessTokenSecret, tw.Username, tw.UserId };
 
             try
             {
@@ -4005,7 +3998,8 @@ namespace OpenTween
                         _hookGlobalHotkey.RegisterOriginalHotkey(this._cfgCommon.HotkeyKey, this._cfgCommon.HotkeyValue, modKey);
                     }
 
-                    if (uid != tw.Username) this.doGetFollowersMenu();
+                    if (tw.Username != oldUser.Username)
+                        this.doGetFollowersMenu();
 
                     if (this._cfgCommon.IsUseNotifyGrowl) gh.RegisterGrowl();
                     try
@@ -4017,6 +4011,11 @@ namespace OpenTween
                     }
                 }
             }
+            else
+            {
+                // キャンセル時は Twitter クラスの認証情報を画面表示前の状態に戻す
+                this.tw.Initialize(oldUser.AccessToken, oldUser.AccessTokenSecret, oldUser.Username, oldUser.UserId);
+            }
 
             Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
 
@@ -7630,7 +7629,6 @@ namespace OpenTween
                 _cfgCommon.Password = tw.Password;
                 _cfgCommon.Token = tw.AccessToken;
                 _cfgCommon.TokenSecret = tw.AccessTokenSecret;
-                _cfgCommon.UserAccounts = SettingDialog.UserAccounts;
 
                 if (IdeographicSpaceToSpaceToolStripMenuItem != null &&
                    IdeographicSpaceToSpaceToolStripMenuItem.IsDisposed == false)