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;
52 private bool _ValidationError = false;
53 private long? InitialUserId;
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;
90 this.InitialUserId = activeUser.UserId;
94 public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
96 this.BasedPanel.SaveConfig(settingCommon);
97 this.GetPeriodPanel.SaveConfig(settingCommon);
98 this.StartupPanel.SaveConfig(settingCommon);
99 this.TweetPrvPanel.SaveConfig(settingCommon);
100 this.TweetActPanel.SaveConfig(settingCommon, settingLocal);
101 this.ActionPanel.SaveConfig(settingCommon, settingLocal);
102 this.FontPanel.SaveConfig(settingLocal);
103 this.FontPanel2.SaveConfig(settingLocal);
104 this.PreviewPanel.SaveConfig(settingCommon);
105 this.GetCountPanel.SaveConfig(settingCommon);
106 this.ShortUrlPanel.SaveConfig(settingCommon);
107 this.ProxyPanel.SaveConfig(settingLocal);
108 this.CooperatePanel.SaveConfig(settingCommon);
109 this.ConnectionPanel.SaveConfig(settingCommon);
110 this.NotifyPanel.SaveConfig(settingCommon);
112 var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
113 if (userAccountIdx != -1)
115 var u = settingCommon.UserAccounts[userAccountIdx];
116 this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
120 this.tw.VerifyCredentials();
121 u.UserId = this.tw.UserId;
126 this.tw.ClearAuthInfo();
127 this.tw.Initialize("", "", "", 0);
131 private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
133 if (this.TreeViewSetting.SelectedNode == null) return;
134 var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
135 if (pnl == null) return;
140 private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
142 if (e.Node == null) return;
143 var pnl = (SettingPanelBase)e.Node.Tag;
144 if (pnl == null) return;
148 if (pnl.Name == "PreviewPanel")
150 if (GrowlHelper.IsDllExists)
152 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
156 this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
161 private void Save_Click(object sender, EventArgs e)
163 if (MyCommon.IsNetworkAvailable() &&
164 (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp))
166 // bit.ly 短縮機能実装のプライバシー問題の暫定対応
167 // bit.ly 使用時はログインIDとAPIキーの指定を必須とする
168 // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
169 if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text))
171 MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName);
172 _ValidationError = true;
173 TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
174 TreeViewSetting.Select();
175 this.ShortUrlPanel.TextBitlyId.Focus();
179 if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
181 MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
182 _ValidationError = true;
183 TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
184 TreeViewSetting.Select();
185 this.ShortUrlPanel.TextBitlyId.Focus();
190 _ValidationError = false;
195 _ValidationError = false;
199 private void Setting_FormClosing(object sender, FormClosingEventArgs e)
201 if (MyCommon._endingFlag) return;
203 if (tw != null && string.IsNullOrEmpty(tw.Username) && e.CloseReason == CloseReason.None)
205 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
210 if (_ValidationError)
214 if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
216 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
217 curPanel.Visible = false;
218 curPanel.Enabled = false;
222 private void Setting_Load(object sender, EventArgs e)
224 this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
225 this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
226 this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
227 this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
228 this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
229 this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
230 this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
231 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
232 this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
233 this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
234 this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
235 this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
236 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
237 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
238 this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
240 this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
241 this.TreeViewSetting.ExpandAll();
243 ActiveControl = BasedPanel.StartAuthButton;
246 private void UReadMng_CheckedChanged(object sender, EventArgs e)
248 if (this.ActionPanel.UReadMng.Checked == true)
250 this.StartupPanel.StartupReaded.Enabled = true;
254 this.StartupPanel.StartupReaded.Enabled = false;
258 private bool StartAuth()
262 if (this.ProxyPanel.RadioProxyNone.Checked)
264 ptype = ProxyType.None;
266 else if (this.ProxyPanel.RadioProxyIE.Checked)
268 ptype = ProxyType.IE;
272 ptype = ProxyType.Specified;
274 string padr = this.ProxyPanel.TextProxyAddress.Text.Trim();
275 int pport = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
276 string pusr = this.ProxyPanel.TextProxyUser.Text.Trim();
277 string ppw = this.ProxyPanel.TextProxyPassword.Text.Trim();
280 Networking.DefaultTimeout = TimeSpan.FromSeconds(20);
281 Networking.SetWebProxy(ptype, padr, pport, pusr, ppw);
282 HttpTwitter.TwitterUrl = this.ConnectionPanel.TwitterAPIText.Text.Trim();
283 tw.Initialize("", "", "", 0);
284 string pinPageUrl = "";
285 string rslt = tw.StartAuthentication(ref pinPageUrl);
286 if (string.IsNullOrEmpty(rslt))
288 string pin = AuthDialog.DoAuth(this, pinPageUrl);
289 if (!string.IsNullOrEmpty(pin))
301 MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK);
306 private bool PinAuth()
308 string pin = this._pin; //PIN Code
310 string rslt = tw.Authenticate(pin);
311 if (string.IsNullOrEmpty(rslt))
313 MessageBox.Show(Properties.Resources.AuthorizeButton_Click1, "Authenticate", MessageBoxButtons.OK);
315 UserAccount user = new UserAccount();
316 user.Username = tw.Username;
317 user.UserId = tw.UserId;
318 user.Token = tw.AccessToken;
319 user.TokenSecret = tw.AccessTokenSecret;
321 foreach (object u in this.BasedPanel.AuthUserCombo.Items)
323 if (((UserAccount)u).Username.ToLower() == tw.Username.ToLower())
325 idx = this.BasedPanel.AuthUserCombo.Items.IndexOf(u);
331 this.BasedPanel.AuthUserCombo.Items.RemoveAt(idx);
332 this.BasedPanel.AuthUserCombo.Items.Insert(idx, user);
333 this.BasedPanel.AuthUserCombo.SelectedIndex = idx;
337 this.BasedPanel.AuthUserCombo.SelectedIndex = this.BasedPanel.AuthUserCombo.Items.Add(user);
343 MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK);
348 private void StartAuthButton_Click(object sender, EventArgs e)
358 private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
360 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled;
363 private void Setting_Shown(object sender, EventArgs e)
368 if (this.Disposing || this.IsDisposed) return;
369 } while (!this.IsHandleCreated);
370 this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
372 this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled;
373 this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamEnabled;
376 private bool BitlyValidation(string id, string apikey)
378 if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
383 string req = "http://api.bit.ly/v3/validate";
385 Dictionary<string, string> param = new Dictionary<string, string>();
387 param.Add("login", ApplicationSettings.BitlyLoginId);
388 param.Add("apiKey", ApplicationSettings.BitlyApiKey);
389 param.Add("x_login", id);
390 param.Add("x_apiKey", apikey);
391 param.Add("format", "txt");
393 if (!(new HttpVarious()).PostData(req, param, out content))
395 return true; // 通信エラーの場合はとりあえずチェックを通ったことにする
397 else if (content.Trim() == "1")
401 else if (content.Trim() == "0")
403 return false; // 検証失敗 APIキーとIDの組み合わせが違う
407 return true; // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする
411 private void Cancel_Click(object sender, EventArgs e)
413 _ValidationError = false;
416 private void OpenUrl(string url)
419 string path = this.ActionPanel.BrowserPathText.Text;
422 if (!string.IsNullOrEmpty(path))
424 if (path.StartsWith("\"") && path.Length > 2 && path.IndexOf("\"", 2) > -1)
426 int sep = path.IndexOf("\"", 2);
427 string browserPath = path.Substring(1, sep - 1);
429 if (sep < path.Length - 1)
431 arg = path.Substring(sep + 1);
433 myPath = arg + " " + myPath;
434 System.Diagnostics.Process.Start(browserPath, myPath);
438 System.Diagnostics.Process.Start(path, myPath);
443 System.Diagnostics.Process.Start(myPath);
451 private void CreateAccountButton_Click(object sender, EventArgs e)
453 this.OpenUrl("https://twitter.com/signup");
456 private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
458 if (this.IntervalChanged != null)
459 this.IntervalChanged(sender, e);
463 public class IntervalChangedEventArgs : EventArgs
465 public bool UserStream;
466 public bool Timeline;
468 public bool DirectMessage;
469 public bool PublicSearch;
471 public bool UserTimeline;