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.
28 using System.Collections.Generic;
29 using System.ComponentModel;
33 using System.Net.Http;
35 using System.Windows.Forms;
36 using System.Threading;
38 using System.Resources;
40 using OpenTween.Connection;
41 using OpenTween.Thumbnail;
42 using System.Threading.Tasks;
43 using OpenTween.Setting.Panel;
47 public partial class AppendSettingDialog : OTBaseForm
49 public event EventHandler<IntervalChangedEventArgs> IntervalChanged;
52 internal TwitterApi twitterApi;
54 public AppendSettingDialog()
56 this.InitializeComponent();
58 this.BasedPanel.StartAuthButton.Click += this.StartAuthButton_Click;
59 this.BasedPanel.CreateAccountButton.Click += this.CreateAccountButton_Click;
60 this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged;
61 this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged;
63 this.Icon = Properties.Resources.MIcon;
66 public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
68 this.BasedPanel.LoadConfig(settingCommon);
69 this.GetPeriodPanel.LoadConfig(settingCommon);
70 this.StartupPanel.LoadConfig(settingCommon);
71 this.TweetPrvPanel.LoadConfig(settingCommon);
72 this.TweetActPanel.LoadConfig(settingCommon, settingLocal);
73 this.ActionPanel.LoadConfig(settingCommon, settingLocal);
74 this.FontPanel.LoadConfig(settingLocal);
75 this.FontPanel2.LoadConfig(settingLocal);
76 this.PreviewPanel.LoadConfig(settingCommon);
77 this.GetCountPanel.LoadConfig(settingCommon);
78 this.ShortUrlPanel.LoadConfig(settingCommon);
79 this.ProxyPanel.LoadConfig(settingLocal);
80 this.CooperatePanel.LoadConfig(settingCommon);
81 this.ConnectionPanel.LoadConfig(settingCommon);
82 this.NotifyPanel.LoadConfig(settingCommon);
84 var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.tw.UserId);
85 if (activeUser != null)
87 this.BasedPanel.AuthUserCombo.SelectedItem = activeUser;
91 public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
93 this.BasedPanel.SaveConfig(settingCommon);
94 this.GetPeriodPanel.SaveConfig(settingCommon);
95 this.StartupPanel.SaveConfig(settingCommon);
96 this.TweetPrvPanel.SaveConfig(settingCommon);
97 this.TweetActPanel.SaveConfig(settingCommon, settingLocal);
98 this.ActionPanel.SaveConfig(settingCommon, settingLocal);
99 this.FontPanel.SaveConfig(settingLocal);
100 this.FontPanel2.SaveConfig(settingLocal);
101 this.PreviewPanel.SaveConfig(settingCommon);
102 this.GetCountPanel.SaveConfig(settingCommon);
103 this.ShortUrlPanel.SaveConfig(settingCommon);
104 this.ProxyPanel.SaveConfig(settingLocal);
105 this.CooperatePanel.SaveConfig(settingCommon);
106 this.ConnectionPanel.SaveConfig(settingCommon);
107 this.NotifyPanel.SaveConfig(settingCommon);
109 var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
110 if (userAccountIdx != -1)
112 var u = settingCommon.UserAccounts[userAccountIdx];
113 this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
117 this.tw.ClearAuthInfo();
118 this.tw.Initialize("", "", "", 0);
122 private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
124 if (this.TreeViewSetting.SelectedNode == null) return;
125 var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
126 if (pnl == null) return;
131 private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
133 if (e.Node == null) return;
134 var pnl = (SettingPanelBase)e.Node.Tag;
135 if (pnl == null) return;
139 if (pnl.Name == "PreviewPanel")
141 if (GrowlHelper.IsDllExists)
143 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
147 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
152 private void Setting_FormClosing(object sender, FormClosingEventArgs e)
154 if (MyCommon._endingFlag) return;
156 if (this.BasedPanel.AuthUserCombo.SelectedIndex == -1 && e.CloseReason == CloseReason.None)
158 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
163 if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
165 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
166 curPanel.Visible = false;
167 curPanel.Enabled = false;
171 private void Setting_Load(object sender, EventArgs e)
173 this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
174 this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
175 this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
176 this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
177 this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
178 this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
179 this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
180 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
181 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
182 this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
183 this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
184 this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
185 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
186 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
187 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
189 this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
190 this.TreeViewSetting.ExpandAll();
192 ActiveControl = BasedPanel.StartAuthButton;
195 private void UReadMng_CheckedChanged(object sender, EventArgs e)
197 if (this.ActionPanel.UReadMng.Checked == true)
199 this.StartupPanel.StartupReaded.Enabled = true;
203 this.StartupPanel.StartupReaded.Enabled = false;
207 private async void StartAuthButton_Click(object sender, EventArgs e)
209 using (ControlTransaction.Disabled(this.BasedPanel.StartAuthButton))
213 this.ApplyNetworkSettings();
215 var newAccount = await this.PinAuth();
216 if (newAccount == null)
219 var authUserCombo = this.BasedPanel.AuthUserCombo;
221 var oldAccount = authUserCombo.Items.Cast<UserAccount>()
222 .FirstOrDefault(x => x.UserId == newAccount.UserId);
225 if (oldAccount != null)
227 idx = authUserCombo.Items.IndexOf(oldAccount);
228 authUserCombo.Items[idx] = newAccount;
232 idx = authUserCombo.Items.Add(newAccount);
235 authUserCombo.SelectedIndex = idx;
237 MessageBox.Show(this, Properties.Resources.AuthorizeButton_Click1,
238 "Authenticate", MessageBoxButtons.OK);
240 catch (WebApiException ex)
242 var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
243 MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
249 /// 現在設定画面に入力されているネットワーク関係の設定を適用します
251 public void ApplyNetworkSettings()
254 if (this.ProxyPanel.RadioProxyNone.Checked)
255 proxyType = ProxyType.None;
256 else if (this.ProxyPanel.RadioProxyIE.Checked)
257 proxyType = ProxyType.IE;
259 proxyType = ProxyType.Specified;
261 var proxyAddress = this.ProxyPanel.TextProxyAddress.Text.Trim();
262 var proxyPort = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
263 var proxyUser = this.ProxyPanel.TextProxyUser.Text.Trim();
264 var proxyPassword = this.ProxyPanel.TextProxyPassword.Text.Trim();
265 Networking.SetWebProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
267 var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
268 Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
270 var uploadImageTimeout = int.Parse(this.ConnectionPanel.UploadImageTimeout.Text.Trim());
271 Networking.UploadImageTimeout = TimeSpan.FromSeconds(uploadImageTimeout);
273 Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
275 TwitterApiConnection.RestApiHost = this.ConnectionPanel.TwitterAPIText.Text.Trim();
278 private async Task<UserAccount> PinAuth()
280 var requestToken = await TwitterApiConnection.GetRequestTokenAsync();
282 var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);
284 var pin = AuthDialog.DoAuth(this, pinPageUrl);
285 if (string.IsNullOrEmpty(pin))
286 return null; // キャンセルされた場合
288 var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);
290 return new UserAccount
292 Username = accessTokenResponse["screen_name"],
293 UserId = long.Parse(accessTokenResponse["user_id"]),
294 Token = accessTokenResponse["oauth_token"],
295 TokenSecret = accessTokenResponse["oauth_token_secret"],
299 private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
300 => this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
302 private void Setting_Shown(object sender, EventArgs e)
307 if (this.Disposing || this.IsDisposed) return;
308 } while (!this.IsHandleCreated);
309 this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
311 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
312 this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
315 private void OpenUrl(string url)
318 var path = this.ActionPanel.BrowserPathText.Text;
321 if (!string.IsNullOrEmpty(path))
323 if (path.StartsWith("\"", StringComparison.Ordinal) && path.Length > 2 && path.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
325 var sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
326 var browserPath = path.Substring(1, sep - 1);
328 if (sep < path.Length - 1)
330 arg = path.Substring(sep + 1);
332 myPath = arg + " " + myPath;
333 System.Diagnostics.Process.Start(browserPath, myPath);
337 System.Diagnostics.Process.Start(path, myPath);
342 System.Diagnostics.Process.Start(myPath);
350 private void CreateAccountButton_Click(object sender, EventArgs e)
351 => this.OpenUrl("https://twitter.com/signup");
353 private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
354 => this.IntervalChanged?.Invoke(sender, e);
357 public class IntervalChangedEventArgs : EventArgs
359 public bool UserStream;
360 public bool Timeline;
362 public bool DirectMessage;
363 public bool PublicSearch;
365 public bool UserTimeline;
367 public static IntervalChangedEventArgs ResetAll => new IntervalChangedEventArgs
372 DirectMessage = true,