1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
30 using System.Collections.Generic;
31 using System.ComponentModel;
36 using System.Net.Http;
37 using System.Resources;
39 using System.Threading;
40 using System.Threading.Tasks;
41 using System.Windows.Forms;
43 using OpenTween.Connection;
44 using OpenTween.Setting.Panel;
45 using OpenTween.Thumbnail;
49 public partial class AppendSettingDialog : OTBaseForm
51 public event EventHandler<IntervalChangedEventArgs>? IntervalChanged;
53 internal Twitter Tw = null!;
54 internal TwitterApi TwitterApi = null!;
56 public AppendSettingDialog()
58 this.InitializeComponent();
60 this.BasedPanel.StartAuthButton.Click += this.StartAuthButton_Click;
61 this.BasedPanel.CreateAccountButton.Click += this.CreateAccountButton_Click;
62 this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged;
63 this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged;
65 this.Icon = Properties.Resources.MIcon;
68 public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
70 this.BasedPanel.LoadConfig(settingCommon);
71 this.GetPeriodPanel.LoadConfig(settingCommon);
72 this.StartupPanel.LoadConfig(settingCommon);
73 this.TweetPrvPanel.LoadConfig(settingCommon);
74 this.TweetActPanel.LoadConfig(settingCommon, settingLocal);
75 this.ActionPanel.LoadConfig(settingCommon, settingLocal);
76 this.FontPanel.LoadConfig(settingLocal);
77 this.FontPanel2.LoadConfig(settingLocal);
78 this.PreviewPanel.LoadConfig(settingCommon);
79 this.GetCountPanel.LoadConfig(settingCommon);
80 this.ShortUrlPanel.LoadConfig(settingCommon);
81 this.ProxyPanel.LoadConfig(settingLocal);
82 this.CooperatePanel.LoadConfig(settingCommon);
83 this.ConnectionPanel.LoadConfig(settingCommon);
84 this.NotifyPanel.LoadConfig(settingCommon);
86 var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.Tw.UserId);
87 if (activeUser != null)
89 this.BasedPanel.AuthUserCombo.SelectedItem = activeUser;
93 public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
95 this.BasedPanel.SaveConfig(settingCommon);
96 this.GetPeriodPanel.SaveConfig(settingCommon);
97 this.StartupPanel.SaveConfig(settingCommon);
98 this.TweetPrvPanel.SaveConfig(settingCommon);
99 this.TweetActPanel.SaveConfig(settingCommon, settingLocal);
100 this.ActionPanel.SaveConfig(settingCommon, settingLocal);
101 this.FontPanel.SaveConfig(settingLocal);
102 this.FontPanel2.SaveConfig(settingLocal);
103 this.PreviewPanel.SaveConfig(settingCommon);
104 this.GetCountPanel.SaveConfig(settingCommon);
105 this.ShortUrlPanel.SaveConfig(settingCommon);
106 this.ProxyPanel.SaveConfig(settingLocal);
107 this.CooperatePanel.SaveConfig(settingCommon);
108 this.ConnectionPanel.SaveConfig(settingCommon);
109 this.NotifyPanel.SaveConfig(settingCommon);
111 var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
112 if (userAccountIdx != -1)
114 var u = settingCommon.UserAccounts[userAccountIdx];
115 this.Tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
119 this.Tw.ClearAuthInfo();
120 this.Tw.Initialize("", "", "", 0);
124 private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
126 if (this.TreeViewSetting.SelectedNode == null) return;
127 var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
128 if (pnl == null) return;
133 private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
135 if (e.Node == null) return;
136 var pnl = (SettingPanelBase)e.Node.Tag;
137 if (pnl == null) return;
142 private void Setting_FormClosing(object sender, FormClosingEventArgs e)
144 if (MyCommon.EndingFlag) return;
146 if (this.BasedPanel.AuthUserCombo.SelectedIndex == -1 && e.CloseReason == CloseReason.None)
148 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
153 if (e.Cancel == false && this.TreeViewSetting.SelectedNode != null)
155 var curPanel = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
156 curPanel.Visible = false;
157 curPanel.Enabled = false;
161 private void Setting_Load(object sender, EventArgs e)
163 this.TreeViewSetting.Nodes["BasedNode"].Tag = this.BasedPanel;
164 this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = this.GetPeriodPanel;
165 this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = this.StartupPanel;
166 this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = this.GetCountPanel;
167 this.TreeViewSetting.Nodes["ActionNode"].Tag = this.ActionPanel;
168 this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = this.TweetActPanel;
169 this.TreeViewSetting.Nodes["PreviewNode"].Tag = this.PreviewPanel;
170 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = this.TweetPrvPanel;
171 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = this.NotifyPanel;
172 this.TreeViewSetting.Nodes["FontNode"].Tag = this.FontPanel;
173 this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = this.FontPanel2;
174 this.TreeViewSetting.Nodes["ConnectionNode"].Tag = this.ConnectionPanel;
175 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = this.ProxyPanel;
176 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = this.CooperatePanel;
177 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = this.ShortUrlPanel;
179 this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
180 this.TreeViewSetting.ExpandAll();
182 this.ActiveControl = this.BasedPanel.StartAuthButton;
185 private void UReadMng_CheckedChanged(object sender, EventArgs e)
187 if (this.ActionPanel.UReadMng.Checked == true)
189 this.StartupPanel.StartupReaded.Enabled = true;
193 this.StartupPanel.StartupReaded.Enabled = false;
197 private async void StartAuthButton_Click(object sender, EventArgs e)
199 using (ControlTransaction.Disabled(this.BasedPanel.StartAuthButton))
203 this.ApplyNetworkSettings();
205 var newAccount = await this.PinAuth();
206 if (newAccount == null)
209 var authUserCombo = this.BasedPanel.AuthUserCombo;
211 var oldAccount = authUserCombo.Items.Cast<UserAccount>()
212 .FirstOrDefault(x => x.UserId == newAccount.UserId);
215 if (oldAccount != null)
217 idx = authUserCombo.Items.IndexOf(oldAccount);
218 authUserCombo.Items[idx] = newAccount;
222 idx = authUserCombo.Items.Add(newAccount);
225 authUserCombo.SelectedIndex = idx;
229 Properties.Resources.AuthorizeButton_Click1,
231 MessageBoxButtons.OK);
233 catch (WebApiException ex)
235 var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
236 MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
242 /// 現在設定画面に入力されているネットワーク関係の設定を適用します
244 public void ApplyNetworkSettings()
247 if (this.ProxyPanel.RadioProxyNone.Checked)
248 proxyType = ProxyType.None;
249 else if (this.ProxyPanel.RadioProxyIE.Checked)
250 proxyType = ProxyType.IE;
252 proxyType = ProxyType.Specified;
254 var proxyAddress = this.ProxyPanel.TextProxyAddress.Text.Trim();
255 var proxyPort = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
256 var proxyUser = this.ProxyPanel.TextProxyUser.Text.Trim();
257 var proxyPassword = this.ProxyPanel.TextProxyPassword.Text.Trim();
258 Networking.SetWebProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
260 var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
261 Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
263 var uploadImageTimeout = int.Parse(this.ConnectionPanel.UploadImageTimeout.Text.Trim());
264 Networking.UploadImageTimeout = TimeSpan.FromSeconds(uploadImageTimeout);
266 Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
268 TwitterApiConnection.RestApiHost = this.ConnectionPanel.TwitterAPIText.Text.Trim();
271 private async Task<UserAccount?> PinAuth()
273 var requestToken = await TwitterApiConnection.GetRequestTokenAsync();
275 var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);
277 var browserPath = this.ActionPanel.BrowserPathText.Text;
278 var pin = AuthDialog.DoAuth(this, pinPageUrl, browserPath);
279 if (MyCommon.IsNullOrEmpty(pin))
280 return null; // キャンセルされた場合
282 var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);
284 return new UserAccount
286 Username = accessTokenResponse["screen_name"],
287 UserId = long.Parse(accessTokenResponse["user_id"]),
288 Token = accessTokenResponse["oauth_token"],
289 TokenSecret = accessTokenResponse["oauth_token_secret"],
293 private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
294 => this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked;
296 private void Setting_Shown(object sender, EventArgs e)
301 if (this.Disposing || this.IsDisposed) return;
303 while (!this.IsHandleCreated);
304 this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
306 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked;
309 private async Task OpenUrl(string url)
311 var browserPathWithArgs = this.ActionPanel.BrowserPathText.Text;
312 await MyCommon.OpenInBrowserAsync(this, browserPathWithArgs, url);
315 private async void CreateAccountButton_Click(object sender, EventArgs e)
316 => await this.OpenUrl("https://twitter.com/signup");
318 private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
319 => this.IntervalChanged?.Invoke(sender, e);
322 public class IntervalChangedEventArgs : EventArgs
324 public bool Timeline;
326 public bool DirectMessage;
327 public bool PublicSearch;
329 public bool UserTimeline;
331 public static IntervalChangedEventArgs ResetAll => new IntervalChangedEventArgs
335 DirectMessage = true,