OSDN Git Service

Twitter.Initialize内でTwitterApi.Initializeメソッドを呼び出す
[opentween/open-tween.git] / OpenTween / AppendSettingDialog.cs
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.
9 // 
10 // This file is part of OpenTween.
11 // 
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)
15 // any later version.
16 // 
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
20 // for more details. 
21 // 
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.
26
27 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Data;
31 using System.Drawing;
32 using System.Linq;
33 using System.Text;
34 using System.Windows.Forms;
35 using System.Threading;
36 using System.IO;
37 using System.Resources;
38 using OpenTween.Api;
39 using OpenTween.Connection;
40 using OpenTween.Thumbnail;
41 using System.Threading.Tasks;
42 using OpenTween.Setting.Panel;
43
44 namespace OpenTween
45 {
46     public partial class AppendSettingDialog : OTBaseForm
47     {
48         public event EventHandler<IntervalChangedEventArgs> IntervalChanged;
49
50         internal Twitter tw;
51         internal TwitterApi twitterApi;
52
53         private bool _ValidationError = false;
54
55         public AppendSettingDialog()
56         {
57             this.InitializeComponent();
58
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;
63
64             this.Icon = Properties.Resources.MIcon;
65         }
66
67         public void LoadConfig(SettingCommon settingCommon, SettingLocal settingLocal)
68         {
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);
84
85             var activeUser = settingCommon.UserAccounts.FirstOrDefault(x => x.UserId == this.tw.UserId);
86             if (activeUser != null)
87             {
88                 this.BasedPanel.AuthUserCombo.SelectedItem = activeUser;
89             }
90         }
91
92         public void SaveConfig(SettingCommon settingCommon, SettingLocal settingLocal)
93         {
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);
109
110             var userAccountIdx = this.BasedPanel.AuthUserCombo.SelectedIndex;
111             if (userAccountIdx != -1)
112             {
113                 var u = settingCommon.UserAccounts[userAccountIdx];
114                 this.tw.Initialize(u.Token, u.TokenSecret, u.Username, u.UserId);
115
116                 if (u.UserId == 0)
117                 {
118                     this.tw.VerifyCredentials();
119                     u.UserId = this.tw.UserId;
120                 }
121             }
122             else
123             {
124                 this.tw.ClearAuthInfo();
125                 this.tw.Initialize("", "", "", 0);
126             }
127         }
128
129         private void TreeViewSetting_BeforeSelect(object sender, TreeViewCancelEventArgs e)
130         {
131             if (this.TreeViewSetting.SelectedNode == null) return;
132             var pnl = (SettingPanelBase)this.TreeViewSetting.SelectedNode.Tag;
133             if (pnl == null) return;
134             pnl.Enabled = false;
135             pnl.Visible = false;
136         }
137
138         private void TreeViewSetting_AfterSelect(object sender, TreeViewEventArgs e)
139         {
140             if (e.Node == null) return;
141             var pnl = (SettingPanelBase)e.Node.Tag;
142             if (pnl == null) return;
143             pnl.Enabled = true;
144             pnl.Visible = true;
145
146             if (pnl.Name == "PreviewPanel")
147             {
148                 if (GrowlHelper.IsDllExists)
149                 {
150                     this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = true;
151                 }
152                 else
153                 {
154                     this.PreviewPanel.IsNotifyUseGrowlCheckBox.Enabled = false;
155                 }
156             }
157         }
158
159         private void Save_Click(object sender, EventArgs e)
160         {
161             if (MyCommon.IsNetworkAvailable() &&
162                 (this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Bitly || this.ShortUrlPanel.ComboBoxAutoShortUrlFirst.SelectedIndex == (int)MyCommon.UrlConverter.Jmp))
163             {
164                 // bit.ly 短縮機能実装のプライバシー問題の暫定対応
165                 // bit.ly 使用時はログインIDとAPIキーの指定を必須とする
166                 // 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
167                 if (string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyId.Text) || string.IsNullOrEmpty(this.ShortUrlPanel.TextBitlyPw.Text))
168                 {
169                     MessageBox.Show("bit.ly のログイン名とAPIキーの指定は必須項目です。", Application.ProductName);
170                     _ValidationError = true;
171                     TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
172                     TreeViewSetting.Select();
173                     this.ShortUrlPanel.TextBitlyId.Focus();
174                     return;
175                 }
176
177                 if (!BitlyValidation(this.ShortUrlPanel.TextBitlyId.Text, this.ShortUrlPanel.TextBitlyPw.Text))
178                 {
179                     MessageBox.Show(Properties.Resources.SettingSave_ClickText1);
180                     _ValidationError = true;
181                     TreeViewSetting.SelectedNode = TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"]; // 動作タブを選択
182                     TreeViewSetting.Select();
183                     this.ShortUrlPanel.TextBitlyId.Focus();
184                     return;
185                 }
186                 else
187                 {
188                     _ValidationError = false;
189                 }
190             }
191             else
192             {
193                 _ValidationError = false;
194             }
195         }
196
197         private void Setting_FormClosing(object sender, FormClosingEventArgs e)
198         {
199             if (MyCommon._endingFlag) return;
200
201             if (this.BasedPanel.AuthUserCombo.SelectedIndex == -1 && e.CloseReason == CloseReason.None)
202             {
203                 if (MessageBox.Show(Properties.Resources.Setting_FormClosing1, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
204                 {
205                     e.Cancel = true;
206                 }
207             }
208             if (_ValidationError)
209             {
210                 e.Cancel = true;
211             }
212             if (e.Cancel == false && TreeViewSetting.SelectedNode != null)
213             {
214                 var curPanel = (SettingPanelBase)TreeViewSetting.SelectedNode.Tag;
215                 curPanel.Visible = false;
216                 curPanel.Enabled = false;
217             }
218         }
219
220         private void Setting_Load(object sender, EventArgs e)
221         {
222             this.TreeViewSetting.Nodes["BasedNode"].Tag = BasedPanel;
223             this.TreeViewSetting.Nodes["BasedNode"].Nodes["PeriodNode"].Tag = GetPeriodPanel;
224             this.TreeViewSetting.Nodes["BasedNode"].Nodes["StartUpNode"].Tag = StartupPanel;
225             this.TreeViewSetting.Nodes["BasedNode"].Nodes["GetCountNode"].Tag = GetCountPanel;
226             this.TreeViewSetting.Nodes["ActionNode"].Tag = ActionPanel;
227             this.TreeViewSetting.Nodes["ActionNode"].Nodes["TweetActNode"].Tag = TweetActPanel;
228             this.TreeViewSetting.Nodes["PreviewNode"].Tag = PreviewPanel;
229             this.TreeViewSetting.Nodes["PreviewNode"].Nodes["TweetPrvNode"].Tag = TweetPrvPanel;
230             this.TreeViewSetting.Nodes["PreviewNode"].Nodes["NotifyNode"].Tag = NotifyPanel;
231             this.TreeViewSetting.Nodes["FontNode"].Tag = FontPanel;
232             this.TreeViewSetting.Nodes["FontNode"].Nodes["FontNode2"].Tag = FontPanel2;
233             this.TreeViewSetting.Nodes["ConnectionNode"].Tag = ConnectionPanel;
234             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ProxyNode"].Tag = ProxyPanel;
235             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["CooperateNode"].Tag = CooperatePanel;
236             this.TreeViewSetting.Nodes["ConnectionNode"].Nodes["ShortUrlNode"].Tag = ShortUrlPanel;
237
238             this.TreeViewSetting.SelectedNode = this.TreeViewSetting.Nodes[0];
239             this.TreeViewSetting.ExpandAll();
240
241             ActiveControl = BasedPanel.StartAuthButton;
242         }
243
244         private void UReadMng_CheckedChanged(object sender, EventArgs e)
245         {
246             if (this.ActionPanel.UReadMng.Checked == true)
247             {
248                 this.StartupPanel.StartupReaded.Enabled = true;
249             }
250             else
251             {
252                 this.StartupPanel.StartupReaded.Enabled = false;
253             }
254         }
255
256         private void StartAuthButton_Click(object sender, EventArgs e)
257         {
258             try
259             {
260                 this.ApplyNetworkSettings();
261
262                 var newAccount = this.PinAuth();
263                 if (newAccount == null)
264                     return;
265
266                 var authUserCombo = this.BasedPanel.AuthUserCombo;
267
268                 var oldAccount = authUserCombo.Items.Cast<UserAccount>()
269                     .FirstOrDefault(x => x.UserId == this.tw.UserId);
270
271                 int idx;
272                 if (oldAccount != null)
273                 {
274                     idx = authUserCombo.Items.IndexOf(oldAccount);
275                     authUserCombo.Items[idx] = newAccount;
276                 }
277                 else
278                 {
279                     idx = authUserCombo.Items.Add(newAccount);
280                 }
281
282                 authUserCombo.SelectedIndex = idx;
283
284                 MessageBox.Show(this, Properties.Resources.AuthorizeButton_Click1,
285                     "Authenticate", MessageBoxButtons.OK);
286             }
287             catch (WebApiException ex)
288             {
289                 var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine + ex.Message;
290                 MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
291             }
292         }
293
294         /// <summary>
295         /// 現在設定画面に入力されているネットワーク関係の設定を適用します
296         /// </summary>
297         public void ApplyNetworkSettings()
298         {
299             ProxyType proxyType;
300             if (this.ProxyPanel.RadioProxyNone.Checked)
301                 proxyType = ProxyType.None;
302             else if (this.ProxyPanel.RadioProxyIE.Checked)
303                 proxyType = ProxyType.IE;
304             else
305                 proxyType = ProxyType.Specified;
306
307             var proxyAddress = this.ProxyPanel.TextProxyAddress.Text.Trim();
308             var proxyPort = int.Parse(this.ProxyPanel.TextProxyPort.Text.Trim());
309             var proxyUser = this.ProxyPanel.TextProxyUser.Text.Trim();
310             var proxyPassword = this.ProxyPanel.TextProxyPassword.Text.Trim();
311             Networking.SetWebProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
312
313             var timeout = int.Parse(this.ConnectionPanel.ConnectionTimeOut.Text.Trim());
314             Networking.DefaultTimeout = TimeSpan.FromSeconds(timeout);
315
316             Networking.ForceIPv4 = this.ConnectionPanel.checkBoxForceIPv4.Checked;
317
318             HttpTwitter.TwitterUrl = this.ConnectionPanel.TwitterAPIText.Text.Trim();
319         }
320
321         private UserAccount PinAuth()
322         {
323             this.tw.Initialize("", "", "", 0);
324
325             var pinPageUrl = this.tw.StartAuthentication();
326
327             var pin = AuthDialog.DoAuth(this, pinPageUrl);
328             if (string.IsNullOrEmpty(pin))
329                 return null; // キャンセルされた場合
330
331             this.tw.Authenticate(pin);
332
333             return new UserAccount
334             {
335                 Username = this.tw.Username,
336                 UserId = this.tw.UserId,
337                 Token = this.tw.AccessToken,
338                 TokenSecret = this.tw.AccessTokenSecret,
339             };
340         }
341
342         private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
343         {
344             this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
345         }
346
347         private void Setting_Shown(object sender, EventArgs e)
348         {
349             do
350             {
351                 Thread.Sleep(10);
352                 if (this.Disposing || this.IsDisposed) return;
353             } while (!this.IsHandleCreated);
354             this.TopMost = this.PreviewPanel.CheckAlwaysTop.Checked;
355
356             this.GetPeriodPanel.LabelPostAndGet.Visible = this.GetPeriodPanel.CheckPostAndGet.Checked && !tw.UserStreamActive;
357             this.GetPeriodPanel.LabelUserStreamActive.Visible = tw.UserStreamActive;
358         }
359
360         private bool BitlyValidation(string id, string apikey)
361         {
362             if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(apikey))
363             {
364                 return false;
365             }
366
367             string req = "http://api.bit.ly/v3/validate";
368             string content = "";
369             Dictionary<string, string> param = new Dictionary<string, string>();
370
371             param.Add("login", ApplicationSettings.BitlyLoginId);
372             param.Add("apiKey", ApplicationSettings.BitlyApiKey);
373             param.Add("x_login", id);
374             param.Add("x_apiKey", apikey);
375             param.Add("format", "txt");
376
377             if (!(new HttpVarious()).PostData(req, param, out content))
378             {
379                 return true;             // 通信エラーの場合はとりあえずチェックを通ったことにする
380             }
381             else if (content.Trim() == "1")
382             {
383                 return true;             // 検証成功
384             }
385             else if (content.Trim() == "0")
386             {
387                 return false;            // 検証失敗 APIキーとIDの組み合わせが違う
388             }
389             else
390             {
391                 return true;             // 規定外応答:通信エラーの可能性があるためとりあえずチェックを通ったことにする
392             }
393         }
394
395         private void Cancel_Click(object sender, EventArgs e)
396         {
397             _ValidationError = false;
398         }
399
400         private void OpenUrl(string url)
401         {
402             string myPath = url;
403             string path = this.ActionPanel.BrowserPathText.Text;
404             try
405             {
406                 if (!string.IsNullOrEmpty(path))
407                 {
408                     if (path.StartsWith("\"", StringComparison.Ordinal) && path.Length > 2 && path.IndexOf("\"", 2, StringComparison.Ordinal) > -1)
409                     {
410                         int sep = path.IndexOf("\"", 2, StringComparison.Ordinal);
411                         string browserPath = path.Substring(1, sep - 1);
412                         string arg = "";
413                         if (sep < path.Length - 1)
414                         {
415                             arg = path.Substring(sep + 1);
416                         }
417                         myPath = arg + " " + myPath;
418                         System.Diagnostics.Process.Start(browserPath, myPath);
419                     }
420                     else
421                     {
422                         System.Diagnostics.Process.Start(path, myPath);
423                     }
424                 }
425                 else
426                 {
427                     System.Diagnostics.Process.Start(myPath);
428                 }
429             }
430             catch(Exception)
431             {
432             }
433         }
434
435         private void CreateAccountButton_Click(object sender, EventArgs e)
436         {
437             this.OpenUrl("https://twitter.com/signup");
438         }
439
440         private void GetPeriodPanel_IntervalChanged(object sender, IntervalChangedEventArgs e)
441         {
442             this.IntervalChanged?.Invoke(sender, e);
443         }
444     }
445
446     public class IntervalChangedEventArgs : EventArgs
447     {
448         public bool UserStream;
449         public bool Timeline;
450         public bool Reply;
451         public bool DirectMessage;
452         public bool PublicSearch;
453         public bool Lists;
454         public bool UserTimeline;
455
456         public static IntervalChangedEventArgs ResetAll
457         {
458             get
459             {
460                 return new IntervalChangedEventArgs()
461                 {
462                     UserStream = true,
463                     Timeline = true,
464                     Reply = true,
465                     DirectMessage = true,
466                     PublicSearch = true,
467                     Lists = true,
468                     UserTimeline = true,
469                 };
470             }
471         }
472     }
473 }