// OpenTween - Client of Twitter // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) // (c) 2008-2011 Moz (@syo68k) // (c) 2008-2011 takeshik (@takeshik) // (c) 2010-2011 anis774 (@anis774) // (c) 2010-2011 fantasticswallow (@f_swallow) // (c) 2011 kim_upsilon (@kim_upsilon) // All rights reserved. // // This file is part of OpenTween. // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 3 of the License, or (at your option) // any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License along // with this program. If not, see , or write to // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, // Boston, MA 02110-1301, USA. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; using System.Resources; using OpenTween.Api; using OpenTween.Connection; using OpenTween.Thumbnail; using System.Threading.Tasks; using OpenTween.Setting.Panel; namespace OpenTween { public partial class AppendSettingDialog : OTBaseForm { internal Twitter tw; private bool _ValidationError = false; private long? InitialUserId; public TwitterConfiguration TwitterConfiguration = TwitterConfiguration.DefaultConfiguration(); private string _pin; public event EventHandler IntervalChanged; public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal) { this.BasedPanel.LoadConfig(settingCommon); this.GetPeriodPanel.LoadConfig(settingCommon); this.StartupPanel.LoadConfig(settingCommon); this.TweetPrvPanel.LoadConfig(settingCommon); this.TweetActPanel.LoadConfig(settingCommon, settingLocal); this.ActionPanel.LoadConfig(settingCommon, settingLocal); this.FontPanel.LoadConfig(settingLocal); this.FontPanel2.LoadConfig(settingLocal); this.PreviewPanel.LoadConfig(settingCommon); this.GetCountPanel.LoadConfig(settingCommon); this.ShortUrlPanel.LoadConfig(settingCommon); this.ProxyPanel.LoadConfig(settingLocal); this.CooperatePanel.LoadConfig(settingCommon); this.ConnectionPanel.LoadConfig(settingCommon); this.NotifyPanel.LoadConfig(settingCommon); var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.tw.UserId); if (activeUser != null) { this.BasedPanel.AuthUserCombo.SelectedItem = activeUser; this.InitialUserId = activeUser.UserId; } } public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal) { this.BasedPanel.SaveConfig(settingCommon); this.GetPeriodPanel.SaveConfig(settingCommon); this.StartupPanel.SaveConfig(settingCommon); this.TweetPrvPanel.SaveConfig(settingCommon); this.TweetActPanel.SaveConfig(settingCommon, settingLocal); this.ActionPanel.SaveConfig(settingCommon, settingLocal); this.FontPanel.SaveConfig(settingLocal); this.FontPanel2.SaveConfig(settingLocal); this.PreviewPanel.SaveConfig(settingCommon); this.GetCountPanel.SaveConfig(settingCommon); this.ShortUrlPanel.SaveConfig(settingCommon); this.ProxyPanel.SaveConfig(settingLocal); this.CooperatePanel.SaveConfig(settingCommon); this.ConnectionPanel.SaveConfig(settingCommon); this.NotifyPanel.SaveConfig(settingCommon); var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex; if (userAccountIdx != -1) { var u = settingCommon.UserAccounts[userAccountIdx]; this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId); if (u.UserId == 0) { this.tw.VerifyCredentials(); u.UserId = this.tw.UserId; } } else { this.tw.ClearAuthInfo(); this.tw.Initialize("", "", "", 0); } } private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e) { if (this.TreeViewSetting.SelectedNode == null) return; var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag; if (pnl == null) return; pnl.Enabled = false; pnl.Visible = false; } private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node == null) return; var pnl = (SettingPanelBase)e.Node.Tag; if (pnl == null) return; pnl.Enabled = true; pnl.Visible = true; if (pnl.Name == "PreviewPanel") { if (GrowlHelper.IsDllExists) { this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true; } else { this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false; } } } private void Save_Click(object sender, EventArgs e) { if (MyCommon.IsNetworkAvailable() && (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp)) { // bit.ly 短縮機能実装のプライバシー問題の暫定対応 // bit.ly 使用時はログインIDとAPIキーの指定を必須とする // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text)) { MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName); _ValidationError = true; TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択 TreeViewSetting.Select(); this.ShortUrlPanel.TextBitlyId.Focus(); return; } if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text)) { MessageBox.Show(Properties.Resources.SettingSave_ClickText1); _ValidationError = true; TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択 TreeViewSetting.Select(); this.ShortUrlPanel.TextBitlyId.Focus(); return; } else { _ValidationError = false; } } else { _ValidationError = false; } #if UA //フォロー if (this.FollowCheckBox.Checked) { //現在の設定内容で通信 HttpConnection.ProxyType ptype; if (RadioProxyNone.Checked) { ptype = HttpConnection.ProxyType.None; } else if (RadioProxyIE.Checked) { ptype = HttpConnection.ProxyType.IE; } else { ptype = HttpConnection.ProxyType.Specified; } string padr = TextProxyAddress.Text.Trim(); int pport = int.Parse(TextProxyPort.Text.Trim()); string pusr = TextProxyUser.Text.Trim(); string ppw = TextProxyPassword.Text.Trim(); HttpConnection.InitializeConnection(20, ptype, padr, pport, pusr, ppw); string ret = tw.PostFollowCommand(ApplicationSettings.FeedbackTwitterName); } #endif } private void Setting_FormClosing(object sender, FormClosingEventArgs e) { if (MyCommon._endingFlag) return; if (tw != null && string.IsNullOrEmpty(tw.Username) && e.CloseReason == CloseReason.None) { if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { e.Cancel = true; } } if (_ValidationError) { e.Cancel = true; } if (e.Cancel == false && TreeViewSetting.SelectedNode != null) { var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag; curPanel.Visible = false; curPanel.Enabled = false; } } private void Setting_Load(object sender, EventArgs e) { this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel; this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel; this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel; this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel; //this.TreeViewSetting.Nodes["BasedNode"].Nodes["UserStreamNode"].Tag = UserStreamPanel; this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel; this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel; this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel; this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel; this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel; this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel; this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2; this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel; this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel; this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel; this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel; this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0]; this.TreeViewSetting.ExpandAll(); //TreeViewSetting.SelectedNode = TreeViewSetting.TopNode; ActiveControl = BasedPanel.StartAuthButton; } private void UReadMng_CheckedChanged(object sender, EventArgs e) { if (this.ActionPanel.UReadMng.Checked == true) { this.StartupPanel.StartupReaded.Enabled = true; } else { this.StartupPanel.StartupReaded.Enabled = false; } } public string RecommendStatusText { get; set; } private bool StartAuth() { //現在の設定内容で通信 ProxyType ptype; if (this.ProxyPanel.RadioProxyNone.Checked) { ptype = ProxyType.None; } else if (this.ProxyPanel.RadioProxyIE.Checked) { ptype = ProxyType.IE; } else { ptype = ProxyType.Specified; } string padr = this.ProxyPanel.TextProxyAddress.Text.Trim(); int pport = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim()); string pusr = this.ProxyPanel.TextProxyUser.Text.Trim(); string ppw = this.ProxyPanel.TextProxyPassword.Text.Trim(); //通信基底クラス初期化 Networking.DefaultTimeout = TimeSpan.FromSeconds(20); Networking.SetWebProxy(ptype, padr, pport, pusr, ppw); HttpTwitter.TwitterUrl = this.ConnectionPanel.TwitterAPIText.Text.Trim(); tw.Initialize("", "", "", 0); //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4; //this.AuthUserLabel.Text = ""; string pinPageUrl = ""; string rslt = tw.StartAuthentication(ref pinPageUrl); if (string.IsNullOrEmpty(rslt)) { string pin = AuthDialog.DoAuth(this, pinPageUrl); if (!string.IsNullOrEmpty(pin)) { this._pin = pin; return true; } else { return false; } } else { MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK); return false; } } private bool PinAuth() { string pin = this._pin; //PIN Code string rslt = tw.Authenticate(pin); if (string.IsNullOrEmpty(rslt)) { MessageBox.Show(Properties.Resources.AuthorizeButton_Click1, "Authenticate", MessageBoxButtons.OK); //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click3; //this.AuthUserLabel.Text = tw.Username; int idx = -1; UserAccount user = new UserAccount(); user.Username = tw.Username; user.UserId = tw.UserId; user.Token = tw.AccessToken; user.TokenSecret = tw.AccessTokenSecret; foreach (object u in this.BasedPanel.AuthUserCombo.Items) { if (((UserAccount)u).Username.ToLower() == tw.Username.ToLower()) { idx = this.BasedPanel.AuthUserCombo.Items.IndexOf(u); break; } } if (idx > -1) { this.BasedPanel.AuthUserCombo.Items.RemoveAt(idx); this.BasedPanel.AuthUserCombo.Items.Insert(idx, user); this.BasedPanel.AuthUserCombo.SelectedIndex = idx; } else { this.BasedPanel.AuthUserCombo.SelectedIndex = this.BasedPanel.AuthUserCombo.Items.Add(user); } //if (TwitterApiInfo.AccessLevel = ApiAccessLevel.ReadWrite) //{ // this.AuthStateLabel.Text += "(xAuth)"; //} //else if (TwitterApiInfo.AccessLevel == ApiAccessLevel.ReadWriteAndDirectMessage) //{ // this.AuthStateLabel.Text += "(OAuth)"; //} return true; } else { MessageBox.Show(Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + rslt, "Authenticate", MessageBoxButtons.OK); //this.AuthStateLabel.Text = Properties.Resources.AuthorizeButton_Click4; //this.AuthUserLabel.Text = ""; return false; } } private void StartAuthButton_Click(object sender, EventArgs e) { //this.Save.Enabled = false; if (StartAuth()) { if (PinAuth()) { //this.Save.Enabled = true; } } } private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e) { this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled; } private void Setting_Shown(object sender, EventArgs e) { do { Thread.Sleep(10); if (this.Disposing || this.IsDisposed) return; } while (!this.IsHandleCreated); this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked; this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamEnabled; this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamEnabled; } private bool BitlyValidation(string id, string apikey) { if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey)) { return false; } string req = "http://api.bit.ly/v3/validate"; string content = ""; Dictionary param = new Dictionary(); param.Add("login", ApplicationSettings.BitlyLoginId); param.Add("apiKey", ApplicationSettings.BitlyApiKey); param.Add("x_login", id); param.Add("x_apiKey", apikey); param.Add("format", "txt"); if (!(new HttpVarious()).PostData(req, param, out content)) { return true; // 通信エラーの場合はとりあえずチェックを通ったことにする } else if (content.Trim() == "1") { return true; // 検証成功 } else if (content.Trim() == "0") { return false; // 検証失敗 APIキーとIDの組み合わせが違う } else { return true; // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする } } private void Cancel_Click(object sender, EventArgs e) { _ValidationError = false; } //private void CheckEventNotify_CheckedChanged(object sender, EventArgs e) // Handles CheckEventNotify.CheckedChanged, CheckFavoritesEvent.CheckStateChanged, // CheckUnfavoritesEvent.CheckStateChanged, CheckFollowEvent.CheckStateChanged, // CheckListMemberAddedEvent.CheckStateChanged, CheckListMemberRemovedEvent.CheckStateChanged, // CheckListCreatedEvent.CheckStateChanged, CheckUserUpdateEvent.CheckStateChanged //{ // EventNotifyEnabled = CheckEventNotify.Checked; // GetEventNotifyFlag(EventNotifyFlag, IsMyEventNotifyFlag); // ApplyEventNotifyFlag(EventNotifyEnabled, EventNotifyFlag, IsMyEventNotifyFlag); //} //private void CheckForceEventNotify_CheckedChanged(object sender, EventArgs e) //{ // _MyForceEventNotify = CheckEventNotify.Checked; //} //private void CheckFavEventUnread_CheckedChanged(object sender, EventArgs e) //{ // _MyFavEventUnread = CheckFavEventUnread.Checked; //} //private void ComboBoxTranslateLanguage_SelectedIndexChanged(object sender, EventArgs e) //{ // _MyTranslateLanguage = (new Google()).GetLanguageEnumFromIndex(ComboBoxTranslateLanguage.SelectedIndex); //} //private void ComboBoxEventNotifySound_VisibleChanged(object sender, EventArgs e) //{ // SoundFileListup(); //} //private void ComboBoxEventNotifySound_SelectedIndexChanged(object sender, EventArgs e) //{ // if (_soundfileListup) return; // _MyEventSoundFile = (string)ComboBoxEventNotifySound.SelectedItem; //} private void OpenUrl(string url) { string myPath = url; string path = this.ActionPanel.BrowserPathText.Text; try { if (!string.IsNullOrEmpty(path)) { if (path.StartsWith("\"") && path.Length > 2 && path.IndexOf("\"", 2) > -1) { int sep = path.IndexOf("\"", 2); string browserPath = path.Substring(1, sep - 1); string arg = ""; if (sep < path.Length - 1) { arg = path.Substring(sep + 1); } myPath = arg + " " + myPath; System.Diagnostics.Process.Start(browserPath, myPath); } else { System.Diagnostics.Process.Start(path, myPath); } } else { System.Diagnostics.Process.Start(myPath); } } catch(Exception) { // MessageBox.Show("ブラウザの起動に失敗、またはタイムアウトしました。" + ex.ToString()); } } private void CreateAccountButton_Click(object sender, EventArgs e) { this.OpenUrl("https://twitter.com/signup"); } public AppendSettingDialog() { InitializeComponent(); this.BasedPanel.StartAuthButton.Click += this.StartAuthButton_Click; this.BasedPanel.CreateAccountButton.Click += this.CreateAccountButton_Click; this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged; this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged; this.Icon = Properties.Resources.MIcon; } private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e) { if (this.IntervalChanged != null) this.IntervalChanged(sender, e); } } public class IntervalChangedEventArgs : EventArgs { public bool UserStream; public bool Timeline; public bool Reply; public bool DirectMessage; public bool PublicSearch; public bool Lists; public bool UserTimeline; } }