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;
35 using System.Net.Http;
37 using System.Windows.Forms;
38 using System.Threading;
40 using System.Resources;
42 using OpenTween.Connection;
43 using OpenTween.Thumbnail;
44 using System.Threading.Tasks;
45 using OpenTween.Setting.Panel;
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;
141 if (pnl.Name == "PreviewPanel")
143 if (GrowlHelper.IsDllExists)
145 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
149 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
154 private void Setting_FormClosing(object sender, FormClosingEventArgs e)
156 if (MyCommon._endingFlag) return;
158 if (this.BasedPanel.AuthUserCombo.SelectedIndex == -1 && e.CloseReason == CloseReason.None)
160 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
165 if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
167 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
168 curPanel.Visible = false;
169 curPanel.Enabled = false;
173 private void Setting_Load(object sender, EventArgs e)
175 this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
176 this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
177 this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
178 this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
179 this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
180 this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
181 this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
182 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
183 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
184 this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
185 this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
186 this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
187 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
188 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
189 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
191 this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
192 this.TreeViewSetting.ExpandAll();
194 ActiveControl = BasedPanel.StartAuthButton;
197 private void UReadMng_CheckedChanged(object sender, EventArgs e)
199 if (this.ActionPanel.UReadMng.Checked == true)
201 this.StartupPanel.StartupReaded.Enabled = true;
205 this.StartupPanel.StartupReaded.Enabled = false;
209 private async void StartAuthButton_Click(object sender, EventArgs e)
211 using (ControlTransaction.Disabled(this.BasedPanel.StartAuthButton))
215 this.ApplyNetworkSettings();
217 var newAccount = await this.PinAuth();
218 if (newAccount == null)
221 var authUserCombo = this.BasedPanel.AuthUserCombo;
223 var oldAccount = authUserCombo.Items.Cast<UserAccount>()
224 .FirstOrDefault(x => x.UserId == newAccount.UserId);
227 if (oldAccount != null)
229 idx = authUserCombo.Items.IndexOf(oldAccount);
230 authUserCombo.Items[idx] = newAccount;
234 idx = authUserCombo.Items.Add(newAccount);
237 authUserCombo.SelectedIndex = idx;
239 MessageBox.Show(this, Properties.Resources.AuthorizeButton_Click1,
240 "Authenticate", MessageBoxButtons.OK);
242 catch (WebApiException ex)
244 var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
245 MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
251 /// 現在設定画面に入力されているネットワーク関係の設定を適用します
253 public void ApplyNetworkSettings()
256 if (this.ProxyPanel.RadioProxyNone.Checked)
257 proxyType = ProxyType.None;
258 else if (this.ProxyPanel.RadioProxyIE.Checked)
259 proxyType = ProxyType.IE;
261 proxyType = ProxyType.Specified;
263 var proxyAddress = this.ProxyPanel.TextProxyAddress.Text.Trim();
264 var proxyPort = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
265 var proxyUser = this.ProxyPanel.TextProxyUser.Text.Trim();
266 var proxyPassword = this.ProxyPanel.TextProxyPassword.Text.Trim();
267 Networking.SetWebProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
269 var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
270 Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
272 var uploadImageTimeout = int.Parse(this.ConnectionPanel.UploadImageTimeout.Text.Trim());
273 Networking.UploadImageTimeout = TimeSpan.FromSeconds(uploadImageTimeout);
275 Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
277 TwitterApiConnection.RestApiHost = this.ConnectionPanel.TwitterAPIText.Text.Trim();
280 private async Task<UserAccount?> PinAuth()
282 var requestToken = await TwitterApiConnection.GetRequestTokenAsync();
284 var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);
286 var pin = AuthDialog.DoAuth(this, pinPageUrl);
287 if (MyCommon.IsNullOrEmpty(pin))
288 return null; // キャンセルされた場合
290 var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);
292 return new UserAccount
294 Username = accessTokenResponse["screen_name"],
295 UserId = long.Parse(accessTokenResponse["user_id"]),
296 Token = accessTokenResponse["oauth_token"],
297 TokenSecret = accessTokenResponse["oauth_token_secret"],
301 private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
302 => this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
304 private void Setting_Shown(object sender, EventArgs e)
309 if (this.Disposing || this.IsDisposed) return;
310 } while (!this.IsHandleCreated);
311 this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
313 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
314 this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
317 private void OpenUrl(string url)
320 var path = this.ActionPanel.BrowserPathText.Text;
323 if (!MyCommon.IsNullOrEmpty(path))
325 if (path.StartsWith("\"", StringComparison.Ordinal) && path.Length > 2 && path.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
327 var sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
328 var browserPath = path.Substring(1, sep - 1);
330 if (sep < path.Length - 1)
332 arg = path.Substring(sep + 1);
334 myPath = arg + " " + myPath;
335 System.Diagnostics.Process.Start(browserPath, myPath);
339 System.Diagnostics.Process.Start(path, myPath);
344 System.Diagnostics.Process.Start(myPath);
352 private void CreateAccountButton_Click(object sender, EventArgs e)
353 => this.OpenUrl("https://twitter.com/signup");
355 private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
356 => this.IntervalChanged?.Invoke(sender, e);
359 public class IntervalChangedEventArgs : EventArgs
361 public bool UserStream;
362 public bool Timeline;
364 public bool DirectMessage;
365 public bool PublicSearch;
367 public bool UserTimeline;
369 public static IntervalChangedEventArgs ResetAll => new IntervalChangedEventArgs
374 DirectMessage = true,