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;
34 using System.Windows.Forms;
35 using System.Threading;
37 using System.Resources;
39 using OpenTween.Connection;
40 using OpenTween.Thumbnail;
41 using System.Threading.Tasks;
42 using OpenTween.Setting.Panel;
46 public partial class AppendSettingDialog : OTBaseForm
48 public event EventHandler<IntervalChangedEventArgs> IntervalChanged;
51 internal TwitterApi twitterApi;
53 private bool _ValidationError = false;
55 public AppendSettingDialog()
57 this.InitializeComponent();
59 this.BasedPanel.StartAuthButton.Click += this.StartAuthButton_Click;
60 this.BasedPanel.CreateAccountButton.Click += this.CreateAccountButton_Click;
61 this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged;
62 this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged;
64 this.Icon = Properties.Resources.MIcon;
67 public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
69 this.BasedPanel.LoadConfig(settingCommon);
70 this.GetPeriodPanel.LoadConfig(settingCommon);
71 this.StartupPanel.LoadConfig(settingCommon);
72 this.TweetPrvPanel.LoadConfig(settingCommon);
73 this.TweetActPanel.LoadConfig(settingCommon, settingLocal);
74 this.ActionPanel.LoadConfig(settingCommon, settingLocal);
75 this.FontPanel.LoadConfig(settingLocal);
76 this.FontPanel2.LoadConfig(settingLocal);
77 this.PreviewPanel.LoadConfig(settingCommon);
78 this.GetCountPanel.LoadConfig(settingCommon);
79 this.ShortUrlPanel.LoadConfig(settingCommon);
80 this.ProxyPanel.LoadConfig(settingLocal);
81 this.CooperatePanel.LoadConfig(settingCommon);
82 this.ConnectionPanel.LoadConfig(settingCommon);
83 this.NotifyPanel.LoadConfig(settingCommon);
85 var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.tw.UserId);
86 if (activeUser != null)
88 this.BasedPanel.AuthUserCombo.SelectedItem = activeUser;
92 public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
94 this.BasedPanel.SaveConfig(settingCommon);
95 this.GetPeriodPanel.SaveConfig(settingCommon);
96 this.StartupPanel.SaveConfig(settingCommon);
97 this.TweetPrvPanel.SaveConfig(settingCommon);
98 this.TweetActPanel.SaveConfig(settingCommon, settingLocal);
99 this.ActionPanel.SaveConfig(settingCommon, settingLocal);
100 this.FontPanel.SaveConfig(settingLocal);
101 this.FontPanel2.SaveConfig(settingLocal);
102 this.PreviewPanel.SaveConfig(settingCommon);
103 this.GetCountPanel.SaveConfig(settingCommon);
104 this.ShortUrlPanel.SaveConfig(settingCommon);
105 this.ProxyPanel.SaveConfig(settingLocal);
106 this.CooperatePanel.SaveConfig(settingCommon);
107 this.ConnectionPanel.SaveConfig(settingCommon);
108 this.NotifyPanel.SaveConfig(settingCommon);
110 var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
111 if (userAccountIdx != -1)
113 var u = settingCommon.UserAccounts[userAccountIdx];
114 this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
118 this.tw.ClearAuthInfo();
119 this.tw.Initialize("", "", "", 0);
123 private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
125 if (this.TreeViewSetting.SelectedNode == null) return;
126 var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
127 if (pnl == null) return;
132 private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
134 if (e.Node == null) return;
135 var pnl = (SettingPanelBase)e.Node.Tag;
136 if (pnl == null) return;
140 if (pnl.Name == "PreviewPanel")
142 if (GrowlHelper.IsDllExists)
144 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
148 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
153 private void Save_Click(object sender, EventArgs e)
155 if (MyCommon.IsNetworkAvailable() &&
156 (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp))
158 // bit.ly 短縮機能実装のプライバシー問題の暫定対応
159 // bit.ly 使用時はログインIDとAPIキーの指定を必須とする
160 // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
161 if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text))
163 MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName);
164 _ValidationError = true;
165 TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
166 TreeViewSetting.Select();
167 this.ShortUrlPanel.TextBitlyId.Focus();
171 if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
173 MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
174 _ValidationError = true;
175 TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
176 TreeViewSetting.Select();
177 this.ShortUrlPanel.TextBitlyId.Focus();
182 _ValidationError = false;
187 _ValidationError = false;
191 private void Setting_FormClosing(object sender, FormClosingEventArgs e)
193 if (MyCommon._endingFlag) return;
195 if (this.BasedPanel.AuthUserCombo.SelectedIndex == -1 && e.CloseReason == CloseReason.None)
197 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
202 if (_ValidationError)
206 if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
208 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
209 curPanel.Visible = false;
210 curPanel.Enabled = false;
214 private void Setting_Load(object sender, EventArgs e)
216 this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
217 this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
218 this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
219 this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
220 this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
221 this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
222 this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
223 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
224 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
225 this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
226 this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
227 this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
228 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
229 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
230 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
232 this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
233 this.TreeViewSetting.ExpandAll();
235 ActiveControl = BasedPanel.StartAuthButton;
238 private void UReadMng_CheckedChanged(object sender, EventArgs e)
240 if (this.ActionPanel.UReadMng.Checked == true)
242 this.StartupPanel.StartupReaded.Enabled = true;
246 this.StartupPanel.StartupReaded.Enabled = false;
250 private async void StartAuthButton_Click(object sender, EventArgs e)
252 using (ControlTransaction.Disabled(this.BasedPanel.StartAuthButton))
256 this.ApplyNetworkSettings();
258 var newAccount = await this.PinAuth();
259 if (newAccount == null)
262 var authUserCombo = this.BasedPanel.AuthUserCombo;
264 var oldAccount = authUserCombo.Items.Cast<UserAccount>()
265 .FirstOrDefault(x => x.UserId == this.tw.UserId);
268 if (oldAccount != null)
270 idx = authUserCombo.Items.IndexOf(oldAccount);
271 authUserCombo.Items[idx] = newAccount;
275 idx = authUserCombo.Items.Add(newAccount);
278 authUserCombo.SelectedIndex = idx;
280 MessageBox.Show(this, Properties.Resources.AuthorizeButton_Click1,
281 "Authenticate", MessageBoxButtons.OK);
283 catch (WebApiException ex)
285 var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
286 MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
292 /// 現在設定画面に入力されているネットワーク関係の設定を適用します
294 public void ApplyNetworkSettings()
297 if (this.ProxyPanel.RadioProxyNone.Checked)
298 proxyType = ProxyType.None;
299 else if (this.ProxyPanel.RadioProxyIE.Checked)
300 proxyType = ProxyType.IE;
302 proxyType = ProxyType.Specified;
304 var proxyAddress = this.ProxyPanel.TextProxyAddress.Text.Trim();
305 var proxyPort = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
306 var proxyUser = this.ProxyPanel.TextProxyUser.Text.Trim();
307 var proxyPassword = this.ProxyPanel.TextProxyPassword.Text.Trim();
308 Networking.SetWebProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
310 var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
311 Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
313 Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
315 HttpTwitter.TwitterUrl = this.ConnectionPanel.TwitterAPIText.Text.Trim();
318 private async Task<UserAccount> PinAuth()
320 var requestToken = await TwitterApiConnection.GetRequestTokenAsync();
322 var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);
324 var pin = AuthDialog.DoAuth(this, pinPageUrl);
325 if (string.IsNullOrEmpty(pin))
326 return null; // キャンセルされた場合
328 var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);
330 return new UserAccount
332 Username = accessTokenResponse["screen_name"],
333 UserId = long.Parse(accessTokenResponse["user_id"]),
334 Token = accessTokenResponse["oauth_token"],
335 TokenSecret = accessTokenResponse["oauth_token_secret"],
339 private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
341 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
344 private void Setting_Shown(object sender, EventArgs e)
349 if (this.Disposing || this.IsDisposed) return;
350 } while (!this.IsHandleCreated);
351 this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
353 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
354 this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
357 private bool BitlyValidation(string id, string apikey)
359 if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
364 string req = "http://api.bit.ly/v3/validate";
366 Dictionary<string, string> param = new Dictionary<string, string>();
368 param.Add("login", ApplicationSettings.BitlyLoginId);
369 param.Add("apiKey", ApplicationSettings.BitlyApiKey);
370 param.Add("x_login", id);
371 param.Add("x_apiKey", apikey);
372 param.Add("format", "txt");
374 if (!(new HttpVarious()).PostData(req, param, out content))
376 return true; // 通信エラーの場合はとりあえずチェックを通ったことにする
378 else if (content.Trim() == "1")
382 else if (content.Trim() == "0")
384 return false; // 検証失敗 APIキーとIDの組み合わせが違う
388 return true; // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする
392 private void Cancel_Click(object sender, EventArgs e)
394 _ValidationError = false;
397 private void OpenUrl(string url)
400 string path = this.ActionPanel.BrowserPathText.Text;
403 if (!string.IsNullOrEmpty(path))
405 if (path.StartsWith("\"", StringComparison.Ordinal) && path.Length > 2 && path.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
407 int sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
408 string browserPath = path.Substring(1, sep - 1);
410 if (sep < path.Length - 1)
412 arg = path.Substring(sep + 1);
414 myPath = arg + " " + myPath;
415 System.Diagnostics.Process.Start(browserPath, myPath);
419 System.Diagnostics.Process.Start(path, myPath);
424 System.Diagnostics.Process.Start(myPath);
432 private void CreateAccountButton_Click(object sender, EventArgs e)
434 this.OpenUrl("https://twitter.com/signup");
437 private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
439 this.IntervalChanged?.Invoke(sender, e);
443 public class IntervalChangedEventArgs : EventArgs
445 public bool UserStream;
446 public bool Timeline;
448 public bool DirectMessage;
449 public bool PublicSearch;
451 public bool UserTimeline;
453 public static IntervalChangedEventArgs ResetAll
457 return new IntervalChangedEventArgs()
462 DirectMessage = true,